difftool: allow running outside Git worktrees with --no-index
[gitweb.git] / builtin / am.c
index 3ee9a9d2a92aaa0e9716719640deaec7e260c811..4fb107a9d1bdf36ff4c71d761abdda4d50d0e24b 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Based on git-am.sh by Junio C Hamano.
  */
+#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
 #include "config.h"
 #include "builtin.h"
 #include "packfile.h"
 #include "repository.h"
 
-/**
- * Returns 1 if the file is empty or does not exist, 0 otherwise.
- */
-static int is_empty_file(const char *filename)
-{
-       struct stat st;
-
-       if (stat(filename, &st) < 0) {
-               if (errno == ENOENT)
-                       return 1;
-               die_errno(_("could not stat %s"), filename);
-       }
-
-       return !st.st_size;
-}
-
 /**
  * Returns the length of the first line of msg.
  */
@@ -260,32 +245,6 @@ static int read_state_file(struct strbuf *sb, const struct am_state *state,
        die_errno(_("could not read '%s'"), am_path(state, file));
 }
 
-/**
- * Take a series of KEY='VALUE' lines where VALUE part is
- * sq-quoted, and append <KEY, VALUE> at the end of the string list
- */
-static int parse_key_value_squoted(char *buf, struct string_list *list)
-{
-       while (*buf) {
-               struct string_list_item *item;
-               char *np;
-               char *cp = strchr(buf, '=');
-               if (!cp)
-                       return -1;
-               np = strchrnul(cp, '\n');
-               *cp++ = '\0';
-               item = string_list_append(list, buf);
-
-               buf = np + (*np == '\n');
-               *np = '\0';
-               cp = sq_dequote(cp);
-               if (!cp)
-                       return -1;
-               item->util = xstrdup(cp);
-       }
-       return 0;
-}
-
 /**
  * Reads and parses the state directory's "author-script" file, and sets
  * state->author_name, state->author_email and state->author_date accordingly.
@@ -302,42 +261,16 @@ static int parse_key_value_squoted(char *buf, struct string_list *list)
  * script, and thus if the file differs from what this function expects, it is
  * better to bail out than to do something that the user does not expect.
  */
-static int read_author_script(struct am_state *state)
+static int read_am_author_script(struct am_state *state)
 {
        const char *filename = am_path(state, "author-script");
-       struct strbuf buf = STRBUF_INIT;
-       struct string_list kv = STRING_LIST_INIT_DUP;
-       int retval = -1; /* assume failure */
-       int fd;
 
        assert(!state->author_name);
        assert(!state->author_email);
        assert(!state->author_date);
 
-       fd = open(filename, O_RDONLY);
-       if (fd < 0) {
-               if (errno == ENOENT)
-                       return 0;
-               die_errno(_("could not open '%s' for reading"), filename);
-       }
-       strbuf_read(&buf, fd, 0);
-       close(fd);
-       if (parse_key_value_squoted(buf.buf, &kv))
-               goto finish;
-
-       if (kv.nr != 3 ||
-           strcmp(kv.items[0].string, "GIT_AUTHOR_NAME") ||
-           strcmp(kv.items[1].string, "GIT_AUTHOR_EMAIL") ||
-           strcmp(kv.items[2].string, "GIT_AUTHOR_DATE"))
-               goto finish;
-       state->author_name = kv.items[0].util;
-       state->author_email = kv.items[1].util;
-       state->author_date = kv.items[2].util;
-       retval = 0;
-finish:
-       string_list_clear(&kv, !!retval);
-       strbuf_release(&buf);
-       return retval;
+       return read_author_script(filename, &state->author_name,
+                                 &state->author_email, &state->author_date, 1);
 }
 
 /**
@@ -411,7 +344,7 @@ static void am_load(struct am_state *state)
                BUG("state file 'last' does not exist");
        state->last = strtol(sb.buf, NULL, 10);
 
-       if (read_author_script(state) < 0)
+       if (read_am_author_script(state) < 0)
                die(_("could not parse author script"));
 
        read_commit_msg(state);
@@ -520,6 +453,7 @@ static int run_post_rewrite_hook(const struct am_state *state)
 
        cp.in = xopen(am_path(state, "rewritten"), O_RDONLY);
        cp.stdout_to_stderr = 1;
+       cp.trace2_hook_name = "post-rewrite";
 
        ret = run_command(&cp);
 
@@ -579,7 +513,7 @@ static int copy_notes_for_rebase(const struct am_state *state)
        }
 
 finish:
-       finish_copy_notes_for_rewrite(c, msg);
+       finish_copy_notes_for_rewrite(the_repository, c, msg);
        fclose(fp);
        strbuf_release(&sb);
        return ret;
@@ -1272,7 +1206,7 @@ static int parse_mail(struct am_state *state, const char *mail)
                goto finish;
        }
 
-       if (is_empty_file(am_path(state, "patch"))) {
+       if (is_empty_or_missing_file(am_path(state, "patch"))) {
                printf_ln(_("Patch is empty."));
                die_user_resolve(state);
        }
@@ -1597,7 +1531,7 @@ static int fall_back_threeway(const struct am_state *state, const char *index_pa
         * changes.
         */
 
-       init_merge_options(&o);
+       init_merge_options(&o, the_repository);
 
        o.branch1 = "HEAD";
        their_tree_name = xstrfmt("%.*s", linelen(state->msg), state->msg);
@@ -1646,6 +1580,7 @@ static void do_commit(const struct am_state *state)
        }
 
        author = fmt_ident(state->author_name, state->author_email,
+               WANT_AUTHOR_IDENT,
                        state->ignore_date ? NULL : state->author_date,
                        IDENT_STRICT);
 
@@ -1771,7 +1706,7 @@ static void am_run(struct am_state *state, int resume)
 
        refresh_and_write_cache();
 
-       if (index_has_changes(&the_index, NULL, &sb)) {
+       if (repo_index_has_changes(the_repository, NULL, &sb)) {
                write_state_bool(state, "dirtyindex", 1);
                die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
        }
@@ -1829,7 +1764,7 @@ static void am_run(struct am_state *state, int resume)
                         * the result may have produced the same tree as ours.
                         */
                        if (!apply_status &&
-                           !index_has_changes(&the_index, NULL, NULL)) {
+                           !repo_index_has_changes(the_repository, NULL, NULL)) {
                                say(state, stdout, _("No changes -- Patch already applied."));
                                goto next;
                        }
@@ -1855,7 +1790,7 @@ static void am_run(struct am_state *state, int resume)
                resume = 0;
        }
 
-       if (!is_empty_file(am_path(state, "rewritten"))) {
+       if (!is_empty_or_missing_file(am_path(state, "rewritten"))) {
                assert(state->rebasing);
                copy_notes_for_rebase(state);
                run_post_rewrite_hook(state);
@@ -1883,7 +1818,7 @@ static void am_resolve(struct am_state *state)
 
        say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
 
-       if (!index_has_changes(&the_index, NULL, NULL)) {
+       if (!repo_index_has_changes(the_repository, NULL, NULL)) {
                printf_ln(_("No changes - did you forget to use 'git add'?\n"
                        "If there is nothing left to stage, chances are that something else\n"
                        "already introduced the same changes; you might want to skip this patch."));
@@ -2022,7 +1957,7 @@ static int clean_index(const struct object_id *head, const struct object_id *rem
        if (merge_tree(remote_tree))
                return -1;
 
-       remove_branch_state();
+       remove_branch_state(the_repository);
 
        return 0;
 }
@@ -2033,7 +1968,7 @@ static int clean_index(const struct object_id *head, const struct object_id *rem
 static void am_rerere_clear(void)
 {
        struct string_list merge_rr = STRING_LIST_INIT_DUP;
-       rerere_clear(&merge_rr);
+       rerere_clear(the_repository, &merge_rr);
        string_list_clear(&merge_rr, 1);
 }
 
@@ -2052,6 +1987,15 @@ static void am_skip(struct am_state *state)
        if (clean_index(&head, &head))
                die(_("failed to clean index"));
 
+       if (state->rebasing) {
+               FILE *fp = xfopen(am_path(state, "rewritten"), "a");
+
+               assert(!is_null_oid(&state->orig_commit));
+               fprintf(fp, "%s ", oid_to_hex(&state->orig_commit));
+               fprintf(fp, "%s\n", oid_to_hex(&head));
+               fclose(fp);
+       }
+
        am_next(state);
        am_load(state);
        am_run(state, 0);
@@ -2165,7 +2109,9 @@ static int parse_opt_patchformat(const struct option *opt, const char *arg, int
 {
        int *opt_value = opt->value;
 
-       if (!strcmp(arg, "mbox"))
+       if (unset)
+               *opt_value = PATCH_FORMAT_UNKNOWN;
+       else if (!strcmp(arg, "mbox"))
                *opt_value = PATCH_FORMAT_MBOX;
        else if (!strcmp(arg, "stgit"))
                *opt_value = PATCH_FORMAT_STGIT;
@@ -2175,6 +2121,10 @@ static int parse_opt_patchformat(const struct option *opt, const char *arg, int
                *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;
@@ -2328,7 +2278,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
        /* Ensure a valid committer ident can be constructed */
        git_committer_info(IDENT_STRICT);
 
-       if (read_index_preload(&the_index, NULL, 0) < 0)
+       if (repo_read_index_preload(the_repository, NULL, 0) < 0)
                die(_("failed to read the index"));
 
        if (in_progress) {