Commit Diff


commit - eff4b972377331c3faf693c81c0b90e0aa8fe60664709c4e3697d427e51a0232
commit + 5d42d6ba704c3fb69f59c0b1f486ebaa6a40cad3baaa50b40331a5cfd5f0a0ac
blob - 44e185c805c00bc483846dacb510f32f6ee90967b970e8f824c081d763c53bca
blob + d947ec4d2454e9ad65cc2f69da19ec9baf3b3e0ec57548058fe30feff0d9ee2e
--- gotd/repo_write.c	
+++ gotd/repo_write.c	
@@ -1565,8 +1565,9 @@ protect_refs_from_moving(void)
 
 		RB_FOREACH(pe, got_pathlist_head,
 		    &repo_write.protected_branch_namespaces) {
-			if (strcmp(pe->path, refname) != 0)
-				continue;
+			err = protect_ref_namespace(refname, pe->path);
+			if (err)
+				return err;
 
 			err = protect_require_yca(&ref_update->new_id, 0,
 			    NULL, NULL, ref_update->ref);
blob - 1dff288b47b2ab0566b3f581f21411fb8f64760e2820dadb66165f5ca9aad659
blob + 746d51df23e4b646a611c3b3301e17c9da972f57816ef21cec2a112b1c8bd923
--- regress/gotd/Makefile	
+++ regress/gotd/Makefile	
@@ -176,6 +176,7 @@ start_gotd_rw_protected: ensure_root
 	@echo '    permit rw $(GOTD_DEVUSER)' >> $(PWD)/gotd.conf
 	@echo '    protect branch foo' >> $(PWD)/gotd.conf
 	@echo '    protect tag namespace "refs/tags/"' >> $(PWD)/gotd.conf
+	@echo '    protect branch namespace "refs/heads/protected/"' >> $(PWD)/gotd.conf
 	@echo '    protect { branch refs/heads/main }' >> $(PWD)/gotd.conf
 	@echo "}" >> $(PWD)/gotd.conf
 	@$(GOTD_TRAP); $(GOTD_START_CMD)
blob - 08e82bd3923554c4117072c3b03ea890b8a087dcc4dfa7bf28f11a21d6daa8ec
blob + 9192271b98ef137925eca9e7404d9b277ef206647d7093e3a969fd6818666e38
--- regress/gotd/repo_write_protected.sh	
+++ regress/gotd/repo_write_protected.sh	
@@ -354,8 +354,95 @@ test_modify_protected_branch() {
 	test_done "$testroot" $ret
 }
 
+test_modify_protected_branch_namespace() {
+	local testroot=`test_init modify_protected_branch_namespace`
+
+	got clone -a -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		echo "got clone failed unexpectedly" >&2
+		test_done "$testroot" 1
+		return 1
+	fi
+
+	local commit_id=`git_show_head $testroot/repo-clone`
+
+	got branch -r $testroot/repo-clone -c $commit_id protected/foo
+
+	got checkout -b protected/foo $testroot/repo-clone $testroot/wt >/dev/null
+	echo change file alpha in one way > $testroot/wt/alpha
+	(cd $testroot/wt && got commit -m 'change alpha on a new branch' > /dev/null)
+
+	# Creating a new branch should succeed.
+	got send -q -r $testroot/repo-clone -b protected/foo 2> $testroot/stderr
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		echo "got send failed unexpectedly" >&2
+		test_done "$testroot" 1
+		return 1
+	fi
+
+	# Forcefully rewinding an existing branch should fail.
+	(cd $testroot/wt && got update -c:head:-1 > /dev/null)
+	(cd $testroot/wt && got histedit -d > /dev/null)
+	got send -q -r $testroot/repo-clone -b protected/foo -f \
+		2> $testroot/stderr
+	ret=$?
+	if [ $ret -eq 0 ]; then
+		echo "got send succeeded unexpectedly" >&2
+		test_done "$testroot" 1
+		return 1
+	fi
+
+	grep ^gotsh: $testroot/stderr > $testroot/stderr.filtered
+	cat > $testroot/stderr.expected <<EOF
+gotsh: refs/heads/protected/: reference namespace is protected
+EOF
+	cmp -s $testroot/stderr.expected $testroot/stderr.filtered
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" 1
+		return 1
+	fi
+
+	# Overwriting an existing branch with different history should fail.
+	got branch -r $testroot/repo-clone -d refs/protected/foo > /dev/null
+	got branch -r $testroot/repo-clone -c $commit_id protected/foo
+
+	got checkout -b protected/foo $testroot/repo-clone $testroot/wt2 \
+		> /dev/null
+	echo change file alpha in another way > $testroot/wt2/alpha
+	(cd $testroot/wt2 && got commit -m 'change alpha on another branch' \
+		> /dev/null)
+
+	got send -q -f -r $testroot/repo-clone -b protected/foo \
+		2> $testroot/stderr
+	ret=$?
+	if [ $ret -eq 0 ]; then
+		echo "got send succeeded unexpectedly" >&2
+		test_done "$testroot" 1
+		return 1
+	fi
+
+	grep ^gotsh: $testroot/stderr > $testroot/stderr.filtered
+	cat > $testroot/stderr.expected <<EOF
+gotsh: refs/heads/protected/foo: reference is protected
+EOF
+	cmp -s $testroot/stderr.expected $testroot/stderr.filtered
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" 1
+		return 1
+	fi
+
+	test_done "$testroot" 0
+}
+
 test_parseargs "$@"
 run_test test_create_protected_branch
 run_test test_modify_protected_tag_namespace
 run_test test_delete_protected_branch
 run_test test_modify_protected_branch
+run_test test_modify_protected_branch_namespace