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 <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Jul 14, 2017 at 16:45 UTC
3546c8d927d31048f0d6f41aa132ebdb82cf8bda
4 files changed
+70
-26
builtin/rebase--helper.c
+9
-1
@@ -14,7 +14,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
14
struct replay_opts opts = REPLAY_OPTS_INIT;
15
int keep_empty = 0;
16
enum {
17
- CONTINUE = 1, ABORT, MAKE_SCRIPT
17
+ CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_SHA1S, EXPAND_SHA1S
18
} command = 0;
19
struct option options[] = {
20
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)
25
ABORT),
26
OPT_CMDMODE(0, "make-script", &command,
27
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),
32
OPT_END()
33
};
34
@@ -43,5 +47,9 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
47
return !!sequencer_remove_state(&opts);
48
if (command == MAKE_SCRIPT && argc > 1)
49
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);
54
usage_with_options(builtin_rebase_helper_usage, options);
55
}
git-rebase--interactive.sh
+2
-25
@@ -750,35 +750,12 @@ skip_unnecessary_picks () {
750
die "$(gettext "Could not skip unnecessary pick commands")"
751
}
752
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
-
753
expand_todo_ids() {
777
- transform_todo_ids
754
+ git rebase--helper --expand-ids
755
}
756
757
collapse_todo_ids() {
781
- transform_todo_ids --short
758
+ git rebase--helper --shorten-ids
759
}
760
761
# Rearrange the todo list that has both "pick sha1 msg" and
sequencer.c
+57
@@ -2462,3 +2462,60 @@ int sequencer_make_script(int keep_empty, FILE *out,
2462
strbuf_release(&buf);
2463
return 0;
2464
}
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
+2
@@ -48,6 +48,8 @@ int sequencer_remove_state(struct replay_opts *opts);
48
int sequencer_make_script(int keep_empty, FILE *out,
49
int argc, const char **argv);
50
51
+int transform_todo_ids(int shorten_ids);
52
+
53
extern const char sign_off_header[];
54
55
void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag);