Commit Diff


commit - a367ff0fbac77bc991350a4463ee56e3cb0d9e7e
commit + 45d344f6a19dc0a91e021920cf6b3d0c5f4d0146
blob - 3bdf53187da1ea5a8532d5825fa083375dda1c7f
blob + bdb817999466a7bc0d119183955f7661c2de3db9
--- got/got.c
+++ 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);
blob - 5ddd473dfa87ec48c9de2af60e5d77fdf26a9842
blob + 31e8151d3cd31c2504b971a44a3dc1f1af79b5f2
--- regress/cmdline/checkout.sh
+++ 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