rebase -i: rearrange fixup/squash lines using the rebase--helper

This operation has quadratic complexity, which is especially painful
on Windows, where shell scripts are *already* slow (mainly due to the
overhead of the POSIX emulation layer).

Let's reimplement this with linear complexity (using a hash map to
match the commits' subject lines) for the common case; Sadly, the
fixup/squash feature's design neglected performance considerations,
allowing arbitrary prefixes (read: `fixup! hell` will match the
commit subject `hello world`), which means that we are stuck with
quadratic performance in the worst case.

The reimplemented logic also happens to fix a bug where commented-out
lines (representing empty patches) were dropped by the previous code.

While at it, clarify how the fixup/squash feature works in `git rebase
-i`'s man page.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Johannes Schindelin
2017-07-14 16:45:31 +02:00
committed by Junio C Hamano
parent b174ae7df2
commit c44a4c650c
6 changed files with 213 additions and 98 deletions

View File

@ -20,6 +20,7 @@
#include "trailer.h"
#include "log-tree.h"
#include "wt-status.h"
#include "hashmap.h"
#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
@ -2748,3 +2749,198 @@ int skip_unnecessary_picks(void)
return 0;
}
struct subject2item_entry {
struct hashmap_entry entry;
int i;
char subject[FLEX_ARRAY];
};
static int subject2item_cmp(const void *fndata,
const struct subject2item_entry *a,
const struct subject2item_entry *b, const void *key)
{
return key ? strcmp(a->subject, key) : strcmp(a->subject, b->subject);
}
/*
* Rearrange the todo list that has both "pick commit-id msg" and "pick
* commit-id fixup!/squash! msg" in it so that the latter is put immediately
* after the former, and change "pick" to "fixup"/"squash".
*
* Note that if the config has specified a custom instruction format, each log
* message will have to be retrieved from the commit (as the oneline in the
* script cannot be trusted) in order to normalize the autosquash arrangement.
*/
int rearrange_squash(void)
{
const char *todo_file = rebase_path_todo();
struct todo_list todo_list = TODO_LIST_INIT;
struct hashmap subject2item;
int res = 0, rearranged = 0, *next, *tail, fd, i;
char **subjects;
fd = open(todo_file, O_RDONLY);
if (fd < 0)
return error_errno(_("could not open '%s'"), todo_file);
if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
close(fd);
return error(_("could not read '%s'."), todo_file);
}
close(fd);
if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) {
todo_list_release(&todo_list);
return -1;
}
/*
* The hashmap maps onelines to the respective todo list index.
*
* If any items need to be rearranged, the next[i] value will indicate
* which item was moved directly after the i'th.
*
* In that case, last[i] will indicate the index of the latest item to
* be moved to appear after the i'th.
*/
hashmap_init(&subject2item, (hashmap_cmp_fn) subject2item_cmp,
NULL, todo_list.nr);
ALLOC_ARRAY(next, todo_list.nr);
ALLOC_ARRAY(tail, todo_list.nr);
ALLOC_ARRAY(subjects, todo_list.nr);
for (i = 0; i < todo_list.nr; i++) {
struct strbuf buf = STRBUF_INIT;
struct todo_item *item = todo_list.items + i;
const char *commit_buffer, *subject, *p;
size_t subject_len;
int i2 = -1;
struct subject2item_entry *entry;
next[i] = tail[i] = -1;
if (item->command >= TODO_EXEC) {
subjects[i] = NULL;
continue;
}
if (is_fixup(item->command)) {
todo_list_release(&todo_list);
return error(_("the script was already rearranged."));
}
item->commit->util = item;
parse_commit(item->commit);
commit_buffer = get_commit_buffer(item->commit, NULL);
find_commit_subject(commit_buffer, &subject);
format_subject(&buf, subject, " ");
subject = subjects[i] = strbuf_detach(&buf, &subject_len);
unuse_commit_buffer(item->commit, commit_buffer);
if ((skip_prefix(subject, "fixup! ", &p) ||
skip_prefix(subject, "squash! ", &p))) {
struct commit *commit2;
for (;;) {
while (isspace(*p))
p++;
if (!skip_prefix(p, "fixup! ", &p) &&
!skip_prefix(p, "squash! ", &p))
break;
}
if ((entry = hashmap_get_from_hash(&subject2item,
strhash(p), p)))
/* found by title */
i2 = entry->i;
else if (!strchr(p, ' ') &&
(commit2 =
lookup_commit_reference_by_name(p)) &&
commit2->util)
/* found by commit name */
i2 = (struct todo_item *)commit2->util
- todo_list.items;
else {
/* copy can be a prefix of the commit subject */
for (i2 = 0; i2 < i; i2++)
if (subjects[i2] &&
starts_with(subjects[i2], p))
break;
if (i2 == i)
i2 = -1;
}
}
if (i2 >= 0) {
rearranged = 1;
todo_list.items[i].command =
starts_with(subject, "fixup!") ?
TODO_FIXUP : TODO_SQUASH;
if (next[i2] < 0)
next[i2] = i;
else
next[tail[i2]] = i;
tail[i2] = i;
} else if (!hashmap_get_from_hash(&subject2item,
strhash(subject), subject)) {
FLEX_ALLOC_MEM(entry, subject, subject, subject_len);
entry->i = i;
hashmap_entry_init(entry, strhash(entry->subject));
hashmap_put(&subject2item, entry);
}
}
if (rearranged) {
struct strbuf buf = STRBUF_INIT;
for (i = 0; i < todo_list.nr; i++) {
enum todo_command command = todo_list.items[i].command;
int cur = i;
/*
* Initially, all commands are 'pick's. If it is a
* fixup or a squash now, we have rearranged it.
*/
if (is_fixup(command))
continue;
while (cur >= 0) {
int offset = todo_list.items[cur].offset_in_buf;
int end_offset = cur + 1 < todo_list.nr ?
todo_list.items[cur + 1].offset_in_buf :
todo_list.buf.len;
char *bol = todo_list.buf.buf + offset;
char *eol = todo_list.buf.buf + end_offset;
/* replace 'pick', by 'fixup' or 'squash' */
command = todo_list.items[cur].command;
if (is_fixup(command)) {
strbuf_addstr(&buf,
todo_command_info[command].str);
bol += strcspn(bol, " \t");
}
strbuf_add(&buf, bol, eol - bol);
cur = next[cur];
}
}
fd = open(todo_file, O_WRONLY);
if (fd < 0)
res = error_errno(_("could not open '%s'"), todo_file);
else if (write(fd, buf.buf, buf.len) < 0)
res = error_errno(_("could not read '%s'."), todo_file);
else if (ftruncate(fd, buf.len) < 0)
res = error_errno(_("could not finish '%s'"),
todo_file);
close(fd);
strbuf_release(&buf);
}
free(next);
free(tail);
for (i = 0; i < todo_list.nr; i++)
free(subjects[i]);
free(subjects);
hashmap_free(&subject2item, 1);
todo_list_release(&todo_list);
return res;
}