Skip to content

Commit 09812a3

Browse files
dschogitster
authored andcommitted
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]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7bb4c8d commit 09812a3

File tree

4 files changed

+99
-39
lines changed

4 files changed

+99
-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: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2616,3 +2616,93 @@ int check_todo_list(void)
26162616

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

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)