switch branches during noop rebases Always update to the specified branch even if the branch is already rebased (no commits needed to be rebased, and the branch does not need a fast forward). With the old behavior of erroring and staying on the current branch, I sometimes found myself ignoring the error message, treating it as information and assuming the branch update, and later on committing on top of origin/main instead of the main branch. feedback and ok stsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
diff --git a/got/got.c b/got/got.c
index 488cd9f..79b1186 100644
--- a/got/got.c
+++ b/got/got.c
@@ -9147,12 +9147,41 @@ cmd_rebase(int argc, char *argv[])
goto done;
error = NULL;
} else {
- static char msg[128];
- snprintf(msg, sizeof(msg),
- "%s is already based on %s",
+ struct got_pathlist_head paths;
+ printf("%s is already based on %s\n",
got_ref_get_name(branch),
got_worktree_get_head_ref_name(worktree));
- error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
+ error = switch_head_ref(branch, branch_head_commit_id,
+ worktree, repo);
+ if (error)
+ goto done;
+ error = got_worktree_set_base_commit_id(worktree, repo,
+ branch_head_commit_id);
+ if (error)
+ goto done;
+ TAILQ_INIT(&paths);
+ error = got_pathlist_append(&paths, "", NULL);
+ if (error)
+ goto done;
+ error = got_worktree_checkout_files(worktree,
+ &paths, repo, update_progress, &upa,
+ check_cancelled, NULL);
+ got_pathlist_free(&paths);
+ if (error)
+ goto done;
+ if (upa.did_something) {
+ char *id_str;
+ error = got_object_id_str(&id_str,
+ branch_head_commit_id);
+ if (error)
+ goto done;
+ printf("Updated to %s: %s\n",
+ got_worktree_get_head_ref_name(worktree),
+ id_str);
+ free(id_str);
+ } else
+ printf("Already up-to-date\n");
+ print_update_progress_stats(&upa);
goto done;
}
error = got_worktree_rebase_prepare(&new_base_branch,
diff --git a/regress/cmdline/rebase.sh b/regress/cmdline/rebase.sh
index b17666e..c629d06 100755
--- a/regress/cmdline/rebase.sh
+++ b/regress/cmdline/rebase.sh
@@ -243,11 +243,18 @@ test_rebase_ancestry_check() {
(cd $testroot/repo && git checkout -q -b newbranch)
echo "modified delta on branch" > $testroot/repo/gamma/delta
git_commit $testroot/repo -m "committing to delta on newbranch"
+ local newbranch_id=`git_show_head $testroot/repo`
(cd $testroot/wt && got rebase newbranch > $testroot/stdout \
2> $testroot/stderr)
- echo -n > $testroot/stdout.expected
+ echo "refs/heads/newbranch is already based on refs/heads/master" \
+ > $testroot/stdout.expected
+ echo "Switching work tree from refs/heads/master to refs/heads/newbranch" \
+ >> $testroot/stdout.expected
+ echo "U gamma/delta" >> $testroot/stdout.expected
+ echo "Updated to refs/heads/newbranch: ${newbranch_id}" \
+ >> $testroot/stdout.expected
cmp -s $testroot/stdout.expected $testroot/stdout
ret="$?"
if [ "$ret" != "0" ]; then
@@ -256,8 +263,7 @@ test_rebase_ancestry_check() {
return 1
fi
- echo "got: refs/heads/newbranch is already based on refs/heads/master" \
- > $testroot/stderr.expected
+ echo -n > $testroot/stderr.expected
cmp -s $testroot/stderr.expected $testroot/stderr
ret="$?"
if [ "$ret" != "0" ]; then