Commit fc66b545cb13384f19d6637645eb2a01db32f763

Stefan Sperling 2019-08-12T14:45:02

fix behaviour of 'got rebase' with no commits to rebase; with semarie@

diff --git a/got/got.c b/got/got.c
index 486e9fe..24418f1 100644
--- a/got/got.c
+++ b/got/got.c
@@ -4268,6 +4268,20 @@ cmd_rebase(int argc, char *argv[])
 
 	parent_ids = got_object_commit_get_parent_ids(commit);
 	pid = SIMPLEQ_FIRST(parent_ids);
+	if (pid == NULL) {
+		if (!continue_rebase) {
+			int did_something;
+			error = got_worktree_rebase_abort(worktree, fileindex,
+			    repo, new_base_branch, update_progress,
+			    &did_something);
+			if (error)
+				goto done;
+			printf("Rebase of %s aborted\n",
+			    got_ref_get_name(branch));
+		}
+		error = got_error(GOT_ERR_EMPTY_REBASE);
+		goto done;
+	}
 	error = collect_commits(&commits, commit_id, pid->id,
 	    yca_id, got_worktree_get_path_prefix(worktree),
 	    GOT_ERR_REBASE_PATH, repo);
diff --git a/regress/cmdline/rebase.sh b/regress/cmdline/rebase.sh
index fb98c2f..0d64a54 100755
--- a/regress/cmdline/rebase.sh
+++ b/regress/cmdline/rebase.sh
@@ -714,6 +714,55 @@ function test_rebase_preserves_logmsg {
 	test_done "$testroot" "$ret"
 }
 
+function test_rebase_no_commits_to_rebase {
+	local testroot=`test_init rebase_no_commits_to_rebase`
+
+	got checkout $testroot/repo $testroot/wt > /dev/null
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	(cd $testroot/wt && got branch newbranch)
+
+	echo "modified alpha on master" > $testroot/wt/alpha
+	(cd $testroot/wt && got commit -m 'test rebase_no_commits_to_rebase' \
+		> /dev/null)
+	(cd $testroot/wt && got update > /dev/null)
+
+	(cd $testroot/wt && got rebase newbranch > $testroot/stdout \
+		2> $testroot/stderr)
+
+	echo "got: no commits to rebase" > $testroot/stderr.expected
+	cmp -s $testroot/stderr.expected $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "Rebase of refs/heads/newbranch aborted" \
+		> $testroot/stdout.expected
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	(cd $testroot/wt && got update > $testroot/stdout)
+	echo "Already up-to-date" > $testroot/stdout.expected
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+	fi
+	test_done "$testroot" "$ret"
+}
+
 run_test test_rebase_basic
 run_test test_rebase_ancestry_check
 run_test test_rebase_continue
@@ -722,3 +771,4 @@ run_test test_rebase_no_op_change
 run_test test_rebase_in_progress
 run_test test_rebase_path_prefix
 run_test test_rebase_preserves_logmsg
+run_test test_rebase_no_commits_to_rebase