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