test-mergesort: add test subcommand
[git/debian.git] / rebase-interactive.c
blob762853bc7e074cba1ae904790cbc1eb07520f613
1 #include "cache.h"
2 #include "commit.h"
3 #include "sequencer.h"
4 #include "rebase-interactive.h"
5 #include "strbuf.h"
6 #include "commit-slab.h"
7 #include "config.h"
8 #include "dir.h"
10 static const char edit_todo_list_advice[] =
11 N_("You can fix this with 'git rebase --edit-todo' "
12 "and then run 'git rebase --continue'.\n"
13 "Or you can abort the rebase with 'git rebase"
14 " --abort'.\n");
16 enum missing_commit_check_level {
17 MISSING_COMMIT_CHECK_IGNORE = 0,
18 MISSING_COMMIT_CHECK_WARN,
19 MISSING_COMMIT_CHECK_ERROR
22 static enum missing_commit_check_level get_missing_commit_check_level(void)
24 const char *value;
26 if (git_config_get_value("rebase.missingcommitscheck", &value) ||
27 !strcasecmp("ignore", value))
28 return MISSING_COMMIT_CHECK_IGNORE;
29 if (!strcasecmp("warn", value))
30 return MISSING_COMMIT_CHECK_WARN;
31 if (!strcasecmp("error", value))
32 return MISSING_COMMIT_CHECK_ERROR;
33 warning(_("unrecognized setting %s for option "
34 "rebase.missingCommitsCheck. Ignoring."), value);
35 return MISSING_COMMIT_CHECK_IGNORE;
38 void append_todo_help(int command_count,
39 const char *shortrevisions, const char *shortonto,
40 struct strbuf *buf)
42 const char *msg = _("\nCommands:\n"
43 "p, pick <commit> = use commit\n"
44 "r, reword <commit> = use commit, but edit the commit message\n"
45 "e, edit <commit> = use commit, but stop for amending\n"
46 "s, squash <commit> = use commit, but meld into previous commit\n"
47 "f, fixup <commit> = like \"squash\", but discard this commit's log message\n"
48 "x, exec <command> = run command (the rest of the line) using shell\n"
49 "b, break = stop here (continue rebase later with 'git rebase --continue')\n"
50 "d, drop <commit> = remove commit\n"
51 "l, label <label> = label current HEAD with a name\n"
52 "t, reset <label> = reset HEAD to a label\n"
53 "m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]\n"
54 ". create a merge commit using the original merge commit's\n"
55 ". message (or the oneline, if no original merge commit was\n"
56 ". specified). Use -c <commit> to reword the commit message.\n"
57 "\n"
58 "These lines can be re-ordered; they are executed from top to bottom.\n");
59 unsigned edit_todo = !(shortrevisions && shortonto);
61 if (!edit_todo) {
62 strbuf_addch(buf, '\n');
63 strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)",
64 "Rebase %s onto %s (%d commands)",
65 command_count),
66 shortrevisions, shortonto, command_count);
69 strbuf_add_commented_lines(buf, msg, strlen(msg));
71 if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
72 msg = _("\nDo not remove any line. Use 'drop' "
73 "explicitly to remove a commit.\n");
74 else
75 msg = _("\nIf you remove a line here "
76 "THAT COMMIT WILL BE LOST.\n");
78 strbuf_add_commented_lines(buf, msg, strlen(msg));
80 if (edit_todo)
81 msg = _("\nYou are editing the todo file "
82 "of an ongoing interactive rebase.\n"
83 "To continue rebase after editing, run:\n"
84 " git rebase --continue\n\n");
85 else
86 msg = _("\nHowever, if you remove everything, "
87 "the rebase will be aborted.\n\n");
89 strbuf_add_commented_lines(buf, msg, strlen(msg));
92 int edit_todo_list(struct repository *r, struct todo_list *todo_list,
93 struct todo_list *new_todo, const char *shortrevisions,
94 const char *shortonto, unsigned flags)
96 const char *todo_file = rebase_path_todo(),
97 *todo_backup = rebase_path_todo_backup();
98 unsigned initial = shortrevisions && shortonto;
99 int incorrect = 0;
101 /* If the user is editing the todo list, we first try to parse
102 * it. If there is an error, we do not return, because the user
103 * might want to fix it in the first place. */
104 if (!initial)
105 incorrect = todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list) |
106 file_exists(rebase_path_dropped());
108 if (todo_list_write_to_file(r, todo_list, todo_file, shortrevisions, shortonto,
109 -1, flags | TODO_LIST_SHORTEN_IDS | TODO_LIST_APPEND_TODO_HELP))
110 return error_errno(_("could not write '%s'"), todo_file);
112 if (!incorrect &&
113 todo_list_write_to_file(r, todo_list, todo_backup,
114 shortrevisions, shortonto, -1,
115 (flags | TODO_LIST_APPEND_TODO_HELP) & ~TODO_LIST_SHORTEN_IDS) < 0)
116 return error(_("could not write '%s'."), rebase_path_todo_backup());
118 if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
119 return -2;
121 strbuf_stripspace(&new_todo->buf, 1);
122 if (initial && new_todo->buf.len == 0)
123 return -3;
125 if (todo_list_parse_insn_buffer(r, new_todo->buf.buf, new_todo)) {
126 fprintf(stderr, _(edit_todo_list_advice));
127 return -4;
130 if (incorrect) {
131 if (todo_list_check_against_backup(r, new_todo)) {
132 write_file(rebase_path_dropped(), "%s", "");
133 return -4;
136 if (incorrect > 0)
137 unlink(rebase_path_dropped());
138 } else if (todo_list_check(todo_list, new_todo)) {
139 write_file(rebase_path_dropped(), "%s", "");
140 return -4;
143 return 0;
146 define_commit_slab(commit_seen, unsigned char);
148 * Check if the user dropped some commits by mistake
149 * Behaviour determined by rebase.missingCommitsCheck.
150 * Check if there is an unrecognized command or a
151 * bad SHA-1 in a command.
153 int todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo)
155 enum missing_commit_check_level check_level = get_missing_commit_check_level();
156 struct strbuf missing = STRBUF_INIT;
157 int res = 0, i;
158 struct commit_seen commit_seen;
160 init_commit_seen(&commit_seen);
162 if (check_level == MISSING_COMMIT_CHECK_IGNORE)
163 goto leave_check;
165 /* Mark the commits in git-rebase-todo as seen */
166 for (i = 0; i < new_todo->nr; i++) {
167 struct commit *commit = new_todo->items[i].commit;
168 if (commit)
169 *commit_seen_at(&commit_seen, commit) = 1;
172 /* Find commits in git-rebase-todo.backup yet unseen */
173 for (i = old_todo->nr - 1; i >= 0; i--) {
174 struct todo_item *item = old_todo->items + i;
175 struct commit *commit = item->commit;
176 if (commit && !*commit_seen_at(&commit_seen, commit)) {
177 strbuf_addf(&missing, " - %s %.*s\n",
178 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV),
179 item->arg_len,
180 todo_item_get_arg(old_todo, item));
181 *commit_seen_at(&commit_seen, commit) = 1;
185 /* Warn about missing commits */
186 if (!missing.len)
187 goto leave_check;
189 if (check_level == MISSING_COMMIT_CHECK_ERROR)
190 res = 1;
192 fprintf(stderr,
193 _("Warning: some commits may have been dropped accidentally.\n"
194 "Dropped commits (newer to older):\n"));
196 /* Make the list user-friendly and display */
197 fputs(missing.buf, stderr);
198 strbuf_release(&missing);
200 fprintf(stderr, _("To avoid this message, use \"drop\" to "
201 "explicitly remove a commit.\n\n"
202 "Use 'git config rebase.missingCommitsCheck' to change "
203 "the level of warnings.\n"
204 "The possible behaviours are: ignore, warn, error.\n\n"));
206 fprintf(stderr, _(edit_todo_list_advice));
208 leave_check:
209 clear_commit_seen(&commit_seen);
210 return res;
213 int todo_list_check_against_backup(struct repository *r, struct todo_list *todo_list)
215 struct todo_list backup = TODO_LIST_INIT;
216 int res = 0;
218 if (strbuf_read_file(&backup.buf, rebase_path_todo_backup(), 0) > 0) {
219 todo_list_parse_insn_buffer(r, backup.buf.buf, &backup);
220 res = todo_list_check(&backup, todo_list);
223 todo_list_release(&backup);
224 return res;
227 int check_todo_list_from_file(struct repository *r)
229 struct todo_list old_todo = TODO_LIST_INIT, new_todo = TODO_LIST_INIT;
230 int res = 0;
232 if (strbuf_read_file(&new_todo.buf, rebase_path_todo(), 0) < 0) {
233 res = error(_("could not read '%s'."), rebase_path_todo());
234 goto out;
237 if (strbuf_read_file(&old_todo.buf, rebase_path_todo_backup(), 0) < 0) {
238 res = error(_("could not read '%s'."), rebase_path_todo_backup());
239 goto out;
242 res = todo_list_parse_insn_buffer(r, old_todo.buf.buf, &old_todo);
243 if (!res)
244 res = todo_list_parse_insn_buffer(r, new_todo.buf.buf, &new_todo);
245 if (res)
246 fprintf(stderr, _(edit_todo_list_advice));
247 if (!res)
248 res = todo_list_check(&old_todo, &new_todo);
249 out:
250 todo_list_release(&old_todo);
251 todo_list_release(&new_todo);
253 return res;