Merge branch 'jt/midx-doc-fix'
[git/debian.git] / rebase-interactive.c
blob87649d0c016a371f83710a04140909d54a553aa7
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 [-C | -c] <commit> = like \"squash\" but keep only the previous\n"
48 " commit's log message, unless -C is used, in which case\n"
49 " keep only this commit's message; -c is same as -C but\n"
50 " opens the editor\n"
51 "x, exec <command> = run command (the rest of the line) using shell\n"
52 "b, break = stop here (continue rebase later with 'git rebase --continue')\n"
53 "d, drop <commit> = remove commit\n"
54 "l, label <label> = label current HEAD with a name\n"
55 "t, reset <label> = reset HEAD to a label\n"
56 "m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]\n"
57 ". create a merge commit using the original merge commit's\n"
58 ". message (or the oneline, if no original merge commit was\n"
59 ". specified); use -c <commit> to reword the commit message\n"
60 "\n"
61 "These lines can be re-ordered; they are executed from top to bottom.\n");
62 unsigned edit_todo = !(shortrevisions && shortonto);
64 if (!edit_todo) {
65 strbuf_addch(buf, '\n');
66 strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)",
67 "Rebase %s onto %s (%d commands)",
68 command_count),
69 shortrevisions, shortonto, command_count);
72 strbuf_add_commented_lines(buf, msg, strlen(msg));
74 if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
75 msg = _("\nDo not remove any line. Use 'drop' "
76 "explicitly to remove a commit.\n");
77 else
78 msg = _("\nIf you remove a line here "
79 "THAT COMMIT WILL BE LOST.\n");
81 strbuf_add_commented_lines(buf, msg, strlen(msg));
83 if (edit_todo)
84 msg = _("\nYou are editing the todo file "
85 "of an ongoing interactive rebase.\n"
86 "To continue rebase after editing, run:\n"
87 " git rebase --continue\n\n");
88 else
89 msg = _("\nHowever, if you remove everything, "
90 "the rebase will be aborted.\n\n");
92 strbuf_add_commented_lines(buf, msg, strlen(msg));
95 int edit_todo_list(struct repository *r, struct todo_list *todo_list,
96 struct todo_list *new_todo, const char *shortrevisions,
97 const char *shortonto, unsigned flags)
99 const char *todo_file = rebase_path_todo(),
100 *todo_backup = rebase_path_todo_backup();
101 unsigned initial = shortrevisions && shortonto;
102 int incorrect = 0;
104 /* If the user is editing the todo list, we first try to parse
105 * it. If there is an error, we do not return, because the user
106 * might want to fix it in the first place. */
107 if (!initial)
108 incorrect = todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list) |
109 file_exists(rebase_path_dropped());
111 if (todo_list_write_to_file(r, todo_list, todo_file, shortrevisions, shortonto,
112 -1, flags | TODO_LIST_SHORTEN_IDS | TODO_LIST_APPEND_TODO_HELP))
113 return error_errno(_("could not write '%s'"), todo_file);
115 if (!incorrect &&
116 todo_list_write_to_file(r, todo_list, todo_backup,
117 shortrevisions, shortonto, -1,
118 (flags | TODO_LIST_APPEND_TODO_HELP) & ~TODO_LIST_SHORTEN_IDS) < 0)
119 return error(_("could not write '%s'."), rebase_path_todo_backup());
121 if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
122 return -2;
124 strbuf_stripspace(&new_todo->buf, 1);
125 if (initial && new_todo->buf.len == 0)
126 return -3;
128 if (todo_list_parse_insn_buffer(r, new_todo->buf.buf, new_todo)) {
129 fprintf(stderr, _(edit_todo_list_advice));
130 return -4;
133 if (incorrect) {
134 if (todo_list_check_against_backup(r, new_todo)) {
135 write_file(rebase_path_dropped(), "%s", "");
136 return -4;
139 if (incorrect > 0)
140 unlink(rebase_path_dropped());
141 } else if (todo_list_check(todo_list, new_todo)) {
142 write_file(rebase_path_dropped(), "%s", "");
143 return -4;
146 return 0;
149 define_commit_slab(commit_seen, unsigned char);
151 * Check if the user dropped some commits by mistake
152 * Behaviour determined by rebase.missingCommitsCheck.
153 * Check if there is an unrecognized command or a
154 * bad SHA-1 in a command.
156 int todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo)
158 enum missing_commit_check_level check_level = get_missing_commit_check_level();
159 struct strbuf missing = STRBUF_INIT;
160 int res = 0, i;
161 struct commit_seen commit_seen;
163 init_commit_seen(&commit_seen);
165 if (check_level == MISSING_COMMIT_CHECK_IGNORE)
166 goto leave_check;
168 /* Mark the commits in git-rebase-todo as seen */
169 for (i = 0; i < new_todo->nr; i++) {
170 struct commit *commit = new_todo->items[i].commit;
171 if (commit)
172 *commit_seen_at(&commit_seen, commit) = 1;
175 /* Find commits in git-rebase-todo.backup yet unseen */
176 for (i = old_todo->nr - 1; i >= 0; i--) {
177 struct todo_item *item = old_todo->items + i;
178 struct commit *commit = item->commit;
179 if (commit && !*commit_seen_at(&commit_seen, commit)) {
180 strbuf_addf(&missing, " - %s %.*s\n",
181 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV),
182 item->arg_len,
183 todo_item_get_arg(old_todo, item));
184 *commit_seen_at(&commit_seen, commit) = 1;
188 /* Warn about missing commits */
189 if (!missing.len)
190 goto leave_check;
192 if (check_level == MISSING_COMMIT_CHECK_ERROR)
193 res = 1;
195 fprintf(stderr,
196 _("Warning: some commits may have been dropped accidentally.\n"
197 "Dropped commits (newer to older):\n"));
199 /* Make the list user-friendly and display */
200 fputs(missing.buf, stderr);
201 strbuf_release(&missing);
203 fprintf(stderr, _("To avoid this message, use \"drop\" to "
204 "explicitly remove a commit.\n\n"
205 "Use 'git config rebase.missingCommitsCheck' to change "
206 "the level of warnings.\n"
207 "The possible behaviours are: ignore, warn, error.\n\n"));
209 fprintf(stderr, _(edit_todo_list_advice));
211 leave_check:
212 clear_commit_seen(&commit_seen);
213 return res;
216 int todo_list_check_against_backup(struct repository *r, struct todo_list *todo_list)
218 struct todo_list backup = TODO_LIST_INIT;
219 int res = 0;
221 if (strbuf_read_file(&backup.buf, rebase_path_todo_backup(), 0) > 0) {
222 todo_list_parse_insn_buffer(r, backup.buf.buf, &backup);
223 res = todo_list_check(&backup, todo_list);
226 todo_list_release(&backup);
227 return res;