Commit 6aeab5968f2b14a7aaff1360ae8f6a1679626ecb

Stefan Sperling 2019-08-28T14:51:42

fix deletion of branches stored in packed-refs broken since 29606af7a3a58767bf817a38035490899609d13e

diff --git a/lib/reference.c b/lib/reference.c
index c4723d5..c175b73 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -793,6 +793,7 @@ got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
 {
 	const struct got_error *err;
 	char *packed_refs_path, *path_refs = NULL;
+	const char *ondisk_ref_namespace = NULL;
 	FILE *f = NULL;
 	struct got_reference *ref;
 	struct got_reflist_entry *new;
@@ -814,8 +815,9 @@ got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
 			goto done;
 	}
 
+	ondisk_ref_namespace = ref_namespace;
 	if (ref_namespace && strncmp(ref_namespace, "refs/", 5) == 0)
-		ref_namespace += 5;
+		ondisk_ref_namespace += 5;
 
 	/* Gather on-disk refs before parsing packed-refs. */
 	free(path_refs);
@@ -825,7 +827,8 @@ got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
 		goto done;
 	}
 	err = gather_on_disk_refs(refs, path_refs,
-	    ref_namespace ? ref_namespace : "", repo, cmp_cb, cmp_arg);
+	    ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
+	    cmp_cb, cmp_arg);
 	if (err)
 		goto done;
 
diff --git a/regress/cmdline/branch.sh b/regress/cmdline/branch.sh
index 3b0f39a..1b883c4 100755
--- a/regress/cmdline/branch.sh
+++ b/regress/cmdline/branch.sh
@@ -248,6 +248,19 @@ function test_branch_delete {
 		return 1
 	fi
 
+	got ref -l -r $testroot/repo > $testroot/stdout
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
+	echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
 	got branch -d bogus_branch_name -r $testroot/repo \
 		> $testroot/stdout 2> $testroot/stderr
 	ret="$?"
@@ -292,7 +305,76 @@ function test_branch_delete_current_branch {
 	test_done "$testroot" "$ret"
 }
 
+function test_branch_delete_packed {
+	local testroot=`test_init branch_delete_packed`
+	local commit_id=`git_show_head $testroot/repo`
+
+	for b in branch1 branch2 branch3; do
+		got branch -r $testroot/repo $b
+		ret="$?"
+		if [ "$ret" != "0" ]; then
+			echo "got branch command failed unexpectedly"
+			test_done "$testroot" "$ret"
+			return 1
+		fi
+	done
+
+	(cd $testroot/repo && git pack-refs --all)
+
+	got branch -d branch2 -r $testroot/repo > $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got update command failed unexpectedly"
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got branch -l -r $testroot/repo > $testroot/stdout
+	echo "  branch1: $commit_id" > $testroot/stdout.expected
+	echo "  branch3: $commit_id" >> $testroot/stdout.expected
+	echo "  master: $commit_id" >> $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got ref -l -r $testroot/repo > $testroot/stdout
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
+	echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got branch -d bogus_branch_name -r $testroot/repo \
+		> $testroot/stdout 2> $testroot/stderr
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "got update succeeded unexpectedly"
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "got: reference refs/heads/bogus_branch_name not found" \
+		> $testroot/stderr.expected
+	cmp -s $testroot/stderr $testroot/stderr.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+	fi
+	test_done "$testroot" "$ret"
+}
+
 run_test test_branch_create
 run_test test_branch_list
 run_test test_branch_delete
 run_test test_branch_delete_current_branch
+run_test test_branch_delete_packed