rebase--interactive: move rearrange_squash_in_todo_file()
[gitweb.git] / rebase-interactive.c
index 4a9a10eff4a5a256540f45a123e170e178bc9ac4..dfa6dd530f3dd246de866ca5db5174a549cf31f5 100644 (file)
@@ -1,8 +1,32 @@
 #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"
+
+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 edit_todo, unsigned keep_empty,
                      struct strbuf *buf)
@@ -14,6 +38,7 @@ void append_todo_help(unsigned edit_todo, unsigned keep_empty,
 "s, squash <commit> = use commit, but meld into previous commit\n"
 "f, fixup <commit> = like \"squash\", but discard this commit's log message\n"
 "x, exec <command> = run command (the rest of the line) using shell\n"
+"b, break = stop here (continue rebase later with 'git rebase --continue')\n"
 "d, drop <commit> = remove commit\n"
 "l, label <label> = label current HEAD with a name\n"
 "t, reset <label> = reset HEAD to a label\n"
@@ -52,29 +77,7 @@ void append_todo_help(unsigned edit_todo, unsigned keep_empty,
        }
 }
 
-int append_todo_help_to_file(unsigned edit_todo, unsigned keep_empty)
-{
-       struct strbuf buf = STRBUF_INIT;
-       FILE *todo;
-       int ret;
-
-       todo = fopen_or_warn(rebase_path_todo(), "a");
-       if (!todo)
-               return -1;
-
-       append_todo_help(edit_todo, keep_empty, &buf);
-
-       ret = fputs(buf.buf, todo);
-       if (ret < 0)
-               error_errno(_("could not append help text to '%s'"), rebase_path_todo());
-
-       fclose(todo);
-       strbuf_release(&buf);
-
-       return ret;
-}
-
-int edit_todo_list(unsigned flags)
+int edit_todo_list(struct repository *r, unsigned flags)
 {
        struct strbuf buf = STRBUF_INIT;
        const char *todo_file = rebase_path_todo();
@@ -90,7 +93,7 @@ int edit_todo_list(unsigned flags)
 
        strbuf_release(&buf);
 
-       transform_todos(flags | TODO_LIST_SHORTEN_IDS);
+       transform_todo_file(r, flags | TODO_LIST_SHORTEN_IDS);
 
        if (strbuf_read_file(&buf, todo_file, 0) < 0)
                return error_errno(_("could not read '%s'."), todo_file);
@@ -106,7 +109,72 @@ int edit_todo_list(unsigned flags)
        if (launch_sequence_editor(todo_file, NULL, NULL))
                return -1;
 
-       transform_todos(flags & ~(TODO_LIST_SHORTEN_IDS));
+       transform_todo_file(r, flags & ~(TODO_LIST_SHORTEN_IDS));
 
        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;
+}