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 <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Johannes Schindelin
2017-07-14 16:45:21 +02:00
committed by Junio C Hamano
parent 47d4ac019b
commit 943999493f
4 changed files with 134 additions and 160 deletions

View File

@ -2519,3 +2519,125 @@ int transform_todo_ids(int shorten_ids)
todo_list_release(&todo_list);
return 0;
}
enum check_level {
CHECK_IGNORE = 0, CHECK_WARN, CHECK_ERROR
};
static enum check_level get_missing_commit_check_level(void)
{
const char *value;
if (git_config_get_value("rebase.missingcommitscheck", &value) ||
!strcasecmp("ignore", value))
return CHECK_IGNORE;
if (!strcasecmp("warn", value))
return CHECK_WARN;
if (!strcasecmp("error", value))
return CHECK_ERROR;
warning(_("unrecognized setting %s for option"
"rebase.missingCommitsCheck. Ignoring."), value);
return CHECK_IGNORE;
}
/*
* Check if the user dropped some commits by mistake
* Behaviour determined by rebase.missingCommitsCheck.
* Check if there is an unrecognized command or a
* bad SHA-1 in a command.
*/
int check_todo_list(void)
{
enum check_level check_level = get_missing_commit_check_level();
struct strbuf todo_file = STRBUF_INIT;
struct todo_list todo_list = TODO_LIST_INIT;
struct strbuf missing = STRBUF_INIT;
int advise_to_edit_todo = 0, res = 0, fd, i;
strbuf_addstr(&todo_file, rebase_path_todo());
fd = open(todo_file.buf, O_RDONLY);
if (fd < 0) {
res = error_errno(_("could not open '%s'"), todo_file.buf);
goto leave_check;
}
if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
close(fd);
res = error(_("could not read '%s'."), todo_file.buf);
goto leave_check;
}
close(fd);
advise_to_edit_todo = res =
parse_insn_buffer(todo_list.buf.buf, &todo_list);
if (res || check_level == CHECK_IGNORE)
goto leave_check;
/* Mark the commits in git-rebase-todo as seen */
for (i = 0; i < todo_list.nr; i++) {
struct commit *commit = todo_list.items[i].commit;
if (commit)
commit->util = (void *)1;
}
todo_list_release(&todo_list);
strbuf_addstr(&todo_file, ".backup");
fd = open(todo_file.buf, O_RDONLY);
if (fd < 0) {
res = error_errno(_("could not open '%s'"), todo_file.buf);
goto leave_check;
}
if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
close(fd);
res = error(_("could not read '%s'."), todo_file.buf);
goto leave_check;
}
close(fd);
strbuf_release(&todo_file);
res = !!parse_insn_buffer(todo_list.buf.buf, &todo_list);
/* Find commits in git-rebase-todo.backup yet unseen */
for (i = todo_list.nr - 1; i >= 0; i--) {
struct todo_item *item = todo_list.items + i;
struct commit *commit = item->commit;
if (commit && !commit->util) {
strbuf_addf(&missing, " - %s %.*s\n",
short_commit_name(commit),
item->arg_len, item->arg);
commit->util = (void *)1;
}
}
/* Warn about missing commits */
if (!missing.len)
goto leave_check;
if (check_level == CHECK_ERROR)
advise_to_edit_todo = res = 1;
fprintf(stderr,
_("Warning: some commits may have been dropped accidentally.\n"
"Dropped commits (newer to older):\n"));
/* Make the list user-friendly and display */
fputs(missing.buf, stderr);
strbuf_release(&missing);
fprintf(stderr, _("To avoid this message, use \"drop\" to "
"explicitly remove a commit.\n\n"
"Use 'git config rebase.missingCommitsCheck' to change "
"the level of warnings.\n"
"The possible behaviours are: ignore, warn, error.\n\n"));
leave_check:
strbuf_release(&todo_file);
todo_list_release(&todo_list);
if (advise_to_edit_todo)
fprintf(stderr,
_("You can fix this with 'git rebase --edit-todo' "
"and then run 'git rebase --continue'.\n"
"Or you can abort the rebase with 'git rebase"
" --abort'.\n"));
return res;
}