builtin-am: implement --abort
Since 3e5057a (git am --abort, 2008-07-16), git-am supported the --abort option that will rewind HEAD back to the original commit. Re-implement this through am_abort(). Since 7b3b7e3 (am --abort: keep unrelated commits since the last failure and warn, 2010-12-21), to prevent commits made since the last failure from being lost, git-am will not rewind HEAD back to the original commit if HEAD moved since the last failure. Re-implement this through safe_to_abort(). Helped-by: Stefan Beller <sbeller@google.com> Signed-off-by: Paul Tan <pyokagan@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Paul Tan committed
Aug 4, 2015 at 21:51 UTC
33388a71d23e6a296eb33879e418e857444f2d74
1 file changed
+100
-3
builtin/am.c
+100
-3
@@ -490,6 +490,8 @@ static int split_mail(struct am_state *state, enum patch_format patch_format,
490
static void am_setup(struct am_state *state, enum patch_format patch_format,
491
const char **paths)
492
{
493
+ unsigned char curr_head[GIT_SHA1_RAWSZ];
494
+
495
if (!patch_format)
496
patch_format = detect_patch_format(paths);
497
@@ -506,6 +508,14 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
508
die(_("Failed to split patches."));
509
}
510
511
+ if (!get_sha1("HEAD", curr_head)) {
512
+ write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(curr_head));
513
+ update_ref("am", "ORIG_HEAD", curr_head, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
514
+ } else {
515
+ write_file(am_path(state, "abort-safety"), 1, "%s", "");
516
+ delete_ref("ORIG_HEAD", NULL, 0);
517
+ }
518
+
519
/*
520
* NOTE: Since the "next" and "last" files determine if an am_state
521
* session is in progress, they should be written last.
@@ -522,6 +532,8 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
532
*/
533
static void am_next(struct am_state *state)
534
{
535
+ unsigned char head[GIT_SHA1_RAWSZ];
536
+
537
free(state->author_name);
538
state->author_name = NULL;
539
@@ -538,6 +550,11 @@ static void am_next(struct am_state *state)
550
unlink(am_path(state, "author-script"));
551
unlink(am_path(state, "final-commit"));
552
553
+ if (!get_sha1("HEAD", head))
554
+ write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(head));
555
+ else
556
+ write_file(am_path(state, "abort-safety"), 1, "%s", "");
557
+
558
state->cur++;
559
write_file(am_path(state, "next"), 1, "%d", state->cur);
560
}
@@ -788,10 +805,14 @@ static void am_run(struct am_state *state, int resume)
805
const char *argv_gc_auto[] = {"gc", "--auto", NULL};
806
struct strbuf sb = STRBUF_INIT;
807
808
+ unlink(am_path(state, "dirtyindex"));
809
+
810
refresh_and_write_cache();
811
793
- if (index_has_changes(&sb))
812
+ if (index_has_changes(&sb)) {
813
+ write_file(am_path(state, "dirtyindex"), 1, "t");
814
die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
815
+ }
816
817
strbuf_release(&sb);
818
@@ -979,6 +1000,75 @@ static void am_skip(struct am_state *state)
1000
am_run(state, 0);
1001
}
1002
1003
+/**
1004
+ * Returns true if it is safe to reset HEAD to the ORIG_HEAD, false otherwise.
1005
+ *
1006
+ * It is not safe to reset HEAD when:
1007
+ * 1. git-am previously failed because the index was dirty.
1008
+ * 2. HEAD has moved since git-am previously failed.
1009
+ */
1010
+static int safe_to_abort(const struct am_state *state)
1011
+{
1012
+ struct strbuf sb = STRBUF_INIT;
1013
+ unsigned char abort_safety[GIT_SHA1_RAWSZ], head[GIT_SHA1_RAWSZ];
1014
+
1015
+ if (file_exists(am_path(state, "dirtyindex")))
1016
+ return 0;
1017
+
1018
+ if (read_state_file(&sb, state, "abort-safety", 1) > 0) {
1019
+ if (get_sha1_hex(sb.buf, abort_safety))
1020
+ die(_("could not parse %s"), am_path(state, "abort_safety"));
1021
+ } else
1022
+ hashclr(abort_safety);
1023
+
1024
+ if (get_sha1("HEAD", head))
1025
+ hashclr(head);
1026
+
1027
+ if (!hashcmp(head, abort_safety))
1028
+ return 1;
1029
+
1030
+ error(_("You seem to have moved HEAD since the last 'am' failure.\n"
1031
+ "Not rewinding to ORIG_HEAD"));
1032
+
1033
+ return 0;
1034
+}
1035
+
1036
+/**
1037
+ * Aborts the current am session if it is safe to do so.
1038
+ */
1039
+static void am_abort(struct am_state *state)
1040
+{
1041
+ unsigned char curr_head[GIT_SHA1_RAWSZ], orig_head[GIT_SHA1_RAWSZ];
1042
+ int has_curr_head, has_orig_head;
1043
+ char *curr_branch;
1044
+
1045
+ if (!safe_to_abort(state)) {
1046
+ am_destroy(state);
1047
+ return;
1048
+ }
1049
+
1050
+ curr_branch = resolve_refdup("HEAD", 0, curr_head, NULL);
1051
+ has_curr_head = !is_null_sha1(curr_head);
1052
+ if (!has_curr_head)
1053
+ hashcpy(curr_head, EMPTY_TREE_SHA1_BIN);
1054
+
1055
+ has_orig_head = !get_sha1("ORIG_HEAD", orig_head);
1056
+ if (!has_orig_head)
1057
+ hashcpy(orig_head, EMPTY_TREE_SHA1_BIN);
1058
+
1059
+ clean_index(curr_head, orig_head);
1060
+
1061
+ if (has_orig_head)
1062
+ update_ref("am --abort", "HEAD", orig_head,
1063
+ has_curr_head ? curr_head : NULL, 0,
1064
+ UPDATE_REFS_DIE_ON_ERR);
1065
+ else if (curr_branch)
1066
+ delete_ref(curr_branch, NULL, REF_NODEREF);
1067
+
1068
+ free(curr_branch);
1069
+ am_destroy(state);
1070
+}
1071
+
1072
/**
1073
* parse_options() callback that validates and sets opt->value to the
1074
* PATCH_FORMAT_* enum value corresponding to `arg`.
@@ -998,7 +1088,8 @@ enum resume_mode {
1088
RESUME_FALSE = 0,
1089
RESUME_APPLY,
1090
RESUME_RESOLVED,
1001
- RESUME_SKIP
1091
+ RESUME_SKIP,
1092
+ RESUME_ABORT
1093
};
1094
1095
int cmd_am(int argc, const char **argv, const char *prefix)
@@ -1009,7 +1100,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
1100
1101
const char * const usage[] = {
1102
N_("git am [options] [(<mbox>|<Maildir>)...]"),
1012
- N_("git am [options] (--continue | --skip)"),
1103
+ N_("git am [options] (--continue | --skip | --abort)"),
1104
NULL
1105
};
1106
@@ -1026,6 +1117,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
1117
OPT_CMDMODE(0, "skip", &resume,
1118
N_("skip the current patch"),
1119
RESUME_SKIP),
1120
+ OPT_CMDMODE(0, "abort", &resume,
1121
+ N_("restore the original branch and abort the patching operation."),
1122
+ RESUME_ABORT),
1123
OPT_END()
1124
};
1125
@@ -1090,6 +1184,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
1184
case RESUME_SKIP:
1185
am_skip(&state);
1186
break;
1187
+ case RESUME_ABORT:
1188
+ am_abort(&state);
1189
+ break;
1190
default:
1191
die("BUG: invalid resume value");
1192
}