builtin-am: apply patch with git-apply

Implement applying the patch to the index using git-apply. If a file is unchanged but stat-dirty, git-apply may erroneously fail to apply patches, thinking that they conflict with a dirty working tree. As such, since 2a6f08a (am: refresh the index at start and --resolved, 2011-08-15), git-am will refresh the index before applying patches. Re-implement this behavior. Helped-by: Junio C Hamano <gitster@pobox.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 38a824fe050c4da3f2e0979a94062119080a77a0
1 file changed +71 -1
builtin/am.c
+71 -1
@@ -10,6 +10,7 @@
10 #include "dir.h"
11 #include "run-command.h"
12 #include "quote.h"
13 +#include "lockfile.h"
14
15 /**
16 * Returns 1 if the file is empty or does not exist, 0 otherwise.
@@ -42,6 +43,14 @@ static int strbuf_getline_crlf(struct strbuf *sb, FILE *fp)
43 return 0;
44 }
45
46 +/**
47 + * Returns the length of the first line of msg.
48 + */
49 +static int linelen(const char *msg)
50 +{
51 + return strchrnul(msg, '\n') - msg;
52 +}
53 +
54 enum patch_format {
55 PATCH_FORMAT_UNKNOWN = 0,
56 PATCH_FORMAT_MBOX
@@ -539,6 +548,19 @@ static const char *msgnum(const struct am_state *state)
548 return sb.buf;
549 }
550
551 +/**
552 + * Refresh and write index.
553 + */
554 +static void refresh_and_write_cache(void)
555 +{
556 + struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
557 +
558 + hold_locked_index(lock_file, 1);
559 + refresh_cache(REFRESH_QUIET);
560 + if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
561 + die(_("unable to write index file"));
562 +}
563 +
564 /**
565 * Parses `mail` using git-mailinfo, extracting its patch and authorship info.
566 * state->msg will be set to the patch message. state->author_name,
@@ -628,11 +650,36 @@ finish:
650 return ret;
651 }
652
653 +/**
654 + * Applies current patch with git-apply. Returns 0 on success, -1 otherwise.
655 + */
656 +static int run_apply(const struct am_state *state)
657 +{
658 + struct child_process cp = CHILD_PROCESS_INIT;
659 +
660 + cp.git_cmd = 1;
661 +
662 + argv_array_push(&cp.args, "apply");
663 + argv_array_push(&cp.args, "--index");
664 + argv_array_push(&cp.args, am_path(state, "patch"));
665 +
666 + if (run_command(&cp))
667 + return -1;
668 +
669 + /* Reload index as git-apply will have modified it. */
670 + discard_cache();
671 + read_cache();
672 +
673 + return 0;
674 +}
675 +
676 /**
677 * Applies all queued mail.
678 */
679 static void am_run(struct am_state *state)
680 {
681 + refresh_and_write_cache();
682 +
683 while (state->cur <= state->last) {
684 const char *mail = am_path(state, msgnum(state));
685
@@ -645,7 +692,27 @@ static void am_run(struct am_state *state)
692 write_author_script(state);
693 write_commit_msg(state);
694
648 - /* NEEDSWORK: Patch application not implemented yet */
695 + printf_ln(_("Applying: %.*s"), linelen(state->msg), state->msg);
696 +
697 + if (run_apply(state) < 0) {
698 + int advice_amworkdir = 1;
699 +
700 + printf_ln(_("Patch failed at %s %.*s"), msgnum(state),
701 + linelen(state->msg), state->msg);
702 +
703 + git_config_get_bool("advice.amworkdir", &advice_amworkdir);
704 +
705 + if (advice_amworkdir)
706 + printf_ln(_("The copy of the patch that failed is found in: %s"),
707 + am_path(state, "patch"));
708 +
709 + exit(128);
710 + }
711 +
712 + /*
713 + * NEEDSWORK: After the patch has been applied to the index
714 + * with git-apply, we need to make commit as well.
715 + */
716
717 next:
718 am_next(state);
@@ -707,6 +774,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
774
775 argc = parse_options(argc, argv, prefix, options, usage, 0);
776
777 + if (read_index_preload(&the_index, NULL) < 0)
778 + die(_("failed to read the index"));
779 +
780 if (am_in_progress(&state))
781 am_load(&state);
782 else {