parse-options: decouple "--end-of-options" and "--"
[git/gitster.git] / rebase-interactive.c
blob7407c593191679c757573e567a28f35c65c9cb33
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 "u, update-ref <ref> = track a placeholder for the <ref> to be updated\n"
61 " to this position in the new commits. The <ref> is\n"
62 " updated at the end of the rebase\n"
63 "\n"
64 "These lines can be re-ordered; they are executed from top to bottom.\n");
65 unsigned edit_todo = !(shortrevisions && shortonto);
67 if (!edit_todo) {
68 strbuf_addch(buf, '\n');
69 strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)",
70 "Rebase %s onto %s (%d commands)",
71 command_count),
72 shortrevisions, shortonto, command_count);
75 strbuf_add_commented_lines(buf, msg, strlen(msg));
77 if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
78 msg = _("\nDo not remove any line. Use 'drop' "
79 "explicitly to remove a commit.\n");
80 else
81 msg = _("\nIf you remove a line here "
82 "THAT COMMIT WILL BE LOST.\n");
84 strbuf_add_commented_lines(buf, msg, strlen(msg));
86 if (edit_todo)
87 msg = _("\nYou are editing the todo file "
88 "of an ongoing interactive rebase.\n"
89 "To continue rebase after editing, run:\n"
90 " git rebase --continue\n\n");
91 else
92 msg = _("\nHowever, if you remove everything, "
93 "the rebase will be aborted.\n\n");
95 strbuf_add_commented_lines(buf, msg, strlen(msg));
98 int edit_todo_list(struct repository *r, struct todo_list *todo_list,
99 struct todo_list *new_todo, const char *shortrevisions,
100 const char *shortonto, unsigned flags)
102 const char *todo_file = rebase_path_todo(),
103 *todo_backup = rebase_path_todo_backup();
104 unsigned initial = shortrevisions && shortonto;
105 int incorrect = 0;
107 /* If the user is editing the todo list, we first try to parse
108 * it. If there is an error, we do not return, because the user
109 * might want to fix it in the first place. */
110 if (!initial)
111 incorrect = todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list) |
112 file_exists(rebase_path_dropped());
114 if (todo_list_write_to_file(r, todo_list, todo_file, shortrevisions, shortonto,
115 -1, flags | TODO_LIST_SHORTEN_IDS | TODO_LIST_APPEND_TODO_HELP))
116 return error_errno(_("could not write '%s'"), todo_file);
118 if (!incorrect &&
119 todo_list_write_to_file(r, todo_list, todo_backup,
120 shortrevisions, shortonto, -1,
121 (flags | TODO_LIST_APPEND_TODO_HELP) & ~TODO_LIST_SHORTEN_IDS) < 0)
122 return error(_("could not write '%s'."), rebase_path_todo_backup());
124 if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
125 return -2;
127 strbuf_stripspace(&new_todo->buf, 1);
128 if (initial && new_todo->buf.len == 0)
129 return -3;
131 if (todo_list_parse_insn_buffer(r, new_todo->buf.buf, new_todo)) {
132 fprintf(stderr, _(edit_todo_list_advice));
133 return -4;
136 if (incorrect) {
137 if (todo_list_check_against_backup(r, new_todo)) {
138 write_file(rebase_path_dropped(), "%s", "");
139 return -4;
142 if (incorrect > 0)
143 unlink(rebase_path_dropped());
144 } else if (todo_list_check(todo_list, new_todo)) {
145 write_file(rebase_path_dropped(), "%s", "");
146 return -4;
150 * See if branches need to be added or removed from the update-refs
151 * file based on the new todo list.
153 todo_list_filter_update_refs(r, new_todo);
155 return 0;
158 define_commit_slab(commit_seen, unsigned char);
160 * Check if the user dropped some commits by mistake
161 * Behaviour determined by rebase.missingCommitsCheck.
162 * Check if there is an unrecognized command or a
163 * bad SHA-1 in a command.
165 int todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo)
167 enum missing_commit_check_level check_level = get_missing_commit_check_level();
168 struct strbuf missing = STRBUF_INIT;
169 int res = 0, i;
170 struct commit_seen commit_seen;
172 init_commit_seen(&commit_seen);
174 if (check_level == MISSING_COMMIT_CHECK_IGNORE)
175 goto leave_check;
177 /* Mark the commits in git-rebase-todo as seen */
178 for (i = 0; i < new_todo->nr; i++) {
179 struct commit *commit = new_todo->items[i].commit;
180 if (commit)
181 *commit_seen_at(&commit_seen, commit) = 1;
184 /* Find commits in git-rebase-todo.backup yet unseen */
185 for (i = old_todo->nr - 1; i >= 0; i--) {
186 struct todo_item *item = old_todo->items + i;
187 struct commit *commit = item->commit;
188 if (commit && !*commit_seen_at(&commit_seen, commit)) {
189 strbuf_addf(&missing, " - %s %.*s\n",
190 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV),
191 item->arg_len,
192 todo_item_get_arg(old_todo, item));
193 *commit_seen_at(&commit_seen, commit) = 1;
197 /* Warn about missing commits */
198 if (!missing.len)
199 goto leave_check;
201 if (check_level == MISSING_COMMIT_CHECK_ERROR)
202 res = 1;
204 fprintf(stderr,
205 _("Warning: some commits may have been dropped accidentally.\n"
206 "Dropped commits (newer to older):\n"));
208 /* Make the list user-friendly and display */
209 fputs(missing.buf, stderr);
210 strbuf_release(&missing);
212 fprintf(stderr, _("To avoid this message, use \"drop\" to "
213 "explicitly remove a commit.\n\n"
214 "Use 'git config rebase.missingCommitsCheck' to change "
215 "the level of warnings.\n"
216 "The possible behaviours are: ignore, warn, error.\n\n"));
218 fprintf(stderr, _(edit_todo_list_advice));
220 leave_check:
221 clear_commit_seen(&commit_seen);
222 return res;
225 int todo_list_check_against_backup(struct repository *r, struct todo_list *todo_list)
227 struct todo_list backup = TODO_LIST_INIT;
228 int res = 0;
230 if (strbuf_read_file(&backup.buf, rebase_path_todo_backup(), 0) > 0) {
231 todo_list_parse_insn_buffer(r, backup.buf.buf, &backup);
232 res = todo_list_check(&backup, todo_list);
235 todo_list_release(&backup);
236 return res;