Skip to content

Commit 60ac5c2

Browse files
committed
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. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent b96c11e commit 60ac5c2

File tree

5 files changed

+205
-91
lines changed

5 files changed

+205
-91
lines changed

builtin/rebase--helper.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
1414
int keep_empty = 0;
1515
enum {
1616
CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_SHA1S, EXPAND_SHA1S,
17-
CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS
17+
CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH
1818
} command = 0;
1919
struct option options[] = {
2020
OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
@@ -33,6 +33,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
3333
N_("check the todo list"), CHECK_TODO_LIST),
3434
OPT_CMDMODE(0, "skip-unnecessary-picks", &command,
3535
N_("skip unnecessary picks"), SKIP_UNNECESSARY_PICKS),
36+
OPT_CMDMODE(0, "rearrange-squash", &command,
37+
N_("rearrange fixup/squash lines"), REARRANGE_SQUASH),
3638
OPT_END()
3739
};
3840

@@ -59,5 +61,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
5961
return !!check_todo_list();
6062
if (command == SKIP_UNNECESSARY_PICKS && argc == 1)
6163
return !!skip_unnecessary_picks();
64+
if (command == REARRANGE_SQUASH && argc == 1)
65+
return !!rearrange_squash();
6266
usage_with_options(builtin_rebase_helper_usage, options);
6367
}

git-rebase--interactive.sh

Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -734,94 +734,6 @@ collapse_todo_ids() {
734734
git rebase--helper --shorten-sha1s
735735
}
736736

737-
# Rearrange the todo list that has both "pick sha1 msg" and
738-
# "pick sha1 fixup!/squash! msg" appears in it so that the latter
739-
# comes immediately after the former, and change "pick" to
740-
# "fixup"/"squash".
741-
#
742-
# Note that if the config has specified a custom instruction format
743-
# each log message will be re-retrieved in order to normalize the
744-
# autosquash arrangement
745-
rearrange_squash () {
746-
format=$(git config --get rebase.instructionFormat)
747-
# extract fixup!/squash! lines and resolve any referenced sha1's
748-
while read -r pick sha1 message
749-
do
750-
test -z "${format}" || message=$(git log -n 1 --format="%s" ${sha1})
751-
case "$message" in
752-
"squash! "*|"fixup! "*)
753-
action="${message%%!*}"
754-
rest=$message
755-
prefix=
756-
# skip all squash! or fixup! (but save for later)
757-
while :
758-
do
759-
case "$rest" in
760-
"squash! "*|"fixup! "*)
761-
prefix="$prefix${rest%%!*},"
762-
rest="${rest#*! }"
763-
;;
764-
*)
765-
break
766-
;;
767-
esac
768-
done
769-
printf '%s %s %s %s\n' "$sha1" "$action" "$prefix" "$rest"
770-
# if it's a single word, try to resolve to a full sha1 and
771-
# emit a second copy. This allows us to match on both message
772-
# and on sha1 prefix
773-
if test "${rest#* }" = "$rest"; then
774-
fullsha="$(git rev-parse -q --verify "$rest" 2>/dev/null)"
775-
if test -n "$fullsha"; then
776-
# prefix the action to uniquely identify this line as
777-
# intended for full sha1 match
778-
echo "$sha1 +$action $prefix $fullsha"
779-
fi
780-
fi
781-
esac
782-
done >"$1.sq" <"$1"
783-
test -s "$1.sq" || return
784-
785-
used=
786-
while read -r pick sha1 message
787-
do
788-
case " $used" in
789-
*" $sha1 "*) continue ;;
790-
esac
791-
printf '%s\n' "$pick $sha1 $message"
792-
test -z "${format}" || message=$(git log -n 1 --format="%s" ${sha1})
793-
used="$used$sha1 "
794-
while read -r squash action msg_prefix msg_content
795-
do
796-
case " $used" in
797-
*" $squash "*) continue ;;
798-
esac
799-
emit=0
800-
case "$action" in
801-
+*)
802-
action="${action#+}"
803-
# full sha1 prefix test
804-
case "$msg_content" in "$sha1"*) emit=1;; esac ;;
805-
*)
806-
# message prefix test
807-
case "$message" in "$msg_content"*) emit=1;; esac ;;
808-
esac
809-
if test $emit = 1; then
810-
if test -n "${format}"
811-
then
812-
msg_content=$(git log -n 1 --format="${format}" ${squash})
813-
else
814-
msg_content="$(echo "$msg_prefix" | sed "s/,/! /g")$msg_content"
815-
fi
816-
printf '%s\n' "$action $squash $msg_content"
817-
used="$used$squash "
818-
fi
819-
done <"$1.sq"
820-
done >"$1.rearranged" <"$1"
821-
cat "$1.rearranged" >"$1"
822-
rm -f "$1.sq" "$1.rearranged"
823-
}
824-
825737
# Add commands after a pick or after a squash/fixup serie
826738
# in the todo list.
827739
add_exec_commands () {
@@ -1084,7 +996,7 @@ then
1084996
fi
1085997

1086998
test -s "$todo" || echo noop >> "$todo"
1087-
test -n "$autosquash" && rearrange_squash "$todo"
999+
test -z "$autosquash" || git rebase--helper --rearrange-squash || exit
10881000
test -n "$cmd" && add_exec_commands "$todo"
10891001

10901002
todocount=$(git stripspace --strip-comments <"$todo" | wc -l)

sequencer.c

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "quote.h"
1919
#include "log-tree.h"
2020
#include "wt-status.h"
21+
#include "hashmap.h"
2122

2223
#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
2324

@@ -2662,3 +2663,199 @@ int skip_unnecessary_picks(void)
26622663

26632664
return 0;
26642665
}
2666+
2667+
struct subject2item_entry {
2668+
struct hashmap_entry entry;
2669+
int i;
2670+
char subject[FLEX_ARRAY];
2671+
};
2672+
2673+
static int subject2item_cmp(const struct subject2item_entry *a,
2674+
const struct subject2item_entry *b, const void *key)
2675+
{
2676+
return key ? strcmp(a->subject, key) : strcmp(a->subject, b->subject);
2677+
}
2678+
2679+
/*
2680+
* Rearrange the todo list that has both "pick sha1 msg" and "pick sha1
2681+
* fixup!/squash! msg" in it so that the latter is put immediately after the
2682+
* former, and change "pick" to "fixup"/"squash".
2683+
*
2684+
* Note that if the config has specified a custom instruction format, each log
2685+
* message will have to be retrieved from the commit (as the oneline in the
2686+
* script cannot be trusted) in order to normalize the autosquash arrangement.
2687+
*/
2688+
int rearrange_squash(void)
2689+
{
2690+
const char *todo_file = rebase_path_todo();
2691+
struct todo_list todo_list = TODO_LIST_INIT;
2692+
struct hashmap subject2item;
2693+
int res = 0, rearranged = 0, *next, *tail, fd, i;
2694+
char **subjects;
2695+
2696+
fd = open(todo_file, O_RDONLY);
2697+
if (fd < 0)
2698+
return error_errno(_("Could not open %s"), todo_file);
2699+
if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
2700+
close(fd);
2701+
return error(_("Could not read %s."), todo_file);
2702+
}
2703+
close(fd);
2704+
if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) {
2705+
todo_list_release(&todo_list);
2706+
return -1;
2707+
}
2708+
2709+
/*
2710+
* The hashmap maps onelines to the respective todo list index.
2711+
*
2712+
* If any items need to be rearranged, the next[i] value will indicate
2713+
* which item was moved directly after the i'th.
2714+
*
2715+
* In that case, last[i] will indicate the index of the latest item to
2716+
* be moved to appear after the i'th.
2717+
*/
2718+
hashmap_init(&subject2item, (hashmap_cmp_fn) subject2item_cmp,
2719+
todo_list.nr);
2720+
ALLOC_ARRAY(next, todo_list.nr);
2721+
ALLOC_ARRAY(tail, todo_list.nr);
2722+
ALLOC_ARRAY(subjects, todo_list.nr);
2723+
for (i = 0; i < todo_list.nr; i++) {
2724+
struct strbuf buf = STRBUF_INIT;
2725+
struct todo_item *item = todo_list.items + i;
2726+
const char *commit_buffer, *subject, *p;
2727+
int i2 = -1;
2728+
struct subject2item_entry *entry;
2729+
2730+
next[i] = tail[i] = -1;
2731+
if (item->command >= TODO_EXEC) {
2732+
subjects[i] = NULL;
2733+
continue;
2734+
}
2735+
2736+
if (is_fixup(item->command)) {
2737+
todo_list_release(&todo_list);
2738+
return error(_("The script was already rearranged."));
2739+
}
2740+
2741+
item->commit->util = item;
2742+
2743+
parse_commit(item->commit);
2744+
commit_buffer = get_commit_buffer(item->commit, NULL);
2745+
find_commit_subject(commit_buffer, &subject);
2746+
format_subject(&buf, subject, " ");
2747+
subject = subjects[i] = buf.buf;
2748+
unuse_commit_buffer(item->commit, commit_buffer);
2749+
if ((skip_prefix(subject, "fixup! ", &p) ||
2750+
skip_prefix(subject, "squash! ", &p))) {
2751+
struct commit *commit2;
2752+
2753+
for (;;) {
2754+
while (isspace(*p))
2755+
p++;
2756+
if (!skip_prefix(p, "fixup! ", &p) &&
2757+
!skip_prefix(p, "squash! ", &p))
2758+
break;
2759+
}
2760+
2761+
if ((entry = hashmap_get_from_hash(&subject2item,
2762+
strhash(p), p)))
2763+
/* found by title */
2764+
i2 = entry->i;
2765+
else if (!strchr(p, ' ') &&
2766+
(commit2 =
2767+
lookup_commit_reference_by_name(p)) &&
2768+
commit2->util)
2769+
/* found by commit name */
2770+
i2 = (struct todo_item *)commit2->util
2771+
- todo_list.items;
2772+
else {
2773+
/* copy can be a prefix of the commit subject */
2774+
for (i2 = 0; i2 < i; i2++)
2775+
if (subjects[i2] &&
2776+
starts_with(subjects[i2], p))
2777+
break;
2778+
if (i2 == i)
2779+
i2 = -1;
2780+
}
2781+
}
2782+
if (i2 >= 0) {
2783+
rearranged = 1;
2784+
todo_list.items[i].command =
2785+
starts_with(subject, "fixup!") ?
2786+
TODO_FIXUP : TODO_SQUASH;
2787+
if (next[i2] < 0)
2788+
next[i2] = i;
2789+
else
2790+
next[tail[i2]] = i;
2791+
tail[i2] = i;
2792+
}
2793+
else if (!hashmap_get_from_hash(&subject2item,
2794+
strhash(subject), subject)) {
2795+
FLEX_ALLOC_MEM(entry, subject, buf.buf, buf.len);
2796+
entry->i = i;
2797+
hashmap_entry_init(entry, strhash(entry->subject));
2798+
hashmap_put(&subject2item, entry);
2799+
}
2800+
strbuf_detach(&buf, NULL);
2801+
}
2802+
2803+
if (rearranged) {
2804+
struct strbuf buf = STRBUF_INIT;
2805+
char *format = NULL;
2806+
2807+
git_config_get_string("rebase.instructionFormat", &format);
2808+
for (i = 0; i < todo_list.nr; i++) {
2809+
enum todo_command command = todo_list.items[i].command;
2810+
int cur = i;
2811+
2812+
/*
2813+
* Initially, all commands are 'pick's. If it is a
2814+
* fixup or a squash now, we have rearranged it.
2815+
*/
2816+
if (is_fixup(command))
2817+
continue;
2818+
2819+
while (cur >= 0) {
2820+
int offset = todo_list.items[cur].offset_in_buf;
2821+
int end_offset = cur + 1 < todo_list.nr ?
2822+
todo_list.items[cur + 1].offset_in_buf :
2823+
todo_list.buf.len;
2824+
char *bol = todo_list.buf.buf + offset;
2825+
char *eol = todo_list.buf.buf + end_offset;
2826+
2827+
/* replace 'pick', by 'fixup' or 'squash' */
2828+
command = todo_list.items[cur].command;
2829+
if (is_fixup(command)) {
2830+
strbuf_addstr(&buf,
2831+
todo_command_info[command].str);
2832+
bol += strcspn(bol, " \t");
2833+
}
2834+
2835+
strbuf_add(&buf, bol, eol - bol);
2836+
2837+
cur = next[cur];
2838+
}
2839+
}
2840+
2841+
fd = open(todo_file, O_WRONLY);
2842+
if (fd < 0)
2843+
res |= error_errno(_("Could not open %s"), todo_file);
2844+
else if (write(fd, buf.buf, buf.len) < 0)
2845+
res |= error_errno(_("Could not read %s."), todo_file);
2846+
else if (ftruncate(fd, buf.len) < 0)
2847+
res |= error_errno(_("Could not finish %s"), todo_file);
2848+
close(fd);
2849+
strbuf_release(&buf);
2850+
}
2851+
2852+
free(next);
2853+
free(tail);
2854+
for (i = 0; i < todo_list.nr; i++)
2855+
free(subjects[i]);
2856+
free(subjects);
2857+
hashmap_free(&subject2item, 1);
2858+
todo_list_release(&todo_list);
2859+
2860+
return res;
2861+
}

sequencer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ int sequencer_make_script(int keep_empty, FILE *out,
6464
int transform_todo_ids(int shorten_sha1s);
6565
int check_todo_list(void);
6666
int skip_unnecessary_picks(void);
67+
int rearrange_squash(void);
6768

6869
extern const char sign_off_header[];
6970

t/t3415-rebase-autosquash.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ set_backup_editor () {
278278
test_set_editor "$PWD/backup-editor.sh"
279279
}
280280

281-
test_expect_failure 'autosquash with multiple empty patches' '
281+
test_expect_success 'autosquash with multiple empty patches' '
282282
test_tick &&
283283
git commit --allow-empty -m "empty" &&
284284
test_tick &&

0 commit comments

Comments
 (0)