summaryrefslogtreecommitdiff
path: root/sequencer.c
diff options
context:
space:
mode:
authorAlban Gruin <alban.gruin@gmail.com>2018-08-28 12:10:36 (GMT)
committerJunio C Hamano <gitster@pobox.com>2018-08-29 20:38:18 (GMT)
commitb97e187364990fb8410355ff8b4365d0e37bbbbe (patch)
tree73e0346e20435cb289e56cf2c59e269339c74b43 /sequencer.c
parentd078c39106895b26512ca6b259d66fe64b274e1b (diff)
downloadgit-b97e187364990fb8410355ff8b4365d0e37bbbbe.zip
git-b97e187364990fb8410355ff8b4365d0e37bbbbe.tar.gz
git-b97e187364990fb8410355ff8b4365d0e37bbbbe.tar.bz2
rebase -i: rewrite complete_action() in C
This rewrites complete_action() from shell to C. A new mode is added to rebase--helper (`--complete-action`), as well as a new flag (`--autosquash`). Finally, complete_action() is stripped from git-rebase--interactive.sh. The original complete_action() would return the code 2 when the todo list contained no actions. This was a special case for rebase -i and -p; git-rebase.sh would then apply the autostash, delete the state directory, and die with the message "Nothing to do". This cleanup is rewritten in C instead of returning 2. As rebase -i no longer returns 2, the comment describing this behaviour in git-rebase.sh is updated to reflect this change. The message "Nothing to do" is now printed with error(), and so becomes "error: nothing to do". Some tests in t3404 check this value, so they are updated to fit this change. The first check might seem useless as we write "noop" to the todo list if it is empty. Actually, the todo list might contain commented commands (ie. empty commits). In this case, complete_action() won’t write "noop", and will abort without starting the editor. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'sequencer.c')
-rw-r--r--sequencer.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/sequencer.c b/sequencer.c
index cd2374e..98aacc8 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -30,6 +30,7 @@
#include "oidset.h"
#include "commit-slab.h"
#include "alias.h"
+#include "rebase-interactive.h"
#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
@@ -53,6 +54,9 @@ static GIT_PATH_FUNC(rebase_path, "rebase-merge")
* file and written to the tail of 'done'.
*/
GIT_PATH_FUNC(rebase_path_todo, "rebase-merge/git-rebase-todo")
+static GIT_PATH_FUNC(rebase_path_todo_backup,
+ "rebase-merge/git-rebase-todo.backup")
+
/*
* The rebase command lines that have already been processed. A line
* is moved here when it is first handled, before any associated user
@@ -4495,6 +4499,106 @@ int skip_unnecessary_picks(struct object_id *output_oid)
return 0;
}
+int complete_action(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)
+{
+ const char *shortonto, *todo_file = rebase_path_todo();
+ struct todo_list todo_list = TODO_LIST_INIT;
+ struct strbuf *buf = &(todo_list.buf);
+ struct object_id oid;
+ struct stat st;
+
+ get_oid(onto, &oid);
+ shortonto = find_unique_abbrev(&oid, DEFAULT_ABBREV);
+
+ if (!lstat(todo_file, &st) && st.st_size == 0 &&
+ write_message("noop\n", 5, todo_file, 0))
+ return -1;
+
+ if (autosquash && rearrange_squash())
+ return -1;
+
+ if (cmd && *cmd)
+ sequencer_add_exec_commands(cmd);
+
+ if (strbuf_read_file(buf, todo_file, 0) < 0)
+ return error_errno(_("could not read '%s'."), todo_file);
+
+ if (parse_insn_buffer(buf->buf, &todo_list)) {
+ todo_list_release(&todo_list);
+ return error(_("unusable todo list: '%s'"), todo_file);
+ }
+
+ if (count_commands(&todo_list) == 0) {
+ apply_autostash(opts);
+ sequencer_remove_state(opts);
+ todo_list_release(&todo_list);
+
+ return error(_("nothing to do"));
+ }
+
+ strbuf_addch(buf, '\n');
+ strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)",
+ "Rebase %s onto %s (%d commands)",
+ count_commands(&todo_list)),
+ shortrevisions, shortonto, count_commands(&todo_list));
+ append_todo_help(0, flags & TODO_LIST_KEEP_EMPTY, buf);
+
+ if (write_message(buf->buf, buf->len, todo_file, 0)) {
+ todo_list_release(&todo_list);
+ return -1;
+ }
+
+ if (copy_file(rebase_path_todo_backup(), todo_file, 0666))
+ return error(_("could not copy '%s' to '%s'."), todo_file,
+ rebase_path_todo_backup());
+
+ if (transform_todos(flags | TODO_LIST_SHORTEN_IDS))
+ return error(_("could not transform the todo list"));
+
+ strbuf_reset(buf);
+
+ if (launch_sequence_editor(todo_file, buf, NULL)) {
+ apply_autostash(opts);
+ sequencer_remove_state(opts);
+ todo_list_release(&todo_list);
+
+ return -1;
+ }
+
+ strbuf_stripspace(buf, 1);
+ if (buf->len == 0) {
+ apply_autostash(opts);
+ sequencer_remove_state(opts);
+ todo_list_release(&todo_list);
+
+ return error(_("nothing to do"));
+ }
+
+ todo_list_release(&todo_list);
+
+ if (check_todo_list()) {
+ checkout_onto(opts, onto_name, onto, orig_head);
+ return -1;
+ }
+
+ if (transform_todos(flags & ~(TODO_LIST_SHORTEN_IDS)))
+ return error(_("could not transform the todo list"));
+
+ if (opts->allow_ff && skip_unnecessary_picks(&oid))
+ return error(_("could not skip unnecessary pick commands"));
+
+ if (checkout_onto(opts, onto_name, oid_to_hex(&oid), orig_head))
+ return -1;
+;
+ if (require_clean_work_tree("rebase", "", 1, 1))
+ return -1;
+
+ return sequencer_continue(opts);
+}
+
struct subject2item_entry {
struct hashmap_entry entry;
int i;