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