]>
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" | |
f57fd48d DS |
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" | |
a97d7916 DS |
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" | |
145e05ac AG |
63 | "\n" |
64 | "These lines can be re-ordered; they are executed from top to bottom.\n"); | |
af1fc3ad AG |
65 | unsigned edit_todo = !(shortrevisions && shortonto); |
66 | ||
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); | |
73 | } | |
145e05ac | 74 | |
a9f5476f | 75 | strbuf_add_commented_lines(buf, msg, strlen(msg)); |
145e05ac AG |
76 | |
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"); | |
83 | ||
a9f5476f | 84 | strbuf_add_commented_lines(buf, msg, strlen(msg)); |
145e05ac AG |
85 | |
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"); | |
94 | ||
a9f5476f | 95 | strbuf_add_commented_lines(buf, msg, strlen(msg)); |
a9f5476f AG |
96 | } |
97 | ||
a930eb03 AG |
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) | |
64a43cbd | 101 | { |
5a5445d8 AG |
102 | const char *todo_file = rebase_path_todo(), |
103 | *todo_backup = rebase_path_todo_backup(); | |
a930eb03 | 104 | unsigned initial = shortrevisions && shortonto; |
5a5445d8 | 105 | int incorrect = 0; |
64a43cbd | 106 | |
a930eb03 AG |
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) | |
5a5445d8 AG |
111 | incorrect = todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list) | |
112 | file_exists(rebase_path_dropped()); | |
a930eb03 AG |
113 | |
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); | |
117 | ||
5a5445d8 AG |
118 | if (!incorrect && |
119 | todo_list_write_to_file(r, todo_list, todo_backup, | |
26027625 JS |
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()); | |
a930eb03 AG |
123 | |
124 | if (launch_sequence_editor(todo_file, &new_todo->buf, NULL)) | |
125 | return -2; | |
126 | ||
127 | strbuf_stripspace(&new_todo->buf, 1); | |
128 | if (initial && new_todo->buf.len == 0) | |
129 | return -3; | |
a9f5476f | 130 | |
5a5445d8 AG |
131 | if (todo_list_parse_insn_buffer(r, new_todo->buf.buf, new_todo)) { |
132 | fprintf(stderr, _(edit_todo_list_advice)); | |
133 | return -4; | |
134 | } | |
135 | ||
136 | if (incorrect) { | |
137 | if (todo_list_check_against_backup(r, new_todo)) { | |
7daf4f2a | 138 | write_file(rebase_path_dropped(), "%s", ""); |
5a5445d8 AG |
139 | return -4; |
140 | } | |
141 | ||
142 | if (incorrect > 0) | |
143 | unlink(rebase_path_dropped()); | |
144 | } else if (todo_list_check(todo_list, new_todo)) { | |
7daf4f2a | 145 | write_file(rebase_path_dropped(), "%s", ""); |
5a5445d8 AG |
146 | return -4; |
147 | } | |
64a43cbd | 148 | |
b3b1a21d DS |
149 | /* |
150 | * See if branches need to be added or removed from the update-refs | |
151 | * file based on the new todo list. | |
152 | */ | |
153 | todo_list_filter_update_refs(r, new_todo); | |
154 | ||
a930eb03 | 155 | return 0; |
64a43cbd | 156 | } |
6ca89c6f AG |
157 | |
158 | define_commit_slab(commit_seen, unsigned char); | |
159 | /* | |
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. | |
164 | */ | |
165 | int todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo) | |
166 | { | |
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; | |
171 | ||
172 | init_commit_seen(&commit_seen); | |
173 | ||
174 | if (check_level == MISSING_COMMIT_CHECK_IGNORE) | |
175 | goto leave_check; | |
176 | ||
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; | |
182 | } | |
183 | ||
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; | |
194 | } | |
195 | } | |
196 | ||
197 | /* Warn about missing commits */ | |
198 | if (!missing.len) | |
199 | goto leave_check; | |
200 | ||
201 | if (check_level == MISSING_COMMIT_CHECK_ERROR) | |
202 | res = 1; | |
203 | ||
204 | fprintf(stderr, | |
205 | _("Warning: some commits may have been dropped accidentally.\n" | |
206 | "Dropped commits (newer to older):\n")); | |
207 | ||
208 | /* Make the list user-friendly and display */ | |
209 | fputs(missing.buf, stderr); | |
210 | strbuf_release(&missing); | |
211 | ||
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")); | |
217 | ||
5a5445d8 AG |
218 | fprintf(stderr, _(edit_todo_list_advice)); |
219 | ||
6ca89c6f AG |
220 | leave_check: |
221 | clear_commit_seen(&commit_seen); | |
222 | return res; | |
223 | } | |
1da5874c | 224 | |
5a5445d8 AG |
225 | int todo_list_check_against_backup(struct repository *r, struct todo_list *todo_list) |
226 | { | |
227 | struct todo_list backup = TODO_LIST_INIT; | |
228 | int res = 0; | |
229 | ||
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); | |
233 | } | |
234 | ||
235 | todo_list_release(&backup); | |
236 | return res; | |
237 | } |