Skip to content

Commit 7364756

Browse files
committed
sequencer (rebase -i): copy commit notes at end
This fixes t3404.50. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent ad7a43f commit 7364756

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

sequencer.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ static GIT_PATH_FUNC(git_path_rebase_amend, "rebase-merge/amend")
9292
* the long commit name of the corresponding patch.
9393
*/
9494
static GIT_PATH_FUNC(stopped_sha, "rebase-merge/stopped-sha")
95+
/*
96+
* For the post-rewrite hook, we make a list of rewritten commits and
97+
* their new sha1s. The rewritten-pending list keeps the sha1s of
98+
* commits that have been processed, but not committed yet,
99+
* e.g. because they are waiting for a 'squash' command.
100+
*/
101+
static GIT_PATH_FUNC(rewritten_list, "rebase-merge/rewritten-list")
102+
static GIT_PATH_FUNC(rewritten_pending, "rebase-merge/rewritten-pending")
95103
/*
96104
* The following files are written by git-rebase just after parsing the
97105
* command-line (and are only consumed, not modified, by the sequencer).
@@ -799,6 +807,43 @@ static int update_squash_messages(enum todo_command command,
799807
return 0;
800808
}
801809

810+
static void flush_rewritten_pending(void) {
811+
struct strbuf buf = STRBUF_INIT;
812+
unsigned char newsha1[20];
813+
FILE *out;
814+
815+
if (strbuf_read_file(&buf, rewritten_pending(), 82) > 0 &&
816+
!get_sha1("HEAD", newsha1) &&
817+
(out = fopen(rewritten_list(), "a"))) {
818+
char *bol = buf.buf, *eol;
819+
820+
while (*bol) {
821+
eol = strchrnul(bol, '\n');
822+
fprintf(out, "%.*s %s\n", (int)(eol - bol),
823+
bol, sha1_to_hex(newsha1));
824+
if (!*eol)
825+
break;
826+
bol = eol + 1;
827+
}
828+
fclose(out);
829+
unlink(rewritten_pending());
830+
}
831+
}
832+
833+
static void record_in_rewritten(struct object_id *oid,
834+
enum todo_command next_command) {
835+
FILE *out = fopen(rewritten_pending(), "a");
836+
837+
if (!out)
838+
return;
839+
840+
fprintf(out, "%s\n", oid_to_hex(oid));
841+
fclose(out);
842+
843+
if (!is_fixup(next_command))
844+
flush_rewritten_pending();
845+
}
846+
802847
static int do_pick_commit(enum todo_command command, struct commit *commit,
803848
struct replay_opts *opts, int final_fixup)
804849
{
@@ -1554,6 +1599,17 @@ static int is_final_fixup(struct todo_list *todo_list)
15541599
return 1;
15551600
}
15561601

1602+
static enum todo_command peek_command(struct todo_list *todo_list, int offset)
1603+
{
1604+
int i;
1605+
1606+
for (i = todo_list->current + offset; i < todo_list->nr; i++)
1607+
if (todo_list->items[i].command != TODO_NOOP)
1608+
return todo_list->items[i].command;
1609+
1610+
return -1;
1611+
}
1612+
15571613
static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
15581614
{
15591615
int res = 0;
@@ -1587,6 +1643,9 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
15871643
item->arg_len, item->arg);
15881644
return error_with_patch(commit, opts, res);
15891645
}
1646+
if (IS_REBASE_I() && !res)
1647+
record_in_rewritten(&item->commit->object.oid,
1648+
peek_command(todo_list, 1));
15901649
if (res && is_fixup(item->command))
15911650
return error_failed_squash(item->commit, opts,
15921651
item->arg_len, item->arg);
@@ -1611,6 +1670,7 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
16111670

16121671
if (IS_REBASE_I()) {
16131672
struct strbuf head_ref = STRBUF_INIT, buf = STRBUF_INIT;
1673+
struct stat st;
16141674

16151675
/* Stopped in the middle, as planned? */
16161676
if (todo_list->current < todo_list->nr)
@@ -1656,6 +1716,19 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
16561716
run_command_v_opt(argv, RUN_GIT_CMD);
16571717
strbuf_reset(&buf);
16581718
}
1719+
flush_rewritten_pending();
1720+
if (!stat(rewritten_list(), &st) && st.st_size > 0) {
1721+
struct child_process child = CHILD_PROCESS_INIT;
1722+
1723+
child.in = open(rewritten_list(), O_RDONLY);
1724+
child.git_cmd = 1;
1725+
argv_array_push(&child.args, "notes");
1726+
argv_array_push(&child.args, "copy");
1727+
argv_array_push(&child.args, "--for-rewrite=rebase");
1728+
/* we don't care if this copying failed */
1729+
run_command(&child);
1730+
}
1731+
16591732
strbuf_release(&buf);
16601733
strbuf_release(&head_ref);
16611734
}

0 commit comments

Comments
 (0)