Line | Count | Source (jump to first uncovered line) |
1 | | #define USE_THE_REPOSITORY_VARIABLE |
2 | | |
3 | | #include "git-compat-util.h" |
4 | | #include "abspath.h" |
5 | | #include "advice.h" |
6 | | #include "config.h" |
7 | | #include "copy.h" |
8 | | #include "environment.h" |
9 | | #include "gettext.h" |
10 | | #include "hex.h" |
11 | | #include "lockfile.h" |
12 | | #include "dir.h" |
13 | | #include "object-file.h" |
14 | | #include "object-name.h" |
15 | | #include "object-store-ll.h" |
16 | | #include "object.h" |
17 | | #include "pager.h" |
18 | | #include "commit.h" |
19 | | #include "sequencer.h" |
20 | | #include "run-command.h" |
21 | | #include "hook.h" |
22 | | #include "utf8.h" |
23 | | #include "cache-tree.h" |
24 | | #include "diff.h" |
25 | | #include "path.h" |
26 | | #include "revision.h" |
27 | | #include "rerere.h" |
28 | | #include "merge.h" |
29 | | #include "merge-ort.h" |
30 | | #include "merge-ort-wrappers.h" |
31 | | #include "refs.h" |
32 | | #include "sparse-index.h" |
33 | | #include "strvec.h" |
34 | | #include "quote.h" |
35 | | #include "trailer.h" |
36 | | #include "log-tree.h" |
37 | | #include "wt-status.h" |
38 | | #include "hashmap.h" |
39 | | #include "notes-utils.h" |
40 | | #include "sigchain.h" |
41 | | #include "unpack-trees.h" |
42 | | #include "oidmap.h" |
43 | | #include "oidset.h" |
44 | | #include "commit-slab.h" |
45 | | #include "alias.h" |
46 | | #include "commit-reach.h" |
47 | | #include "rebase-interactive.h" |
48 | | #include "reset.h" |
49 | | #include "branch.h" |
50 | | |
51 | 0 | #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION" |
52 | | |
53 | | /* |
54 | | * To accommodate common filesystem limitations, where the loose refs' file |
55 | | * names must not exceed `NAME_MAX`, the labels generated by `git rebase |
56 | | * --rebase-merges` need to be truncated if the corresponding commit subjects |
57 | | * are too long. |
58 | | * Add some margin to stay clear from reaching `NAME_MAX`. |
59 | | */ |
60 | 0 | #define GIT_MAX_LABEL_LENGTH ((NAME_MAX) - (LOCK_SUFFIX_LEN) - 16) |
61 | | |
62 | | static const char sign_off_header[] = "Signed-off-by: "; |
63 | | static const char cherry_picked_prefix[] = "(cherry picked from commit "; |
64 | | |
65 | | GIT_PATH_FUNC(git_path_commit_editmsg, "COMMIT_EDITMSG") |
66 | | |
67 | | static GIT_PATH_FUNC(git_path_seq_dir, "sequencer") |
68 | | |
69 | | static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo") |
70 | | static GIT_PATH_FUNC(git_path_opts_file, "sequencer/opts") |
71 | | static GIT_PATH_FUNC(git_path_head_file, "sequencer/head") |
72 | | static GIT_PATH_FUNC(git_path_abort_safety_file, "sequencer/abort-safety") |
73 | | |
74 | | static GIT_PATH_FUNC(rebase_path, "rebase-merge") |
75 | | /* |
76 | | * The file containing rebase commands, comments, and empty lines. |
77 | | * This file is created by "git rebase -i" then edited by the user. As |
78 | | * the lines are processed, they are removed from the front of this |
79 | | * file and written to the tail of 'done'. |
80 | | */ |
81 | | GIT_PATH_FUNC(rebase_path_todo, "rebase-merge/git-rebase-todo") |
82 | | GIT_PATH_FUNC(rebase_path_todo_backup, "rebase-merge/git-rebase-todo.backup") |
83 | | |
84 | | GIT_PATH_FUNC(rebase_path_dropped, "rebase-merge/dropped") |
85 | | |
86 | | /* |
87 | | * The rebase command lines that have already been processed. A line |
88 | | * is moved here when it is first handled, before any associated user |
89 | | * actions. |
90 | | */ |
91 | | static GIT_PATH_FUNC(rebase_path_done, "rebase-merge/done") |
92 | | /* |
93 | | * The file to keep track of how many commands were already processed (e.g. |
94 | | * for the prompt). |
95 | | */ |
96 | | static GIT_PATH_FUNC(rebase_path_msgnum, "rebase-merge/msgnum") |
97 | | /* |
98 | | * The file to keep track of how many commands are to be processed in total |
99 | | * (e.g. for the prompt). |
100 | | */ |
101 | | static GIT_PATH_FUNC(rebase_path_msgtotal, "rebase-merge/end") |
102 | | /* |
103 | | * The commit message that is planned to be used for any changes that |
104 | | * need to be committed following a user interaction. |
105 | | */ |
106 | | static GIT_PATH_FUNC(rebase_path_message, "rebase-merge/message") |
107 | | /* |
108 | | * The file into which is accumulated the suggested commit message for |
109 | | * squash/fixup commands. When the first of a series of squash/fixups |
110 | | * is seen, the file is created and the commit message from the |
111 | | * previous commit and from the first squash/fixup commit are written |
112 | | * to it. The commit message for each subsequent squash/fixup commit |
113 | | * is appended to the file as it is processed. |
114 | | */ |
115 | | static GIT_PATH_FUNC(rebase_path_squash_msg, "rebase-merge/message-squash") |
116 | | /* |
117 | | * If the current series of squash/fixups has not yet included a squash |
118 | | * command, then this file exists and holds the commit message of the |
119 | | * original "pick" commit. (If the series ends without a "squash" |
120 | | * command, then this can be used as the commit message of the combined |
121 | | * commit without opening the editor.) |
122 | | */ |
123 | | static GIT_PATH_FUNC(rebase_path_fixup_msg, "rebase-merge/message-fixup") |
124 | | /* |
125 | | * This file contains the list fixup/squash commands that have been |
126 | | * accumulated into message-fixup or message-squash so far. |
127 | | */ |
128 | | static GIT_PATH_FUNC(rebase_path_current_fixups, "rebase-merge/current-fixups") |
129 | | /* |
130 | | * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and |
131 | | * GIT_AUTHOR_DATE that will be used for the commit that is currently |
132 | | * being rebased. |
133 | | */ |
134 | | static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script") |
135 | | /* |
136 | | * When an "edit" rebase command is being processed, the SHA1 of the |
137 | | * commit to be edited is recorded in this file. When "git rebase |
138 | | * --continue" is executed, if there are any staged changes then they |
139 | | * will be amended to the HEAD commit, but only provided the HEAD |
140 | | * commit is still the commit to be edited. When any other rebase |
141 | | * command is processed, this file is deleted. |
142 | | */ |
143 | | static GIT_PATH_FUNC(rebase_path_amend, "rebase-merge/amend") |
144 | | /* |
145 | | * When we stop at a given patch via the "edit" command, this file contains |
146 | | * the commit object name of the corresponding patch. |
147 | | */ |
148 | | static GIT_PATH_FUNC(rebase_path_stopped_sha, "rebase-merge/stopped-sha") |
149 | | /* |
150 | | * When we stop for the user to resolve conflicts this file contains |
151 | | * the patch of the commit that is being picked. |
152 | | */ |
153 | | static GIT_PATH_FUNC(rebase_path_patch, "rebase-merge/patch") |
154 | | /* |
155 | | * For the post-rewrite hook, we make a list of rewritten commits and |
156 | | * their new sha1s. The rewritten-pending list keeps the sha1s of |
157 | | * commits that have been processed, but not committed yet, |
158 | | * e.g. because they are waiting for a 'squash' command. |
159 | | */ |
160 | | static GIT_PATH_FUNC(rebase_path_rewritten_list, "rebase-merge/rewritten-list") |
161 | | static GIT_PATH_FUNC(rebase_path_rewritten_pending, |
162 | | "rebase-merge/rewritten-pending") |
163 | | |
164 | | /* |
165 | | * The path of the file containing the OID of the "squash onto" commit, i.e. |
166 | | * the dummy commit used for `reset [new root]`. |
167 | | */ |
168 | | static GIT_PATH_FUNC(rebase_path_squash_onto, "rebase-merge/squash-onto") |
169 | | |
170 | | /* |
171 | | * The path of the file listing refs that need to be deleted after the rebase |
172 | | * finishes. This is used by the `label` command to record the need for cleanup. |
173 | | */ |
174 | | static GIT_PATH_FUNC(rebase_path_refs_to_delete, "rebase-merge/refs-to-delete") |
175 | | |
176 | | /* |
177 | | * The update-refs file stores a list of refs that will be updated at the end |
178 | | * of the rebase sequence. The 'update-ref <ref>' commands in the todo file |
179 | | * update the OIDs for the refs in this file, but the refs are not updated |
180 | | * until the end of the rebase sequence. |
181 | | * |
182 | | * rebase_path_update_refs() returns the path to this file for a given |
183 | | * worktree directory. For the current worktree, pass the_repository->gitdir. |
184 | | */ |
185 | | static char *rebase_path_update_refs(const char *wt_git_dir) |
186 | 0 | { |
187 | 0 | return xstrfmt("%s/rebase-merge/update-refs", wt_git_dir); |
188 | 0 | } |
189 | | |
190 | | /* |
191 | | * The following files are written by git-rebase just after parsing the |
192 | | * command-line. |
193 | | */ |
194 | | static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt") |
195 | | static GIT_PATH_FUNC(rebase_path_cdate_is_adate, "rebase-merge/cdate_is_adate") |
196 | | static GIT_PATH_FUNC(rebase_path_ignore_date, "rebase-merge/ignore_date") |
197 | | static GIT_PATH_FUNC(rebase_path_orig_head, "rebase-merge/orig-head") |
198 | | static GIT_PATH_FUNC(rebase_path_verbose, "rebase-merge/verbose") |
199 | | static GIT_PATH_FUNC(rebase_path_quiet, "rebase-merge/quiet") |
200 | | static GIT_PATH_FUNC(rebase_path_signoff, "rebase-merge/signoff") |
201 | | static GIT_PATH_FUNC(rebase_path_head_name, "rebase-merge/head-name") |
202 | | static GIT_PATH_FUNC(rebase_path_onto, "rebase-merge/onto") |
203 | | static GIT_PATH_FUNC(rebase_path_autostash, "rebase-merge/autostash") |
204 | | static GIT_PATH_FUNC(rebase_path_strategy, "rebase-merge/strategy") |
205 | | static GIT_PATH_FUNC(rebase_path_strategy_opts, "rebase-merge/strategy_opts") |
206 | | static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate, "rebase-merge/allow_rerere_autoupdate") |
207 | | static GIT_PATH_FUNC(rebase_path_reschedule_failed_exec, "rebase-merge/reschedule-failed-exec") |
208 | | static GIT_PATH_FUNC(rebase_path_no_reschedule_failed_exec, "rebase-merge/no-reschedule-failed-exec") |
209 | | static GIT_PATH_FUNC(rebase_path_drop_redundant_commits, "rebase-merge/drop_redundant_commits") |
210 | | static GIT_PATH_FUNC(rebase_path_keep_redundant_commits, "rebase-merge/keep_redundant_commits") |
211 | | |
212 | | /* |
213 | | * A 'struct replay_ctx' represents the private state of the sequencer. |
214 | | */ |
215 | | struct replay_ctx { |
216 | | /* |
217 | | * The commit message that will be used except at the end of a |
218 | | * chain of fixup and squash commands. |
219 | | */ |
220 | | struct strbuf message; |
221 | | /* |
222 | | * The list of completed fixup and squash commands in the |
223 | | * current chain. |
224 | | */ |
225 | | struct strbuf current_fixups; |
226 | | /* |
227 | | * Stores the reflog message that will be used when creating a |
228 | | * commit. Points to a static buffer and should not be free()'d. |
229 | | */ |
230 | | const char *reflog_message; |
231 | | /* |
232 | | * The number of completed fixup and squash commands in the |
233 | | * current chain. |
234 | | */ |
235 | | int current_fixup_count; |
236 | | /* |
237 | | * Whether message contains a commit message. |
238 | | */ |
239 | | unsigned have_message :1; |
240 | | }; |
241 | | |
242 | | struct replay_ctx* replay_ctx_new(void) |
243 | 0 | { |
244 | 0 | struct replay_ctx *ctx = xcalloc(1, sizeof(*ctx)); |
245 | |
|
246 | 0 | strbuf_init(&ctx->current_fixups, 0); |
247 | 0 | strbuf_init(&ctx->message, 0); |
248 | |
|
249 | 0 | return ctx; |
250 | 0 | } |
251 | | |
252 | | /** |
253 | | * A 'struct update_refs_record' represents a value in the update-refs |
254 | | * list. We use a string_list to map refs to these (before, after) pairs. |
255 | | */ |
256 | | struct update_ref_record { |
257 | | struct object_id before; |
258 | | struct object_id after; |
259 | | }; |
260 | | |
261 | | static struct update_ref_record *init_update_ref_record(const char *ref) |
262 | 0 | { |
263 | 0 | struct update_ref_record *rec; |
264 | |
|
265 | 0 | CALLOC_ARRAY(rec, 1); |
266 | |
|
267 | 0 | oidcpy(&rec->before, null_oid()); |
268 | 0 | oidcpy(&rec->after, null_oid()); |
269 | | |
270 | | /* This may fail, but that's fine, we will keep the null OID. */ |
271 | 0 | refs_read_ref(get_main_ref_store(the_repository), ref, &rec->before); |
272 | |
|
273 | 0 | return rec; |
274 | 0 | } |
275 | | |
276 | | static int git_sequencer_config(const char *k, const char *v, |
277 | | const struct config_context *ctx, void *cb) |
278 | 0 | { |
279 | 0 | struct replay_opts *opts = cb; |
280 | |
|
281 | 0 | if (!strcmp(k, "commit.cleanup")) { |
282 | 0 | if (!v) |
283 | 0 | return config_error_nonbool(k); |
284 | | |
285 | 0 | if (!strcmp(v, "verbatim")) { |
286 | 0 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_NONE; |
287 | 0 | opts->explicit_cleanup = 1; |
288 | 0 | } else if (!strcmp(v, "whitespace")) { |
289 | 0 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SPACE; |
290 | 0 | opts->explicit_cleanup = 1; |
291 | 0 | } else if (!strcmp(v, "strip")) { |
292 | 0 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_ALL; |
293 | 0 | opts->explicit_cleanup = 1; |
294 | 0 | } else if (!strcmp(v, "scissors")) { |
295 | 0 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SCISSORS; |
296 | 0 | opts->explicit_cleanup = 1; |
297 | 0 | } else { |
298 | 0 | warning(_("invalid commit message cleanup mode '%s'"), |
299 | 0 | v); |
300 | 0 | } |
301 | |
|
302 | 0 | return 0; |
303 | 0 | } |
304 | | |
305 | 0 | if (!strcmp(k, "commit.gpgsign")) { |
306 | 0 | free(opts->gpg_sign); |
307 | 0 | opts->gpg_sign = git_config_bool(k, v) ? xstrdup("") : NULL; |
308 | 0 | return 0; |
309 | 0 | } |
310 | | |
311 | 0 | if (!opts->default_strategy && !strcmp(k, "pull.twohead")) { |
312 | 0 | int ret = git_config_string(&opts->default_strategy, k, v); |
313 | 0 | if (ret == 0) { |
314 | | /* |
315 | | * pull.twohead is allowed to be multi-valued; we only |
316 | | * care about the first value. |
317 | | */ |
318 | 0 | char *tmp = strchr(opts->default_strategy, ' '); |
319 | 0 | if (tmp) |
320 | 0 | *tmp = '\0'; |
321 | 0 | } |
322 | 0 | return ret; |
323 | 0 | } |
324 | | |
325 | 0 | if (opts->action == REPLAY_REVERT && !strcmp(k, "revert.reference")) |
326 | 0 | opts->commit_use_reference = git_config_bool(k, v); |
327 | |
|
328 | 0 | return git_diff_basic_config(k, v, ctx, NULL); |
329 | 0 | } |
330 | | |
331 | | void sequencer_init_config(struct replay_opts *opts) |
332 | 0 | { |
333 | 0 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_NONE; |
334 | 0 | git_config(git_sequencer_config, opts); |
335 | 0 | } |
336 | | |
337 | | static inline int is_rebase_i(const struct replay_opts *opts) |
338 | 0 | { |
339 | 0 | return opts->action == REPLAY_INTERACTIVE_REBASE; |
340 | 0 | } |
341 | | |
342 | | static const char *get_dir(const struct replay_opts *opts) |
343 | 0 | { |
344 | 0 | if (is_rebase_i(opts)) |
345 | 0 | return rebase_path(); |
346 | 0 | return git_path_seq_dir(); |
347 | 0 | } |
348 | | |
349 | | static const char *get_todo_path(const struct replay_opts *opts) |
350 | 0 | { |
351 | 0 | if (is_rebase_i(opts)) |
352 | 0 | return rebase_path_todo(); |
353 | 0 | return git_path_todo_file(); |
354 | 0 | } |
355 | | |
356 | | /* |
357 | | * Returns 0 for non-conforming footer |
358 | | * Returns 1 for conforming footer |
359 | | * Returns 2 when sob exists within conforming footer |
360 | | * Returns 3 when sob exists within conforming footer as last entry |
361 | | */ |
362 | | static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, |
363 | | size_t ignore_footer) |
364 | 0 | { |
365 | 0 | struct trailer_iterator iter; |
366 | 0 | size_t i = 0; |
367 | 0 | int found_sob = 0, found_sob_last = 0; |
368 | 0 | char saved_char; |
369 | |
|
370 | 0 | if (ignore_footer) { |
371 | 0 | saved_char = sb->buf[sb->len - ignore_footer]; |
372 | 0 | sb->buf[sb->len - ignore_footer] = '\0'; |
373 | 0 | } |
374 | |
|
375 | 0 | trailer_iterator_init(&iter, sb->buf); |
376 | |
|
377 | 0 | if (ignore_footer) |
378 | 0 | sb->buf[sb->len - ignore_footer] = saved_char; |
379 | |
|
380 | 0 | while (trailer_iterator_advance(&iter)) { |
381 | 0 | i++; |
382 | 0 | if (sob && !strncmp(iter.raw, sob->buf, sob->len)) |
383 | 0 | found_sob = i; |
384 | 0 | } |
385 | 0 | trailer_iterator_release(&iter); |
386 | |
|
387 | 0 | if (!i) |
388 | 0 | return 0; |
389 | | |
390 | 0 | found_sob_last = (int)i == found_sob; |
391 | |
|
392 | 0 | if (found_sob_last) |
393 | 0 | return 3; |
394 | 0 | if (found_sob) |
395 | 0 | return 2; |
396 | 0 | return 1; |
397 | 0 | } |
398 | | |
399 | | static const char *gpg_sign_opt_quoted(struct replay_opts *opts) |
400 | 0 | { |
401 | 0 | static struct strbuf buf = STRBUF_INIT; |
402 | |
|
403 | 0 | strbuf_reset(&buf); |
404 | 0 | if (opts->gpg_sign) |
405 | 0 | sq_quotef(&buf, "-S%s", opts->gpg_sign); |
406 | 0 | return buf.buf; |
407 | 0 | } |
408 | | |
409 | | static void replay_ctx_release(struct replay_ctx *ctx) |
410 | 0 | { |
411 | 0 | strbuf_release(&ctx->current_fixups); |
412 | 0 | strbuf_release(&ctx->message); |
413 | 0 | } |
414 | | |
415 | | void replay_opts_release(struct replay_opts *opts) |
416 | 0 | { |
417 | 0 | struct replay_ctx *ctx = opts->ctx; |
418 | |
|
419 | 0 | free(opts->gpg_sign); |
420 | 0 | free(opts->reflog_action); |
421 | 0 | free(opts->default_strategy); |
422 | 0 | free(opts->strategy); |
423 | 0 | strvec_clear (&opts->xopts); |
424 | 0 | if (opts->revs) |
425 | 0 | release_revisions(opts->revs); |
426 | 0 | free(opts->revs); |
427 | 0 | replay_ctx_release(ctx); |
428 | 0 | free(opts->ctx); |
429 | 0 | } |
430 | | |
431 | | int sequencer_remove_state(struct replay_opts *opts) |
432 | 0 | { |
433 | 0 | struct strbuf buf = STRBUF_INIT; |
434 | 0 | int ret = 0; |
435 | |
|
436 | 0 | if (is_rebase_i(opts) && |
437 | 0 | strbuf_read_file(&buf, rebase_path_refs_to_delete(), 0) > 0) { |
438 | 0 | char *p = buf.buf; |
439 | 0 | while (*p) { |
440 | 0 | char *eol = strchr(p, '\n'); |
441 | 0 | if (eol) |
442 | 0 | *eol = '\0'; |
443 | 0 | if (refs_delete_ref(get_main_ref_store(the_repository), "(rebase) cleanup", p, NULL, 0) < 0) { |
444 | 0 | warning(_("could not delete '%s'"), p); |
445 | 0 | ret = -1; |
446 | 0 | } |
447 | 0 | if (!eol) |
448 | 0 | break; |
449 | 0 | p = eol + 1; |
450 | 0 | } |
451 | 0 | } |
452 | |
|
453 | 0 | strbuf_reset(&buf); |
454 | 0 | strbuf_addstr(&buf, get_dir(opts)); |
455 | 0 | if (remove_dir_recursively(&buf, 0)) |
456 | 0 | ret = error(_("could not remove '%s'"), buf.buf); |
457 | 0 | strbuf_release(&buf); |
458 | |
|
459 | 0 | return ret; |
460 | 0 | } |
461 | | |
462 | | static const char *action_name(const struct replay_opts *opts) |
463 | 0 | { |
464 | 0 | switch (opts->action) { |
465 | 0 | case REPLAY_REVERT: |
466 | 0 | return N_("revert"); |
467 | 0 | case REPLAY_PICK: |
468 | 0 | return N_("cherry-pick"); |
469 | 0 | case REPLAY_INTERACTIVE_REBASE: |
470 | 0 | return N_("rebase"); |
471 | 0 | } |
472 | 0 | die(_("unknown action: %d"), opts->action); |
473 | 0 | } |
474 | | |
475 | | struct commit_message { |
476 | | char *parent_label; |
477 | | char *label; |
478 | | char *subject; |
479 | | const char *message; |
480 | | }; |
481 | | |
482 | | static const char *short_commit_name(struct repository *r, struct commit *commit) |
483 | 0 | { |
484 | 0 | return repo_find_unique_abbrev(r, &commit->object.oid, DEFAULT_ABBREV); |
485 | 0 | } |
486 | | |
487 | | static int get_message(struct commit *commit, struct commit_message *out) |
488 | 0 | { |
489 | 0 | const char *abbrev, *subject; |
490 | 0 | int subject_len; |
491 | |
|
492 | 0 | out->message = repo_logmsg_reencode(the_repository, commit, NULL, |
493 | 0 | get_commit_output_encoding()); |
494 | 0 | abbrev = short_commit_name(the_repository, commit); |
495 | |
|
496 | 0 | subject_len = find_commit_subject(out->message, &subject); |
497 | |
|
498 | 0 | out->subject = xmemdupz(subject, subject_len); |
499 | 0 | out->label = xstrfmt("%s (%s)", abbrev, out->subject); |
500 | 0 | out->parent_label = xstrfmt("parent of %s", out->label); |
501 | |
|
502 | 0 | return 0; |
503 | 0 | } |
504 | | |
505 | | static void free_message(struct commit *commit, struct commit_message *msg) |
506 | 0 | { |
507 | 0 | free(msg->parent_label); |
508 | 0 | free(msg->label); |
509 | 0 | free(msg->subject); |
510 | 0 | repo_unuse_commit_buffer(the_repository, commit, msg->message); |
511 | 0 | } |
512 | | |
513 | | const char *rebase_resolvemsg = |
514 | | N_("Resolve all conflicts manually, mark them as resolved with\n" |
515 | | "\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n" |
516 | | "You can instead skip this commit: run \"git rebase --skip\".\n" |
517 | | "To abort and get back to the state before \"git rebase\", run " |
518 | | "\"git rebase --abort\"."); |
519 | | |
520 | | static void print_advice(struct repository *r, int show_hint, |
521 | | struct replay_opts *opts) |
522 | 0 | { |
523 | 0 | const char *msg; |
524 | |
|
525 | 0 | if (is_rebase_i(opts)) |
526 | 0 | msg = rebase_resolvemsg; |
527 | 0 | else |
528 | 0 | msg = getenv("GIT_CHERRY_PICK_HELP"); |
529 | |
|
530 | 0 | if (msg) { |
531 | 0 | advise_if_enabled(ADVICE_MERGE_CONFLICT, "%s", msg); |
532 | | /* |
533 | | * A conflict has occurred but the porcelain |
534 | | * (typically rebase --interactive) wants to take care |
535 | | * of the commit itself so remove CHERRY_PICK_HEAD |
536 | | */ |
537 | 0 | refs_delete_ref(get_main_ref_store(r), "", "CHERRY_PICK_HEAD", |
538 | 0 | NULL, REF_NO_DEREF); |
539 | 0 | return; |
540 | 0 | } |
541 | | |
542 | 0 | if (show_hint) { |
543 | 0 | if (opts->no_commit) |
544 | 0 | advise_if_enabled(ADVICE_MERGE_CONFLICT, |
545 | 0 | _("after resolving the conflicts, mark the corrected paths\n" |
546 | 0 | "with 'git add <paths>' or 'git rm <paths>'")); |
547 | 0 | else if (opts->action == REPLAY_PICK) |
548 | 0 | advise_if_enabled(ADVICE_MERGE_CONFLICT, |
549 | 0 | _("After resolving the conflicts, mark them with\n" |
550 | 0 | "\"git add/rm <pathspec>\", then run\n" |
551 | 0 | "\"git cherry-pick --continue\".\n" |
552 | 0 | "You can instead skip this commit with \"git cherry-pick --skip\".\n" |
553 | 0 | "To abort and get back to the state before \"git cherry-pick\",\n" |
554 | 0 | "run \"git cherry-pick --abort\".")); |
555 | 0 | else if (opts->action == REPLAY_REVERT) |
556 | 0 | advise_if_enabled(ADVICE_MERGE_CONFLICT, |
557 | 0 | _("After resolving the conflicts, mark them with\n" |
558 | 0 | "\"git add/rm <pathspec>\", then run\n" |
559 | 0 | "\"git revert --continue\".\n" |
560 | 0 | "You can instead skip this commit with \"git revert --skip\".\n" |
561 | 0 | "To abort and get back to the state before \"git revert\",\n" |
562 | 0 | "run \"git revert --abort\".")); |
563 | 0 | else |
564 | 0 | BUG("unexpected pick action in print_advice()"); |
565 | 0 | } |
566 | 0 | } |
567 | | |
568 | | static int write_message(const void *buf, size_t len, const char *filename, |
569 | | int append_eol) |
570 | 0 | { |
571 | 0 | struct lock_file msg_file = LOCK_INIT; |
572 | |
|
573 | 0 | int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0); |
574 | 0 | if (msg_fd < 0) |
575 | 0 | return error_errno(_("could not lock '%s'"), filename); |
576 | 0 | if (write_in_full(msg_fd, buf, len) < 0) { |
577 | 0 | error_errno(_("could not write to '%s'"), filename); |
578 | 0 | rollback_lock_file(&msg_file); |
579 | 0 | return -1; |
580 | 0 | } |
581 | 0 | if (append_eol && write(msg_fd, "\n", 1) < 0) { |
582 | 0 | error_errno(_("could not write eol to '%s'"), filename); |
583 | 0 | rollback_lock_file(&msg_file); |
584 | 0 | return -1; |
585 | 0 | } |
586 | 0 | if (commit_lock_file(&msg_file) < 0) |
587 | 0 | return error(_("failed to finalize '%s'"), filename); |
588 | | |
589 | 0 | return 0; |
590 | 0 | } |
591 | | |
592 | | int read_oneliner(struct strbuf *buf, |
593 | | const char *path, unsigned flags) |
594 | 0 | { |
595 | 0 | int orig_len = buf->len; |
596 | |
|
597 | 0 | if (strbuf_read_file(buf, path, 0) < 0) { |
598 | 0 | if ((flags & READ_ONELINER_WARN_MISSING) || |
599 | 0 | (errno != ENOENT && errno != ENOTDIR)) |
600 | 0 | warning_errno(_("could not read '%s'"), path); |
601 | 0 | return 0; |
602 | 0 | } |
603 | | |
604 | 0 | if (buf->len > orig_len && buf->buf[buf->len - 1] == '\n') { |
605 | 0 | if (--buf->len > orig_len && buf->buf[buf->len - 1] == '\r') |
606 | 0 | --buf->len; |
607 | 0 | buf->buf[buf->len] = '\0'; |
608 | 0 | } |
609 | |
|
610 | 0 | if ((flags & READ_ONELINER_SKIP_IF_EMPTY) && buf->len == orig_len) |
611 | 0 | return 0; |
612 | | |
613 | 0 | return 1; |
614 | 0 | } |
615 | | |
616 | | static struct tree *empty_tree(struct repository *r) |
617 | 0 | { |
618 | 0 | return lookup_tree(r, the_hash_algo->empty_tree); |
619 | 0 | } |
620 | | |
621 | | static int error_dirty_index(struct repository *repo, struct replay_opts *opts) |
622 | 0 | { |
623 | 0 | if (repo_read_index_unmerged(repo)) |
624 | 0 | return error_resolve_conflict(action_name(opts)); |
625 | | |
626 | 0 | error(_("your local changes would be overwritten by %s."), |
627 | 0 | _(action_name(opts))); |
628 | |
|
629 | 0 | if (advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)) |
630 | 0 | advise(_("commit your changes or stash them to proceed.")); |
631 | 0 | return -1; |
632 | 0 | } |
633 | | |
634 | | static void update_abort_safety_file(void) |
635 | 0 | { |
636 | 0 | struct object_id head; |
637 | | |
638 | | /* Do nothing on a single-pick */ |
639 | 0 | if (!file_exists(git_path_seq_dir())) |
640 | 0 | return; |
641 | | |
642 | 0 | if (!repo_get_oid(the_repository, "HEAD", &head)) |
643 | 0 | write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head)); |
644 | 0 | else |
645 | 0 | write_file(git_path_abort_safety_file(), "%s", ""); |
646 | 0 | } |
647 | | |
648 | | static int fast_forward_to(struct repository *r, |
649 | | const struct object_id *to, |
650 | | const struct object_id *from, |
651 | | int unborn, |
652 | | struct replay_opts *opts) |
653 | 0 | { |
654 | 0 | struct ref_transaction *transaction; |
655 | 0 | struct strbuf sb = STRBUF_INIT; |
656 | 0 | struct strbuf err = STRBUF_INIT; |
657 | |
|
658 | 0 | repo_read_index(r); |
659 | 0 | if (checkout_fast_forward(r, from, to, 1)) |
660 | 0 | return -1; /* the callee should have complained already */ |
661 | | |
662 | 0 | strbuf_addf(&sb, "%s: fast-forward", action_name(opts)); |
663 | |
|
664 | 0 | transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), |
665 | 0 | &err); |
666 | 0 | if (!transaction || |
667 | 0 | ref_transaction_update(transaction, "HEAD", |
668 | 0 | to, unborn && !is_rebase_i(opts) ? |
669 | 0 | null_oid() : from, NULL, NULL, |
670 | 0 | 0, sb.buf, &err) || |
671 | 0 | ref_transaction_commit(transaction, &err)) { |
672 | 0 | ref_transaction_free(transaction); |
673 | 0 | error("%s", err.buf); |
674 | 0 | strbuf_release(&sb); |
675 | 0 | strbuf_release(&err); |
676 | 0 | return -1; |
677 | 0 | } |
678 | | |
679 | 0 | strbuf_release(&sb); |
680 | 0 | strbuf_release(&err); |
681 | 0 | ref_transaction_free(transaction); |
682 | 0 | update_abort_safety_file(); |
683 | 0 | return 0; |
684 | 0 | } |
685 | | |
686 | | enum commit_msg_cleanup_mode get_cleanup_mode(const char *cleanup_arg, |
687 | | int use_editor) |
688 | 0 | { |
689 | 0 | if (!cleanup_arg || !strcmp(cleanup_arg, "default")) |
690 | 0 | return use_editor ? COMMIT_MSG_CLEANUP_ALL : |
691 | 0 | COMMIT_MSG_CLEANUP_SPACE; |
692 | 0 | else if (!strcmp(cleanup_arg, "verbatim")) |
693 | 0 | return COMMIT_MSG_CLEANUP_NONE; |
694 | 0 | else if (!strcmp(cleanup_arg, "whitespace")) |
695 | 0 | return COMMIT_MSG_CLEANUP_SPACE; |
696 | 0 | else if (!strcmp(cleanup_arg, "strip")) |
697 | 0 | return COMMIT_MSG_CLEANUP_ALL; |
698 | 0 | else if (!strcmp(cleanup_arg, "scissors")) |
699 | 0 | return use_editor ? COMMIT_MSG_CLEANUP_SCISSORS : |
700 | 0 | COMMIT_MSG_CLEANUP_SPACE; |
701 | 0 | else |
702 | 0 | die(_("Invalid cleanup mode %s"), cleanup_arg); |
703 | 0 | } |
704 | | |
705 | | /* |
706 | | * NB using int rather than enum cleanup_mode to stop clang's |
707 | | * -Wtautological-constant-out-of-range-compare complaining that the comparison |
708 | | * is always true. |
709 | | */ |
710 | | static const char *describe_cleanup_mode(int cleanup_mode) |
711 | 0 | { |
712 | 0 | static const char *modes[] = { "whitespace", |
713 | 0 | "verbatim", |
714 | 0 | "scissors", |
715 | 0 | "strip" }; |
716 | |
|
717 | 0 | if (cleanup_mode < ARRAY_SIZE(modes)) |
718 | 0 | return modes[cleanup_mode]; |
719 | | |
720 | 0 | BUG("invalid cleanup_mode provided (%d)", cleanup_mode); |
721 | 0 | } |
722 | | |
723 | | void append_conflicts_hint(struct index_state *istate, |
724 | | struct strbuf *msgbuf, enum commit_msg_cleanup_mode cleanup_mode) |
725 | 0 | { |
726 | 0 | int i; |
727 | |
|
728 | 0 | if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) { |
729 | 0 | strbuf_addch(msgbuf, '\n'); |
730 | 0 | wt_status_append_cut_line(msgbuf); |
731 | 0 | strbuf_addstr(msgbuf, comment_line_str); |
732 | 0 | } |
733 | |
|
734 | 0 | strbuf_addch(msgbuf, '\n'); |
735 | 0 | strbuf_commented_addf(msgbuf, comment_line_str, "Conflicts:\n"); |
736 | 0 | for (i = 0; i < istate->cache_nr;) { |
737 | 0 | const struct cache_entry *ce = istate->cache[i++]; |
738 | 0 | if (ce_stage(ce)) { |
739 | 0 | strbuf_commented_addf(msgbuf, comment_line_str, |
740 | 0 | "\t%s\n", ce->name); |
741 | 0 | while (i < istate->cache_nr && |
742 | 0 | !strcmp(ce->name, istate->cache[i]->name)) |
743 | 0 | i++; |
744 | 0 | } |
745 | 0 | } |
746 | 0 | } |
747 | | |
748 | | static int do_recursive_merge(struct repository *r, |
749 | | struct commit *base, struct commit *next, |
750 | | const char *base_label, const char *next_label, |
751 | | struct object_id *head, struct strbuf *msgbuf, |
752 | | struct replay_opts *opts) |
753 | 0 | { |
754 | 0 | struct merge_options o; |
755 | 0 | struct merge_result result; |
756 | 0 | struct tree *next_tree, *base_tree, *head_tree; |
757 | 0 | int clean, show_output; |
758 | 0 | int i; |
759 | 0 | struct lock_file index_lock = LOCK_INIT; |
760 | |
|
761 | 0 | if (repo_hold_locked_index(r, &index_lock, LOCK_REPORT_ON_ERROR) < 0) |
762 | 0 | return -1; |
763 | | |
764 | 0 | repo_read_index(r); |
765 | |
|
766 | 0 | init_ui_merge_options(&o, r); |
767 | 0 | o.ancestor = base ? base_label : "(empty tree)"; |
768 | 0 | o.branch1 = "HEAD"; |
769 | 0 | o.branch2 = next ? next_label : "(empty tree)"; |
770 | 0 | if (is_rebase_i(opts)) |
771 | 0 | o.buffer_output = 2; |
772 | 0 | o.show_rename_progress = 1; |
773 | |
|
774 | 0 | head_tree = parse_tree_indirect(head); |
775 | 0 | if (!head_tree) |
776 | 0 | return error(_("unable to read tree (%s)"), oid_to_hex(head)); |
777 | 0 | next_tree = next ? repo_get_commit_tree(r, next) : empty_tree(r); |
778 | 0 | base_tree = base ? repo_get_commit_tree(r, base) : empty_tree(r); |
779 | |
|
780 | 0 | for (i = 0; i < opts->xopts.nr; i++) |
781 | 0 | parse_merge_opt(&o, opts->xopts.v[i]); |
782 | |
|
783 | 0 | if (!opts->strategy || !strcmp(opts->strategy, "ort")) { |
784 | 0 | memset(&result, 0, sizeof(result)); |
785 | 0 | merge_incore_nonrecursive(&o, base_tree, head_tree, next_tree, |
786 | 0 | &result); |
787 | 0 | show_output = !is_rebase_i(opts) || !result.clean; |
788 | | /* |
789 | | * TODO: merge_switch_to_result will update index/working tree; |
790 | | * we only really want to do that if !result.clean || this is |
791 | | * the final patch to be picked. But determining this is the |
792 | | * final patch would take some work, and "head_tree" would need |
793 | | * to be replace with the tree the index matched before we |
794 | | * started doing any picks. |
795 | | */ |
796 | 0 | merge_switch_to_result(&o, head_tree, &result, 1, show_output); |
797 | 0 | clean = result.clean; |
798 | 0 | } else { |
799 | 0 | ensure_full_index(r->index); |
800 | 0 | clean = merge_trees(&o, head_tree, next_tree, base_tree); |
801 | 0 | if (is_rebase_i(opts) && clean <= 0) |
802 | 0 | fputs(o.obuf.buf, stdout); |
803 | 0 | strbuf_release(&o.obuf); |
804 | 0 | } |
805 | 0 | if (clean < 0) { |
806 | 0 | rollback_lock_file(&index_lock); |
807 | 0 | return clean; |
808 | 0 | } |
809 | | |
810 | 0 | if (write_locked_index(r->index, &index_lock, |
811 | 0 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) |
812 | | /* |
813 | | * TRANSLATORS: %s will be "revert", "cherry-pick" or |
814 | | * "rebase". |
815 | | */ |
816 | 0 | return error(_("%s: Unable to write new index file"), |
817 | 0 | _(action_name(opts))); |
818 | | |
819 | 0 | if (!clean) |
820 | 0 | append_conflicts_hint(r->index, msgbuf, |
821 | 0 | opts->default_msg_cleanup); |
822 | |
|
823 | 0 | return !clean; |
824 | 0 | } |
825 | | |
826 | | static struct object_id *get_cache_tree_oid(struct index_state *istate) |
827 | 0 | { |
828 | 0 | if (!cache_tree_fully_valid(istate->cache_tree)) |
829 | 0 | if (cache_tree_update(istate, 0)) { |
830 | 0 | error(_("unable to update cache tree")); |
831 | 0 | return NULL; |
832 | 0 | } |
833 | | |
834 | 0 | return &istate->cache_tree->oid; |
835 | 0 | } |
836 | | |
837 | | static int is_index_unchanged(struct repository *r) |
838 | 0 | { |
839 | 0 | struct object_id head_oid, *cache_tree_oid; |
840 | 0 | const struct object_id *head_tree_oid; |
841 | 0 | struct commit *head_commit; |
842 | 0 | struct index_state *istate = r->index; |
843 | 0 | const char *head_name; |
844 | |
|
845 | 0 | if (!refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", RESOLVE_REF_READING, &head_oid, NULL)) { |
846 | | /* Check to see if this is an unborn branch */ |
847 | 0 | head_name = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), |
848 | 0 | "HEAD", |
849 | 0 | RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, |
850 | 0 | &head_oid, NULL); |
851 | 0 | if (!head_name || |
852 | 0 | !starts_with(head_name, "refs/heads/") || |
853 | 0 | !is_null_oid(&head_oid)) |
854 | 0 | return error(_("could not resolve HEAD commit")); |
855 | 0 | head_tree_oid = the_hash_algo->empty_tree; |
856 | 0 | } else { |
857 | 0 | head_commit = lookup_commit(r, &head_oid); |
858 | | |
859 | | /* |
860 | | * If head_commit is NULL, check_commit, called from |
861 | | * lookup_commit, would have indicated that head_commit is not |
862 | | * a commit object already. repo_parse_commit() will return failure |
863 | | * without further complaints in such a case. Otherwise, if |
864 | | * the commit is invalid, repo_parse_commit() will complain. So |
865 | | * there is nothing for us to say here. Just return failure. |
866 | | */ |
867 | 0 | if (repo_parse_commit(r, head_commit)) |
868 | 0 | return -1; |
869 | | |
870 | 0 | head_tree_oid = get_commit_tree_oid(head_commit); |
871 | 0 | } |
872 | | |
873 | 0 | if (!(cache_tree_oid = get_cache_tree_oid(istate))) |
874 | 0 | return -1; |
875 | | |
876 | 0 | return oideq(cache_tree_oid, head_tree_oid); |
877 | 0 | } |
878 | | |
879 | | static int write_author_script(const char *message) |
880 | 0 | { |
881 | 0 | struct strbuf buf = STRBUF_INIT; |
882 | 0 | const char *eol; |
883 | 0 | int res; |
884 | |
|
885 | 0 | for (;;) |
886 | 0 | if (!*message || starts_with(message, "\n")) { |
887 | 0 | missing_author: |
888 | | /* Missing 'author' line? */ |
889 | 0 | unlink(rebase_path_author_script()); |
890 | 0 | return 0; |
891 | 0 | } else if (skip_prefix(message, "author ", &message)) |
892 | 0 | break; |
893 | 0 | else if ((eol = strchr(message, '\n'))) |
894 | 0 | message = eol + 1; |
895 | 0 | else |
896 | 0 | goto missing_author; |
897 | | |
898 | 0 | strbuf_addstr(&buf, "GIT_AUTHOR_NAME='"); |
899 | 0 | while (*message && *message != '\n' && *message != '\r') |
900 | 0 | if (skip_prefix(message, " <", &message)) |
901 | 0 | break; |
902 | 0 | else if (*message != '\'') |
903 | 0 | strbuf_addch(&buf, *(message++)); |
904 | 0 | else |
905 | 0 | strbuf_addf(&buf, "'\\%c'", *(message++)); |
906 | 0 | strbuf_addstr(&buf, "'\nGIT_AUTHOR_EMAIL='"); |
907 | 0 | while (*message && *message != '\n' && *message != '\r') |
908 | 0 | if (skip_prefix(message, "> ", &message)) |
909 | 0 | break; |
910 | 0 | else if (*message != '\'') |
911 | 0 | strbuf_addch(&buf, *(message++)); |
912 | 0 | else |
913 | 0 | strbuf_addf(&buf, "'\\%c'", *(message++)); |
914 | 0 | strbuf_addstr(&buf, "'\nGIT_AUTHOR_DATE='@"); |
915 | 0 | while (*message && *message != '\n' && *message != '\r') |
916 | 0 | if (*message != '\'') |
917 | 0 | strbuf_addch(&buf, *(message++)); |
918 | 0 | else |
919 | 0 | strbuf_addf(&buf, "'\\%c'", *(message++)); |
920 | 0 | strbuf_addch(&buf, '\''); |
921 | 0 | res = write_message(buf.buf, buf.len, rebase_path_author_script(), 1); |
922 | 0 | strbuf_release(&buf); |
923 | 0 | return res; |
924 | 0 | } |
925 | | |
926 | | /** |
927 | | * Take a series of KEY='VALUE' lines where VALUE part is |
928 | | * sq-quoted, and append <KEY, VALUE> at the end of the string list |
929 | | */ |
930 | | static int parse_key_value_squoted(char *buf, struct string_list *list) |
931 | 0 | { |
932 | 0 | while (*buf) { |
933 | 0 | struct string_list_item *item; |
934 | 0 | char *np; |
935 | 0 | char *cp = strchr(buf, '='); |
936 | 0 | if (!cp) { |
937 | 0 | np = strchrnul(buf, '\n'); |
938 | 0 | return error(_("no key present in '%.*s'"), |
939 | 0 | (int) (np - buf), buf); |
940 | 0 | } |
941 | 0 | np = strchrnul(cp, '\n'); |
942 | 0 | *cp++ = '\0'; |
943 | 0 | item = string_list_append(list, buf); |
944 | |
|
945 | 0 | buf = np + (*np == '\n'); |
946 | 0 | *np = '\0'; |
947 | 0 | cp = sq_dequote(cp); |
948 | 0 | if (!cp) |
949 | 0 | return error(_("unable to dequote value of '%s'"), |
950 | 0 | item->string); |
951 | 0 | item->util = xstrdup(cp); |
952 | 0 | } |
953 | 0 | return 0; |
954 | 0 | } |
955 | | |
956 | | /** |
957 | | * Reads and parses the state directory's "author-script" file, and sets name, |
958 | | * email and date accordingly. |
959 | | * Returns 0 on success, -1 if the file could not be parsed. |
960 | | * |
961 | | * The author script is of the format: |
962 | | * |
963 | | * GIT_AUTHOR_NAME='$author_name' |
964 | | * GIT_AUTHOR_EMAIL='$author_email' |
965 | | * GIT_AUTHOR_DATE='$author_date' |
966 | | * |
967 | | * where $author_name, $author_email and $author_date are quoted. We are strict |
968 | | * with our parsing, as the file was meant to be eval'd in the now-removed |
969 | | * git-am.sh/git-rebase--interactive.sh scripts, and thus if the file differs |
970 | | * from what this function expects, it is better to bail out than to do |
971 | | * something that the user does not expect. |
972 | | */ |
973 | | int read_author_script(const char *path, char **name, char **email, char **date, |
974 | | int allow_missing) |
975 | 0 | { |
976 | 0 | struct strbuf buf = STRBUF_INIT; |
977 | 0 | struct string_list kv = STRING_LIST_INIT_DUP; |
978 | 0 | int retval = -1; /* assume failure */ |
979 | 0 | int i, name_i = -2, email_i = -2, date_i = -2, err = 0; |
980 | |
|
981 | 0 | if (strbuf_read_file(&buf, path, 256) <= 0) { |
982 | 0 | strbuf_release(&buf); |
983 | 0 | if (errno == ENOENT && allow_missing) |
984 | 0 | return 0; |
985 | 0 | else |
986 | 0 | return error_errno(_("could not open '%s' for reading"), |
987 | 0 | path); |
988 | 0 | } |
989 | | |
990 | 0 | if (parse_key_value_squoted(buf.buf, &kv)) |
991 | 0 | goto finish; |
992 | | |
993 | 0 | for (i = 0; i < kv.nr; i++) { |
994 | 0 | if (!strcmp(kv.items[i].string, "GIT_AUTHOR_NAME")) { |
995 | 0 | if (name_i != -2) |
996 | 0 | name_i = error(_("'GIT_AUTHOR_NAME' already given")); |
997 | 0 | else |
998 | 0 | name_i = i; |
999 | 0 | } else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_EMAIL")) { |
1000 | 0 | if (email_i != -2) |
1001 | 0 | email_i = error(_("'GIT_AUTHOR_EMAIL' already given")); |
1002 | 0 | else |
1003 | 0 | email_i = i; |
1004 | 0 | } else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_DATE")) { |
1005 | 0 | if (date_i != -2) |
1006 | 0 | date_i = error(_("'GIT_AUTHOR_DATE' already given")); |
1007 | 0 | else |
1008 | 0 | date_i = i; |
1009 | 0 | } else { |
1010 | 0 | err = error(_("unknown variable '%s'"), |
1011 | 0 | kv.items[i].string); |
1012 | 0 | } |
1013 | 0 | } |
1014 | 0 | if (name_i == -2) |
1015 | 0 | error(_("missing 'GIT_AUTHOR_NAME'")); |
1016 | 0 | if (email_i == -2) |
1017 | 0 | error(_("missing 'GIT_AUTHOR_EMAIL'")); |
1018 | 0 | if (date_i == -2) |
1019 | 0 | error(_("missing 'GIT_AUTHOR_DATE'")); |
1020 | 0 | if (name_i < 0 || email_i < 0 || date_i < 0 || err) |
1021 | 0 | goto finish; |
1022 | 0 | *name = kv.items[name_i].util; |
1023 | 0 | *email = kv.items[email_i].util; |
1024 | 0 | *date = kv.items[date_i].util; |
1025 | 0 | retval = 0; |
1026 | 0 | finish: |
1027 | 0 | string_list_clear(&kv, !!retval); |
1028 | 0 | strbuf_release(&buf); |
1029 | 0 | return retval; |
1030 | 0 | } |
1031 | | |
1032 | | /* |
1033 | | * Read a GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL AND GIT_AUTHOR_DATE from a |
1034 | | * file with shell quoting into struct strvec. Returns -1 on |
1035 | | * error, 0 otherwise. |
1036 | | */ |
1037 | | static int read_env_script(struct strvec *env) |
1038 | 0 | { |
1039 | 0 | char *name, *email, *date; |
1040 | |
|
1041 | 0 | if (read_author_script(rebase_path_author_script(), |
1042 | 0 | &name, &email, &date, 0)) |
1043 | 0 | return -1; |
1044 | | |
1045 | 0 | strvec_pushf(env, "GIT_AUTHOR_NAME=%s", name); |
1046 | 0 | strvec_pushf(env, "GIT_AUTHOR_EMAIL=%s", email); |
1047 | 0 | strvec_pushf(env, "GIT_AUTHOR_DATE=%s", date); |
1048 | 0 | free(name); |
1049 | 0 | free(email); |
1050 | 0 | free(date); |
1051 | |
|
1052 | 0 | return 0; |
1053 | 0 | } |
1054 | | |
1055 | | static char *get_author(const char *message) |
1056 | 0 | { |
1057 | 0 | size_t len; |
1058 | 0 | const char *a; |
1059 | |
|
1060 | 0 | a = find_commit_header(message, "author", &len); |
1061 | 0 | if (a) |
1062 | 0 | return xmemdupz(a, len); |
1063 | | |
1064 | 0 | return NULL; |
1065 | 0 | } |
1066 | | |
1067 | | static const char *author_date_from_env(const struct strvec *env) |
1068 | 0 | { |
1069 | 0 | int i; |
1070 | 0 | const char *date; |
1071 | |
|
1072 | 0 | for (i = 0; i < env->nr; i++) |
1073 | 0 | if (skip_prefix(env->v[i], |
1074 | 0 | "GIT_AUTHOR_DATE=", &date)) |
1075 | 0 | return date; |
1076 | | /* |
1077 | | * If GIT_AUTHOR_DATE is missing we should have already errored out when |
1078 | | * reading the script |
1079 | | */ |
1080 | 0 | BUG("GIT_AUTHOR_DATE missing from author script"); |
1081 | 0 | } |
1082 | | |
1083 | | static const char staged_changes_advice[] = |
1084 | | N_("you have staged changes in your working tree\n" |
1085 | | "If these changes are meant to be squashed into the previous commit, run:\n" |
1086 | | "\n" |
1087 | | " git commit --amend %s\n" |
1088 | | "\n" |
1089 | | "If they are meant to go into a new commit, run:\n" |
1090 | | "\n" |
1091 | | " git commit %s\n" |
1092 | | "\n" |
1093 | | "In both cases, once you're done, continue with:\n" |
1094 | | "\n" |
1095 | | " git rebase --continue\n"); |
1096 | | |
1097 | 0 | #define ALLOW_EMPTY (1<<0) |
1098 | 0 | #define EDIT_MSG (1<<1) |
1099 | 0 | #define AMEND_MSG (1<<2) |
1100 | 0 | #define CLEANUP_MSG (1<<3) |
1101 | 0 | #define VERIFY_MSG (1<<4) |
1102 | 0 | #define CREATE_ROOT_COMMIT (1<<5) |
1103 | 0 | #define VERBATIM_MSG (1<<6) |
1104 | | |
1105 | | static int run_command_silent_on_success(struct child_process *cmd) |
1106 | 0 | { |
1107 | 0 | struct strbuf buf = STRBUF_INIT; |
1108 | 0 | int rc; |
1109 | |
|
1110 | 0 | cmd->stdout_to_stderr = 1; |
1111 | 0 | rc = pipe_command(cmd, |
1112 | 0 | NULL, 0, |
1113 | 0 | NULL, 0, |
1114 | 0 | &buf, 0); |
1115 | |
|
1116 | 0 | if (rc) |
1117 | 0 | fputs(buf.buf, stderr); |
1118 | 0 | strbuf_release(&buf); |
1119 | 0 | return rc; |
1120 | 0 | } |
1121 | | |
1122 | | /* |
1123 | | * If we are cherry-pick, and if the merge did not result in |
1124 | | * hand-editing, we will hit this commit and inherit the original |
1125 | | * author date and name. |
1126 | | * |
1127 | | * If we are revert, or if our cherry-pick results in a hand merge, |
1128 | | * we had better say that the current user is responsible for that. |
1129 | | * |
1130 | | * An exception is when run_git_commit() is called during an |
1131 | | * interactive rebase: in that case, we will want to retain the |
1132 | | * author metadata. |
1133 | | */ |
1134 | | static int run_git_commit(const char *defmsg, |
1135 | | struct replay_opts *opts, |
1136 | | unsigned int flags) |
1137 | 0 | { |
1138 | 0 | struct replay_ctx *ctx = opts->ctx; |
1139 | 0 | struct child_process cmd = CHILD_PROCESS_INIT; |
1140 | |
|
1141 | 0 | if ((flags & CLEANUP_MSG) && (flags & VERBATIM_MSG)) |
1142 | 0 | BUG("CLEANUP_MSG and VERBATIM_MSG are mutually exclusive"); |
1143 | | |
1144 | 0 | cmd.git_cmd = 1; |
1145 | |
|
1146 | 0 | if (is_rebase_i(opts) && |
1147 | 0 | ((opts->committer_date_is_author_date && !opts->ignore_date) || |
1148 | 0 | !(!defmsg && (flags & AMEND_MSG))) && |
1149 | 0 | read_env_script(&cmd.env)) { |
1150 | 0 | const char *gpg_opt = gpg_sign_opt_quoted(opts); |
1151 | |
|
1152 | 0 | return error(_(staged_changes_advice), |
1153 | 0 | gpg_opt, gpg_opt); |
1154 | 0 | } |
1155 | | |
1156 | 0 | strvec_pushf(&cmd.env, GIT_REFLOG_ACTION "=%s", ctx->reflog_message); |
1157 | |
|
1158 | 0 | if (opts->committer_date_is_author_date) |
1159 | 0 | strvec_pushf(&cmd.env, "GIT_COMMITTER_DATE=%s", |
1160 | 0 | opts->ignore_date ? |
1161 | 0 | "" : |
1162 | 0 | author_date_from_env(&cmd.env)); |
1163 | 0 | if (opts->ignore_date) |
1164 | 0 | strvec_push(&cmd.env, "GIT_AUTHOR_DATE="); |
1165 | |
|
1166 | 0 | strvec_push(&cmd.args, "commit"); |
1167 | |
|
1168 | 0 | if (!(flags & VERIFY_MSG)) |
1169 | 0 | strvec_push(&cmd.args, "-n"); |
1170 | 0 | if ((flags & AMEND_MSG)) |
1171 | 0 | strvec_push(&cmd.args, "--amend"); |
1172 | 0 | if (opts->gpg_sign) |
1173 | 0 | strvec_pushf(&cmd.args, "-S%s", opts->gpg_sign); |
1174 | 0 | else |
1175 | 0 | strvec_push(&cmd.args, "--no-gpg-sign"); |
1176 | 0 | if (defmsg) |
1177 | 0 | strvec_pushl(&cmd.args, "-F", defmsg, NULL); |
1178 | 0 | else if (!(flags & EDIT_MSG)) |
1179 | 0 | strvec_pushl(&cmd.args, "-C", "HEAD", NULL); |
1180 | 0 | if ((flags & CLEANUP_MSG)) |
1181 | 0 | strvec_push(&cmd.args, "--cleanup=strip"); |
1182 | 0 | if ((flags & VERBATIM_MSG)) |
1183 | 0 | strvec_push(&cmd.args, "--cleanup=verbatim"); |
1184 | 0 | if ((flags & EDIT_MSG)) |
1185 | 0 | strvec_push(&cmd.args, "-e"); |
1186 | 0 | else if (!(flags & CLEANUP_MSG) && |
1187 | 0 | !opts->signoff && !opts->record_origin && |
1188 | 0 | !opts->explicit_cleanup) |
1189 | 0 | strvec_push(&cmd.args, "--cleanup=verbatim"); |
1190 | |
|
1191 | 0 | if ((flags & ALLOW_EMPTY)) |
1192 | 0 | strvec_push(&cmd.args, "--allow-empty"); |
1193 | |
|
1194 | 0 | if (!(flags & EDIT_MSG)) |
1195 | 0 | strvec_push(&cmd.args, "--allow-empty-message"); |
1196 | |
|
1197 | 0 | if (is_rebase_i(opts) && !(flags & EDIT_MSG)) |
1198 | 0 | return run_command_silent_on_success(&cmd); |
1199 | 0 | else |
1200 | 0 | return run_command(&cmd); |
1201 | 0 | } |
1202 | | |
1203 | | static int rest_is_empty(const struct strbuf *sb, int start) |
1204 | 0 | { |
1205 | 0 | int i, eol; |
1206 | 0 | const char *nl; |
1207 | | |
1208 | | /* Check if the rest is just whitespace and Signed-off-by's. */ |
1209 | 0 | for (i = start; i < sb->len; i++) { |
1210 | 0 | nl = memchr(sb->buf + i, '\n', sb->len - i); |
1211 | 0 | if (nl) |
1212 | 0 | eol = nl - sb->buf; |
1213 | 0 | else |
1214 | 0 | eol = sb->len; |
1215 | |
|
1216 | 0 | if (strlen(sign_off_header) <= eol - i && |
1217 | 0 | starts_with(sb->buf + i, sign_off_header)) { |
1218 | 0 | i = eol; |
1219 | 0 | continue; |
1220 | 0 | } |
1221 | 0 | while (i < eol) |
1222 | 0 | if (!isspace(sb->buf[i++])) |
1223 | 0 | return 0; |
1224 | 0 | } |
1225 | | |
1226 | 0 | return 1; |
1227 | 0 | } |
1228 | | |
1229 | | void cleanup_message(struct strbuf *msgbuf, |
1230 | | enum commit_msg_cleanup_mode cleanup_mode, int verbose) |
1231 | 0 | { |
1232 | 0 | if (verbose || /* Truncate the message just before the diff, if any. */ |
1233 | 0 | cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) |
1234 | 0 | strbuf_setlen(msgbuf, wt_status_locate_end(msgbuf->buf, msgbuf->len)); |
1235 | 0 | if (cleanup_mode != COMMIT_MSG_CLEANUP_NONE) |
1236 | 0 | strbuf_stripspace(msgbuf, |
1237 | 0 | cleanup_mode == COMMIT_MSG_CLEANUP_ALL ? comment_line_str : NULL); |
1238 | 0 | } |
1239 | | |
1240 | | /* |
1241 | | * Find out if the message in the strbuf contains only whitespace and |
1242 | | * Signed-off-by lines. |
1243 | | */ |
1244 | | int message_is_empty(const struct strbuf *sb, |
1245 | | enum commit_msg_cleanup_mode cleanup_mode) |
1246 | 0 | { |
1247 | 0 | if (cleanup_mode == COMMIT_MSG_CLEANUP_NONE && sb->len) |
1248 | 0 | return 0; |
1249 | 0 | return rest_is_empty(sb, 0); |
1250 | 0 | } |
1251 | | |
1252 | | /* |
1253 | | * See if the user edited the message in the editor or left what |
1254 | | * was in the template intact |
1255 | | */ |
1256 | | int template_untouched(const struct strbuf *sb, const char *template_file, |
1257 | | enum commit_msg_cleanup_mode cleanup_mode) |
1258 | 0 | { |
1259 | 0 | struct strbuf tmpl = STRBUF_INIT; |
1260 | 0 | const char *start; |
1261 | |
|
1262 | 0 | if (cleanup_mode == COMMIT_MSG_CLEANUP_NONE && sb->len) |
1263 | 0 | return 0; |
1264 | | |
1265 | 0 | if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0) |
1266 | 0 | return 0; |
1267 | | |
1268 | 0 | strbuf_stripspace(&tmpl, |
1269 | 0 | cleanup_mode == COMMIT_MSG_CLEANUP_ALL ? comment_line_str : NULL); |
1270 | 0 | if (!skip_prefix(sb->buf, tmpl.buf, &start)) |
1271 | 0 | start = sb->buf; |
1272 | 0 | strbuf_release(&tmpl); |
1273 | 0 | return rest_is_empty(sb, start - sb->buf); |
1274 | 0 | } |
1275 | | |
1276 | | int update_head_with_reflog(const struct commit *old_head, |
1277 | | const struct object_id *new_head, |
1278 | | const char *action, const struct strbuf *msg, |
1279 | | struct strbuf *err) |
1280 | 0 | { |
1281 | 0 | struct ref_transaction *transaction; |
1282 | 0 | struct strbuf sb = STRBUF_INIT; |
1283 | 0 | const char *nl; |
1284 | 0 | int ret = 0; |
1285 | |
|
1286 | 0 | if (action) { |
1287 | 0 | strbuf_addstr(&sb, action); |
1288 | 0 | strbuf_addstr(&sb, ": "); |
1289 | 0 | } |
1290 | |
|
1291 | 0 | nl = strchr(msg->buf, '\n'); |
1292 | 0 | if (nl) { |
1293 | 0 | strbuf_add(&sb, msg->buf, nl + 1 - msg->buf); |
1294 | 0 | } else { |
1295 | 0 | strbuf_addbuf(&sb, msg); |
1296 | 0 | strbuf_addch(&sb, '\n'); |
1297 | 0 | } |
1298 | |
|
1299 | 0 | transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), |
1300 | 0 | err); |
1301 | 0 | if (!transaction || |
1302 | 0 | ref_transaction_update(transaction, "HEAD", new_head, |
1303 | 0 | old_head ? &old_head->object.oid : null_oid(), |
1304 | 0 | NULL, NULL, 0, sb.buf, err) || |
1305 | 0 | ref_transaction_commit(transaction, err)) { |
1306 | 0 | ret = -1; |
1307 | 0 | } |
1308 | 0 | ref_transaction_free(transaction); |
1309 | 0 | strbuf_release(&sb); |
1310 | |
|
1311 | 0 | return ret; |
1312 | 0 | } |
1313 | | |
1314 | | static int run_rewrite_hook(const struct object_id *oldoid, |
1315 | | const struct object_id *newoid) |
1316 | 0 | { |
1317 | 0 | struct child_process proc = CHILD_PROCESS_INIT; |
1318 | 0 | int code; |
1319 | 0 | struct strbuf sb = STRBUF_INIT; |
1320 | 0 | const char *hook_path = find_hook(the_repository, "post-rewrite"); |
1321 | |
|
1322 | 0 | if (!hook_path) |
1323 | 0 | return 0; |
1324 | | |
1325 | 0 | strvec_pushl(&proc.args, hook_path, "amend", NULL); |
1326 | 0 | proc.in = -1; |
1327 | 0 | proc.stdout_to_stderr = 1; |
1328 | 0 | proc.trace2_hook_name = "post-rewrite"; |
1329 | |
|
1330 | 0 | code = start_command(&proc); |
1331 | 0 | if (code) |
1332 | 0 | return code; |
1333 | 0 | strbuf_addf(&sb, "%s %s\n", oid_to_hex(oldoid), oid_to_hex(newoid)); |
1334 | 0 | sigchain_push(SIGPIPE, SIG_IGN); |
1335 | 0 | write_in_full(proc.in, sb.buf, sb.len); |
1336 | 0 | close(proc.in); |
1337 | 0 | strbuf_release(&sb); |
1338 | 0 | sigchain_pop(SIGPIPE); |
1339 | 0 | return finish_command(&proc); |
1340 | 0 | } |
1341 | | |
1342 | | void commit_post_rewrite(struct repository *r, |
1343 | | const struct commit *old_head, |
1344 | | const struct object_id *new_head) |
1345 | 0 | { |
1346 | 0 | struct notes_rewrite_cfg *cfg; |
1347 | |
|
1348 | 0 | cfg = init_copy_notes_for_rewrite("amend"); |
1349 | 0 | if (cfg) { |
1350 | | /* we are amending, so old_head is not NULL */ |
1351 | 0 | copy_note_for_rewrite(cfg, &old_head->object.oid, new_head); |
1352 | 0 | finish_copy_notes_for_rewrite(r, cfg, "Notes added by 'git commit --amend'"); |
1353 | 0 | } |
1354 | 0 | run_rewrite_hook(&old_head->object.oid, new_head); |
1355 | 0 | } |
1356 | | |
1357 | | static int run_prepare_commit_msg_hook(struct repository *r, |
1358 | | struct strbuf *msg, |
1359 | | const char *commit) |
1360 | 0 | { |
1361 | 0 | int ret = 0; |
1362 | 0 | const char *name, *arg1 = NULL, *arg2 = NULL; |
1363 | |
|
1364 | 0 | name = git_path_commit_editmsg(); |
1365 | 0 | if (write_message(msg->buf, msg->len, name, 0)) |
1366 | 0 | return -1; |
1367 | | |
1368 | 0 | if (commit) { |
1369 | 0 | arg1 = "commit"; |
1370 | 0 | arg2 = commit; |
1371 | 0 | } else { |
1372 | 0 | arg1 = "message"; |
1373 | 0 | } |
1374 | 0 | if (run_commit_hook(0, r->index_file, NULL, "prepare-commit-msg", name, |
1375 | 0 | arg1, arg2, NULL)) |
1376 | 0 | ret = error(_("'prepare-commit-msg' hook failed")); |
1377 | |
|
1378 | 0 | return ret; |
1379 | 0 | } |
1380 | | |
1381 | | static const char implicit_ident_advice_noconfig[] = |
1382 | | N_("Your name and email address were configured automatically based\n" |
1383 | | "on your username and hostname. Please check that they are accurate.\n" |
1384 | | "You can suppress this message by setting them explicitly. Run the\n" |
1385 | | "following command and follow the instructions in your editor to edit\n" |
1386 | | "your configuration file:\n" |
1387 | | "\n" |
1388 | | " git config --global --edit\n" |
1389 | | "\n" |
1390 | | "After doing this, you may fix the identity used for this commit with:\n" |
1391 | | "\n" |
1392 | | " git commit --amend --reset-author\n"); |
1393 | | |
1394 | | static const char implicit_ident_advice_config[] = |
1395 | | N_("Your name and email address were configured automatically based\n" |
1396 | | "on your username and hostname. Please check that they are accurate.\n" |
1397 | | "You can suppress this message by setting them explicitly:\n" |
1398 | | "\n" |
1399 | | " git config --global user.name \"Your Name\"\n" |
1400 | | " git config --global user.email you@example.com\n" |
1401 | | "\n" |
1402 | | "After doing this, you may fix the identity used for this commit with:\n" |
1403 | | "\n" |
1404 | | " git commit --amend --reset-author\n"); |
1405 | | |
1406 | | static const char *implicit_ident_advice(void) |
1407 | 0 | { |
1408 | 0 | char *user_config = interpolate_path("~/.gitconfig", 0); |
1409 | 0 | char *xdg_config = xdg_config_home("config"); |
1410 | 0 | int config_exists = file_exists(user_config) || file_exists(xdg_config); |
1411 | |
|
1412 | 0 | free(user_config); |
1413 | 0 | free(xdg_config); |
1414 | |
|
1415 | 0 | if (config_exists) |
1416 | 0 | return _(implicit_ident_advice_config); |
1417 | 0 | else |
1418 | 0 | return _(implicit_ident_advice_noconfig); |
1419 | |
|
1420 | 0 | } |
1421 | | |
1422 | | void print_commit_summary(struct repository *r, |
1423 | | const char *prefix, |
1424 | | const struct object_id *oid, |
1425 | | unsigned int flags) |
1426 | 0 | { |
1427 | 0 | struct rev_info rev; |
1428 | 0 | struct commit *commit; |
1429 | 0 | struct strbuf format = STRBUF_INIT; |
1430 | 0 | const char *head; |
1431 | 0 | struct pretty_print_context pctx = {0}; |
1432 | 0 | struct strbuf author_ident = STRBUF_INIT; |
1433 | 0 | struct strbuf committer_ident = STRBUF_INIT; |
1434 | 0 | struct ref_store *refs; |
1435 | |
|
1436 | 0 | commit = lookup_commit(r, oid); |
1437 | 0 | if (!commit) |
1438 | 0 | die(_("couldn't look up newly created commit")); |
1439 | 0 | if (repo_parse_commit(r, commit)) |
1440 | 0 | die(_("could not parse newly created commit")); |
1441 | | |
1442 | 0 | strbuf_addstr(&format, "format:%h] %s"); |
1443 | |
|
1444 | 0 | repo_format_commit_message(r, commit, "%an <%ae>", &author_ident, |
1445 | 0 | &pctx); |
1446 | 0 | repo_format_commit_message(r, commit, "%cn <%ce>", &committer_ident, |
1447 | 0 | &pctx); |
1448 | 0 | if (strbuf_cmp(&author_ident, &committer_ident)) { |
1449 | 0 | strbuf_addstr(&format, "\n Author: "); |
1450 | 0 | strbuf_addbuf_percentquote(&format, &author_ident); |
1451 | 0 | } |
1452 | 0 | if (flags & SUMMARY_SHOW_AUTHOR_DATE) { |
1453 | 0 | struct strbuf date = STRBUF_INIT; |
1454 | |
|
1455 | 0 | repo_format_commit_message(r, commit, "%ad", &date, &pctx); |
1456 | 0 | strbuf_addstr(&format, "\n Date: "); |
1457 | 0 | strbuf_addbuf_percentquote(&format, &date); |
1458 | 0 | strbuf_release(&date); |
1459 | 0 | } |
1460 | 0 | if (!committer_ident_sufficiently_given()) { |
1461 | 0 | strbuf_addstr(&format, "\n Committer: "); |
1462 | 0 | strbuf_addbuf_percentquote(&format, &committer_ident); |
1463 | 0 | if (advice_enabled(ADVICE_IMPLICIT_IDENTITY)) { |
1464 | 0 | strbuf_addch(&format, '\n'); |
1465 | 0 | strbuf_addstr(&format, implicit_ident_advice()); |
1466 | 0 | } |
1467 | 0 | } |
1468 | 0 | strbuf_release(&author_ident); |
1469 | 0 | strbuf_release(&committer_ident); |
1470 | |
|
1471 | 0 | repo_init_revisions(r, &rev, prefix); |
1472 | 0 | setup_revisions(0, NULL, &rev, NULL); |
1473 | |
|
1474 | 0 | rev.diff = 1; |
1475 | 0 | rev.diffopt.output_format = |
1476 | 0 | DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY; |
1477 | |
|
1478 | 0 | rev.verbose_header = 1; |
1479 | 0 | rev.show_root_diff = 1; |
1480 | 0 | get_commit_format(format.buf, &rev); |
1481 | 0 | rev.always_show_header = 0; |
1482 | 0 | rev.diffopt.detect_rename = DIFF_DETECT_RENAME; |
1483 | 0 | diff_setup_done(&rev.diffopt); |
1484 | |
|
1485 | 0 | refs = get_main_ref_store(r); |
1486 | 0 | head = refs_resolve_ref_unsafe(refs, "HEAD", 0, NULL, NULL); |
1487 | 0 | if (!head) |
1488 | 0 | die(_("unable to resolve HEAD after creating commit")); |
1489 | 0 | if (!strcmp(head, "HEAD")) |
1490 | 0 | head = _("detached HEAD"); |
1491 | 0 | else |
1492 | 0 | skip_prefix(head, "refs/heads/", &head); |
1493 | 0 | printf("[%s%s ", head, (flags & SUMMARY_INITIAL_COMMIT) ? |
1494 | 0 | _(" (root-commit)") : ""); |
1495 | |
|
1496 | 0 | if (!log_tree_commit(&rev, commit)) { |
1497 | 0 | rev.always_show_header = 1; |
1498 | 0 | rev.use_terminator = 1; |
1499 | 0 | log_tree_commit(&rev, commit); |
1500 | 0 | } |
1501 | |
|
1502 | 0 | release_revisions(&rev); |
1503 | 0 | strbuf_release(&format); |
1504 | 0 | } |
1505 | | |
1506 | | static int parse_head(struct repository *r, struct commit **head) |
1507 | 0 | { |
1508 | 0 | struct commit *current_head; |
1509 | 0 | struct object_id oid; |
1510 | |
|
1511 | 0 | if (repo_get_oid(r, "HEAD", &oid)) { |
1512 | 0 | current_head = NULL; |
1513 | 0 | } else { |
1514 | 0 | current_head = lookup_commit_reference(r, &oid); |
1515 | 0 | if (!current_head) |
1516 | 0 | return error(_("could not parse HEAD")); |
1517 | 0 | if (!oideq(&oid, ¤t_head->object.oid)) { |
1518 | 0 | warning(_("HEAD %s is not a commit!"), |
1519 | 0 | oid_to_hex(&oid)); |
1520 | 0 | } |
1521 | 0 | if (repo_parse_commit(r, current_head)) |
1522 | 0 | return error(_("could not parse HEAD commit")); |
1523 | 0 | } |
1524 | 0 | *head = current_head; |
1525 | |
|
1526 | 0 | return 0; |
1527 | 0 | } |
1528 | | |
1529 | | /* |
1530 | | * Try to commit without forking 'git commit'. In some cases we need |
1531 | | * to run 'git commit' to display an error message |
1532 | | * |
1533 | | * Returns: |
1534 | | * -1 - error unable to commit |
1535 | | * 0 - success |
1536 | | * 1 - run 'git commit' |
1537 | | */ |
1538 | | static int try_to_commit(struct repository *r, |
1539 | | struct strbuf *msg, const char *author, |
1540 | | struct replay_opts *opts, unsigned int flags, |
1541 | | struct object_id *oid) |
1542 | 0 | { |
1543 | 0 | struct replay_ctx *ctx = opts->ctx; |
1544 | 0 | struct object_id tree; |
1545 | 0 | struct commit *current_head = NULL; |
1546 | 0 | struct commit_list *parents = NULL; |
1547 | 0 | struct commit_extra_header *extra = NULL; |
1548 | 0 | struct strbuf err = STRBUF_INIT; |
1549 | 0 | struct strbuf commit_msg = STRBUF_INIT; |
1550 | 0 | char *amend_author = NULL; |
1551 | 0 | const char *committer = NULL; |
1552 | 0 | const char *hook_commit = NULL; |
1553 | 0 | enum commit_msg_cleanup_mode cleanup; |
1554 | 0 | int res = 0; |
1555 | |
|
1556 | 0 | if ((flags & CLEANUP_MSG) && (flags & VERBATIM_MSG)) |
1557 | 0 | BUG("CLEANUP_MSG and VERBATIM_MSG are mutually exclusive"); |
1558 | | |
1559 | 0 | if (parse_head(r, ¤t_head)) |
1560 | 0 | return -1; |
1561 | | |
1562 | 0 | if (flags & AMEND_MSG) { |
1563 | 0 | const char *exclude_gpgsig[] = { "gpgsig", "gpgsig-sha256", NULL }; |
1564 | 0 | const char *out_enc = get_commit_output_encoding(); |
1565 | 0 | const char *message = repo_logmsg_reencode(r, current_head, |
1566 | 0 | NULL, out_enc); |
1567 | |
|
1568 | 0 | if (!msg) { |
1569 | 0 | const char *orig_message = NULL; |
1570 | |
|
1571 | 0 | find_commit_subject(message, &orig_message); |
1572 | 0 | msg = &commit_msg; |
1573 | 0 | strbuf_addstr(msg, orig_message); |
1574 | 0 | hook_commit = "HEAD"; |
1575 | 0 | } |
1576 | 0 | author = amend_author = get_author(message); |
1577 | 0 | repo_unuse_commit_buffer(r, current_head, |
1578 | 0 | message); |
1579 | 0 | if (!author) { |
1580 | 0 | res = error(_("unable to parse commit author")); |
1581 | 0 | goto out; |
1582 | 0 | } |
1583 | 0 | parents = copy_commit_list(current_head->parents); |
1584 | 0 | extra = read_commit_extra_headers(current_head, exclude_gpgsig); |
1585 | 0 | } else if (current_head && |
1586 | 0 | (!(flags & CREATE_ROOT_COMMIT) || (flags & AMEND_MSG))) { |
1587 | 0 | commit_list_insert(current_head, &parents); |
1588 | 0 | } |
1589 | | |
1590 | 0 | if (write_index_as_tree(&tree, r->index, r->index_file, 0, NULL)) { |
1591 | 0 | res = error(_("git write-tree failed to write a tree")); |
1592 | 0 | goto out; |
1593 | 0 | } |
1594 | | |
1595 | 0 | if (!(flags & ALLOW_EMPTY)) { |
1596 | 0 | struct commit *first_parent = current_head; |
1597 | |
|
1598 | 0 | if (flags & AMEND_MSG) { |
1599 | 0 | if (current_head->parents) { |
1600 | 0 | first_parent = current_head->parents->item; |
1601 | 0 | if (repo_parse_commit(r, first_parent)) { |
1602 | 0 | res = error(_("could not parse HEAD commit")); |
1603 | 0 | goto out; |
1604 | 0 | } |
1605 | 0 | } else { |
1606 | 0 | first_parent = NULL; |
1607 | 0 | } |
1608 | 0 | } |
1609 | 0 | if (oideq(first_parent |
1610 | 0 | ? get_commit_tree_oid(first_parent) |
1611 | 0 | : the_hash_algo->empty_tree, |
1612 | 0 | &tree)) { |
1613 | 0 | res = 1; /* run 'git commit' to display error message */ |
1614 | 0 | goto out; |
1615 | 0 | } |
1616 | 0 | } |
1617 | | |
1618 | 0 | if (hook_exists(r, "prepare-commit-msg")) { |
1619 | 0 | res = run_prepare_commit_msg_hook(r, msg, hook_commit); |
1620 | 0 | if (res) |
1621 | 0 | goto out; |
1622 | 0 | if (strbuf_read_file(&commit_msg, git_path_commit_editmsg(), |
1623 | 0 | 2048) < 0) { |
1624 | 0 | res = error_errno(_("unable to read commit message " |
1625 | 0 | "from '%s'"), |
1626 | 0 | git_path_commit_editmsg()); |
1627 | 0 | goto out; |
1628 | 0 | } |
1629 | 0 | msg = &commit_msg; |
1630 | 0 | } |
1631 | | |
1632 | 0 | if (flags & CLEANUP_MSG) |
1633 | 0 | cleanup = COMMIT_MSG_CLEANUP_ALL; |
1634 | 0 | else if (flags & VERBATIM_MSG) |
1635 | 0 | cleanup = COMMIT_MSG_CLEANUP_NONE; |
1636 | 0 | else if ((opts->signoff || opts->record_origin) && |
1637 | 0 | !opts->explicit_cleanup) |
1638 | 0 | cleanup = COMMIT_MSG_CLEANUP_SPACE; |
1639 | 0 | else |
1640 | 0 | cleanup = opts->default_msg_cleanup; |
1641 | |
|
1642 | 0 | if (cleanup != COMMIT_MSG_CLEANUP_NONE) |
1643 | 0 | strbuf_stripspace(msg, |
1644 | 0 | cleanup == COMMIT_MSG_CLEANUP_ALL ? comment_line_str : NULL); |
1645 | 0 | if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) { |
1646 | 0 | res = 1; /* run 'git commit' to display error message */ |
1647 | 0 | goto out; |
1648 | 0 | } |
1649 | | |
1650 | 0 | if (opts->committer_date_is_author_date) { |
1651 | 0 | struct ident_split id; |
1652 | 0 | struct strbuf date = STRBUF_INIT; |
1653 | |
|
1654 | 0 | if (!opts->ignore_date) { |
1655 | 0 | if (split_ident_line(&id, author, (int)strlen(author)) < 0) { |
1656 | 0 | res = error(_("invalid author identity '%s'"), |
1657 | 0 | author); |
1658 | 0 | goto out; |
1659 | 0 | } |
1660 | 0 | if (!id.date_begin) { |
1661 | 0 | res = error(_( |
1662 | 0 | "corrupt author: missing date information")); |
1663 | 0 | goto out; |
1664 | 0 | } |
1665 | 0 | strbuf_addf(&date, "@%.*s %.*s", |
1666 | 0 | (int)(id.date_end - id.date_begin), |
1667 | 0 | id.date_begin, |
1668 | 0 | (int)(id.tz_end - id.tz_begin), |
1669 | 0 | id.tz_begin); |
1670 | 0 | } else { |
1671 | 0 | reset_ident_date(); |
1672 | 0 | } |
1673 | 0 | committer = fmt_ident(getenv("GIT_COMMITTER_NAME"), |
1674 | 0 | getenv("GIT_COMMITTER_EMAIL"), |
1675 | 0 | WANT_COMMITTER_IDENT, |
1676 | 0 | opts->ignore_date ? NULL : date.buf, |
1677 | 0 | IDENT_STRICT); |
1678 | 0 | strbuf_release(&date); |
1679 | 0 | } else { |
1680 | 0 | reset_ident_date(); |
1681 | 0 | } |
1682 | | |
1683 | 0 | if (opts->ignore_date) { |
1684 | 0 | struct ident_split id; |
1685 | 0 | char *name, *email; |
1686 | |
|
1687 | 0 | if (split_ident_line(&id, author, strlen(author)) < 0) { |
1688 | 0 | error(_("invalid author identity '%s'"), author); |
1689 | 0 | goto out; |
1690 | 0 | } |
1691 | 0 | name = xmemdupz(id.name_begin, id.name_end - id.name_begin); |
1692 | 0 | email = xmemdupz(id.mail_begin, id.mail_end - id.mail_begin); |
1693 | 0 | author = fmt_ident(name, email, WANT_AUTHOR_IDENT, NULL, |
1694 | 0 | IDENT_STRICT); |
1695 | 0 | free(name); |
1696 | 0 | free(email); |
1697 | 0 | } |
1698 | | |
1699 | 0 | if (commit_tree_extended(msg->buf, msg->len, &tree, parents, oid, |
1700 | 0 | author, committer, opts->gpg_sign, extra)) { |
1701 | 0 | res = error(_("failed to write commit object")); |
1702 | 0 | goto out; |
1703 | 0 | } |
1704 | | |
1705 | 0 | if (update_head_with_reflog(current_head, oid, ctx->reflog_message, |
1706 | 0 | msg, &err)) { |
1707 | 0 | res = error("%s", err.buf); |
1708 | 0 | goto out; |
1709 | 0 | } |
1710 | | |
1711 | 0 | run_commit_hook(0, r->index_file, NULL, "post-commit", NULL); |
1712 | 0 | if (flags & AMEND_MSG) |
1713 | 0 | commit_post_rewrite(r, current_head, oid); |
1714 | |
|
1715 | 0 | out: |
1716 | 0 | free_commit_extra_headers(extra); |
1717 | 0 | free_commit_list(parents); |
1718 | 0 | strbuf_release(&err); |
1719 | 0 | strbuf_release(&commit_msg); |
1720 | 0 | free(amend_author); |
1721 | |
|
1722 | 0 | return res; |
1723 | 0 | } |
1724 | | |
1725 | | static int write_rebase_head(struct object_id *oid) |
1726 | 0 | { |
1727 | 0 | if (refs_update_ref(get_main_ref_store(the_repository), "rebase", "REBASE_HEAD", oid, |
1728 | 0 | NULL, REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) |
1729 | 0 | return error(_("could not update %s"), "REBASE_HEAD"); |
1730 | | |
1731 | 0 | return 0; |
1732 | 0 | } |
1733 | | |
1734 | | static int do_commit(struct repository *r, |
1735 | | const char *msg_file, const char *author, |
1736 | | struct replay_opts *opts, unsigned int flags, |
1737 | | struct object_id *oid) |
1738 | 0 | { |
1739 | 0 | int res = 1; |
1740 | |
|
1741 | 0 | if (!(flags & EDIT_MSG) && !(flags & VERIFY_MSG)) { |
1742 | 0 | struct object_id oid; |
1743 | 0 | struct strbuf sb = STRBUF_INIT; |
1744 | |
|
1745 | 0 | if (msg_file && strbuf_read_file(&sb, msg_file, 2048) < 0) |
1746 | 0 | return error_errno(_("unable to read commit message " |
1747 | 0 | "from '%s'"), |
1748 | 0 | msg_file); |
1749 | | |
1750 | 0 | res = try_to_commit(r, msg_file ? &sb : NULL, |
1751 | 0 | author, opts, flags, &oid); |
1752 | 0 | strbuf_release(&sb); |
1753 | 0 | if (!res) { |
1754 | 0 | refs_delete_ref(get_main_ref_store(r), "", |
1755 | 0 | "CHERRY_PICK_HEAD", NULL, REF_NO_DEREF); |
1756 | 0 | unlink(git_path_merge_msg(r)); |
1757 | 0 | if (!is_rebase_i(opts)) |
1758 | 0 | print_commit_summary(r, NULL, &oid, |
1759 | 0 | SUMMARY_SHOW_AUTHOR_DATE); |
1760 | 0 | return res; |
1761 | 0 | } |
1762 | 0 | } |
1763 | 0 | if (res == 1) { |
1764 | 0 | if (is_rebase_i(opts) && oid) |
1765 | 0 | if (write_rebase_head(oid)) |
1766 | 0 | return -1; |
1767 | 0 | return run_git_commit(msg_file, opts, flags); |
1768 | 0 | } |
1769 | | |
1770 | 0 | return res; |
1771 | 0 | } |
1772 | | |
1773 | | static int is_original_commit_empty(struct commit *commit) |
1774 | 0 | { |
1775 | 0 | const struct object_id *ptree_oid; |
1776 | |
|
1777 | 0 | if (repo_parse_commit(the_repository, commit)) |
1778 | 0 | return error(_("could not parse commit %s"), |
1779 | 0 | oid_to_hex(&commit->object.oid)); |
1780 | 0 | if (commit->parents) { |
1781 | 0 | struct commit *parent = commit->parents->item; |
1782 | 0 | if (repo_parse_commit(the_repository, parent)) |
1783 | 0 | return error(_("could not parse parent commit %s"), |
1784 | 0 | oid_to_hex(&parent->object.oid)); |
1785 | 0 | ptree_oid = get_commit_tree_oid(parent); |
1786 | 0 | } else { |
1787 | 0 | ptree_oid = the_hash_algo->empty_tree; /* commit is root */ |
1788 | 0 | } |
1789 | | |
1790 | 0 | return oideq(ptree_oid, get_commit_tree_oid(commit)); |
1791 | 0 | } |
1792 | | |
1793 | | /* |
1794 | | * Should empty commits be allowed? Return status: |
1795 | | * <0: Error in is_index_unchanged(r) or is_original_commit_empty(commit) |
1796 | | * 0: Halt on empty commit |
1797 | | * 1: Allow empty commit |
1798 | | * 2: Drop empty commit |
1799 | | */ |
1800 | | static int allow_empty(struct repository *r, |
1801 | | struct replay_opts *opts, |
1802 | | struct commit *commit) |
1803 | 0 | { |
1804 | 0 | int index_unchanged, originally_empty; |
1805 | | |
1806 | | /* |
1807 | | * For a commit that is initially empty, allow_empty determines if it |
1808 | | * should be kept or not |
1809 | | * |
1810 | | * For a commit that becomes empty, keep_redundant_commits and |
1811 | | * drop_redundant_commits determine whether the commit should be kept or |
1812 | | * dropped. If neither is specified, halt. |
1813 | | */ |
1814 | 0 | index_unchanged = is_index_unchanged(r); |
1815 | 0 | if (index_unchanged < 0) |
1816 | 0 | return index_unchanged; |
1817 | 0 | if (!index_unchanged) |
1818 | 0 | return 0; /* we do not have to say --allow-empty */ |
1819 | | |
1820 | 0 | originally_empty = is_original_commit_empty(commit); |
1821 | 0 | if (originally_empty < 0) |
1822 | 0 | return originally_empty; |
1823 | 0 | if (originally_empty) |
1824 | 0 | return opts->allow_empty; |
1825 | 0 | else if (opts->keep_redundant_commits) |
1826 | 0 | return 1; |
1827 | 0 | else if (opts->drop_redundant_commits) |
1828 | 0 | return 2; |
1829 | 0 | else |
1830 | 0 | return 0; |
1831 | 0 | } |
1832 | | |
1833 | | static struct { |
1834 | | char c; |
1835 | | const char *str; |
1836 | | } todo_command_info[] = { |
1837 | | [TODO_PICK] = { 'p', "pick" }, |
1838 | | [TODO_REVERT] = { 0, "revert" }, |
1839 | | [TODO_EDIT] = { 'e', "edit" }, |
1840 | | [TODO_REWORD] = { 'r', "reword" }, |
1841 | | [TODO_FIXUP] = { 'f', "fixup" }, |
1842 | | [TODO_SQUASH] = { 's', "squash" }, |
1843 | | [TODO_EXEC] = { 'x', "exec" }, |
1844 | | [TODO_BREAK] = { 'b', "break" }, |
1845 | | [TODO_LABEL] = { 'l', "label" }, |
1846 | | [TODO_RESET] = { 't', "reset" }, |
1847 | | [TODO_MERGE] = { 'm', "merge" }, |
1848 | | [TODO_UPDATE_REF] = { 'u', "update-ref" }, |
1849 | | [TODO_NOOP] = { 0, "noop" }, |
1850 | | [TODO_DROP] = { 'd', "drop" }, |
1851 | | [TODO_COMMENT] = { 0, NULL }, |
1852 | | }; |
1853 | | |
1854 | | static const char *command_to_string(const enum todo_command command) |
1855 | 0 | { |
1856 | 0 | if (command < TODO_COMMENT) |
1857 | 0 | return todo_command_info[command].str; |
1858 | 0 | if (command == TODO_COMMENT) |
1859 | 0 | return comment_line_str; |
1860 | 0 | die(_("unknown command: %d"), command); |
1861 | 0 | } |
1862 | | |
1863 | | static char command_to_char(const enum todo_command command) |
1864 | 0 | { |
1865 | 0 | if (command < TODO_COMMENT) |
1866 | 0 | return todo_command_info[command].c; |
1867 | 0 | return 0; |
1868 | 0 | } |
1869 | | |
1870 | | static int is_noop(const enum todo_command command) |
1871 | 0 | { |
1872 | 0 | return TODO_NOOP <= command; |
1873 | 0 | } |
1874 | | |
1875 | | static int is_fixup(enum todo_command command) |
1876 | 0 | { |
1877 | 0 | return command == TODO_FIXUP || command == TODO_SQUASH; |
1878 | 0 | } |
1879 | | |
1880 | | /* Does this command create a (non-merge) commit? */ |
1881 | | static int is_pick_or_similar(enum todo_command command) |
1882 | 0 | { |
1883 | 0 | switch (command) { |
1884 | 0 | case TODO_PICK: |
1885 | 0 | case TODO_REVERT: |
1886 | 0 | case TODO_EDIT: |
1887 | 0 | case TODO_REWORD: |
1888 | 0 | case TODO_FIXUP: |
1889 | 0 | case TODO_SQUASH: |
1890 | 0 | return 1; |
1891 | 0 | default: |
1892 | 0 | return 0; |
1893 | 0 | } |
1894 | 0 | } |
1895 | | |
1896 | | enum todo_item_flags { |
1897 | | TODO_EDIT_MERGE_MSG = (1 << 0), |
1898 | | TODO_REPLACE_FIXUP_MSG = (1 << 1), |
1899 | | TODO_EDIT_FIXUP_MSG = (1 << 2), |
1900 | | }; |
1901 | | |
1902 | | static const char first_commit_msg_str[] = N_("This is the 1st commit message:"); |
1903 | | static const char nth_commit_msg_fmt[] = N_("This is the commit message #%d:"); |
1904 | | static const char skip_first_commit_msg_str[] = N_("The 1st commit message will be skipped:"); |
1905 | | static const char skip_nth_commit_msg_fmt[] = N_("The commit message #%d will be skipped:"); |
1906 | | static const char combined_commit_msg_fmt[] = N_("This is a combination of %d commits."); |
1907 | | |
1908 | | static int is_fixup_flag(enum todo_command command, unsigned flag) |
1909 | 0 | { |
1910 | 0 | return command == TODO_FIXUP && ((flag & TODO_REPLACE_FIXUP_MSG) || |
1911 | 0 | (flag & TODO_EDIT_FIXUP_MSG)); |
1912 | 0 | } |
1913 | | |
1914 | | /* |
1915 | | * Wrapper around strbuf_add_commented_lines() which avoids double |
1916 | | * commenting commit subjects. |
1917 | | */ |
1918 | | static void add_commented_lines(struct strbuf *buf, const void *str, size_t len) |
1919 | 0 | { |
1920 | 0 | const char *s = str; |
1921 | 0 | while (starts_with_mem(s, len, comment_line_str)) { |
1922 | 0 | size_t count; |
1923 | 0 | const char *n = memchr(s, '\n', len); |
1924 | 0 | if (!n) |
1925 | 0 | count = len; |
1926 | 0 | else |
1927 | 0 | count = n - s + 1; |
1928 | 0 | strbuf_add(buf, s, count); |
1929 | 0 | s += count; |
1930 | 0 | len -= count; |
1931 | 0 | } |
1932 | 0 | strbuf_add_commented_lines(buf, s, len, comment_line_str); |
1933 | 0 | } |
1934 | | |
1935 | | /* Does the current fixup chain contain a squash command? */ |
1936 | | static int seen_squash(struct replay_ctx *ctx) |
1937 | 0 | { |
1938 | 0 | return starts_with(ctx->current_fixups.buf, "squash") || |
1939 | 0 | strstr(ctx->current_fixups.buf, "\nsquash"); |
1940 | 0 | } |
1941 | | |
1942 | | static void update_comment_bufs(struct strbuf *buf1, struct strbuf *buf2, int n) |
1943 | 0 | { |
1944 | 0 | strbuf_setlen(buf1, 2); |
1945 | 0 | strbuf_addf(buf1, _(nth_commit_msg_fmt), n); |
1946 | 0 | strbuf_addch(buf1, '\n'); |
1947 | 0 | strbuf_setlen(buf2, 2); |
1948 | 0 | strbuf_addf(buf2, _(skip_nth_commit_msg_fmt), n); |
1949 | 0 | strbuf_addch(buf2, '\n'); |
1950 | 0 | } |
1951 | | |
1952 | | /* |
1953 | | * Comment out any un-commented commit messages, updating the message comments |
1954 | | * to say they will be skipped but do not comment out the empty lines that |
1955 | | * surround commit messages and their comments. |
1956 | | */ |
1957 | | static void update_squash_message_for_fixup(struct strbuf *msg) |
1958 | 0 | { |
1959 | 0 | void (*copy_lines)(struct strbuf *, const void *, size_t) = strbuf_add; |
1960 | 0 | struct strbuf buf1 = STRBUF_INIT, buf2 = STRBUF_INIT; |
1961 | 0 | const char *s, *start; |
1962 | 0 | char *orig_msg; |
1963 | 0 | size_t orig_msg_len; |
1964 | 0 | int i = 1; |
1965 | |
|
1966 | 0 | strbuf_addf(&buf1, "# %s\n", _(first_commit_msg_str)); |
1967 | 0 | strbuf_addf(&buf2, "# %s\n", _(skip_first_commit_msg_str)); |
1968 | 0 | s = start = orig_msg = strbuf_detach(msg, &orig_msg_len); |
1969 | 0 | while (s) { |
1970 | 0 | const char *next; |
1971 | 0 | size_t off; |
1972 | 0 | if (skip_prefix(s, buf1.buf, &next)) { |
1973 | | /* |
1974 | | * Copy the last message, preserving the blank line |
1975 | | * preceding the current line |
1976 | | */ |
1977 | 0 | off = (s > start + 1 && s[-2] == '\n') ? 1 : 0; |
1978 | 0 | copy_lines(msg, start, s - start - off); |
1979 | 0 | if (off) |
1980 | 0 | strbuf_addch(msg, '\n'); |
1981 | | /* |
1982 | | * The next message needs to be commented out but the |
1983 | | * message header is already commented out so just copy |
1984 | | * it and the blank line that follows it. |
1985 | | */ |
1986 | 0 | strbuf_addbuf(msg, &buf2); |
1987 | 0 | if (*next == '\n') |
1988 | 0 | strbuf_addch(msg, *next++); |
1989 | 0 | start = s = next; |
1990 | 0 | copy_lines = add_commented_lines; |
1991 | 0 | update_comment_bufs(&buf1, &buf2, ++i); |
1992 | 0 | } else if (skip_prefix(s, buf2.buf, &next)) { |
1993 | 0 | off = (s > start + 1 && s[-2] == '\n') ? 1 : 0; |
1994 | 0 | copy_lines(msg, start, s - start - off); |
1995 | 0 | start = s - off; |
1996 | 0 | s = next; |
1997 | 0 | copy_lines = strbuf_add; |
1998 | 0 | update_comment_bufs(&buf1, &buf2, ++i); |
1999 | 0 | } else { |
2000 | 0 | s = strchr(s, '\n'); |
2001 | 0 | if (s) |
2002 | 0 | s++; |
2003 | 0 | } |
2004 | 0 | } |
2005 | 0 | copy_lines(msg, start, orig_msg_len - (start - orig_msg)); |
2006 | 0 | free(orig_msg); |
2007 | 0 | strbuf_release(&buf1); |
2008 | 0 | strbuf_release(&buf2); |
2009 | 0 | } |
2010 | | |
2011 | | static int append_squash_message(struct strbuf *buf, const char *body, |
2012 | | enum todo_command command, struct replay_opts *opts, |
2013 | | unsigned flag) |
2014 | 0 | { |
2015 | 0 | struct replay_ctx *ctx = opts->ctx; |
2016 | 0 | const char *fixup_msg; |
2017 | 0 | size_t commented_len = 0, fixup_off; |
2018 | | /* |
2019 | | * amend is non-interactive and not normally used with fixup! |
2020 | | * or squash! commits, so only comment out those subjects when |
2021 | | * squashing commit messages. |
2022 | | */ |
2023 | 0 | if (starts_with(body, "amend!") || |
2024 | 0 | ((command == TODO_SQUASH || seen_squash(ctx)) && |
2025 | 0 | (starts_with(body, "squash!") || starts_with(body, "fixup!")))) |
2026 | 0 | commented_len = commit_subject_length(body); |
2027 | |
|
2028 | 0 | strbuf_addf(buf, "\n%s ", comment_line_str); |
2029 | 0 | strbuf_addf(buf, _(nth_commit_msg_fmt), |
2030 | 0 | ++ctx->current_fixup_count + 1); |
2031 | 0 | strbuf_addstr(buf, "\n\n"); |
2032 | 0 | strbuf_add_commented_lines(buf, body, commented_len, comment_line_str); |
2033 | | /* buf->buf may be reallocated so store an offset into the buffer */ |
2034 | 0 | fixup_off = buf->len; |
2035 | 0 | strbuf_addstr(buf, body + commented_len); |
2036 | | |
2037 | | /* fixup -C after squash behaves like squash */ |
2038 | 0 | if (is_fixup_flag(command, flag) && !seen_squash(ctx)) { |
2039 | | /* |
2040 | | * We're replacing the commit message so we need to |
2041 | | * append the Signed-off-by: trailer if the user |
2042 | | * requested '--signoff'. |
2043 | | */ |
2044 | 0 | if (opts->signoff) |
2045 | 0 | append_signoff(buf, 0, 0); |
2046 | |
|
2047 | 0 | if ((command == TODO_FIXUP) && |
2048 | 0 | (flag & TODO_REPLACE_FIXUP_MSG) && |
2049 | 0 | (file_exists(rebase_path_fixup_msg()) || |
2050 | 0 | !file_exists(rebase_path_squash_msg()))) { |
2051 | 0 | fixup_msg = skip_blank_lines(buf->buf + fixup_off); |
2052 | 0 | if (write_message(fixup_msg, strlen(fixup_msg), |
2053 | 0 | rebase_path_fixup_msg(), 0) < 0) |
2054 | 0 | return error(_("cannot write '%s'"), |
2055 | 0 | rebase_path_fixup_msg()); |
2056 | 0 | } else { |
2057 | 0 | unlink(rebase_path_fixup_msg()); |
2058 | 0 | } |
2059 | 0 | } else { |
2060 | 0 | unlink(rebase_path_fixup_msg()); |
2061 | 0 | } |
2062 | | |
2063 | 0 | return 0; |
2064 | 0 | } |
2065 | | |
2066 | | static int update_squash_messages(struct repository *r, |
2067 | | enum todo_command command, |
2068 | | struct commit *commit, |
2069 | | struct replay_opts *opts, |
2070 | | unsigned flag) |
2071 | 0 | { |
2072 | 0 | struct replay_ctx *ctx = opts->ctx; |
2073 | 0 | struct strbuf buf = STRBUF_INIT; |
2074 | 0 | int res = 0; |
2075 | 0 | const char *message, *body; |
2076 | 0 | const char *encoding = get_commit_output_encoding(); |
2077 | |
|
2078 | 0 | if (ctx->current_fixup_count > 0) { |
2079 | 0 | struct strbuf header = STRBUF_INIT; |
2080 | 0 | char *eol; |
2081 | |
|
2082 | 0 | if (strbuf_read_file(&buf, rebase_path_squash_msg(), 9) <= 0) |
2083 | 0 | return error(_("could not read '%s'"), |
2084 | 0 | rebase_path_squash_msg()); |
2085 | | |
2086 | 0 | eol = !starts_with(buf.buf, comment_line_str) ? |
2087 | 0 | buf.buf : strchrnul(buf.buf, '\n'); |
2088 | |
|
2089 | 0 | strbuf_addf(&header, "%s ", comment_line_str); |
2090 | 0 | strbuf_addf(&header, _(combined_commit_msg_fmt), |
2091 | 0 | ctx->current_fixup_count + 2); |
2092 | 0 | strbuf_splice(&buf, 0, eol - buf.buf, header.buf, header.len); |
2093 | 0 | strbuf_release(&header); |
2094 | 0 | if (is_fixup_flag(command, flag) && !seen_squash(ctx)) |
2095 | 0 | update_squash_message_for_fixup(&buf); |
2096 | 0 | } else { |
2097 | 0 | struct object_id head; |
2098 | 0 | struct commit *head_commit; |
2099 | 0 | const char *head_message, *body; |
2100 | |
|
2101 | 0 | if (repo_get_oid(r, "HEAD", &head)) |
2102 | 0 | return error(_("need a HEAD to fixup")); |
2103 | 0 | if (!(head_commit = lookup_commit_reference(r, &head))) |
2104 | 0 | return error(_("could not read HEAD")); |
2105 | 0 | if (!(head_message = repo_logmsg_reencode(r, head_commit, NULL, |
2106 | 0 | encoding))) |
2107 | 0 | return error(_("could not read HEAD's commit message")); |
2108 | | |
2109 | 0 | find_commit_subject(head_message, &body); |
2110 | 0 | if (command == TODO_FIXUP && !flag && write_message(body, strlen(body), |
2111 | 0 | rebase_path_fixup_msg(), 0) < 0) { |
2112 | 0 | repo_unuse_commit_buffer(r, head_commit, head_message); |
2113 | 0 | return error(_("cannot write '%s'"), rebase_path_fixup_msg()); |
2114 | 0 | } |
2115 | 0 | strbuf_addf(&buf, "%s ", comment_line_str); |
2116 | 0 | strbuf_addf(&buf, _(combined_commit_msg_fmt), 2); |
2117 | 0 | strbuf_addf(&buf, "\n%s ", comment_line_str); |
2118 | 0 | strbuf_addstr(&buf, is_fixup_flag(command, flag) ? |
2119 | 0 | _(skip_first_commit_msg_str) : |
2120 | 0 | _(first_commit_msg_str)); |
2121 | 0 | strbuf_addstr(&buf, "\n\n"); |
2122 | 0 | if (is_fixup_flag(command, flag)) |
2123 | 0 | strbuf_add_commented_lines(&buf, body, strlen(body), |
2124 | 0 | comment_line_str); |
2125 | 0 | else |
2126 | 0 | strbuf_addstr(&buf, body); |
2127 | |
|
2128 | 0 | repo_unuse_commit_buffer(r, head_commit, head_message); |
2129 | 0 | } |
2130 | | |
2131 | 0 | if (!(message = repo_logmsg_reencode(r, commit, NULL, encoding))) |
2132 | 0 | return error(_("could not read commit message of %s"), |
2133 | 0 | oid_to_hex(&commit->object.oid)); |
2134 | 0 | find_commit_subject(message, &body); |
2135 | |
|
2136 | 0 | if (command == TODO_SQUASH || is_fixup_flag(command, flag)) { |
2137 | 0 | res = append_squash_message(&buf, body, command, opts, flag); |
2138 | 0 | } else if (command == TODO_FIXUP) { |
2139 | 0 | strbuf_addf(&buf, "\n%s ", comment_line_str); |
2140 | 0 | strbuf_addf(&buf, _(skip_nth_commit_msg_fmt), |
2141 | 0 | ++ctx->current_fixup_count + 1); |
2142 | 0 | strbuf_addstr(&buf, "\n\n"); |
2143 | 0 | strbuf_add_commented_lines(&buf, body, strlen(body), |
2144 | 0 | comment_line_str); |
2145 | 0 | } else |
2146 | 0 | return error(_("unknown command: %d"), command); |
2147 | 0 | repo_unuse_commit_buffer(r, commit, message); |
2148 | |
|
2149 | 0 | if (!res) |
2150 | 0 | res = write_message(buf.buf, buf.len, rebase_path_squash_msg(), |
2151 | 0 | 0); |
2152 | 0 | strbuf_release(&buf); |
2153 | |
|
2154 | 0 | if (!res) { |
2155 | 0 | strbuf_addf(&ctx->current_fixups, "%s%s %s", |
2156 | 0 | ctx->current_fixups.len ? "\n" : "", |
2157 | 0 | command_to_string(command), |
2158 | 0 | oid_to_hex(&commit->object.oid)); |
2159 | 0 | res = write_message(ctx->current_fixups.buf, |
2160 | 0 | ctx->current_fixups.len, |
2161 | 0 | rebase_path_current_fixups(), 0); |
2162 | 0 | } |
2163 | |
|
2164 | 0 | return res; |
2165 | 0 | } |
2166 | | |
2167 | | static void flush_rewritten_pending(void) |
2168 | 0 | { |
2169 | 0 | struct strbuf buf = STRBUF_INIT; |
2170 | 0 | struct object_id newoid; |
2171 | 0 | FILE *out; |
2172 | |
|
2173 | 0 | if (strbuf_read_file(&buf, rebase_path_rewritten_pending(), (GIT_MAX_HEXSZ + 1) * 2) > 0 && |
2174 | 0 | !repo_get_oid(the_repository, "HEAD", &newoid) && |
2175 | 0 | (out = fopen_or_warn(rebase_path_rewritten_list(), "a"))) { |
2176 | 0 | char *bol = buf.buf, *eol; |
2177 | |
|
2178 | 0 | while (*bol) { |
2179 | 0 | eol = strchrnul(bol, '\n'); |
2180 | 0 | fprintf(out, "%.*s %s\n", (int)(eol - bol), |
2181 | 0 | bol, oid_to_hex(&newoid)); |
2182 | 0 | if (!*eol) |
2183 | 0 | break; |
2184 | 0 | bol = eol + 1; |
2185 | 0 | } |
2186 | 0 | fclose(out); |
2187 | 0 | unlink(rebase_path_rewritten_pending()); |
2188 | 0 | } |
2189 | 0 | strbuf_release(&buf); |
2190 | 0 | } |
2191 | | |
2192 | | static void record_in_rewritten(struct object_id *oid, |
2193 | | enum todo_command next_command) |
2194 | 0 | { |
2195 | 0 | FILE *out = fopen_or_warn(rebase_path_rewritten_pending(), "a"); |
2196 | |
|
2197 | 0 | if (!out) |
2198 | 0 | return; |
2199 | | |
2200 | 0 | fprintf(out, "%s\n", oid_to_hex(oid)); |
2201 | 0 | fclose(out); |
2202 | |
|
2203 | 0 | if (!is_fixup(next_command)) |
2204 | 0 | flush_rewritten_pending(); |
2205 | 0 | } |
2206 | | |
2207 | 0 | static int should_edit(struct replay_opts *opts) { |
2208 | 0 | if (opts->edit < 0) |
2209 | | /* |
2210 | | * Note that we only handle the case of non-conflicted |
2211 | | * commits; continue_single_pick() handles the conflicted |
2212 | | * commits itself instead of calling this function. |
2213 | | */ |
2214 | 0 | return (opts->action == REPLAY_REVERT && isatty(0)) ? 1 : 0; |
2215 | 0 | return opts->edit; |
2216 | 0 | } |
2217 | | |
2218 | | static void refer_to_commit(struct replay_opts *opts, |
2219 | | struct strbuf *msgbuf, struct commit *commit) |
2220 | 0 | { |
2221 | 0 | if (opts->commit_use_reference) { |
2222 | 0 | struct pretty_print_context ctx = { |
2223 | 0 | .abbrev = DEFAULT_ABBREV, |
2224 | 0 | .date_mode.type = DATE_SHORT, |
2225 | 0 | }; |
2226 | 0 | repo_format_commit_message(the_repository, commit, |
2227 | 0 | "%h (%s, %ad)", msgbuf, &ctx); |
2228 | 0 | } else { |
2229 | 0 | strbuf_addstr(msgbuf, oid_to_hex(&commit->object.oid)); |
2230 | 0 | } |
2231 | 0 | } |
2232 | | |
2233 | | static int do_pick_commit(struct repository *r, |
2234 | | struct todo_item *item, |
2235 | | struct replay_opts *opts, |
2236 | | int final_fixup, int *check_todo) |
2237 | 0 | { |
2238 | 0 | struct replay_ctx *ctx = opts->ctx; |
2239 | 0 | unsigned int flags = should_edit(opts) ? EDIT_MSG : 0; |
2240 | 0 | const char *msg_file = should_edit(opts) ? NULL : git_path_merge_msg(r); |
2241 | 0 | struct object_id head; |
2242 | 0 | struct commit *base, *next, *parent; |
2243 | 0 | const char *base_label, *next_label; |
2244 | 0 | char *author = NULL; |
2245 | 0 | struct commit_message msg = { NULL, NULL, NULL, NULL }; |
2246 | 0 | int res, unborn = 0, reword = 0, allow, drop_commit; |
2247 | 0 | enum todo_command command = item->command; |
2248 | 0 | struct commit *commit = item->commit; |
2249 | |
|
2250 | 0 | if (opts->no_commit) { |
2251 | | /* |
2252 | | * We do not intend to commit immediately. We just want to |
2253 | | * merge the differences in, so let's compute the tree |
2254 | | * that represents the "current" state for the merge machinery |
2255 | | * to work on. |
2256 | | */ |
2257 | 0 | if (write_index_as_tree(&head, r->index, r->index_file, 0, NULL)) |
2258 | 0 | return error(_("your index file is unmerged.")); |
2259 | 0 | } else { |
2260 | 0 | unborn = repo_get_oid(r, "HEAD", &head); |
2261 | | /* Do we want to generate a root commit? */ |
2262 | 0 | if (is_pick_or_similar(command) && opts->have_squash_onto && |
2263 | 0 | oideq(&head, &opts->squash_onto)) { |
2264 | 0 | if (is_fixup(command)) |
2265 | 0 | return error(_("cannot fixup root commit")); |
2266 | 0 | flags |= CREATE_ROOT_COMMIT; |
2267 | 0 | unborn = 1; |
2268 | 0 | } else if (unborn) |
2269 | 0 | oidcpy(&head, the_hash_algo->empty_tree); |
2270 | 0 | if (index_differs_from(r, unborn ? empty_tree_oid_hex(the_repository->hash_algo) : "HEAD", |
2271 | 0 | NULL, 0)) |
2272 | 0 | return error_dirty_index(r, opts); |
2273 | 0 | } |
2274 | 0 | discard_index(r->index); |
2275 | |
|
2276 | 0 | if (!commit->parents) |
2277 | 0 | parent = NULL; |
2278 | 0 | else if (commit->parents->next) { |
2279 | | /* Reverting or cherry-picking a merge commit */ |
2280 | 0 | int cnt; |
2281 | 0 | struct commit_list *p; |
2282 | |
|
2283 | 0 | if (!opts->mainline) |
2284 | 0 | return error(_("commit %s is a merge but no -m option was given."), |
2285 | 0 | oid_to_hex(&commit->object.oid)); |
2286 | | |
2287 | 0 | for (cnt = 1, p = commit->parents; |
2288 | 0 | cnt != opts->mainline && p; |
2289 | 0 | cnt++) |
2290 | 0 | p = p->next; |
2291 | 0 | if (cnt != opts->mainline || !p) |
2292 | 0 | return error(_("commit %s does not have parent %d"), |
2293 | 0 | oid_to_hex(&commit->object.oid), opts->mainline); |
2294 | 0 | parent = p->item; |
2295 | 0 | } else if (1 < opts->mainline) |
2296 | | /* |
2297 | | * Non-first parent explicitly specified as mainline for |
2298 | | * non-merge commit |
2299 | | */ |
2300 | 0 | return error(_("commit %s does not have parent %d"), |
2301 | 0 | oid_to_hex(&commit->object.oid), opts->mainline); |
2302 | 0 | else |
2303 | 0 | parent = commit->parents->item; |
2304 | | |
2305 | 0 | if (get_message(commit, &msg) != 0) |
2306 | 0 | return error(_("cannot get commit message for %s"), |
2307 | 0 | oid_to_hex(&commit->object.oid)); |
2308 | | |
2309 | 0 | if (opts->allow_ff && !is_fixup(command) && |
2310 | 0 | ((parent && oideq(&parent->object.oid, &head)) || |
2311 | 0 | (!parent && unborn))) { |
2312 | 0 | if (is_rebase_i(opts)) |
2313 | 0 | write_author_script(msg.message); |
2314 | 0 | res = fast_forward_to(r, &commit->object.oid, &head, unborn, |
2315 | 0 | opts); |
2316 | 0 | if (res || command != TODO_REWORD) |
2317 | 0 | goto leave; |
2318 | 0 | reword = 1; |
2319 | 0 | msg_file = NULL; |
2320 | 0 | goto fast_forward_edit; |
2321 | 0 | } |
2322 | 0 | if (parent && repo_parse_commit(r, parent) < 0) |
2323 | | /* TRANSLATORS: The first %s will be a "todo" command like |
2324 | | "revert" or "pick", the second %s a SHA1. */ |
2325 | 0 | return error(_("%s: cannot parse parent commit %s"), |
2326 | 0 | command_to_string(command), |
2327 | 0 | oid_to_hex(&parent->object.oid)); |
2328 | | |
2329 | | /* |
2330 | | * "commit" is an existing commit. We would want to apply |
2331 | | * the difference it introduces since its first parent "prev" |
2332 | | * on top of the current HEAD if we are cherry-pick. Or the |
2333 | | * reverse of it if we are revert. |
2334 | | */ |
2335 | | |
2336 | 0 | if (command == TODO_REVERT) { |
2337 | 0 | const char *orig_subject; |
2338 | |
|
2339 | 0 | base = commit; |
2340 | 0 | base_label = msg.label; |
2341 | 0 | next = parent; |
2342 | 0 | next_label = msg.parent_label; |
2343 | 0 | if (opts->commit_use_reference) { |
2344 | 0 | strbuf_addstr(&ctx->message, |
2345 | 0 | "# *** SAY WHY WE ARE REVERTING ON THE TITLE LINE ***"); |
2346 | 0 | } else if (skip_prefix(msg.subject, "Revert \"", &orig_subject) && |
2347 | | /* |
2348 | | * We don't touch pre-existing repeated reverts, because |
2349 | | * theoretically these can be nested arbitrarily deeply, |
2350 | | * thus requiring excessive complexity to deal with. |
2351 | | */ |
2352 | 0 | !starts_with(orig_subject, "Revert \"")) { |
2353 | 0 | strbuf_addstr(&ctx->message, "Reapply \""); |
2354 | 0 | strbuf_addstr(&ctx->message, orig_subject); |
2355 | 0 | } else { |
2356 | 0 | strbuf_addstr(&ctx->message, "Revert \""); |
2357 | 0 | strbuf_addstr(&ctx->message, msg.subject); |
2358 | 0 | strbuf_addstr(&ctx->message, "\""); |
2359 | 0 | } |
2360 | 0 | strbuf_addstr(&ctx->message, "\n\nThis reverts commit "); |
2361 | 0 | refer_to_commit(opts, &ctx->message, commit); |
2362 | |
|
2363 | 0 | if (commit->parents && commit->parents->next) { |
2364 | 0 | strbuf_addstr(&ctx->message, ", reversing\nchanges made to "); |
2365 | 0 | refer_to_commit(opts, &ctx->message, parent); |
2366 | 0 | } |
2367 | 0 | strbuf_addstr(&ctx->message, ".\n"); |
2368 | 0 | } else { |
2369 | 0 | const char *p; |
2370 | |
|
2371 | 0 | base = parent; |
2372 | 0 | base_label = msg.parent_label; |
2373 | 0 | next = commit; |
2374 | 0 | next_label = msg.label; |
2375 | | |
2376 | | /* Append the commit log message to ctx->message. */ |
2377 | 0 | if (find_commit_subject(msg.message, &p)) |
2378 | 0 | strbuf_addstr(&ctx->message, p); |
2379 | |
|
2380 | 0 | if (opts->record_origin) { |
2381 | 0 | strbuf_complete_line(&ctx->message); |
2382 | 0 | if (!has_conforming_footer(&ctx->message, NULL, 0)) |
2383 | 0 | strbuf_addch(&ctx->message, '\n'); |
2384 | 0 | strbuf_addstr(&ctx->message, cherry_picked_prefix); |
2385 | 0 | strbuf_addstr(&ctx->message, oid_to_hex(&commit->object.oid)); |
2386 | 0 | strbuf_addstr(&ctx->message, ")\n"); |
2387 | 0 | } |
2388 | 0 | if (!is_fixup(command)) |
2389 | 0 | author = get_author(msg.message); |
2390 | 0 | } |
2391 | 0 | ctx->have_message = 1; |
2392 | |
|
2393 | 0 | if (command == TODO_REWORD) |
2394 | 0 | reword = 1; |
2395 | 0 | else if (is_fixup(command)) { |
2396 | 0 | if (update_squash_messages(r, command, commit, |
2397 | 0 | opts, item->flags)) { |
2398 | 0 | res = -1; |
2399 | 0 | goto leave; |
2400 | 0 | } |
2401 | 0 | flags |= AMEND_MSG; |
2402 | 0 | if (!final_fixup) |
2403 | 0 | msg_file = rebase_path_squash_msg(); |
2404 | 0 | else if (file_exists(rebase_path_fixup_msg())) { |
2405 | 0 | flags |= VERBATIM_MSG; |
2406 | 0 | msg_file = rebase_path_fixup_msg(); |
2407 | 0 | } else { |
2408 | 0 | const char *dest = git_path_squash_msg(r); |
2409 | 0 | unlink(dest); |
2410 | 0 | if (copy_file(dest, rebase_path_squash_msg(), 0666)) { |
2411 | 0 | res = error(_("could not copy '%s' to '%s'"), |
2412 | 0 | rebase_path_squash_msg(), dest); |
2413 | 0 | goto leave; |
2414 | 0 | } |
2415 | 0 | unlink(git_path_merge_msg(r)); |
2416 | 0 | msg_file = dest; |
2417 | 0 | flags |= EDIT_MSG; |
2418 | 0 | } |
2419 | 0 | } |
2420 | | |
2421 | 0 | if (opts->signoff && !is_fixup(command)) |
2422 | 0 | append_signoff(&ctx->message, 0, 0); |
2423 | |
|
2424 | 0 | if (is_rebase_i(opts) && write_author_script(msg.message) < 0) |
2425 | 0 | res = -1; |
2426 | 0 | else if (!opts->strategy || |
2427 | 0 | !strcmp(opts->strategy, "recursive") || |
2428 | 0 | !strcmp(opts->strategy, "ort") || |
2429 | 0 | command == TODO_REVERT) { |
2430 | 0 | res = do_recursive_merge(r, base, next, base_label, next_label, |
2431 | 0 | &head, &ctx->message, opts); |
2432 | 0 | if (res < 0) |
2433 | 0 | goto leave; |
2434 | | |
2435 | 0 | res |= write_message(ctx->message.buf, ctx->message.len, |
2436 | 0 | git_path_merge_msg(r), 0); |
2437 | 0 | } else { |
2438 | 0 | struct commit_list *common = NULL; |
2439 | 0 | struct commit_list *remotes = NULL; |
2440 | |
|
2441 | 0 | res = write_message(ctx->message.buf, ctx->message.len, |
2442 | 0 | git_path_merge_msg(r), 0); |
2443 | |
|
2444 | 0 | commit_list_insert(base, &common); |
2445 | 0 | commit_list_insert(next, &remotes); |
2446 | 0 | res |= try_merge_command(r, opts->strategy, |
2447 | 0 | opts->xopts.nr, opts->xopts.v, |
2448 | 0 | common, oid_to_hex(&head), remotes); |
2449 | 0 | free_commit_list(common); |
2450 | 0 | free_commit_list(remotes); |
2451 | 0 | } |
2452 | | |
2453 | | /* |
2454 | | * If the merge was clean or if it failed due to conflict, we write |
2455 | | * CHERRY_PICK_HEAD for the subsequent invocation of commit to use. |
2456 | | * However, if the merge did not even start, then we don't want to |
2457 | | * write it at all. |
2458 | | */ |
2459 | 0 | if ((command == TODO_PICK || command == TODO_REWORD || |
2460 | 0 | command == TODO_EDIT) && !opts->no_commit && |
2461 | 0 | (res == 0 || res == 1) && |
2462 | 0 | refs_update_ref(get_main_ref_store(the_repository), NULL, "CHERRY_PICK_HEAD", &commit->object.oid, NULL, |
2463 | 0 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) |
2464 | 0 | res = -1; |
2465 | 0 | if (command == TODO_REVERT && ((opts->no_commit && res == 0) || res == 1) && |
2466 | 0 | refs_update_ref(get_main_ref_store(the_repository), NULL, "REVERT_HEAD", &commit->object.oid, NULL, |
2467 | 0 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) |
2468 | 0 | res = -1; |
2469 | |
|
2470 | 0 | if (res) { |
2471 | 0 | error(command == TODO_REVERT |
2472 | 0 | ? _("could not revert %s... %s") |
2473 | 0 | : _("could not apply %s... %s"), |
2474 | 0 | short_commit_name(r, commit), msg.subject); |
2475 | 0 | print_advice(r, res == 1, opts); |
2476 | 0 | repo_rerere(r, opts->allow_rerere_auto); |
2477 | 0 | goto leave; |
2478 | 0 | } |
2479 | | |
2480 | 0 | drop_commit = 0; |
2481 | 0 | allow = allow_empty(r, opts, commit); |
2482 | 0 | if (allow < 0) { |
2483 | 0 | res = allow; |
2484 | 0 | goto leave; |
2485 | 0 | } else if (allow == 1) { |
2486 | 0 | flags |= ALLOW_EMPTY; |
2487 | 0 | } else if (allow == 2) { |
2488 | 0 | drop_commit = 1; |
2489 | 0 | refs_delete_ref(get_main_ref_store(r), "", "CHERRY_PICK_HEAD", |
2490 | 0 | NULL, REF_NO_DEREF); |
2491 | 0 | unlink(git_path_merge_msg(r)); |
2492 | 0 | refs_delete_ref(get_main_ref_store(r), "", "AUTO_MERGE", |
2493 | 0 | NULL, REF_NO_DEREF); |
2494 | 0 | fprintf(stderr, |
2495 | 0 | _("dropping %s %s -- patch contents already upstream\n"), |
2496 | 0 | oid_to_hex(&commit->object.oid), msg.subject); |
2497 | 0 | } /* else allow == 0 and there's nothing special to do */ |
2498 | 0 | if (!opts->no_commit && !drop_commit) { |
2499 | 0 | if (author || command == TODO_REVERT || (flags & AMEND_MSG)) |
2500 | 0 | res = do_commit(r, msg_file, author, opts, flags, |
2501 | 0 | commit? &commit->object.oid : NULL); |
2502 | 0 | else |
2503 | 0 | res = error(_("unable to parse commit author")); |
2504 | 0 | *check_todo = !!(flags & EDIT_MSG); |
2505 | 0 | if (!res && reword) { |
2506 | 0 | fast_forward_edit: |
2507 | 0 | res = run_git_commit(NULL, opts, EDIT_MSG | |
2508 | 0 | VERIFY_MSG | AMEND_MSG | |
2509 | 0 | (flags & ALLOW_EMPTY)); |
2510 | 0 | *check_todo = 1; |
2511 | 0 | } |
2512 | 0 | } |
2513 | | |
2514 | |
|
2515 | 0 | if (!res && final_fixup) { |
2516 | 0 | unlink(rebase_path_fixup_msg()); |
2517 | 0 | unlink(rebase_path_squash_msg()); |
2518 | 0 | unlink(rebase_path_current_fixups()); |
2519 | 0 | strbuf_reset(&ctx->current_fixups); |
2520 | 0 | ctx->current_fixup_count = 0; |
2521 | 0 | } |
2522 | |
|
2523 | 0 | leave: |
2524 | 0 | free_message(commit, &msg); |
2525 | 0 | free(author); |
2526 | 0 | update_abort_safety_file(); |
2527 | |
|
2528 | 0 | return res; |
2529 | 0 | } |
2530 | | |
2531 | | static int prepare_revs(struct replay_opts *opts) |
2532 | 0 | { |
2533 | | /* |
2534 | | * picking (but not reverting) ranges (but not individual revisions) |
2535 | | * should be done in reverse |
2536 | | */ |
2537 | 0 | if (opts->action == REPLAY_PICK && !opts->revs->no_walk) |
2538 | 0 | opts->revs->reverse ^= 1; |
2539 | |
|
2540 | 0 | if (prepare_revision_walk(opts->revs)) |
2541 | 0 | return error(_("revision walk setup failed")); |
2542 | | |
2543 | 0 | return 0; |
2544 | 0 | } |
2545 | | |
2546 | | static int read_and_refresh_cache(struct repository *r, |
2547 | | struct replay_opts *opts) |
2548 | 0 | { |
2549 | 0 | struct lock_file index_lock = LOCK_INIT; |
2550 | 0 | int index_fd = repo_hold_locked_index(r, &index_lock, 0); |
2551 | 0 | if (repo_read_index(r) < 0) { |
2552 | 0 | rollback_lock_file(&index_lock); |
2553 | 0 | return error(_("git %s: failed to read the index"), |
2554 | 0 | action_name(opts)); |
2555 | 0 | } |
2556 | 0 | refresh_index(r->index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL); |
2557 | |
|
2558 | 0 | if (index_fd >= 0) { |
2559 | 0 | if (write_locked_index(r->index, &index_lock, |
2560 | 0 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) { |
2561 | 0 | return error(_("git %s: failed to refresh the index"), |
2562 | 0 | action_name(opts)); |
2563 | 0 | } |
2564 | 0 | } |
2565 | | |
2566 | | /* |
2567 | | * If we are resolving merges in any way other than "ort", then |
2568 | | * expand the sparse index. |
2569 | | */ |
2570 | 0 | if (opts->strategy && strcmp(opts->strategy, "ort")) |
2571 | 0 | ensure_full_index(r->index); |
2572 | 0 | return 0; |
2573 | 0 | } |
2574 | | |
2575 | | void todo_list_release(struct todo_list *todo_list) |
2576 | 0 | { |
2577 | 0 | strbuf_release(&todo_list->buf); |
2578 | 0 | FREE_AND_NULL(todo_list->items); |
2579 | 0 | todo_list->nr = todo_list->alloc = 0; |
2580 | 0 | } |
2581 | | |
2582 | | static struct todo_item *append_new_todo(struct todo_list *todo_list) |
2583 | 0 | { |
2584 | 0 | ALLOC_GROW(todo_list->items, todo_list->nr + 1, todo_list->alloc); |
2585 | 0 | return todo_list->items + todo_list->nr++; |
2586 | 0 | } |
2587 | | |
2588 | | const char *todo_item_get_arg(struct todo_list *todo_list, |
2589 | | struct todo_item *item) |
2590 | 0 | { |
2591 | 0 | return todo_list->buf.buf + item->arg_offset; |
2592 | 0 | } |
2593 | | |
2594 | | static int is_command(enum todo_command command, const char **bol) |
2595 | 0 | { |
2596 | 0 | const char *str = todo_command_info[command].str; |
2597 | 0 | const char nick = todo_command_info[command].c; |
2598 | 0 | const char *p = *bol; |
2599 | |
|
2600 | 0 | return (skip_prefix(p, str, &p) || (nick && *p++ == nick)) && |
2601 | 0 | (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || !*p) && |
2602 | 0 | (*bol = p); |
2603 | 0 | } |
2604 | | |
2605 | | static int check_label_or_ref_arg(enum todo_command command, const char *arg) |
2606 | 0 | { |
2607 | 0 | switch (command) { |
2608 | 0 | case TODO_LABEL: |
2609 | | /* |
2610 | | * '#' is not a valid label as the merge command uses it to |
2611 | | * separate merge parents from the commit subject. |
2612 | | */ |
2613 | 0 | if (!strcmp(arg, "#") || |
2614 | 0 | check_refname_format(arg, REFNAME_ALLOW_ONELEVEL)) |
2615 | 0 | return error(_("'%s' is not a valid label"), arg); |
2616 | 0 | break; |
2617 | | |
2618 | 0 | case TODO_UPDATE_REF: |
2619 | 0 | if (check_refname_format(arg, REFNAME_ALLOW_ONELEVEL)) |
2620 | 0 | return error(_("'%s' is not a valid refname"), arg); |
2621 | 0 | if (check_refname_format(arg, 0)) |
2622 | 0 | return error(_("update-ref requires a fully qualified " |
2623 | 0 | "refname e.g. refs/heads/%s"), arg); |
2624 | 0 | break; |
2625 | | |
2626 | 0 | default: |
2627 | 0 | BUG("unexpected todo_command"); |
2628 | 0 | } |
2629 | | |
2630 | 0 | return 0; |
2631 | 0 | } |
2632 | | |
2633 | | static int check_merge_commit_insn(enum todo_command command) |
2634 | 0 | { |
2635 | 0 | switch(command) { |
2636 | 0 | case TODO_PICK: |
2637 | 0 | error(_("'%s' does not accept merge commits"), |
2638 | 0 | todo_command_info[command].str); |
2639 | 0 | advise_if_enabled(ADVICE_REBASE_TODO_ERROR, _( |
2640 | | /* |
2641 | | * TRANSLATORS: 'pick' and 'merge -C' should not be |
2642 | | * translated. |
2643 | | */ |
2644 | 0 | "'pick' does not take a merge commit. If you wanted to\n" |
2645 | 0 | "replay the merge, use 'merge -C' on the commit.")); |
2646 | 0 | return -1; |
2647 | | |
2648 | 0 | case TODO_REWORD: |
2649 | 0 | error(_("'%s' does not accept merge commits"), |
2650 | 0 | todo_command_info[command].str); |
2651 | 0 | advise_if_enabled(ADVICE_REBASE_TODO_ERROR, _( |
2652 | | /* |
2653 | | * TRANSLATORS: 'reword' and 'merge -c' should not be |
2654 | | * translated. |
2655 | | */ |
2656 | 0 | "'reword' does not take a merge commit. If you wanted to\n" |
2657 | 0 | "replay the merge and reword the commit message, use\n" |
2658 | 0 | "'merge -c' on the commit")); |
2659 | 0 | return -1; |
2660 | | |
2661 | 0 | case TODO_EDIT: |
2662 | 0 | error(_("'%s' does not accept merge commits"), |
2663 | 0 | todo_command_info[command].str); |
2664 | 0 | advise_if_enabled(ADVICE_REBASE_TODO_ERROR, _( |
2665 | | /* |
2666 | | * TRANSLATORS: 'edit', 'merge -C' and 'break' should |
2667 | | * not be translated. |
2668 | | */ |
2669 | 0 | "'edit' does not take a merge commit. If you wanted to\n" |
2670 | 0 | "replay the merge, use 'merge -C' on the commit, and then\n" |
2671 | 0 | "'break' to give the control back to you so that you can\n" |
2672 | 0 | "do 'git commit --amend && git rebase --continue'.")); |
2673 | 0 | return -1; |
2674 | | |
2675 | 0 | case TODO_FIXUP: |
2676 | 0 | case TODO_SQUASH: |
2677 | 0 | return error(_("cannot squash merge commit into another commit")); |
2678 | | |
2679 | 0 | case TODO_MERGE: |
2680 | 0 | return 0; |
2681 | | |
2682 | 0 | default: |
2683 | 0 | BUG("unexpected todo_command"); |
2684 | 0 | } |
2685 | 0 | } |
2686 | | |
2687 | | static int parse_insn_line(struct repository *r, struct replay_opts *opts, |
2688 | | struct todo_item *item, const char *buf, |
2689 | | const char *bol, char *eol) |
2690 | 0 | { |
2691 | 0 | struct object_id commit_oid; |
2692 | 0 | char *end_of_object_name; |
2693 | 0 | int i, saved, status, padding; |
2694 | |
|
2695 | 0 | item->flags = 0; |
2696 | | |
2697 | | /* left-trim */ |
2698 | 0 | bol += strspn(bol, " \t"); |
2699 | |
|
2700 | 0 | if (bol == eol || *bol == '\r' || starts_with_mem(bol, eol - bol, comment_line_str)) { |
2701 | 0 | item->command = TODO_COMMENT; |
2702 | 0 | item->commit = NULL; |
2703 | 0 | item->arg_offset = bol - buf; |
2704 | 0 | item->arg_len = eol - bol; |
2705 | 0 | return 0; |
2706 | 0 | } |
2707 | | |
2708 | 0 | for (i = 0; i < TODO_COMMENT; i++) |
2709 | 0 | if (is_command(i, &bol)) { |
2710 | 0 | item->command = i; |
2711 | 0 | break; |
2712 | 0 | } |
2713 | 0 | if (i >= TODO_COMMENT) |
2714 | 0 | return error(_("invalid command '%.*s'"), |
2715 | 0 | (int)strcspn(bol, " \t\r\n"), bol); |
2716 | | |
2717 | | /* Eat up extra spaces/ tabs before object name */ |
2718 | 0 | padding = strspn(bol, " \t"); |
2719 | 0 | bol += padding; |
2720 | |
|
2721 | 0 | if (item->command == TODO_NOOP || item->command == TODO_BREAK) { |
2722 | 0 | if (bol != eol) |
2723 | 0 | return error(_("%s does not accept arguments: '%s'"), |
2724 | 0 | command_to_string(item->command), bol); |
2725 | 0 | item->commit = NULL; |
2726 | 0 | item->arg_offset = bol - buf; |
2727 | 0 | item->arg_len = eol - bol; |
2728 | 0 | return 0; |
2729 | 0 | } |
2730 | | |
2731 | 0 | if (!padding) |
2732 | 0 | return error(_("missing arguments for %s"), |
2733 | 0 | command_to_string(item->command)); |
2734 | | |
2735 | 0 | if (item->command == TODO_EXEC || item->command == TODO_LABEL || |
2736 | 0 | item->command == TODO_RESET || item->command == TODO_UPDATE_REF) { |
2737 | 0 | int ret = 0; |
2738 | |
|
2739 | 0 | item->commit = NULL; |
2740 | 0 | item->arg_offset = bol - buf; |
2741 | 0 | item->arg_len = (int)(eol - bol); |
2742 | 0 | if (item->command == TODO_LABEL || |
2743 | 0 | item->command == TODO_UPDATE_REF) { |
2744 | 0 | saved = *eol; |
2745 | 0 | *eol = '\0'; |
2746 | 0 | ret = check_label_or_ref_arg(item->command, bol); |
2747 | 0 | *eol = saved; |
2748 | 0 | } |
2749 | 0 | return ret; |
2750 | 0 | } |
2751 | | |
2752 | 0 | if (item->command == TODO_FIXUP) { |
2753 | 0 | if (skip_prefix(bol, "-C", &bol)) { |
2754 | 0 | bol += strspn(bol, " \t"); |
2755 | 0 | item->flags |= TODO_REPLACE_FIXUP_MSG; |
2756 | 0 | } else if (skip_prefix(bol, "-c", &bol)) { |
2757 | 0 | bol += strspn(bol, " \t"); |
2758 | 0 | item->flags |= TODO_EDIT_FIXUP_MSG; |
2759 | 0 | } |
2760 | 0 | } |
2761 | |
|
2762 | 0 | if (item->command == TODO_MERGE) { |
2763 | 0 | if (skip_prefix(bol, "-C", &bol)) |
2764 | 0 | bol += strspn(bol, " \t"); |
2765 | 0 | else if (skip_prefix(bol, "-c", &bol)) { |
2766 | 0 | bol += strspn(bol, " \t"); |
2767 | 0 | item->flags |= TODO_EDIT_MERGE_MSG; |
2768 | 0 | } else { |
2769 | 0 | item->flags |= TODO_EDIT_MERGE_MSG; |
2770 | 0 | item->commit = NULL; |
2771 | 0 | item->arg_offset = bol - buf; |
2772 | 0 | item->arg_len = (int)(eol - bol); |
2773 | 0 | return 0; |
2774 | 0 | } |
2775 | 0 | } |
2776 | | |
2777 | 0 | end_of_object_name = (char *) bol + strcspn(bol, " \t\n"); |
2778 | 0 | saved = *end_of_object_name; |
2779 | 0 | *end_of_object_name = '\0'; |
2780 | 0 | status = repo_get_oid(r, bol, &commit_oid); |
2781 | 0 | if (status < 0) |
2782 | 0 | error(_("could not parse '%s'"), bol); /* return later */ |
2783 | 0 | *end_of_object_name = saved; |
2784 | |
|
2785 | 0 | bol = end_of_object_name + strspn(end_of_object_name, " \t"); |
2786 | 0 | item->arg_offset = bol - buf; |
2787 | 0 | item->arg_len = (int)(eol - bol); |
2788 | |
|
2789 | 0 | if (status < 0) |
2790 | 0 | return status; |
2791 | | |
2792 | 0 | item->commit = lookup_commit_reference(r, &commit_oid); |
2793 | 0 | if (!item->commit) |
2794 | 0 | return -1; |
2795 | 0 | if (is_rebase_i(opts) && |
2796 | 0 | item->commit->parents && item->commit->parents->next) |
2797 | 0 | return check_merge_commit_insn(item->command); |
2798 | 0 | return 0; |
2799 | 0 | } |
2800 | | |
2801 | | int sequencer_get_last_command(struct repository *r UNUSED, enum replay_action *action) |
2802 | 0 | { |
2803 | 0 | const char *todo_file, *bol; |
2804 | 0 | struct strbuf buf = STRBUF_INIT; |
2805 | 0 | int ret = 0; |
2806 | |
|
2807 | 0 | todo_file = git_path_todo_file(); |
2808 | 0 | if (strbuf_read_file(&buf, todo_file, 0) < 0) { |
2809 | 0 | if (errno == ENOENT || errno == ENOTDIR) |
2810 | 0 | return -1; |
2811 | 0 | else |
2812 | 0 | return error_errno("unable to open '%s'", todo_file); |
2813 | 0 | } |
2814 | 0 | bol = buf.buf + strspn(buf.buf, " \t\r\n"); |
2815 | 0 | if (is_command(TODO_PICK, &bol) && (*bol == ' ' || *bol == '\t')) |
2816 | 0 | *action = REPLAY_PICK; |
2817 | 0 | else if (is_command(TODO_REVERT, &bol) && |
2818 | 0 | (*bol == ' ' || *bol == '\t')) |
2819 | 0 | *action = REPLAY_REVERT; |
2820 | 0 | else |
2821 | 0 | ret = -1; |
2822 | |
|
2823 | 0 | strbuf_release(&buf); |
2824 | |
|
2825 | 0 | return ret; |
2826 | 0 | } |
2827 | | |
2828 | | int todo_list_parse_insn_buffer(struct repository *r, struct replay_opts *opts, |
2829 | | char *buf, struct todo_list *todo_list) |
2830 | 0 | { |
2831 | 0 | struct todo_item *item; |
2832 | 0 | char *p = buf, *next_p; |
2833 | 0 | int i, res = 0, fixup_okay = file_exists(rebase_path_done()); |
2834 | |
|
2835 | 0 | todo_list->current = todo_list->nr = todo_list->total_nr = 0; |
2836 | |
|
2837 | 0 | for (i = 1; *p; i++, p = next_p) { |
2838 | 0 | char *eol = strchrnul(p, '\n'); |
2839 | |
|
2840 | 0 | next_p = *eol ? eol + 1 /* skip LF */ : eol; |
2841 | |
|
2842 | 0 | if (p != eol && eol[-1] == '\r') |
2843 | 0 | eol--; /* strip Carriage Return */ |
2844 | |
|
2845 | 0 | item = append_new_todo(todo_list); |
2846 | 0 | item->offset_in_buf = p - todo_list->buf.buf; |
2847 | 0 | if (parse_insn_line(r, opts, item, buf, p, eol)) { |
2848 | 0 | res = error(_("invalid line %d: %.*s"), |
2849 | 0 | i, (int)(eol - p), p); |
2850 | 0 | item->command = TODO_COMMENT + 1; |
2851 | 0 | item->arg_offset = p - buf; |
2852 | 0 | item->arg_len = (int)(eol - p); |
2853 | 0 | item->commit = NULL; |
2854 | 0 | } |
2855 | |
|
2856 | 0 | if (item->command != TODO_COMMENT) |
2857 | 0 | todo_list->total_nr++; |
2858 | |
|
2859 | 0 | if (fixup_okay) |
2860 | 0 | ; /* do nothing */ |
2861 | 0 | else if (is_fixup(item->command)) |
2862 | 0 | res = error(_("cannot '%s' without a previous commit"), |
2863 | 0 | command_to_string(item->command)); |
2864 | 0 | else if (!is_noop(item->command)) |
2865 | 0 | fixup_okay = 1; |
2866 | 0 | } |
2867 | |
|
2868 | 0 | return res; |
2869 | 0 | } |
2870 | | |
2871 | | static int count_commands(struct todo_list *todo_list) |
2872 | 0 | { |
2873 | 0 | int count = 0, i; |
2874 | |
|
2875 | 0 | for (i = 0; i < todo_list->nr; i++) |
2876 | 0 | if (todo_list->items[i].command != TODO_COMMENT) |
2877 | 0 | count++; |
2878 | |
|
2879 | 0 | return count; |
2880 | 0 | } |
2881 | | |
2882 | | static int get_item_line_offset(struct todo_list *todo_list, int index) |
2883 | 0 | { |
2884 | 0 | return index < todo_list->nr ? |
2885 | 0 | todo_list->items[index].offset_in_buf : todo_list->buf.len; |
2886 | 0 | } |
2887 | | |
2888 | | static const char *get_item_line(struct todo_list *todo_list, int index) |
2889 | 0 | { |
2890 | 0 | return todo_list->buf.buf + get_item_line_offset(todo_list, index); |
2891 | 0 | } |
2892 | | |
2893 | | static int get_item_line_length(struct todo_list *todo_list, int index) |
2894 | 0 | { |
2895 | 0 | return get_item_line_offset(todo_list, index + 1) |
2896 | 0 | - get_item_line_offset(todo_list, index); |
2897 | 0 | } |
2898 | | |
2899 | | static ssize_t strbuf_read_file_or_whine(struct strbuf *sb, const char *path) |
2900 | 0 | { |
2901 | 0 | int fd; |
2902 | 0 | ssize_t len; |
2903 | |
|
2904 | 0 | fd = open(path, O_RDONLY); |
2905 | 0 | if (fd < 0) |
2906 | 0 | return error_errno(_("could not open '%s'"), path); |
2907 | 0 | len = strbuf_read(sb, fd, 0); |
2908 | 0 | close(fd); |
2909 | 0 | if (len < 0) |
2910 | 0 | return error(_("could not read '%s'."), path); |
2911 | 0 | return len; |
2912 | 0 | } |
2913 | | |
2914 | | static int have_finished_the_last_pick(void) |
2915 | 0 | { |
2916 | 0 | struct strbuf buf = STRBUF_INIT; |
2917 | 0 | const char *eol; |
2918 | 0 | const char *todo_path = git_path_todo_file(); |
2919 | 0 | int ret = 0; |
2920 | |
|
2921 | 0 | if (strbuf_read_file(&buf, todo_path, 0) < 0) { |
2922 | 0 | if (errno == ENOENT) { |
2923 | 0 | return 0; |
2924 | 0 | } else { |
2925 | 0 | error_errno("unable to open '%s'", todo_path); |
2926 | 0 | return 0; |
2927 | 0 | } |
2928 | 0 | } |
2929 | | /* If there is only one line then we are done */ |
2930 | 0 | eol = strchr(buf.buf, '\n'); |
2931 | 0 | if (!eol || !eol[1]) |
2932 | 0 | ret = 1; |
2933 | |
|
2934 | 0 | strbuf_release(&buf); |
2935 | |
|
2936 | 0 | return ret; |
2937 | 0 | } |
2938 | | |
2939 | | void sequencer_post_commit_cleanup(struct repository *r, int verbose) |
2940 | 0 | { |
2941 | 0 | struct replay_opts opts = REPLAY_OPTS_INIT; |
2942 | 0 | int need_cleanup = 0; |
2943 | |
|
2944 | 0 | if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD")) { |
2945 | 0 | if (!refs_delete_ref(get_main_ref_store(r), "", |
2946 | 0 | "CHERRY_PICK_HEAD", NULL, REF_NO_DEREF) && |
2947 | 0 | verbose) |
2948 | 0 | warning(_("cancelling a cherry picking in progress")); |
2949 | 0 | opts.action = REPLAY_PICK; |
2950 | 0 | need_cleanup = 1; |
2951 | 0 | } |
2952 | |
|
2953 | 0 | if (refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD")) { |
2954 | 0 | if (!refs_delete_ref(get_main_ref_store(r), "", "REVERT_HEAD", |
2955 | 0 | NULL, REF_NO_DEREF) && |
2956 | 0 | verbose) |
2957 | 0 | warning(_("cancelling a revert in progress")); |
2958 | 0 | opts.action = REPLAY_REVERT; |
2959 | 0 | need_cleanup = 1; |
2960 | 0 | } |
2961 | |
|
2962 | 0 | refs_delete_ref(get_main_ref_store(r), "", "AUTO_MERGE", |
2963 | 0 | NULL, REF_NO_DEREF); |
2964 | |
|
2965 | 0 | if (!need_cleanup) |
2966 | 0 | goto out; |
2967 | | |
2968 | 0 | if (!have_finished_the_last_pick()) |
2969 | 0 | goto out; |
2970 | | |
2971 | 0 | sequencer_remove_state(&opts); |
2972 | 0 | out: |
2973 | 0 | replay_opts_release(&opts); |
2974 | 0 | } |
2975 | | |
2976 | | static void todo_list_write_total_nr(struct todo_list *todo_list) |
2977 | 0 | { |
2978 | 0 | FILE *f = fopen_or_warn(rebase_path_msgtotal(), "w"); |
2979 | |
|
2980 | 0 | if (f) { |
2981 | 0 | fprintf(f, "%d\n", todo_list->total_nr); |
2982 | 0 | fclose(f); |
2983 | 0 | } |
2984 | 0 | } |
2985 | | |
2986 | | static int read_populate_todo(struct repository *r, |
2987 | | struct todo_list *todo_list, |
2988 | | struct replay_opts *opts) |
2989 | 0 | { |
2990 | 0 | const char *todo_file = get_todo_path(opts); |
2991 | 0 | int res; |
2992 | |
|
2993 | 0 | strbuf_reset(&todo_list->buf); |
2994 | 0 | if (strbuf_read_file_or_whine(&todo_list->buf, todo_file) < 0) |
2995 | 0 | return -1; |
2996 | | |
2997 | 0 | res = todo_list_parse_insn_buffer(r, opts, todo_list->buf.buf, todo_list); |
2998 | 0 | if (res) { |
2999 | 0 | if (is_rebase_i(opts)) |
3000 | 0 | return error(_("please fix this using " |
3001 | 0 | "'git rebase --edit-todo'.")); |
3002 | 0 | return error(_("unusable instruction sheet: '%s'"), todo_file); |
3003 | 0 | } |
3004 | | |
3005 | 0 | if (!todo_list->nr && |
3006 | 0 | (!is_rebase_i(opts) || !file_exists(rebase_path_done()))) |
3007 | 0 | return error(_("no commits parsed.")); |
3008 | | |
3009 | 0 | if (!is_rebase_i(opts)) { |
3010 | 0 | enum todo_command valid = |
3011 | 0 | opts->action == REPLAY_PICK ? TODO_PICK : TODO_REVERT; |
3012 | 0 | int i; |
3013 | |
|
3014 | 0 | for (i = 0; i < todo_list->nr; i++) |
3015 | 0 | if (valid == todo_list->items[i].command) |
3016 | 0 | continue; |
3017 | 0 | else if (valid == TODO_PICK) |
3018 | 0 | return error(_("cannot cherry-pick during a revert.")); |
3019 | 0 | else |
3020 | 0 | return error(_("cannot revert during a cherry-pick.")); |
3021 | 0 | } |
3022 | | |
3023 | 0 | if (is_rebase_i(opts)) { |
3024 | 0 | struct todo_list done = TODO_LIST_INIT; |
3025 | |
|
3026 | 0 | if (strbuf_read_file(&done.buf, rebase_path_done(), 0) > 0 && |
3027 | 0 | !todo_list_parse_insn_buffer(r, opts, done.buf.buf, &done)) |
3028 | 0 | todo_list->done_nr = count_commands(&done); |
3029 | 0 | else |
3030 | 0 | todo_list->done_nr = 0; |
3031 | |
|
3032 | 0 | todo_list->total_nr = todo_list->done_nr |
3033 | 0 | + count_commands(todo_list); |
3034 | 0 | todo_list_release(&done); |
3035 | |
|
3036 | 0 | todo_list_write_total_nr(todo_list); |
3037 | 0 | } |
3038 | |
|
3039 | 0 | return 0; |
3040 | 0 | } |
3041 | | |
3042 | | static int git_config_string_dup(char **dest, |
3043 | | const char *var, const char *value) |
3044 | 0 | { |
3045 | 0 | if (!value) |
3046 | 0 | return config_error_nonbool(var); |
3047 | 0 | free(*dest); |
3048 | 0 | *dest = xstrdup(value); |
3049 | 0 | return 0; |
3050 | 0 | } |
3051 | | |
3052 | | static int populate_opts_cb(const char *key, const char *value, |
3053 | | const struct config_context *ctx, |
3054 | | void *data) |
3055 | 0 | { |
3056 | 0 | struct replay_opts *opts = data; |
3057 | 0 | int error_flag = 1; |
3058 | |
|
3059 | 0 | if (!value) |
3060 | 0 | error_flag = 0; |
3061 | 0 | else if (!strcmp(key, "options.no-commit")) |
3062 | 0 | opts->no_commit = git_config_bool_or_int(key, value, ctx->kvi, &error_flag); |
3063 | 0 | else if (!strcmp(key, "options.edit")) |
3064 | 0 | opts->edit = git_config_bool_or_int(key, value, ctx->kvi, &error_flag); |
3065 | 0 | else if (!strcmp(key, "options.allow-empty")) |
3066 | 0 | opts->allow_empty = |
3067 | 0 | git_config_bool_or_int(key, value, ctx->kvi, &error_flag); |
3068 | 0 | else if (!strcmp(key, "options.allow-empty-message")) |
3069 | 0 | opts->allow_empty_message = |
3070 | 0 | git_config_bool_or_int(key, value, ctx->kvi, &error_flag); |
3071 | 0 | else if (!strcmp(key, "options.drop-redundant-commits")) |
3072 | 0 | opts->drop_redundant_commits = |
3073 | 0 | git_config_bool_or_int(key, value, ctx->kvi, &error_flag); |
3074 | 0 | else if (!strcmp(key, "options.keep-redundant-commits")) |
3075 | 0 | opts->keep_redundant_commits = |
3076 | 0 | git_config_bool_or_int(key, value, ctx->kvi, &error_flag); |
3077 | 0 | else if (!strcmp(key, "options.signoff")) |
3078 | 0 | opts->signoff = git_config_bool_or_int(key, value, ctx->kvi, &error_flag); |
3079 | 0 | else if (!strcmp(key, "options.record-origin")) |
3080 | 0 | opts->record_origin = git_config_bool_or_int(key, value, ctx->kvi, &error_flag); |
3081 | 0 | else if (!strcmp(key, "options.allow-ff")) |
3082 | 0 | opts->allow_ff = git_config_bool_or_int(key, value, ctx->kvi, &error_flag); |
3083 | 0 | else if (!strcmp(key, "options.mainline")) |
3084 | 0 | opts->mainline = git_config_int(key, value, ctx->kvi); |
3085 | 0 | else if (!strcmp(key, "options.strategy")) |
3086 | 0 | git_config_string_dup(&opts->strategy, key, value); |
3087 | 0 | else if (!strcmp(key, "options.gpg-sign")) |
3088 | 0 | git_config_string_dup(&opts->gpg_sign, key, value); |
3089 | 0 | else if (!strcmp(key, "options.strategy-option")) { |
3090 | 0 | strvec_push(&opts->xopts, value); |
3091 | 0 | } else if (!strcmp(key, "options.allow-rerere-auto")) |
3092 | 0 | opts->allow_rerere_auto = |
3093 | 0 | git_config_bool_or_int(key, value, ctx->kvi, &error_flag) ? |
3094 | 0 | RERERE_AUTOUPDATE : RERERE_NOAUTOUPDATE; |
3095 | 0 | else if (!strcmp(key, "options.default-msg-cleanup")) { |
3096 | 0 | opts->explicit_cleanup = 1; |
3097 | 0 | opts->default_msg_cleanup = get_cleanup_mode(value, 1); |
3098 | 0 | } else |
3099 | 0 | return error(_("invalid key: %s"), key); |
3100 | | |
3101 | 0 | if (!error_flag) |
3102 | 0 | return error(_("invalid value for '%s': '%s'"), key, value); |
3103 | | |
3104 | 0 | return 0; |
3105 | 0 | } |
3106 | | |
3107 | | static void parse_strategy_opts(struct replay_opts *opts, char *raw_opts) |
3108 | 0 | { |
3109 | 0 | int i; |
3110 | 0 | int count; |
3111 | 0 | const char **argv; |
3112 | 0 | char *strategy_opts_string = raw_opts; |
3113 | |
|
3114 | 0 | if (*strategy_opts_string == ' ') |
3115 | 0 | strategy_opts_string++; |
3116 | |
|
3117 | 0 | count = split_cmdline(strategy_opts_string, &argv); |
3118 | 0 | if (count < 0) |
3119 | 0 | BUG("could not split '%s': %s", strategy_opts_string, |
3120 | 0 | split_cmdline_strerror(count)); |
3121 | 0 | for (i = 0; i < count; i++) { |
3122 | 0 | const char *arg = argv[i]; |
3123 | |
|
3124 | 0 | skip_prefix(arg, "--", &arg); |
3125 | 0 | strvec_push(&opts->xopts, arg); |
3126 | 0 | } |
3127 | 0 | free(argv); |
3128 | 0 | } |
3129 | | |
3130 | | static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf) |
3131 | 0 | { |
3132 | 0 | strbuf_reset(buf); |
3133 | 0 | if (!read_oneliner(buf, rebase_path_strategy(), 0)) |
3134 | 0 | return; |
3135 | 0 | opts->strategy = strbuf_detach(buf, NULL); |
3136 | 0 | if (!read_oneliner(buf, rebase_path_strategy_opts(), 0)) |
3137 | 0 | return; |
3138 | | |
3139 | 0 | parse_strategy_opts(opts, buf->buf); |
3140 | 0 | } |
3141 | | |
3142 | | static int read_populate_opts(struct replay_opts *opts) |
3143 | 0 | { |
3144 | 0 | struct replay_ctx *ctx = opts->ctx; |
3145 | |
|
3146 | 0 | if (is_rebase_i(opts)) { |
3147 | 0 | struct strbuf buf = STRBUF_INIT; |
3148 | 0 | int ret = 0; |
3149 | |
|
3150 | 0 | if (read_oneliner(&buf, rebase_path_gpg_sign_opt(), |
3151 | 0 | READ_ONELINER_SKIP_IF_EMPTY)) { |
3152 | 0 | if (!starts_with(buf.buf, "-S")) |
3153 | 0 | strbuf_reset(&buf); |
3154 | 0 | else { |
3155 | 0 | free(opts->gpg_sign); |
3156 | 0 | opts->gpg_sign = xstrdup(buf.buf + 2); |
3157 | 0 | } |
3158 | 0 | strbuf_reset(&buf); |
3159 | 0 | } |
3160 | |
|
3161 | 0 | if (read_oneliner(&buf, rebase_path_allow_rerere_autoupdate(), |
3162 | 0 | READ_ONELINER_SKIP_IF_EMPTY)) { |
3163 | 0 | if (!strcmp(buf.buf, "--rerere-autoupdate")) |
3164 | 0 | opts->allow_rerere_auto = RERERE_AUTOUPDATE; |
3165 | 0 | else if (!strcmp(buf.buf, "--no-rerere-autoupdate")) |
3166 | 0 | opts->allow_rerere_auto = RERERE_NOAUTOUPDATE; |
3167 | 0 | strbuf_reset(&buf); |
3168 | 0 | } |
3169 | |
|
3170 | 0 | if (file_exists(rebase_path_verbose())) |
3171 | 0 | opts->verbose = 1; |
3172 | |
|
3173 | 0 | if (file_exists(rebase_path_quiet())) |
3174 | 0 | opts->quiet = 1; |
3175 | |
|
3176 | 0 | if (file_exists(rebase_path_signoff())) { |
3177 | 0 | opts->allow_ff = 0; |
3178 | 0 | opts->signoff = 1; |
3179 | 0 | } |
3180 | |
|
3181 | 0 | if (file_exists(rebase_path_cdate_is_adate())) { |
3182 | 0 | opts->allow_ff = 0; |
3183 | 0 | opts->committer_date_is_author_date = 1; |
3184 | 0 | } |
3185 | |
|
3186 | 0 | if (file_exists(rebase_path_ignore_date())) { |
3187 | 0 | opts->allow_ff = 0; |
3188 | 0 | opts->ignore_date = 1; |
3189 | 0 | } |
3190 | |
|
3191 | 0 | if (file_exists(rebase_path_reschedule_failed_exec())) |
3192 | 0 | opts->reschedule_failed_exec = 1; |
3193 | 0 | else if (file_exists(rebase_path_no_reschedule_failed_exec())) |
3194 | 0 | opts->reschedule_failed_exec = 0; |
3195 | |
|
3196 | 0 | if (file_exists(rebase_path_drop_redundant_commits())) |
3197 | 0 | opts->drop_redundant_commits = 1; |
3198 | |
|
3199 | 0 | if (file_exists(rebase_path_keep_redundant_commits())) |
3200 | 0 | opts->keep_redundant_commits = 1; |
3201 | |
|
3202 | 0 | read_strategy_opts(opts, &buf); |
3203 | 0 | strbuf_reset(&buf); |
3204 | |
|
3205 | 0 | if (read_oneliner(&ctx->current_fixups, |
3206 | 0 | rebase_path_current_fixups(), |
3207 | 0 | READ_ONELINER_SKIP_IF_EMPTY)) { |
3208 | 0 | const char *p = ctx->current_fixups.buf; |
3209 | 0 | ctx->current_fixup_count = 1; |
3210 | 0 | while ((p = strchr(p, '\n'))) { |
3211 | 0 | ctx->current_fixup_count++; |
3212 | 0 | p++; |
3213 | 0 | } |
3214 | 0 | } |
3215 | |
|
3216 | 0 | if (read_oneliner(&buf, rebase_path_squash_onto(), 0)) { |
3217 | 0 | if (repo_get_oid_committish(the_repository, buf.buf, &opts->squash_onto) < 0) { |
3218 | 0 | ret = error(_("unusable squash-onto")); |
3219 | 0 | goto done_rebase_i; |
3220 | 0 | } |
3221 | 0 | opts->have_squash_onto = 1; |
3222 | 0 | } |
3223 | | |
3224 | 0 | done_rebase_i: |
3225 | 0 | strbuf_release(&buf); |
3226 | 0 | return ret; |
3227 | 0 | } |
3228 | | |
3229 | 0 | if (!file_exists(git_path_opts_file())) |
3230 | 0 | return 0; |
3231 | | /* |
3232 | | * The function git_parse_source(), called from git_config_from_file(), |
3233 | | * may die() in case of a syntactically incorrect file. We do not care |
3234 | | * about this case, though, because we wrote that file ourselves, so we |
3235 | | * are pretty certain that it is syntactically correct. |
3236 | | */ |
3237 | 0 | if (git_config_from_file(populate_opts_cb, git_path_opts_file(), opts) < 0) |
3238 | 0 | return error(_("malformed options sheet: '%s'"), |
3239 | 0 | git_path_opts_file()); |
3240 | 0 | return 0; |
3241 | 0 | } |
3242 | | |
3243 | | static void write_strategy_opts(struct replay_opts *opts) |
3244 | 0 | { |
3245 | 0 | struct strbuf buf = STRBUF_INIT; |
3246 | | |
3247 | | /* |
3248 | | * Quote strategy options so that they can be read correctly |
3249 | | * by split_cmdline(). |
3250 | | */ |
3251 | 0 | quote_cmdline(&buf, opts->xopts.v); |
3252 | 0 | write_file(rebase_path_strategy_opts(), "%s\n", buf.buf); |
3253 | 0 | strbuf_release(&buf); |
3254 | 0 | } |
3255 | | |
3256 | | int write_basic_state(struct replay_opts *opts, const char *head_name, |
3257 | | struct commit *onto, const struct object_id *orig_head) |
3258 | 0 | { |
3259 | 0 | if (head_name) |
3260 | 0 | write_file(rebase_path_head_name(), "%s\n", head_name); |
3261 | 0 | if (onto) |
3262 | 0 | write_file(rebase_path_onto(), "%s\n", |
3263 | 0 | oid_to_hex(&onto->object.oid)); |
3264 | 0 | if (orig_head) |
3265 | 0 | write_file(rebase_path_orig_head(), "%s\n", |
3266 | 0 | oid_to_hex(orig_head)); |
3267 | |
|
3268 | 0 | if (opts->quiet) |
3269 | 0 | write_file(rebase_path_quiet(), "%s", ""); |
3270 | 0 | if (opts->verbose) |
3271 | 0 | write_file(rebase_path_verbose(), "%s", ""); |
3272 | 0 | if (opts->strategy) |
3273 | 0 | write_file(rebase_path_strategy(), "%s\n", opts->strategy); |
3274 | 0 | if (opts->xopts.nr > 0) |
3275 | 0 | write_strategy_opts(opts); |
3276 | |
|
3277 | 0 | if (opts->allow_rerere_auto == RERERE_AUTOUPDATE) |
3278 | 0 | write_file(rebase_path_allow_rerere_autoupdate(), "--rerere-autoupdate\n"); |
3279 | 0 | else if (opts->allow_rerere_auto == RERERE_NOAUTOUPDATE) |
3280 | 0 | write_file(rebase_path_allow_rerere_autoupdate(), "--no-rerere-autoupdate\n"); |
3281 | |
|
3282 | 0 | if (opts->gpg_sign) |
3283 | 0 | write_file(rebase_path_gpg_sign_opt(), "-S%s\n", opts->gpg_sign); |
3284 | 0 | if (opts->signoff) |
3285 | 0 | write_file(rebase_path_signoff(), "--signoff\n"); |
3286 | 0 | if (opts->drop_redundant_commits) |
3287 | 0 | write_file(rebase_path_drop_redundant_commits(), "%s", ""); |
3288 | 0 | if (opts->keep_redundant_commits) |
3289 | 0 | write_file(rebase_path_keep_redundant_commits(), "%s", ""); |
3290 | 0 | if (opts->committer_date_is_author_date) |
3291 | 0 | write_file(rebase_path_cdate_is_adate(), "%s", ""); |
3292 | 0 | if (opts->ignore_date) |
3293 | 0 | write_file(rebase_path_ignore_date(), "%s", ""); |
3294 | 0 | if (opts->reschedule_failed_exec) |
3295 | 0 | write_file(rebase_path_reschedule_failed_exec(), "%s", ""); |
3296 | 0 | else |
3297 | 0 | write_file(rebase_path_no_reschedule_failed_exec(), "%s", ""); |
3298 | |
|
3299 | 0 | return 0; |
3300 | 0 | } |
3301 | | |
3302 | | static int walk_revs_populate_todo(struct todo_list *todo_list, |
3303 | | struct replay_opts *opts) |
3304 | 0 | { |
3305 | 0 | enum todo_command command = opts->action == REPLAY_PICK ? |
3306 | 0 | TODO_PICK : TODO_REVERT; |
3307 | 0 | const char *command_string = todo_command_info[command].str; |
3308 | 0 | const char *encoding; |
3309 | 0 | struct commit *commit; |
3310 | |
|
3311 | 0 | if (prepare_revs(opts)) |
3312 | 0 | return -1; |
3313 | | |
3314 | 0 | encoding = get_log_output_encoding(); |
3315 | |
|
3316 | 0 | while ((commit = get_revision(opts->revs))) { |
3317 | 0 | struct todo_item *item = append_new_todo(todo_list); |
3318 | 0 | const char *commit_buffer = repo_logmsg_reencode(the_repository, |
3319 | 0 | commit, NULL, |
3320 | 0 | encoding); |
3321 | 0 | const char *subject; |
3322 | 0 | int subject_len; |
3323 | |
|
3324 | 0 | item->command = command; |
3325 | 0 | item->commit = commit; |
3326 | 0 | item->arg_offset = 0; |
3327 | 0 | item->arg_len = 0; |
3328 | 0 | item->offset_in_buf = todo_list->buf.len; |
3329 | 0 | subject_len = find_commit_subject(commit_buffer, &subject); |
3330 | 0 | strbuf_addf(&todo_list->buf, "%s %s %.*s\n", command_string, |
3331 | 0 | short_commit_name(the_repository, commit), |
3332 | 0 | subject_len, subject); |
3333 | 0 | repo_unuse_commit_buffer(the_repository, commit, |
3334 | 0 | commit_buffer); |
3335 | 0 | } |
3336 | |
|
3337 | 0 | if (!todo_list->nr) |
3338 | 0 | return error(_("empty commit set passed")); |
3339 | | |
3340 | 0 | return 0; |
3341 | 0 | } |
3342 | | |
3343 | | static int create_seq_dir(struct repository *r) |
3344 | 0 | { |
3345 | 0 | enum replay_action action; |
3346 | 0 | const char *in_progress_error = NULL; |
3347 | 0 | const char *in_progress_advice = NULL; |
3348 | 0 | unsigned int advise_skip = |
3349 | 0 | refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD") || |
3350 | 0 | refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD"); |
3351 | |
|
3352 | 0 | if (!sequencer_get_last_command(r, &action)) { |
3353 | 0 | switch (action) { |
3354 | 0 | case REPLAY_REVERT: |
3355 | 0 | in_progress_error = _("revert is already in progress"); |
3356 | 0 | in_progress_advice = |
3357 | 0 | _("try \"git revert (--continue | %s--abort | --quit)\""); |
3358 | 0 | break; |
3359 | 0 | case REPLAY_PICK: |
3360 | 0 | in_progress_error = _("cherry-pick is already in progress"); |
3361 | 0 | in_progress_advice = |
3362 | 0 | _("try \"git cherry-pick (--continue | %s--abort | --quit)\""); |
3363 | 0 | break; |
3364 | 0 | default: |
3365 | 0 | BUG("unexpected action in create_seq_dir"); |
3366 | 0 | } |
3367 | 0 | } |
3368 | 0 | if (in_progress_error) { |
3369 | 0 | error("%s", in_progress_error); |
3370 | 0 | if (advice_enabled(ADVICE_SEQUENCER_IN_USE)) |
3371 | 0 | advise(in_progress_advice, |
3372 | 0 | advise_skip ? "--skip | " : ""); |
3373 | 0 | return -1; |
3374 | 0 | } |
3375 | 0 | if (mkdir(git_path_seq_dir(), 0777) < 0) |
3376 | 0 | return error_errno(_("could not create sequencer directory '%s'"), |
3377 | 0 | git_path_seq_dir()); |
3378 | | |
3379 | 0 | return 0; |
3380 | 0 | } |
3381 | | |
3382 | | static int save_head(const char *head) |
3383 | 0 | { |
3384 | 0 | return write_message(head, strlen(head), git_path_head_file(), 1); |
3385 | 0 | } |
3386 | | |
3387 | | static int rollback_is_safe(void) |
3388 | 0 | { |
3389 | 0 | struct strbuf sb = STRBUF_INIT; |
3390 | 0 | struct object_id expected_head, actual_head; |
3391 | |
|
3392 | 0 | if (strbuf_read_file(&sb, git_path_abort_safety_file(), 0) >= 0) { |
3393 | 0 | strbuf_trim(&sb); |
3394 | 0 | if (get_oid_hex(sb.buf, &expected_head)) { |
3395 | 0 | strbuf_release(&sb); |
3396 | 0 | die(_("could not parse %s"), git_path_abort_safety_file()); |
3397 | 0 | } |
3398 | 0 | strbuf_release(&sb); |
3399 | 0 | } |
3400 | 0 | else if (errno == ENOENT) |
3401 | 0 | oidclr(&expected_head, the_repository->hash_algo); |
3402 | 0 | else |
3403 | 0 | die_errno(_("could not read '%s'"), git_path_abort_safety_file()); |
3404 | | |
3405 | 0 | if (repo_get_oid(the_repository, "HEAD", &actual_head)) |
3406 | 0 | oidclr(&actual_head, the_repository->hash_algo); |
3407 | |
|
3408 | 0 | return oideq(&actual_head, &expected_head); |
3409 | 0 | } |
3410 | | |
3411 | | static int reset_merge(const struct object_id *oid) |
3412 | 0 | { |
3413 | 0 | struct child_process cmd = CHILD_PROCESS_INIT; |
3414 | |
|
3415 | 0 | cmd.git_cmd = 1; |
3416 | 0 | strvec_pushl(&cmd.args, "reset", "--merge", NULL); |
3417 | |
|
3418 | 0 | if (!is_null_oid(oid)) |
3419 | 0 | strvec_push(&cmd.args, oid_to_hex(oid)); |
3420 | |
|
3421 | 0 | return run_command(&cmd); |
3422 | 0 | } |
3423 | | |
3424 | | static int rollback_single_pick(struct repository *r) |
3425 | 0 | { |
3426 | 0 | struct object_id head_oid; |
3427 | |
|
3428 | 0 | if (!refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") && |
3429 | 0 | !refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD")) |
3430 | 0 | return error(_("no cherry-pick or revert in progress")); |
3431 | 0 | if (refs_read_ref_full(get_main_ref_store(the_repository), "HEAD", 0, &head_oid, NULL)) |
3432 | 0 | return error(_("cannot resolve HEAD")); |
3433 | 0 | if (is_null_oid(&head_oid)) |
3434 | 0 | return error(_("cannot abort from a branch yet to be born")); |
3435 | 0 | return reset_merge(&head_oid); |
3436 | 0 | } |
3437 | | |
3438 | | static int skip_single_pick(void) |
3439 | 0 | { |
3440 | 0 | struct object_id head; |
3441 | |
|
3442 | 0 | if (refs_read_ref_full(get_main_ref_store(the_repository), "HEAD", 0, &head, NULL)) |
3443 | 0 | return error(_("cannot resolve HEAD")); |
3444 | 0 | return reset_merge(&head); |
3445 | 0 | } |
3446 | | |
3447 | | int sequencer_rollback(struct repository *r, struct replay_opts *opts) |
3448 | 0 | { |
3449 | 0 | FILE *f; |
3450 | 0 | struct object_id oid; |
3451 | 0 | struct strbuf buf = STRBUF_INIT; |
3452 | 0 | const char *p; |
3453 | |
|
3454 | 0 | f = fopen(git_path_head_file(), "r"); |
3455 | 0 | if (!f && errno == ENOENT) { |
3456 | | /* |
3457 | | * There is no multiple-cherry-pick in progress. |
3458 | | * If CHERRY_PICK_HEAD or REVERT_HEAD indicates |
3459 | | * a single-cherry-pick in progress, abort that. |
3460 | | */ |
3461 | 0 | return rollback_single_pick(r); |
3462 | 0 | } |
3463 | 0 | if (!f) |
3464 | 0 | return error_errno(_("cannot open '%s'"), git_path_head_file()); |
3465 | 0 | if (strbuf_getline_lf(&buf, f)) { |
3466 | 0 | error(_("cannot read '%s': %s"), git_path_head_file(), |
3467 | 0 | ferror(f) ? strerror(errno) : _("unexpected end of file")); |
3468 | 0 | fclose(f); |
3469 | 0 | goto fail; |
3470 | 0 | } |
3471 | 0 | fclose(f); |
3472 | 0 | if (parse_oid_hex(buf.buf, &oid, &p) || *p != '\0') { |
3473 | 0 | error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"), |
3474 | 0 | git_path_head_file()); |
3475 | 0 | goto fail; |
3476 | 0 | } |
3477 | 0 | if (is_null_oid(&oid)) { |
3478 | 0 | error(_("cannot abort from a branch yet to be born")); |
3479 | 0 | goto fail; |
3480 | 0 | } |
3481 | | |
3482 | 0 | if (!rollback_is_safe()) { |
3483 | | /* Do not error, just do not rollback */ |
3484 | 0 | warning(_("You seem to have moved HEAD. " |
3485 | 0 | "Not rewinding, check your HEAD!")); |
3486 | 0 | } else |
3487 | 0 | if (reset_merge(&oid)) |
3488 | 0 | goto fail; |
3489 | 0 | strbuf_release(&buf); |
3490 | 0 | return sequencer_remove_state(opts); |
3491 | 0 | fail: |
3492 | 0 | strbuf_release(&buf); |
3493 | 0 | return -1; |
3494 | 0 | } |
3495 | | |
3496 | | int sequencer_skip(struct repository *r, struct replay_opts *opts) |
3497 | 0 | { |
3498 | 0 | enum replay_action action = -1; |
3499 | 0 | sequencer_get_last_command(r, &action); |
3500 | | |
3501 | | /* |
3502 | | * Check whether the subcommand requested to skip the commit is actually |
3503 | | * in progress and that it's safe to skip the commit. |
3504 | | * |
3505 | | * opts->action tells us which subcommand requested to skip the commit. |
3506 | | * If the corresponding .git/<ACTION>_HEAD exists, we know that the |
3507 | | * action is in progress and we can skip the commit. |
3508 | | * |
3509 | | * Otherwise we check that the last instruction was related to the |
3510 | | * particular subcommand we're trying to execute and barf if that's not |
3511 | | * the case. |
3512 | | * |
3513 | | * Finally we check that the rollback is "safe", i.e., has the HEAD |
3514 | | * moved? In this case, it doesn't make sense to "reset the merge" and |
3515 | | * "skip the commit" as the user already handled this by committing. But |
3516 | | * we'd not want to barf here, instead give advice on how to proceed. We |
3517 | | * only need to check that when .git/<ACTION>_HEAD doesn't exist because |
3518 | | * it gets removed when the user commits, so if it still exists we're |
3519 | | * sure the user can't have committed before. |
3520 | | */ |
3521 | 0 | switch (opts->action) { |
3522 | 0 | case REPLAY_REVERT: |
3523 | 0 | if (!refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD")) { |
3524 | 0 | if (action != REPLAY_REVERT) |
3525 | 0 | return error(_("no revert in progress")); |
3526 | 0 | if (!rollback_is_safe()) |
3527 | 0 | goto give_advice; |
3528 | 0 | } |
3529 | 0 | break; |
3530 | 0 | case REPLAY_PICK: |
3531 | 0 | if (!refs_ref_exists(get_main_ref_store(r), |
3532 | 0 | "CHERRY_PICK_HEAD")) { |
3533 | 0 | if (action != REPLAY_PICK) |
3534 | 0 | return error(_("no cherry-pick in progress")); |
3535 | 0 | if (!rollback_is_safe()) |
3536 | 0 | goto give_advice; |
3537 | 0 | } |
3538 | 0 | break; |
3539 | 0 | default: |
3540 | 0 | BUG("unexpected action in sequencer_skip"); |
3541 | 0 | } |
3542 | | |
3543 | 0 | if (skip_single_pick()) |
3544 | 0 | return error(_("failed to skip the commit")); |
3545 | 0 | if (!is_directory(git_path_seq_dir())) |
3546 | 0 | return 0; |
3547 | | |
3548 | 0 | return sequencer_continue(r, opts); |
3549 | | |
3550 | 0 | give_advice: |
3551 | 0 | error(_("there is nothing to skip")); |
3552 | |
|
3553 | 0 | if (advice_enabled(ADVICE_RESOLVE_CONFLICT)) { |
3554 | 0 | advise(_("have you committed already?\n" |
3555 | 0 | "try \"git %s --continue\""), |
3556 | 0 | action == REPLAY_REVERT ? "revert" : "cherry-pick"); |
3557 | 0 | } |
3558 | 0 | return -1; |
3559 | 0 | } |
3560 | | |
3561 | | static int save_todo(struct todo_list *todo_list, struct replay_opts *opts, |
3562 | | int reschedule) |
3563 | 0 | { |
3564 | 0 | struct lock_file todo_lock = LOCK_INIT; |
3565 | 0 | const char *todo_path = get_todo_path(opts); |
3566 | 0 | int next = todo_list->current, offset, fd; |
3567 | | |
3568 | | /* |
3569 | | * rebase -i writes "git-rebase-todo" without the currently executing |
3570 | | * command, appending it to "done" instead. |
3571 | | */ |
3572 | 0 | if (is_rebase_i(opts) && !reschedule) |
3573 | 0 | next++; |
3574 | |
|
3575 | 0 | fd = hold_lock_file_for_update(&todo_lock, todo_path, 0); |
3576 | 0 | if (fd < 0) |
3577 | 0 | return error_errno(_("could not lock '%s'"), todo_path); |
3578 | 0 | offset = get_item_line_offset(todo_list, next); |
3579 | 0 | if (write_in_full(fd, todo_list->buf.buf + offset, |
3580 | 0 | todo_list->buf.len - offset) < 0) |
3581 | 0 | return error_errno(_("could not write to '%s'"), todo_path); |
3582 | 0 | if (commit_lock_file(&todo_lock) < 0) |
3583 | 0 | return error(_("failed to finalize '%s'"), todo_path); |
3584 | | |
3585 | 0 | if (is_rebase_i(opts) && !reschedule && next > 0) { |
3586 | 0 | const char *done = rebase_path_done(); |
3587 | 0 | int fd = open(done, O_CREAT | O_WRONLY | O_APPEND, 0666); |
3588 | 0 | int ret = 0; |
3589 | |
|
3590 | 0 | if (fd < 0) |
3591 | 0 | return 0; |
3592 | 0 | if (write_in_full(fd, get_item_line(todo_list, next - 1), |
3593 | 0 | get_item_line_length(todo_list, next - 1)) |
3594 | 0 | < 0) |
3595 | 0 | ret = error_errno(_("could not write to '%s'"), done); |
3596 | 0 | if (close(fd) < 0) |
3597 | 0 | ret = error_errno(_("failed to finalize '%s'"), done); |
3598 | 0 | return ret; |
3599 | 0 | } |
3600 | 0 | return 0; |
3601 | 0 | } |
3602 | | |
3603 | | static int save_opts(struct replay_opts *opts) |
3604 | 0 | { |
3605 | 0 | const char *opts_file = git_path_opts_file(); |
3606 | 0 | int res = 0; |
3607 | |
|
3608 | 0 | if (opts->no_commit) |
3609 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3610 | 0 | "options.no-commit", NULL, "true"); |
3611 | 0 | if (opts->edit >= 0) |
3612 | 0 | res |= git_config_set_in_file_gently(opts_file, "options.edit", NULL, |
3613 | 0 | opts->edit ? "true" : "false"); |
3614 | 0 | if (opts->allow_empty) |
3615 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3616 | 0 | "options.allow-empty", NULL, "true"); |
3617 | 0 | if (opts->allow_empty_message) |
3618 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3619 | 0 | "options.allow-empty-message", NULL, "true"); |
3620 | 0 | if (opts->drop_redundant_commits) |
3621 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3622 | 0 | "options.drop-redundant-commits", NULL, "true"); |
3623 | 0 | if (opts->keep_redundant_commits) |
3624 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3625 | 0 | "options.keep-redundant-commits", NULL, "true"); |
3626 | 0 | if (opts->signoff) |
3627 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3628 | 0 | "options.signoff", NULL, "true"); |
3629 | 0 | if (opts->record_origin) |
3630 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3631 | 0 | "options.record-origin", NULL, "true"); |
3632 | 0 | if (opts->allow_ff) |
3633 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3634 | 0 | "options.allow-ff", NULL, "true"); |
3635 | 0 | if (opts->mainline) { |
3636 | 0 | struct strbuf buf = STRBUF_INIT; |
3637 | 0 | strbuf_addf(&buf, "%d", opts->mainline); |
3638 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3639 | 0 | "options.mainline", NULL, buf.buf); |
3640 | 0 | strbuf_release(&buf); |
3641 | 0 | } |
3642 | 0 | if (opts->strategy) |
3643 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3644 | 0 | "options.strategy", NULL, opts->strategy); |
3645 | 0 | if (opts->gpg_sign) |
3646 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3647 | 0 | "options.gpg-sign", NULL, opts->gpg_sign); |
3648 | 0 | for (size_t i = 0; i < opts->xopts.nr; i++) |
3649 | 0 | res |= git_config_set_multivar_in_file_gently(opts_file, |
3650 | 0 | "options.strategy-option", |
3651 | 0 | opts->xopts.v[i], "^$", NULL, 0); |
3652 | 0 | if (opts->allow_rerere_auto) |
3653 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3654 | 0 | "options.allow-rerere-auto", NULL, |
3655 | 0 | opts->allow_rerere_auto == RERERE_AUTOUPDATE ? |
3656 | 0 | "true" : "false"); |
3657 | |
|
3658 | 0 | if (opts->explicit_cleanup) |
3659 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3660 | 0 | "options.default-msg-cleanup", NULL, |
3661 | 0 | describe_cleanup_mode(opts->default_msg_cleanup)); |
3662 | 0 | return res; |
3663 | 0 | } |
3664 | | |
3665 | | static int make_patch(struct repository *r, |
3666 | | struct commit *commit, |
3667 | | struct replay_opts *opts) |
3668 | 0 | { |
3669 | 0 | struct rev_info log_tree_opt; |
3670 | 0 | const char *subject; |
3671 | 0 | char hex[GIT_MAX_HEXSZ + 1]; |
3672 | 0 | int res = 0; |
3673 | |
|
3674 | 0 | if (!is_rebase_i(opts)) |
3675 | 0 | BUG("make_patch should only be called when rebasing"); |
3676 | | |
3677 | 0 | oid_to_hex_r(hex, &commit->object.oid); |
3678 | 0 | if (write_message(hex, strlen(hex), rebase_path_stopped_sha(), 1) < 0) |
3679 | 0 | return -1; |
3680 | 0 | res |= write_rebase_head(&commit->object.oid); |
3681 | |
|
3682 | 0 | memset(&log_tree_opt, 0, sizeof(log_tree_opt)); |
3683 | 0 | repo_init_revisions(r, &log_tree_opt, NULL); |
3684 | 0 | log_tree_opt.abbrev = 0; |
3685 | 0 | log_tree_opt.diff = 1; |
3686 | 0 | log_tree_opt.diffopt.output_format = DIFF_FORMAT_PATCH; |
3687 | 0 | log_tree_opt.disable_stdin = 1; |
3688 | 0 | log_tree_opt.no_commit_id = 1; |
3689 | 0 | log_tree_opt.diffopt.file = fopen(rebase_path_patch(), "w"); |
3690 | 0 | log_tree_opt.diffopt.use_color = GIT_COLOR_NEVER; |
3691 | 0 | if (!log_tree_opt.diffopt.file) |
3692 | 0 | res |= error_errno(_("could not open '%s'"), |
3693 | 0 | rebase_path_patch()); |
3694 | 0 | else { |
3695 | 0 | res |= log_tree_commit(&log_tree_opt, commit); |
3696 | 0 | fclose(log_tree_opt.diffopt.file); |
3697 | 0 | } |
3698 | |
|
3699 | 0 | if (!file_exists(rebase_path_message())) { |
3700 | 0 | const char *encoding = get_commit_output_encoding(); |
3701 | 0 | const char *commit_buffer = repo_logmsg_reencode(r, |
3702 | 0 | commit, NULL, |
3703 | 0 | encoding); |
3704 | 0 | find_commit_subject(commit_buffer, &subject); |
3705 | 0 | res |= write_message(subject, strlen(subject), rebase_path_message(), 1); |
3706 | 0 | repo_unuse_commit_buffer(r, commit, |
3707 | 0 | commit_buffer); |
3708 | 0 | } |
3709 | 0 | release_revisions(&log_tree_opt); |
3710 | |
|
3711 | 0 | return res; |
3712 | 0 | } |
3713 | | |
3714 | | static int intend_to_amend(void) |
3715 | 0 | { |
3716 | 0 | struct object_id head; |
3717 | 0 | char *p; |
3718 | |
|
3719 | 0 | if (repo_get_oid(the_repository, "HEAD", &head)) |
3720 | 0 | return error(_("cannot read HEAD")); |
3721 | | |
3722 | 0 | p = oid_to_hex(&head); |
3723 | 0 | return write_message(p, strlen(p), rebase_path_amend(), 1); |
3724 | 0 | } |
3725 | | |
3726 | | static int error_with_patch(struct repository *r, |
3727 | | struct commit *commit, |
3728 | | const char *subject, int subject_len, |
3729 | | struct replay_opts *opts, |
3730 | | int exit_code, int to_amend) |
3731 | 0 | { |
3732 | 0 | struct replay_ctx *ctx = opts->ctx; |
3733 | | |
3734 | | /* |
3735 | | * Write the commit message to be used by "git rebase |
3736 | | * --continue". If a "fixup" or "squash" command has conflicts |
3737 | | * then we will have already written rebase_path_message() in |
3738 | | * error_failed_squash(). If an "edit" command was |
3739 | | * fast-forwarded then we don't have a message in ctx->message |
3740 | | * and rely on make_patch() to write rebase_path_message() |
3741 | | * instead. |
3742 | | */ |
3743 | 0 | if (ctx->have_message && !file_exists(rebase_path_message()) && |
3744 | 0 | write_message(ctx->message.buf, ctx->message.len, |
3745 | 0 | rebase_path_message(), 0)) |
3746 | 0 | return error(_("could not write commit message file")); |
3747 | | |
3748 | 0 | if (commit && make_patch(r, commit, opts)) |
3749 | 0 | return -1; |
3750 | | |
3751 | 0 | if (to_amend) { |
3752 | 0 | if (intend_to_amend()) |
3753 | 0 | return -1; |
3754 | | |
3755 | 0 | fprintf(stderr, |
3756 | 0 | _("You can amend the commit now, with\n" |
3757 | 0 | "\n" |
3758 | 0 | " git commit --amend %s\n" |
3759 | 0 | "\n" |
3760 | 0 | "Once you are satisfied with your changes, run\n" |
3761 | 0 | "\n" |
3762 | 0 | " git rebase --continue\n"), |
3763 | 0 | gpg_sign_opt_quoted(opts)); |
3764 | 0 | } else if (exit_code) { |
3765 | 0 | if (commit) |
3766 | 0 | fprintf_ln(stderr, _("Could not apply %s... %.*s"), |
3767 | 0 | short_commit_name(r, commit), subject_len, subject); |
3768 | 0 | else |
3769 | | /* |
3770 | | * We don't have the hash of the parent so |
3771 | | * just print the line from the todo file. |
3772 | | */ |
3773 | 0 | fprintf_ln(stderr, _("Could not merge %.*s"), |
3774 | 0 | subject_len, subject); |
3775 | 0 | } |
3776 | | |
3777 | 0 | return exit_code; |
3778 | 0 | } |
3779 | | |
3780 | | static int error_failed_squash(struct repository *r, |
3781 | | struct commit *commit, |
3782 | | struct replay_opts *opts, |
3783 | | int subject_len, |
3784 | | const char *subject) |
3785 | 0 | { |
3786 | 0 | if (copy_file(rebase_path_message(), rebase_path_squash_msg(), 0666)) |
3787 | 0 | return error(_("could not copy '%s' to '%s'"), |
3788 | 0 | rebase_path_squash_msg(), rebase_path_message()); |
3789 | 0 | unlink(git_path_merge_msg(r)); |
3790 | 0 | if (copy_file(git_path_merge_msg(r), rebase_path_message(), 0666)) |
3791 | 0 | return error(_("could not copy '%s' to '%s'"), |
3792 | 0 | rebase_path_message(), |
3793 | 0 | git_path_merge_msg(r)); |
3794 | 0 | return error_with_patch(r, commit, subject, subject_len, opts, 1, 0); |
3795 | 0 | } |
3796 | | |
3797 | | static int do_exec(struct repository *r, const char *command_line, int quiet) |
3798 | 0 | { |
3799 | 0 | struct child_process cmd = CHILD_PROCESS_INIT; |
3800 | 0 | int dirty, status; |
3801 | |
|
3802 | 0 | if (!quiet) |
3803 | 0 | fprintf(stderr, _("Executing: %s\n"), command_line); |
3804 | 0 | cmd.use_shell = 1; |
3805 | 0 | strvec_push(&cmd.args, command_line); |
3806 | 0 | strvec_push(&cmd.env, "GIT_CHERRY_PICK_HELP"); |
3807 | 0 | status = run_command(&cmd); |
3808 | | |
3809 | | /* force re-reading of the cache */ |
3810 | 0 | discard_index(r->index); |
3811 | 0 | if (repo_read_index(r) < 0) |
3812 | 0 | return error(_("could not read index")); |
3813 | | |
3814 | 0 | dirty = require_clean_work_tree(r, "rebase", NULL, 1, 1); |
3815 | |
|
3816 | 0 | if (status) { |
3817 | 0 | warning(_("execution failed: %s\n%s" |
3818 | 0 | "You can fix the problem, and then run\n" |
3819 | 0 | "\n" |
3820 | 0 | " git rebase --continue\n" |
3821 | 0 | "\n"), |
3822 | 0 | command_line, |
3823 | 0 | dirty ? _("and made changes to the index and/or the " |
3824 | 0 | "working tree.\n") : ""); |
3825 | 0 | if (status == 127) |
3826 | | /* command not found */ |
3827 | 0 | status = 1; |
3828 | 0 | } else if (dirty) { |
3829 | 0 | warning(_("execution succeeded: %s\nbut " |
3830 | 0 | "left changes to the index and/or the working tree.\n" |
3831 | 0 | "Commit or stash your changes, and then run\n" |
3832 | 0 | "\n" |
3833 | 0 | " git rebase --continue\n" |
3834 | 0 | "\n"), command_line); |
3835 | 0 | status = 1; |
3836 | 0 | } |
3837 | |
|
3838 | 0 | return status; |
3839 | 0 | } |
3840 | | |
3841 | | __attribute__((format (printf, 2, 3))) |
3842 | | static int safe_append(const char *filename, const char *fmt, ...) |
3843 | 0 | { |
3844 | 0 | va_list ap; |
3845 | 0 | struct lock_file lock = LOCK_INIT; |
3846 | 0 | int fd = hold_lock_file_for_update(&lock, filename, |
3847 | 0 | LOCK_REPORT_ON_ERROR); |
3848 | 0 | struct strbuf buf = STRBUF_INIT; |
3849 | |
|
3850 | 0 | if (fd < 0) |
3851 | 0 | return -1; |
3852 | | |
3853 | 0 | if (strbuf_read_file(&buf, filename, 0) < 0 && errno != ENOENT) { |
3854 | 0 | error_errno(_("could not read '%s'"), filename); |
3855 | 0 | rollback_lock_file(&lock); |
3856 | 0 | return -1; |
3857 | 0 | } |
3858 | 0 | strbuf_complete(&buf, '\n'); |
3859 | 0 | va_start(ap, fmt); |
3860 | 0 | strbuf_vaddf(&buf, fmt, ap); |
3861 | 0 | va_end(ap); |
3862 | |
|
3863 | 0 | if (write_in_full(fd, buf.buf, buf.len) < 0) { |
3864 | 0 | error_errno(_("could not write to '%s'"), filename); |
3865 | 0 | strbuf_release(&buf); |
3866 | 0 | rollback_lock_file(&lock); |
3867 | 0 | return -1; |
3868 | 0 | } |
3869 | 0 | if (commit_lock_file(&lock) < 0) { |
3870 | 0 | strbuf_release(&buf); |
3871 | 0 | return error(_("failed to finalize '%s'"), filename); |
3872 | 0 | } |
3873 | | |
3874 | 0 | strbuf_release(&buf); |
3875 | 0 | return 0; |
3876 | 0 | } |
3877 | | |
3878 | | static int do_label(struct repository *r, const char *name, int len) |
3879 | 0 | { |
3880 | 0 | struct ref_store *refs = get_main_ref_store(r); |
3881 | 0 | struct ref_transaction *transaction; |
3882 | 0 | struct strbuf ref_name = STRBUF_INIT, err = STRBUF_INIT; |
3883 | 0 | struct strbuf msg = STRBUF_INIT; |
3884 | 0 | int ret = 0; |
3885 | 0 | struct object_id head_oid; |
3886 | |
|
3887 | 0 | if (len == 1 && *name == '#') |
3888 | 0 | return error(_("illegal label name: '%.*s'"), len, name); |
3889 | | |
3890 | 0 | strbuf_addf(&ref_name, "refs/rewritten/%.*s", len, name); |
3891 | 0 | strbuf_addf(&msg, "rebase (label) '%.*s'", len, name); |
3892 | |
|
3893 | 0 | transaction = ref_store_transaction_begin(refs, &err); |
3894 | 0 | if (!transaction) { |
3895 | 0 | error("%s", err.buf); |
3896 | 0 | ret = -1; |
3897 | 0 | } else if (repo_get_oid(r, "HEAD", &head_oid)) { |
3898 | 0 | error(_("could not read HEAD")); |
3899 | 0 | ret = -1; |
3900 | 0 | } else if (ref_transaction_update(transaction, ref_name.buf, |
3901 | 0 | &head_oid, NULL, NULL, NULL, |
3902 | 0 | 0, msg.buf, &err) < 0 || |
3903 | 0 | ref_transaction_commit(transaction, &err)) { |
3904 | 0 | error("%s", err.buf); |
3905 | 0 | ret = -1; |
3906 | 0 | } |
3907 | 0 | ref_transaction_free(transaction); |
3908 | 0 | strbuf_release(&err); |
3909 | 0 | strbuf_release(&msg); |
3910 | |
|
3911 | 0 | if (!ret) |
3912 | 0 | ret = safe_append(rebase_path_refs_to_delete(), |
3913 | 0 | "%s\n", ref_name.buf); |
3914 | 0 | strbuf_release(&ref_name); |
3915 | |
|
3916 | 0 | return ret; |
3917 | 0 | } |
3918 | | |
3919 | | static const char *sequencer_reflog_action(struct replay_opts *opts) |
3920 | 0 | { |
3921 | 0 | if (!opts->reflog_action) { |
3922 | 0 | opts->reflog_action = getenv(GIT_REFLOG_ACTION); |
3923 | 0 | opts->reflog_action = |
3924 | 0 | xstrdup(opts->reflog_action ? opts->reflog_action |
3925 | 0 | : action_name(opts)); |
3926 | 0 | } |
3927 | |
|
3928 | 0 | return opts->reflog_action; |
3929 | 0 | } |
3930 | | |
3931 | | __attribute__((format (printf, 3, 4))) |
3932 | | static const char *reflog_message(struct replay_opts *opts, |
3933 | | const char *sub_action, const char *fmt, ...) |
3934 | 0 | { |
3935 | 0 | va_list ap; |
3936 | 0 | static struct strbuf buf = STRBUF_INIT; |
3937 | |
|
3938 | 0 | va_start(ap, fmt); |
3939 | 0 | strbuf_reset(&buf); |
3940 | 0 | strbuf_addstr(&buf, sequencer_reflog_action(opts)); |
3941 | 0 | if (sub_action) |
3942 | 0 | strbuf_addf(&buf, " (%s)", sub_action); |
3943 | 0 | if (fmt) { |
3944 | 0 | strbuf_addstr(&buf, ": "); |
3945 | 0 | strbuf_vaddf(&buf, fmt, ap); |
3946 | 0 | } |
3947 | 0 | va_end(ap); |
3948 | |
|
3949 | 0 | return buf.buf; |
3950 | 0 | } |
3951 | | |
3952 | | static struct commit *lookup_label(struct repository *r, const char *label, |
3953 | | int len, struct strbuf *buf) |
3954 | 0 | { |
3955 | 0 | struct commit *commit; |
3956 | 0 | struct object_id oid; |
3957 | |
|
3958 | 0 | strbuf_reset(buf); |
3959 | 0 | strbuf_addf(buf, "refs/rewritten/%.*s", len, label); |
3960 | 0 | if (!refs_read_ref(get_main_ref_store(the_repository), buf->buf, &oid)) { |
3961 | 0 | commit = lookup_commit_object(r, &oid); |
3962 | 0 | } else { |
3963 | | /* fall back to non-rewritten ref or commit */ |
3964 | 0 | strbuf_splice(buf, 0, strlen("refs/rewritten/"), "", 0); |
3965 | 0 | commit = lookup_commit_reference_by_name(buf->buf); |
3966 | 0 | } |
3967 | |
|
3968 | 0 | if (!commit) |
3969 | 0 | error(_("could not resolve '%s'"), buf->buf); |
3970 | |
|
3971 | 0 | return commit; |
3972 | 0 | } |
3973 | | |
3974 | | static int do_reset(struct repository *r, |
3975 | | const char *name, int len, |
3976 | | struct replay_opts *opts) |
3977 | 0 | { |
3978 | 0 | struct strbuf ref_name = STRBUF_INIT; |
3979 | 0 | struct object_id oid; |
3980 | 0 | struct lock_file lock = LOCK_INIT; |
3981 | 0 | struct tree_desc desc = { 0 }; |
3982 | 0 | struct tree *tree; |
3983 | 0 | struct unpack_trees_options unpack_tree_opts = { 0 }; |
3984 | 0 | int ret = 0; |
3985 | |
|
3986 | 0 | if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) |
3987 | 0 | return -1; |
3988 | | |
3989 | 0 | if (len == 10 && !strncmp("[new root]", name, len)) { |
3990 | 0 | if (!opts->have_squash_onto) { |
3991 | 0 | const char *hex; |
3992 | 0 | if (commit_tree("", 0, the_hash_algo->empty_tree, |
3993 | 0 | NULL, &opts->squash_onto, |
3994 | 0 | NULL, NULL)) |
3995 | 0 | return error(_("writing fake root commit")); |
3996 | 0 | opts->have_squash_onto = 1; |
3997 | 0 | hex = oid_to_hex(&opts->squash_onto); |
3998 | 0 | if (write_message(hex, strlen(hex), |
3999 | 0 | rebase_path_squash_onto(), 0)) |
4000 | 0 | return error(_("writing squash-onto")); |
4001 | 0 | } |
4002 | 0 | oidcpy(&oid, &opts->squash_onto); |
4003 | 0 | } else { |
4004 | 0 | int i; |
4005 | 0 | struct commit *commit; |
4006 | | |
4007 | | /* Determine the length of the label */ |
4008 | 0 | for (i = 0; i < len; i++) |
4009 | 0 | if (isspace(name[i])) |
4010 | 0 | break; |
4011 | 0 | len = i; |
4012 | |
|
4013 | 0 | commit = lookup_label(r, name, len, &ref_name); |
4014 | 0 | if (!commit) { |
4015 | 0 | ret = -1; |
4016 | 0 | goto cleanup; |
4017 | 0 | } |
4018 | 0 | oid = commit->object.oid; |
4019 | 0 | } |
4020 | | |
4021 | 0 | setup_unpack_trees_porcelain(&unpack_tree_opts, "reset"); |
4022 | 0 | unpack_tree_opts.head_idx = 1; |
4023 | 0 | unpack_tree_opts.src_index = r->index; |
4024 | 0 | unpack_tree_opts.dst_index = r->index; |
4025 | 0 | unpack_tree_opts.fn = oneway_merge; |
4026 | 0 | unpack_tree_opts.merge = 1; |
4027 | 0 | unpack_tree_opts.update = 1; |
4028 | 0 | unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */ |
4029 | 0 | unpack_tree_opts.skip_cache_tree_update = 1; |
4030 | 0 | init_checkout_metadata(&unpack_tree_opts.meta, name, &oid, NULL); |
4031 | |
|
4032 | 0 | if (repo_read_index_unmerged(r)) { |
4033 | 0 | ret = error_resolve_conflict(action_name(opts)); |
4034 | 0 | goto cleanup; |
4035 | 0 | } |
4036 | | |
4037 | 0 | if (!fill_tree_descriptor(r, &desc, &oid)) { |
4038 | 0 | ret = error(_("failed to find tree of %s"), oid_to_hex(&oid)); |
4039 | 0 | goto cleanup; |
4040 | 0 | } |
4041 | | |
4042 | 0 | if (unpack_trees(1, &desc, &unpack_tree_opts)) { |
4043 | 0 | ret = -1; |
4044 | 0 | goto cleanup; |
4045 | 0 | } |
4046 | | |
4047 | 0 | tree = parse_tree_indirect(&oid); |
4048 | 0 | if (!tree) |
4049 | 0 | return error(_("unable to read tree (%s)"), oid_to_hex(&oid)); |
4050 | 0 | prime_cache_tree(r, r->index, tree); |
4051 | |
|
4052 | 0 | if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) |
4053 | 0 | ret = error(_("could not write index")); |
4054 | |
|
4055 | 0 | if (!ret) |
4056 | 0 | ret = refs_update_ref(get_main_ref_store(the_repository), reflog_message(opts, "reset", "'%.*s'", |
4057 | 0 | len, name), |
4058 | 0 | "HEAD", &oid, |
4059 | 0 | NULL, 0, UPDATE_REFS_MSG_ON_ERR); |
4060 | 0 | cleanup: |
4061 | 0 | free((void *)desc.buffer); |
4062 | 0 | if (ret < 0) |
4063 | 0 | rollback_lock_file(&lock); |
4064 | 0 | strbuf_release(&ref_name); |
4065 | 0 | clear_unpack_trees_porcelain(&unpack_tree_opts); |
4066 | 0 | return ret; |
4067 | 0 | } |
4068 | | |
4069 | | static int do_merge(struct repository *r, |
4070 | | struct commit *commit, |
4071 | | const char *arg, int arg_len, |
4072 | | int flags, int *check_todo, struct replay_opts *opts) |
4073 | 0 | { |
4074 | 0 | struct replay_ctx *ctx = opts->ctx; |
4075 | 0 | int run_commit_flags = 0; |
4076 | 0 | struct strbuf ref_name = STRBUF_INIT; |
4077 | 0 | struct commit *head_commit, *merge_commit, *i; |
4078 | 0 | struct commit_list *bases = NULL, *j; |
4079 | 0 | struct commit_list *to_merge = NULL, **tail = &to_merge; |
4080 | 0 | const char *strategy = !opts->xopts.nr && |
4081 | 0 | (!opts->strategy || |
4082 | 0 | !strcmp(opts->strategy, "recursive") || |
4083 | 0 | !strcmp(opts->strategy, "ort")) ? |
4084 | 0 | NULL : opts->strategy; |
4085 | 0 | struct merge_options o; |
4086 | 0 | int merge_arg_len, oneline_offset, can_fast_forward, ret, k; |
4087 | 0 | static struct lock_file lock; |
4088 | 0 | const char *p; |
4089 | |
|
4090 | 0 | if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) { |
4091 | 0 | ret = -1; |
4092 | 0 | goto leave_merge; |
4093 | 0 | } |
4094 | | |
4095 | 0 | head_commit = lookup_commit_reference_by_name("HEAD"); |
4096 | 0 | if (!head_commit) { |
4097 | 0 | ret = error(_("cannot merge without a current revision")); |
4098 | 0 | goto leave_merge; |
4099 | 0 | } |
4100 | | |
4101 | | /* |
4102 | | * For octopus merges, the arg starts with the list of revisions to be |
4103 | | * merged. The list is optionally followed by '#' and the oneline. |
4104 | | */ |
4105 | 0 | merge_arg_len = oneline_offset = arg_len; |
4106 | 0 | for (p = arg; p - arg < arg_len; p += strspn(p, " \t\n")) { |
4107 | 0 | if (!*p) |
4108 | 0 | break; |
4109 | 0 | if (*p == '#' && (!p[1] || isspace(p[1]))) { |
4110 | 0 | p += 1 + strspn(p + 1, " \t\n"); |
4111 | 0 | oneline_offset = p - arg; |
4112 | 0 | break; |
4113 | 0 | } |
4114 | 0 | k = strcspn(p, " \t\n"); |
4115 | 0 | if (!k) |
4116 | 0 | continue; |
4117 | 0 | merge_commit = lookup_label(r, p, k, &ref_name); |
4118 | 0 | if (!merge_commit) { |
4119 | 0 | ret = error(_("unable to parse '%.*s'"), k, p); |
4120 | 0 | goto leave_merge; |
4121 | 0 | } |
4122 | 0 | tail = &commit_list_insert(merge_commit, tail)->next; |
4123 | 0 | p += k; |
4124 | 0 | merge_arg_len = p - arg; |
4125 | 0 | } |
4126 | | |
4127 | 0 | if (!to_merge) { |
4128 | 0 | ret = error(_("nothing to merge: '%.*s'"), arg_len, arg); |
4129 | 0 | goto leave_merge; |
4130 | 0 | } |
4131 | | |
4132 | 0 | if (opts->have_squash_onto && |
4133 | 0 | oideq(&head_commit->object.oid, &opts->squash_onto)) { |
4134 | | /* |
4135 | | * When the user tells us to "merge" something into a |
4136 | | * "[new root]", let's simply fast-forward to the merge head. |
4137 | | */ |
4138 | 0 | rollback_lock_file(&lock); |
4139 | 0 | if (to_merge->next) |
4140 | 0 | ret = error(_("octopus merge cannot be executed on " |
4141 | 0 | "top of a [new root]")); |
4142 | 0 | else |
4143 | 0 | ret = fast_forward_to(r, &to_merge->item->object.oid, |
4144 | 0 | &head_commit->object.oid, 0, |
4145 | 0 | opts); |
4146 | 0 | goto leave_merge; |
4147 | 0 | } |
4148 | | |
4149 | | /* |
4150 | | * If HEAD is not identical to the first parent of the original merge |
4151 | | * commit, we cannot fast-forward. |
4152 | | */ |
4153 | 0 | can_fast_forward = opts->allow_ff && commit && commit->parents && |
4154 | 0 | oideq(&commit->parents->item->object.oid, |
4155 | 0 | &head_commit->object.oid); |
4156 | | |
4157 | | /* |
4158 | | * If any merge head is different from the original one, we cannot |
4159 | | * fast-forward. |
4160 | | */ |
4161 | 0 | if (can_fast_forward) { |
4162 | 0 | struct commit_list *p = commit->parents->next; |
4163 | |
|
4164 | 0 | for (j = to_merge; j && p; j = j->next, p = p->next) |
4165 | 0 | if (!oideq(&j->item->object.oid, |
4166 | 0 | &p->item->object.oid)) { |
4167 | 0 | can_fast_forward = 0; |
4168 | 0 | break; |
4169 | 0 | } |
4170 | | /* |
4171 | | * If the number of merge heads differs from the original merge |
4172 | | * commit, we cannot fast-forward. |
4173 | | */ |
4174 | 0 | if (j || p) |
4175 | 0 | can_fast_forward = 0; |
4176 | 0 | } |
4177 | |
|
4178 | 0 | if (can_fast_forward) { |
4179 | 0 | rollback_lock_file(&lock); |
4180 | 0 | ret = fast_forward_to(r, &commit->object.oid, |
4181 | 0 | &head_commit->object.oid, 0, opts); |
4182 | 0 | if (flags & TODO_EDIT_MERGE_MSG) |
4183 | 0 | goto fast_forward_edit; |
4184 | | |
4185 | 0 | goto leave_merge; |
4186 | 0 | } |
4187 | | |
4188 | 0 | if (commit) { |
4189 | 0 | const char *encoding = get_commit_output_encoding(); |
4190 | 0 | const char *message = repo_logmsg_reencode(r, commit, NULL, |
4191 | 0 | encoding); |
4192 | 0 | const char *body; |
4193 | 0 | int len; |
4194 | |
|
4195 | 0 | if (!message) { |
4196 | 0 | ret = error(_("could not get commit message of '%s'"), |
4197 | 0 | oid_to_hex(&commit->object.oid)); |
4198 | 0 | goto leave_merge; |
4199 | 0 | } |
4200 | 0 | write_author_script(message); |
4201 | 0 | find_commit_subject(message, &body); |
4202 | 0 | len = strlen(body); |
4203 | 0 | strbuf_add(&ctx->message, body, len); |
4204 | 0 | repo_unuse_commit_buffer(r, commit, message); |
4205 | 0 | } else { |
4206 | 0 | struct strbuf buf = STRBUF_INIT; |
4207 | |
|
4208 | 0 | strbuf_addf(&buf, "author %s", git_author_info(0)); |
4209 | 0 | write_author_script(buf.buf); |
4210 | 0 | strbuf_release(&buf); |
4211 | |
|
4212 | 0 | if (oneline_offset < arg_len) { |
4213 | 0 | strbuf_add(&ctx->message, arg + oneline_offset, |
4214 | 0 | arg_len - oneline_offset); |
4215 | 0 | } else { |
4216 | 0 | strbuf_addf(&ctx->message, "Merge %s '%.*s'", |
4217 | 0 | to_merge->next ? "branches" : "branch", |
4218 | 0 | merge_arg_len, arg); |
4219 | 0 | } |
4220 | 0 | } |
4221 | 0 | ctx->have_message = 1; |
4222 | 0 | if (write_message(ctx->message.buf, ctx->message.len, |
4223 | 0 | git_path_merge_msg(r), 0)) { |
4224 | 0 | ret = error_errno(_("could not write '%s'"), |
4225 | 0 | git_path_merge_msg(r)); |
4226 | 0 | goto leave_merge; |
4227 | 0 | } |
4228 | | |
4229 | 0 | if (strategy || to_merge->next) { |
4230 | | /* Octopus merge */ |
4231 | 0 | struct child_process cmd = CHILD_PROCESS_INIT; |
4232 | |
|
4233 | 0 | if (read_env_script(&cmd.env)) { |
4234 | 0 | const char *gpg_opt = gpg_sign_opt_quoted(opts); |
4235 | |
|
4236 | 0 | ret = error(_(staged_changes_advice), gpg_opt, gpg_opt); |
4237 | 0 | goto leave_merge; |
4238 | 0 | } |
4239 | | |
4240 | 0 | if (opts->committer_date_is_author_date) |
4241 | 0 | strvec_pushf(&cmd.env, "GIT_COMMITTER_DATE=%s", |
4242 | 0 | opts->ignore_date ? |
4243 | 0 | "" : |
4244 | 0 | author_date_from_env(&cmd.env)); |
4245 | 0 | if (opts->ignore_date) |
4246 | 0 | strvec_push(&cmd.env, "GIT_AUTHOR_DATE="); |
4247 | |
|
4248 | 0 | cmd.git_cmd = 1; |
4249 | 0 | strvec_push(&cmd.args, "merge"); |
4250 | 0 | strvec_push(&cmd.args, "-s"); |
4251 | 0 | if (!strategy) |
4252 | 0 | strvec_push(&cmd.args, "octopus"); |
4253 | 0 | else { |
4254 | 0 | strvec_push(&cmd.args, strategy); |
4255 | 0 | for (k = 0; k < opts->xopts.nr; k++) |
4256 | 0 | strvec_pushf(&cmd.args, |
4257 | 0 | "-X%s", opts->xopts.v[k]); |
4258 | 0 | } |
4259 | 0 | if (!(flags & TODO_EDIT_MERGE_MSG)) |
4260 | 0 | strvec_push(&cmd.args, "--no-edit"); |
4261 | 0 | else |
4262 | 0 | strvec_push(&cmd.args, "--edit"); |
4263 | 0 | strvec_push(&cmd.args, "--no-ff"); |
4264 | 0 | strvec_push(&cmd.args, "--no-log"); |
4265 | 0 | strvec_push(&cmd.args, "--no-stat"); |
4266 | 0 | strvec_push(&cmd.args, "-F"); |
4267 | 0 | strvec_push(&cmd.args, git_path_merge_msg(r)); |
4268 | 0 | if (opts->gpg_sign) |
4269 | 0 | strvec_pushf(&cmd.args, "-S%s", opts->gpg_sign); |
4270 | 0 | else |
4271 | 0 | strvec_push(&cmd.args, "--no-gpg-sign"); |
4272 | | |
4273 | | /* Add the tips to be merged */ |
4274 | 0 | for (j = to_merge; j; j = j->next) |
4275 | 0 | strvec_push(&cmd.args, |
4276 | 0 | oid_to_hex(&j->item->object.oid)); |
4277 | |
|
4278 | 0 | strbuf_release(&ref_name); |
4279 | 0 | refs_delete_ref(get_main_ref_store(r), "", "CHERRY_PICK_HEAD", |
4280 | 0 | NULL, REF_NO_DEREF); |
4281 | 0 | rollback_lock_file(&lock); |
4282 | |
|
4283 | 0 | ret = run_command(&cmd); |
4284 | | |
4285 | | /* force re-reading of the cache */ |
4286 | 0 | if (!ret) { |
4287 | 0 | discard_index(r->index); |
4288 | 0 | if (repo_read_index(r) < 0) |
4289 | 0 | ret = error(_("could not read index")); |
4290 | 0 | } |
4291 | 0 | goto leave_merge; |
4292 | 0 | } |
4293 | | |
4294 | 0 | merge_commit = to_merge->item; |
4295 | 0 | if (repo_get_merge_bases(r, head_commit, merge_commit, &bases) < 0) { |
4296 | 0 | ret = -1; |
4297 | 0 | goto leave_merge; |
4298 | 0 | } |
4299 | | |
4300 | 0 | if (bases && oideq(&merge_commit->object.oid, |
4301 | 0 | &bases->item->object.oid)) { |
4302 | 0 | ret = 0; |
4303 | | /* skip merging an ancestor of HEAD */ |
4304 | 0 | goto leave_merge; |
4305 | 0 | } |
4306 | | |
4307 | 0 | write_message(oid_to_hex(&merge_commit->object.oid), the_hash_algo->hexsz, |
4308 | 0 | git_path_merge_head(r), 0); |
4309 | 0 | write_message("no-ff", 5, git_path_merge_mode(r), 0); |
4310 | |
|
4311 | 0 | bases = reverse_commit_list(bases); |
4312 | |
|
4313 | 0 | repo_read_index(r); |
4314 | 0 | init_ui_merge_options(&o, r); |
4315 | 0 | o.branch1 = "HEAD"; |
4316 | 0 | o.branch2 = ref_name.buf; |
4317 | 0 | o.buffer_output = 2; |
4318 | |
|
4319 | 0 | if (!opts->strategy || !strcmp(opts->strategy, "ort")) { |
4320 | | /* |
4321 | | * TODO: Should use merge_incore_recursive() and |
4322 | | * merge_switch_to_result(), skipping the call to |
4323 | | * merge_switch_to_result() when we don't actually need to |
4324 | | * update the index and working copy immediately. |
4325 | | */ |
4326 | 0 | ret = merge_ort_recursive(&o, |
4327 | 0 | head_commit, merge_commit, bases, |
4328 | 0 | &i); |
4329 | 0 | } else { |
4330 | 0 | ret = merge_recursive(&o, head_commit, merge_commit, bases, |
4331 | 0 | &i); |
4332 | 0 | } |
4333 | 0 | if (ret <= 0) |
4334 | 0 | fputs(o.obuf.buf, stdout); |
4335 | 0 | strbuf_release(&o.obuf); |
4336 | 0 | if (ret < 0) { |
4337 | 0 | error(_("could not even attempt to merge '%.*s'"), |
4338 | 0 | merge_arg_len, arg); |
4339 | 0 | unlink(git_path_merge_msg(r)); |
4340 | 0 | goto leave_merge; |
4341 | 0 | } |
4342 | | /* |
4343 | | * The return value of merge_recursive() is 1 on clean, and 0 on |
4344 | | * unclean merge. |
4345 | | * |
4346 | | * Let's reverse that, so that do_merge() returns 0 upon success and |
4347 | | * 1 upon failed merge (keeping the return value -1 for the cases where |
4348 | | * we will want to reschedule the `merge` command). |
4349 | | */ |
4350 | 0 | ret = !ret; |
4351 | |
|
4352 | 0 | if (r->index->cache_changed && |
4353 | 0 | write_locked_index(r->index, &lock, COMMIT_LOCK)) { |
4354 | 0 | ret = error(_("merge: Unable to write new index file")); |
4355 | 0 | goto leave_merge; |
4356 | 0 | } |
4357 | | |
4358 | 0 | rollback_lock_file(&lock); |
4359 | 0 | if (ret) |
4360 | 0 | repo_rerere(r, opts->allow_rerere_auto); |
4361 | 0 | else |
4362 | | /* |
4363 | | * In case of problems, we now want to return a positive |
4364 | | * value (a negative one would indicate that the `merge` |
4365 | | * command needs to be rescheduled). |
4366 | | */ |
4367 | 0 | ret = !!run_git_commit(git_path_merge_msg(r), opts, |
4368 | 0 | run_commit_flags); |
4369 | |
|
4370 | 0 | if (!ret && flags & TODO_EDIT_MERGE_MSG) { |
4371 | 0 | fast_forward_edit: |
4372 | 0 | *check_todo = 1; |
4373 | 0 | run_commit_flags |= AMEND_MSG | EDIT_MSG | VERIFY_MSG; |
4374 | 0 | ret = !!run_git_commit(NULL, opts, run_commit_flags); |
4375 | 0 | } |
4376 | | |
4377 | |
|
4378 | 0 | leave_merge: |
4379 | 0 | strbuf_release(&ref_name); |
4380 | 0 | rollback_lock_file(&lock); |
4381 | 0 | free_commit_list(to_merge); |
4382 | 0 | free_commit_list(bases); |
4383 | 0 | return ret; |
4384 | 0 | } |
4385 | | |
4386 | | static int write_update_refs_state(struct string_list *refs_to_oids) |
4387 | 0 | { |
4388 | 0 | int result = 0; |
4389 | 0 | struct lock_file lock = LOCK_INIT; |
4390 | 0 | FILE *fp = NULL; |
4391 | 0 | struct string_list_item *item; |
4392 | 0 | char *path; |
4393 | |
|
4394 | 0 | path = rebase_path_update_refs(the_repository->gitdir); |
4395 | |
|
4396 | 0 | if (!refs_to_oids->nr) { |
4397 | 0 | if (unlink(path) && errno != ENOENT) |
4398 | 0 | result = error_errno(_("could not unlink: %s"), path); |
4399 | 0 | goto cleanup; |
4400 | 0 | } |
4401 | | |
4402 | 0 | if (safe_create_leading_directories(path)) { |
4403 | 0 | result = error(_("unable to create leading directories of %s"), |
4404 | 0 | path); |
4405 | 0 | goto cleanup; |
4406 | 0 | } |
4407 | | |
4408 | 0 | if (hold_lock_file_for_update(&lock, path, 0) < 0) { |
4409 | 0 | result = error(_("another 'rebase' process appears to be running; " |
4410 | 0 | "'%s.lock' already exists"), |
4411 | 0 | path); |
4412 | 0 | goto cleanup; |
4413 | 0 | } |
4414 | | |
4415 | 0 | fp = fdopen_lock_file(&lock, "w"); |
4416 | 0 | if (!fp) { |
4417 | 0 | result = error_errno(_("could not open '%s' for writing"), path); |
4418 | 0 | rollback_lock_file(&lock); |
4419 | 0 | goto cleanup; |
4420 | 0 | } |
4421 | | |
4422 | 0 | for_each_string_list_item(item, refs_to_oids) { |
4423 | 0 | struct update_ref_record *rec = item->util; |
4424 | 0 | fprintf(fp, "%s\n%s\n%s\n", item->string, |
4425 | 0 | oid_to_hex(&rec->before), oid_to_hex(&rec->after)); |
4426 | 0 | } |
4427 | |
|
4428 | 0 | result = commit_lock_file(&lock); |
4429 | |
|
4430 | 0 | cleanup: |
4431 | 0 | free(path); |
4432 | 0 | return result; |
4433 | 0 | } |
4434 | | |
4435 | | /* |
4436 | | * Parse the update-refs file for the current rebase, then remove the |
4437 | | * refs that do not appear in the todo_list (and have not had updated |
4438 | | * values stored) and add refs that are in the todo_list but not |
4439 | | * represented in the update-refs file. |
4440 | | * |
4441 | | * If there are changes to the update-refs list, then write the new state |
4442 | | * to disk. |
4443 | | */ |
4444 | | void todo_list_filter_update_refs(struct repository *r, |
4445 | | struct todo_list *todo_list) |
4446 | 0 | { |
4447 | 0 | int i; |
4448 | 0 | int updated = 0; |
4449 | 0 | struct string_list update_refs = STRING_LIST_INIT_DUP; |
4450 | |
|
4451 | 0 | sequencer_get_update_refs_state(r->gitdir, &update_refs); |
4452 | | |
4453 | | /* |
4454 | | * For each item in the update_refs list, if it has no updated |
4455 | | * value and does not appear in the todo_list, then remove it |
4456 | | * from the update_refs list. |
4457 | | */ |
4458 | 0 | for (i = 0; i < update_refs.nr; i++) { |
4459 | 0 | int j; |
4460 | 0 | int found = 0; |
4461 | 0 | const char *ref = update_refs.items[i].string; |
4462 | 0 | size_t reflen = strlen(ref); |
4463 | 0 | struct update_ref_record *rec = update_refs.items[i].util; |
4464 | | |
4465 | | /* OID already stored as updated. */ |
4466 | 0 | if (!is_null_oid(&rec->after)) |
4467 | 0 | continue; |
4468 | | |
4469 | 0 | for (j = 0; !found && j < todo_list->nr; j++) { |
4470 | 0 | struct todo_item *item = &todo_list->items[j]; |
4471 | 0 | const char *arg = todo_list->buf.buf + item->arg_offset; |
4472 | |
|
4473 | 0 | if (item->command != TODO_UPDATE_REF) |
4474 | 0 | continue; |
4475 | | |
4476 | 0 | if (item->arg_len != reflen || |
4477 | 0 | strncmp(arg, ref, reflen)) |
4478 | 0 | continue; |
4479 | | |
4480 | 0 | found = 1; |
4481 | 0 | } |
4482 | |
|
4483 | 0 | if (!found) { |
4484 | 0 | free(update_refs.items[i].string); |
4485 | 0 | free(update_refs.items[i].util); |
4486 | |
|
4487 | 0 | update_refs.nr--; |
4488 | 0 | MOVE_ARRAY(update_refs.items + i, update_refs.items + i + 1, update_refs.nr - i); |
4489 | |
|
4490 | 0 | updated = 1; |
4491 | 0 | i--; |
4492 | 0 | } |
4493 | 0 | } |
4494 | | |
4495 | | /* |
4496 | | * For each todo_item, check if its ref is in the update_refs list. |
4497 | | * If not, then add it as an un-updated ref. |
4498 | | */ |
4499 | 0 | for (i = 0; i < todo_list->nr; i++) { |
4500 | 0 | struct todo_item *item = &todo_list->items[i]; |
4501 | 0 | const char *arg = todo_list->buf.buf + item->arg_offset; |
4502 | 0 | int j, found = 0; |
4503 | |
|
4504 | 0 | if (item->command != TODO_UPDATE_REF) |
4505 | 0 | continue; |
4506 | | |
4507 | 0 | for (j = 0; !found && j < update_refs.nr; j++) { |
4508 | 0 | const char *ref = update_refs.items[j].string; |
4509 | |
|
4510 | 0 | found = strlen(ref) == item->arg_len && |
4511 | 0 | !strncmp(ref, arg, item->arg_len); |
4512 | 0 | } |
4513 | |
|
4514 | 0 | if (!found) { |
4515 | 0 | struct string_list_item *inserted; |
4516 | 0 | struct strbuf argref = STRBUF_INIT; |
4517 | |
|
4518 | 0 | strbuf_add(&argref, arg, item->arg_len); |
4519 | 0 | inserted = string_list_insert(&update_refs, argref.buf); |
4520 | 0 | inserted->util = init_update_ref_record(argref.buf); |
4521 | 0 | strbuf_release(&argref); |
4522 | 0 | updated = 1; |
4523 | 0 | } |
4524 | 0 | } |
4525 | |
|
4526 | 0 | if (updated) |
4527 | 0 | write_update_refs_state(&update_refs); |
4528 | 0 | string_list_clear(&update_refs, 1); |
4529 | 0 | } |
4530 | | |
4531 | | static int do_update_ref(struct repository *r, const char *refname) |
4532 | 0 | { |
4533 | 0 | struct string_list_item *item; |
4534 | 0 | struct string_list list = STRING_LIST_INIT_DUP; |
4535 | |
|
4536 | 0 | if (sequencer_get_update_refs_state(r->gitdir, &list)) |
4537 | 0 | return -1; |
4538 | | |
4539 | 0 | for_each_string_list_item(item, &list) { |
4540 | 0 | if (!strcmp(item->string, refname)) { |
4541 | 0 | struct update_ref_record *rec = item->util; |
4542 | 0 | if (refs_read_ref(get_main_ref_store(the_repository), "HEAD", &rec->after)) |
4543 | 0 | return -1; |
4544 | 0 | break; |
4545 | 0 | } |
4546 | 0 | } |
4547 | | |
4548 | 0 | write_update_refs_state(&list); |
4549 | 0 | string_list_clear(&list, 1); |
4550 | 0 | return 0; |
4551 | 0 | } |
4552 | | |
4553 | | static int do_update_refs(struct repository *r, int quiet) |
4554 | 0 | { |
4555 | 0 | int res = 0; |
4556 | 0 | struct string_list_item *item; |
4557 | 0 | struct string_list refs_to_oids = STRING_LIST_INIT_DUP; |
4558 | 0 | struct ref_store *refs = get_main_ref_store(r); |
4559 | 0 | struct strbuf update_msg = STRBUF_INIT; |
4560 | 0 | struct strbuf error_msg = STRBUF_INIT; |
4561 | |
|
4562 | 0 | if ((res = sequencer_get_update_refs_state(r->gitdir, &refs_to_oids))) |
4563 | 0 | return res; |
4564 | | |
4565 | 0 | for_each_string_list_item(item, &refs_to_oids) { |
4566 | 0 | struct update_ref_record *rec = item->util; |
4567 | 0 | int loop_res; |
4568 | |
|
4569 | 0 | loop_res = refs_update_ref(refs, "rewritten during rebase", |
4570 | 0 | item->string, |
4571 | 0 | &rec->after, &rec->before, |
4572 | 0 | 0, UPDATE_REFS_MSG_ON_ERR); |
4573 | 0 | res |= loop_res; |
4574 | |
|
4575 | 0 | if (quiet) |
4576 | 0 | continue; |
4577 | | |
4578 | 0 | if (loop_res) |
4579 | 0 | strbuf_addf(&error_msg, "\t%s\n", item->string); |
4580 | 0 | else |
4581 | 0 | strbuf_addf(&update_msg, "\t%s\n", item->string); |
4582 | 0 | } |
4583 | |
|
4584 | 0 | if (!quiet && |
4585 | 0 | (update_msg.len || error_msg.len)) { |
4586 | 0 | fprintf(stderr, |
4587 | 0 | _("Updated the following refs with %s:\n%s"), |
4588 | 0 | "--update-refs", |
4589 | 0 | update_msg.buf); |
4590 | |
|
4591 | 0 | if (res) |
4592 | 0 | fprintf(stderr, |
4593 | 0 | _("Failed to update the following refs with %s:\n%s"), |
4594 | 0 | "--update-refs", |
4595 | 0 | error_msg.buf); |
4596 | 0 | } |
4597 | |
|
4598 | 0 | string_list_clear(&refs_to_oids, 1); |
4599 | 0 | strbuf_release(&update_msg); |
4600 | 0 | strbuf_release(&error_msg); |
4601 | 0 | return res; |
4602 | 0 | } |
4603 | | |
4604 | | static int is_final_fixup(struct todo_list *todo_list) |
4605 | 0 | { |
4606 | 0 | int i = todo_list->current; |
4607 | |
|
4608 | 0 | if (!is_fixup(todo_list->items[i].command)) |
4609 | 0 | return 0; |
4610 | | |
4611 | 0 | while (++i < todo_list->nr) |
4612 | 0 | if (is_fixup(todo_list->items[i].command)) |
4613 | 0 | return 0; |
4614 | 0 | else if (!is_noop(todo_list->items[i].command)) |
4615 | 0 | break; |
4616 | 0 | return 1; |
4617 | 0 | } |
4618 | | |
4619 | | static enum todo_command peek_command(struct todo_list *todo_list, int offset) |
4620 | 0 | { |
4621 | 0 | int i; |
4622 | |
|
4623 | 0 | for (i = todo_list->current + offset; i < todo_list->nr; i++) |
4624 | 0 | if (!is_noop(todo_list->items[i].command)) |
4625 | 0 | return todo_list->items[i].command; |
4626 | | |
4627 | 0 | return -1; |
4628 | 0 | } |
4629 | | |
4630 | | static void create_autostash_internal(struct repository *r, |
4631 | | const char *path, |
4632 | | const char *refname) |
4633 | 0 | { |
4634 | 0 | struct strbuf buf = STRBUF_INIT; |
4635 | 0 | struct lock_file lock_file = LOCK_INIT; |
4636 | 0 | int fd; |
4637 | |
|
4638 | 0 | if (path && refname) |
4639 | 0 | BUG("can only pass path or refname"); |
4640 | | |
4641 | 0 | fd = repo_hold_locked_index(r, &lock_file, 0); |
4642 | 0 | refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL); |
4643 | 0 | if (0 <= fd) |
4644 | 0 | repo_update_index_if_able(r, &lock_file); |
4645 | 0 | rollback_lock_file(&lock_file); |
4646 | |
|
4647 | 0 | if (has_unstaged_changes(r, 1) || |
4648 | 0 | has_uncommitted_changes(r, 1)) { |
4649 | 0 | struct child_process stash = CHILD_PROCESS_INIT; |
4650 | 0 | struct reset_head_opts ropts = { .flags = RESET_HEAD_HARD }; |
4651 | 0 | struct object_id oid; |
4652 | |
|
4653 | 0 | strvec_pushl(&stash.args, |
4654 | 0 | "stash", "create", "autostash", NULL); |
4655 | 0 | stash.git_cmd = 1; |
4656 | 0 | stash.no_stdin = 1; |
4657 | 0 | strbuf_reset(&buf); |
4658 | 0 | if (capture_command(&stash, &buf, GIT_MAX_HEXSZ)) |
4659 | 0 | die(_("Cannot autostash")); |
4660 | 0 | strbuf_trim_trailing_newline(&buf); |
4661 | 0 | if (repo_get_oid(r, buf.buf, &oid)) |
4662 | 0 | die(_("Unexpected stash response: '%s'"), |
4663 | 0 | buf.buf); |
4664 | 0 | strbuf_reset(&buf); |
4665 | 0 | strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV); |
4666 | |
|
4667 | 0 | if (path) { |
4668 | 0 | if (safe_create_leading_directories_const(path)) |
4669 | 0 | die(_("Could not create directory for '%s'"), |
4670 | 0 | path); |
4671 | 0 | write_file(path, "%s", oid_to_hex(&oid)); |
4672 | 0 | } else { |
4673 | 0 | refs_update_ref(get_main_ref_store(r), "", refname, |
4674 | 0 | &oid, null_oid(), 0, UPDATE_REFS_DIE_ON_ERR); |
4675 | 0 | } |
4676 | | |
4677 | 0 | printf(_("Created autostash: %s\n"), buf.buf); |
4678 | 0 | if (reset_head(r, &ropts) < 0) |
4679 | 0 | die(_("could not reset --hard")); |
4680 | 0 | discard_index(r->index); |
4681 | 0 | if (repo_read_index(r) < 0) |
4682 | 0 | die(_("could not read index")); |
4683 | 0 | } |
4684 | 0 | strbuf_release(&buf); |
4685 | 0 | } |
4686 | | |
4687 | | void create_autostash(struct repository *r, const char *path) |
4688 | 0 | { |
4689 | 0 | create_autostash_internal(r, path, NULL); |
4690 | 0 | } |
4691 | | |
4692 | | void create_autostash_ref(struct repository *r, const char *refname) |
4693 | 0 | { |
4694 | 0 | create_autostash_internal(r, NULL, refname); |
4695 | 0 | } |
4696 | | |
4697 | | static int apply_save_autostash_oid(const char *stash_oid, int attempt_apply) |
4698 | 0 | { |
4699 | 0 | struct child_process child = CHILD_PROCESS_INIT; |
4700 | 0 | int ret = 0; |
4701 | |
|
4702 | 0 | if (attempt_apply) { |
4703 | 0 | child.git_cmd = 1; |
4704 | 0 | child.no_stdout = 1; |
4705 | 0 | child.no_stderr = 1; |
4706 | 0 | strvec_push(&child.args, "stash"); |
4707 | 0 | strvec_push(&child.args, "apply"); |
4708 | 0 | strvec_push(&child.args, stash_oid); |
4709 | 0 | ret = run_command(&child); |
4710 | 0 | } |
4711 | |
|
4712 | 0 | if (attempt_apply && !ret) |
4713 | 0 | fprintf(stderr, _("Applied autostash.\n")); |
4714 | 0 | else { |
4715 | 0 | struct child_process store = CHILD_PROCESS_INIT; |
4716 | |
|
4717 | 0 | store.git_cmd = 1; |
4718 | 0 | strvec_push(&store.args, "stash"); |
4719 | 0 | strvec_push(&store.args, "store"); |
4720 | 0 | strvec_push(&store.args, "-m"); |
4721 | 0 | strvec_push(&store.args, "autostash"); |
4722 | 0 | strvec_push(&store.args, "-q"); |
4723 | 0 | strvec_push(&store.args, stash_oid); |
4724 | 0 | if (run_command(&store)) |
4725 | 0 | ret = error(_("cannot store %s"), stash_oid); |
4726 | 0 | else |
4727 | 0 | fprintf(stderr, |
4728 | 0 | _("%s\n" |
4729 | 0 | "Your changes are safe in the stash.\n" |
4730 | 0 | "You can run \"git stash pop\" or" |
4731 | 0 | " \"git stash drop\" at any time.\n"), |
4732 | 0 | attempt_apply ? |
4733 | 0 | _("Applying autostash resulted in conflicts.") : |
4734 | 0 | _("Autostash exists; creating a new stash entry.")); |
4735 | 0 | } |
4736 | |
|
4737 | 0 | return ret; |
4738 | 0 | } |
4739 | | |
4740 | | static int apply_save_autostash(const char *path, int attempt_apply) |
4741 | 0 | { |
4742 | 0 | struct strbuf stash_oid = STRBUF_INIT; |
4743 | 0 | int ret = 0; |
4744 | |
|
4745 | 0 | if (!read_oneliner(&stash_oid, path, |
4746 | 0 | READ_ONELINER_SKIP_IF_EMPTY)) { |
4747 | 0 | strbuf_release(&stash_oid); |
4748 | 0 | return 0; |
4749 | 0 | } |
4750 | 0 | strbuf_trim(&stash_oid); |
4751 | |
|
4752 | 0 | ret = apply_save_autostash_oid(stash_oid.buf, attempt_apply); |
4753 | |
|
4754 | 0 | unlink(path); |
4755 | 0 | strbuf_release(&stash_oid); |
4756 | 0 | return ret; |
4757 | 0 | } |
4758 | | |
4759 | | int save_autostash(const char *path) |
4760 | 0 | { |
4761 | 0 | return apply_save_autostash(path, 0); |
4762 | 0 | } |
4763 | | |
4764 | | int apply_autostash(const char *path) |
4765 | 0 | { |
4766 | 0 | return apply_save_autostash(path, 1); |
4767 | 0 | } |
4768 | | |
4769 | | int apply_autostash_oid(const char *stash_oid) |
4770 | 0 | { |
4771 | 0 | return apply_save_autostash_oid(stash_oid, 1); |
4772 | 0 | } |
4773 | | |
4774 | | static int apply_save_autostash_ref(struct repository *r, const char *refname, |
4775 | | int attempt_apply) |
4776 | 0 | { |
4777 | 0 | struct object_id stash_oid; |
4778 | 0 | char stash_oid_hex[GIT_MAX_HEXSZ + 1]; |
4779 | 0 | int flag, ret; |
4780 | |
|
4781 | 0 | if (!refs_ref_exists(get_main_ref_store(r), refname)) |
4782 | 0 | return 0; |
4783 | | |
4784 | 0 | if (!refs_resolve_ref_unsafe(get_main_ref_store(r), refname, |
4785 | 0 | RESOLVE_REF_READING, &stash_oid, &flag)) |
4786 | 0 | return -1; |
4787 | 0 | if (flag & REF_ISSYMREF) |
4788 | 0 | return error(_("autostash reference is a symref")); |
4789 | | |
4790 | 0 | oid_to_hex_r(stash_oid_hex, &stash_oid); |
4791 | 0 | ret = apply_save_autostash_oid(stash_oid_hex, attempt_apply); |
4792 | |
|
4793 | 0 | refs_delete_ref(get_main_ref_store(r), "", refname, |
4794 | 0 | &stash_oid, REF_NO_DEREF); |
4795 | |
|
4796 | 0 | return ret; |
4797 | 0 | } |
4798 | | |
4799 | | int save_autostash_ref(struct repository *r, const char *refname) |
4800 | 0 | { |
4801 | 0 | return apply_save_autostash_ref(r, refname, 0); |
4802 | 0 | } |
4803 | | |
4804 | | int apply_autostash_ref(struct repository *r, const char *refname) |
4805 | 0 | { |
4806 | 0 | return apply_save_autostash_ref(r, refname, 1); |
4807 | 0 | } |
4808 | | |
4809 | | static int checkout_onto(struct repository *r, struct replay_opts *opts, |
4810 | | const char *onto_name, const struct object_id *onto, |
4811 | | const struct object_id *orig_head) |
4812 | 0 | { |
4813 | 0 | struct reset_head_opts ropts = { |
4814 | 0 | .oid = onto, |
4815 | 0 | .orig_head = orig_head, |
4816 | 0 | .flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD | |
4817 | 0 | RESET_HEAD_RUN_POST_CHECKOUT_HOOK, |
4818 | 0 | .head_msg = reflog_message(opts, "start", "checkout %s", |
4819 | 0 | onto_name), |
4820 | 0 | .default_reflog_action = sequencer_reflog_action(opts) |
4821 | 0 | }; |
4822 | 0 | if (reset_head(r, &ropts)) { |
4823 | 0 | apply_autostash(rebase_path_autostash()); |
4824 | 0 | sequencer_remove_state(opts); |
4825 | 0 | return error(_("could not detach HEAD")); |
4826 | 0 | } |
4827 | | |
4828 | 0 | return 0; |
4829 | 0 | } |
4830 | | |
4831 | | static int stopped_at_head(struct repository *r) |
4832 | 0 | { |
4833 | 0 | struct object_id head; |
4834 | 0 | struct commit *commit; |
4835 | 0 | struct commit_message message; |
4836 | |
|
4837 | 0 | if (repo_get_oid(r, "HEAD", &head) || |
4838 | 0 | !(commit = lookup_commit(r, &head)) || |
4839 | 0 | repo_parse_commit(r, commit) || get_message(commit, &message)) |
4840 | 0 | fprintf(stderr, _("Stopped at HEAD\n")); |
4841 | 0 | else { |
4842 | 0 | fprintf(stderr, _("Stopped at %s\n"), message.label); |
4843 | 0 | free_message(commit, &message); |
4844 | 0 | } |
4845 | 0 | return 0; |
4846 | |
|
4847 | 0 | } |
4848 | | |
4849 | | static int reread_todo_if_changed(struct repository *r, |
4850 | | struct todo_list *todo_list, |
4851 | | struct replay_opts *opts) |
4852 | 0 | { |
4853 | 0 | int offset; |
4854 | 0 | struct strbuf buf = STRBUF_INIT; |
4855 | |
|
4856 | 0 | if (strbuf_read_file_or_whine(&buf, get_todo_path(opts)) < 0) |
4857 | 0 | return -1; |
4858 | 0 | offset = get_item_line_offset(todo_list, todo_list->current + 1); |
4859 | 0 | if (buf.len != todo_list->buf.len - offset || |
4860 | 0 | memcmp(buf.buf, todo_list->buf.buf + offset, buf.len)) { |
4861 | | /* Reread the todo file if it has changed. */ |
4862 | 0 | todo_list_release(todo_list); |
4863 | 0 | if (read_populate_todo(r, todo_list, opts)) |
4864 | 0 | return -1; /* message was printed */ |
4865 | | /* `current` will be incremented on return */ |
4866 | 0 | todo_list->current = -1; |
4867 | 0 | } |
4868 | 0 | strbuf_release(&buf); |
4869 | |
|
4870 | 0 | return 0; |
4871 | 0 | } |
4872 | | |
4873 | | static const char rescheduled_advice[] = |
4874 | | N_("Could not execute the todo command\n" |
4875 | | "\n" |
4876 | | " %.*s" |
4877 | | "\n" |
4878 | | "It has been rescheduled; To edit the command before continuing, please\n" |
4879 | | "edit the todo list first:\n" |
4880 | | "\n" |
4881 | | " git rebase --edit-todo\n" |
4882 | | " git rebase --continue\n"); |
4883 | | |
4884 | | static int pick_one_commit(struct repository *r, |
4885 | | struct todo_list *todo_list, |
4886 | | struct replay_opts *opts, |
4887 | | int *check_todo, int* reschedule) |
4888 | 0 | { |
4889 | 0 | struct replay_ctx *ctx = opts->ctx; |
4890 | 0 | int res; |
4891 | 0 | struct todo_item *item = todo_list->items + todo_list->current; |
4892 | 0 | const char *arg = todo_item_get_arg(todo_list, item); |
4893 | 0 | if (is_rebase_i(opts)) |
4894 | 0 | ctx->reflog_message = reflog_message( |
4895 | 0 | opts, command_to_string(item->command), NULL); |
4896 | |
|
4897 | 0 | res = do_pick_commit(r, item, opts, is_final_fixup(todo_list), |
4898 | 0 | check_todo); |
4899 | 0 | if (is_rebase_i(opts) && res < 0) { |
4900 | | /* Reschedule */ |
4901 | 0 | *reschedule = 1; |
4902 | 0 | return -1; |
4903 | 0 | } |
4904 | 0 | if (item->command == TODO_EDIT) { |
4905 | 0 | struct commit *commit = item->commit; |
4906 | 0 | if (!res) { |
4907 | 0 | if (!opts->verbose) |
4908 | 0 | term_clear_line(); |
4909 | 0 | fprintf(stderr, _("Stopped at %s... %.*s\n"), |
4910 | 0 | short_commit_name(r, commit), item->arg_len, arg); |
4911 | 0 | } |
4912 | 0 | return error_with_patch(r, commit, |
4913 | 0 | arg, item->arg_len, opts, res, !res); |
4914 | 0 | } |
4915 | 0 | if (is_rebase_i(opts) && !res) |
4916 | 0 | record_in_rewritten(&item->commit->object.oid, |
4917 | 0 | peek_command(todo_list, 1)); |
4918 | 0 | if (res && is_fixup(item->command)) { |
4919 | 0 | if (res == 1) |
4920 | 0 | intend_to_amend(); |
4921 | 0 | return error_failed_squash(r, item->commit, opts, |
4922 | 0 | item->arg_len, arg); |
4923 | 0 | } else if (res && is_rebase_i(opts) && item->commit) { |
4924 | 0 | int to_amend = 0; |
4925 | 0 | struct object_id oid; |
4926 | | |
4927 | | /* |
4928 | | * If we are rewording and have either |
4929 | | * fast-forwarded already, or are about to |
4930 | | * create a new root commit, we want to amend, |
4931 | | * otherwise we do not. |
4932 | | */ |
4933 | 0 | if (item->command == TODO_REWORD && |
4934 | 0 | !repo_get_oid(r, "HEAD", &oid) && |
4935 | 0 | (oideq(&item->commit->object.oid, &oid) || |
4936 | 0 | (opts->have_squash_onto && |
4937 | 0 | oideq(&opts->squash_onto, &oid)))) |
4938 | 0 | to_amend = 1; |
4939 | |
|
4940 | 0 | return res | error_with_patch(r, item->commit, |
4941 | 0 | arg, item->arg_len, opts, |
4942 | 0 | res, to_amend); |
4943 | 0 | } |
4944 | 0 | return res; |
4945 | 0 | } |
4946 | | |
4947 | | static int pick_commits(struct repository *r, |
4948 | | struct todo_list *todo_list, |
4949 | | struct replay_opts *opts) |
4950 | 0 | { |
4951 | 0 | struct replay_ctx *ctx = opts->ctx; |
4952 | 0 | int res = 0, reschedule = 0; |
4953 | |
|
4954 | 0 | ctx->reflog_message = sequencer_reflog_action(opts); |
4955 | 0 | if (opts->allow_ff) |
4956 | 0 | assert(!(opts->signoff || opts->no_commit || |
4957 | 0 | opts->record_origin || should_edit(opts) || |
4958 | 0 | opts->committer_date_is_author_date || |
4959 | 0 | opts->ignore_date)); |
4960 | 0 | if (read_and_refresh_cache(r, opts)) |
4961 | 0 | return -1; |
4962 | | |
4963 | 0 | unlink(rebase_path_message()); |
4964 | 0 | unlink(rebase_path_stopped_sha()); |
4965 | 0 | unlink(rebase_path_amend()); |
4966 | 0 | unlink(rebase_path_patch()); |
4967 | |
|
4968 | 0 | while (todo_list->current < todo_list->nr) { |
4969 | 0 | struct todo_item *item = todo_list->items + todo_list->current; |
4970 | 0 | const char *arg = todo_item_get_arg(todo_list, item); |
4971 | 0 | int check_todo = 0; |
4972 | |
|
4973 | 0 | if (save_todo(todo_list, opts, reschedule)) |
4974 | 0 | return -1; |
4975 | 0 | if (is_rebase_i(opts)) { |
4976 | 0 | if (item->command != TODO_COMMENT) { |
4977 | 0 | FILE *f = fopen(rebase_path_msgnum(), "w"); |
4978 | |
|
4979 | 0 | todo_list->done_nr++; |
4980 | |
|
4981 | 0 | if (f) { |
4982 | 0 | fprintf(f, "%d\n", todo_list->done_nr); |
4983 | 0 | fclose(f); |
4984 | 0 | } |
4985 | 0 | if (!opts->quiet) |
4986 | 0 | fprintf(stderr, _("Rebasing (%d/%d)%s"), |
4987 | 0 | todo_list->done_nr, |
4988 | 0 | todo_list->total_nr, |
4989 | 0 | opts->verbose ? "\n" : "\r"); |
4990 | 0 | } |
4991 | 0 | unlink(rebase_path_author_script()); |
4992 | 0 | unlink(git_path_merge_head(r)); |
4993 | 0 | refs_delete_ref(get_main_ref_store(r), "", "AUTO_MERGE", |
4994 | 0 | NULL, REF_NO_DEREF); |
4995 | 0 | refs_delete_ref(get_main_ref_store(r), "", "REBASE_HEAD", |
4996 | 0 | NULL, REF_NO_DEREF); |
4997 | |
|
4998 | 0 | if (item->command == TODO_BREAK) { |
4999 | 0 | if (!opts->verbose) |
5000 | 0 | term_clear_line(); |
5001 | 0 | return stopped_at_head(r); |
5002 | 0 | } |
5003 | 0 | } |
5004 | 0 | strbuf_reset(&ctx->message); |
5005 | 0 | ctx->have_message = 0; |
5006 | 0 | if (item->command <= TODO_SQUASH) { |
5007 | 0 | res = pick_one_commit(r, todo_list, opts, &check_todo, |
5008 | 0 | &reschedule); |
5009 | 0 | if (!res && item->command == TODO_EDIT) |
5010 | 0 | return 0; |
5011 | 0 | } else if (item->command == TODO_EXEC) { |
5012 | 0 | char *end_of_arg = (char *)(arg + item->arg_len); |
5013 | 0 | int saved = *end_of_arg; |
5014 | |
|
5015 | 0 | if (!opts->verbose) |
5016 | 0 | term_clear_line(); |
5017 | 0 | *end_of_arg = '\0'; |
5018 | 0 | res = do_exec(r, arg, opts->quiet); |
5019 | 0 | *end_of_arg = saved; |
5020 | |
|
5021 | 0 | if (res) { |
5022 | 0 | if (opts->reschedule_failed_exec) |
5023 | 0 | reschedule = 1; |
5024 | 0 | } |
5025 | 0 | check_todo = 1; |
5026 | 0 | } else if (item->command == TODO_LABEL) { |
5027 | 0 | if ((res = do_label(r, arg, item->arg_len))) |
5028 | 0 | reschedule = 1; |
5029 | 0 | } else if (item->command == TODO_RESET) { |
5030 | 0 | if ((res = do_reset(r, arg, item->arg_len, opts))) |
5031 | 0 | reschedule = 1; |
5032 | 0 | } else if (item->command == TODO_MERGE) { |
5033 | 0 | if ((res = do_merge(r, item->commit, arg, item->arg_len, |
5034 | 0 | item->flags, &check_todo, opts)) < 0) |
5035 | 0 | reschedule = 1; |
5036 | 0 | else if (item->commit) |
5037 | 0 | record_in_rewritten(&item->commit->object.oid, |
5038 | 0 | peek_command(todo_list, 1)); |
5039 | 0 | if (res > 0) |
5040 | | /* failed with merge conflicts */ |
5041 | 0 | return error_with_patch(r, item->commit, |
5042 | 0 | arg, item->arg_len, |
5043 | 0 | opts, res, 0); |
5044 | 0 | } else if (item->command == TODO_UPDATE_REF) { |
5045 | 0 | struct strbuf ref = STRBUF_INIT; |
5046 | 0 | strbuf_add(&ref, arg, item->arg_len); |
5047 | 0 | if ((res = do_update_ref(r, ref.buf))) |
5048 | 0 | reschedule = 1; |
5049 | 0 | strbuf_release(&ref); |
5050 | 0 | } else if (!is_noop(item->command)) |
5051 | 0 | return error(_("unknown command %d"), item->command); |
5052 | | |
5053 | 0 | if (reschedule) { |
5054 | 0 | advise(_(rescheduled_advice), |
5055 | 0 | get_item_line_length(todo_list, |
5056 | 0 | todo_list->current), |
5057 | 0 | get_item_line(todo_list, todo_list->current)); |
5058 | 0 | if (save_todo(todo_list, opts, reschedule)) |
5059 | 0 | return -1; |
5060 | 0 | if (item->commit) |
5061 | 0 | write_rebase_head(&item->commit->object.oid); |
5062 | 0 | } else if (is_rebase_i(opts) && check_todo && !res && |
5063 | 0 | reread_todo_if_changed(r, todo_list, opts)) { |
5064 | 0 | return -1; |
5065 | 0 | } |
5066 | | |
5067 | 0 | if (res) |
5068 | 0 | return res; |
5069 | | |
5070 | 0 | todo_list->current++; |
5071 | 0 | } |
5072 | | |
5073 | 0 | if (is_rebase_i(opts)) { |
5074 | 0 | struct strbuf head_ref = STRBUF_INIT, buf = STRBUF_INIT; |
5075 | 0 | struct stat st; |
5076 | |
|
5077 | 0 | if (read_oneliner(&head_ref, rebase_path_head_name(), 0) && |
5078 | 0 | starts_with(head_ref.buf, "refs/")) { |
5079 | 0 | const char *msg; |
5080 | 0 | struct object_id head, orig; |
5081 | 0 | int res; |
5082 | |
|
5083 | 0 | if (repo_get_oid(r, "HEAD", &head)) { |
5084 | 0 | res = error(_("cannot read HEAD")); |
5085 | 0 | cleanup_head_ref: |
5086 | 0 | strbuf_release(&head_ref); |
5087 | 0 | strbuf_release(&buf); |
5088 | 0 | return res; |
5089 | 0 | } |
5090 | 0 | if (!read_oneliner(&buf, rebase_path_orig_head(), 0) || |
5091 | 0 | get_oid_hex(buf.buf, &orig)) { |
5092 | 0 | res = error(_("could not read orig-head")); |
5093 | 0 | goto cleanup_head_ref; |
5094 | 0 | } |
5095 | 0 | strbuf_reset(&buf); |
5096 | 0 | if (!read_oneliner(&buf, rebase_path_onto(), 0)) { |
5097 | 0 | res = error(_("could not read 'onto'")); |
5098 | 0 | goto cleanup_head_ref; |
5099 | 0 | } |
5100 | 0 | msg = reflog_message(opts, "finish", "%s onto %s", |
5101 | 0 | head_ref.buf, buf.buf); |
5102 | 0 | if (refs_update_ref(get_main_ref_store(the_repository), msg, head_ref.buf, &head, &orig, |
5103 | 0 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) { |
5104 | 0 | res = error(_("could not update %s"), |
5105 | 0 | head_ref.buf); |
5106 | 0 | goto cleanup_head_ref; |
5107 | 0 | } |
5108 | 0 | msg = reflog_message(opts, "finish", "returning to %s", |
5109 | 0 | head_ref.buf); |
5110 | 0 | if (refs_update_symref(get_main_ref_store(the_repository), "HEAD", head_ref.buf, msg)) { |
5111 | 0 | res = error(_("could not update HEAD to %s"), |
5112 | 0 | head_ref.buf); |
5113 | 0 | goto cleanup_head_ref; |
5114 | 0 | } |
5115 | 0 | strbuf_reset(&buf); |
5116 | 0 | } |
5117 | | |
5118 | 0 | if (opts->verbose) { |
5119 | 0 | struct rev_info log_tree_opt; |
5120 | 0 | struct object_id orig, head; |
5121 | |
|
5122 | 0 | memset(&log_tree_opt, 0, sizeof(log_tree_opt)); |
5123 | 0 | repo_init_revisions(r, &log_tree_opt, NULL); |
5124 | 0 | log_tree_opt.diff = 1; |
5125 | 0 | log_tree_opt.diffopt.output_format = |
5126 | 0 | DIFF_FORMAT_DIFFSTAT; |
5127 | 0 | log_tree_opt.disable_stdin = 1; |
5128 | |
|
5129 | 0 | if (read_oneliner(&buf, rebase_path_orig_head(), 0) && |
5130 | 0 | !repo_get_oid(r, buf.buf, &orig) && |
5131 | 0 | !repo_get_oid(r, "HEAD", &head)) { |
5132 | 0 | diff_tree_oid(&orig, &head, "", |
5133 | 0 | &log_tree_opt.diffopt); |
5134 | 0 | log_tree_diff_flush(&log_tree_opt); |
5135 | 0 | } |
5136 | 0 | release_revisions(&log_tree_opt); |
5137 | 0 | } |
5138 | 0 | flush_rewritten_pending(); |
5139 | 0 | if (!stat(rebase_path_rewritten_list(), &st) && |
5140 | 0 | st.st_size > 0) { |
5141 | 0 | struct child_process child = CHILD_PROCESS_INIT; |
5142 | 0 | struct run_hooks_opt hook_opt = RUN_HOOKS_OPT_INIT; |
5143 | |
|
5144 | 0 | child.in = open(rebase_path_rewritten_list(), O_RDONLY); |
5145 | 0 | child.git_cmd = 1; |
5146 | 0 | strvec_push(&child.args, "notes"); |
5147 | 0 | strvec_push(&child.args, "copy"); |
5148 | 0 | strvec_push(&child.args, "--for-rewrite=rebase"); |
5149 | | /* we don't care if this copying failed */ |
5150 | 0 | run_command(&child); |
5151 | |
|
5152 | 0 | hook_opt.path_to_stdin = rebase_path_rewritten_list(); |
5153 | 0 | strvec_push(&hook_opt.args, "rebase"); |
5154 | 0 | run_hooks_opt(r, "post-rewrite", &hook_opt); |
5155 | 0 | } |
5156 | 0 | apply_autostash(rebase_path_autostash()); |
5157 | |
|
5158 | 0 | if (!opts->quiet) { |
5159 | 0 | if (!opts->verbose) |
5160 | 0 | term_clear_line(); |
5161 | 0 | fprintf(stderr, |
5162 | 0 | _("Successfully rebased and updated %s.\n"), |
5163 | 0 | head_ref.buf); |
5164 | 0 | } |
5165 | |
|
5166 | 0 | strbuf_release(&buf); |
5167 | 0 | strbuf_release(&head_ref); |
5168 | |
|
5169 | 0 | if (do_update_refs(r, opts->quiet)) |
5170 | 0 | return -1; |
5171 | 0 | } |
5172 | | |
5173 | | /* |
5174 | | * Sequence of picks finished successfully; cleanup by |
5175 | | * removing the .git/sequencer directory |
5176 | | */ |
5177 | 0 | return sequencer_remove_state(opts); |
5178 | 0 | } |
5179 | | |
5180 | | static int continue_single_pick(struct repository *r, struct replay_opts *opts) |
5181 | 0 | { |
5182 | 0 | struct child_process cmd = CHILD_PROCESS_INIT; |
5183 | |
|
5184 | 0 | if (!refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") && |
5185 | 0 | !refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD")) |
5186 | 0 | return error(_("no cherry-pick or revert in progress")); |
5187 | | |
5188 | 0 | cmd.git_cmd = 1; |
5189 | 0 | strvec_push(&cmd.args, "commit"); |
5190 | | |
5191 | | /* |
5192 | | * continue_single_pick() handles the case of recovering from a |
5193 | | * conflict. should_edit() doesn't handle that case; for a conflict, |
5194 | | * we want to edit if the user asked for it, or if they didn't specify |
5195 | | * and stdin is a tty. |
5196 | | */ |
5197 | 0 | if (!opts->edit || (opts->edit < 0 && !isatty(0))) |
5198 | | /* |
5199 | | * Include --cleanup=strip as well because we don't want the |
5200 | | * "# Conflicts:" messages. |
5201 | | */ |
5202 | 0 | strvec_pushl(&cmd.args, "--no-edit", "--cleanup=strip", NULL); |
5203 | |
|
5204 | 0 | return run_command(&cmd); |
5205 | 0 | } |
5206 | | |
5207 | | static int commit_staged_changes(struct repository *r, |
5208 | | struct replay_opts *opts, |
5209 | | struct todo_list *todo_list) |
5210 | 0 | { |
5211 | 0 | struct replay_ctx *ctx = opts->ctx; |
5212 | 0 | unsigned int flags = ALLOW_EMPTY | EDIT_MSG; |
5213 | 0 | unsigned int final_fixup = 0, is_clean; |
5214 | 0 | struct strbuf rev = STRBUF_INIT; |
5215 | 0 | int ret; |
5216 | |
|
5217 | 0 | if (has_unstaged_changes(r, 1)) { |
5218 | 0 | ret = error(_("cannot rebase: You have unstaged changes.")); |
5219 | 0 | goto out; |
5220 | 0 | } |
5221 | | |
5222 | 0 | is_clean = !has_uncommitted_changes(r, 0); |
5223 | |
|
5224 | 0 | if (!is_clean && !file_exists(rebase_path_message())) { |
5225 | 0 | const char *gpg_opt = gpg_sign_opt_quoted(opts); |
5226 | 0 | ret = error(_(staged_changes_advice), gpg_opt, gpg_opt); |
5227 | 0 | goto out; |
5228 | 0 | } |
5229 | | |
5230 | 0 | if (file_exists(rebase_path_amend())) { |
5231 | 0 | struct object_id head, to_amend; |
5232 | |
|
5233 | 0 | if (repo_get_oid(r, "HEAD", &head)) { |
5234 | 0 | ret = error(_("cannot amend non-existing commit")); |
5235 | 0 | goto out; |
5236 | 0 | } |
5237 | | |
5238 | 0 | if (!read_oneliner(&rev, rebase_path_amend(), 0)) { |
5239 | 0 | ret = error(_("invalid file: '%s'"), rebase_path_amend()); |
5240 | 0 | goto out; |
5241 | 0 | } |
5242 | | |
5243 | 0 | if (get_oid_hex(rev.buf, &to_amend)) { |
5244 | 0 | ret = error(_("invalid contents: '%s'"), |
5245 | 0 | rebase_path_amend()); |
5246 | 0 | goto out; |
5247 | 0 | } |
5248 | 0 | if (!is_clean && !oideq(&head, &to_amend)) { |
5249 | 0 | ret = error(_("\nYou have uncommitted changes in your " |
5250 | 0 | "working tree. Please, commit them\n" |
5251 | 0 | "first and then run 'git rebase " |
5252 | 0 | "--continue' again.")); |
5253 | 0 | goto out; |
5254 | 0 | } |
5255 | | /* |
5256 | | * When skipping a failed fixup/squash, we need to edit the |
5257 | | * commit message, the current fixup list and count, and if it |
5258 | | * was the last fixup/squash in the chain, we need to clean up |
5259 | | * the commit message and if there was a squash, let the user |
5260 | | * edit it. |
5261 | | */ |
5262 | 0 | if (!is_clean || !ctx->current_fixup_count) |
5263 | 0 | ; /* this is not the final fixup */ |
5264 | 0 | else if (!oideq(&head, &to_amend) || |
5265 | 0 | !file_exists(rebase_path_stopped_sha())) { |
5266 | | /* was a final fixup or squash done manually? */ |
5267 | 0 | if (!is_fixup(peek_command(todo_list, 0))) { |
5268 | 0 | unlink(rebase_path_fixup_msg()); |
5269 | 0 | unlink(rebase_path_squash_msg()); |
5270 | 0 | unlink(rebase_path_current_fixups()); |
5271 | 0 | strbuf_reset(&ctx->current_fixups); |
5272 | 0 | ctx->current_fixup_count = 0; |
5273 | 0 | } |
5274 | 0 | } else { |
5275 | | /* we are in a fixup/squash chain */ |
5276 | 0 | const char *p = ctx->current_fixups.buf; |
5277 | 0 | int len = ctx->current_fixups.len; |
5278 | |
|
5279 | 0 | ctx->current_fixup_count--; |
5280 | 0 | if (!len) |
5281 | 0 | BUG("Incorrect current_fixups:\n%s", p); |
5282 | 0 | while (len && p[len - 1] != '\n') |
5283 | 0 | len--; |
5284 | 0 | strbuf_setlen(&ctx->current_fixups, len); |
5285 | 0 | if (write_message(p, len, rebase_path_current_fixups(), |
5286 | 0 | 0) < 0) { |
5287 | 0 | ret = error(_("could not write file: '%s'"), |
5288 | 0 | rebase_path_current_fixups()); |
5289 | 0 | goto out; |
5290 | 0 | } |
5291 | | |
5292 | | /* |
5293 | | * If a fixup/squash in a fixup/squash chain failed, the |
5294 | | * commit message is already correct, no need to commit |
5295 | | * it again. |
5296 | | * |
5297 | | * Only if it is the final command in the fixup/squash |
5298 | | * chain, and only if the chain is longer than a single |
5299 | | * fixup/squash command (which was just skipped), do we |
5300 | | * actually need to re-commit with a cleaned up commit |
5301 | | * message. |
5302 | | */ |
5303 | 0 | if (ctx->current_fixup_count > 0 && |
5304 | 0 | !is_fixup(peek_command(todo_list, 0))) { |
5305 | 0 | final_fixup = 1; |
5306 | | /* |
5307 | | * If there was not a single "squash" in the |
5308 | | * chain, we only need to clean up the commit |
5309 | | * message, no need to bother the user with |
5310 | | * opening the commit message in the editor. |
5311 | | */ |
5312 | 0 | if (!starts_with(p, "squash ") && |
5313 | 0 | !strstr(p, "\nsquash ")) |
5314 | 0 | flags = (flags & ~EDIT_MSG) | CLEANUP_MSG; |
5315 | 0 | } else if (is_fixup(peek_command(todo_list, 0))) { |
5316 | | /* |
5317 | | * We need to update the squash message to skip |
5318 | | * the latest commit message. |
5319 | | */ |
5320 | 0 | struct commit *commit; |
5321 | 0 | const char *msg; |
5322 | 0 | const char *path = rebase_path_squash_msg(); |
5323 | 0 | const char *encoding = get_commit_output_encoding(); |
5324 | |
|
5325 | 0 | if (parse_head(r, &commit)) { |
5326 | 0 | ret = error(_("could not parse HEAD")); |
5327 | 0 | goto out; |
5328 | 0 | } |
5329 | | |
5330 | 0 | p = repo_logmsg_reencode(r, commit, NULL, encoding); |
5331 | 0 | if (!p) { |
5332 | 0 | ret = error(_("could not parse commit %s"), |
5333 | 0 | oid_to_hex(&commit->object.oid)); |
5334 | 0 | goto unuse_commit_buffer; |
5335 | 0 | } |
5336 | 0 | find_commit_subject(p, &msg); |
5337 | 0 | if (write_message(msg, strlen(msg), path, 0)) { |
5338 | 0 | ret = error(_("could not write file: " |
5339 | 0 | "'%s'"), path); |
5340 | 0 | goto unuse_commit_buffer; |
5341 | 0 | } |
5342 | | |
5343 | 0 | ret = 0; |
5344 | |
|
5345 | 0 | unuse_commit_buffer: |
5346 | 0 | repo_unuse_commit_buffer(r, commit, p); |
5347 | 0 | if (ret) |
5348 | 0 | goto out; |
5349 | 0 | } |
5350 | 0 | } |
5351 | | |
5352 | 0 | flags |= AMEND_MSG; |
5353 | 0 | } |
5354 | | |
5355 | 0 | if (is_clean) { |
5356 | 0 | if (refs_ref_exists(get_main_ref_store(r), |
5357 | 0 | "CHERRY_PICK_HEAD") && |
5358 | 0 | refs_delete_ref(get_main_ref_store(r), "", |
5359 | 0 | "CHERRY_PICK_HEAD", NULL, REF_NO_DEREF)) { |
5360 | 0 | ret = error(_("could not remove CHERRY_PICK_HEAD")); |
5361 | 0 | goto out; |
5362 | 0 | } |
5363 | | |
5364 | 0 | if (unlink(git_path_merge_msg(r)) && errno != ENOENT) { |
5365 | 0 | ret = error_errno(_("could not remove '%s'"), |
5366 | 0 | git_path_merge_msg(r)); |
5367 | 0 | goto out; |
5368 | 0 | } |
5369 | | |
5370 | 0 | if (!final_fixup) { |
5371 | 0 | ret = 0; |
5372 | 0 | goto out; |
5373 | 0 | } |
5374 | 0 | } |
5375 | | |
5376 | 0 | if (run_git_commit(final_fixup ? NULL : rebase_path_message(), |
5377 | 0 | opts, flags)) { |
5378 | 0 | ret = error(_("could not commit staged changes.")); |
5379 | 0 | goto out; |
5380 | 0 | } |
5381 | | |
5382 | 0 | unlink(rebase_path_amend()); |
5383 | 0 | unlink(git_path_merge_head(r)); |
5384 | 0 | refs_delete_ref(get_main_ref_store(r), "", "AUTO_MERGE", |
5385 | 0 | NULL, REF_NO_DEREF); |
5386 | 0 | if (final_fixup) { |
5387 | 0 | unlink(rebase_path_fixup_msg()); |
5388 | 0 | unlink(rebase_path_squash_msg()); |
5389 | 0 | } |
5390 | 0 | if (ctx->current_fixup_count > 0) { |
5391 | | /* |
5392 | | * Whether final fixup or not, we just cleaned up the commit |
5393 | | * message... |
5394 | | */ |
5395 | 0 | unlink(rebase_path_current_fixups()); |
5396 | 0 | strbuf_reset(&ctx->current_fixups); |
5397 | 0 | ctx->current_fixup_count = 0; |
5398 | 0 | } |
5399 | |
|
5400 | 0 | ret = 0; |
5401 | |
|
5402 | 0 | out: |
5403 | 0 | strbuf_release(&rev); |
5404 | 0 | return ret; |
5405 | 0 | } |
5406 | | |
5407 | | int sequencer_continue(struct repository *r, struct replay_opts *opts) |
5408 | 0 | { |
5409 | 0 | struct replay_ctx *ctx = opts->ctx; |
5410 | 0 | struct todo_list todo_list = TODO_LIST_INIT; |
5411 | 0 | int res; |
5412 | |
|
5413 | 0 | if (read_and_refresh_cache(r, opts)) |
5414 | 0 | return -1; |
5415 | | |
5416 | 0 | if (read_populate_opts(opts)) |
5417 | 0 | return -1; |
5418 | 0 | if (is_rebase_i(opts)) { |
5419 | 0 | if ((res = read_populate_todo(r, &todo_list, opts))) |
5420 | 0 | goto release_todo_list; |
5421 | | |
5422 | 0 | if (file_exists(rebase_path_dropped())) { |
5423 | 0 | if ((res = todo_list_check_against_backup(r, opts, |
5424 | 0 | &todo_list))) |
5425 | 0 | goto release_todo_list; |
5426 | | |
5427 | 0 | unlink(rebase_path_dropped()); |
5428 | 0 | } |
5429 | | |
5430 | 0 | ctx->reflog_message = reflog_message(opts, "continue", NULL); |
5431 | 0 | if (commit_staged_changes(r, opts, &todo_list)) { |
5432 | 0 | res = -1; |
5433 | 0 | goto release_todo_list; |
5434 | 0 | } |
5435 | 0 | } else if (!file_exists(get_todo_path(opts))) |
5436 | 0 | return continue_single_pick(r, opts); |
5437 | 0 | else if ((res = read_populate_todo(r, &todo_list, opts))) |
5438 | 0 | goto release_todo_list; |
5439 | | |
5440 | 0 | if (!is_rebase_i(opts)) { |
5441 | | /* Verify that the conflict has been resolved */ |
5442 | 0 | if (refs_ref_exists(get_main_ref_store(r), |
5443 | 0 | "CHERRY_PICK_HEAD") || |
5444 | 0 | refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD")) { |
5445 | 0 | res = continue_single_pick(r, opts); |
5446 | 0 | if (res) |
5447 | 0 | goto release_todo_list; |
5448 | 0 | } |
5449 | 0 | if (index_differs_from(r, "HEAD", NULL, 0)) { |
5450 | 0 | res = error_dirty_index(r, opts); |
5451 | 0 | goto release_todo_list; |
5452 | 0 | } |
5453 | 0 | todo_list.current++; |
5454 | 0 | } else if (file_exists(rebase_path_stopped_sha())) { |
5455 | 0 | struct strbuf buf = STRBUF_INIT; |
5456 | 0 | struct object_id oid; |
5457 | |
|
5458 | 0 | if (read_oneliner(&buf, rebase_path_stopped_sha(), |
5459 | 0 | READ_ONELINER_SKIP_IF_EMPTY) && |
5460 | 0 | !get_oid_hex(buf.buf, &oid)) |
5461 | 0 | record_in_rewritten(&oid, peek_command(&todo_list, 0)); |
5462 | 0 | strbuf_release(&buf); |
5463 | 0 | } |
5464 | | |
5465 | 0 | res = pick_commits(r, &todo_list, opts); |
5466 | 0 | release_todo_list: |
5467 | 0 | todo_list_release(&todo_list); |
5468 | 0 | return res; |
5469 | 0 | } |
5470 | | |
5471 | | static int single_pick(struct repository *r, |
5472 | | struct commit *cmit, |
5473 | | struct replay_opts *opts) |
5474 | 0 | { |
5475 | 0 | int check_todo; |
5476 | 0 | struct todo_item item; |
5477 | |
|
5478 | 0 | item.command = opts->action == REPLAY_PICK ? |
5479 | 0 | TODO_PICK : TODO_REVERT; |
5480 | 0 | item.commit = cmit; |
5481 | |
|
5482 | 0 | opts->ctx->reflog_message = sequencer_reflog_action(opts); |
5483 | 0 | return do_pick_commit(r, &item, opts, 0, &check_todo); |
5484 | 0 | } |
5485 | | |
5486 | | int sequencer_pick_revisions(struct repository *r, |
5487 | | struct replay_opts *opts) |
5488 | 0 | { |
5489 | 0 | struct todo_list todo_list = TODO_LIST_INIT; |
5490 | 0 | struct object_id oid; |
5491 | 0 | int i, res; |
5492 | |
|
5493 | 0 | assert(opts->revs); |
5494 | 0 | if (read_and_refresh_cache(r, opts)) { |
5495 | 0 | res = -1; |
5496 | 0 | goto out; |
5497 | 0 | } |
5498 | | |
5499 | 0 | for (i = 0; i < opts->revs->pending.nr; i++) { |
5500 | 0 | struct object_id oid; |
5501 | 0 | const char *name = opts->revs->pending.objects[i].name; |
5502 | | |
5503 | | /* This happens when using --stdin. */ |
5504 | 0 | if (!strlen(name)) |
5505 | 0 | continue; |
5506 | | |
5507 | 0 | if (!repo_get_oid(r, name, &oid)) { |
5508 | 0 | if (!lookup_commit_reference_gently(r, &oid, 1)) { |
5509 | 0 | enum object_type type = oid_object_info(r, |
5510 | 0 | &oid, |
5511 | 0 | NULL); |
5512 | 0 | res = error(_("%s: can't cherry-pick a %s"), |
5513 | 0 | name, type_name(type)); |
5514 | 0 | goto out; |
5515 | 0 | } |
5516 | 0 | } else { |
5517 | 0 | res = error(_("%s: bad revision"), name); |
5518 | 0 | goto out; |
5519 | 0 | } |
5520 | 0 | } |
5521 | | |
5522 | | /* |
5523 | | * If we were called as "git cherry-pick <commit>", just |
5524 | | * cherry-pick/revert it, set CHERRY_PICK_HEAD / |
5525 | | * REVERT_HEAD, and don't touch the sequencer state. |
5526 | | * This means it is possible to cherry-pick in the middle |
5527 | | * of a cherry-pick sequence. |
5528 | | */ |
5529 | 0 | if (opts->revs->cmdline.nr == 1 && |
5530 | 0 | opts->revs->cmdline.rev->whence == REV_CMD_REV && |
5531 | 0 | opts->revs->no_walk && |
5532 | 0 | !opts->revs->cmdline.rev->flags) { |
5533 | 0 | struct commit *cmit; |
5534 | |
|
5535 | 0 | if (prepare_revision_walk(opts->revs)) { |
5536 | 0 | res = error(_("revision walk setup failed")); |
5537 | 0 | goto out; |
5538 | 0 | } |
5539 | | |
5540 | 0 | cmit = get_revision(opts->revs); |
5541 | 0 | if (!cmit) { |
5542 | 0 | res = error(_("empty commit set passed")); |
5543 | 0 | goto out; |
5544 | 0 | } |
5545 | | |
5546 | 0 | if (get_revision(opts->revs)) |
5547 | 0 | BUG("unexpected extra commit from walk"); |
5548 | | |
5549 | 0 | res = single_pick(r, cmit, opts); |
5550 | 0 | goto out; |
5551 | 0 | } |
5552 | | |
5553 | | /* |
5554 | | * Start a new cherry-pick/ revert sequence; but |
5555 | | * first, make sure that an existing one isn't in |
5556 | | * progress |
5557 | | */ |
5558 | | |
5559 | 0 | if (walk_revs_populate_todo(&todo_list, opts) || |
5560 | 0 | create_seq_dir(r) < 0) { |
5561 | 0 | res = -1; |
5562 | 0 | goto out; |
5563 | 0 | } |
5564 | | |
5565 | 0 | if (repo_get_oid(r, "HEAD", &oid) && (opts->action == REPLAY_REVERT)) { |
5566 | 0 | res = error(_("can't revert as initial commit")); |
5567 | 0 | goto out; |
5568 | 0 | } |
5569 | | |
5570 | 0 | if (save_head(oid_to_hex(&oid))) { |
5571 | 0 | res = -1; |
5572 | 0 | goto out; |
5573 | 0 | } |
5574 | | |
5575 | 0 | if (save_opts(opts)) { |
5576 | 0 | res = -1; |
5577 | 0 | goto out; |
5578 | 0 | } |
5579 | | |
5580 | 0 | update_abort_safety_file(); |
5581 | 0 | res = pick_commits(r, &todo_list, opts); |
5582 | |
|
5583 | 0 | out: |
5584 | 0 | todo_list_release(&todo_list); |
5585 | 0 | return res; |
5586 | 0 | } |
5587 | | |
5588 | | void append_signoff(struct strbuf *msgbuf, size_t ignore_footer, unsigned flag) |
5589 | 0 | { |
5590 | 0 | unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP; |
5591 | 0 | struct strbuf sob = STRBUF_INIT; |
5592 | 0 | int has_footer; |
5593 | |
|
5594 | 0 | strbuf_addstr(&sob, sign_off_header); |
5595 | 0 | strbuf_addstr(&sob, fmt_name(WANT_COMMITTER_IDENT)); |
5596 | 0 | strbuf_addch(&sob, '\n'); |
5597 | |
|
5598 | 0 | if (!ignore_footer) |
5599 | 0 | strbuf_complete_line(msgbuf); |
5600 | | |
5601 | | /* |
5602 | | * If the whole message buffer is equal to the sob, pretend that we |
5603 | | * found a conforming footer with a matching sob |
5604 | | */ |
5605 | 0 | if (msgbuf->len - ignore_footer == sob.len && |
5606 | 0 | !strncmp(msgbuf->buf, sob.buf, sob.len)) |
5607 | 0 | has_footer = 3; |
5608 | 0 | else |
5609 | 0 | has_footer = has_conforming_footer(msgbuf, &sob, ignore_footer); |
5610 | |
|
5611 | 0 | if (!has_footer) { |
5612 | 0 | const char *append_newlines = NULL; |
5613 | 0 | size_t len = msgbuf->len - ignore_footer; |
5614 | |
|
5615 | 0 | if (!len) { |
5616 | | /* |
5617 | | * The buffer is completely empty. Leave foom for |
5618 | | * the title and body to be filled in by the user. |
5619 | | */ |
5620 | 0 | append_newlines = "\n\n"; |
5621 | 0 | } else if (len == 1) { |
5622 | | /* |
5623 | | * Buffer contains a single newline. Add another |
5624 | | * so that we leave room for the title and body. |
5625 | | */ |
5626 | 0 | append_newlines = "\n"; |
5627 | 0 | } else if (msgbuf->buf[len - 2] != '\n') { |
5628 | | /* |
5629 | | * Buffer ends with a single newline. Add another |
5630 | | * so that there is an empty line between the message |
5631 | | * body and the sob. |
5632 | | */ |
5633 | 0 | append_newlines = "\n"; |
5634 | 0 | } /* else, the buffer already ends with two newlines. */ |
5635 | |
|
5636 | 0 | if (append_newlines) |
5637 | 0 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, |
5638 | 0 | append_newlines, strlen(append_newlines)); |
5639 | 0 | } |
5640 | |
|
5641 | 0 | if (has_footer != 3 && (!no_dup_sob || has_footer != 2)) |
5642 | 0 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, |
5643 | 0 | sob.buf, sob.len); |
5644 | |
|
5645 | 0 | strbuf_release(&sob); |
5646 | 0 | } |
5647 | | |
5648 | | struct labels_entry { |
5649 | | struct hashmap_entry entry; |
5650 | | char label[FLEX_ARRAY]; |
5651 | | }; |
5652 | | |
5653 | | static int labels_cmp(const void *fndata UNUSED, |
5654 | | const struct hashmap_entry *eptr, |
5655 | | const struct hashmap_entry *entry_or_key, const void *key) |
5656 | 0 | { |
5657 | 0 | const struct labels_entry *a, *b; |
5658 | |
|
5659 | 0 | a = container_of(eptr, const struct labels_entry, entry); |
5660 | 0 | b = container_of(entry_or_key, const struct labels_entry, entry); |
5661 | |
|
5662 | 0 | return key ? strcmp(a->label, key) : strcmp(a->label, b->label); |
5663 | 0 | } |
5664 | | |
5665 | | struct string_entry { |
5666 | | struct oidmap_entry entry; |
5667 | | char string[FLEX_ARRAY]; |
5668 | | }; |
5669 | | |
5670 | | struct label_state { |
5671 | | struct oidmap commit2label; |
5672 | | struct hashmap labels; |
5673 | | struct strbuf buf; |
5674 | | int max_label_length; |
5675 | | }; |
5676 | | |
5677 | | static const char *label_oid(struct object_id *oid, const char *label, |
5678 | | struct label_state *state) |
5679 | 0 | { |
5680 | 0 | struct labels_entry *labels_entry; |
5681 | 0 | struct string_entry *string_entry; |
5682 | 0 | struct object_id dummy; |
5683 | 0 | int i; |
5684 | |
|
5685 | 0 | string_entry = oidmap_get(&state->commit2label, oid); |
5686 | 0 | if (string_entry) |
5687 | 0 | return string_entry->string; |
5688 | | |
5689 | | /* |
5690 | | * For "uninteresting" commits, i.e. commits that are not to be |
5691 | | * rebased, and which can therefore not be labeled, we use a unique |
5692 | | * abbreviation of the commit name. This is slightly more complicated |
5693 | | * than calling repo_find_unique_abbrev() because we also need to make |
5694 | | * sure that the abbreviation does not conflict with any other |
5695 | | * label. |
5696 | | * |
5697 | | * We disallow "interesting" commits to be labeled by a string that |
5698 | | * is a valid full-length hash, to ensure that we always can find an |
5699 | | * abbreviation for any uninteresting commit's names that does not |
5700 | | * clash with any other label. |
5701 | | */ |
5702 | 0 | strbuf_reset(&state->buf); |
5703 | 0 | if (!label) { |
5704 | 0 | char *p; |
5705 | |
|
5706 | 0 | strbuf_grow(&state->buf, GIT_MAX_HEXSZ); |
5707 | 0 | label = p = state->buf.buf; |
5708 | |
|
5709 | 0 | repo_find_unique_abbrev_r(the_repository, p, oid, |
5710 | 0 | default_abbrev); |
5711 | | |
5712 | | /* |
5713 | | * We may need to extend the abbreviated hash so that there is |
5714 | | * no conflicting label. |
5715 | | */ |
5716 | 0 | if (hashmap_get_from_hash(&state->labels, strihash(p), p)) { |
5717 | 0 | size_t i = strlen(p) + 1; |
5718 | |
|
5719 | 0 | oid_to_hex_r(p, oid); |
5720 | 0 | for (; i < the_hash_algo->hexsz; i++) { |
5721 | 0 | char save = p[i]; |
5722 | 0 | p[i] = '\0'; |
5723 | 0 | if (!hashmap_get_from_hash(&state->labels, |
5724 | 0 | strihash(p), p)) |
5725 | 0 | break; |
5726 | 0 | p[i] = save; |
5727 | 0 | } |
5728 | 0 | } |
5729 | 0 | } else { |
5730 | 0 | struct strbuf *buf = &state->buf; |
5731 | 0 | int label_is_utf8 = 1; /* start with this assumption */ |
5732 | 0 | size_t max_len = buf->len + state->max_label_length; |
5733 | | |
5734 | | /* |
5735 | | * Sanitize labels by replacing non-alpha-numeric characters |
5736 | | * (including white-space ones) by dashes, as they might be |
5737 | | * illegal in file names (and hence in ref names). |
5738 | | * |
5739 | | * Note that we retain non-ASCII UTF-8 characters (identified |
5740 | | * via the most significant bit). They should be all acceptable |
5741 | | * in file names. |
5742 | | * |
5743 | | * As we will use the labels as names of (loose) refs, it is |
5744 | | * vital that the name not be longer than the maximum component |
5745 | | * size of the file system (`NAME_MAX`). We are careful to |
5746 | | * truncate the label accordingly, allowing for the `.lock` |
5747 | | * suffix and for the label to be UTF-8 encoded (i.e. we avoid |
5748 | | * truncating in the middle of a character). |
5749 | | */ |
5750 | 0 | for (; *label && buf->len + 1 < max_len; label++) |
5751 | 0 | if (isalnum(*label) || |
5752 | 0 | (!label_is_utf8 && (*label & 0x80))) |
5753 | 0 | strbuf_addch(buf, *label); |
5754 | 0 | else if (*label & 0x80) { |
5755 | 0 | const char *p = label; |
5756 | |
|
5757 | 0 | utf8_width(&p, NULL); |
5758 | 0 | if (p) { |
5759 | 0 | if (buf->len + (p - label) > max_len) |
5760 | 0 | break; |
5761 | 0 | strbuf_add(buf, label, p - label); |
5762 | 0 | label = p - 1; |
5763 | 0 | } else { |
5764 | 0 | label_is_utf8 = 0; |
5765 | 0 | strbuf_addch(buf, *label); |
5766 | 0 | } |
5767 | | /* avoid leading dash and double-dashes */ |
5768 | 0 | } else if (buf->len && buf->buf[buf->len - 1] != '-') |
5769 | 0 | strbuf_addch(buf, '-'); |
5770 | 0 | if (!buf->len) { |
5771 | 0 | strbuf_addstr(buf, "rev-"); |
5772 | 0 | strbuf_add_unique_abbrev(buf, oid, default_abbrev); |
5773 | 0 | } |
5774 | 0 | label = buf->buf; |
5775 | |
|
5776 | 0 | if ((buf->len == the_hash_algo->hexsz && |
5777 | 0 | !get_oid_hex(label, &dummy)) || |
5778 | 0 | (buf->len == 1 && *label == '#') || |
5779 | 0 | hashmap_get_from_hash(&state->labels, |
5780 | 0 | strihash(label), label)) { |
5781 | | /* |
5782 | | * If the label already exists, or if the label is a |
5783 | | * valid full OID, or the label is a '#' (which we use |
5784 | | * as a separator between merge heads and oneline), we |
5785 | | * append a dash and a number to make it unique. |
5786 | | */ |
5787 | 0 | size_t len = buf->len; |
5788 | |
|
5789 | 0 | for (i = 2; ; i++) { |
5790 | 0 | strbuf_setlen(buf, len); |
5791 | 0 | strbuf_addf(buf, "-%d", i); |
5792 | 0 | if (!hashmap_get_from_hash(&state->labels, |
5793 | 0 | strihash(buf->buf), |
5794 | 0 | buf->buf)) |
5795 | 0 | break; |
5796 | 0 | } |
5797 | |
|
5798 | 0 | label = buf->buf; |
5799 | 0 | } |
5800 | 0 | } |
5801 | |
|
5802 | 0 | FLEX_ALLOC_STR(labels_entry, label, label); |
5803 | 0 | hashmap_entry_init(&labels_entry->entry, strihash(label)); |
5804 | 0 | hashmap_add(&state->labels, &labels_entry->entry); |
5805 | |
|
5806 | 0 | FLEX_ALLOC_STR(string_entry, string, label); |
5807 | 0 | oidcpy(&string_entry->entry.oid, oid); |
5808 | 0 | oidmap_put(&state->commit2label, string_entry); |
5809 | |
|
5810 | 0 | return string_entry->string; |
5811 | 0 | } |
5812 | | |
5813 | | static int make_script_with_merges(struct pretty_print_context *pp, |
5814 | | struct rev_info *revs, struct strbuf *out, |
5815 | | unsigned flags) |
5816 | 0 | { |
5817 | 0 | int keep_empty = flags & TODO_LIST_KEEP_EMPTY; |
5818 | 0 | int rebase_cousins = flags & TODO_LIST_REBASE_COUSINS; |
5819 | 0 | int root_with_onto = flags & TODO_LIST_ROOT_WITH_ONTO; |
5820 | 0 | int skipped_commit = 0; |
5821 | 0 | struct strbuf buf = STRBUF_INIT, oneline = STRBUF_INIT; |
5822 | 0 | struct strbuf label = STRBUF_INIT; |
5823 | 0 | struct commit_list *commits = NULL, **tail = &commits, *iter; |
5824 | 0 | struct commit_list *tips = NULL, **tips_tail = &tips; |
5825 | 0 | struct commit *commit; |
5826 | 0 | struct oidmap commit2todo = OIDMAP_INIT; |
5827 | 0 | struct string_entry *entry; |
5828 | 0 | struct oidset interesting = OIDSET_INIT, child_seen = OIDSET_INIT, |
5829 | 0 | shown = OIDSET_INIT; |
5830 | 0 | struct label_state state = |
5831 | 0 | { OIDMAP_INIT, { NULL }, STRBUF_INIT, GIT_MAX_LABEL_LENGTH }; |
5832 | |
|
5833 | 0 | int abbr = flags & TODO_LIST_ABBREVIATE_CMDS; |
5834 | 0 | const char *cmd_pick = abbr ? "p" : "pick", |
5835 | 0 | *cmd_label = abbr ? "l" : "label", |
5836 | 0 | *cmd_reset = abbr ? "t" : "reset", |
5837 | 0 | *cmd_merge = abbr ? "m" : "merge"; |
5838 | |
|
5839 | 0 | git_config_get_int("rebase.maxlabellength", &state.max_label_length); |
5840 | |
|
5841 | 0 | oidmap_init(&commit2todo, 0); |
5842 | 0 | oidmap_init(&state.commit2label, 0); |
5843 | 0 | hashmap_init(&state.labels, labels_cmp, NULL, 0); |
5844 | 0 | strbuf_init(&state.buf, 32); |
5845 | |
|
5846 | 0 | if (revs->cmdline.nr && (revs->cmdline.rev[0].flags & BOTTOM)) { |
5847 | 0 | struct labels_entry *onto_label_entry; |
5848 | 0 | struct object_id *oid = &revs->cmdline.rev[0].item->oid; |
5849 | 0 | FLEX_ALLOC_STR(entry, string, "onto"); |
5850 | 0 | oidcpy(&entry->entry.oid, oid); |
5851 | 0 | oidmap_put(&state.commit2label, entry); |
5852 | |
|
5853 | 0 | FLEX_ALLOC_STR(onto_label_entry, label, "onto"); |
5854 | 0 | hashmap_entry_init(&onto_label_entry->entry, strihash("onto")); |
5855 | 0 | hashmap_add(&state.labels, &onto_label_entry->entry); |
5856 | 0 | } |
5857 | | |
5858 | | /* |
5859 | | * First phase: |
5860 | | * - get onelines for all commits |
5861 | | * - gather all branch tips (i.e. 2nd or later parents of merges) |
5862 | | * - label all branch tips |
5863 | | */ |
5864 | 0 | while ((commit = get_revision(revs))) { |
5865 | 0 | struct commit_list *to_merge; |
5866 | 0 | const char *p1, *p2; |
5867 | 0 | struct object_id *oid; |
5868 | 0 | int is_empty; |
5869 | |
|
5870 | 0 | tail = &commit_list_insert(commit, tail)->next; |
5871 | 0 | oidset_insert(&interesting, &commit->object.oid); |
5872 | |
|
5873 | 0 | is_empty = is_original_commit_empty(commit); |
5874 | 0 | if (!is_empty && (commit->object.flags & PATCHSAME)) { |
5875 | 0 | if (flags & TODO_LIST_WARN_SKIPPED_CHERRY_PICKS) |
5876 | 0 | warning(_("skipped previously applied commit %s"), |
5877 | 0 | short_commit_name(the_repository, commit)); |
5878 | 0 | skipped_commit = 1; |
5879 | 0 | continue; |
5880 | 0 | } |
5881 | 0 | if (is_empty && !keep_empty) |
5882 | 0 | continue; |
5883 | | |
5884 | 0 | strbuf_reset(&oneline); |
5885 | 0 | pretty_print_commit(pp, commit, &oneline); |
5886 | |
|
5887 | 0 | to_merge = commit->parents ? commit->parents->next : NULL; |
5888 | 0 | if (!to_merge) { |
5889 | | /* non-merge commit: easy case */ |
5890 | 0 | strbuf_reset(&buf); |
5891 | 0 | strbuf_addf(&buf, "%s %s %s", cmd_pick, |
5892 | 0 | oid_to_hex(&commit->object.oid), |
5893 | 0 | oneline.buf); |
5894 | 0 | if (is_empty) |
5895 | 0 | strbuf_addf(&buf, " %s empty", |
5896 | 0 | comment_line_str); |
5897 | |
|
5898 | 0 | FLEX_ALLOC_STR(entry, string, buf.buf); |
5899 | 0 | oidcpy(&entry->entry.oid, &commit->object.oid); |
5900 | 0 | oidmap_put(&commit2todo, entry); |
5901 | |
|
5902 | 0 | continue; |
5903 | 0 | } |
5904 | | |
5905 | | /* Create a label */ |
5906 | 0 | strbuf_reset(&label); |
5907 | 0 | if (skip_prefix(oneline.buf, "Merge ", &p1) && |
5908 | 0 | (p1 = strchr(p1, '\'')) && |
5909 | 0 | (p2 = strchr(++p1, '\''))) |
5910 | 0 | strbuf_add(&label, p1, p2 - p1); |
5911 | 0 | else if (skip_prefix(oneline.buf, "Merge pull request ", |
5912 | 0 | &p1) && |
5913 | 0 | (p1 = strstr(p1, " from "))) |
5914 | 0 | strbuf_addstr(&label, p1 + strlen(" from ")); |
5915 | 0 | else |
5916 | 0 | strbuf_addbuf(&label, &oneline); |
5917 | |
|
5918 | 0 | strbuf_reset(&buf); |
5919 | 0 | strbuf_addf(&buf, "%s -C %s", |
5920 | 0 | cmd_merge, oid_to_hex(&commit->object.oid)); |
5921 | | |
5922 | | /* label the tips of merged branches */ |
5923 | 0 | for (; to_merge; to_merge = to_merge->next) { |
5924 | 0 | oid = &to_merge->item->object.oid; |
5925 | 0 | strbuf_addch(&buf, ' '); |
5926 | |
|
5927 | 0 | if (!oidset_contains(&interesting, oid)) { |
5928 | 0 | strbuf_addstr(&buf, label_oid(oid, NULL, |
5929 | 0 | &state)); |
5930 | 0 | continue; |
5931 | 0 | } |
5932 | | |
5933 | 0 | tips_tail = &commit_list_insert(to_merge->item, |
5934 | 0 | tips_tail)->next; |
5935 | |
|
5936 | 0 | strbuf_addstr(&buf, label_oid(oid, label.buf, &state)); |
5937 | 0 | } |
5938 | 0 | strbuf_addf(&buf, " # %s", oneline.buf); |
5939 | |
|
5940 | 0 | FLEX_ALLOC_STR(entry, string, buf.buf); |
5941 | 0 | oidcpy(&entry->entry.oid, &commit->object.oid); |
5942 | 0 | oidmap_put(&commit2todo, entry); |
5943 | 0 | } |
5944 | 0 | if (skipped_commit) |
5945 | 0 | advise_if_enabled(ADVICE_SKIPPED_CHERRY_PICKS, |
5946 | 0 | _("use --reapply-cherry-picks to include skipped commits")); |
5947 | | |
5948 | | /* |
5949 | | * Second phase: |
5950 | | * - label branch points |
5951 | | * - add HEAD to the branch tips |
5952 | | */ |
5953 | 0 | for (iter = commits; iter; iter = iter->next) { |
5954 | 0 | struct commit_list *parent = iter->item->parents; |
5955 | 0 | for (; parent; parent = parent->next) { |
5956 | 0 | struct object_id *oid = &parent->item->object.oid; |
5957 | 0 | if (!oidset_contains(&interesting, oid)) |
5958 | 0 | continue; |
5959 | 0 | if (oidset_insert(&child_seen, oid)) |
5960 | 0 | label_oid(oid, "branch-point", &state); |
5961 | 0 | } |
5962 | | |
5963 | | /* Add HEAD as implicit "tip of branch" */ |
5964 | 0 | if (!iter->next) |
5965 | 0 | tips_tail = &commit_list_insert(iter->item, |
5966 | 0 | tips_tail)->next; |
5967 | 0 | } |
5968 | | |
5969 | | /* |
5970 | | * Third phase: output the todo list. This is a bit tricky, as we |
5971 | | * want to avoid jumping back and forth between revisions. To |
5972 | | * accomplish that goal, we walk backwards from the branch tips, |
5973 | | * gathering commits not yet shown, reversing the list on the fly, |
5974 | | * then outputting that list (labeling revisions as needed). |
5975 | | */ |
5976 | 0 | strbuf_addf(out, "%s onto\n", cmd_label); |
5977 | 0 | for (iter = tips; iter; iter = iter->next) { |
5978 | 0 | struct commit_list *list = NULL, *iter2; |
5979 | |
|
5980 | 0 | commit = iter->item; |
5981 | 0 | if (oidset_contains(&shown, &commit->object.oid)) |
5982 | 0 | continue; |
5983 | 0 | entry = oidmap_get(&state.commit2label, &commit->object.oid); |
5984 | |
|
5985 | 0 | if (entry) |
5986 | 0 | strbuf_addf(out, "\n%s Branch %s\n", comment_line_str, entry->string); |
5987 | 0 | else |
5988 | 0 | strbuf_addch(out, '\n'); |
5989 | |
|
5990 | 0 | while (oidset_contains(&interesting, &commit->object.oid) && |
5991 | 0 | !oidset_contains(&shown, &commit->object.oid)) { |
5992 | 0 | commit_list_insert(commit, &list); |
5993 | 0 | if (!commit->parents) { |
5994 | 0 | commit = NULL; |
5995 | 0 | break; |
5996 | 0 | } |
5997 | 0 | commit = commit->parents->item; |
5998 | 0 | } |
5999 | |
|
6000 | 0 | if (!commit) |
6001 | 0 | strbuf_addf(out, "%s %s\n", cmd_reset, |
6002 | 0 | rebase_cousins || root_with_onto ? |
6003 | 0 | "onto" : "[new root]"); |
6004 | 0 | else { |
6005 | 0 | const char *to = NULL; |
6006 | |
|
6007 | 0 | entry = oidmap_get(&state.commit2label, |
6008 | 0 | &commit->object.oid); |
6009 | 0 | if (entry) |
6010 | 0 | to = entry->string; |
6011 | 0 | else if (!rebase_cousins) |
6012 | 0 | to = label_oid(&commit->object.oid, NULL, |
6013 | 0 | &state); |
6014 | |
|
6015 | 0 | if (!to || !strcmp(to, "onto")) |
6016 | 0 | strbuf_addf(out, "%s onto\n", cmd_reset); |
6017 | 0 | else { |
6018 | 0 | strbuf_reset(&oneline); |
6019 | 0 | pretty_print_commit(pp, commit, &oneline); |
6020 | 0 | strbuf_addf(out, "%s %s # %s\n", |
6021 | 0 | cmd_reset, to, oneline.buf); |
6022 | 0 | } |
6023 | 0 | } |
6024 | |
|
6025 | 0 | for (iter2 = list; iter2; iter2 = iter2->next) { |
6026 | 0 | struct object_id *oid = &iter2->item->object.oid; |
6027 | 0 | entry = oidmap_get(&commit2todo, oid); |
6028 | | /* only show if not already upstream */ |
6029 | 0 | if (entry) |
6030 | 0 | strbuf_addf(out, "%s\n", entry->string); |
6031 | 0 | entry = oidmap_get(&state.commit2label, oid); |
6032 | 0 | if (entry) |
6033 | 0 | strbuf_addf(out, "%s %s\n", |
6034 | 0 | cmd_label, entry->string); |
6035 | 0 | oidset_insert(&shown, oid); |
6036 | 0 | } |
6037 | |
|
6038 | 0 | free_commit_list(list); |
6039 | 0 | } |
6040 | |
|
6041 | 0 | free_commit_list(commits); |
6042 | 0 | free_commit_list(tips); |
6043 | |
|
6044 | 0 | strbuf_release(&label); |
6045 | 0 | strbuf_release(&oneline); |
6046 | 0 | strbuf_release(&buf); |
6047 | |
|
6048 | 0 | oidset_clear(&interesting); |
6049 | 0 | oidset_clear(&child_seen); |
6050 | 0 | oidset_clear(&shown); |
6051 | 0 | oidmap_free(&commit2todo, 1); |
6052 | 0 | oidmap_free(&state.commit2label, 1); |
6053 | 0 | hashmap_clear_and_free(&state.labels, struct labels_entry, entry); |
6054 | 0 | strbuf_release(&state.buf); |
6055 | |
|
6056 | 0 | return 0; |
6057 | 0 | } |
6058 | | |
6059 | | int sequencer_make_script(struct repository *r, struct strbuf *out, int argc, |
6060 | | const char **argv, unsigned flags) |
6061 | 0 | { |
6062 | 0 | char *format = NULL; |
6063 | 0 | struct pretty_print_context pp = {0}; |
6064 | 0 | struct rev_info revs; |
6065 | 0 | struct commit *commit; |
6066 | 0 | int keep_empty = flags & TODO_LIST_KEEP_EMPTY; |
6067 | 0 | const char *insn = flags & TODO_LIST_ABBREVIATE_CMDS ? "p" : "pick"; |
6068 | 0 | int rebase_merges = flags & TODO_LIST_REBASE_MERGES; |
6069 | 0 | int reapply_cherry_picks = flags & TODO_LIST_REAPPLY_CHERRY_PICKS; |
6070 | 0 | int skipped_commit = 0; |
6071 | 0 | int ret = 0; |
6072 | |
|
6073 | 0 | repo_init_revisions(r, &revs, NULL); |
6074 | 0 | revs.verbose_header = 1; |
6075 | 0 | if (!rebase_merges) |
6076 | 0 | revs.max_parents = 1; |
6077 | 0 | revs.cherry_mark = !reapply_cherry_picks; |
6078 | 0 | revs.limited = 1; |
6079 | 0 | revs.reverse = 1; |
6080 | 0 | revs.right_only = 1; |
6081 | 0 | revs.sort_order = REV_SORT_IN_GRAPH_ORDER; |
6082 | 0 | revs.topo_order = 1; |
6083 | |
|
6084 | 0 | revs.pretty_given = 1; |
6085 | 0 | git_config_get_string("rebase.instructionFormat", &format); |
6086 | 0 | if (!format || !*format) { |
6087 | 0 | free(format); |
6088 | 0 | format = xstrdup("%s"); |
6089 | 0 | } |
6090 | 0 | get_commit_format(format, &revs); |
6091 | 0 | free(format); |
6092 | 0 | pp.fmt = revs.commit_format; |
6093 | 0 | pp.output_encoding = get_log_output_encoding(); |
6094 | |
|
6095 | 0 | if (setup_revisions(argc, argv, &revs, NULL) > 1) { |
6096 | 0 | ret = error(_("make_script: unhandled options")); |
6097 | 0 | goto cleanup; |
6098 | 0 | } |
6099 | | |
6100 | 0 | if (prepare_revision_walk(&revs) < 0) { |
6101 | 0 | ret = error(_("make_script: error preparing revisions")); |
6102 | 0 | goto cleanup; |
6103 | 0 | } |
6104 | | |
6105 | 0 | if (rebase_merges) { |
6106 | 0 | ret = make_script_with_merges(&pp, &revs, out, flags); |
6107 | 0 | goto cleanup; |
6108 | 0 | } |
6109 | | |
6110 | 0 | while ((commit = get_revision(&revs))) { |
6111 | 0 | int is_empty = is_original_commit_empty(commit); |
6112 | |
|
6113 | 0 | if (!is_empty && (commit->object.flags & PATCHSAME)) { |
6114 | 0 | if (flags & TODO_LIST_WARN_SKIPPED_CHERRY_PICKS) |
6115 | 0 | warning(_("skipped previously applied commit %s"), |
6116 | 0 | short_commit_name(r, commit)); |
6117 | 0 | skipped_commit = 1; |
6118 | 0 | continue; |
6119 | 0 | } |
6120 | 0 | if (is_empty && !keep_empty) |
6121 | 0 | continue; |
6122 | 0 | strbuf_addf(out, "%s %s ", insn, |
6123 | 0 | oid_to_hex(&commit->object.oid)); |
6124 | 0 | pretty_print_commit(&pp, commit, out); |
6125 | 0 | if (is_empty) |
6126 | 0 | strbuf_addf(out, " %s empty", comment_line_str); |
6127 | 0 | strbuf_addch(out, '\n'); |
6128 | 0 | } |
6129 | 0 | if (skipped_commit) |
6130 | 0 | advise_if_enabled(ADVICE_SKIPPED_CHERRY_PICKS, |
6131 | 0 | _("use --reapply-cherry-picks to include skipped commits")); |
6132 | 0 | cleanup: |
6133 | 0 | release_revisions(&revs); |
6134 | 0 | return ret; |
6135 | 0 | } |
6136 | | |
6137 | | /* |
6138 | | * Add commands after pick and (series of) squash/fixup commands |
6139 | | * in the todo list. |
6140 | | */ |
6141 | | static void todo_list_add_exec_commands(struct todo_list *todo_list, |
6142 | | struct string_list *commands) |
6143 | 0 | { |
6144 | 0 | struct strbuf *buf = &todo_list->buf; |
6145 | 0 | size_t base_offset = buf->len; |
6146 | 0 | int i, insert, nr = 0, alloc = 0; |
6147 | 0 | struct todo_item *items = NULL, *base_items = NULL; |
6148 | |
|
6149 | 0 | CALLOC_ARRAY(base_items, commands->nr); |
6150 | 0 | for (i = 0; i < commands->nr; i++) { |
6151 | 0 | size_t command_len = strlen(commands->items[i].string); |
6152 | |
|
6153 | 0 | strbuf_addstr(buf, commands->items[i].string); |
6154 | 0 | strbuf_addch(buf, '\n'); |
6155 | |
|
6156 | 0 | base_items[i].command = TODO_EXEC; |
6157 | 0 | base_items[i].offset_in_buf = base_offset; |
6158 | 0 | base_items[i].arg_offset = base_offset; |
6159 | 0 | base_items[i].arg_len = command_len; |
6160 | |
|
6161 | 0 | base_offset += command_len + 1; |
6162 | 0 | } |
6163 | | |
6164 | | /* |
6165 | | * Insert <commands> after every pick. Here, fixup/squash chains |
6166 | | * are considered part of the pick, so we insert the commands *after* |
6167 | | * those chains if there are any. |
6168 | | * |
6169 | | * As we insert the exec commands immediately after rearranging |
6170 | | * any fixups and before the user edits the list, a fixup chain |
6171 | | * can never contain comments (any comments are empty picks that |
6172 | | * have been commented out because the user did not specify |
6173 | | * --keep-empty). So, it is safe to insert an exec command |
6174 | | * without looking at the command following a comment. |
6175 | | */ |
6176 | 0 | insert = 0; |
6177 | 0 | for (i = 0; i < todo_list->nr; i++) { |
6178 | 0 | enum todo_command command = todo_list->items[i].command; |
6179 | 0 | if (insert && !is_fixup(command)) { |
6180 | 0 | ALLOC_GROW(items, nr + commands->nr, alloc); |
6181 | 0 | COPY_ARRAY(items + nr, base_items, commands->nr); |
6182 | 0 | nr += commands->nr; |
6183 | |
|
6184 | 0 | insert = 0; |
6185 | 0 | } |
6186 | |
|
6187 | 0 | ALLOC_GROW(items, nr + 1, alloc); |
6188 | 0 | items[nr++] = todo_list->items[i]; |
6189 | |
|
6190 | 0 | if (command == TODO_PICK || command == TODO_MERGE) |
6191 | 0 | insert = 1; |
6192 | 0 | } |
6193 | | |
6194 | | /* insert or append final <commands> */ |
6195 | 0 | if (insert) { |
6196 | 0 | ALLOC_GROW(items, nr + commands->nr, alloc); |
6197 | 0 | COPY_ARRAY(items + nr, base_items, commands->nr); |
6198 | 0 | nr += commands->nr; |
6199 | 0 | } |
6200 | |
|
6201 | 0 | free(base_items); |
6202 | 0 | FREE_AND_NULL(todo_list->items); |
6203 | 0 | todo_list->items = items; |
6204 | 0 | todo_list->nr = nr; |
6205 | 0 | todo_list->alloc = alloc; |
6206 | 0 | } |
6207 | | |
6208 | | static void todo_list_to_strbuf(struct repository *r, |
6209 | | struct todo_list *todo_list, |
6210 | | struct strbuf *buf, int num, unsigned flags) |
6211 | 0 | { |
6212 | 0 | struct todo_item *item; |
6213 | 0 | int i, max = todo_list->nr; |
6214 | |
|
6215 | 0 | if (num > 0 && num < max) |
6216 | 0 | max = num; |
6217 | |
|
6218 | 0 | for (item = todo_list->items, i = 0; i < max; i++, item++) { |
6219 | 0 | char cmd; |
6220 | | |
6221 | | /* if the item is not a command write it and continue */ |
6222 | 0 | if (item->command >= TODO_COMMENT) { |
6223 | 0 | strbuf_addf(buf, "%.*s\n", item->arg_len, |
6224 | 0 | todo_item_get_arg(todo_list, item)); |
6225 | 0 | continue; |
6226 | 0 | } |
6227 | | |
6228 | | /* add command to the buffer */ |
6229 | 0 | cmd = command_to_char(item->command); |
6230 | 0 | if ((flags & TODO_LIST_ABBREVIATE_CMDS) && cmd) |
6231 | 0 | strbuf_addch(buf, cmd); |
6232 | 0 | else |
6233 | 0 | strbuf_addstr(buf, command_to_string(item->command)); |
6234 | | |
6235 | | /* add commit id */ |
6236 | 0 | if (item->commit) { |
6237 | 0 | const char *oid = flags & TODO_LIST_SHORTEN_IDS ? |
6238 | 0 | short_commit_name(r, item->commit) : |
6239 | 0 | oid_to_hex(&item->commit->object.oid); |
6240 | |
|
6241 | 0 | if (item->command == TODO_FIXUP) { |
6242 | 0 | if (item->flags & TODO_EDIT_FIXUP_MSG) |
6243 | 0 | strbuf_addstr(buf, " -c"); |
6244 | 0 | else if (item->flags & TODO_REPLACE_FIXUP_MSG) { |
6245 | 0 | strbuf_addstr(buf, " -C"); |
6246 | 0 | } |
6247 | 0 | } |
6248 | |
|
6249 | 0 | if (item->command == TODO_MERGE) { |
6250 | 0 | if (item->flags & TODO_EDIT_MERGE_MSG) |
6251 | 0 | strbuf_addstr(buf, " -c"); |
6252 | 0 | else |
6253 | 0 | strbuf_addstr(buf, " -C"); |
6254 | 0 | } |
6255 | |
|
6256 | 0 | strbuf_addf(buf, " %s", oid); |
6257 | 0 | } |
6258 | | |
6259 | | /* add all the rest */ |
6260 | 0 | if (!item->arg_len) |
6261 | 0 | strbuf_addch(buf, '\n'); |
6262 | 0 | else |
6263 | 0 | strbuf_addf(buf, " %.*s\n", item->arg_len, |
6264 | 0 | todo_item_get_arg(todo_list, item)); |
6265 | 0 | } |
6266 | 0 | } |
6267 | | |
6268 | | int todo_list_write_to_file(struct repository *r, struct todo_list *todo_list, |
6269 | | const char *file, const char *shortrevisions, |
6270 | | const char *shortonto, int num, unsigned flags) |
6271 | 0 | { |
6272 | 0 | int res; |
6273 | 0 | struct strbuf buf = STRBUF_INIT; |
6274 | |
|
6275 | 0 | todo_list_to_strbuf(r, todo_list, &buf, num, flags); |
6276 | 0 | if (flags & TODO_LIST_APPEND_TODO_HELP) |
6277 | 0 | append_todo_help(count_commands(todo_list), |
6278 | 0 | shortrevisions, shortonto, &buf); |
6279 | |
|
6280 | 0 | res = write_message(buf.buf, buf.len, file, 0); |
6281 | 0 | strbuf_release(&buf); |
6282 | |
|
6283 | 0 | return res; |
6284 | 0 | } |
6285 | | |
6286 | | /* skip picking commits whose parents are unchanged */ |
6287 | | static int skip_unnecessary_picks(struct repository *r, |
6288 | | struct todo_list *todo_list, |
6289 | | struct object_id *base_oid) |
6290 | 0 | { |
6291 | 0 | struct object_id *parent_oid; |
6292 | 0 | int i; |
6293 | |
|
6294 | 0 | for (i = 0; i < todo_list->nr; i++) { |
6295 | 0 | struct todo_item *item = todo_list->items + i; |
6296 | |
|
6297 | 0 | if (item->command >= TODO_NOOP) |
6298 | 0 | continue; |
6299 | 0 | if (item->command != TODO_PICK) |
6300 | 0 | break; |
6301 | 0 | if (repo_parse_commit(r, item->commit)) { |
6302 | 0 | return error(_("could not parse commit '%s'"), |
6303 | 0 | oid_to_hex(&item->commit->object.oid)); |
6304 | 0 | } |
6305 | 0 | if (!item->commit->parents) |
6306 | 0 | break; /* root commit */ |
6307 | 0 | if (item->commit->parents->next) |
6308 | 0 | break; /* merge commit */ |
6309 | 0 | parent_oid = &item->commit->parents->item->object.oid; |
6310 | 0 | if (!oideq(parent_oid, base_oid)) |
6311 | 0 | break; |
6312 | 0 | oidcpy(base_oid, &item->commit->object.oid); |
6313 | 0 | } |
6314 | 0 | if (i > 0) { |
6315 | 0 | const char *done_path = rebase_path_done(); |
6316 | |
|
6317 | 0 | if (todo_list_write_to_file(r, todo_list, done_path, NULL, NULL, i, 0)) { |
6318 | 0 | error_errno(_("could not write to '%s'"), done_path); |
6319 | 0 | return -1; |
6320 | 0 | } |
6321 | | |
6322 | 0 | MOVE_ARRAY(todo_list->items, todo_list->items + i, todo_list->nr - i); |
6323 | 0 | todo_list->nr -= i; |
6324 | 0 | todo_list->current = 0; |
6325 | 0 | todo_list->done_nr += i; |
6326 | |
|
6327 | 0 | if (is_fixup(peek_command(todo_list, 0))) |
6328 | 0 | record_in_rewritten(base_oid, peek_command(todo_list, 0)); |
6329 | 0 | } |
6330 | | |
6331 | 0 | return 0; |
6332 | 0 | } |
6333 | | |
6334 | | struct todo_add_branch_context { |
6335 | | struct todo_item *items; |
6336 | | size_t items_nr; |
6337 | | size_t items_alloc; |
6338 | | struct strbuf *buf; |
6339 | | struct commit *commit; |
6340 | | struct string_list refs_to_oids; |
6341 | | }; |
6342 | | |
6343 | | static int add_decorations_to_list(const struct commit *commit, |
6344 | | struct todo_add_branch_context *ctx) |
6345 | 0 | { |
6346 | 0 | const struct name_decoration *decoration = get_name_decoration(&commit->object); |
6347 | 0 | const char *head_ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), |
6348 | 0 | "HEAD", |
6349 | 0 | RESOLVE_REF_READING, |
6350 | 0 | NULL, |
6351 | 0 | NULL); |
6352 | |
|
6353 | 0 | while (decoration) { |
6354 | 0 | struct todo_item *item; |
6355 | 0 | const char *path; |
6356 | 0 | size_t base_offset = ctx->buf->len; |
6357 | | |
6358 | | /* |
6359 | | * If the branch is the current HEAD, then it will be |
6360 | | * updated by the default rebase behavior. |
6361 | | */ |
6362 | 0 | if (head_ref && !strcmp(head_ref, decoration->name)) { |
6363 | 0 | decoration = decoration->next; |
6364 | 0 | continue; |
6365 | 0 | } |
6366 | | |
6367 | 0 | ALLOC_GROW(ctx->items, |
6368 | 0 | ctx->items_nr + 1, |
6369 | 0 | ctx->items_alloc); |
6370 | 0 | item = &ctx->items[ctx->items_nr]; |
6371 | 0 | memset(item, 0, sizeof(*item)); |
6372 | | |
6373 | | /* If the branch is checked out, then leave a comment instead. */ |
6374 | 0 | if ((path = branch_checked_out(decoration->name))) { |
6375 | 0 | item->command = TODO_COMMENT; |
6376 | 0 | strbuf_addf(ctx->buf, "# Ref %s checked out at '%s'\n", |
6377 | 0 | decoration->name, path); |
6378 | 0 | } else { |
6379 | 0 | struct string_list_item *sti; |
6380 | 0 | item->command = TODO_UPDATE_REF; |
6381 | 0 | strbuf_addf(ctx->buf, "%s\n", decoration->name); |
6382 | |
|
6383 | 0 | sti = string_list_insert(&ctx->refs_to_oids, |
6384 | 0 | decoration->name); |
6385 | 0 | sti->util = init_update_ref_record(decoration->name); |
6386 | 0 | } |
6387 | |
|
6388 | 0 | item->offset_in_buf = base_offset; |
6389 | 0 | item->arg_offset = base_offset; |
6390 | 0 | item->arg_len = ctx->buf->len - base_offset; |
6391 | 0 | ctx->items_nr++; |
6392 | |
|
6393 | 0 | decoration = decoration->next; |
6394 | 0 | } |
6395 | |
|
6396 | 0 | return 0; |
6397 | 0 | } |
6398 | | |
6399 | | /* |
6400 | | * For each 'pick' command, find out if the commit has a decoration in |
6401 | | * refs/heads/. If so, then add a 'label for-update-refs/' command. |
6402 | | */ |
6403 | | static int todo_list_add_update_ref_commands(struct todo_list *todo_list) |
6404 | 0 | { |
6405 | 0 | int i, res; |
6406 | 0 | static struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP; |
6407 | 0 | static struct string_list decorate_refs_exclude_config = STRING_LIST_INIT_NODUP; |
6408 | 0 | static struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP; |
6409 | 0 | struct decoration_filter decoration_filter = { |
6410 | 0 | .include_ref_pattern = &decorate_refs_include, |
6411 | 0 | .exclude_ref_pattern = &decorate_refs_exclude, |
6412 | 0 | .exclude_ref_config_pattern = &decorate_refs_exclude_config, |
6413 | 0 | }; |
6414 | 0 | struct todo_add_branch_context ctx = { |
6415 | 0 | .buf = &todo_list->buf, |
6416 | 0 | .refs_to_oids = STRING_LIST_INIT_DUP, |
6417 | 0 | }; |
6418 | |
|
6419 | 0 | ctx.items_alloc = 2 * todo_list->nr + 1; |
6420 | 0 | ALLOC_ARRAY(ctx.items, ctx.items_alloc); |
6421 | |
|
6422 | 0 | string_list_append(&decorate_refs_include, "refs/heads/"); |
6423 | 0 | load_ref_decorations(&decoration_filter, 0); |
6424 | |
|
6425 | 0 | for (i = 0; i < todo_list->nr; ) { |
6426 | 0 | struct todo_item *item = &todo_list->items[i]; |
6427 | | |
6428 | | /* insert ith item into new list */ |
6429 | 0 | ALLOC_GROW(ctx.items, |
6430 | 0 | ctx.items_nr + 1, |
6431 | 0 | ctx.items_alloc); |
6432 | |
|
6433 | 0 | ctx.items[ctx.items_nr++] = todo_list->items[i++]; |
6434 | |
|
6435 | 0 | if (item->commit) { |
6436 | 0 | ctx.commit = item->commit; |
6437 | 0 | add_decorations_to_list(item->commit, &ctx); |
6438 | 0 | } |
6439 | 0 | } |
6440 | |
|
6441 | 0 | res = write_update_refs_state(&ctx.refs_to_oids); |
6442 | |
|
6443 | 0 | string_list_clear(&ctx.refs_to_oids, 1); |
6444 | |
|
6445 | 0 | if (res) { |
6446 | | /* we failed, so clean up the new list. */ |
6447 | 0 | free(ctx.items); |
6448 | 0 | return res; |
6449 | 0 | } |
6450 | | |
6451 | 0 | free(todo_list->items); |
6452 | 0 | todo_list->items = ctx.items; |
6453 | 0 | todo_list->nr = ctx.items_nr; |
6454 | 0 | todo_list->alloc = ctx.items_alloc; |
6455 | |
|
6456 | 0 | return 0; |
6457 | 0 | } |
6458 | | |
6459 | | int complete_action(struct repository *r, struct replay_opts *opts, unsigned flags, |
6460 | | const char *shortrevisions, const char *onto_name, |
6461 | | struct commit *onto, const struct object_id *orig_head, |
6462 | | struct string_list *commands, unsigned autosquash, |
6463 | | unsigned update_refs, |
6464 | | struct todo_list *todo_list) |
6465 | 0 | { |
6466 | 0 | char shortonto[GIT_MAX_HEXSZ + 1]; |
6467 | 0 | const char *todo_file = rebase_path_todo(); |
6468 | 0 | struct todo_list new_todo = TODO_LIST_INIT; |
6469 | 0 | struct strbuf *buf = &todo_list->buf, buf2 = STRBUF_INIT; |
6470 | 0 | struct object_id oid = onto->object.oid; |
6471 | 0 | int res; |
6472 | |
|
6473 | 0 | repo_find_unique_abbrev_r(r, shortonto, &oid, |
6474 | 0 | DEFAULT_ABBREV); |
6475 | |
|
6476 | 0 | if (buf->len == 0) { |
6477 | 0 | struct todo_item *item = append_new_todo(todo_list); |
6478 | 0 | item->command = TODO_NOOP; |
6479 | 0 | item->commit = NULL; |
6480 | 0 | item->arg_len = item->arg_offset = item->flags = item->offset_in_buf = 0; |
6481 | 0 | } |
6482 | |
|
6483 | 0 | if (update_refs && todo_list_add_update_ref_commands(todo_list)) |
6484 | 0 | return -1; |
6485 | | |
6486 | 0 | if (autosquash && todo_list_rearrange_squash(todo_list)) |
6487 | 0 | return -1; |
6488 | | |
6489 | 0 | if (commands->nr) |
6490 | 0 | todo_list_add_exec_commands(todo_list, commands); |
6491 | |
|
6492 | 0 | if (count_commands(todo_list) == 0) { |
6493 | 0 | apply_autostash(rebase_path_autostash()); |
6494 | 0 | sequencer_remove_state(opts); |
6495 | |
|
6496 | 0 | return error(_("nothing to do")); |
6497 | 0 | } |
6498 | | |
6499 | 0 | res = edit_todo_list(r, opts, todo_list, &new_todo, shortrevisions, |
6500 | 0 | shortonto, flags); |
6501 | 0 | if (res == -1) |
6502 | 0 | return -1; |
6503 | 0 | else if (res == -2) { |
6504 | 0 | apply_autostash(rebase_path_autostash()); |
6505 | 0 | sequencer_remove_state(opts); |
6506 | |
|
6507 | 0 | return -1; |
6508 | 0 | } else if (res == -3) { |
6509 | 0 | apply_autostash(rebase_path_autostash()); |
6510 | 0 | sequencer_remove_state(opts); |
6511 | 0 | todo_list_release(&new_todo); |
6512 | |
|
6513 | 0 | return error(_("nothing to do")); |
6514 | 0 | } else if (res == -4) { |
6515 | 0 | checkout_onto(r, opts, onto_name, &onto->object.oid, orig_head); |
6516 | 0 | todo_list_release(&new_todo); |
6517 | |
|
6518 | 0 | return -1; |
6519 | 0 | } |
6520 | | |
6521 | | /* Expand the commit IDs */ |
6522 | 0 | todo_list_to_strbuf(r, &new_todo, &buf2, -1, 0); |
6523 | 0 | strbuf_swap(&new_todo.buf, &buf2); |
6524 | 0 | strbuf_release(&buf2); |
6525 | | /* Nothing is done yet, and we're reparsing, so let's reset the count */ |
6526 | 0 | new_todo.total_nr = 0; |
6527 | 0 | if (todo_list_parse_insn_buffer(r, opts, new_todo.buf.buf, &new_todo) < 0) |
6528 | 0 | BUG("invalid todo list after expanding IDs:\n%s", |
6529 | 0 | new_todo.buf.buf); |
6530 | | |
6531 | 0 | if (opts->allow_ff && skip_unnecessary_picks(r, &new_todo, &oid)) { |
6532 | 0 | todo_list_release(&new_todo); |
6533 | 0 | return error(_("could not skip unnecessary pick commands")); |
6534 | 0 | } |
6535 | | |
6536 | 0 | if (todo_list_write_to_file(r, &new_todo, todo_file, NULL, NULL, -1, |
6537 | 0 | flags & ~(TODO_LIST_SHORTEN_IDS))) { |
6538 | 0 | todo_list_release(&new_todo); |
6539 | 0 | return error_errno(_("could not write '%s'"), todo_file); |
6540 | 0 | } |
6541 | | |
6542 | 0 | res = -1; |
6543 | |
|
6544 | 0 | if (checkout_onto(r, opts, onto_name, &oid, orig_head)) |
6545 | 0 | goto cleanup; |
6546 | | |
6547 | 0 | if (require_clean_work_tree(r, "rebase", NULL, 1, 1)) |
6548 | 0 | goto cleanup; |
6549 | | |
6550 | 0 | todo_list_write_total_nr(&new_todo); |
6551 | 0 | res = pick_commits(r, &new_todo, opts); |
6552 | |
|
6553 | 0 | cleanup: |
6554 | 0 | todo_list_release(&new_todo); |
6555 | |
|
6556 | 0 | return res; |
6557 | 0 | } |
6558 | | |
6559 | | struct subject2item_entry { |
6560 | | struct hashmap_entry entry; |
6561 | | int i; |
6562 | | char subject[FLEX_ARRAY]; |
6563 | | }; |
6564 | | |
6565 | | static int subject2item_cmp(const void *fndata UNUSED, |
6566 | | const struct hashmap_entry *eptr, |
6567 | | const struct hashmap_entry *entry_or_key, |
6568 | | const void *key) |
6569 | 0 | { |
6570 | 0 | const struct subject2item_entry *a, *b; |
6571 | |
|
6572 | 0 | a = container_of(eptr, const struct subject2item_entry, entry); |
6573 | 0 | b = container_of(entry_or_key, const struct subject2item_entry, entry); |
6574 | |
|
6575 | 0 | return key ? strcmp(a->subject, key) : strcmp(a->subject, b->subject); |
6576 | 0 | } |
6577 | | |
6578 | | define_commit_slab(commit_todo_item, struct todo_item *); |
6579 | | |
6580 | 0 | static int skip_fixupish(const char *subject, const char **p) { |
6581 | 0 | return skip_prefix(subject, "fixup! ", p) || |
6582 | 0 | skip_prefix(subject, "amend! ", p) || |
6583 | 0 | skip_prefix(subject, "squash! ", p); |
6584 | 0 | } |
6585 | | |
6586 | | /* |
6587 | | * Rearrange the todo list that has both "pick commit-id msg" and "pick |
6588 | | * commit-id fixup!/squash! msg" in it so that the latter is put immediately |
6589 | | * after the former, and change "pick" to "fixup"/"squash". |
6590 | | * |
6591 | | * Note that if the config has specified a custom instruction format, each log |
6592 | | * message will have to be retrieved from the commit (as the oneline in the |
6593 | | * script cannot be trusted) in order to normalize the autosquash arrangement. |
6594 | | */ |
6595 | | int todo_list_rearrange_squash(struct todo_list *todo_list) |
6596 | 0 | { |
6597 | 0 | struct hashmap subject2item; |
6598 | 0 | int rearranged = 0, *next, *tail, i, nr = 0; |
6599 | 0 | char **subjects; |
6600 | 0 | struct commit_todo_item commit_todo; |
6601 | 0 | struct todo_item *items = NULL; |
6602 | |
|
6603 | 0 | init_commit_todo_item(&commit_todo); |
6604 | | /* |
6605 | | * The hashmap maps onelines to the respective todo list index. |
6606 | | * |
6607 | | * If any items need to be rearranged, the next[i] value will indicate |
6608 | | * which item was moved directly after the i'th. |
6609 | | * |
6610 | | * In that case, last[i] will indicate the index of the latest item to |
6611 | | * be moved to appear after the i'th. |
6612 | | */ |
6613 | 0 | hashmap_init(&subject2item, subject2item_cmp, NULL, todo_list->nr); |
6614 | 0 | ALLOC_ARRAY(next, todo_list->nr); |
6615 | 0 | ALLOC_ARRAY(tail, todo_list->nr); |
6616 | 0 | ALLOC_ARRAY(subjects, todo_list->nr); |
6617 | 0 | for (i = 0; i < todo_list->nr; i++) { |
6618 | 0 | struct strbuf buf = STRBUF_INIT; |
6619 | 0 | struct todo_item *item = todo_list->items + i; |
6620 | 0 | const char *commit_buffer, *subject, *p; |
6621 | 0 | size_t subject_len; |
6622 | 0 | int i2 = -1; |
6623 | 0 | struct subject2item_entry *entry; |
6624 | |
|
6625 | 0 | next[i] = tail[i] = -1; |
6626 | 0 | if (!item->commit || item->command == TODO_DROP) { |
6627 | 0 | subjects[i] = NULL; |
6628 | 0 | continue; |
6629 | 0 | } |
6630 | | |
6631 | 0 | if (is_fixup(item->command)) { |
6632 | 0 | clear_commit_todo_item(&commit_todo); |
6633 | 0 | return error(_("the script was already rearranged.")); |
6634 | 0 | } |
6635 | | |
6636 | 0 | repo_parse_commit(the_repository, item->commit); |
6637 | 0 | commit_buffer = repo_logmsg_reencode(the_repository, |
6638 | 0 | item->commit, NULL, |
6639 | 0 | "UTF-8"); |
6640 | 0 | find_commit_subject(commit_buffer, &subject); |
6641 | 0 | format_subject(&buf, subject, " "); |
6642 | 0 | subject = subjects[i] = strbuf_detach(&buf, &subject_len); |
6643 | 0 | repo_unuse_commit_buffer(the_repository, item->commit, |
6644 | 0 | commit_buffer); |
6645 | 0 | if (skip_fixupish(subject, &p)) { |
6646 | 0 | struct commit *commit2; |
6647 | |
|
6648 | 0 | for (;;) { |
6649 | 0 | while (isspace(*p)) |
6650 | 0 | p++; |
6651 | 0 | if (!skip_fixupish(p, &p)) |
6652 | 0 | break; |
6653 | 0 | } |
6654 | |
|
6655 | 0 | entry = hashmap_get_entry_from_hash(&subject2item, |
6656 | 0 | strhash(p), p, |
6657 | 0 | struct subject2item_entry, |
6658 | 0 | entry); |
6659 | 0 | if (entry) |
6660 | | /* found by title */ |
6661 | 0 | i2 = entry->i; |
6662 | 0 | else if (!strchr(p, ' ') && |
6663 | 0 | (commit2 = |
6664 | 0 | lookup_commit_reference_by_name(p)) && |
6665 | 0 | *commit_todo_item_at(&commit_todo, commit2)) |
6666 | | /* found by commit name */ |
6667 | 0 | i2 = *commit_todo_item_at(&commit_todo, commit2) |
6668 | 0 | - todo_list->items; |
6669 | 0 | else { |
6670 | | /* copy can be a prefix of the commit subject */ |
6671 | 0 | for (i2 = 0; i2 < i; i2++) |
6672 | 0 | if (subjects[i2] && |
6673 | 0 | starts_with(subjects[i2], p)) |
6674 | 0 | break; |
6675 | 0 | if (i2 == i) |
6676 | 0 | i2 = -1; |
6677 | 0 | } |
6678 | 0 | } |
6679 | 0 | if (i2 >= 0) { |
6680 | 0 | rearranged = 1; |
6681 | 0 | if (starts_with(subject, "fixup!")) { |
6682 | 0 | todo_list->items[i].command = TODO_FIXUP; |
6683 | 0 | } else if (starts_with(subject, "amend!")) { |
6684 | 0 | todo_list->items[i].command = TODO_FIXUP; |
6685 | 0 | todo_list->items[i].flags = TODO_REPLACE_FIXUP_MSG; |
6686 | 0 | } else { |
6687 | 0 | todo_list->items[i].command = TODO_SQUASH; |
6688 | 0 | } |
6689 | 0 | if (tail[i2] < 0) { |
6690 | 0 | next[i] = next[i2]; |
6691 | 0 | next[i2] = i; |
6692 | 0 | } else { |
6693 | 0 | next[i] = next[tail[i2]]; |
6694 | 0 | next[tail[i2]] = i; |
6695 | 0 | } |
6696 | 0 | tail[i2] = i; |
6697 | 0 | } else if (!hashmap_get_from_hash(&subject2item, |
6698 | 0 | strhash(subject), subject)) { |
6699 | 0 | FLEX_ALLOC_MEM(entry, subject, subject, subject_len); |
6700 | 0 | entry->i = i; |
6701 | 0 | hashmap_entry_init(&entry->entry, |
6702 | 0 | strhash(entry->subject)); |
6703 | 0 | hashmap_put(&subject2item, &entry->entry); |
6704 | 0 | } |
6705 | |
|
6706 | 0 | *commit_todo_item_at(&commit_todo, item->commit) = item; |
6707 | 0 | } |
6708 | | |
6709 | 0 | if (rearranged) { |
6710 | 0 | ALLOC_ARRAY(items, todo_list->nr); |
6711 | |
|
6712 | 0 | for (i = 0; i < todo_list->nr; i++) { |
6713 | 0 | enum todo_command command = todo_list->items[i].command; |
6714 | 0 | int cur = i; |
6715 | | |
6716 | | /* |
6717 | | * Initially, all commands are 'pick's. If it is a |
6718 | | * fixup or a squash now, we have rearranged it. |
6719 | | */ |
6720 | 0 | if (is_fixup(command)) |
6721 | 0 | continue; |
6722 | | |
6723 | 0 | while (cur >= 0) { |
6724 | 0 | items[nr++] = todo_list->items[cur]; |
6725 | 0 | cur = next[cur]; |
6726 | 0 | } |
6727 | 0 | } |
6728 | |
|
6729 | 0 | assert(nr == todo_list->nr); |
6730 | 0 | todo_list->alloc = nr; |
6731 | 0 | FREE_AND_NULL(todo_list->items); |
6732 | 0 | todo_list->items = items; |
6733 | 0 | } |
6734 | | |
6735 | 0 | free(next); |
6736 | 0 | free(tail); |
6737 | 0 | for (i = 0; i < todo_list->nr; i++) |
6738 | 0 | free(subjects[i]); |
6739 | 0 | free(subjects); |
6740 | 0 | hashmap_clear_and_free(&subject2item, struct subject2item_entry, entry); |
6741 | |
|
6742 | 0 | clear_commit_todo_item(&commit_todo); |
6743 | |
|
6744 | 0 | return 0; |
6745 | 0 | } |
6746 | | |
6747 | | int sequencer_determine_whence(struct repository *r, enum commit_whence *whence) |
6748 | 0 | { |
6749 | 0 | if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD")) { |
6750 | 0 | struct object_id cherry_pick_head, rebase_head; |
6751 | |
|
6752 | 0 | if (file_exists(git_path_seq_dir())) |
6753 | 0 | *whence = FROM_CHERRY_PICK_MULTI; |
6754 | 0 | if (file_exists(rebase_path()) && |
6755 | 0 | !repo_get_oid(r, "REBASE_HEAD", &rebase_head) && |
6756 | 0 | !repo_get_oid(r, "CHERRY_PICK_HEAD", &cherry_pick_head) && |
6757 | 0 | oideq(&rebase_head, &cherry_pick_head)) |
6758 | 0 | *whence = FROM_REBASE_PICK; |
6759 | 0 | else |
6760 | 0 | *whence = FROM_CHERRY_PICK_SINGLE; |
6761 | |
|
6762 | 0 | return 1; |
6763 | 0 | } |
6764 | | |
6765 | 0 | return 0; |
6766 | 0 | } |
6767 | | |
6768 | | int sequencer_get_update_refs_state(const char *wt_dir, |
6769 | | struct string_list *refs) |
6770 | 0 | { |
6771 | 0 | int result = 0; |
6772 | 0 | FILE *fp = NULL; |
6773 | 0 | struct strbuf ref = STRBUF_INIT; |
6774 | 0 | struct strbuf hash = STRBUF_INIT; |
6775 | 0 | struct update_ref_record *rec = NULL; |
6776 | |
|
6777 | 0 | char *path = rebase_path_update_refs(wt_dir); |
6778 | |
|
6779 | 0 | fp = fopen(path, "r"); |
6780 | 0 | if (!fp) |
6781 | 0 | goto cleanup; |
6782 | | |
6783 | 0 | while (strbuf_getline(&ref, fp) != EOF) { |
6784 | 0 | struct string_list_item *item; |
6785 | |
|
6786 | 0 | CALLOC_ARRAY(rec, 1); |
6787 | |
|
6788 | 0 | if (strbuf_getline(&hash, fp) == EOF || |
6789 | 0 | get_oid_hex(hash.buf, &rec->before)) { |
6790 | 0 | warning(_("update-refs file at '%s' is invalid"), |
6791 | 0 | path); |
6792 | 0 | result = -1; |
6793 | 0 | goto cleanup; |
6794 | 0 | } |
6795 | | |
6796 | 0 | if (strbuf_getline(&hash, fp) == EOF || |
6797 | 0 | get_oid_hex(hash.buf, &rec->after)) { |
6798 | 0 | warning(_("update-refs file at '%s' is invalid"), |
6799 | 0 | path); |
6800 | 0 | result = -1; |
6801 | 0 | goto cleanup; |
6802 | 0 | } |
6803 | | |
6804 | 0 | item = string_list_insert(refs, ref.buf); |
6805 | 0 | item->util = rec; |
6806 | 0 | rec = NULL; |
6807 | 0 | } |
6808 | | |
6809 | 0 | cleanup: |
6810 | 0 | if (fp) |
6811 | 0 | fclose(fp); |
6812 | 0 | free(path); |
6813 | 0 | free(rec); |
6814 | 0 | strbuf_release(&ref); |
6815 | 0 | strbuf_release(&hash); |
6816 | 0 | return result; |
6817 | 0 | } |