]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Merge branch 'jk/am-i-resolved-fix' into maint
authorJunio C Hamano <gitster@pobox.com>
Thu, 25 Jul 2019 21:27:09 +0000 (14:27 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 25 Jul 2019 21:27:09 +0000 (14:27 -0700)
"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

builtin/am.c
t/t4257-am-interactive.sh [new file with mode: 0755]

index 912d9821b18acfa0a4c1e3bed8e6c9e7b43dcdf8..78389d08b631f0281b7f315de0fb67f61c044d0f 100644 (file)
@@ -1339,9 +1339,10 @@ static void write_index_patch(const struct am_state *state)
        struct rev_info rev_info;
        FILE *fp;
 
-       if (!get_oid_tree("HEAD", &head))
-               tree = lookup_tree(the_repository, &head);
-       else
+       if (!get_oid("HEAD", &head)) {
+               struct commit *commit = lookup_commit_or_die(&head, "HEAD");
+               tree = get_commit_tree(commit);
+       } else
                tree = lookup_tree(the_repository,
                                   the_repository->hash_algo->empty_tree);
 
@@ -1643,11 +1644,8 @@ static int do_interactive(struct am_state *state)
 {
        assert(state->msg);
 
-       if (!isatty(0))
-               die(_("cannot be interactive without stdin connected to a terminal."));
-
        for (;;) {
-               const char *reply;
+               char reply[64];
 
                puts(_("Commit Body is:"));
                puts("--------------------------");
@@ -1659,11 +1657,11 @@ static int do_interactive(struct am_state *state)
                 * in your translation. The program will only accept English
                 * input at this point.
                 */
-               reply = git_prompt(_("Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all: "), PROMPT_ECHO);
+               printf(_("Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all: "));
+               if (!fgets(reply, sizeof(reply), stdin))
+                       die("unable to read from stdin; aborting");
 
-               if (!reply) {
-                       continue;
-               } else if (*reply == 'y' || *reply == 'Y') {
+               if (*reply == 'y' || *reply == 'Y') {
                        return 0;
                } else if (*reply == 'a' || *reply == 'A') {
                        state->interactive = 0;
@@ -2334,6 +2332,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
                                argv_array_push(&paths, mkpath("%s/%s", prefix, argv[i]));
                }
 
+               if (state.interactive && !paths.argc)
+                       die(_("interactive mode requires patches on the command line"));
+
                am_setup(&state, patch_format, paths.argv, keep_cr);
 
                argv_array_clear(&paths);
diff --git a/t/t4257-am-interactive.sh b/t/t4257-am-interactive.sh
new file mode 100755 (executable)
index 0000000..5344bd2
--- /dev/null
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+test_description='am --interactive tests'
+. ./test-lib.sh
+
+test_expect_success 'set up patches to apply' '
+       test_commit unrelated &&
+       test_commit no-conflict &&
+       test_commit conflict-patch file patch &&
+       git format-patch --stdout -2 >mbox &&
+
+       git reset --hard unrelated &&
+       test_commit conflict-master file master base
+'
+
+# Sanity check our setup.
+test_expect_success 'applying all patches generates conflict' '
+       test_must_fail git am mbox &&
+       echo resolved >file &&
+       git add -u &&
+       git am --resolved
+'
+
+test_expect_success 'interactive am can apply a single patch' '
+       git reset --hard base &&
+       # apply the first, but not the second
+       test_write_lines y n | git am -i mbox &&
+
+       echo no-conflict >expect &&
+       git log -1 --format=%s >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'interactive am can resolve conflict' '
+       git reset --hard base &&
+       # apply both; the second one will conflict
+       test_write_lines y y | test_must_fail git am -i mbox &&
+       echo resolved >file &&
+       git add -u &&
+       # interactive "--resolved" will ask us if we want to apply the result
+       echo y | git am -i --resolved &&
+
+       echo conflict-patch >expect &&
+       git log -1 --format=%s >actual &&
+       test_cmp expect actual &&
+
+       echo resolved >expect &&
+       git cat-file blob HEAD:file >actual &&
+       test_cmp expect actual
+'
+
+test_done