make 'got checkout' verify that specified branch and commit match
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
diff --git a/got/got.c b/got/got.c
index 3bdf531..bdb8179 100644
--- a/got/got.c
+++ b/got/got.c
@@ -522,6 +522,9 @@ cmd_checkout(int argc, char *argv[])
free(commit_id);
goto done;
}
+ error = check_same_branch(commit_id, head_ref, repo);
+ if (error)
+ goto done;
error = got_worktree_set_base_commit_id(worktree, repo,
commit_id);
free(commit_id);
diff --git a/regress/cmdline/checkout.sh b/regress/cmdline/checkout.sh
index 5ddd473..31e8151 100755
--- a/regress/cmdline/checkout.sh
+++ b/regress/cmdline/checkout.sh
@@ -90,5 +90,44 @@ function test_checkout_sets_xbit {
test_done "$testroot" "$ret"
}
+function test_checkout_commit_from_wrong_branch {
+ local testroot=`test_init checkout_commit_from_wrong_branch`
+
+ (cd $testroot/repo && git checkout -q -b newbranch)
+ echo "modified alpha on new branch" > $testroot/repo/alpha
+ git_commit $testroot/repo -m "modified alpha on new branch"
+
+ local head_rev=`git_show_head $testroot/repo`
+ got checkout -b master -c $head_rev $testroot/repo $testroot/wt \
+ > $testroot/stdout 2> $testroot/stderr
+ ret="$?"
+ if [ "$ret" == "0" ]; then
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ echo -n "" > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "got: target commit is on a different branch" \
+ > $testroot/stderr.expected
+ cmp -s $testroot/stderr.expected $testroot/stderr
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ test_done "$testroot" "$ret"
+}
+
run_test test_checkout_basic
run_test test_checkout_sets_xbit
+run_test test_checkout_commit_from_wrong_branch