]> git.ipfire.org Git - thirdparty/git.git/blame - rebase-interactive.c
sequencer: move check_todo_list_from_file() to rebase-interactive.c
[thirdparty/git.git] / rebase-interactive.c
CommitLineData
145e05ac
AG
1#include "cache.h"
2#include "commit.h"
145e05ac 3#include "sequencer.h"
6ca89c6f 4#include "rebase-interactive.h"
145e05ac 5#include "strbuf.h"
6ca89c6f
AG
6#include "commit-slab.h"
7#include "config.h"
8
1da5874c
AG
9static const char edit_todo_list_advice[] =
10N_("You can fix this with 'git rebase --edit-todo' "
11"and then run 'git rebase --continue'.\n"
12"Or you can abort the rebase with 'git rebase"
13" --abort'.\n");
14
6ca89c6f
AG
15enum missing_commit_check_level {
16 MISSING_COMMIT_CHECK_IGNORE = 0,
17 MISSING_COMMIT_CHECK_WARN,
18 MISSING_COMMIT_CHECK_ERROR
19};
20
21static enum missing_commit_check_level get_missing_commit_check_level(void)
22{
23 const char *value;
24
25 if (git_config_get_value("rebase.missingcommitscheck", &value) ||
26 !strcasecmp("ignore", value))
27 return MISSING_COMMIT_CHECK_IGNORE;
28 if (!strcasecmp("warn", value))
29 return MISSING_COMMIT_CHECK_WARN;
30 if (!strcasecmp("error", value))
31 return MISSING_COMMIT_CHECK_ERROR;
32 warning(_("unrecognized setting %s for option "
33 "rebase.missingCommitsCheck. Ignoring."), value);
34 return MISSING_COMMIT_CHECK_IGNORE;
35}
145e05ac 36
af1fc3ad
AG
37void append_todo_help(unsigned keep_empty, int command_count,
38 const char *shortrevisions, const char *shortonto,
a9f5476f 39 struct strbuf *buf)
145e05ac 40{
145e05ac
AG
41 const char *msg = _("\nCommands:\n"
42"p, pick <commit> = use commit\n"
43"r, reword <commit> = use commit, but edit the commit message\n"
44"e, edit <commit> = use commit, but stop for amending\n"
45"s, squash <commit> = use commit, but meld into previous commit\n"
46"f, fixup <commit> = like \"squash\", but discard this commit's log message\n"
47"x, exec <command> = run command (the rest of the line) using shell\n"
71f82465 48"b, break = stop here (continue rebase later with 'git rebase --continue')\n"
145e05ac
AG
49"d, drop <commit> = remove commit\n"
50"l, label <label> = label current HEAD with a name\n"
51"t, reset <label> = reset HEAD to a label\n"
52"m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]\n"
53". create a merge commit using the original merge commit's\n"
54". message (or the oneline, if no original merge commit was\n"
55". specified). Use -c <commit> to reword the commit message.\n"
56"\n"
57"These lines can be re-ordered; they are executed from top to bottom.\n");
af1fc3ad
AG
58 unsigned edit_todo = !(shortrevisions && shortonto);
59
60 if (!edit_todo) {
61 strbuf_addch(buf, '\n');
62 strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)",
63 "Rebase %s onto %s (%d commands)",
64 command_count),
65 shortrevisions, shortonto, command_count);
66 }
145e05ac 67
a9f5476f 68 strbuf_add_commented_lines(buf, msg, strlen(msg));
145e05ac
AG
69
70 if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
71 msg = _("\nDo not remove any line. Use 'drop' "
72 "explicitly to remove a commit.\n");
73 else
74 msg = _("\nIf you remove a line here "
75 "THAT COMMIT WILL BE LOST.\n");
76
a9f5476f 77 strbuf_add_commented_lines(buf, msg, strlen(msg));
145e05ac
AG
78
79 if (edit_todo)
80 msg = _("\nYou are editing the todo file "
81 "of an ongoing interactive rebase.\n"
82 "To continue rebase after editing, run:\n"
83 " git rebase --continue\n\n");
84 else
85 msg = _("\nHowever, if you remove everything, "
86 "the rebase will be aborted.\n\n");
87
a9f5476f 88 strbuf_add_commented_lines(buf, msg, strlen(msg));
145e05ac
AG
89
90 if (!keep_empty) {
91 msg = _("Note that empty commits are commented out");
a9f5476f 92 strbuf_add_commented_lines(buf, msg, strlen(msg));
145e05ac 93 }
a9f5476f
AG
94}
95
a930eb03
AG
96int edit_todo_list(struct repository *r, struct todo_list *todo_list,
97 struct todo_list *new_todo, const char *shortrevisions,
98 const char *shortonto, unsigned flags)
64a43cbd 99{
64a43cbd 100 const char *todo_file = rebase_path_todo();
a930eb03 101 unsigned initial = shortrevisions && shortonto;
64a43cbd 102
a930eb03
AG
103 /* If the user is editing the todo list, we first try to parse
104 * it. If there is an error, we do not return, because the user
105 * might want to fix it in the first place. */
106 if (!initial)
107 todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list);
108
109 if (todo_list_write_to_file(r, todo_list, todo_file, shortrevisions, shortonto,
110 -1, flags | TODO_LIST_SHORTEN_IDS | TODO_LIST_APPEND_TODO_HELP))
111 return error_errno(_("could not write '%s'"), todo_file);
112
26027625
JS
113 if (initial &&
114 todo_list_write_to_file(r, todo_list, rebase_path_todo_backup(),
115 shortrevisions, shortonto, -1,
116 (flags | TODO_LIST_APPEND_TODO_HELP) & ~TODO_LIST_SHORTEN_IDS) < 0)
117 return error(_("could not write '%s'."), rebase_path_todo_backup());
a930eb03
AG
118
119 if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
120 return -2;
121
122 strbuf_stripspace(&new_todo->buf, 1);
123 if (initial && new_todo->buf.len == 0)
124 return -3;
a9f5476f 125
a930eb03
AG
126 /* For the initial edit, the todo list gets parsed in
127 * complete_action(). */
128 if (!initial)
129 return todo_list_parse_insn_buffer(r, new_todo->buf.buf, new_todo);
64a43cbd 130
a930eb03 131 return 0;
64a43cbd 132}
6ca89c6f
AG
133
134define_commit_slab(commit_seen, unsigned char);
135/*
136 * Check if the user dropped some commits by mistake
137 * Behaviour determined by rebase.missingCommitsCheck.
138 * Check if there is an unrecognized command or a
139 * bad SHA-1 in a command.
140 */
141int todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo)
142{
143 enum missing_commit_check_level check_level = get_missing_commit_check_level();
144 struct strbuf missing = STRBUF_INIT;
145 int res = 0, i;
146 struct commit_seen commit_seen;
147
148 init_commit_seen(&commit_seen);
149
150 if (check_level == MISSING_COMMIT_CHECK_IGNORE)
151 goto leave_check;
152
153 /* Mark the commits in git-rebase-todo as seen */
154 for (i = 0; i < new_todo->nr; i++) {
155 struct commit *commit = new_todo->items[i].commit;
156 if (commit)
157 *commit_seen_at(&commit_seen, commit) = 1;
158 }
159
160 /* Find commits in git-rebase-todo.backup yet unseen */
161 for (i = old_todo->nr - 1; i >= 0; i--) {
162 struct todo_item *item = old_todo->items + i;
163 struct commit *commit = item->commit;
164 if (commit && !*commit_seen_at(&commit_seen, commit)) {
165 strbuf_addf(&missing, " - %s %.*s\n",
166 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV),
167 item->arg_len,
168 todo_item_get_arg(old_todo, item));
169 *commit_seen_at(&commit_seen, commit) = 1;
170 }
171 }
172
173 /* Warn about missing commits */
174 if (!missing.len)
175 goto leave_check;
176
177 if (check_level == MISSING_COMMIT_CHECK_ERROR)
178 res = 1;
179
180 fprintf(stderr,
181 _("Warning: some commits may have been dropped accidentally.\n"
182 "Dropped commits (newer to older):\n"));
183
184 /* Make the list user-friendly and display */
185 fputs(missing.buf, stderr);
186 strbuf_release(&missing);
187
188 fprintf(stderr, _("To avoid this message, use \"drop\" to "
189 "explicitly remove a commit.\n\n"
190 "Use 'git config rebase.missingCommitsCheck' to change "
191 "the level of warnings.\n"
192 "The possible behaviours are: ignore, warn, error.\n\n"));
193
194leave_check:
195 clear_commit_seen(&commit_seen);
196 return res;
197}
1da5874c
AG
198
199int check_todo_list_from_file(struct repository *r)
200{
201 struct todo_list old_todo = TODO_LIST_INIT, new_todo = TODO_LIST_INIT;
202 int res = 0;
203
204 if (strbuf_read_file(&new_todo.buf, rebase_path_todo(), 0) < 0) {
205 res = error(_("could not read '%s'."), rebase_path_todo());
206 goto out;
207 }
208
209 if (strbuf_read_file(&old_todo.buf, rebase_path_todo_backup(), 0) < 0) {
210 res = error(_("could not read '%s'."), rebase_path_todo_backup());
211 goto out;
212 }
213
214 res = todo_list_parse_insn_buffer(r, old_todo.buf.buf, &old_todo);
215 if (!res)
216 res = todo_list_parse_insn_buffer(r, new_todo.buf.buf, &new_todo);
217 if (!res)
218 res = todo_list_check(&old_todo, &new_todo);
219 if (res)
220 fprintf(stderr, _(edit_todo_list_advice));
221out:
222 todo_list_release(&old_todo);
223 todo_list_release(&new_todo);
224
225 return res;
226}