Merge branch 'jk/am-i-resolved-fix'
authorJunio C Hamano <gitster@pobox.com>
Mon, 17 Jun 2019 17:15:15 +0000 (10:15 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 17 Jun 2019 17:15:15 +0000 (10:15 -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

1  2 
builtin/am.c
diff --combined builtin/am.c
index 912d9821b18acfa0a4c1e3bed8e6c9e7b43dcdf8,d9199015f5fcc82c7b028a10c68b5ea67955827a..78389d08b631f0281b7f315de0fb67f61c044d0f
@@@ -453,7 -453,6 +453,7 @@@ static int run_post_rewrite_hook(const 
  
        cp.in = xopen(am_path(state, "rewritten"), O_RDONLY);
        cp.stdout_to_stderr = 1;
 +      cp.trace2_hook_name = "post-rewrite";
  
        ret = run_command(&cp);
  
@@@ -486,24 -485,23 +486,24 @@@ static int copy_notes_for_rebase(const 
  
        while (!strbuf_getline_lf(&sb, fp)) {
                struct object_id from_obj, to_obj;
 +              const char *p;
  
 -              if (sb.len != GIT_SHA1_HEXSZ * 2 + 1) {
 +              if (sb.len != the_hash_algo->hexsz * 2 + 1) {
                        ret = error(invalid_line, sb.buf);
                        goto finish;
                }
  
 -              if (get_oid_hex(sb.buf, &from_obj)) {
 +              if (parse_oid_hex(sb.buf, &from_obj, &p)) {
                        ret = error(invalid_line, sb.buf);
                        goto finish;
                }
  
 -              if (sb.buf[GIT_SHA1_HEXSZ] != ' ') {
 +              if (*p != ' ') {
                        ret = error(invalid_line, sb.buf);
                        goto finish;
                }
  
 -              if (get_oid_hex(sb.buf + GIT_SHA1_HEXSZ + 1, &to_obj)) {
 +              if (get_oid_hex(p + 1, &to_obj)) {
                        ret = error(invalid_line, sb.buf);
                        goto finish;
                }
@@@ -1339,9 -1337,10 +1339,10 @@@ static void write_index_patch(const str
        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);
  
@@@ -1502,11 -1501,11 +1503,11 @@@ static int fall_back_threeway(const str
                 * review them with extra care to spot mismerges.
                 */
                struct rev_info rev_info;
 -              const char *diff_filter_str = "--diff-filter=AM";
  
                repo_init_revisions(the_repository, &rev_info, NULL);
                rev_info.diffopt.output_format = DIFF_FORMAT_NAME_STATUS;
 -              diff_opt_parse(&rev_info.diffopt, &diff_filter_str, 1, rev_info.prefix);
 +              rev_info.diffopt.filter |= diff_filter_bit('A');
 +              rev_info.diffopt.filter |= diff_filter_bit('M');
                add_pending_oid(&rev_info, "HEAD", &our_tree, 0);
                diff_setup_done(&rev_info.diffopt);
                run_diff_index(&rev_info, 1);
@@@ -1581,7 -1580,6 +1582,7 @@@ static void do_commit(const struct am_s
        }
  
        author = fmt_ident(state->author_name, state->author_email,
 +              WANT_AUTHOR_IDENT,
                        state->ignore_date ? NULL : state->author_date,
                        IDENT_STRICT);
  
@@@ -1643,11 -1641,8 +1644,8 @@@ static int do_interactive(struct am_sta
  {
        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("--------------------------");
                 * 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;
@@@ -2122,10 -2117,6 +2120,10 @@@ static int parse_opt_patchformat(const 
                *opt_value = PATCH_FORMAT_HG;
        else if (!strcmp(arg, "mboxrd"))
                *opt_value = PATCH_FORMAT_MBOXRD;
 +      /*
 +       * Please update $__git_patchformat in git-completion.bash
 +       * when you add new options
 +       */
        else
                return error(_("Invalid value for --patch-format: %s"), arg);
        return 0;
@@@ -2334,6 -2325,9 +2332,9 @@@ int cmd_am(int argc, const char **argv
                                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);