]>
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" | |
9e3cebd9 CM |
47 | "f, fixup [-C | -c] <commit> = like \"squash\", but discard this\n" |
48 | " commit's log message. Use -C to replace with this\n" | |
49 | " commit message or -c to edit the commit message\n" | |
145e05ac | 50 | "x, exec <command> = run command (the rest of the line) using shell\n" |
71f82465 | 51 | "b, break = stop here (continue rebase later with 'git rebase --continue')\n" |
145e05ac AG |
52 | "d, drop <commit> = remove commit\n" |
53 | "l, label <label> = label current HEAD with a name\n" | |
54 | "t, reset <label> = reset HEAD to a label\n" | |
55 | "m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]\n" | |
56 | ". create a merge commit using the original merge commit's\n" | |
57 | ". message (or the oneline, if no original merge commit was\n" | |
58 | ". specified). Use -c <commit> to reword the commit message.\n" | |
59 | "\n" | |
60 | "These lines can be re-ordered; they are executed from top to bottom.\n"); | |
af1fc3ad AG |
61 | unsigned edit_todo = !(shortrevisions && shortonto); |
62 | ||
63 | if (!edit_todo) { | |
64 | strbuf_addch(buf, '\n'); | |
65 | strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)", | |
66 | "Rebase %s onto %s (%d commands)", | |
67 | command_count), | |
68 | shortrevisions, shortonto, command_count); | |
69 | } | |
145e05ac | 70 | |
a9f5476f | 71 | strbuf_add_commented_lines(buf, msg, strlen(msg)); |
145e05ac AG |
72 | |
73 | if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR) | |
74 | msg = _("\nDo not remove any line. Use 'drop' " | |
75 | "explicitly to remove a commit.\n"); | |
76 | else | |
77 | msg = _("\nIf you remove a line here " | |
78 | "THAT COMMIT WILL BE LOST.\n"); | |
79 | ||
a9f5476f | 80 | strbuf_add_commented_lines(buf, msg, strlen(msg)); |
145e05ac AG |
81 | |
82 | if (edit_todo) | |
83 | msg = _("\nYou are editing the todo file " | |
84 | "of an ongoing interactive rebase.\n" | |
85 | "To continue rebase after editing, run:\n" | |
86 | " git rebase --continue\n\n"); | |
87 | else | |
88 | msg = _("\nHowever, if you remove everything, " | |
89 | "the rebase will be aborted.\n\n"); | |
90 | ||
a9f5476f | 91 | strbuf_add_commented_lines(buf, msg, strlen(msg)); |
a9f5476f AG |
92 | } |
93 | ||
a930eb03 AG |
94 | int edit_todo_list(struct repository *r, struct todo_list *todo_list, |
95 | struct todo_list *new_todo, const char *shortrevisions, | |
96 | const char *shortonto, unsigned flags) | |
64a43cbd | 97 | { |
5a5445d8 AG |
98 | const char *todo_file = rebase_path_todo(), |
99 | *todo_backup = rebase_path_todo_backup(); | |
a930eb03 | 100 | unsigned initial = shortrevisions && shortonto; |
5a5445d8 | 101 | int incorrect = 0; |
64a43cbd | 102 | |
a930eb03 AG |
103 | /* If the user is editing the todo list, we first try to parse |
104 | * it. If there is an error, we do not return, because the user | |
105 | * might want to fix it in the first place. */ | |
106 | if (!initial) | |
5a5445d8 AG |
107 | incorrect = todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list) | |
108 | file_exists(rebase_path_dropped()); | |
a930eb03 AG |
109 | |
110 | if (todo_list_write_to_file(r, todo_list, todo_file, shortrevisions, shortonto, | |
111 | -1, flags | TODO_LIST_SHORTEN_IDS | TODO_LIST_APPEND_TODO_HELP)) | |
112 | return error_errno(_("could not write '%s'"), todo_file); | |
113 | ||
5a5445d8 AG |
114 | if (!incorrect && |
115 | todo_list_write_to_file(r, todo_list, todo_backup, | |
26027625 JS |
116 | shortrevisions, shortonto, -1, |
117 | (flags | TODO_LIST_APPEND_TODO_HELP) & ~TODO_LIST_SHORTEN_IDS) < 0) | |
118 | return error(_("could not write '%s'."), rebase_path_todo_backup()); | |
a930eb03 AG |
119 | |
120 | if (launch_sequence_editor(todo_file, &new_todo->buf, NULL)) | |
121 | return -2; | |
122 | ||
123 | strbuf_stripspace(&new_todo->buf, 1); | |
124 | if (initial && new_todo->buf.len == 0) | |
125 | return -3; | |
a9f5476f | 126 | |
5a5445d8 AG |
127 | if (todo_list_parse_insn_buffer(r, new_todo->buf.buf, new_todo)) { |
128 | fprintf(stderr, _(edit_todo_list_advice)); | |
129 | return -4; | |
130 | } | |
131 | ||
132 | if (incorrect) { | |
133 | if (todo_list_check_against_backup(r, new_todo)) { | |
7daf4f2a | 134 | write_file(rebase_path_dropped(), "%s", ""); |
5a5445d8 AG |
135 | return -4; |
136 | } | |
137 | ||
138 | if (incorrect > 0) | |
139 | unlink(rebase_path_dropped()); | |
140 | } else if (todo_list_check(todo_list, new_todo)) { | |
7daf4f2a | 141 | write_file(rebase_path_dropped(), "%s", ""); |
5a5445d8 AG |
142 | return -4; |
143 | } | |
64a43cbd | 144 | |
a930eb03 | 145 | return 0; |
64a43cbd | 146 | } |
6ca89c6f AG |
147 | |
148 | define_commit_slab(commit_seen, unsigned char); | |
149 | /* | |
150 | * Check if the user dropped some commits by mistake | |
151 | * Behaviour determined by rebase.missingCommitsCheck. | |
152 | * Check if there is an unrecognized command or a | |
153 | * bad SHA-1 in a command. | |
154 | */ | |
155 | int todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo) | |
156 | { | |
157 | enum missing_commit_check_level check_level = get_missing_commit_check_level(); | |
158 | struct strbuf missing = STRBUF_INIT; | |
159 | int res = 0, i; | |
160 | struct commit_seen commit_seen; | |
161 | ||
162 | init_commit_seen(&commit_seen); | |
163 | ||
164 | if (check_level == MISSING_COMMIT_CHECK_IGNORE) | |
165 | goto leave_check; | |
166 | ||
167 | /* Mark the commits in git-rebase-todo as seen */ | |
168 | for (i = 0; i < new_todo->nr; i++) { | |
169 | struct commit *commit = new_todo->items[i].commit; | |
170 | if (commit) | |
171 | *commit_seen_at(&commit_seen, commit) = 1; | |
172 | } | |
173 | ||
174 | /* Find commits in git-rebase-todo.backup yet unseen */ | |
175 | for (i = old_todo->nr - 1; i >= 0; i--) { | |
176 | struct todo_item *item = old_todo->items + i; | |
177 | struct commit *commit = item->commit; | |
178 | if (commit && !*commit_seen_at(&commit_seen, commit)) { | |
179 | strbuf_addf(&missing, " - %s %.*s\n", | |
180 | find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV), | |
181 | item->arg_len, | |
182 | todo_item_get_arg(old_todo, item)); | |
183 | *commit_seen_at(&commit_seen, commit) = 1; | |
184 | } | |
185 | } | |
186 | ||
187 | /* Warn about missing commits */ | |
188 | if (!missing.len) | |
189 | goto leave_check; | |
190 | ||
191 | if (check_level == MISSING_COMMIT_CHECK_ERROR) | |
192 | res = 1; | |
193 | ||
194 | fprintf(stderr, | |
195 | _("Warning: some commits may have been dropped accidentally.\n" | |
196 | "Dropped commits (newer to older):\n")); | |
197 | ||
198 | /* Make the list user-friendly and display */ | |
199 | fputs(missing.buf, stderr); | |
200 | strbuf_release(&missing); | |
201 | ||
202 | fprintf(stderr, _("To avoid this message, use \"drop\" to " | |
203 | "explicitly remove a commit.\n\n" | |
204 | "Use 'git config rebase.missingCommitsCheck' to change " | |
205 | "the level of warnings.\n" | |
206 | "The possible behaviours are: ignore, warn, error.\n\n")); | |
207 | ||
5a5445d8 AG |
208 | fprintf(stderr, _(edit_todo_list_advice)); |
209 | ||
6ca89c6f AG |
210 | leave_check: |
211 | clear_commit_seen(&commit_seen); | |
212 | return res; | |
213 | } | |
1da5874c | 214 | |
5a5445d8 AG |
215 | int todo_list_check_against_backup(struct repository *r, struct todo_list *todo_list) |
216 | { | |
217 | struct todo_list backup = TODO_LIST_INIT; | |
218 | int res = 0; | |
219 | ||
220 | if (strbuf_read_file(&backup.buf, rebase_path_todo_backup(), 0) > 0) { | |
221 | todo_list_parse_insn_buffer(r, backup.buf.buf, &backup); | |
222 | res = todo_list_check(&backup, todo_list); | |
223 | } | |
224 | ||
225 | todo_list_release(&backup); | |
226 | return res; | |
227 | } | |
228 | ||
1da5874c AG |
229 | int check_todo_list_from_file(struct repository *r) |
230 | { | |
231 | struct todo_list old_todo = TODO_LIST_INIT, new_todo = TODO_LIST_INIT; | |
232 | int res = 0; | |
233 | ||
234 | if (strbuf_read_file(&new_todo.buf, rebase_path_todo(), 0) < 0) { | |
235 | res = error(_("could not read '%s'."), rebase_path_todo()); | |
236 | goto out; | |
237 | } | |
238 | ||
239 | if (strbuf_read_file(&old_todo.buf, rebase_path_todo_backup(), 0) < 0) { | |
240 | res = error(_("could not read '%s'."), rebase_path_todo_backup()); | |
241 | goto out; | |
242 | } | |
243 | ||
244 | res = todo_list_parse_insn_buffer(r, old_todo.buf.buf, &old_todo); | |
245 | if (!res) | |
246 | res = todo_list_parse_insn_buffer(r, new_todo.buf.buf, &new_todo); | |
1da5874c AG |
247 | if (res) |
248 | fprintf(stderr, _(edit_todo_list_advice)); | |
5a5445d8 AG |
249 | if (!res) |
250 | res = todo_list_check(&old_todo, &new_todo); | |
1da5874c AG |
251 | out: |
252 | todo_list_release(&old_todo); | |
253 | todo_list_release(&new_todo); | |
254 | ||
255 | return res; | |
256 | } |