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