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