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