Commit 9e822917b02cb36e8dd86a73a51ddfd57baa6c25

Stefan Sperling 2021-03-23T11:39:17

rebase/histedit -l: error out if no backups for the requested branch exist

diff --git a/got/got.c b/got/got.c
index 28c535e..5d1639c 100644
--- a/got/got.c
+++ b/got/got.c
@@ -7923,6 +7923,7 @@ list_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
 	char *branch_name = NULL;
 	struct got_commit_object *old_commit = NULL;
 	struct got_reflist_object_id_map *refs_idmap = NULL;
+	int wanted_branch_found = 0;
 
 	TAILQ_INIT(&refs);
 	TAILQ_INIT(&backup_refs);
@@ -7978,6 +7979,7 @@ list_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
 
 		if (wanted_branch_name == NULL ||
 		    strcmp(wanted_branch_name, branch_name) == 0) {
+			wanted_branch_found = 1;
 			err = print_backup_ref(branch_name, refname,
 			   old_commit_id, old_commit, refs_idmap, repo);
 			if (err)
@@ -7991,6 +7993,11 @@ list_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
 		got_object_commit_close(old_commit);
 		old_commit = NULL;
 	}
+
+	if (wanted_branch_name && !wanted_branch_found) {
+		err = got_error_fmt(GOT_ERR_NOT_REF,
+		    "%s/%s/", backup_ref_prefix, wanted_branch_name);
+	}
 done:
 	if (refs_idmap)
 		got_reflist_object_id_map_free(refs_idmap);
diff --git a/regress/cmdline/rebase.sh b/regress/cmdline/rebase.sh
index f972bec..432e94f 100755
--- a/regress/cmdline/rebase.sh
+++ b/regress/cmdline/rebase.sh
@@ -172,6 +172,25 @@ EOF
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+	fi
+
+	# Asking for backups of a branch which has none should yield an error
+	(cd $testroot/repo && got rebase -l master \
+		> $testroot/stdout 2> $testroot/stderr)
+	echo -n > $testroot/stdout.expected
+	echo "got: refs/got/backup/rebase/master/: no such reference found" \
+		> $testroot/stderr.expected
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+	fi
+	cmp -s $testroot/stderr.expected $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
 	fi
 	test_done "$testroot" "$ret"
 }