"GOT", but the "O" is a cute, smiling sun Index | Thread

From:
Josh Rickmar <openbsd+lists@zettaport.com>
Subject:
Disallow integrating into non-branch references
To:
gameoftrees@openbsd.org
Date:
Tue, 27 Sep 2022 10:48:11 -0400

Download raw body.

We want to prevent, for example, integrating into any refs/remotes/
reference.  Don't see a reason to allow this for anything other than
branches. 

-----------------------------------------------
commit 26d7aa89d29895b540232b141de257f2c9b895dc (integrate_into_nonbranch)
from: Josh Rickmar <jrick@zettaport.com>
date: Tue Sep 27 14:46:12 2022 UTC
 
 disallow integrating into references outside refs/heads/
 
 Spotted by stsp@ while considering a feature request for
 cmd_integrate.
 
diff e02bf8ada9fce6518a7ad40401425a26bdaadbb4 26d7aa89d29895b540232b141de257f2c9b895dc
commit - e02bf8ada9fce6518a7ad40401425a26bdaadbb4
commit + 26d7aa89d29895b540232b141de257f2c9b895dc
blob - 04f6245bfb7bfdc492c4818321900ff49e579289
blob + 2fe9e120a0bace5a7b80643b8da00a79f1462de8
--- got/got.c
+++ got/got.c
@@ -11961,6 +11961,12 @@ cmd_integrate(int argc, char *argv[])
 		    branch_ref, base_branch_ref);
 		goto done;
 	}
+	if (strncmp(base_refname, "refs/heads/", 11) != 0) {
+		error = got_error(GOT_ERR_INTEGRATE_BRANCH);
+		got_worktree_integrate_abort(worktree, fileindex, repo,
+		    branch_ref, base_branch_ref);
+		goto done;
+	}
 
 	error = got_ref_resolve(&commit_id, repo, branch_ref);
 	if (error)
blob - 81dce95fb636e4a9cb9a660616f6048f8403eb14
blob + 3ef43e397f0c76093e7db235561aa095240895e8
--- include/got_error.h
+++ include/got_error.h
@@ -174,6 +174,7 @@
 #define GOT_ERR_SIGNING_TAG	156
 #define GOT_ERR_COMMIT_REDUNDANT_AUTHOR 157
 #define GOT_ERR_BAD_QUERYSTRING	158
+#define GOT_ERR_INTEGRATE_BRANCH 159
 
 struct got_error {
         int code;
blob - 711554564cdd65bb927ad7bc3bebbbc253efd6ac
blob + c723f568d0bd96f1f5426bbe92a5ff499bad92f6
--- lib/error.c
+++ lib/error.c
@@ -223,6 +223,8 @@ static const struct got_error got_errors[] = {
 	{ GOT_ERR_COMMIT_REDUNDANT_AUTHOR, "specified author is equal to the "
 	    "default one"},
 	{ GOT_ERR_BAD_QUERYSTRING, "invalid query string" },
+	{ GOT_ERR_INTEGRATE_BRANCH, "will not integrate into a reference "
+	    "outside the \"refs/heads/\" reference namespace" },
 };
 
 static struct got_custom_error {
blob - 8c528ea1f5a368de063e916a5668b66b65392105
blob + a4561e55b71ef739aea26419efa37a02818e8885
--- regress/cmdline/integrate.sh
+++ regress/cmdline/integrate.sh
@@ -499,6 +499,51 @@ test_parseargs "$@"
 	test_done "$testroot" "$ret"
 }
 
+test_integrate_into_nonbranch() {
+	local testroot=`test_init test_integrate_into_nonbranch`
+
+	got checkout $testroot/repo $testroot/wt > /dev/null
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		echo "checkout failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	local commit=`git_show_head $testroot/repo`
+	(cd $testroot/repo && got ref -c $commit refs/remotes/origin/master)
+
+	echo "modified alpha on branch" > $testroot/repo/alpha
+	git_commit $testroot/repo -m "committing to alpha on master"
+
+	(cd $testroot/wt && got up -b origin/master > /dev/null)
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		echo "got branch failed unexpectedly"
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	(cd $testroot/wt && got integrate master \
+		> $testroot/stdout 2> $testroot/stderr)
+	ret=$?
+	if [ $ret -eq 0 ]; then
+		echo "got integrate succeeded unexpectedly"
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo -n "got: will not integrate into a reference outside the " \
+		> $testroot/stderr.expected
+	echo "\"refs/heads/\" reference namespace" >> $testroot/stderr.expected
+	cmp -s $testroot/stderr.expected $testroot/stderr
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+	fi
+	test_done "$testroot" "$ret"
+}
+
 test_parseargs "$@"
 run_test test_integrate_basic
 run_test test_integrate_requires_rebase_first
@@ -506,3 +551,4 @@ run_test test_integrate_replace_file_with_symlink
 run_test test_integrate_backwards_in_time
 run_test test_integrate_replace_symlink_with_file
 run_test test_integrate_replace_file_with_symlink
+run_test test_integrate_into_nonbranch