Skip to content

Commit 1b90588

Browse files
zivarahgitster
authored andcommitted
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 <[email protected]> Helped-by: Junio C Hamano <[email protected]> Signed-off-by: Brian Lyles <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c282eba commit 1b90588

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

sequencer.c

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -772,29 +772,42 @@ static struct object_id *get_cache_tree_oid(struct index_state *istate)
772772
static int is_index_unchanged(struct repository *r)
773773
{
774774
struct object_id head_oid, *cache_tree_oid;
775+
const struct object_id *head_tree_oid;
775776
struct commit *head_commit;
776777
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);
777792

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;
782803

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+
}
793806

794807
if (!(cache_tree_oid = get_cache_tree_oid(istate)))
795808
return -1;
796809

797-
return oideq(cache_tree_oid, get_commit_tree_oid(head_commit));
810+
return oideq(cache_tree_oid, head_tree_oid);
798811
}
799812

800813
static int write_author_script(const char *message)

t/t3501-revert-cherry-pick.sh

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,19 @@ test_expect_success 'revert forbidden on dirty working tree' '
104104
'
105105

106106
test_expect_success 'cherry-pick on unborn branch' '
107-
git checkout --orphan unborn &&
107+
git switch --orphan unborn &&
108108
git rm --cached -r . &&
109-
rm -rf * &&
110109
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 &&
112120
test_cmp_rev ! initial HEAD
113121
'
114122

0 commit comments

Comments
 (0)