Skip to content

Commit 38bf320

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 975e774 commit 38bf320

File tree

4 files changed

+137
-160
lines changed

4 files changed

+137
-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
@@ -890,96 +890,6 @@ add_exec_commands () {
890890
mv "$1.new" "$1"
891891
}
892892

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

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

1325-
check_todo_list
1167+
git rebase--helper --check-todo-list || {
1168+
ret=$?
1169+
checkout_onto
1170+
exit $ret
1171+
}
13261172

13271173
expand_todo_ids
13281174

sequencer.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,3 +2492,128 @@ int transform_todo_ids(int shorten_sha1s)
24922492
todo_list_release(&todo_list);
24932493
return 0;
24942494
}
2495+
2496+
enum check_level {
2497+
CHECK_IGNORE = 0, CHECK_WARN, CHECK_ERROR
2498+
};
2499+
2500+
static enum check_level get_missing_commit_check_level(void)
2501+
{
2502+
const char *value;
2503+
2504+
if (git_config_get_value("rebase.missingcommitscheck", &value) ||
2505+
!strcasecmp("ignore", value))
2506+
return CHECK_IGNORE;
2507+
if (!strcasecmp("warn", value))
2508+
return CHECK_WARN;
2509+
if (!strcasecmp("error", value))
2510+
return CHECK_ERROR;
2511+
warning(_("unrecognized setting %s for option"
2512+
"rebase.missingCommitsCheck. Ignoring."), value);
2513+
return CHECK_IGNORE;
2514+
}
2515+
2516+
/*
2517+
* Check if the user dropped some commits by mistake
2518+
* Behaviour determined by rebase.missingCommitsCheck.
2519+
* Check if there is an unrecognized command or a
2520+
* bad SHA-1 in a command.
2521+
*/
2522+
int check_todo_list(void)
2523+
{
2524+
enum check_level check_level = get_missing_commit_check_level();
2525+
struct strbuf todo_file = STRBUF_INIT;
2526+
struct todo_list todo_list = TODO_LIST_INIT;
2527+
struct commit_list *missing = NULL;
2528+
int raise_error = 0, res = 0, fd, i;
2529+
2530+
strbuf_addstr(&todo_file, rebase_path_todo());
2531+
fd = open(todo_file.buf, O_RDONLY);
2532+
if (fd < 0) {
2533+
res = error_errno(_("could not open '%s'"), todo_file.buf);
2534+
goto leave_check;
2535+
}
2536+
if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
2537+
close(fd);
2538+
res = error(_("could not read '%s'."), todo_file.buf);
2539+
goto leave_check;
2540+
}
2541+
close(fd);
2542+
raise_error = res =
2543+
parse_insn_buffer(todo_list.buf.buf, &todo_list);
2544+
2545+
if (check_level == CHECK_IGNORE)
2546+
goto leave_check;
2547+
2548+
/* Get the SHA-1 of the commits */
2549+
for (i = 0; i < todo_list.nr; i++) {
2550+
struct commit *commit = todo_list.items[i].commit;
2551+
if (commit)
2552+
commit->util = todo_list.items + i;
2553+
}
2554+
2555+
todo_list_release(&todo_list);
2556+
strbuf_addstr(&todo_file, ".backup");
2557+
fd = open(todo_file.buf, O_RDONLY);
2558+
if (fd < 0) {
2559+
res = error_errno(_("could not open '%s'"), todo_file.buf);
2560+
goto leave_check;
2561+
}
2562+
if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
2563+
close(fd);
2564+
res = error(_("could not read '%s'."), todo_file.buf);
2565+
goto leave_check;
2566+
}
2567+
close(fd);
2568+
strbuf_release(&todo_file);
2569+
res = !!parse_insn_buffer(todo_list.buf.buf, &todo_list);
2570+
2571+
/* Find commits that are missing after editing */
2572+
for (i = 0; i < todo_list.nr; i++) {
2573+
struct commit *commit = todo_list.items[i].commit;
2574+
if (commit && !commit->util) {
2575+
commit_list_insert(commit, &missing);
2576+
commit->util = todo_list.items + i;
2577+
}
2578+
}
2579+
2580+
/* Warn about missing commits */
2581+
if (!missing)
2582+
goto leave_check;
2583+
2584+
if (check_level == CHECK_ERROR)
2585+
raise_error = res = 1;
2586+
2587+
fprintf(stderr,
2588+
_("Warning: some commits may have been dropped accidentally.\n"
2589+
"Dropped commits (newer to older):\n"));
2590+
2591+
/* Make the list user-friendly and display */
2592+
while (missing) {
2593+
struct commit *commit = pop_commit(&missing);
2594+
struct todo_item *item = commit->util;
2595+
2596+
fprintf(stderr, " - %s %.*s\n", short_commit_name(commit),
2597+
item->arg_len, item->arg);
2598+
}
2599+
free_commit_list(missing);
2600+
2601+
fprintf(stderr, _("To avoid this message, use \"drop\" to "
2602+
"explicitly remove a commit.\n\n"
2603+
"Use 'git config rebase.missingCommitsCheck' to change "
2604+
"the level of warnings.\n"
2605+
"The possible behaviours are: ignore, warn, error.\n\n"));
2606+
2607+
leave_check:
2608+
strbuf_release(&todo_file);
2609+
todo_list_release(&todo_list);
2610+
2611+
if (raise_error)
2612+
fprintf(stderr,
2613+
_("You can fix this with 'git rebase --edit-todo' "
2614+
"and then run 'git rebase --continue'.\n"
2615+
"Or you can abort the rebase with 'git rebase"
2616+
" --abort'.\n"));
2617+
2618+
return res;
2619+
}

sequencer.h

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

5151
int transform_todo_ids(int shorten_sha1s);
52+
int check_todo_list(void);
5253

5354
extern const char sign_off_header[];
5455

0 commit comments

Comments
 (0)