rebase-interactive.con commit rebase -i: rewrite the edit-todo functionality in C (64a43cb)
   1#include "cache.h"
   2#include "commit.h"
   3#include "rebase-interactive.h"
   4#include "sequencer.h"
   5#include "strbuf.h"
   6
   7int append_todo_help(unsigned edit_todo, unsigned keep_empty)
   8{
   9        struct strbuf buf = STRBUF_INIT;
  10        FILE *todo;
  11        int ret;
  12        const char *msg = _("\nCommands:\n"
  13"p, pick <commit> = use commit\n"
  14"r, reword <commit> = use commit, but edit the commit message\n"
  15"e, edit <commit> = use commit, but stop for amending\n"
  16"s, squash <commit> = use commit, but meld into previous commit\n"
  17"f, fixup <commit> = like \"squash\", but discard this commit's log message\n"
  18"x, exec <command> = run command (the rest of the line) using shell\n"
  19"d, drop <commit> = remove commit\n"
  20"l, label <label> = label current HEAD with a name\n"
  21"t, reset <label> = reset HEAD to a label\n"
  22"m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]\n"
  23".       create a merge commit using the original merge commit's\n"
  24".       message (or the oneline, if no original merge commit was\n"
  25".       specified). Use -c <commit> to reword the commit message.\n"
  26"\n"
  27"These lines can be re-ordered; they are executed from top to bottom.\n");
  28
  29        todo = fopen_or_warn(rebase_path_todo(), "a");
  30        if (!todo)
  31                return 1;
  32
  33        strbuf_add_commented_lines(&buf, msg, strlen(msg));
  34
  35        if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
  36                msg = _("\nDo not remove any line. Use 'drop' "
  37                         "explicitly to remove a commit.\n");
  38        else
  39                msg = _("\nIf you remove a line here "
  40                         "THAT COMMIT WILL BE LOST.\n");
  41
  42        strbuf_add_commented_lines(&buf, msg, strlen(msg));
  43
  44        if (edit_todo)
  45                msg = _("\nYou are editing the todo file "
  46                        "of an ongoing interactive rebase.\n"
  47                        "To continue rebase after editing, run:\n"
  48                        "    git rebase --continue\n\n");
  49        else
  50                msg = _("\nHowever, if you remove everything, "
  51                        "the rebase will be aborted.\n\n");
  52
  53        strbuf_add_commented_lines(&buf, msg, strlen(msg));
  54
  55        if (!keep_empty) {
  56                msg = _("Note that empty commits are commented out");
  57                strbuf_add_commented_lines(&buf, msg, strlen(msg));
  58        }
  59
  60        ret = fputs(buf.buf, todo);
  61        if (ret < 0)
  62                error_errno(_("could not append help text to '%s'"), rebase_path_todo());
  63
  64        fclose(todo);
  65        strbuf_release(&buf);
  66
  67        return ret;
  68}
  69
  70int edit_todo_list(unsigned flags)
  71{
  72        struct strbuf buf = STRBUF_INIT;
  73        const char *todo_file = rebase_path_todo();
  74
  75        if (strbuf_read_file(&buf, todo_file, 0) < 0)
  76                return error_errno(_("could not read '%s'."), todo_file);
  77
  78        strbuf_stripspace(&buf, 1);
  79        if (write_message(buf.buf, buf.len, todo_file, 0)) {
  80                strbuf_release(&buf);
  81                return -1;
  82        }
  83
  84        strbuf_release(&buf);
  85
  86        transform_todos(flags | TODO_LIST_SHORTEN_IDS);
  87        append_todo_help(1, 0);
  88
  89        if (launch_sequence_editor(todo_file, NULL, NULL))
  90                return -1;
  91
  92        transform_todos(flags & ~(TODO_LIST_SHORTEN_IDS));
  93
  94        return 0;
  95}