Skip to content

Commit 3546c8d

Browse files
dschogitster
authored andcommitted
rebase -i: also expand/collapse the SHA-1s via the rebase--helper
This is crucial to improve performance on Windows, as the speed is now mostly dominated by the SHA-1 transformation (because it spawns a new rev-parse process for *every* line, and spawning processes is pretty slow from Git for Windows' MSYS2 Bash). Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1f4044a commit 3546c8d

File tree

4 files changed

+70
-26
lines changed

4 files changed

+70
-26
lines changed

builtin/rebase--helper.c

Lines changed: 9 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
struct replay_opts opts = REPLAY_OPTS_INIT;
1515
int keep_empty = 0;
1616
enum {
17-
CONTINUE = 1, ABORT, MAKE_SCRIPT
17+
CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_SHA1S, EXPAND_SHA1S
1818
} command = 0;
1919
struct option options[] = {
2020
OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
@@ -25,6 +25,10 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
2525
ABORT),
2626
OPT_CMDMODE(0, "make-script", &command,
2727
N_("make rebase script"), MAKE_SCRIPT),
28+
OPT_CMDMODE(0, "shorten-ids", &command,
29+
N_("shorten SHA-1s in the todo list"), SHORTEN_SHA1S),
30+
OPT_CMDMODE(0, "expand-ids", &command,
31+
N_("expand SHA-1s in the todo list"), EXPAND_SHA1S),
2832
OPT_END()
2933
};
3034

@@ -43,5 +47,9 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
4347
return !!sequencer_remove_state(&opts);
4448
if (command == MAKE_SCRIPT && argc > 1)
4549
return !!sequencer_make_script(keep_empty, stdout, argc, argv);
50+
if (command == SHORTEN_SHA1S && argc == 1)
51+
return !!transform_todo_ids(1);
52+
if (command == EXPAND_SHA1S && argc == 1)
53+
return !!transform_todo_ids(0);
4654
usage_with_options(builtin_rebase_helper_usage, options);
4755
}

git-rebase--interactive.sh

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -750,35 +750,12 @@ skip_unnecessary_picks () {
750750
die "$(gettext "Could not skip unnecessary pick commands")"
751751
}
752752

753-
transform_todo_ids () {
754-
while read -r command rest
755-
do
756-
case "$command" in
757-
"$comment_char"* | exec)
758-
# Be careful for oddball commands like 'exec'
759-
# that do not have a SHA-1 at the beginning of $rest.
760-
;;
761-
*)
762-
sha1=$(git rev-parse --verify --quiet "$@" ${rest%%[ ]*}) &&
763-
if test "a$rest" = "a${rest#*[ ]}"
764-
then
765-
rest=$sha1
766-
else
767-
rest="$sha1 ${rest#*[ ]}"
768-
fi
769-
;;
770-
esac
771-
printf '%s\n' "$command${rest:+ }$rest"
772-
done <"$todo" >"$todo.new" &&
773-
mv -f "$todo.new" "$todo"
774-
}
775-
776753
expand_todo_ids() {
777-
transform_todo_ids
754+
git rebase--helper --expand-ids
778755
}
779756

780757
collapse_todo_ids() {
781-
transform_todo_ids --short
758+
git rebase--helper --shorten-ids
782759
}
783760

784761
# Rearrange the todo list that has both "pick sha1 msg" and

sequencer.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2462,3 +2462,60 @@ int sequencer_make_script(int keep_empty, FILE *out,
24622462
strbuf_release(&buf);
24632463
return 0;
24642464
}
2465+
2466+
2467+
int transform_todo_ids(int shorten_ids)
2468+
{
2469+
const char *todo_file = rebase_path_todo();
2470+
struct todo_list todo_list = TODO_LIST_INIT;
2471+
int fd, res, i;
2472+
FILE *out;
2473+
2474+
strbuf_reset(&todo_list.buf);
2475+
fd = open(todo_file, O_RDONLY);
2476+
if (fd < 0)
2477+
return error_errno(_("could not open '%s'"), todo_file);
2478+
if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
2479+
close(fd);
2480+
return error(_("could not read '%s'."), todo_file);
2481+
}
2482+
close(fd);
2483+
2484+
res = parse_insn_buffer(todo_list.buf.buf, &todo_list);
2485+
if (res) {
2486+
todo_list_release(&todo_list);
2487+
return error(_("unusable todo list: '%s'"), todo_file);
2488+
}
2489+
2490+
out = fopen(todo_file, "w");
2491+
if (!out) {
2492+
todo_list_release(&todo_list);
2493+
return error(_("unable to open '%s' for writing"), todo_file);
2494+
}
2495+
for (i = 0; i < todo_list.nr; i++) {
2496+
struct todo_item *item = todo_list.items + i;
2497+
int bol = item->offset_in_buf;
2498+
const char *p = todo_list.buf.buf + bol;
2499+
int eol = i + 1 < todo_list.nr ?
2500+
todo_list.items[i + 1].offset_in_buf :
2501+
todo_list.buf.len;
2502+
2503+
if (item->command >= TODO_EXEC && item->command != TODO_DROP)
2504+
fwrite(p, eol - bol, 1, out);
2505+
else {
2506+
const char *id = shorten_ids ?
2507+
short_commit_name(item->commit) :
2508+
oid_to_hex(&item->commit->object.oid);
2509+
int len;
2510+
2511+
p += strspn(p, " \t"); /* left-trim command */
2512+
len = strcspn(p, " \t"); /* length of command */
2513+
2514+
fprintf(out, "%.*s %s %.*s\n",
2515+
len, p, id, item->arg_len, item->arg);
2516+
}
2517+
}
2518+
fclose(out);
2519+
todo_list_release(&todo_list);
2520+
return 0;
2521+
}

sequencer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ int sequencer_remove_state(struct replay_opts *opts);
4848
int sequencer_make_script(int keep_empty, FILE *out,
4949
int argc, const char **argv);
5050

51+
int transform_todo_ids(int shorten_ids);
52+
5153
extern const char sign_off_header[];
5254

5355
void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag);

0 commit comments

Comments
 (0)