wrapper.h: move declarations for wrapper.c functions from cache.h
[git/debian.git] / rebase-interactive.c
blob7c885c35bfdd1be3afbe5c893c95d9ddc000a97e
1 #include "cache.h"
2 #include "commit.h"
3 #include "gettext.h"
4 #include "sequencer.h"
5 #include "rebase-interactive.h"
6 #include "strbuf.h"
7 #include "commit-slab.h"
8 #include "config.h"
9 #include "dir.h"
10 #include "wrapper.h"
12 static const char edit_todo_list_advice[] =
13 N_("You can fix this with 'git rebase --edit-todo' "
14 "and then run 'git rebase --continue'.\n"
15 "Or you can abort the rebase with 'git rebase"
16 " --abort'.\n");
18 enum missing_commit_check_level {
19 MISSING_COMMIT_CHECK_IGNORE = 0,
20 MISSING_COMMIT_CHECK_WARN,
21 MISSING_COMMIT_CHECK_ERROR
24 static enum missing_commit_check_level get_missing_commit_check_level(void)
26 const char *value;
28 if (git_config_get_value("rebase.missingcommitscheck", &value) ||
29 !strcasecmp("ignore", value))
30 return MISSING_COMMIT_CHECK_IGNORE;
31 if (!strcasecmp("warn", value))
32 return MISSING_COMMIT_CHECK_WARN;
33 if (!strcasecmp("error", value))
34 return MISSING_COMMIT_CHECK_ERROR;
35 warning(_("unrecognized setting %s for option "
36 "rebase.missingCommitsCheck. Ignoring."), value);
37 return MISSING_COMMIT_CHECK_IGNORE;
40 void append_todo_help(int command_count,
41 const char *shortrevisions, const char *shortonto,
42 struct strbuf *buf)
44 const char *msg = _("\nCommands:\n"
45 "p, pick <commit> = use commit\n"
46 "r, reword <commit> = use commit, but edit the commit message\n"
47 "e, edit <commit> = use commit, but stop for amending\n"
48 "s, squash <commit> = use commit, but meld into previous commit\n"
49 "f, fixup [-C | -c] <commit> = like \"squash\" but keep only the previous\n"
50 " commit's log message, unless -C is used, in which case\n"
51 " keep only this commit's message; -c is same as -C but\n"
52 " opens the editor\n"
53 "x, exec <command> = run command (the rest of the line) using shell\n"
54 "b, break = stop here (continue rebase later with 'git rebase --continue')\n"
55 "d, drop <commit> = remove commit\n"
56 "l, label <label> = label current HEAD with a name\n"
57 "t, reset <label> = reset HEAD to a label\n"
58 "m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]\n"
59 " create a merge commit using the original merge commit's\n"
60 " message (or the oneline, if no original merge commit was\n"
61 " specified); use -c <commit> to reword the commit message\n"
62 "u, update-ref <ref> = track a placeholder for the <ref> to be updated\n"
63 " to this position in the new commits. The <ref> is\n"
64 " updated at the end of the rebase\n"
65 "\n"
66 "These lines can be re-ordered; they are executed from top to bottom.\n");
67 unsigned edit_todo = !(shortrevisions && shortonto);
69 if (!edit_todo) {
70 strbuf_addch(buf, '\n');
71 strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)",
72 "Rebase %s onto %s (%d commands)",
73 command_count),
74 shortrevisions, shortonto, command_count);
77 strbuf_add_commented_lines(buf, msg, strlen(msg));
79 if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
80 msg = _("\nDo not remove any line. Use 'drop' "
81 "explicitly to remove a commit.\n");
82 else
83 msg = _("\nIf you remove a line here "
84 "THAT COMMIT WILL BE LOST.\n");
86 strbuf_add_commented_lines(buf, msg, strlen(msg));
88 if (edit_todo)
89 msg = _("\nYou are editing the todo file "
90 "of an ongoing interactive rebase.\n"
91 "To continue rebase after editing, run:\n"
92 " git rebase --continue\n\n");
93 else
94 msg = _("\nHowever, if you remove everything, "
95 "the rebase will be aborted.\n\n");
97 strbuf_add_commented_lines(buf, msg, strlen(msg));
100 int edit_todo_list(struct repository *r, struct todo_list *todo_list,
101 struct todo_list *new_todo, const char *shortrevisions,
102 const char *shortonto, unsigned flags)
104 const char *todo_file = rebase_path_todo(),
105 *todo_backup = rebase_path_todo_backup();
106 unsigned initial = shortrevisions && shortonto;
107 int incorrect = 0;
109 /* If the user is editing the todo list, we first try to parse
110 * it. If there is an error, we do not return, because the user
111 * might want to fix it in the first place. */
112 if (!initial)
113 incorrect = todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list) |
114 file_exists(rebase_path_dropped());
116 if (todo_list_write_to_file(r, todo_list, todo_file, shortrevisions, shortonto,
117 -1, flags | TODO_LIST_SHORTEN_IDS | TODO_LIST_APPEND_TODO_HELP))
118 return error_errno(_("could not write '%s'"), todo_file);
120 if (!incorrect &&
121 todo_list_write_to_file(r, todo_list, todo_backup,
122 shortrevisions, shortonto, -1,
123 (flags | TODO_LIST_APPEND_TODO_HELP) & ~TODO_LIST_SHORTEN_IDS) < 0)
124 return error(_("could not write '%s'."), rebase_path_todo_backup());
126 if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
127 return -2;
129 strbuf_stripspace(&new_todo->buf, 1);
130 if (initial && new_todo->buf.len == 0)
131 return -3;
133 if (todo_list_parse_insn_buffer(r, new_todo->buf.buf, new_todo)) {
134 fprintf(stderr, _(edit_todo_list_advice));
135 return -4;
138 if (incorrect) {
139 if (todo_list_check_against_backup(r, new_todo)) {
140 write_file(rebase_path_dropped(), "%s", "");
141 return -4;
144 if (incorrect > 0)
145 unlink(rebase_path_dropped());
146 } else if (todo_list_check(todo_list, new_todo)) {
147 write_file(rebase_path_dropped(), "%s", "");
148 return -4;
152 * See if branches need to be added or removed from the update-refs
153 * file based on the new todo list.
155 todo_list_filter_update_refs(r, new_todo);
157 return 0;
160 define_commit_slab(commit_seen, unsigned char);
162 * Check if the user dropped some commits by mistake
163 * Behaviour determined by rebase.missingCommitsCheck.
164 * Check if there is an unrecognized command or a
165 * bad SHA-1 in a command.
167 int todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo)
169 enum missing_commit_check_level check_level = get_missing_commit_check_level();
170 struct strbuf missing = STRBUF_INIT;
171 int res = 0, i;
172 struct commit_seen commit_seen;
174 init_commit_seen(&commit_seen);
176 if (check_level == MISSING_COMMIT_CHECK_IGNORE)
177 goto leave_check;
179 /* Mark the commits in git-rebase-todo as seen */
180 for (i = 0; i < new_todo->nr; i++) {
181 struct commit *commit = new_todo->items[i].commit;
182 if (commit)
183 *commit_seen_at(&commit_seen, commit) = 1;
186 /* Find commits in git-rebase-todo.backup yet unseen */
187 for (i = old_todo->nr - 1; i >= 0; i--) {
188 struct todo_item *item = old_todo->items + i;
189 struct commit *commit = item->commit;
190 if (commit && !*commit_seen_at(&commit_seen, commit)) {
191 strbuf_addf(&missing, " - %s %.*s\n",
192 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV),
193 item->arg_len,
194 todo_item_get_arg(old_todo, item));
195 *commit_seen_at(&commit_seen, commit) = 1;
199 /* Warn about missing commits */
200 if (!missing.len)
201 goto leave_check;
203 if (check_level == MISSING_COMMIT_CHECK_ERROR)
204 res = 1;
206 fprintf(stderr,
207 _("Warning: some commits may have been dropped accidentally.\n"
208 "Dropped commits (newer to older):\n"));
210 /* Make the list user-friendly and display */
211 fputs(missing.buf, stderr);
212 strbuf_release(&missing);
214 fprintf(stderr, _("To avoid this message, use \"drop\" to "
215 "explicitly remove a commit.\n\n"
216 "Use 'git config rebase.missingCommitsCheck' to change "
217 "the level of warnings.\n"
218 "The possible behaviours are: ignore, warn, error.\n\n"));
220 fprintf(stderr, _(edit_todo_list_advice));
222 leave_check:
223 clear_commit_seen(&commit_seen);
224 return res;
227 int todo_list_check_against_backup(struct repository *r, struct todo_list *todo_list)
229 struct todo_list backup = TODO_LIST_INIT;
230 int res = 0;
232 if (strbuf_read_file(&backup.buf, rebase_path_todo_backup(), 0) > 0) {
233 todo_list_parse_insn_buffer(r, backup.buf.buf, &backup);
234 res = todo_list_check(&backup, todo_list);
237 todo_list_release(&backup);
238 return res;