hex: add functions to parse hex object IDs in any algorithm
[git.git] / rebase-interactive.c
blobac001dea5887a119f839ab4577797f9a9a179174
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(unsigned keep_empty, 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));
91 if (!keep_empty) {
92 msg = _("Note that empty commits are commented out");
93 strbuf_add_commented_lines(buf, msg, strlen(msg));
97 int edit_todo_list(struct repository *r, struct todo_list *todo_list,
98 struct todo_list *new_todo, const char *shortrevisions,
99 const char *shortonto, unsigned flags)
101 const char *todo_file = rebase_path_todo(),
102 *todo_backup = rebase_path_todo_backup();
103 unsigned initial = shortrevisions && shortonto;
104 int incorrect = 0;
106 /* If the user is editing the todo list, we first try to parse
107 * it. If there is an error, we do not return, because the user
108 * might want to fix it in the first place. */
109 if (!initial)
110 incorrect = todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list) |
111 file_exists(rebase_path_dropped());
113 if (todo_list_write_to_file(r, todo_list, todo_file, shortrevisions, shortonto,
114 -1, flags | TODO_LIST_SHORTEN_IDS | TODO_LIST_APPEND_TODO_HELP))
115 return error_errno(_("could not write '%s'"), todo_file);
117 if (!incorrect &&
118 todo_list_write_to_file(r, todo_list, todo_backup,
119 shortrevisions, shortonto, -1,
120 (flags | TODO_LIST_APPEND_TODO_HELP) & ~TODO_LIST_SHORTEN_IDS) < 0)
121 return error(_("could not write '%s'."), rebase_path_todo_backup());
123 if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
124 return -2;
126 strbuf_stripspace(&new_todo->buf, 1);
127 if (initial && new_todo->buf.len == 0)
128 return -3;
130 if (todo_list_parse_insn_buffer(r, new_todo->buf.buf, new_todo)) {
131 fprintf(stderr, _(edit_todo_list_advice));
132 return -4;
135 if (incorrect) {
136 if (todo_list_check_against_backup(r, new_todo)) {
137 write_file(rebase_path_dropped(), "");
138 return -4;
141 if (incorrect > 0)
142 unlink(rebase_path_dropped());
143 } else if (todo_list_check(todo_list, new_todo)) {
144 write_file(rebase_path_dropped(), "");
145 return -4;
148 return 0;
151 define_commit_slab(commit_seen, unsigned char);
153 * Check if the user dropped some commits by mistake
154 * Behaviour determined by rebase.missingCommitsCheck.
155 * Check if there is an unrecognized command or a
156 * bad SHA-1 in a command.
158 int todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo)
160 enum missing_commit_check_level check_level = get_missing_commit_check_level();
161 struct strbuf missing = STRBUF_INIT;
162 int res = 0, i;
163 struct commit_seen commit_seen;
165 init_commit_seen(&commit_seen);
167 if (check_level == MISSING_COMMIT_CHECK_IGNORE)
168 goto leave_check;
170 /* Mark the commits in git-rebase-todo as seen */
171 for (i = 0; i < new_todo->nr; i++) {
172 struct commit *commit = new_todo->items[i].commit;
173 if (commit)
174 *commit_seen_at(&commit_seen, commit) = 1;
177 /* Find commits in git-rebase-todo.backup yet unseen */
178 for (i = old_todo->nr - 1; i >= 0; i--) {
179 struct todo_item *item = old_todo->items + i;
180 struct commit *commit = item->commit;
181 if (commit && !*commit_seen_at(&commit_seen, commit)) {
182 strbuf_addf(&missing, " - %s %.*s\n",
183 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV),
184 item->arg_len,
185 todo_item_get_arg(old_todo, item));
186 *commit_seen_at(&commit_seen, commit) = 1;
190 /* Warn about missing commits */
191 if (!missing.len)
192 goto leave_check;
194 if (check_level == MISSING_COMMIT_CHECK_ERROR)
195 res = 1;
197 fprintf(stderr,
198 _("Warning: some commits may have been dropped accidentally.\n"
199 "Dropped commits (newer to older):\n"));
201 /* Make the list user-friendly and display */
202 fputs(missing.buf, stderr);
203 strbuf_release(&missing);
205 fprintf(stderr, _("To avoid this message, use \"drop\" to "
206 "explicitly remove a commit.\n\n"
207 "Use 'git config rebase.missingCommitsCheck' to change "
208 "the level of warnings.\n"
209 "The possible behaviours are: ignore, warn, error.\n\n"));
211 fprintf(stderr, _(edit_todo_list_advice));
213 leave_check:
214 clear_commit_seen(&commit_seen);
215 return res;
218 int todo_list_check_against_backup(struct repository *r, struct todo_list *todo_list)
220 struct todo_list backup = TODO_LIST_INIT;
221 int res = 0;
223 if (strbuf_read_file(&backup.buf, rebase_path_todo_backup(), 0) > 0) {
224 todo_list_parse_insn_buffer(r, backup.buf.buf, &backup);
225 res = todo_list_check(&backup, todo_list);
228 todo_list_release(&backup);
229 return res;
232 int check_todo_list_from_file(struct repository *r)
234 struct todo_list old_todo = TODO_LIST_INIT, new_todo = TODO_LIST_INIT;
235 int res = 0;
237 if (strbuf_read_file(&new_todo.buf, rebase_path_todo(), 0) < 0) {
238 res = error(_("could not read '%s'."), rebase_path_todo());
239 goto out;
242 if (strbuf_read_file(&old_todo.buf, rebase_path_todo_backup(), 0) < 0) {
243 res = error(_("could not read '%s'."), rebase_path_todo_backup());
244 goto out;
247 res = todo_list_parse_insn_buffer(r, old_todo.buf.buf, &old_todo);
248 if (!res)
249 res = todo_list_parse_insn_buffer(r, new_todo.buf.buf, &new_todo);
250 if (res)
251 fprintf(stderr, _(edit_todo_list_advice));
252 if (!res)
253 res = todo_list_check(&old_todo, &new_todo);
254 out:
255 todo_list_release(&old_todo);
256 todo_list_release(&new_todo);
258 return res;