Skip to content

Commit d9804f7

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d0a2bea commit d9804f7

File tree

6 files changed

+212
-98
lines changed

6 files changed

+212
-98
lines changed

Documentation/git-rebase.txt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -425,13 +425,15 @@ without an explicit `--interactive`.
425425
--autosquash::
426426
--no-autosquash::
427427
When the commit log message begins with "squash! ..." (or
428-
"fixup! ..."), and there is a commit whose title begins with
429-
the same ..., automatically modify the todo list of rebase -i
430-
so that the commit marked for squashing comes right after the
431-
commit to be modified, and change the action of the moved
432-
commit from `pick` to `squash` (or `fixup`). Ignores subsequent
433-
"fixup! " or "squash! " after the first, in case you referred to an
434-
earlier fixup/squash with `git commit --fixup/--squash`.
428+
"fixup! ..."), and there is already a commit in the todo list that
429+
matches the same `...`, automatically modify the todo list of rebase
430+
-i so that the commit marked for squashing comes right after the
431+
commit to be modified, and change the action of the moved commit
432+
from `pick` to `squash` (or `fixup`). A commit matches the `...` if
433+
the commit subject matches, or if the `...` refers to the commit's
434+
hash. As a fall-back, partial matches of the commit subject work,
435+
too. The recommended way to create fixup/squash commits is by using
436+
the `--fixup`/`--squash` options of linkgit:git-commit[1].
435437
+
436438
This option is only valid when the `--interactive` option is used.
437439
+

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
@@ -721,94 +721,6 @@ collapse_todo_ids() {
721721
git rebase--helper --shorten-sha1s
722722
}
723723

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

1070982
test -s "$todo" || echo noop >> "$todo"
1071-
test -n "$autosquash" && rearrange_squash "$todo"
983+
test -z "$autosquash" || git rebase--helper --rearrange-squash || exit
1072984
test -n "$cmd" && add_exec_commands "$todo"
1073985

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

sequencer.c

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "trailer.h"
2020
#include "log-tree.h"
2121
#include "wt-status.h"
22+
#include "hashmap.h"
2223

2324
#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
2425

@@ -2723,3 +2724,197 @@ int skip_unnecessary_picks(void)
27232724

27242725
return 0;
27252726
}
2727+
2728+
struct subject2item_entry {
2729+
struct hashmap_entry entry;
2730+
int i;
2731+
char subject[FLEX_ARRAY];
2732+
};
2733+
2734+
static int subject2item_cmp(const struct subject2item_entry *a,
2735+
const struct subject2item_entry *b, const void *key)
2736+
{
2737+
return key ? strcmp(a->subject, key) : strcmp(a->subject, b->subject);
2738+
}
2739+
2740+
/*
2741+
* Rearrange the todo list that has both "pick sha1 msg" and "pick sha1
2742+
* fixup!/squash! msg" in it so that the latter is put immediately after the
2743+
* former, and change "pick" to "fixup"/"squash".
2744+
*
2745+
* Note that if the config has specified a custom instruction format, each log
2746+
* message will have to be retrieved from the commit (as the oneline in the
2747+
* script cannot be trusted) in order to normalize the autosquash arrangement.
2748+
*/
2749+
int rearrange_squash(void)
2750+
{
2751+
const char *todo_file = rebase_path_todo();
2752+
struct todo_list todo_list = TODO_LIST_INIT;
2753+
struct hashmap subject2item;
2754+
int res = 0, rearranged = 0, *next, *tail, fd, i;
2755+
char **subjects;
2756+
2757+
fd = open(todo_file, O_RDONLY);
2758+
if (fd < 0)
2759+
return error_errno(_("could not open '%s'"), todo_file);
2760+
if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
2761+
close(fd);
2762+
return error(_("could not read '%s'."), todo_file);
2763+
}
2764+
close(fd);
2765+
if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) {
2766+
todo_list_release(&todo_list);
2767+
return -1;
2768+
}
2769+
2770+
/*
2771+
* The hashmap maps onelines to the respective todo list index.
2772+
*
2773+
* If any items need to be rearranged, the next[i] value will indicate
2774+
* which item was moved directly after the i'th.
2775+
*
2776+
* In that case, last[i] will indicate the index of the latest item to
2777+
* be moved to appear after the i'th.
2778+
*/
2779+
hashmap_init(&subject2item, (hashmap_cmp_fn) subject2item_cmp,
2780+
todo_list.nr);
2781+
ALLOC_ARRAY(next, todo_list.nr);
2782+
ALLOC_ARRAY(tail, todo_list.nr);
2783+
ALLOC_ARRAY(subjects, todo_list.nr);
2784+
for (i = 0; i < todo_list.nr; i++) {
2785+
struct strbuf buf = STRBUF_INIT;
2786+
struct todo_item *item = todo_list.items + i;
2787+
const char *commit_buffer, *subject, *p;
2788+
size_t subject_len;
2789+
int i2 = -1;
2790+
struct subject2item_entry *entry;
2791+
2792+
next[i] = tail[i] = -1;
2793+
if (item->command >= TODO_EXEC) {
2794+
subjects[i] = NULL;
2795+
continue;
2796+
}
2797+
2798+
if (is_fixup(item->command)) {
2799+
todo_list_release(&todo_list);
2800+
return error(_("the script was already rearranged."));
2801+
}
2802+
2803+
item->commit->util = item;
2804+
2805+
parse_commit(item->commit);
2806+
commit_buffer = get_commit_buffer(item->commit, NULL);
2807+
find_commit_subject(commit_buffer, &subject);
2808+
format_subject(&buf, subject, " ");
2809+
subject = subjects[i] = strbuf_detach(&buf, &subject_len);
2810+
unuse_commit_buffer(item->commit, commit_buffer);
2811+
if ((skip_prefix(subject, "fixup! ", &p) ||
2812+
skip_prefix(subject, "squash! ", &p))) {
2813+
struct commit *commit2;
2814+
2815+
for (;;) {
2816+
while (isspace(*p))
2817+
p++;
2818+
if (!skip_prefix(p, "fixup! ", &p) &&
2819+
!skip_prefix(p, "squash! ", &p))
2820+
break;
2821+
}
2822+
2823+
if ((entry = hashmap_get_from_hash(&subject2item,
2824+
strhash(p), p)))
2825+
/* found by title */
2826+
i2 = entry->i;
2827+
else if (!strchr(p, ' ') &&
2828+
(commit2 =
2829+
lookup_commit_reference_by_name(p)) &&
2830+
commit2->util)
2831+
/* found by commit name */
2832+
i2 = (struct todo_item *)commit2->util
2833+
- todo_list.items;
2834+
else {
2835+
/* copy can be a prefix of the commit subject */
2836+
for (i2 = 0; i2 < i; i2++)
2837+
if (subjects[i2] &&
2838+
starts_with(subjects[i2], p))
2839+
break;
2840+
if (i2 == i)
2841+
i2 = -1;
2842+
}
2843+
}
2844+
if (i2 >= 0) {
2845+
rearranged = 1;
2846+
todo_list.items[i].command =
2847+
starts_with(subject, "fixup!") ?
2848+
TODO_FIXUP : TODO_SQUASH;
2849+
if (next[i2] < 0)
2850+
next[i2] = i;
2851+
else
2852+
next[tail[i2]] = i;
2853+
tail[i2] = i;
2854+
} else if (!hashmap_get_from_hash(&subject2item,
2855+
strhash(subject), subject)) {
2856+
FLEX_ALLOC_MEM(entry, subject, subject, subject_len);
2857+
entry->i = i;
2858+
hashmap_entry_init(entry, strhash(entry->subject));
2859+
hashmap_put(&subject2item, entry);
2860+
}
2861+
}
2862+
2863+
if (rearranged) {
2864+
struct strbuf buf = STRBUF_INIT;
2865+
2866+
for (i = 0; i < todo_list.nr; i++) {
2867+
enum todo_command command = todo_list.items[i].command;
2868+
int cur = i;
2869+
2870+
/*
2871+
* Initially, all commands are 'pick's. If it is a
2872+
* fixup or a squash now, we have rearranged it.
2873+
*/
2874+
if (is_fixup(command))
2875+
continue;
2876+
2877+
while (cur >= 0) {
2878+
int offset = todo_list.items[cur].offset_in_buf;
2879+
int end_offset = cur + 1 < todo_list.nr ?
2880+
todo_list.items[cur + 1].offset_in_buf :
2881+
todo_list.buf.len;
2882+
char *bol = todo_list.buf.buf + offset;
2883+
char *eol = todo_list.buf.buf + end_offset;
2884+
2885+
/* replace 'pick', by 'fixup' or 'squash' */
2886+
command = todo_list.items[cur].command;
2887+
if (is_fixup(command)) {
2888+
strbuf_addstr(&buf,
2889+
todo_command_info[command].str);
2890+
bol += strcspn(bol, " \t");
2891+
}
2892+
2893+
strbuf_add(&buf, bol, eol - bol);
2894+
2895+
cur = next[cur];
2896+
}
2897+
}
2898+
2899+
fd = open(todo_file, O_WRONLY);
2900+
if (fd < 0)
2901+
res = error_errno(_("could not open '%s'"), todo_file);
2902+
else if (write(fd, buf.buf, buf.len) < 0)
2903+
res = error_errno(_("could not read '%s'."), todo_file);
2904+
else if (ftruncate(fd, buf.len) < 0)
2905+
res = error_errno(_("could not finish '%s'"),
2906+
todo_file);
2907+
close(fd);
2908+
strbuf_release(&buf);
2909+
}
2910+
2911+
free(next);
2912+
free(tail);
2913+
for (i = 0; i < todo_list.nr; i++)
2914+
free(subjects[i]);
2915+
free(subjects);
2916+
hashmap_free(&subject2item, 1);
2917+
todo_list_release(&todo_list);
2918+
2919+
return res;
2920+
}

sequencer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ int sequencer_make_script(int keep_empty, FILE *out,
5151
int transform_todo_ids(int shorten_sha1s);
5252
int check_todo_list(void);
5353
int skip_unnecessary_picks(void);
54+
int rearrange_squash(void);
5455

5556
extern const char sign_off_header[];
5657

t/t3415-rebase-autosquash.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ set_backup_editor () {
290290
test_set_editor "$PWD/backup-editor.sh"
291291
}
292292

293-
test_expect_failure 'autosquash with multiple empty patches' '
293+
test_expect_success 'autosquash with multiple empty patches' '
294294
test_tick &&
295295
git commit --allow-empty -m "empty" &&
296296
test_tick &&

0 commit comments

Comments
 (0)