Skip to content

Commit 6d28acb

Browse files
committed
rebase -i: check for missing commits in 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. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent b2f3188 commit 6d28acb

File tree

4 files changed

+136
-160
lines changed

4 files changed

+136
-160
lines changed

builtin/rebase--helper.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
1313
struct replay_opts opts = REPLAY_OPTS_INIT;
1414
int keep_empty = 0;
1515
enum {
16-
CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_SHA1S, EXPAND_SHA1S
16+
CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_SHA1S, EXPAND_SHA1S,
17+
CHECK_TODO_LIST
1718
} command = 0;
1819
struct option options[] = {
1920
OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
@@ -28,6 +29,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
2829
N_("shorten SHA-1s in the todo list"), SHORTEN_SHA1S),
2930
OPT_CMDMODE(0, "expand-sha1s", &command,
3031
N_("expand SHA-1s in the todo list"), EXPAND_SHA1S),
32+
OPT_CMDMODE(0, "check-todo-list", &command,
33+
N_("check the todo list"), CHECK_TODO_LIST),
3134
OPT_END()
3235
};
3336

@@ -50,5 +53,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
5053
return !!transform_todo_ids(1);
5154
if (command == EXPAND_SHA1S && argc == 1)
5255
return !!transform_todo_ids(0);
56+
if (command == CHECK_TODO_LIST && argc == 1)
57+
return !!check_todo_list();
5358
usage_with_options(builtin_rebase_helper_usage, options);
5459
}

git-rebase--interactive.sh

Lines changed: 5 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -880,96 +880,6 @@ add_exec_commands () {
880880
mv "$1.new" "$1"
881881
}
882882

883-
# Check if the SHA-1 passed as an argument is a
884-
# correct one, if not then print $2 in "$todo".badsha
885-
# $1: the SHA-1 to test
886-
# $2: the line number of the input
887-
# $3: the input filename
888-
check_commit_sha () {
889-
badsha=0
890-
if test -z "$1"
891-
then
892-
badsha=1
893-
else
894-
sha1_verif="$(git rev-parse --verify --quiet $1^{commit})"
895-
if test -z "$sha1_verif"
896-
then
897-
badsha=1
898-
fi
899-
fi
900-
901-
if test $badsha -ne 0
902-
then
903-
line="$(sed -n -e "${2}p" "$3")"
904-
warn "$(eval_gettext "\
905-
Warning: the SHA-1 is missing or isn't a commit in the following line:
906-
- \$line")"
907-
warn
908-
fi
909-
910-
return $badsha
911-
}
912-
913-
# prints the bad commits and bad commands
914-
# from the todolist in stdin
915-
check_bad_cmd_and_sha () {
916-
retval=0
917-
lineno=0
918-
while read -r command rest
919-
do
920-
lineno=$(( $lineno + 1 ))
921-
case $command in
922-
"$comment_char"*|''|noop|x|exec)
923-
# Doesn't expect a SHA-1
924-
;;
925-
"$cr")
926-
# Work around CR left by "read" (e.g. with Git for
927-
# Windows' Bash).
928-
;;
929-
pick|p|drop|d|reword|r|edit|e|squash|s|fixup|f)
930-
if ! check_commit_sha "${rest%%[ ]*}" "$lineno" "$1"
931-
then
932-
retval=1
933-
fi
934-
;;
935-
*)
936-
line="$(sed -n -e "${lineno}p" "$1")"
937-
warn "$(eval_gettext "\
938-
Warning: the command isn't recognized in the following line:
939-
- \$line")"
940-
warn
941-
retval=1
942-
;;
943-
esac
944-
done <"$1"
945-
return $retval
946-
}
947-
948-
# Print the list of the SHA-1 of the commits
949-
# from stdin to stdout
950-
todo_list_to_sha_list () {
951-
git stripspace --strip-comments |
952-
while read -r command sha1 rest
953-
do
954-
case $command in
955-
"$comment_char"*|''|noop|x|"exec")
956-
;;
957-
*)
958-
long_sha=$(git rev-list --no-walk "$sha1" 2>/dev/null)
959-
printf "%s\n" "$long_sha"
960-
;;
961-
esac
962-
done
963-
}
964-
965-
# Use warn for each line in stdin
966-
warn_lines () {
967-
while read -r line
968-
do
969-
warn " - $line"
970-
done
971-
}
972-
973883
# Switch to the branch in $into and notify it in the reflog
974884
checkout_onto () {
975885
GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
@@ -984,74 +894,6 @@ get_missing_commit_check_level () {
984894
printf '%s' "$check_level" | tr 'A-Z' 'a-z'
985895
}
986896

987-
# Check if the user dropped some commits by mistake
988-
# Behaviour determined by rebase.missingCommitsCheck.
989-
# Check if there is an unrecognized command or a
990-
# bad SHA-1 in a command.
991-
check_todo_list () {
992-
raise_error=f
993-
994-
check_level=$(get_missing_commit_check_level)
995-
996-
case "$check_level" in
997-
warn|error)
998-
# Get the SHA-1 of the commits
999-
todo_list_to_sha_list <"$todo".backup >"$todo".oldsha1
1000-
todo_list_to_sha_list <"$todo" >"$todo".newsha1
1001-
1002-
# Sort the SHA-1 and compare them
1003-
sort -u "$todo".oldsha1 >"$todo".oldsha1+
1004-
mv "$todo".oldsha1+ "$todo".oldsha1
1005-
sort -u "$todo".newsha1 >"$todo".newsha1+
1006-
mv "$todo".newsha1+ "$todo".newsha1
1007-
comm -2 -3 "$todo".oldsha1 "$todo".newsha1 >"$todo".miss
1008-
1009-
# Warn about missing commits
1010-
if test -s "$todo".miss
1011-
then
1012-
test "$check_level" = error && raise_error=t
1013-
1014-
warn "$(gettext "\
1015-
Warning: some commits may have been dropped accidentally.
1016-
Dropped commits (newer to older):")"
1017-
1018-
# Make the list user-friendly and display
1019-
opt="--no-walk=sorted --format=oneline --abbrev-commit --stdin"
1020-
git rev-list $opt <"$todo".miss | warn_lines
1021-
1022-
warn "$(gettext "\
1023-
To avoid this message, use \"drop\" to explicitly remove a commit.
1024-
1025-
Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1026-
The possible behaviours are: ignore, warn, error.")"
1027-
warn
1028-
fi
1029-
;;
1030-
ignore)
1031-
;;
1032-
*)
1033-
warn "$(eval_gettext "Unrecognized setting \$check_level for option rebase.missingCommitsCheck. Ignoring.")"
1034-
;;
1035-
esac
1036-
1037-
if ! check_bad_cmd_and_sha "$todo"
1038-
then
1039-
raise_error=t
1040-
fi
1041-
1042-
if test $raise_error = t
1043-
then
1044-
# Checkout before the first commit of the
1045-
# rebase: this way git rebase --continue
1046-
# will work correctly as it expects HEAD to be
1047-
# placed before the commit of the next action
1048-
checkout_onto
1049-
1050-
warn "$(gettext "You can fix this with 'git rebase --edit-todo'.")"
1051-
die "$(gettext "Or you can abort the rebase with 'git rebase --abort'.")"
1052-
fi
1053-
}
1054-
1055897
# The whole contents of this file is run by dot-sourcing it from
1056898
# inside a shell function. It used to be that "return"s we see
1057899
# below were not inside any function, and expected to return
@@ -1315,7 +1157,11 @@ git_sequence_editor "$todo" ||
13151157
has_action "$todo" ||
13161158
return 2
13171159

1318-
check_todo_list
1160+
git rebase--helper --check-todo-list || {
1161+
ret=$?
1162+
checkout_onto
1163+
exit $ret
1164+
}
13191165

13201166
expand_todo_ids
13211167

sequencer.c

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2450,3 +2450,127 @@ int transform_todo_ids(int shorten_sha1s)
24502450
todo_list_release(&todo_list);
24512451
return 0;
24522452
}
2453+
2454+
enum check_level {
2455+
CHECK_IGNORE = 0, CHECK_WARN, CHECK_ERROR
2456+
};
2457+
2458+
static enum check_level get_missing_commit_check_level(void)
2459+
{
2460+
const char *value;
2461+
2462+
if (git_config_get_value("rebase.missingcommitscheck", &value) ||
2463+
!strcasecmp("ignore", value))
2464+
return CHECK_IGNORE;
2465+
if (!strcasecmp("warn", value))
2466+
return CHECK_WARN;
2467+
if (!strcasecmp("error", value))
2468+
return CHECK_ERROR;
2469+
warning(_("Unrecognized setting $check_level for option"
2470+
"rebase.missingCommitsCheck. Ignoring."));
2471+
return CHECK_IGNORE;
2472+
}
2473+
2474+
/*
2475+
* Check if the user dropped some commits by mistake
2476+
* Behaviour determined by rebase.missingCommitsCheck.
2477+
* Check if there is an unrecognized command or a
2478+
* bad SHA-1 in a command.
2479+
*/
2480+
int check_todo_list(void)
2481+
{
2482+
enum check_level check_level = get_missing_commit_check_level();
2483+
struct strbuf todo_file = STRBUF_INIT;
2484+
struct todo_list todo_list = TODO_LIST_INIT;
2485+
struct commit_list *missing = NULL;
2486+
int raise_error = 0, res = 0, fd, i;
2487+
2488+
strbuf_addstr(&todo_file, rebase_path_todo());
2489+
fd = open(todo_file.buf, O_RDONLY);
2490+
if (fd < 0) {
2491+
res = error_errno(_("Could not open %s"), todo_file.buf);
2492+
goto leave_check;
2493+
}
2494+
if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
2495+
close(fd);
2496+
res = error(_("Could not read %s."), todo_file.buf);
2497+
goto leave_check;
2498+
}
2499+
close(fd);
2500+
raise_error = res =
2501+
parse_insn_buffer(todo_list.buf.buf, &todo_list);
2502+
2503+
if (check_level == CHECK_IGNORE)
2504+
goto leave_check;
2505+
2506+
/* Get the SHA-1 of the commits */
2507+
for (i = 0; i < todo_list.nr; i++) {
2508+
struct commit *commit = todo_list.items[i].commit;
2509+
if (commit)
2510+
commit->util = todo_list.items + i;
2511+
}
2512+
2513+
todo_list_release(&todo_list);
2514+
strbuf_addstr(&todo_file, ".backup");
2515+
fd = open(todo_file.buf, O_RDONLY);
2516+
if (fd < 0) {
2517+
res = error_errno(_("Could not open %s"), todo_file.buf);
2518+
goto leave_check;
2519+
}
2520+
if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
2521+
close(fd);
2522+
res = error(_("Could not read %s."), todo_file.buf);
2523+
goto leave_check;
2524+
}
2525+
close(fd);
2526+
strbuf_release(&todo_file);
2527+
res = !!parse_insn_buffer(todo_list.buf.buf, &todo_list);
2528+
2529+
/* Find commits that are missing after editing */
2530+
for (i = 0; i < todo_list.nr; i++) {
2531+
struct commit *commit = todo_list.items[i].commit;
2532+
if (commit && !commit->util) {
2533+
commit_list_insert(commit, &missing);
2534+
commit->util = todo_list.items + i;
2535+
}
2536+
}
2537+
2538+
/* Warn about missing commits */
2539+
if (!missing)
2540+
goto leave_check;
2541+
2542+
if (check_level == CHECK_ERROR)
2543+
raise_error = res = 1;
2544+
2545+
fprintf(stderr,
2546+
_("Warning: some commits may have been dropped accidentally.\n"
2547+
"Dropped commits (newer to older):\n"));
2548+
2549+
/* Make the list user-friendly and display */
2550+
while (missing) {
2551+
struct commit *commit = pop_commit(&missing);
2552+
struct todo_item *item = commit->util;
2553+
2554+
fprintf(stderr, " - %s %.*s\n", short_commit_name(commit),
2555+
item->arg_len, item->arg);
2556+
}
2557+
free_commit_list(missing);
2558+
2559+
fprintf(stderr, _("To avoid this message, use \"drop\" to "
2560+
"explicitly remove a commit.\n\n"
2561+
"Use 'git config rebase.missingCommitsCheck' to change "
2562+
"the level of warnings.\n"
2563+
"The possible behaviours are: ignore, warn, error.\n\n"));
2564+
2565+
leave_check:
2566+
strbuf_release(&todo_file);
2567+
todo_list_release(&todo_list);
2568+
2569+
if (raise_error)
2570+
fprintf(stderr,
2571+
_("You can fix this with 'git rebase --edit-todo'.\n"
2572+
"Or you can abort the rebase with 'git rebase"
2573+
" --abort'.\n"));
2574+
2575+
return res;
2576+
}

sequencer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ int sequencer_make_script(int keep_empty, FILE *out,
6262
int argc, const char **argv);
6363

6464
int transform_todo_ids(int shorten_sha1s);
65+
int check_todo_list(void);
6566

6667
extern const char sign_off_header[];
6768

0 commit comments

Comments
 (0)