commit 0b36e980db09c545b0b35d08521d1fd6bb3f2218 from: Stefan Sperling via: Thomas Adam date: Tue Jan 31 15:41:39 2023 UTC make 'got rebase' work when the to-be-rebased branch has no parent commit found by and ok op@, who also provided the test case commit - 661072277fe516d8c1289af46b7ab82a1e524e87 commit + 0b36e980db09c545b0b35d08521d1fd6bb3f2218 blob - 49d3d67e1875e281a9eaa73a164a6fa090250833 blob + 588203b48b6e8f8a5a83dec5174179a920f20eb7 --- got/got.c +++ got/got.c @@ -11165,12 +11165,12 @@ cmd_rebase(int argc, char *argv[]) error = got_commit_graph_find_youngest_common_ancestor(&yca_id, base_commit_id, branch_head_commit_id, 1, repo, check_cancelled, NULL); - if (error) - goto done; - if (yca_id == NULL) { - error = got_error_msg(GOT_ERR_ANCESTRY, - "specified branch shares no common ancestry " - "with work tree's branch"); + if (error) { + if (error->code == GOT_ERR_ANCESTRY) { + error = got_error_msg(GOT_ERR_ANCESTRY, + "specified branch shares no common " + "ancestry with work tree's branch"); + } goto done; } @@ -11226,17 +11226,16 @@ cmd_rebase(int argc, char *argv[]) parent_ids = got_object_commit_get_parent_ids(commit); pid = STAILQ_FIRST(parent_ids); - if (pid == NULL) { - error = got_error(GOT_ERR_EMPTY_REBASE); - goto done; + if (pid) { + error = collect_commits(&commits, commit_id, &pid->id, + yca_id, got_worktree_get_path_prefix(worktree), + GOT_ERR_REBASE_PATH, repo); + if (error) + goto done; } - error = collect_commits(&commits, commit_id, &pid->id, - yca_id, got_worktree_get_path_prefix(worktree), - GOT_ERR_REBASE_PATH, repo); + got_object_commit_close(commit); commit = NULL; - if (error) - goto done; if (!continue_rebase) { error = got_worktree_rebase_prepare(&new_base_branch, blob - 05ee51ee95afd632964c6b89345f6d87c0b8104b blob + 3f1873d64f1c63ef2dc44534ddb9874a56c23447 --- include/got_error.h +++ include/got_error.h @@ -102,7 +102,7 @@ #define GOT_ERR_BRANCH_EXISTS 83 #define GOT_ERR_MODIFIED 84 #define GOT_ERR_NOT_REBASING 85 -#define GOT_ERR_EMPTY_REBASE 86 +/* 86 is currently unused */ #define GOT_ERR_REBASE_COMMITID 87 #define GOT_ERR_REBASING 88 #define GOT_ERR_REBASE_PATH 89 blob - e278a4e9d05d0dcd9a58a1200886f57cf2601bf8 blob + d12852060882bfaa4f9b35bbb149605727ce2f0a --- lib/error.c +++ lib/error.c @@ -138,7 +138,6 @@ static const struct got_error got_errors[] = { { GOT_ERR_MODIFIED, "work tree contains local changes; these " "changes must be committed or reverted first" }, { GOT_ERR_NOT_REBASING, "rebase operation not in progress" }, - { GOT_ERR_EMPTY_REBASE, "no commits to rebase" }, { GOT_ERR_REBASE_COMMITID,"rebase commit ID mismatch" }, { GOT_ERR_REBASING, "a rebase operation is in progress in this " "work tree and must be continued or aborted first" }, blob - 4692840131f7a20f520b5f57a637d5fcf715a4f9 blob + 076ea66b61f236b000343e68fc3d1b761ac0b442 --- regress/cmdline/rebase.sh +++ regress/cmdline/rebase.sh @@ -915,7 +915,11 @@ test_rebase_no_commits_to_rebase() { return 1 fi - (cd $testroot/wt && got branch -n newbranch) + # Create an unrelated branch with 'got import'. + mkdir -p $testroot/newtree + echo "new file" > $testroot/newtree/newfile + got import -m new -b newbranch -r $testroot/repo \ + $testroot/newtree > /dev/null echo "modified alpha on master" > $testroot/wt/alpha (cd $testroot/wt && got commit -m 'test rebase_no_commits_to_rebase' \ @@ -925,7 +929,9 @@ test_rebase_no_commits_to_rebase() { (cd $testroot/wt && got rebase newbranch > $testroot/stdout \ 2> $testroot/stderr) - echo "got: no commits to rebase" > $testroot/stderr.expected + echo -n "got: specified branch shares no common ancestry " \ + > $testroot/stderr.expected + echo "with work tree's branch" >> $testroot/stderr.expected cmp -s $testroot/stderr.expected $testroot/stderr ret=$? if [ $ret -ne 0 ]; then @@ -1886,6 +1892,47 @@ test_rebase_out_of_date2() { diff -u $testroot/stderr.expected $testroot/stderr fi test_done "$testroot" "$ret" +} + +test_rebase_one_commit() { + local testroot=`test_init rebase_one_commit` + + if ! got checkout $testroot/repo $testroot/wt >/dev/null; then + test_done "$testroot" 1 + return 1 + fi + + (cd $testroot/wt && got branch newbranch) >/dev/null + + echo "modified alpha on newbranch" >$testroot/wt/alpha + (cd $testroot/wt && got commit -m 'edit alpha') >/dev/null + (cd $testroot/wt && got update) >/dev/null + local commit=`git_show_branch_head $testroot/repo newbranch` + + echo -n '' > $testroot/stderr.expected + + (cd $testroot/wt && got rebase master >$testroot/stdout \ + 2> $testroot/stderr) + ret=$? + if [ $ret -ne 0 ]; then + echo "rebase comand failed unexpectedly" >&2 + diff -u $testroot/stderr.expected $testroot/stderr + test_done "$testroot" "1" + return 1 + fi + + echo "Forwarding refs/heads/master to commit $commit" \ + >$testroot/stdout.expected + echo "Switching work tree to refs/heads/master" \ + >> $testroot/stdout.expected + + if ! cmp -s $testroot/stdout.expected $testroot/stdout; then + diff -u $testroot/stdout.expected $testroot/stdout + test_done "$testroot" 1 + return 1 + fi + + test_done "$testroot" 0 } test_parseargs "$@" @@ -1909,3 +1956,4 @@ run_test test_rebase_no_author_info run_test test_rebase_nonbranch run_test test_rebase_umask run_test test_rebase_out_of_date2 +run_test test_rebase_one_commit