sequencer: handle unborn branch with `--allow-empty`

When using git-cherry-pick(1) with `--allow-empty` while on an unborn branch, an error is thrown. This is inconsistent with the same cherry-pick when `--allow-empty` is not specified. Detect unborn branches in `is_index_unchanged`. When on an unborn branch, use the `empty_tree` as the tree to compare against. Add a new test to cover this scenario. While modelled off of the existing 'cherry-pick on unborn branch' test, some improvements can be made: - Use `git switch --orphan unborn` instead of `git checkout --orphan unborn` to avoid the need for a separate `rm -rf *` call - Avoid using `--quiet` in the `git diff` call to make debugging easier in the event of a failure. Use simply `--exit-code` instead. Make these improvements to the existing test as well as the new test. Helped-by: Phillip Wood <phillip.wood@dunelm.org.uk> Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Brian Lyles <brianmlyles@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brian Lyles committed Mar 25, 2024 at 18:16 UTC 1b90588d620a2970c82025c272e14f32d5e5969d
2 files changed +39 -18
sequencer.c
+28 -15
@@ -772,29 +772,42 @@ static struct object_id *get_cache_tree_oid(struct index_state *istate)
772 static int is_index_unchanged(struct repository *r)
773 {
774 struct object_id head_oid, *cache_tree_oid;
775 + const struct object_id *head_tree_oid;
776 struct commit *head_commit;
777 struct index_state *istate = r->index;
778 + const char *head_name;
779 +
780 + if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL)) {
781 + /* Check to see if this is an unborn branch */
782 + head_name = resolve_ref_unsafe("HEAD",
783 + RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
784 + &head_oid, NULL);
785 + if (!head_name ||
786 + !starts_with(head_name, "refs/heads/") ||
787 + !is_null_oid(&head_oid))
788 + return error(_("could not resolve HEAD commit"));
789 + head_tree_oid = the_hash_algo->empty_tree;
790 + } else {
791 + head_commit = lookup_commit(r, &head_oid);
792
778 - if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))
779 - return error(_("could not resolve HEAD commit"));
780 -
781 - head_commit = lookup_commit(r, &head_oid);
793 + /*
794 + * If head_commit is NULL, check_commit, called from
795 + * lookup_commit, would have indicated that head_commit is not
796 + * a commit object already. repo_parse_commit() will return failure
797 + * without further complaints in such a case. Otherwise, if
798 + * the commit is invalid, repo_parse_commit() will complain. So
799 + * there is nothing for us to say here. Just return failure.
800 + */
801 + if (repo_parse_commit(r, head_commit))
802 + return -1;
803
783 - /*
784 - * If head_commit is NULL, check_commit, called from
785 - * lookup_commit, would have indicated that head_commit is not
786 - * a commit object already. repo_parse_commit() will return failure
787 - * without further complaints in such a case. Otherwise, if
788 - * the commit is invalid, repo_parse_commit() will complain. So
789 - * there is nothing for us to say here. Just return failure.
790 - */
791 - if (repo_parse_commit(r, head_commit))
792 - return -1;
804 + head_tree_oid = get_commit_tree_oid(head_commit);
805 + }
806
807 if (!(cache_tree_oid = get_cache_tree_oid(istate)))
808 return -1;
809
797 - return oideq(cache_tree_oid, get_commit_tree_oid(head_commit));
810 + return oideq(cache_tree_oid, head_tree_oid);
811 }
812
813 static int write_author_script(const char *message)
t/t3501-revert-cherry-pick.sh
+11 -3
@@ -104,11 +104,19 @@ test_expect_success 'revert forbidden on dirty working tree' '
104 '
105
106 test_expect_success 'cherry-pick on unborn branch' '
107 - git checkout --orphan unborn &&
107 + git switch --orphan unborn &&
108 git rm --cached -r . &&
109 - rm -rf * &&
109 git cherry-pick initial &&
111 - git diff --quiet initial &&
110 + git diff --exit-code initial &&
111 + test_cmp_rev ! initial HEAD
112 +'
113 +
114 +test_expect_success 'cherry-pick on unborn branch with --allow-empty' '
115 + git checkout --detach &&
116 + git branch -D unborn &&
117 + git switch --orphan unborn &&
118 + git cherry-pick initial --allow-empty &&
119 + git diff --exit-code initial &&
120 test_cmp_rev ! initial HEAD
121 '
122