Merge branch 'ag/sequencer-reduce-rewriting-todo'

The scripted version of "git rebase -i" wrote and rewrote the todo
list many times during a single step of its operation, and the
recent C-rewrite made a faithful conversion of the logic to C.  The
implementation has been updated to carry necessary information
around in-core to avoid rewriting the same file over and over
unnecessarily.

* ag/sequencer-reduce-rewriting-todo:
  rebase--interactive: move transform_todo_file()
  sequencer: use edit_todo_list() in complete_action()
  rebase-interactive: rewrite edit_todo_list() to handle the initial edit
  rebase-interactive: append_todo_help() changes
  rebase-interactive: use todo_list_write_to_file() in edit_todo_list()
  sequencer: refactor skip_unnecessary_picks() to work on a todo_list
  rebase--interactive: move rearrange_squash_in_todo_file()
  rebase--interactive: move sequencer_add_exec_commands()
  sequencer: change complete_action() to use the refactored functions
  sequencer: make sequencer_make_script() write its script to a strbuf
  sequencer: refactor rearrange_squash() to work on a todo_list
  sequencer: refactor sequencer_add_exec_commands() to work on a todo_list
  sequencer: refactor check_todo_list() to work on a todo_list
  sequencer: introduce todo_list_write_to_file()
  sequencer: refactor transform_todos() to work on a todo_list
  sequencer: remove the 'arg' field from todo_item
  sequencer: make the todo_list structure public
  sequencer: changes in parse_insn_buffer()
This commit is contained in:
Junio C Hamano 2019-04-25 16:41:11 +09:00
commit e62e68d359
5 changed files with 579 additions and 508 deletions

View file

@ -14,6 +14,103 @@ static GIT_PATH_FUNC(path_state_dir, "rebase-merge/")
static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto")
static GIT_PATH_FUNC(path_interactive, "rebase-merge/interactive")
static int add_exec_commands(struct string_list *commands)
{
const char *todo_file = rebase_path_todo();
struct todo_list todo_list = TODO_LIST_INIT;
int res;
if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
return error_errno(_("could not read '%s'."), todo_file);
if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
&todo_list)) {
todo_list_release(&todo_list);
return error(_("unusable todo list: '%s'"), todo_file);
}
todo_list_add_exec_commands(&todo_list, commands);
res = todo_list_write_to_file(the_repository, &todo_list,
todo_file, NULL, NULL, -1, 0);
todo_list_release(&todo_list);
if (res)
return error_errno(_("could not write '%s'."), todo_file);
return 0;
}
static int rearrange_squash_in_todo_file(void)
{
const char *todo_file = rebase_path_todo();
struct todo_list todo_list = TODO_LIST_INIT;
int res = 0;
if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
return error_errno(_("could not read '%s'."), todo_file);
if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
&todo_list)) {
todo_list_release(&todo_list);
return error(_("unusable todo list: '%s'"), todo_file);
}
res = todo_list_rearrange_squash(&todo_list);
if (!res)
res = todo_list_write_to_file(the_repository, &todo_list,
todo_file, NULL, NULL, -1, 0);
todo_list_release(&todo_list);
if (res)
return error_errno(_("could not write '%s'."), todo_file);
return 0;
}
static int transform_todo_file(unsigned flags)
{
const char *todo_file = rebase_path_todo();
struct todo_list todo_list = TODO_LIST_INIT;
int res;
if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
return error_errno(_("could not read '%s'."), todo_file);
if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
&todo_list)) {
todo_list_release(&todo_list);
return error(_("unusable todo list: '%s'"), todo_file);
}
res = todo_list_write_to_file(the_repository, &todo_list, todo_file,
NULL, NULL, -1, flags);
todo_list_release(&todo_list);
if (res)
return error_errno(_("could not write '%s'."), todo_file);
return 0;
}
static int edit_todo_file(unsigned flags)
{
const char *todo_file = rebase_path_todo();
struct todo_list todo_list = TODO_LIST_INIT,
new_todo = TODO_LIST_INIT;
int res = 0;
if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
return error_errno(_("could not read '%s'."), todo_file);
strbuf_stripspace(&todo_list.buf, 1);
res = edit_todo_list(the_repository, &todo_list, &new_todo, NULL, NULL, flags);
if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file,
NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS)))
res = error_errno(_("could not write '%s'"), todo_file);
todo_list_release(&todo_list);
todo_list_release(&new_todo);
return res;
}
static int get_revision_ranges(const char *upstream, const char *onto,
const char **head_hash,
char **revisions, char **shortrevisions)
@ -66,13 +163,13 @@ static int do_interactive_rebase(struct replay_opts *opts, unsigned flags,
const char *onto, const char *onto_name,
const char *squash_onto, const char *head_name,
const char *restrict_revision, char *raw_strategies,
const char *cmd, unsigned autosquash)
struct string_list *commands, unsigned autosquash)
{
int ret;
const char *head_hash = NULL;
char *revisions = NULL, *shortrevisions = NULL;
struct argv_array make_script_args = ARGV_ARRAY_INIT;
FILE *todo_list;
struct todo_list todo_list = TODO_LIST_INIT;
if (prepare_branch_to_be_rebased(opts, switch_to))
return -1;
@ -94,34 +191,29 @@ static int do_interactive_rebase(struct replay_opts *opts, unsigned flags,
if (!upstream && squash_onto)
write_file(path_squash_onto(), "%s\n", squash_onto);
todo_list = fopen(rebase_path_todo(), "w");
if (!todo_list) {
free(revisions);
free(shortrevisions);
return error_errno(_("could not open %s"), rebase_path_todo());
}
argv_array_pushl(&make_script_args, "", revisions, NULL);
if (restrict_revision)
argv_array_push(&make_script_args, restrict_revision);
ret = sequencer_make_script(the_repository, todo_list,
ret = sequencer_make_script(the_repository, &todo_list.buf,
make_script_args.argc, make_script_args.argv,
flags);
fclose(todo_list);
if (ret)
error(_("could not generate todo list"));
else {
discard_cache();
ret = complete_action(the_repository, opts, flags,
shortrevisions, onto_name, onto,
head_hash, cmd, autosquash);
if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
&todo_list))
BUG("unusable todo list");
ret = complete_action(the_repository, opts, flags, shortrevisions, onto_name,
onto, head_hash, commands, autosquash, &todo_list);
}
free(revisions);
free(shortrevisions);
todo_list_release(&todo_list);
argv_array_clear(&make_script_args);
return ret;
@ -140,6 +232,7 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
const char *onto = NULL, *onto_name = NULL, *restrict_revision = NULL,
*squash_onto = NULL, *upstream = NULL, *head_name = NULL,
*switch_to = NULL, *cmd = NULL;
struct string_list commands = STRING_LIST_INIT_DUP;
char *raw_strategies = NULL;
enum {
NONE = 0, CONTINUE, SKIP, EDIT_TODO, SHOW_CURRENT_PATCH,
@ -224,6 +317,14 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
warning(_("--[no-]rebase-cousins has no effect without "
"--rebase-merges"));
if (cmd && *cmd) {
string_list_split(&commands, cmd, '\n', -1);
/* rebase.c adds a new line to cmd after every command,
* so here the last command is always empty */
string_list_remove_empty_items(&commands, 0);
}
switch (command) {
case NONE:
if (!onto && !upstream)
@ -231,7 +332,7 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
ret = do_interactive_rebase(&opts, flags, switch_to, upstream, onto,
onto_name, squash_onto, head_name, restrict_revision,
raw_strategies, cmd, autosquash);
raw_strategies, &commands, autosquash);
break;
case SKIP: {
struct string_list merge_rr = STRING_LIST_INIT_DUP;
@ -243,7 +344,7 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
break;
}
case EDIT_TODO:
ret = edit_todo_list(the_repository, flags);
ret = edit_todo_file(flags);
break;
case SHOW_CURRENT_PATCH: {
struct child_process cmd = CHILD_PROCESS_INIT;
@ -256,20 +357,21 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
}
case SHORTEN_OIDS:
case EXPAND_OIDS:
ret = transform_todos(the_repository, flags);
ret = transform_todo_file(flags);
break;
case CHECK_TODO_LIST:
ret = check_todo_list(the_repository);
ret = check_todo_list_from_file(the_repository);
break;
case REARRANGE_SQUASH:
ret = rearrange_squash(the_repository);
ret = rearrange_squash_in_todo_file();
break;
case ADD_EXEC:
ret = sequencer_add_exec_commands(the_repository, cmd);
ret = add_exec_commands(&commands);
break;
default:
BUG("invalid command '%d'", command);
}
string_list_clear(&commands, 0);
return !!ret;
}

View file

@ -1,10 +1,35 @@
#include "cache.h"
#include "commit.h"
#include "rebase-interactive.h"
#include "sequencer.h"
#include "rebase-interactive.h"
#include "strbuf.h"
#include "commit-slab.h"
#include "config.h"
void append_todo_help(unsigned edit_todo, unsigned keep_empty,
enum missing_commit_check_level {
MISSING_COMMIT_CHECK_IGNORE = 0,
MISSING_COMMIT_CHECK_WARN,
MISSING_COMMIT_CHECK_ERROR
};
static enum missing_commit_check_level get_missing_commit_check_level(void)
{
const char *value;
if (git_config_get_value("rebase.missingcommitscheck", &value) ||
!strcasecmp("ignore", value))
return MISSING_COMMIT_CHECK_IGNORE;
if (!strcasecmp("warn", value))
return MISSING_COMMIT_CHECK_WARN;
if (!strcasecmp("error", value))
return MISSING_COMMIT_CHECK_ERROR;
warning(_("unrecognized setting %s for option "
"rebase.missingCommitsCheck. Ignoring."), value);
return MISSING_COMMIT_CHECK_IGNORE;
}
void append_todo_help(unsigned keep_empty, int command_count,
const char *shortrevisions, const char *shortonto,
struct strbuf *buf)
{
const char *msg = _("\nCommands:\n"
@ -24,6 +49,15 @@ void append_todo_help(unsigned edit_todo, unsigned keep_empty,
". specified). Use -c <commit> to reword the commit message.\n"
"\n"
"These lines can be re-ordered; they are executed from top to bottom.\n");
unsigned edit_todo = !(shortrevisions && shortonto);
if (!edit_todo) {
strbuf_addch(buf, '\n');
strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)",
"Rebase %s onto %s (%d commands)",
command_count),
shortrevisions, shortonto, command_count);
}
strbuf_add_commented_lines(buf, msg, strlen(msg));
@ -53,39 +87,103 @@ void append_todo_help(unsigned edit_todo, unsigned keep_empty,
}
}
int edit_todo_list(struct repository *r, unsigned flags)
int edit_todo_list(struct repository *r, struct todo_list *todo_list,
struct todo_list *new_todo, const char *shortrevisions,
const char *shortonto, unsigned flags)
{
struct strbuf buf = STRBUF_INIT;
const char *todo_file = rebase_path_todo();
unsigned initial = shortrevisions && shortonto;
if (strbuf_read_file(&buf, todo_file, 0) < 0)
return error_errno(_("could not read '%s'."), todo_file);
/* If the user is editing the todo list, we first try to parse
* it. If there is an error, we do not return, because the user
* might want to fix it in the first place. */
if (!initial)
todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list);
strbuf_stripspace(&buf, 1);
if (write_message(buf.buf, buf.len, todo_file, 0)) {
strbuf_release(&buf);
return -1;
}
if (todo_list_write_to_file(r, todo_list, todo_file, shortrevisions, shortonto,
-1, flags | TODO_LIST_SHORTEN_IDS | TODO_LIST_APPEND_TODO_HELP))
return error_errno(_("could not write '%s'"), todo_file);
strbuf_release(&buf);
if (initial && copy_file(rebase_path_todo_backup(), todo_file, 0666))
return error(_("could not copy '%s' to '%s'."), todo_file,
rebase_path_todo_backup());
transform_todos(r, flags | TODO_LIST_SHORTEN_IDS);
if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
return -2;
if (strbuf_read_file(&buf, todo_file, 0) < 0)
return error_errno(_("could not read '%s'."), todo_file);
strbuf_stripspace(&new_todo->buf, 1);
if (initial && new_todo->buf.len == 0)
return -3;
append_todo_help(1, 0, &buf);
if (write_message(buf.buf, buf.len, todo_file, 0)) {
strbuf_release(&buf);
return -1;
}
strbuf_release(&buf);
if (launch_sequence_editor(todo_file, NULL, NULL))
return -1;
transform_todos(r, flags & ~(TODO_LIST_SHORTEN_IDS));
/* For the initial edit, the todo list gets parsed in
* complete_action(). */
if (!initial)
return todo_list_parse_insn_buffer(r, new_todo->buf.buf, new_todo);
return 0;
}
define_commit_slab(commit_seen, unsigned char);
/*
* 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 todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo)
{
enum missing_commit_check_level check_level = get_missing_commit_check_level();
struct strbuf missing = STRBUF_INIT;
int res = 0, i;
struct commit_seen commit_seen;
init_commit_seen(&commit_seen);
if (check_level == MISSING_COMMIT_CHECK_IGNORE)
goto leave_check;
/* Mark the commits in git-rebase-todo as seen */
for (i = 0; i < new_todo->nr; i++) {
struct commit *commit = new_todo->items[i].commit;
if (commit)
*commit_seen_at(&commit_seen, commit) = 1;
}
/* Find commits in git-rebase-todo.backup yet unseen */
for (i = old_todo->nr - 1; i >= 0; i--) {
struct todo_item *item = old_todo->items + i;
struct commit *commit = item->commit;
if (commit && !*commit_seen_at(&commit_seen, commit)) {
strbuf_addf(&missing, " - %s %.*s\n",
find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV),
item->arg_len,
todo_item_get_arg(old_todo, item));
*commit_seen_at(&commit_seen, commit) = 1;
}
}
/* Warn about missing commits */
if (!missing.len)
goto leave_check;
if (check_level == MISSING_COMMIT_CHECK_ERROR)
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:
clear_commit_seen(&commit_seen);
return res;
}

View file

@ -3,9 +3,14 @@
struct strbuf;
struct repository;
struct todo_list;
void append_todo_help(unsigned edit_todo, unsigned keep_empty,
void append_todo_help(unsigned keep_empty, int command_count,
const char *shortrevisions, const char *shortonto,
struct strbuf *buf);
int edit_todo_list(struct repository *r, unsigned flags);
int edit_todo_list(struct repository *r, struct todo_list *todo_list,
struct todo_list *new_todo, const char *shortrevisions,
const char *shortonto, unsigned flags);
int todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo);
#endif

File diff suppressed because it is too large Load diff

View file

@ -10,6 +10,7 @@ struct repository;
const char *git_path_commit_editmsg(void);
const char *git_path_seq_dir(void);
const char *rebase_path_todo(void);
const char *rebase_path_todo_backup(void);
#define APPEND_SIGNOFF_DEDUP (1u << 0)
@ -66,14 +67,60 @@ struct replay_opts {
};
#define REPLAY_OPTS_INIT { .action = -1, .current_fixups = STRBUF_INIT }
enum missing_commit_check_level {
MISSING_COMMIT_CHECK_IGNORE = 0,
MISSING_COMMIT_CHECK_WARN,
MISSING_COMMIT_CHECK_ERROR
/*
* Note that ordering matters in this enum. Not only must it match the mapping
* of todo_command_info (in sequencer.c), it is also divided into several
* sections that matter. When adding new commands, make sure you add it in the
* right section.
*/
enum todo_command {
/* commands that handle commits */
TODO_PICK = 0,
TODO_REVERT,
TODO_EDIT,
TODO_REWORD,
TODO_FIXUP,
TODO_SQUASH,
/* commands that do something else than handling a single commit */
TODO_EXEC,
TODO_BREAK,
TODO_LABEL,
TODO_RESET,
TODO_MERGE,
/* commands that do nothing but are counted for reporting progress */
TODO_NOOP,
TODO_DROP,
/* comments (not counted for reporting progress) */
TODO_COMMENT
};
int write_message(const void *buf, size_t len, const char *filename,
int append_eol);
struct todo_item {
enum todo_command command;
struct commit *commit;
unsigned int flags;
int arg_len;
/* The offset of the command and its argument in the strbuf */
size_t offset_in_buf, arg_offset;
};
struct todo_list {
struct strbuf buf;
struct todo_item *items;
int nr, alloc, current;
int done_nr, total_nr;
struct stat_data stat;
};
#define TODO_LIST_INIT { STRBUF_INIT }
int todo_list_parse_insn_buffer(struct repository *r, char *buf,
struct todo_list *todo_list);
int todo_list_write_to_file(struct repository *r, struct todo_list *todo_list,
const char *file, const char *shortrevisions,
const char *shortonto, int num, unsigned flags);
void todo_list_release(struct todo_list *todo_list);
const char *todo_item_get_arg(struct todo_list *todo_list,
struct todo_item *item);
/* Call this to setup defaults before parsing command line options */
void sequencer_init_config(struct replay_opts *opts);
@ -93,19 +140,19 @@ int sequencer_remove_state(struct replay_opts *opts);
* commits should be rebased onto the new base, this flag needs to be passed.
*/
#define TODO_LIST_REBASE_COUSINS (1U << 4)
int sequencer_make_script(struct repository *repo, FILE *out,
int argc, const char **argv,
unsigned flags);
#define TODO_LIST_APPEND_TODO_HELP (1U << 5)
int sequencer_add_exec_commands(struct repository *r, const char *command);
int transform_todos(struct repository *r, unsigned flags);
enum missing_commit_check_level get_missing_commit_check_level(void);
int check_todo_list(struct repository *r);
int sequencer_make_script(struct repository *r, struct strbuf *out, int argc,
const char **argv, unsigned flags);
void todo_list_add_exec_commands(struct todo_list *todo_list,
struct string_list *commands);
int check_todo_list_from_file(struct repository *r);
int complete_action(struct repository *r, struct replay_opts *opts, unsigned flags,
const char *shortrevisions, const char *onto_name,
const char *onto, const char *orig_head, const char *cmd,
unsigned autosquash);
int rearrange_squash(struct repository *r);
const char *onto, const char *orig_head, struct string_list *commands,
unsigned autosquash, struct todo_list *todo_list);
int todo_list_rearrange_squash(struct todo_list *todo_list);
/*
* Append a signoff to the commit message in "msgbuf". The ignore_footer