Skip to content

Commit 9b3897a

Browse files
committed
Merge branch 'jk/am-i-resolved-fix'
"git am -i --resolved" segfaulted after trying to see a commit as if it were a tree, which has been corrected. * jk/am-i-resolved-fix: am: fix --interactive HEAD tree resolution am: drop tty requirement for --interactive am: read interactive input from stdin am: simplify prompt response handling
2 parents 86d8730 + 7663e43 commit 9b3897a

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

builtin/am.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,9 +1339,10 @@ static void write_index_patch(const struct am_state *state)
13391339
struct rev_info rev_info;
13401340
FILE *fp;
13411341

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

@@ -1643,11 +1644,8 @@ static int do_interactive(struct am_state *state)
16431644
{
16441645
assert(state->msg);
16451646

1646-
if (!isatty(0))
1647-
die(_("cannot be interactive without stdin connected to a terminal."));
1648-
16491647
for (;;) {
1650-
const char *reply;
1648+
char reply[64];
16511649

16521650
puts(_("Commit Body is:"));
16531651
puts("--------------------------");
@@ -1659,11 +1657,11 @@ static int do_interactive(struct am_state *state)
16591657
* in your translation. The program will only accept English
16601658
* input at this point.
16611659
*/
1662-
reply = git_prompt(_("Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all: "), PROMPT_ECHO);
1660+
printf(_("Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all: "));
1661+
if (!fgets(reply, sizeof(reply), stdin))
1662+
die("unable to read from stdin; aborting");
16631663

1664-
if (!reply) {
1665-
continue;
1666-
} else if (*reply == 'y' || *reply == 'Y') {
1664+
if (*reply == 'y' || *reply == 'Y') {
16671665
return 0;
16681666
} else if (*reply == 'a' || *reply == 'A') {
16691667
state->interactive = 0;
@@ -2334,6 +2332,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
23342332
argv_array_push(&paths, mkpath("%s/%s", prefix, argv[i]));
23352333
}
23362334

2335+
if (state.interactive && !paths.argc)
2336+
die(_("interactive mode requires patches on the command line"));
2337+
23372338
am_setup(&state, patch_format, paths.argv, keep_cr);
23382339

23392340
argv_array_clear(&paths);

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)