Skip to content

Commit 1a5e609

Browse files
committed
rebase -i: skip unnecessary picks using the rebase--helper
In particular on Windows, where shell scripts are even more expensive than on MacOSX or Linux, it makes sense to move a loop that forks Git at least once for every line in the todo list into a builtin. Note: The original code did not try to skip unnecessary picks of root commits but punts instead (probably --root was not considered common enough of a use case to bother optimizing). We do the same, for now. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 38bf320 commit 1a5e609

File tree

4 files changed

+116
-39
lines changed

4 files changed

+116
-39
lines changed

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
17+
CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS
1818
} command = 0;
1919
struct option options[] = {
2020
OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
@@ -31,6 +31,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
3131
N_("expand SHA-1s in the todo list"), EXPAND_SHA1S),
3232
OPT_CMDMODE(0, "check-todo-list", &command,
3333
N_("check the todo list"), CHECK_TODO_LIST),
34+
OPT_CMDMODE(0, "skip-unnecessary-picks", &command,
35+
N_("skip unnecessary picks"), SKIP_UNNECESSARY_PICKS),
3436
OPT_END()
3537
};
3638

@@ -55,5 +57,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
5557
return !!transform_todo_ids(0);
5658
if (command == CHECK_TODO_LIST && argc == 1)
5759
return !!check_todo_list();
60+
if (command == SKIP_UNNECESSARY_PICKS && argc == 1)
61+
return !!skip_unnecessary_picks();
5862
usage_with_options(builtin_rebase_helper_usage, options);
5963
}

git-rebase--interactive.sh

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -713,43 +713,6 @@ do_rest () {
713713
done
714714
}
715715

716-
# skip picking commits whose parents are unchanged
717-
skip_unnecessary_picks () {
718-
fd=3
719-
while read -r command rest
720-
do
721-
# fd=3 means we skip the command
722-
case "$fd,$command" in
723-
3,pick|3,p)
724-
# pick a commit whose parent is current $onto -> skip
725-
sha1=${rest%% *}
726-
case "$(git rev-parse --verify --quiet "$sha1"^)" in
727-
"$onto"*)
728-
onto=$sha1
729-
;;
730-
*)
731-
fd=1
732-
;;
733-
esac
734-
;;
735-
3,"$comment_char"*|3,)
736-
# copy comments
737-
;;
738-
*)
739-
fd=1
740-
;;
741-
esac
742-
printf '%s\n' "$command${rest:+ }$rest" >&$fd
743-
done <"$todo" >"$todo.new" 3>>"$done" &&
744-
mv -f "$todo".new "$todo" &&
745-
case "$(peek_next_command)" in
746-
squash|s|fixup|f)
747-
record_in_rewritten "$onto"
748-
;;
749-
esac ||
750-
die "$(gettext "Could not skip unnecessary pick commands")"
751-
}
752-
753716
transform_todo_ids () {
754717
while read -r command rest
755718
do
@@ -1172,7 +1135,9 @@ git rebase--helper --check-todo-list || {
11721135

11731136
expand_todo_ids
11741137

1175-
test -d "$rewritten" || test -n "$force_rebase" || skip_unnecessary_picks
1138+
test -d "$rewritten" || test -n "$force_rebase" ||
1139+
onto="$(git rebase--helper --skip-unnecessary-picks)" ||
1140+
die "Could not skip unnecessary pick commands"
11761141

11771142
checkout_onto
11781143
if test -z "$rebase_root" && test ! -d "$rewritten"

sequencer.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,3 +2617,110 @@ int check_todo_list(void)
26172617

26182618
return res;
26192619
}
2620+
2621+
/* skip picking commits whose parents are unchanged */
2622+
int skip_unnecessary_picks(void)
2623+
{
2624+
const char *todo_file = rebase_path_todo();
2625+
struct strbuf buf = STRBUF_INIT;
2626+
struct todo_list todo_list = TODO_LIST_INIT;
2627+
struct object_id onto_oid, *oid = &onto_oid, *parent_oid;
2628+
int fd, i;
2629+
2630+
if (!read_oneliner(&buf, rebase_path_onto(), 0))
2631+
return error(_("could not read 'onto'"));
2632+
if (get_sha1(buf.buf, onto_oid.hash)) {
2633+
strbuf_release(&buf);
2634+
return error(_("need a HEAD to fixup"));
2635+
}
2636+
strbuf_release(&buf);
2637+
2638+
fd = open(todo_file, O_RDONLY);
2639+
if (fd < 0) {
2640+
return error_errno(_("could not open '%s'"), todo_file);
2641+
}
2642+
if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
2643+
close(fd);
2644+
return error(_("could not read '%s'."), todo_file);
2645+
}
2646+
close(fd);
2647+
if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) {
2648+
todo_list_release(&todo_list);
2649+
return -1;
2650+
}
2651+
2652+
for (i = 0; i < todo_list.nr; i++) {
2653+
struct todo_item *item = todo_list.items + i;
2654+
2655+
if (item->command >= TODO_NOOP)
2656+
continue;
2657+
if (item->command != TODO_PICK)
2658+
break;
2659+
if (parse_commit(item->commit)) {
2660+
todo_list_release(&todo_list);
2661+
return error(_("could not parse commit '%s'"),
2662+
oid_to_hex(&item->commit->object.oid));
2663+
}
2664+
if (!item->commit->parents)
2665+
break; /* root commit */
2666+
if (item->commit->parents->next)
2667+
break; /* merge commit */
2668+
parent_oid = &item->commit->parents->item->object.oid;
2669+
if (hashcmp(parent_oid->hash, oid->hash))
2670+
break;
2671+
oid = &item->commit->object.oid;
2672+
}
2673+
if (i > 0) {
2674+
int offset = i < todo_list.nr ?
2675+
todo_list.items[i].offset_in_buf : todo_list.buf.len;
2676+
const char *done_path = rebase_path_done();
2677+
2678+
fd = open(done_path, O_CREAT | O_WRONLY | O_APPEND, 0666);
2679+
if (fd < 0) {
2680+
error_errno(_("could not open '%s' for writing"),
2681+
done_path);
2682+
todo_list_release(&todo_list);
2683+
return -1;
2684+
}
2685+
if (write_in_full(fd, todo_list.buf.buf, offset) < 0) {
2686+
error_errno(_("could not write to '%s'"), done_path);
2687+
todo_list_release(&todo_list);
2688+
close(fd);
2689+
return -1;
2690+
}
2691+
close(fd);
2692+
2693+
fd = open(rebase_path_todo(), O_WRONLY, 0666);
2694+
if (fd < 0) {
2695+
error_errno(_("could not open '%s' for writing"),
2696+
rebase_path_todo());
2697+
todo_list_release(&todo_list);
2698+
return -1;
2699+
}
2700+
if (write_in_full(fd, todo_list.buf.buf + offset,
2701+
todo_list.buf.len - offset) < 0) {
2702+
error_errno(_("could not write to '%s'"),
2703+
rebase_path_todo());
2704+
close(fd);
2705+
todo_list_release(&todo_list);
2706+
return -1;
2707+
}
2708+
if (ftruncate(fd, todo_list.buf.len - offset) < 0) {
2709+
error_errno(_("could not truncate '%s'"),
2710+
rebase_path_todo());
2711+
todo_list_release(&todo_list);
2712+
close(fd);
2713+
return -1;
2714+
}
2715+
close(fd);
2716+
2717+
todo_list.current = i;
2718+
if (is_fixup(peek_command(&todo_list, 0)))
2719+
record_in_rewritten(oid, peek_command(&todo_list, 0));
2720+
}
2721+
2722+
todo_list_release(&todo_list);
2723+
printf("%s\n", oid_to_hex(oid));
2724+
2725+
return 0;
2726+
}

sequencer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ int sequencer_make_script(int keep_empty, FILE *out,
5050

5151
int transform_todo_ids(int shorten_sha1s);
5252
int check_todo_list(void);
53+
int skip_unnecessary_picks(void);
5354

5455
extern const char sign_off_header[];
5556

0 commit comments

Comments
 (0)