builtin-am: implement --skip
Since d1c5f2a (Add git-am, applymbox replacement., 2005-10-07), git-am supported resuming from a failed patch application by skipping the current patch. Re-implement this feature by introducing am_skip(). 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
9990080c9db0e4cb1dc0f981d85983edbf2a6f68
1 file changed
+121
-2
builtin/am.c
+121
-2
@@ -16,6 +16,8 @@
16
#include "commit.h"
17
#include "diff.h"
18
#include "diffcore.h"
19
+#include "unpack-trees.h"
20
+#include "branch.h"
21
22
/**
23
* Returns 1 if the file is empty or does not exist, 0 otherwise.
@@ -867,6 +869,116 @@ static void am_resolve(struct am_state *state)
869
am_run(state, 0);
870
}
871
872
+/**
873
+ * Performs a checkout fast-forward from `head` to `remote`. If `reset` is
874
+ * true, any unmerged entries will be discarded. Returns 0 on success, -1 on
875
+ * failure.
876
+ */
877
+static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
878
+{
879
+ struct lock_file *lock_file;
880
+ struct unpack_trees_options opts;
881
+ struct tree_desc t[2];
882
+
883
+ if (parse_tree(head) || parse_tree(remote))
884
+ return -1;
885
+
886
+ lock_file = xcalloc(1, sizeof(struct lock_file));
887
+ hold_locked_index(lock_file, 1);
888
+
889
+ refresh_cache(REFRESH_QUIET);
890
+
891
+ memset(&opts, 0, sizeof(opts));
892
+ opts.head_idx = 1;
893
+ opts.src_index = &the_index;
894
+ opts.dst_index = &the_index;
895
+ opts.update = 1;
896
+ opts.merge = 1;
897
+ opts.reset = reset;
898
+ opts.fn = twoway_merge;
899
+ init_tree_desc(&t[0], head->buffer, head->size);
900
+ init_tree_desc(&t[1], remote->buffer, remote->size);
901
+
902
+ if (unpack_trees(2, t, &opts)) {
903
+ rollback_lock_file(lock_file);
904
+ return -1;
905
+ }
906
+
907
+ if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
908
+ die(_("unable to write new index file"));
909
+
910
+ return 0;
911
+}
912
+
913
+/**
914
+ * Clean the index without touching entries that are not modified between
915
+ * `head` and `remote`.
916
+ */
917
+static int clean_index(const unsigned char *head, const unsigned char *remote)
918
+{
919
+ struct lock_file *lock_file;
920
+ struct tree *head_tree, *remote_tree, *index_tree;
921
+ unsigned char index[GIT_SHA1_RAWSZ];
922
+ struct pathspec pathspec;
923
+
924
+ head_tree = parse_tree_indirect(head);
925
+ if (!head_tree)
926
+ return error(_("Could not parse object '%s'."), sha1_to_hex(head));
927
+
928
+ remote_tree = parse_tree_indirect(remote);
929
+ if (!remote_tree)
930
+ return error(_("Could not parse object '%s'."), sha1_to_hex(remote));
931
+
932
+ read_cache_unmerged();
933
+
934
+ if (fast_forward_to(head_tree, head_tree, 1))
935
+ return -1;
936
+
937
+ if (write_cache_as_tree(index, 0, NULL))
938
+ return -1;
939
+
940
+ index_tree = parse_tree_indirect(index);
941
+ if (!index_tree)
942
+ return error(_("Could not parse object '%s'."), sha1_to_hex(index));
943
+
944
+ if (fast_forward_to(index_tree, remote_tree, 0))
945
+ return -1;
946
+
947
+ memset(&pathspec, 0, sizeof(pathspec));
948
+
949
+ lock_file = xcalloc(1, sizeof(struct lock_file));
950
+ hold_locked_index(lock_file, 1);
951
+
952
+ if (read_tree(remote_tree, 0, &pathspec)) {
953
+ rollback_lock_file(lock_file);
954
+ return -1;
955
+ }
956
+
957
+ if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
958
+ die(_("unable to write new index file"));
959
+
960
+ remove_branch_state();
961
+
962
+ return 0;
963
+}
964
+
965
+/**
966
+ * Resume the current am session by skipping the current patch.
967
+ */
968
+static void am_skip(struct am_state *state)
969
+{
970
+ unsigned char head[GIT_SHA1_RAWSZ];
971
+
972
+ if (get_sha1("HEAD", head))
973
+ hashcpy(head, EMPTY_TREE_SHA1_BIN);
974
+
975
+ if (clean_index(head, head))
976
+ die(_("failed to clean index"));
977
+
978
+ am_next(state);
979
+ am_run(state, 0);
980
+}
981
+
982
/**
983
* parse_options() callback that validates and sets opt->value to the
984
* PATCH_FORMAT_* enum value corresponding to `arg`.
@@ -885,7 +997,8 @@ static int parse_opt_patchformat(const struct option *opt, const char *arg, int
997
enum resume_mode {
998
RESUME_FALSE = 0,
999
RESUME_APPLY,
888
- RESUME_RESOLVED
1000
+ RESUME_RESOLVED,
1001
+ RESUME_SKIP
1002
};
1003
1004
int cmd_am(int argc, const char **argv, const char *prefix)
@@ -896,7 +1009,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
1009
1010
const char * const usage[] = {
1011
N_("git am [options] [(<mbox>|<Maildir>)...]"),
899
- N_("git am [options] --continue"),
1012
+ N_("git am [options] (--continue | --skip)"),
1013
NULL
1014
};
1015
@@ -910,6 +1023,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
1023
OPT_CMDMODE('r', "resolved", &resume,
1024
N_("synonyms for --continue"),
1025
RESUME_RESOLVED),
1026
+ OPT_CMDMODE(0, "skip", &resume,
1027
+ N_("skip the current patch"),
1028
+ RESUME_SKIP),
1029
OPT_END()
1030
};
1031
@@ -971,6 +1087,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
1087
case RESUME_RESOLVED:
1088
am_resolve(&state);
1089
break;
1090
+ case RESUME_SKIP:
1091
+ am_skip(&state);
1092
+ break;
1093
default:
1094
die("BUG: invalid resume value");
1095
}