Skip to content

Commit 9733724

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 84156d1 commit 9733724

File tree

6 files changed

+214
-98
lines changed

6 files changed

+214
-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: 197 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

@@ -2706,3 +2707,199 @@ int skip_unnecessary_picks(void)
27062707

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

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)