]>
Commit | Line | Data |
---|---|---|
145e05ac AG |
1 | #include "cache.h" |
2 | #include "commit.h" | |
145e05ac | 3 | #include "sequencer.h" |
6ca89c6f | 4 | #include "rebase-interactive.h" |
145e05ac | 5 | #include "strbuf.h" |
6ca89c6f AG |
6 | #include "commit-slab.h" |
7 | #include "config.h" | |
5a5445d8 | 8 | #include "dir.h" |
6ca89c6f | 9 | |
1da5874c AG |
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"); | |
15 | ||
6ca89c6f AG |
16 | enum missing_commit_check_level { |
17 | MISSING_COMMIT_CHECK_IGNORE = 0, | |
18 | MISSING_COMMIT_CHECK_WARN, | |
19 | MISSING_COMMIT_CHECK_ERROR | |
20 | }; | |
21 | ||
22 | static enum missing_commit_check_level get_missing_commit_check_level(void) | |
23 | { | |
24 | const char *value; | |
25 | ||
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; | |
36 | } | |
145e05ac | 37 | |
d48e5e21 | 38 | void append_todo_help(int command_count, |
af1fc3ad | 39 | const char *shortrevisions, const char *shortonto, |
a9f5476f | 40 | struct strbuf *buf) |
145e05ac | 41 | { |
145e05ac AG |
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" | |
f07871d3 CM |
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" | |
145e05ac | 51 | "x, exec <command> = run command (the rest of the line) using shell\n" |
71f82465 | 52 | "b, break = stop here (continue rebase later with 'git rebase --continue')\n" |
145e05ac AG |
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" | |
f07871d3 | 59 | ". specified); use -c <commit> to reword the commit message\n" |
145e05ac AG |
60 | "\n" |
61 | "These lines can be re-ordered; they are executed from top to bottom.\n"); | |
af1fc3ad AG |
62 | unsigned edit_todo = !(shortrevisions && shortonto); |
63 | ||
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); | |
70 | } | |
145e05ac | 71 | |
a9f5476f | 72 | strbuf_add_commented_lines(buf, msg, strlen(msg)); |
145e05ac AG |
73 | |
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"); | |
80 | ||
a9f5476f | 81 | strbuf_add_commented_lines(buf, msg, strlen(msg)); |
145e05ac AG |
82 | |
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"); | |
91 | ||
a9f5476f | 92 | strbuf_add_commented_lines(buf, msg, strlen(msg)); |
a9f5476f AG |
93 | } |
94 | ||
a930eb03 AG |
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) | |
64a43cbd | 98 | { |
5a5445d8 AG |
99 | const char *todo_file = rebase_path_todo(), |
100 | *todo_backup = rebase_path_todo_backup(); | |
a930eb03 | 101 | unsigned initial = shortrevisions && shortonto; |
5a5445d8 | 102 | int incorrect = 0; |
64a43cbd | 103 | |
a930eb03 AG |
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) | |
5a5445d8 AG |
108 | incorrect = todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list) | |
109 | file_exists(rebase_path_dropped()); | |
a930eb03 AG |
110 | |
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); | |
114 | ||
5a5445d8 AG |
115 | if (!incorrect && |
116 | todo_list_write_to_file(r, todo_list, todo_backup, | |
26027625 JS |
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()); | |
a930eb03 AG |
120 | |
121 | if (launch_sequence_editor(todo_file, &new_todo->buf, NULL)) | |
122 | return -2; | |
123 | ||
124 | strbuf_stripspace(&new_todo->buf, 1); | |
125 | if (initial && new_todo->buf.len == 0) | |
126 | return -3; | |
a9f5476f | 127 | |
5a5445d8 AG |
128 | if (todo_list_parse_insn_buffer(r, new_todo->buf.buf, new_todo)) { |
129 | fprintf(stderr, _(edit_todo_list_advice)); | |
130 | return -4; | |
131 | } | |
132 | ||
133 | if (incorrect) { | |
134 | if (todo_list_check_against_backup(r, new_todo)) { | |
7daf4f2a | 135 | write_file(rebase_path_dropped(), "%s", ""); |
5a5445d8 AG |
136 | return -4; |
137 | } | |
138 | ||
139 | if (incorrect > 0) | |
140 | unlink(rebase_path_dropped()); | |
141 | } else if (todo_list_check(todo_list, new_todo)) { | |
7daf4f2a | 142 | write_file(rebase_path_dropped(), "%s", ""); |
5a5445d8 AG |
143 | return -4; |
144 | } | |
64a43cbd | 145 | |
a930eb03 | 146 | return 0; |
64a43cbd | 147 | } |
6ca89c6f AG |
148 | |
149 | define_commit_slab(commit_seen, unsigned char); | |
150 | /* | |
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. | |
155 | */ | |
156 | int todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo) | |
157 | { | |
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; | |
162 | ||
163 | init_commit_seen(&commit_seen); | |
164 | ||
165 | if (check_level == MISSING_COMMIT_CHECK_IGNORE) | |
166 | goto leave_check; | |
167 | ||
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; | |
173 | } | |
174 | ||
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; | |
185 | } | |
186 | } | |
187 | ||
188 | /* Warn about missing commits */ | |
189 | if (!missing.len) | |
190 | goto leave_check; | |
191 | ||
192 | if (check_level == MISSING_COMMIT_CHECK_ERROR) | |
193 | res = 1; | |
194 | ||
195 | fprintf(stderr, | |
196 | _("Warning: some commits may have been dropped accidentally.\n" | |
197 | "Dropped commits (newer to older):\n")); | |
198 | ||
199 | /* Make the list user-friendly and display */ | |
200 | fputs(missing.buf, stderr); | |
201 | strbuf_release(&missing); | |
202 | ||
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")); | |
208 | ||
5a5445d8 AG |
209 | fprintf(stderr, _(edit_todo_list_advice)); |
210 | ||
6ca89c6f AG |
211 | leave_check: |
212 | clear_commit_seen(&commit_seen); | |
213 | return res; | |
214 | } | |
1da5874c | 215 | |
5a5445d8 AG |
216 | int todo_list_check_against_backup(struct repository *r, struct todo_list *todo_list) |
217 | { | |
218 | struct todo_list backup = TODO_LIST_INIT; | |
219 | int res = 0; | |
220 | ||
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); | |
224 | } | |
225 | ||
226 | todo_list_release(&backup); | |
227 | return res; | |
228 | } | |
229 | ||
1da5874c AG |
230 | int check_todo_list_from_file(struct repository *r) |
231 | { | |
232 | struct todo_list old_todo = TODO_LIST_INIT, new_todo = TODO_LIST_INIT; | |
233 | int res = 0; | |
234 | ||
235 | if (strbuf_read_file(&new_todo.buf, rebase_path_todo(), 0) < 0) { | |
236 | res = error(_("could not read '%s'."), rebase_path_todo()); | |
237 | goto out; | |
238 | } | |
239 | ||
240 | if (strbuf_read_file(&old_todo.buf, rebase_path_todo_backup(), 0) < 0) { | |
241 | res = error(_("could not read '%s'."), rebase_path_todo_backup()); | |
242 | goto out; | |
243 | } | |
244 | ||
245 | res = todo_list_parse_insn_buffer(r, old_todo.buf.buf, &old_todo); | |
246 | if (!res) | |
247 | res = todo_list_parse_insn_buffer(r, new_todo.buf.buf, &new_todo); | |
1da5874c AG |
248 | if (res) |
249 | fprintf(stderr, _(edit_todo_list_advice)); | |
5a5445d8 AG |
250 | if (!res) |
251 | res = todo_list_check(&old_todo, &new_todo); | |
1da5874c AG |
252 | out: |
253 | todo_list_release(&old_todo); | |
254 | todo_list_release(&new_todo); | |
255 | ||
256 | return res; | |
257 | } |