Skip to content

Commit 13b97ea

Browse files
pyokagangitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7e35dac commit 13b97ea

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

builtin/am.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ struct am_state {
9595
char *msg;
9696
size_t msg_len;
9797

98+
/* when --rebasing, records the original commit the patch came from */
99+
unsigned char orig_commit[GIT_SHA1_RAWSZ];
100+
98101
/* number of digits in patch filename */
99102
int prec;
100103

@@ -392,6 +395,11 @@ static void am_load(struct am_state *state)
392395

393396
read_commit_msg(state);
394397

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+
395403
read_state_file(&sb, state, "threeway", 1);
396404
state->threeway = !strcmp(sb.buf, "t");
397405

@@ -446,6 +454,30 @@ static void am_destroy(const struct am_state *state)
446454
strbuf_release(&sb);
447455
}
448456

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+
449481
/**
450482
* Determines if the file looks like a piece of RFC2822 mail by grabbing all
451483
* 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)
720752
unlink(am_path(state, "author-script"));
721753
unlink(am_path(state, "final-commit"));
722754

755+
hashclr(state->orig_commit);
756+
unlink(am_path(state, "original-commit"));
757+
723758
if (!get_sha1("HEAD", head))
724759
write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(head));
725760
else
@@ -1038,6 +1073,8 @@ static void write_commit_patch(const struct am_state *state, struct commit *comm
10381073
* directly. This is used in --rebasing mode to bypass git-mailinfo's munging
10391074
* of patches.
10401075
*
1076+
* state->orig_commit will be set to the original commit ID.
1077+
*
10411078
* Will always return 0 as the patch should never be skipped.
10421079
*/
10431080
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)
10541091

10551092
write_commit_patch(state, commit);
10561093

1094+
hashcpy(state->orig_commit, commit_sha1);
1095+
write_file(am_path(state, "original-commit"), 1, "%s",
1096+
sha1_to_hex(commit_sha1));
1097+
10571098
return 0;
10581099
}
10591100

@@ -1245,6 +1286,15 @@ static void do_commit(const struct am_state *state)
12451286

12461287
update_ref(sb.buf, "HEAD", commit, ptr, 0, UPDATE_REFS_DIE_ON_ERR);
12471288

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+
12481298
strbuf_release(&sb);
12491299
}
12501300

@@ -1353,6 +1403,11 @@ static void am_run(struct am_state *state, int resume)
13531403
am_next(state);
13541404
}
13551405

1406+
if (!is_empty_file(am_path(state, "rewritten"))) {
1407+
assert(state->rebasing);
1408+
run_post_rewrite_hook(state);
1409+
}
1410+
13561411
/*
13571412
* In rebasing mode, it's up to the caller to take care of
13581413
* housekeeping.

0 commit comments

Comments
 (0)