Skip to content

Commit 1d3d10b

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. While at it, clarify how the fixup/squash feature works in `git rebase -i`'s man page. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 6e4c116 commit 1d3d10b

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
@@ -744,94 +744,6 @@ collapse_todo_ids() {
744744
git rebase--helper --shorten-sha1s
745745
}
746746

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

10931005
test -s "$todo" || echo noop >> "$todo"
1094-
test -n "$autosquash" && rearrange_squash "$todo"
1006+
test -z "$autosquash" || git rebase--helper --rearrange-squash || exit
10951007
test -n "$cmd" && add_exec_commands "$todo"
10961008

10971009
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

@@ -2724,3 +2725,197 @@ int skip_unnecessary_picks(void)
27242725

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

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
@@ -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)