builtin-am: invoke post-rewrite hook

Since 96e1948 (rebase: invoke post-rewrite hook, 2010-03-12), git-am.sh will invoke the post-rewrite hook after it successfully finishes applying all the queued patches. To do this, when parsing a mail to extract its patch and metadata, in --rebasing mode git-am.sh will also store the original commit ID in the $state_dir/original-commit file. Once it applies and commits the patch, the original commit ID, and the new commit ID, will be appended to the $state_dir/rewritten file. Once all of the queued mail have been processed, git-am.sh will then invoke the post-rewrite hook with the contents of the $state_dir/rewritten file. Re-implement this 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 13b97ea5f0bec8f529e8b819a24502023bfbacba
1 file changed +55
builtin/am.c
+55
@@ -95,6 +95,9 @@ struct am_state {
95 char *msg;
96 size_t msg_len;
97
98 + /* when --rebasing, records the original commit the patch came from */
99 + unsigned char orig_commit[GIT_SHA1_RAWSZ];
100 +
101 /* number of digits in patch filename */
102 int prec;
103
@@ -392,6 +395,11 @@ static void am_load(struct am_state *state)
395
396 read_commit_msg(state);
397
398 + if (read_state_file(&sb, state, "original-commit", 1) < 0)
399 + hashclr(state->orig_commit);
400 + else if (get_sha1_hex(sb.buf, state->orig_commit) < 0)
401 + die(_("could not parse %s"), am_path(state, "original-commit"));
402 +
403 read_state_file(&sb, state, "threeway", 1);
404 state->threeway = !strcmp(sb.buf, "t");
405
@@ -446,6 +454,30 @@ static void am_destroy(const struct am_state *state)
454 strbuf_release(&sb);
455 }
456
457 +/**
458 + * Runs post-rewrite hook. Returns it exit code.
459 + */
460 +static int run_post_rewrite_hook(const struct am_state *state)
461 +{
462 + struct child_process cp = CHILD_PROCESS_INIT;
463 + const char *hook = find_hook("post-rewrite");
464 + int ret;
465 +
466 + if (!hook)
467 + return 0;
468 +
469 + argv_array_push(&cp.args, hook);
470 + argv_array_push(&cp.args, "rebase");
471 +
472 + cp.in = xopen(am_path(state, "rewritten"), O_RDONLY);
473 + cp.stdout_to_stderr = 1;
474 +
475 + ret = run_command(&cp);
476 +
477 + close(cp.in);
478 + return ret;
479 +}
480 +
481 /**
482 * Determines if the file looks like a piece of RFC2822 mail by grabbing all
483 * non-indented lines and checking if they look like they begin with valid
@@ -720,6 +752,9 @@ static void am_next(struct am_state *state)
752 unlink(am_path(state, "author-script"));
753 unlink(am_path(state, "final-commit"));
754
755 + hashclr(state->orig_commit);
756 + unlink(am_path(state, "original-commit"));
757 +
758 if (!get_sha1("HEAD", head))
759 write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(head));
760 else
@@ -1038,6 +1073,8 @@ static void write_commit_patch(const struct am_state *state, struct commit *comm
1073 * directly. This is used in --rebasing mode to bypass git-mailinfo's munging
1074 * of patches.
1075 *
1076 + * state->orig_commit will be set to the original commit ID.
1077 + *
1078 * Will always return 0 as the patch should never be skipped.
1079 */
1080 static int parse_mail_rebase(struct am_state *state, const char *mail)
@@ -1054,6 +1091,10 @@ static int parse_mail_rebase(struct am_state *state, const char *mail)
1091
1092 write_commit_patch(state, commit);
1093
1094 + hashcpy(state->orig_commit, commit_sha1);
1095 + write_file(am_path(state, "original-commit"), 1, "%s",
1096 + sha1_to_hex(commit_sha1));
1097 +
1098 return 0;
1099 }
1100
@@ -1245,6 +1286,15 @@ static void do_commit(const struct am_state *state)
1286
1287 update_ref(sb.buf, "HEAD", commit, ptr, 0, UPDATE_REFS_DIE_ON_ERR);
1288
1289 + if (state->rebasing) {
1290 + FILE *fp = xfopen(am_path(state, "rewritten"), "a");
1291 +
1292 + assert(!is_null_sha1(state->orig_commit));
1293 + fprintf(fp, "%s ", sha1_to_hex(state->orig_commit));
1294 + fprintf(fp, "%s\n", sha1_to_hex(commit));
1295 + fclose(fp);
1296 + }
1297 +
1298 strbuf_release(&sb);
1299 }
1300
@@ -1353,6 +1403,11 @@ next:
1403 am_next(state);
1404 }
1405
1406 + if (!is_empty_file(am_path(state, "rewritten"))) {
1407 + assert(state->rebasing);
1408 + run_post_rewrite_hook(state);
1409 + }
1410 +
1411 /*
1412 * In rebasing mode, it's up to the caller to take care of
1413 * housekeeping.