Skip to content

Commit 7663e43

Browse files
peffgitster
authored andcommitted
am: fix --interactive HEAD tree resolution
In --interactive mode, "git am --resolved" will try to generate a patch based on what is in the index, so that it can prompt "apply this patch?". To do so it needs the tree of HEAD, which it tries to get with get_oid_tree(). However, this doesn't yield a tree object; the "tree" part just means "if you must disambiguate short oids, then prefer trees" (and we do not need to disambiguate at all, since we are feeding a ref). Instead, we must parse the oid as a commit (which should always be true in a non-corrupt repository), and access its tree pointer manually. This has been broken since the conversion to C in 7ff2683 (builtin-am: implement -i/--interactive, 2015-08-04), but there was no test coverage because of interactive-mode's insistence on having a tty. That was lifted in the previous commit, so we can now add a test for this case. Note that before this patch, the test would result in a BUG() which comes from 3506dc9 (has_uncommitted_changes(): fall back to empty tree, 2018-07-11). But before that, we'd have simply segfaulted (and in fact this is the exact type of case the BUG() added there was trying to catch!). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6e7baf2 commit 7663e43

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

builtin/am.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,9 +1337,10 @@ static void write_index_patch(const struct am_state *state)
13371337
struct rev_info rev_info;
13381338
FILE *fp;
13391339

1340-
if (!get_oid_tree("HEAD", &head))
1341-
tree = lookup_tree(the_repository, &head);
1342-
else
1340+
if (!get_oid("HEAD", &head)) {
1341+
struct commit *commit = lookup_commit_or_die(&head, "HEAD");
1342+
tree = get_commit_tree(commit);
1343+
} else
13431344
tree = lookup_tree(the_repository,
13441345
the_repository->hash_algo->empty_tree);
13451346

t/t4257-am-interactive.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/sh
2+
3+
test_description='am --interactive tests'
4+
. ./test-lib.sh
5+
6+
test_expect_success 'set up patches to apply' '
7+
test_commit unrelated &&
8+
test_commit no-conflict &&
9+
test_commit conflict-patch file patch &&
10+
git format-patch --stdout -2 >mbox &&
11+
12+
git reset --hard unrelated &&
13+
test_commit conflict-master file master base
14+
'
15+
16+
# Sanity check our setup.
17+
test_expect_success 'applying all patches generates conflict' '
18+
test_must_fail git am mbox &&
19+
echo resolved >file &&
20+
git add -u &&
21+
git am --resolved
22+
'
23+
24+
test_expect_success 'interactive am can apply a single patch' '
25+
git reset --hard base &&
26+
# apply the first, but not the second
27+
test_write_lines y n | git am -i mbox &&
28+
29+
echo no-conflict >expect &&
30+
git log -1 --format=%s >actual &&
31+
test_cmp expect actual
32+
'
33+
34+
test_expect_success 'interactive am can resolve conflict' '
35+
git reset --hard base &&
36+
# apply both; the second one will conflict
37+
test_write_lines y y | test_must_fail git am -i mbox &&
38+
echo resolved >file &&
39+
git add -u &&
40+
# interactive "--resolved" will ask us if we want to apply the result
41+
echo y | git am -i --resolved &&
42+
43+
echo conflict-patch >expect &&
44+
git log -1 --format=%s >actual &&
45+
test_cmp expect actual &&
46+
47+
echo resolved >expect &&
48+
git cat-file blob HEAD:file >actual &&
49+
test_cmp expect actual
50+
'
51+
52+
test_done

0 commit comments

Comments
 (0)