static void am_setup(struct am_state *state, enum patch_format patch_format,
const char **paths)
{
+ unsigned char curr_head[GIT_SHA1_RAWSZ];
+
if (!patch_format)
patch_format = detect_patch_format(paths);
die(_("Failed to split patches."));
}
+ if (!get_sha1("HEAD", curr_head)) {
+ write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(curr_head));
+ update_ref("am", "ORIG_HEAD", curr_head, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
+ } else {
+ write_file(am_path(state, "abort-safety"), 1, "%s", "");
+ delete_ref("ORIG_HEAD", NULL, 0);
+ }
+
/*
* NOTE: Since the "next" and "last" files determine if an am_state
* session is in progress, they should be written last.
*/
static void am_next(struct am_state *state)
{
+ unsigned char head[GIT_SHA1_RAWSZ];
+
free(state->author_name);
state->author_name = NULL;
unlink(am_path(state, "author-script"));
unlink(am_path(state, "final-commit"));
+ if (!get_sha1("HEAD", head))
+ write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(head));
+ else
+ write_file(am_path(state, "abort-safety"), 1, "%s", "");
+
state->cur++;
write_file(am_path(state, "next"), 1, "%d", state->cur);
}
const char *argv_gc_auto[] = {"gc", "--auto", NULL};
struct strbuf sb = STRBUF_INIT;
+ unlink(am_path(state, "dirtyindex"));
+
refresh_and_write_cache();
- if (index_has_changes(&sb))
+ if (index_has_changes(&sb)) {
+ write_file(am_path(state, "dirtyindex"), 1, "t");
die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
+ }
strbuf_release(&sb);
am_run(state, 0);
}
+/**
+ * Returns true if it is safe to reset HEAD to the ORIG_HEAD, false otherwise.
+ *
+ * It is not safe to reset HEAD when:
+ * 1. git-am previously failed because the index was dirty.
+ * 2. HEAD has moved since git-am previously failed.
+ */
+static int safe_to_abort(const struct am_state *state)
+{
+ struct strbuf sb = STRBUF_INIT;
+ unsigned char abort_safety[GIT_SHA1_RAWSZ], head[GIT_SHA1_RAWSZ];
+
+ if (file_exists(am_path(state, "dirtyindex")))
+ return 0;
+
+ if (read_state_file(&sb, state, "abort-safety", 1) > 0) {
+ if (get_sha1_hex(sb.buf, abort_safety))
+ die(_("could not parse %s"), am_path(state, "abort_safety"));
+ } else
+ hashclr(abort_safety);
+
+ if (get_sha1("HEAD", head))
+ hashclr(head);
+
+ if (!hashcmp(head, abort_safety))
+ return 1;
+
+ error(_("You seem to have moved HEAD since the last 'am' failure.\n"
+ "Not rewinding to ORIG_HEAD"));
+
+ return 0;
+}
+
+/**
+ * Aborts the current am session if it is safe to do so.
+ */
+static void am_abort(struct am_state *state)
+{
+ unsigned char curr_head[GIT_SHA1_RAWSZ], orig_head[GIT_SHA1_RAWSZ];
+ int has_curr_head, has_orig_head;
+ char *curr_branch;
+
+ if (!safe_to_abort(state)) {
+ am_destroy(state);
+ return;
+ }
+
+ curr_branch = resolve_refdup("HEAD", 0, curr_head, NULL);
+ has_curr_head = !is_null_sha1(curr_head);
+ if (!has_curr_head)
+ hashcpy(curr_head, EMPTY_TREE_SHA1_BIN);
+
+ has_orig_head = !get_sha1("ORIG_HEAD", orig_head);
+ if (!has_orig_head)
+ hashcpy(orig_head, EMPTY_TREE_SHA1_BIN);
+
+ clean_index(curr_head, orig_head);
+
+ if (has_orig_head)
+ update_ref("am --abort", "HEAD", orig_head,
+ has_curr_head ? curr_head : NULL, 0,
+ UPDATE_REFS_DIE_ON_ERR);
+ else if (curr_branch)
+ delete_ref(curr_branch, NULL, REF_NODEREF);
+
+ free(curr_branch);
+ am_destroy(state);
+}
+
/**
* parse_options() callback that validates and sets opt->value to the
* PATCH_FORMAT_* enum value corresponding to `arg`.
RESUME_FALSE = 0,
RESUME_APPLY,
RESUME_RESOLVED,
- RESUME_SKIP
+ RESUME_SKIP,
+ RESUME_ABORT
};
int cmd_am(int argc, const char **argv, const char *prefix)
const char * const usage[] = {
N_("git am [options] [(<mbox>|<Maildir>)...]"),
- N_("git am [options] (--continue | --skip)"),
+ N_("git am [options] (--continue | --skip | --abort)"),
NULL
};
OPT_CMDMODE(0, "skip", &resume,
N_("skip the current patch"),
RESUME_SKIP),
+ OPT_CMDMODE(0, "abort", &resume,
+ N_("restore the original branch and abort the patching operation."),
+ RESUME_ABORT),
OPT_END()
};
case RESUME_SKIP:
am_skip(&state);
break;
+ case RESUME_ABORT:
+ am_abort(&state);
+ break;
default:
die("BUG: invalid resume value");
}