builtin-am: rerere support

git-am.sh will call git-rerere at the following events: * "git rerere" when a three-way merge fails to record the conflicted automerge results. Since 8389b52 (git-rerere: reuse recorded resolve., 2006-01-28) * Since cb6020b (Teach --[no-]rerere-autoupdate option to merge, revert and friends, 2009-12-04), git-am.sh supports the --[no-]rerere-autoupdate option as well, and would pass it to git-rerere. * "git rerere" when --resolved, to record the hand resolution. Since f131dd4 (rerere: record (or avoid misrecording) resolved, skipped or aborted rebase/am, 2006-12-08) * "git rerere clear" when --skip-ing. Since f131dd4 (rerere: record (or avoid misrecording) resolved, skipped or aborted rebase/am, 2006-12-08) * "git rerere clear" when --abort-ing. Since 3e5057a (git am --abort, 2008-07-16) Re-implement the above in builtin/am.c. 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 f1cb96d687688afba0ae08d1c6fdd58c9fdbec1e
1 file changed +25
builtin/am.c
+25
@@ -24,6 +24,7 @@
24 #include "revision.h"
25 #include "log-tree.h"
26 #include "notes-utils.h"
27 +#include "rerere.h"
28
29 /**
30 * Returns 1 if the file is empty or does not exist, 0 otherwise.
@@ -114,6 +115,7 @@ struct am_state {
115 const char *resolvemsg;
116 int committer_date_is_author_date;
117 int ignore_date;
118 + int allow_rerere_autoupdate;
119 const char *sign_commit;
120 int rebasing;
121 };
@@ -1312,6 +1314,7 @@ static int fall_back_threeway(const struct am_state *state, const char *index_pa
1314 o.verbosity = 0;
1315
1316 if (merge_recursive_generic(&o, our_tree, his_tree, 1, bases, &result)) {
1317 + rerere(state->allow_rerere_autoupdate);
1318 free(his_tree_name);
1319 return error(_("Failed to merge in the changes."));
1320 }
@@ -1531,6 +1534,8 @@ static void am_resolve(struct am_state *state)
1534 die_user_resolve(state);
1535 }
1536
1537 + rerere(0);
1538 +
1539 do_commit(state);
1540
1541 am_next(state);
@@ -1630,6 +1635,21 @@ static int clean_index(const unsigned char *head, const unsigned char *remote)
1635 return 0;
1636 }
1637
1638 +/**
1639 + * Resets rerere's merge resolution metadata.
1640 + */
1641 +static void am_rerere_clear(void)
1642 +{
1643 + struct string_list merge_rr = STRING_LIST_INIT_DUP;
1644 + int fd = setup_rerere(&merge_rr, 0);
1645 +
1646 + if (fd < 0)
1647 + return;
1648 +
1649 + rerere_clear(&merge_rr);
1650 + string_list_clear(&merge_rr, 1);
1651 +}
1652 +
1653 /**
1654 * Resume the current am session by skipping the current patch.
1655 */
@@ -1637,6 +1657,8 @@ static void am_skip(struct am_state *state)
1657 {
1658 unsigned char head[GIT_SHA1_RAWSZ];
1659
1660 + am_rerere_clear();
1661 +
1662 if (get_sha1("HEAD", head))
1663 hashcpy(head, EMPTY_TREE_SHA1_BIN);
1664
@@ -1694,6 +1716,8 @@ static void am_abort(struct am_state *state)
1716 return;
1717 }
1718
1719 + am_rerere_clear();
1720 +
1721 curr_branch = resolve_refdup("HEAD", 0, curr_head, NULL);
1722 has_curr_head = !is_null_sha1(curr_head);
1723 if (!has_curr_head)
@@ -1823,6 +1847,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
1847 N_("lie about committer date")),
1848 OPT_BOOL(0, "ignore-date", &state.ignore_date,
1849 N_("use current timestamp for author date")),
1850 + OPT_RERERE_AUTOUPDATE(&state.allow_rerere_autoupdate),
1851 { OPTION_STRING, 'S', "gpg-sign", &state.sign_commit, N_("key-id"),
1852 N_("GPG-sign commits"),
1853 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },