Commit 67ba61612157092fbe0f8c4a02b60faf31967061

Stefan Sperling 2022-04-08T09:14:02

leave work tree in a usable state after 'got rebase' fails path-prefix checks reported by naddy

diff --git a/got/got.c b/got/got.c
index d75debc..c317c15 100644
--- a/got/got.c
+++ b/got/got.c
@@ -9454,10 +9454,6 @@ cmd_rebase(int argc, char *argv[])
 			print_update_progress_stats(&upa);
 			goto done;
 		}
-		error = got_worktree_rebase_prepare(&new_base_branch,
-		    &tmp_branch, &fileindex, worktree, branch, repo);
-		if (error)
-			goto done;
 	}
 
 	commit_id = branch_head_commit_id;
@@ -9468,16 +9464,6 @@ cmd_rebase(int argc, char *argv[])
 	parent_ids = got_object_commit_get_parent_ids(commit);
 	pid = STAILQ_FIRST(parent_ids);
 	if (pid == NULL) {
-		if (!continue_rebase) {
-			error = got_worktree_rebase_abort(worktree, fileindex,
-			    repo, new_base_branch, abort_progress, &upa);
-			if (error)
-				goto done;
-			printf("Rebase of %s aborted\n",
-			    got_ref_get_name(branch));
-			print_merge_progress_stats(&upa);
-
-		}
 		error = got_error(GOT_ERR_EMPTY_REBASE);
 		goto done;
 	}
@@ -9489,6 +9475,13 @@ cmd_rebase(int argc, char *argv[])
 	if (error)
 		goto done;
 
+	if (!continue_rebase) {
+		error = got_worktree_rebase_prepare(&new_base_branch,
+		    &tmp_branch, &fileindex, worktree, branch, repo);
+		if (error)
+			goto done;
+	}
+
 	if (STAILQ_EMPTY(&commits)) {
 		if (continue_rebase) {
 			error = rebase_complete(worktree, fileindex,
diff --git a/regress/cmdline/rebase.sh b/regress/cmdline/rebase.sh
index 5e74b95..350e19c 100755
--- a/regress/cmdline/rebase.sh
+++ b/regress/cmdline/rebase.sh
@@ -788,6 +788,59 @@ test_rebase_path_prefix() {
 	ret=$?
 	if [ $ret -ne 0 ]; then
 		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	# rebase should succeed when using a complete work tree
+	got checkout $testroot/repo $testroot/wt2 > /dev/null
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	(cd $testroot/wt2 && got rebase newbranch \
+		> $testroot/stdout 2> $testroot/stderr)
+
+	(cd $testroot/repo && git checkout -q newbranch)
+	local new_commit1=`git_show_parent_commit $testroot/repo`
+	local new_commit2=`git_show_head $testroot/repo`
+
+	local short_orig_commit2=`trim_obj_id 28 $orig_commit2`
+	local short_new_commit2=`trim_obj_id 28 $new_commit2`
+
+	echo "G  gamma/delta" > $testroot/stdout.expected
+	echo -n "$short_orig_commit2 -> $short_new_commit2" \
+		>> $testroot/stdout.expected
+	echo ": committing to delta on newbranch" \
+		>> $testroot/stdout.expected
+	echo "Switching work tree to refs/heads/newbranch" \
+		>> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	# the first work tree should remain usable
+	(cd $testroot/wt && got update -b master \
+		> $testroot/stdout 2> $testroot/stderr)
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		echo "update failed unexpectedly" >&2
+		test_done "$testroot" "1"
+		return 1
+	fi
+
+	echo 'Already up-to-date' > $testroot/stdout.expected
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
 	fi
 	test_done "$testroot" "$ret"
 }
@@ -878,8 +931,7 @@ test_rebase_no_commits_to_rebase() {
 		return 1
 	fi
 
-	echo "Rebase of refs/heads/newbranch aborted" \
-		> $testroot/stdout.expected
+	echo -n > $testroot/stdout.expected
 	cmp -s $testroot/stdout.expected $testroot/stdout
 	ret=$?
 	if [ $ret -ne 0 ]; then