Line | Count | Source (jump to first uncovered line) |
1 | | #include "git-compat-util.h" |
2 | | #include "abspath.h" |
3 | | #include "advice.h" |
4 | | #include "config.h" |
5 | | #include "copy.h" |
6 | | #include "environment.h" |
7 | | #include "gettext.h" |
8 | | #include "hex.h" |
9 | | #include "lockfile.h" |
10 | | #include "dir.h" |
11 | | #include "object-file.h" |
12 | | #include "object-name.h" |
13 | | #include "object-store-ll.h" |
14 | | #include "object.h" |
15 | | #include "pager.h" |
16 | | #include "commit.h" |
17 | | #include "sequencer.h" |
18 | | #include "tag.h" |
19 | | #include "run-command.h" |
20 | | #include "hook.h" |
21 | | #include "exec-cmd.h" |
22 | | #include "utf8.h" |
23 | | #include "cache-tree.h" |
24 | | #include "diff.h" |
25 | | #include "path.h" |
26 | | #include "revision.h" |
27 | | #include "rerere.h" |
28 | | #include "merge.h" |
29 | | #include "merge-ort.h" |
30 | | #include "merge-ort-wrappers.h" |
31 | | #include "refs.h" |
32 | | #include "sparse-index.h" |
33 | | #include "strvec.h" |
34 | | #include "quote.h" |
35 | | #include "trailer.h" |
36 | | #include "log-tree.h" |
37 | | #include "wt-status.h" |
38 | | #include "hashmap.h" |
39 | | #include "notes-utils.h" |
40 | | #include "sigchain.h" |
41 | | #include "unpack-trees.h" |
42 | | #include "worktree.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 update_refs_record' represents a value in the update-refs |
215 | | * list. We use a string_list to map refs to these (before, after) pairs. |
216 | | */ |
217 | | struct update_ref_record { |
218 | | struct object_id before; |
219 | | struct object_id after; |
220 | | }; |
221 | | |
222 | | static struct update_ref_record *init_update_ref_record(const char *ref) |
223 | 0 | { |
224 | 0 | struct update_ref_record *rec; |
225 | |
|
226 | 0 | CALLOC_ARRAY(rec, 1); |
227 | |
|
228 | 0 | oidcpy(&rec->before, null_oid()); |
229 | 0 | oidcpy(&rec->after, null_oid()); |
230 | | |
231 | | /* This may fail, but that's fine, we will keep the null OID. */ |
232 | 0 | read_ref(ref, &rec->before); |
233 | |
|
234 | 0 | return rec; |
235 | 0 | } |
236 | | |
237 | | static int git_sequencer_config(const char *k, const char *v, |
238 | | const struct config_context *ctx, void *cb) |
239 | 0 | { |
240 | 0 | struct replay_opts *opts = cb; |
241 | 0 | int status; |
242 | |
|
243 | 0 | if (!strcmp(k, "commit.cleanup")) { |
244 | 0 | const char *s; |
245 | |
|
246 | 0 | status = git_config_string(&s, k, v); |
247 | 0 | if (status) |
248 | 0 | return status; |
249 | | |
250 | 0 | if (!strcmp(s, "verbatim")) { |
251 | 0 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_NONE; |
252 | 0 | opts->explicit_cleanup = 1; |
253 | 0 | } else if (!strcmp(s, "whitespace")) { |
254 | 0 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SPACE; |
255 | 0 | opts->explicit_cleanup = 1; |
256 | 0 | } else if (!strcmp(s, "strip")) { |
257 | 0 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_ALL; |
258 | 0 | opts->explicit_cleanup = 1; |
259 | 0 | } else if (!strcmp(s, "scissors")) { |
260 | 0 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SCISSORS; |
261 | 0 | opts->explicit_cleanup = 1; |
262 | 0 | } else { |
263 | 0 | warning(_("invalid commit message cleanup mode '%s'"), |
264 | 0 | s); |
265 | 0 | } |
266 | |
|
267 | 0 | free((char *)s); |
268 | 0 | return status; |
269 | 0 | } |
270 | | |
271 | 0 | if (!strcmp(k, "commit.gpgsign")) { |
272 | 0 | opts->gpg_sign = git_config_bool(k, v) ? xstrdup("") : NULL; |
273 | 0 | return 0; |
274 | 0 | } |
275 | | |
276 | 0 | if (!opts->default_strategy && !strcmp(k, "pull.twohead")) { |
277 | 0 | int ret = git_config_string((const char**)&opts->default_strategy, k, v); |
278 | 0 | if (ret == 0) { |
279 | | /* |
280 | | * pull.twohead is allowed to be multi-valued; we only |
281 | | * care about the first value. |
282 | | */ |
283 | 0 | char *tmp = strchr(opts->default_strategy, ' '); |
284 | 0 | if (tmp) |
285 | 0 | *tmp = '\0'; |
286 | 0 | } |
287 | 0 | return ret; |
288 | 0 | } |
289 | | |
290 | 0 | if (opts->action == REPLAY_REVERT && !strcmp(k, "revert.reference")) |
291 | 0 | opts->commit_use_reference = git_config_bool(k, v); |
292 | |
|
293 | 0 | return git_diff_basic_config(k, v, ctx, NULL); |
294 | 0 | } |
295 | | |
296 | | void sequencer_init_config(struct replay_opts *opts) |
297 | 0 | { |
298 | 0 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_NONE; |
299 | 0 | git_config(git_sequencer_config, opts); |
300 | 0 | } |
301 | | |
302 | | static inline int is_rebase_i(const struct replay_opts *opts) |
303 | 0 | { |
304 | 0 | return opts->action == REPLAY_INTERACTIVE_REBASE; |
305 | 0 | } |
306 | | |
307 | | static const char *get_dir(const struct replay_opts *opts) |
308 | 0 | { |
309 | 0 | if (is_rebase_i(opts)) |
310 | 0 | return rebase_path(); |
311 | 0 | return git_path_seq_dir(); |
312 | 0 | } |
313 | | |
314 | | static const char *get_todo_path(const struct replay_opts *opts) |
315 | 0 | { |
316 | 0 | if (is_rebase_i(opts)) |
317 | 0 | return rebase_path_todo(); |
318 | 0 | return git_path_todo_file(); |
319 | 0 | } |
320 | | |
321 | | /* |
322 | | * Returns 0 for non-conforming footer |
323 | | * Returns 1 for conforming footer |
324 | | * Returns 2 when sob exists within conforming footer |
325 | | * Returns 3 when sob exists within conforming footer as last entry |
326 | | */ |
327 | | static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, |
328 | | size_t ignore_footer) |
329 | 0 | { |
330 | 0 | struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT; |
331 | 0 | struct trailer_info info; |
332 | 0 | size_t i; |
333 | 0 | int found_sob = 0, found_sob_last = 0; |
334 | 0 | char saved_char; |
335 | |
|
336 | 0 | opts.no_divider = 1; |
337 | |
|
338 | 0 | if (ignore_footer) { |
339 | 0 | saved_char = sb->buf[sb->len - ignore_footer]; |
340 | 0 | sb->buf[sb->len - ignore_footer] = '\0'; |
341 | 0 | } |
342 | |
|
343 | 0 | trailer_info_get(&info, sb->buf, &opts); |
344 | |
|
345 | 0 | if (ignore_footer) |
346 | 0 | sb->buf[sb->len - ignore_footer] = saved_char; |
347 | |
|
348 | 0 | if (info.trailer_start == info.trailer_end) |
349 | 0 | return 0; |
350 | | |
351 | 0 | for (i = 0; i < info.trailer_nr; i++) |
352 | 0 | if (sob && !strncmp(info.trailers[i], sob->buf, sob->len)) { |
353 | 0 | found_sob = 1; |
354 | 0 | if (i == info.trailer_nr - 1) |
355 | 0 | found_sob_last = 1; |
356 | 0 | } |
357 | |
|
358 | 0 | trailer_info_release(&info); |
359 | |
|
360 | 0 | if (found_sob_last) |
361 | 0 | return 3; |
362 | 0 | if (found_sob) |
363 | 0 | return 2; |
364 | 0 | return 1; |
365 | 0 | } |
366 | | |
367 | | static const char *gpg_sign_opt_quoted(struct replay_opts *opts) |
368 | 0 | { |
369 | 0 | static struct strbuf buf = STRBUF_INIT; |
370 | |
|
371 | 0 | strbuf_reset(&buf); |
372 | 0 | if (opts->gpg_sign) |
373 | 0 | sq_quotef(&buf, "-S%s", opts->gpg_sign); |
374 | 0 | return buf.buf; |
375 | 0 | } |
376 | | |
377 | | void replay_opts_release(struct replay_opts *opts) |
378 | 0 | { |
379 | 0 | free(opts->gpg_sign); |
380 | 0 | free(opts->reflog_action); |
381 | 0 | free(opts->default_strategy); |
382 | 0 | free(opts->strategy); |
383 | 0 | strvec_clear (&opts->xopts); |
384 | 0 | strbuf_release(&opts->current_fixups); |
385 | 0 | if (opts->revs) |
386 | 0 | release_revisions(opts->revs); |
387 | 0 | free(opts->revs); |
388 | 0 | } |
389 | | |
390 | | int sequencer_remove_state(struct replay_opts *opts) |
391 | 0 | { |
392 | 0 | struct strbuf buf = STRBUF_INIT; |
393 | 0 | int ret = 0; |
394 | |
|
395 | 0 | if (is_rebase_i(opts) && |
396 | 0 | strbuf_read_file(&buf, rebase_path_refs_to_delete(), 0) > 0) { |
397 | 0 | char *p = buf.buf; |
398 | 0 | while (*p) { |
399 | 0 | char *eol = strchr(p, '\n'); |
400 | 0 | if (eol) |
401 | 0 | *eol = '\0'; |
402 | 0 | if (delete_ref("(rebase) cleanup", p, NULL, 0) < 0) { |
403 | 0 | warning(_("could not delete '%s'"), p); |
404 | 0 | ret = -1; |
405 | 0 | } |
406 | 0 | if (!eol) |
407 | 0 | break; |
408 | 0 | p = eol + 1; |
409 | 0 | } |
410 | 0 | } |
411 | |
|
412 | 0 | strbuf_reset(&buf); |
413 | 0 | strbuf_addstr(&buf, get_dir(opts)); |
414 | 0 | if (remove_dir_recursively(&buf, 0)) |
415 | 0 | ret = error(_("could not remove '%s'"), buf.buf); |
416 | 0 | strbuf_release(&buf); |
417 | |
|
418 | 0 | return ret; |
419 | 0 | } |
420 | | |
421 | | static const char *action_name(const struct replay_opts *opts) |
422 | 0 | { |
423 | 0 | switch (opts->action) { |
424 | 0 | case REPLAY_REVERT: |
425 | 0 | return N_("revert"); |
426 | 0 | case REPLAY_PICK: |
427 | 0 | return N_("cherry-pick"); |
428 | 0 | case REPLAY_INTERACTIVE_REBASE: |
429 | 0 | return N_("rebase"); |
430 | 0 | } |
431 | 0 | die(_("unknown action: %d"), opts->action); |
432 | 0 | } |
433 | | |
434 | | struct commit_message { |
435 | | char *parent_label; |
436 | | char *label; |
437 | | char *subject; |
438 | | const char *message; |
439 | | }; |
440 | | |
441 | | static const char *short_commit_name(struct repository *r, struct commit *commit) |
442 | 0 | { |
443 | 0 | return repo_find_unique_abbrev(r, &commit->object.oid, DEFAULT_ABBREV); |
444 | 0 | } |
445 | | |
446 | | static int get_message(struct commit *commit, struct commit_message *out) |
447 | 0 | { |
448 | 0 | const char *abbrev, *subject; |
449 | 0 | int subject_len; |
450 | |
|
451 | 0 | out->message = repo_logmsg_reencode(the_repository, commit, NULL, |
452 | 0 | get_commit_output_encoding()); |
453 | 0 | abbrev = short_commit_name(the_repository, commit); |
454 | |
|
455 | 0 | subject_len = find_commit_subject(out->message, &subject); |
456 | |
|
457 | 0 | out->subject = xmemdupz(subject, subject_len); |
458 | 0 | out->label = xstrfmt("%s (%s)", abbrev, out->subject); |
459 | 0 | out->parent_label = xstrfmt("parent of %s", out->label); |
460 | |
|
461 | 0 | return 0; |
462 | 0 | } |
463 | | |
464 | | static void free_message(struct commit *commit, struct commit_message *msg) |
465 | 0 | { |
466 | 0 | free(msg->parent_label); |
467 | 0 | free(msg->label); |
468 | 0 | free(msg->subject); |
469 | 0 | repo_unuse_commit_buffer(the_repository, commit, msg->message); |
470 | 0 | } |
471 | | |
472 | | static void print_advice(struct repository *r, int show_hint, |
473 | | struct replay_opts *opts) |
474 | 0 | { |
475 | 0 | char *msg = getenv("GIT_CHERRY_PICK_HELP"); |
476 | |
|
477 | 0 | if (msg) { |
478 | 0 | advise("%s\n", msg); |
479 | | /* |
480 | | * A conflict has occurred but the porcelain |
481 | | * (typically rebase --interactive) wants to take care |
482 | | * of the commit itself so remove CHERRY_PICK_HEAD |
483 | | */ |
484 | 0 | refs_delete_ref(get_main_ref_store(r), "", "CHERRY_PICK_HEAD", |
485 | 0 | NULL, 0); |
486 | 0 | return; |
487 | 0 | } |
488 | | |
489 | 0 | if (show_hint) { |
490 | 0 | if (opts->no_commit) |
491 | 0 | advise(_("after resolving the conflicts, mark the corrected paths\n" |
492 | 0 | "with 'git add <paths>' or 'git rm <paths>'")); |
493 | 0 | else if (opts->action == REPLAY_PICK) |
494 | 0 | advise(_("After resolving the conflicts, mark them with\n" |
495 | 0 | "\"git add/rm <pathspec>\", then run\n" |
496 | 0 | "\"git cherry-pick --continue\".\n" |
497 | 0 | "You can instead skip this commit with \"git cherry-pick --skip\".\n" |
498 | 0 | "To abort and get back to the state before \"git cherry-pick\",\n" |
499 | 0 | "run \"git cherry-pick --abort\".")); |
500 | 0 | else if (opts->action == REPLAY_REVERT) |
501 | 0 | advise(_("After resolving the conflicts, mark them with\n" |
502 | 0 | "\"git add/rm <pathspec>\", then run\n" |
503 | 0 | "\"git revert --continue\".\n" |
504 | 0 | "You can instead skip this commit with \"git revert --skip\".\n" |
505 | 0 | "To abort and get back to the state before \"git revert\",\n" |
506 | 0 | "run \"git revert --abort\".")); |
507 | 0 | else |
508 | 0 | BUG("unexpected pick action in print_advice()"); |
509 | 0 | } |
510 | 0 | } |
511 | | |
512 | | static int write_message(const void *buf, size_t len, const char *filename, |
513 | | int append_eol) |
514 | 0 | { |
515 | 0 | struct lock_file msg_file = LOCK_INIT; |
516 | |
|
517 | 0 | int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0); |
518 | 0 | if (msg_fd < 0) |
519 | 0 | return error_errno(_("could not lock '%s'"), filename); |
520 | 0 | if (write_in_full(msg_fd, buf, len) < 0) { |
521 | 0 | error_errno(_("could not write to '%s'"), filename); |
522 | 0 | rollback_lock_file(&msg_file); |
523 | 0 | return -1; |
524 | 0 | } |
525 | 0 | if (append_eol && write(msg_fd, "\n", 1) < 0) { |
526 | 0 | error_errno(_("could not write eol to '%s'"), filename); |
527 | 0 | rollback_lock_file(&msg_file); |
528 | 0 | return -1; |
529 | 0 | } |
530 | 0 | if (commit_lock_file(&msg_file) < 0) |
531 | 0 | return error(_("failed to finalize '%s'"), filename); |
532 | | |
533 | 0 | return 0; |
534 | 0 | } |
535 | | |
536 | | int read_oneliner(struct strbuf *buf, |
537 | | const char *path, unsigned flags) |
538 | 7.22k | { |
539 | 7.22k | int orig_len = buf->len; |
540 | | |
541 | 7.22k | if (strbuf_read_file(buf, path, 0) < 0) { |
542 | 7.22k | if ((flags & READ_ONELINER_WARN_MISSING) || |
543 | 7.22k | (errno != ENOENT && errno != ENOTDIR)) |
544 | 0 | warning_errno(_("could not read '%s'"), path); |
545 | 7.22k | return 0; |
546 | 7.22k | } |
547 | | |
548 | 0 | if (buf->len > orig_len && buf->buf[buf->len - 1] == '\n') { |
549 | 0 | if (--buf->len > orig_len && buf->buf[buf->len - 1] == '\r') |
550 | 0 | --buf->len; |
551 | 0 | buf->buf[buf->len] = '\0'; |
552 | 0 | } |
553 | |
|
554 | 0 | if ((flags & READ_ONELINER_SKIP_IF_EMPTY) && buf->len == orig_len) |
555 | 0 | return 0; |
556 | | |
557 | 0 | return 1; |
558 | 0 | } |
559 | | |
560 | | static struct tree *empty_tree(struct repository *r) |
561 | 0 | { |
562 | 0 | return lookup_tree(r, the_hash_algo->empty_tree); |
563 | 0 | } |
564 | | |
565 | | static int error_dirty_index(struct repository *repo, struct replay_opts *opts) |
566 | 0 | { |
567 | 0 | if (repo_read_index_unmerged(repo)) |
568 | 0 | return error_resolve_conflict(action_name(opts)); |
569 | | |
570 | 0 | error(_("your local changes would be overwritten by %s."), |
571 | 0 | _(action_name(opts))); |
572 | |
|
573 | 0 | if (advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)) |
574 | 0 | advise(_("commit your changes or stash them to proceed.")); |
575 | 0 | return -1; |
576 | 0 | } |
577 | | |
578 | | static void update_abort_safety_file(void) |
579 | 0 | { |
580 | 0 | struct object_id head; |
581 | | |
582 | | /* Do nothing on a single-pick */ |
583 | 0 | if (!file_exists(git_path_seq_dir())) |
584 | 0 | return; |
585 | | |
586 | 0 | if (!repo_get_oid(the_repository, "HEAD", &head)) |
587 | 0 | write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head)); |
588 | 0 | else |
589 | 0 | write_file(git_path_abort_safety_file(), "%s", ""); |
590 | 0 | } |
591 | | |
592 | | static int fast_forward_to(struct repository *r, |
593 | | const struct object_id *to, |
594 | | const struct object_id *from, |
595 | | int unborn, |
596 | | struct replay_opts *opts) |
597 | 0 | { |
598 | 0 | struct ref_transaction *transaction; |
599 | 0 | struct strbuf sb = STRBUF_INIT; |
600 | 0 | struct strbuf err = STRBUF_INIT; |
601 | |
|
602 | 0 | repo_read_index(r); |
603 | 0 | if (checkout_fast_forward(r, from, to, 1)) |
604 | 0 | return -1; /* the callee should have complained already */ |
605 | | |
606 | 0 | strbuf_addf(&sb, "%s: fast-forward", action_name(opts)); |
607 | |
|
608 | 0 | transaction = ref_transaction_begin(&err); |
609 | 0 | if (!transaction || |
610 | 0 | ref_transaction_update(transaction, "HEAD", |
611 | 0 | to, unborn && !is_rebase_i(opts) ? |
612 | 0 | null_oid() : from, |
613 | 0 | 0, sb.buf, &err) || |
614 | 0 | ref_transaction_commit(transaction, &err)) { |
615 | 0 | ref_transaction_free(transaction); |
616 | 0 | error("%s", err.buf); |
617 | 0 | strbuf_release(&sb); |
618 | 0 | strbuf_release(&err); |
619 | 0 | return -1; |
620 | 0 | } |
621 | | |
622 | 0 | strbuf_release(&sb); |
623 | 0 | strbuf_release(&err); |
624 | 0 | ref_transaction_free(transaction); |
625 | 0 | update_abort_safety_file(); |
626 | 0 | return 0; |
627 | 0 | } |
628 | | |
629 | | enum commit_msg_cleanup_mode get_cleanup_mode(const char *cleanup_arg, |
630 | | int use_editor) |
631 | 7.32k | { |
632 | 7.32k | if (!cleanup_arg || !strcmp(cleanup_arg, "default")) |
633 | 7.32k | return use_editor ? COMMIT_MSG_CLEANUP_ALL : |
634 | 7.32k | COMMIT_MSG_CLEANUP_SPACE; |
635 | 0 | else if (!strcmp(cleanup_arg, "verbatim")) |
636 | 0 | return COMMIT_MSG_CLEANUP_NONE; |
637 | 0 | else if (!strcmp(cleanup_arg, "whitespace")) |
638 | 0 | return COMMIT_MSG_CLEANUP_SPACE; |
639 | 0 | else if (!strcmp(cleanup_arg, "strip")) |
640 | 0 | return COMMIT_MSG_CLEANUP_ALL; |
641 | 0 | else if (!strcmp(cleanup_arg, "scissors")) |
642 | 0 | return use_editor ? COMMIT_MSG_CLEANUP_SCISSORS : |
643 | 0 | COMMIT_MSG_CLEANUP_SPACE; |
644 | 0 | else |
645 | 0 | die(_("Invalid cleanup mode %s"), cleanup_arg); |
646 | 7.32k | } |
647 | | |
648 | | /* |
649 | | * NB using int rather than enum cleanup_mode to stop clang's |
650 | | * -Wtautological-constant-out-of-range-compare complaining that the comparison |
651 | | * is always true. |
652 | | */ |
653 | | static const char *describe_cleanup_mode(int cleanup_mode) |
654 | 0 | { |
655 | 0 | static const char *modes[] = { "whitespace", |
656 | 0 | "verbatim", |
657 | 0 | "scissors", |
658 | 0 | "strip" }; |
659 | |
|
660 | 0 | if (cleanup_mode < ARRAY_SIZE(modes)) |
661 | 0 | return modes[cleanup_mode]; |
662 | | |
663 | 0 | BUG("invalid cleanup_mode provided (%d)", cleanup_mode); |
664 | 0 | } |
665 | | |
666 | | void append_conflicts_hint(struct index_state *istate, |
667 | | struct strbuf *msgbuf, enum commit_msg_cleanup_mode cleanup_mode) |
668 | 0 | { |
669 | 0 | int i; |
670 | |
|
671 | 0 | if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) { |
672 | 0 | strbuf_addch(msgbuf, '\n'); |
673 | 0 | wt_status_append_cut_line(msgbuf); |
674 | 0 | strbuf_addch(msgbuf, comment_line_char); |
675 | 0 | } |
676 | |
|
677 | 0 | strbuf_addch(msgbuf, '\n'); |
678 | 0 | strbuf_commented_addf(msgbuf, comment_line_char, "Conflicts:\n"); |
679 | 0 | for (i = 0; i < istate->cache_nr;) { |
680 | 0 | const struct cache_entry *ce = istate->cache[i++]; |
681 | 0 | if (ce_stage(ce)) { |
682 | 0 | strbuf_commented_addf(msgbuf, comment_line_char, |
683 | 0 | "\t%s\n", ce->name); |
684 | 0 | while (i < istate->cache_nr && |
685 | 0 | !strcmp(ce->name, istate->cache[i]->name)) |
686 | 0 | i++; |
687 | 0 | } |
688 | 0 | } |
689 | 0 | } |
690 | | |
691 | | static int do_recursive_merge(struct repository *r, |
692 | | struct commit *base, struct commit *next, |
693 | | const char *base_label, const char *next_label, |
694 | | struct object_id *head, struct strbuf *msgbuf, |
695 | | struct replay_opts *opts) |
696 | 0 | { |
697 | 0 | struct merge_options o; |
698 | 0 | struct merge_result result; |
699 | 0 | struct tree *next_tree, *base_tree, *head_tree; |
700 | 0 | int clean, show_output; |
701 | 0 | int i; |
702 | 0 | struct lock_file index_lock = LOCK_INIT; |
703 | |
|
704 | 0 | if (repo_hold_locked_index(r, &index_lock, LOCK_REPORT_ON_ERROR) < 0) |
705 | 0 | return -1; |
706 | | |
707 | 0 | repo_read_index(r); |
708 | |
|
709 | 0 | init_merge_options(&o, r); |
710 | 0 | o.ancestor = base ? base_label : "(empty tree)"; |
711 | 0 | o.branch1 = "HEAD"; |
712 | 0 | o.branch2 = next ? next_label : "(empty tree)"; |
713 | 0 | if (is_rebase_i(opts)) |
714 | 0 | o.buffer_output = 2; |
715 | 0 | o.show_rename_progress = 1; |
716 | |
|
717 | 0 | head_tree = parse_tree_indirect(head); |
718 | 0 | next_tree = next ? repo_get_commit_tree(r, next) : empty_tree(r); |
719 | 0 | base_tree = base ? repo_get_commit_tree(r, base) : empty_tree(r); |
720 | |
|
721 | 0 | for (i = 0; i < opts->xopts.nr; i++) |
722 | 0 | parse_merge_opt(&o, opts->xopts.v[i]); |
723 | |
|
724 | 0 | if (!opts->strategy || !strcmp(opts->strategy, "ort")) { |
725 | 0 | memset(&result, 0, sizeof(result)); |
726 | 0 | merge_incore_nonrecursive(&o, base_tree, head_tree, next_tree, |
727 | 0 | &result); |
728 | 0 | show_output = !is_rebase_i(opts) || !result.clean; |
729 | | /* |
730 | | * TODO: merge_switch_to_result will update index/working tree; |
731 | | * we only really want to do that if !result.clean || this is |
732 | | * the final patch to be picked. But determining this is the |
733 | | * final patch would take some work, and "head_tree" would need |
734 | | * to be replace with the tree the index matched before we |
735 | | * started doing any picks. |
736 | | */ |
737 | 0 | merge_switch_to_result(&o, head_tree, &result, 1, show_output); |
738 | 0 | clean = result.clean; |
739 | 0 | } else { |
740 | 0 | ensure_full_index(r->index); |
741 | 0 | clean = merge_trees(&o, head_tree, next_tree, base_tree); |
742 | 0 | if (is_rebase_i(opts) && clean <= 0) |
743 | 0 | fputs(o.obuf.buf, stdout); |
744 | 0 | strbuf_release(&o.obuf); |
745 | 0 | } |
746 | 0 | if (clean < 0) { |
747 | 0 | rollback_lock_file(&index_lock); |
748 | 0 | return clean; |
749 | 0 | } |
750 | | |
751 | 0 | if (write_locked_index(r->index, &index_lock, |
752 | 0 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) |
753 | | /* |
754 | | * TRANSLATORS: %s will be "revert", "cherry-pick" or |
755 | | * "rebase". |
756 | | */ |
757 | 0 | return error(_("%s: Unable to write new index file"), |
758 | 0 | _(action_name(opts))); |
759 | | |
760 | 0 | if (!clean) |
761 | 0 | append_conflicts_hint(r->index, msgbuf, |
762 | 0 | opts->default_msg_cleanup); |
763 | |
|
764 | 0 | return !clean; |
765 | 0 | } |
766 | | |
767 | | static struct object_id *get_cache_tree_oid(struct index_state *istate) |
768 | 0 | { |
769 | 0 | if (!cache_tree_fully_valid(istate->cache_tree)) |
770 | 0 | if (cache_tree_update(istate, 0)) { |
771 | 0 | error(_("unable to update cache tree")); |
772 | 0 | return NULL; |
773 | 0 | } |
774 | | |
775 | 0 | return &istate->cache_tree->oid; |
776 | 0 | } |
777 | | |
778 | | static int is_index_unchanged(struct repository *r) |
779 | 0 | { |
780 | 0 | struct object_id head_oid, *cache_tree_oid; |
781 | 0 | struct commit *head_commit; |
782 | 0 | struct index_state *istate = r->index; |
783 | |
|
784 | 0 | if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL)) |
785 | 0 | return error(_("could not resolve HEAD commit")); |
786 | | |
787 | 0 | head_commit = lookup_commit(r, &head_oid); |
788 | | |
789 | | /* |
790 | | * If head_commit is NULL, check_commit, called from |
791 | | * lookup_commit, would have indicated that head_commit is not |
792 | | * a commit object already. repo_parse_commit() will return failure |
793 | | * without further complaints in such a case. Otherwise, if |
794 | | * the commit is invalid, repo_parse_commit() will complain. So |
795 | | * there is nothing for us to say here. Just return failure. |
796 | | */ |
797 | 0 | if (repo_parse_commit(r, head_commit)) |
798 | 0 | return -1; |
799 | | |
800 | 0 | if (!(cache_tree_oid = get_cache_tree_oid(istate))) |
801 | 0 | return -1; |
802 | | |
803 | 0 | return oideq(cache_tree_oid, get_commit_tree_oid(head_commit)); |
804 | 0 | } |
805 | | |
806 | | static int write_author_script(const char *message) |
807 | 0 | { |
808 | 0 | struct strbuf buf = STRBUF_INIT; |
809 | 0 | const char *eol; |
810 | 0 | int res; |
811 | |
|
812 | 0 | for (;;) |
813 | 0 | if (!*message || starts_with(message, "\n")) { |
814 | 0 | missing_author: |
815 | | /* Missing 'author' line? */ |
816 | 0 | unlink(rebase_path_author_script()); |
817 | 0 | return 0; |
818 | 0 | } else if (skip_prefix(message, "author ", &message)) |
819 | 0 | break; |
820 | 0 | else if ((eol = strchr(message, '\n'))) |
821 | 0 | message = eol + 1; |
822 | 0 | else |
823 | 0 | goto missing_author; |
824 | | |
825 | 0 | strbuf_addstr(&buf, "GIT_AUTHOR_NAME='"); |
826 | 0 | while (*message && *message != '\n' && *message != '\r') |
827 | 0 | if (skip_prefix(message, " <", &message)) |
828 | 0 | break; |
829 | 0 | else if (*message != '\'') |
830 | 0 | strbuf_addch(&buf, *(message++)); |
831 | 0 | else |
832 | 0 | strbuf_addf(&buf, "'\\%c'", *(message++)); |
833 | 0 | strbuf_addstr(&buf, "'\nGIT_AUTHOR_EMAIL='"); |
834 | 0 | while (*message && *message != '\n' && *message != '\r') |
835 | 0 | if (skip_prefix(message, "> ", &message)) |
836 | 0 | break; |
837 | 0 | else if (*message != '\'') |
838 | 0 | strbuf_addch(&buf, *(message++)); |
839 | 0 | else |
840 | 0 | strbuf_addf(&buf, "'\\%c'", *(message++)); |
841 | 0 | strbuf_addstr(&buf, "'\nGIT_AUTHOR_DATE='@"); |
842 | 0 | while (*message && *message != '\n' && *message != '\r') |
843 | 0 | if (*message != '\'') |
844 | 0 | strbuf_addch(&buf, *(message++)); |
845 | 0 | else |
846 | 0 | strbuf_addf(&buf, "'\\%c'", *(message++)); |
847 | 0 | strbuf_addch(&buf, '\''); |
848 | 0 | res = write_message(buf.buf, buf.len, rebase_path_author_script(), 1); |
849 | 0 | strbuf_release(&buf); |
850 | 0 | return res; |
851 | 0 | } |
852 | | |
853 | | /** |
854 | | * Take a series of KEY='VALUE' lines where VALUE part is |
855 | | * sq-quoted, and append <KEY, VALUE> at the end of the string list |
856 | | */ |
857 | | static int parse_key_value_squoted(char *buf, struct string_list *list) |
858 | 0 | { |
859 | 0 | while (*buf) { |
860 | 0 | struct string_list_item *item; |
861 | 0 | char *np; |
862 | 0 | char *cp = strchr(buf, '='); |
863 | 0 | if (!cp) { |
864 | 0 | np = strchrnul(buf, '\n'); |
865 | 0 | return error(_("no key present in '%.*s'"), |
866 | 0 | (int) (np - buf), buf); |
867 | 0 | } |
868 | 0 | np = strchrnul(cp, '\n'); |
869 | 0 | *cp++ = '\0'; |
870 | 0 | item = string_list_append(list, buf); |
871 | |
|
872 | 0 | buf = np + (*np == '\n'); |
873 | 0 | *np = '\0'; |
874 | 0 | cp = sq_dequote(cp); |
875 | 0 | if (!cp) |
876 | 0 | return error(_("unable to dequote value of '%s'"), |
877 | 0 | item->string); |
878 | 0 | item->util = xstrdup(cp); |
879 | 0 | } |
880 | 0 | return 0; |
881 | 0 | } |
882 | | |
883 | | /** |
884 | | * Reads and parses the state directory's "author-script" file, and sets name, |
885 | | * email and date accordingly. |
886 | | * Returns 0 on success, -1 if the file could not be parsed. |
887 | | * |
888 | | * The author script is of the format: |
889 | | * |
890 | | * GIT_AUTHOR_NAME='$author_name' |
891 | | * GIT_AUTHOR_EMAIL='$author_email' |
892 | | * GIT_AUTHOR_DATE='$author_date' |
893 | | * |
894 | | * where $author_name, $author_email and $author_date are quoted. We are strict |
895 | | * with our parsing, as the file was meant to be eval'd in the now-removed |
896 | | * git-am.sh/git-rebase--interactive.sh scripts, and thus if the file differs |
897 | | * from what this function expects, it is better to bail out than to do |
898 | | * something that the user does not expect. |
899 | | */ |
900 | | int read_author_script(const char *path, char **name, char **email, char **date, |
901 | | int allow_missing) |
902 | 0 | { |
903 | 0 | struct strbuf buf = STRBUF_INIT; |
904 | 0 | struct string_list kv = STRING_LIST_INIT_DUP; |
905 | 0 | int retval = -1; /* assume failure */ |
906 | 0 | int i, name_i = -2, email_i = -2, date_i = -2, err = 0; |
907 | |
|
908 | 0 | if (strbuf_read_file(&buf, path, 256) <= 0) { |
909 | 0 | strbuf_release(&buf); |
910 | 0 | if (errno == ENOENT && allow_missing) |
911 | 0 | return 0; |
912 | 0 | else |
913 | 0 | return error_errno(_("could not open '%s' for reading"), |
914 | 0 | path); |
915 | 0 | } |
916 | | |
917 | 0 | if (parse_key_value_squoted(buf.buf, &kv)) |
918 | 0 | goto finish; |
919 | | |
920 | 0 | for (i = 0; i < kv.nr; i++) { |
921 | 0 | if (!strcmp(kv.items[i].string, "GIT_AUTHOR_NAME")) { |
922 | 0 | if (name_i != -2) |
923 | 0 | name_i = error(_("'GIT_AUTHOR_NAME' already given")); |
924 | 0 | else |
925 | 0 | name_i = i; |
926 | 0 | } else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_EMAIL")) { |
927 | 0 | if (email_i != -2) |
928 | 0 | email_i = error(_("'GIT_AUTHOR_EMAIL' already given")); |
929 | 0 | else |
930 | 0 | email_i = i; |
931 | 0 | } else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_DATE")) { |
932 | 0 | if (date_i != -2) |
933 | 0 | date_i = error(_("'GIT_AUTHOR_DATE' already given")); |
934 | 0 | else |
935 | 0 | date_i = i; |
936 | 0 | } else { |
937 | 0 | err = error(_("unknown variable '%s'"), |
938 | 0 | kv.items[i].string); |
939 | 0 | } |
940 | 0 | } |
941 | 0 | if (name_i == -2) |
942 | 0 | error(_("missing 'GIT_AUTHOR_NAME'")); |
943 | 0 | if (email_i == -2) |
944 | 0 | error(_("missing 'GIT_AUTHOR_EMAIL'")); |
945 | 0 | if (date_i == -2) |
946 | 0 | error(_("missing 'GIT_AUTHOR_DATE'")); |
947 | 0 | if (name_i < 0 || email_i < 0 || date_i < 0 || err) |
948 | 0 | goto finish; |
949 | 0 | *name = kv.items[name_i].util; |
950 | 0 | *email = kv.items[email_i].util; |
951 | 0 | *date = kv.items[date_i].util; |
952 | 0 | retval = 0; |
953 | 0 | finish: |
954 | 0 | string_list_clear(&kv, !!retval); |
955 | 0 | strbuf_release(&buf); |
956 | 0 | return retval; |
957 | 0 | } |
958 | | |
959 | | /* |
960 | | * Read a GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL AND GIT_AUTHOR_DATE from a |
961 | | * file with shell quoting into struct strvec. Returns -1 on |
962 | | * error, 0 otherwise. |
963 | | */ |
964 | | static int read_env_script(struct strvec *env) |
965 | 0 | { |
966 | 0 | char *name, *email, *date; |
967 | |
|
968 | 0 | if (read_author_script(rebase_path_author_script(), |
969 | 0 | &name, &email, &date, 0)) |
970 | 0 | return -1; |
971 | | |
972 | 0 | strvec_pushf(env, "GIT_AUTHOR_NAME=%s", name); |
973 | 0 | strvec_pushf(env, "GIT_AUTHOR_EMAIL=%s", email); |
974 | 0 | strvec_pushf(env, "GIT_AUTHOR_DATE=%s", date); |
975 | 0 | free(name); |
976 | 0 | free(email); |
977 | 0 | free(date); |
978 | |
|
979 | 0 | return 0; |
980 | 0 | } |
981 | | |
982 | | static char *get_author(const char *message) |
983 | 0 | { |
984 | 0 | size_t len; |
985 | 0 | const char *a; |
986 | |
|
987 | 0 | a = find_commit_header(message, "author", &len); |
988 | 0 | if (a) |
989 | 0 | return xmemdupz(a, len); |
990 | | |
991 | 0 | return NULL; |
992 | 0 | } |
993 | | |
994 | | static const char *author_date_from_env(const struct strvec *env) |
995 | 0 | { |
996 | 0 | int i; |
997 | 0 | const char *date; |
998 | |
|
999 | 0 | for (i = 0; i < env->nr; i++) |
1000 | 0 | if (skip_prefix(env->v[i], |
1001 | 0 | "GIT_AUTHOR_DATE=", &date)) |
1002 | 0 | return date; |
1003 | | /* |
1004 | | * If GIT_AUTHOR_DATE is missing we should have already errored out when |
1005 | | * reading the script |
1006 | | */ |
1007 | 0 | BUG("GIT_AUTHOR_DATE missing from author script"); |
1008 | 0 | } |
1009 | | |
1010 | | static const char staged_changes_advice[] = |
1011 | | N_("you have staged changes in your working tree\n" |
1012 | | "If these changes are meant to be squashed into the previous commit, run:\n" |
1013 | | "\n" |
1014 | | " git commit --amend %s\n" |
1015 | | "\n" |
1016 | | "If they are meant to go into a new commit, run:\n" |
1017 | | "\n" |
1018 | | " git commit %s\n" |
1019 | | "\n" |
1020 | | "In both cases, once you're done, continue with:\n" |
1021 | | "\n" |
1022 | | " git rebase --continue\n"); |
1023 | | |
1024 | 0 | #define ALLOW_EMPTY (1<<0) |
1025 | 0 | #define EDIT_MSG (1<<1) |
1026 | 0 | #define AMEND_MSG (1<<2) |
1027 | 0 | #define CLEANUP_MSG (1<<3) |
1028 | 0 | #define VERIFY_MSG (1<<4) |
1029 | 0 | #define CREATE_ROOT_COMMIT (1<<5) |
1030 | 0 | #define VERBATIM_MSG (1<<6) |
1031 | | |
1032 | | static int run_command_silent_on_success(struct child_process *cmd) |
1033 | 0 | { |
1034 | 0 | struct strbuf buf = STRBUF_INIT; |
1035 | 0 | int rc; |
1036 | |
|
1037 | 0 | cmd->stdout_to_stderr = 1; |
1038 | 0 | rc = pipe_command(cmd, |
1039 | 0 | NULL, 0, |
1040 | 0 | NULL, 0, |
1041 | 0 | &buf, 0); |
1042 | |
|
1043 | 0 | if (rc) |
1044 | 0 | fputs(buf.buf, stderr); |
1045 | 0 | strbuf_release(&buf); |
1046 | 0 | return rc; |
1047 | 0 | } |
1048 | | |
1049 | | /* |
1050 | | * If we are cherry-pick, and if the merge did not result in |
1051 | | * hand-editing, we will hit this commit and inherit the original |
1052 | | * author date and name. |
1053 | | * |
1054 | | * If we are revert, or if our cherry-pick results in a hand merge, |
1055 | | * we had better say that the current user is responsible for that. |
1056 | | * |
1057 | | * An exception is when run_git_commit() is called during an |
1058 | | * interactive rebase: in that case, we will want to retain the |
1059 | | * author metadata. |
1060 | | */ |
1061 | | static int run_git_commit(const char *defmsg, |
1062 | | struct replay_opts *opts, |
1063 | | unsigned int flags) |
1064 | 0 | { |
1065 | 0 | struct child_process cmd = CHILD_PROCESS_INIT; |
1066 | |
|
1067 | 0 | if ((flags & CLEANUP_MSG) && (flags & VERBATIM_MSG)) |
1068 | 0 | BUG("CLEANUP_MSG and VERBATIM_MSG are mutually exclusive"); |
1069 | | |
1070 | 0 | cmd.git_cmd = 1; |
1071 | |
|
1072 | 0 | if (is_rebase_i(opts) && |
1073 | 0 | ((opts->committer_date_is_author_date && !opts->ignore_date) || |
1074 | 0 | !(!defmsg && (flags & AMEND_MSG))) && |
1075 | 0 | read_env_script(&cmd.env)) { |
1076 | 0 | const char *gpg_opt = gpg_sign_opt_quoted(opts); |
1077 | |
|
1078 | 0 | return error(_(staged_changes_advice), |
1079 | 0 | gpg_opt, gpg_opt); |
1080 | 0 | } |
1081 | | |
1082 | 0 | strvec_pushf(&cmd.env, GIT_REFLOG_ACTION "=%s", opts->reflog_message); |
1083 | |
|
1084 | 0 | if (opts->committer_date_is_author_date) |
1085 | 0 | strvec_pushf(&cmd.env, "GIT_COMMITTER_DATE=%s", |
1086 | 0 | opts->ignore_date ? |
1087 | 0 | "" : |
1088 | 0 | author_date_from_env(&cmd.env)); |
1089 | 0 | if (opts->ignore_date) |
1090 | 0 | strvec_push(&cmd.env, "GIT_AUTHOR_DATE="); |
1091 | |
|
1092 | 0 | strvec_push(&cmd.args, "commit"); |
1093 | |
|
1094 | 0 | if (!(flags & VERIFY_MSG)) |
1095 | 0 | strvec_push(&cmd.args, "-n"); |
1096 | 0 | if ((flags & AMEND_MSG)) |
1097 | 0 | strvec_push(&cmd.args, "--amend"); |
1098 | 0 | if (opts->gpg_sign) |
1099 | 0 | strvec_pushf(&cmd.args, "-S%s", opts->gpg_sign); |
1100 | 0 | else |
1101 | 0 | strvec_push(&cmd.args, "--no-gpg-sign"); |
1102 | 0 | if (defmsg) |
1103 | 0 | strvec_pushl(&cmd.args, "-F", defmsg, NULL); |
1104 | 0 | else if (!(flags & EDIT_MSG)) |
1105 | 0 | strvec_pushl(&cmd.args, "-C", "HEAD", NULL); |
1106 | 0 | if ((flags & CLEANUP_MSG)) |
1107 | 0 | strvec_push(&cmd.args, "--cleanup=strip"); |
1108 | 0 | if ((flags & VERBATIM_MSG)) |
1109 | 0 | strvec_push(&cmd.args, "--cleanup=verbatim"); |
1110 | 0 | if ((flags & EDIT_MSG)) |
1111 | 0 | strvec_push(&cmd.args, "-e"); |
1112 | 0 | else if (!(flags & CLEANUP_MSG) && |
1113 | 0 | !opts->signoff && !opts->record_origin && |
1114 | 0 | !opts->explicit_cleanup) |
1115 | 0 | strvec_push(&cmd.args, "--cleanup=verbatim"); |
1116 | |
|
1117 | 0 | if ((flags & ALLOW_EMPTY)) |
1118 | 0 | strvec_push(&cmd.args, "--allow-empty"); |
1119 | |
|
1120 | 0 | if (!(flags & EDIT_MSG)) |
1121 | 0 | strvec_push(&cmd.args, "--allow-empty-message"); |
1122 | |
|
1123 | 0 | if (is_rebase_i(opts) && !(flags & EDIT_MSG)) |
1124 | 0 | return run_command_silent_on_success(&cmd); |
1125 | 0 | else |
1126 | 0 | return run_command(&cmd); |
1127 | 0 | } |
1128 | | |
1129 | | static int rest_is_empty(const struct strbuf *sb, int start) |
1130 | 7.22k | { |
1131 | 7.22k | int i, eol; |
1132 | 7.22k | const char *nl; |
1133 | | |
1134 | | /* Check if the rest is just whitespace and Signed-off-by's. */ |
1135 | 7.22k | for (i = start; i < sb->len; i++) { |
1136 | 7.22k | nl = memchr(sb->buf + i, '\n', sb->len - i); |
1137 | 7.22k | if (nl) |
1138 | 7.22k | eol = nl - sb->buf; |
1139 | 0 | else |
1140 | 0 | eol = sb->len; |
1141 | | |
1142 | 7.22k | if (strlen(sign_off_header) <= eol - i && |
1143 | 7.22k | starts_with(sb->buf + i, sign_off_header)) { |
1144 | 0 | i = eol; |
1145 | 0 | continue; |
1146 | 0 | } |
1147 | 7.22k | while (i < eol) |
1148 | 7.22k | if (!isspace(sb->buf[i++])) |
1149 | 7.22k | return 0; |
1150 | 7.22k | } |
1151 | | |
1152 | 0 | return 1; |
1153 | 7.22k | } |
1154 | | |
1155 | | void cleanup_message(struct strbuf *msgbuf, |
1156 | | enum commit_msg_cleanup_mode cleanup_mode, int verbose) |
1157 | 7.22k | { |
1158 | 7.22k | if (verbose || /* Truncate the message just before the diff, if any. */ |
1159 | 7.22k | cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) |
1160 | 0 | strbuf_setlen(msgbuf, wt_status_locate_end(msgbuf->buf, msgbuf->len)); |
1161 | 7.22k | if (cleanup_mode != COMMIT_MSG_CLEANUP_NONE) |
1162 | 7.22k | strbuf_stripspace(msgbuf, |
1163 | 7.22k | cleanup_mode == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0'); |
1164 | 7.22k | } |
1165 | | |
1166 | | /* |
1167 | | * Find out if the message in the strbuf contains only whitespace and |
1168 | | * Signed-off-by lines. |
1169 | | */ |
1170 | | int message_is_empty(const struct strbuf *sb, |
1171 | | enum commit_msg_cleanup_mode cleanup_mode) |
1172 | 7.22k | { |
1173 | 7.22k | if (cleanup_mode == COMMIT_MSG_CLEANUP_NONE && sb->len) |
1174 | 0 | return 0; |
1175 | 7.22k | return rest_is_empty(sb, 0); |
1176 | 7.22k | } |
1177 | | |
1178 | | /* |
1179 | | * See if the user edited the message in the editor or left what |
1180 | | * was in the template intact |
1181 | | */ |
1182 | | int template_untouched(const struct strbuf *sb, const char *template_file, |
1183 | | enum commit_msg_cleanup_mode cleanup_mode) |
1184 | 7.22k | { |
1185 | 7.22k | struct strbuf tmpl = STRBUF_INIT; |
1186 | 7.22k | const char *start; |
1187 | | |
1188 | 7.22k | if (cleanup_mode == COMMIT_MSG_CLEANUP_NONE && sb->len) |
1189 | 0 | return 0; |
1190 | | |
1191 | 7.22k | if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0) |
1192 | 7.22k | return 0; |
1193 | | |
1194 | 0 | strbuf_stripspace(&tmpl, |
1195 | 0 | cleanup_mode == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0'); |
1196 | 0 | if (!skip_prefix(sb->buf, tmpl.buf, &start)) |
1197 | 0 | start = sb->buf; |
1198 | 0 | strbuf_release(&tmpl); |
1199 | 0 | return rest_is_empty(sb, start - sb->buf); |
1200 | 7.22k | } |
1201 | | |
1202 | | int update_head_with_reflog(const struct commit *old_head, |
1203 | | const struct object_id *new_head, |
1204 | | const char *action, const struct strbuf *msg, |
1205 | | struct strbuf *err) |
1206 | 7.22k | { |
1207 | 7.22k | struct ref_transaction *transaction; |
1208 | 7.22k | struct strbuf sb = STRBUF_INIT; |
1209 | 7.22k | const char *nl; |
1210 | 7.22k | int ret = 0; |
1211 | | |
1212 | 7.22k | if (action) { |
1213 | 7.22k | strbuf_addstr(&sb, action); |
1214 | 7.22k | strbuf_addstr(&sb, ": "); |
1215 | 7.22k | } |
1216 | | |
1217 | 7.22k | nl = strchr(msg->buf, '\n'); |
1218 | 7.22k | if (nl) { |
1219 | 7.22k | strbuf_add(&sb, msg->buf, nl + 1 - msg->buf); |
1220 | 7.22k | } else { |
1221 | 0 | strbuf_addbuf(&sb, msg); |
1222 | 0 | strbuf_addch(&sb, '\n'); |
1223 | 0 | } |
1224 | | |
1225 | 7.22k | transaction = ref_transaction_begin(err); |
1226 | 7.22k | if (!transaction || |
1227 | 7.22k | ref_transaction_update(transaction, "HEAD", new_head, |
1228 | 7.22k | old_head ? &old_head->object.oid : null_oid(), |
1229 | 7.22k | 0, sb.buf, err) || |
1230 | 7.22k | ref_transaction_commit(transaction, err)) { |
1231 | 0 | ret = -1; |
1232 | 0 | } |
1233 | 7.22k | ref_transaction_free(transaction); |
1234 | 7.22k | strbuf_release(&sb); |
1235 | | |
1236 | 7.22k | return ret; |
1237 | 7.22k | } |
1238 | | |
1239 | | static int run_rewrite_hook(const struct object_id *oldoid, |
1240 | | const struct object_id *newoid) |
1241 | 0 | { |
1242 | 0 | struct child_process proc = CHILD_PROCESS_INIT; |
1243 | 0 | int code; |
1244 | 0 | struct strbuf sb = STRBUF_INIT; |
1245 | 0 | const char *hook_path = find_hook("post-rewrite"); |
1246 | |
|
1247 | 0 | if (!hook_path) |
1248 | 0 | return 0; |
1249 | | |
1250 | 0 | strvec_pushl(&proc.args, hook_path, "amend", NULL); |
1251 | 0 | proc.in = -1; |
1252 | 0 | proc.stdout_to_stderr = 1; |
1253 | 0 | proc.trace2_hook_name = "post-rewrite"; |
1254 | |
|
1255 | 0 | code = start_command(&proc); |
1256 | 0 | if (code) |
1257 | 0 | return code; |
1258 | 0 | strbuf_addf(&sb, "%s %s\n", oid_to_hex(oldoid), oid_to_hex(newoid)); |
1259 | 0 | sigchain_push(SIGPIPE, SIG_IGN); |
1260 | 0 | write_in_full(proc.in, sb.buf, sb.len); |
1261 | 0 | close(proc.in); |
1262 | 0 | strbuf_release(&sb); |
1263 | 0 | sigchain_pop(SIGPIPE); |
1264 | 0 | return finish_command(&proc); |
1265 | 0 | } |
1266 | | |
1267 | | void commit_post_rewrite(struct repository *r, |
1268 | | const struct commit *old_head, |
1269 | | const struct object_id *new_head) |
1270 | 0 | { |
1271 | 0 | struct notes_rewrite_cfg *cfg; |
1272 | |
|
1273 | 0 | cfg = init_copy_notes_for_rewrite("amend"); |
1274 | 0 | if (cfg) { |
1275 | | /* we are amending, so old_head is not NULL */ |
1276 | 0 | copy_note_for_rewrite(cfg, &old_head->object.oid, new_head); |
1277 | 0 | finish_copy_notes_for_rewrite(r, cfg, "Notes added by 'git commit --amend'"); |
1278 | 0 | } |
1279 | 0 | run_rewrite_hook(&old_head->object.oid, new_head); |
1280 | 0 | } |
1281 | | |
1282 | | static int run_prepare_commit_msg_hook(struct repository *r, |
1283 | | struct strbuf *msg, |
1284 | | const char *commit) |
1285 | 0 | { |
1286 | 0 | int ret = 0; |
1287 | 0 | const char *name, *arg1 = NULL, *arg2 = NULL; |
1288 | |
|
1289 | 0 | name = git_path_commit_editmsg(); |
1290 | 0 | if (write_message(msg->buf, msg->len, name, 0)) |
1291 | 0 | return -1; |
1292 | | |
1293 | 0 | if (commit) { |
1294 | 0 | arg1 = "commit"; |
1295 | 0 | arg2 = commit; |
1296 | 0 | } else { |
1297 | 0 | arg1 = "message"; |
1298 | 0 | } |
1299 | 0 | if (run_commit_hook(0, r->index_file, NULL, "prepare-commit-msg", name, |
1300 | 0 | arg1, arg2, NULL)) |
1301 | 0 | ret = error(_("'prepare-commit-msg' hook failed")); |
1302 | |
|
1303 | 0 | return ret; |
1304 | 0 | } |
1305 | | |
1306 | | static const char implicit_ident_advice_noconfig[] = |
1307 | | N_("Your name and email address were configured automatically based\n" |
1308 | | "on your username and hostname. Please check that they are accurate.\n" |
1309 | | "You can suppress this message by setting them explicitly. Run the\n" |
1310 | | "following command and follow the instructions in your editor to edit\n" |
1311 | | "your configuration file:\n" |
1312 | | "\n" |
1313 | | " git config --global --edit\n" |
1314 | | "\n" |
1315 | | "After doing this, you may fix the identity used for this commit with:\n" |
1316 | | "\n" |
1317 | | " git commit --amend --reset-author\n"); |
1318 | | |
1319 | | static const char implicit_ident_advice_config[] = |
1320 | | N_("Your name and email address were configured automatically based\n" |
1321 | | "on your username and hostname. Please check that they are accurate.\n" |
1322 | | "You can suppress this message by setting them explicitly:\n" |
1323 | | "\n" |
1324 | | " git config --global user.name \"Your Name\"\n" |
1325 | | " git config --global user.email you@example.com\n" |
1326 | | "\n" |
1327 | | "After doing this, you may fix the identity used for this commit with:\n" |
1328 | | "\n" |
1329 | | " git commit --amend --reset-author\n"); |
1330 | | |
1331 | | static const char *implicit_ident_advice(void) |
1332 | 0 | { |
1333 | 0 | char *user_config = interpolate_path("~/.gitconfig", 0); |
1334 | 0 | char *xdg_config = xdg_config_home("config"); |
1335 | 0 | int config_exists = file_exists(user_config) || file_exists(xdg_config); |
1336 | |
|
1337 | 0 | free(user_config); |
1338 | 0 | free(xdg_config); |
1339 | |
|
1340 | 0 | if (config_exists) |
1341 | 0 | return _(implicit_ident_advice_config); |
1342 | 0 | else |
1343 | 0 | return _(implicit_ident_advice_noconfig); |
1344 | |
|
1345 | 0 | } |
1346 | | |
1347 | | void print_commit_summary(struct repository *r, |
1348 | | const char *prefix, |
1349 | | const struct object_id *oid, |
1350 | | unsigned int flags) |
1351 | 7.22k | { |
1352 | 7.22k | struct rev_info rev; |
1353 | 7.22k | struct commit *commit; |
1354 | 7.22k | struct strbuf format = STRBUF_INIT; |
1355 | 7.22k | const char *head; |
1356 | 7.22k | struct pretty_print_context pctx = {0}; |
1357 | 7.22k | struct strbuf author_ident = STRBUF_INIT; |
1358 | 7.22k | struct strbuf committer_ident = STRBUF_INIT; |
1359 | 7.22k | struct ref_store *refs; |
1360 | | |
1361 | 7.22k | commit = lookup_commit(r, oid); |
1362 | 7.22k | if (!commit) |
1363 | 0 | die(_("couldn't look up newly created commit")); |
1364 | 7.22k | if (repo_parse_commit(r, commit)) |
1365 | 0 | die(_("could not parse newly created commit")); |
1366 | | |
1367 | 7.22k | strbuf_addstr(&format, "format:%h] %s"); |
1368 | | |
1369 | 7.22k | repo_format_commit_message(r, commit, "%an <%ae>", &author_ident, |
1370 | 7.22k | &pctx); |
1371 | 7.22k | repo_format_commit_message(r, commit, "%cn <%ce>", &committer_ident, |
1372 | 7.22k | &pctx); |
1373 | 7.22k | if (strbuf_cmp(&author_ident, &committer_ident)) { |
1374 | 0 | strbuf_addstr(&format, "\n Author: "); |
1375 | 0 | strbuf_addbuf_percentquote(&format, &author_ident); |
1376 | 0 | } |
1377 | 7.22k | if (flags & SUMMARY_SHOW_AUTHOR_DATE) { |
1378 | 0 | struct strbuf date = STRBUF_INIT; |
1379 | |
|
1380 | 0 | repo_format_commit_message(r, commit, "%ad", &date, &pctx); |
1381 | 0 | strbuf_addstr(&format, "\n Date: "); |
1382 | 0 | strbuf_addbuf_percentquote(&format, &date); |
1383 | 0 | strbuf_release(&date); |
1384 | 0 | } |
1385 | 7.22k | if (!committer_ident_sufficiently_given()) { |
1386 | 0 | strbuf_addstr(&format, "\n Committer: "); |
1387 | 0 | strbuf_addbuf_percentquote(&format, &committer_ident); |
1388 | 0 | if (advice_enabled(ADVICE_IMPLICIT_IDENTITY)) { |
1389 | 0 | strbuf_addch(&format, '\n'); |
1390 | 0 | strbuf_addstr(&format, implicit_ident_advice()); |
1391 | 0 | } |
1392 | 0 | } |
1393 | 7.22k | strbuf_release(&author_ident); |
1394 | 7.22k | strbuf_release(&committer_ident); |
1395 | | |
1396 | 7.22k | repo_init_revisions(r, &rev, prefix); |
1397 | 7.22k | setup_revisions(0, NULL, &rev, NULL); |
1398 | | |
1399 | 7.22k | rev.diff = 1; |
1400 | 7.22k | rev.diffopt.output_format = |
1401 | 7.22k | DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY; |
1402 | | |
1403 | 7.22k | rev.verbose_header = 1; |
1404 | 7.22k | rev.show_root_diff = 1; |
1405 | 7.22k | get_commit_format(format.buf, &rev); |
1406 | 7.22k | rev.always_show_header = 0; |
1407 | 7.22k | rev.diffopt.detect_rename = DIFF_DETECT_RENAME; |
1408 | 7.22k | diff_setup_done(&rev.diffopt); |
1409 | | |
1410 | 7.22k | refs = get_main_ref_store(r); |
1411 | 7.22k | head = refs_resolve_ref_unsafe(refs, "HEAD", 0, NULL, NULL); |
1412 | 7.22k | if (!head) |
1413 | 0 | die(_("unable to resolve HEAD after creating commit")); |
1414 | 7.22k | if (!strcmp(head, "HEAD")) |
1415 | 0 | head = _("detached HEAD"); |
1416 | 7.22k | else |
1417 | 7.22k | skip_prefix(head, "refs/heads/", &head); |
1418 | 7.22k | printf("[%s%s ", head, (flags & SUMMARY_INITIAL_COMMIT) ? |
1419 | 6.00k | _(" (root-commit)") : ""); |
1420 | | |
1421 | 7.22k | if (!log_tree_commit(&rev, commit)) { |
1422 | 0 | rev.always_show_header = 1; |
1423 | 0 | rev.use_terminator = 1; |
1424 | 0 | log_tree_commit(&rev, commit); |
1425 | 0 | } |
1426 | | |
1427 | 7.22k | release_revisions(&rev); |
1428 | 7.22k | strbuf_release(&format); |
1429 | 7.22k | } |
1430 | | |
1431 | | static int parse_head(struct repository *r, struct commit **head) |
1432 | 0 | { |
1433 | 0 | struct commit *current_head; |
1434 | 0 | struct object_id oid; |
1435 | |
|
1436 | 0 | if (repo_get_oid(r, "HEAD", &oid)) { |
1437 | 0 | current_head = NULL; |
1438 | 0 | } else { |
1439 | 0 | current_head = lookup_commit_reference(r, &oid); |
1440 | 0 | if (!current_head) |
1441 | 0 | return error(_("could not parse HEAD")); |
1442 | 0 | if (!oideq(&oid, ¤t_head->object.oid)) { |
1443 | 0 | warning(_("HEAD %s is not a commit!"), |
1444 | 0 | oid_to_hex(&oid)); |
1445 | 0 | } |
1446 | 0 | if (repo_parse_commit(r, current_head)) |
1447 | 0 | return error(_("could not parse HEAD commit")); |
1448 | 0 | } |
1449 | 0 | *head = current_head; |
1450 | |
|
1451 | 0 | return 0; |
1452 | 0 | } |
1453 | | |
1454 | | /* |
1455 | | * Try to commit without forking 'git commit'. In some cases we need |
1456 | | * to run 'git commit' to display an error message |
1457 | | * |
1458 | | * Returns: |
1459 | | * -1 - error unable to commit |
1460 | | * 0 - success |
1461 | | * 1 - run 'git commit' |
1462 | | */ |
1463 | | static int try_to_commit(struct repository *r, |
1464 | | struct strbuf *msg, const char *author, |
1465 | | struct replay_opts *opts, unsigned int flags, |
1466 | | struct object_id *oid) |
1467 | 0 | { |
1468 | 0 | struct object_id tree; |
1469 | 0 | struct commit *current_head = NULL; |
1470 | 0 | struct commit_list *parents = NULL; |
1471 | 0 | struct commit_extra_header *extra = NULL; |
1472 | 0 | struct strbuf err = STRBUF_INIT; |
1473 | 0 | struct strbuf commit_msg = STRBUF_INIT; |
1474 | 0 | char *amend_author = NULL; |
1475 | 0 | const char *committer = NULL; |
1476 | 0 | const char *hook_commit = NULL; |
1477 | 0 | enum commit_msg_cleanup_mode cleanup; |
1478 | 0 | int res = 0; |
1479 | |
|
1480 | 0 | if ((flags & CLEANUP_MSG) && (flags & VERBATIM_MSG)) |
1481 | 0 | BUG("CLEANUP_MSG and VERBATIM_MSG are mutually exclusive"); |
1482 | | |
1483 | 0 | if (parse_head(r, ¤t_head)) |
1484 | 0 | return -1; |
1485 | | |
1486 | 0 | if (flags & AMEND_MSG) { |
1487 | 0 | const char *exclude_gpgsig[] = { "gpgsig", "gpgsig-sha256", NULL }; |
1488 | 0 | const char *out_enc = get_commit_output_encoding(); |
1489 | 0 | const char *message = repo_logmsg_reencode(r, current_head, |
1490 | 0 | NULL, out_enc); |
1491 | |
|
1492 | 0 | if (!msg) { |
1493 | 0 | const char *orig_message = NULL; |
1494 | |
|
1495 | 0 | find_commit_subject(message, &orig_message); |
1496 | 0 | msg = &commit_msg; |
1497 | 0 | strbuf_addstr(msg, orig_message); |
1498 | 0 | hook_commit = "HEAD"; |
1499 | 0 | } |
1500 | 0 | author = amend_author = get_author(message); |
1501 | 0 | repo_unuse_commit_buffer(r, current_head, |
1502 | 0 | message); |
1503 | 0 | if (!author) { |
1504 | 0 | res = error(_("unable to parse commit author")); |
1505 | 0 | goto out; |
1506 | 0 | } |
1507 | 0 | parents = copy_commit_list(current_head->parents); |
1508 | 0 | extra = read_commit_extra_headers(current_head, exclude_gpgsig); |
1509 | 0 | } else if (current_head && |
1510 | 0 | (!(flags & CREATE_ROOT_COMMIT) || (flags & AMEND_MSG))) { |
1511 | 0 | commit_list_insert(current_head, &parents); |
1512 | 0 | } |
1513 | | |
1514 | 0 | if (write_index_as_tree(&tree, r->index, r->index_file, 0, NULL)) { |
1515 | 0 | res = error(_("git write-tree failed to write a tree")); |
1516 | 0 | goto out; |
1517 | 0 | } |
1518 | | |
1519 | 0 | if (!(flags & ALLOW_EMPTY)) { |
1520 | 0 | struct commit *first_parent = current_head; |
1521 | |
|
1522 | 0 | if (flags & AMEND_MSG) { |
1523 | 0 | if (current_head->parents) { |
1524 | 0 | first_parent = current_head->parents->item; |
1525 | 0 | if (repo_parse_commit(r, first_parent)) { |
1526 | 0 | res = error(_("could not parse HEAD commit")); |
1527 | 0 | goto out; |
1528 | 0 | } |
1529 | 0 | } else { |
1530 | 0 | first_parent = NULL; |
1531 | 0 | } |
1532 | 0 | } |
1533 | 0 | if (oideq(first_parent |
1534 | 0 | ? get_commit_tree_oid(first_parent) |
1535 | 0 | : the_hash_algo->empty_tree, |
1536 | 0 | &tree)) { |
1537 | 0 | res = 1; /* run 'git commit' to display error message */ |
1538 | 0 | goto out; |
1539 | 0 | } |
1540 | 0 | } |
1541 | | |
1542 | 0 | if (hook_exists("prepare-commit-msg")) { |
1543 | 0 | res = run_prepare_commit_msg_hook(r, msg, hook_commit); |
1544 | 0 | if (res) |
1545 | 0 | goto out; |
1546 | 0 | if (strbuf_read_file(&commit_msg, git_path_commit_editmsg(), |
1547 | 0 | 2048) < 0) { |
1548 | 0 | res = error_errno(_("unable to read commit message " |
1549 | 0 | "from '%s'"), |
1550 | 0 | git_path_commit_editmsg()); |
1551 | 0 | goto out; |
1552 | 0 | } |
1553 | 0 | msg = &commit_msg; |
1554 | 0 | } |
1555 | | |
1556 | 0 | if (flags & CLEANUP_MSG) |
1557 | 0 | cleanup = COMMIT_MSG_CLEANUP_ALL; |
1558 | 0 | else if (flags & VERBATIM_MSG) |
1559 | 0 | cleanup = COMMIT_MSG_CLEANUP_NONE; |
1560 | 0 | else if ((opts->signoff || opts->record_origin) && |
1561 | 0 | !opts->explicit_cleanup) |
1562 | 0 | cleanup = COMMIT_MSG_CLEANUP_SPACE; |
1563 | 0 | else |
1564 | 0 | cleanup = opts->default_msg_cleanup; |
1565 | |
|
1566 | 0 | if (cleanup != COMMIT_MSG_CLEANUP_NONE) |
1567 | 0 | strbuf_stripspace(msg, |
1568 | 0 | cleanup == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0'); |
1569 | 0 | if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) { |
1570 | 0 | res = 1; /* run 'git commit' to display error message */ |
1571 | 0 | goto out; |
1572 | 0 | } |
1573 | | |
1574 | 0 | if (opts->committer_date_is_author_date) { |
1575 | 0 | struct ident_split id; |
1576 | 0 | struct strbuf date = STRBUF_INIT; |
1577 | |
|
1578 | 0 | if (!opts->ignore_date) { |
1579 | 0 | if (split_ident_line(&id, author, (int)strlen(author)) < 0) { |
1580 | 0 | res = error(_("invalid author identity '%s'"), |
1581 | 0 | author); |
1582 | 0 | goto out; |
1583 | 0 | } |
1584 | 0 | if (!id.date_begin) { |
1585 | 0 | res = error(_( |
1586 | 0 | "corrupt author: missing date information")); |
1587 | 0 | goto out; |
1588 | 0 | } |
1589 | 0 | strbuf_addf(&date, "@%.*s %.*s", |
1590 | 0 | (int)(id.date_end - id.date_begin), |
1591 | 0 | id.date_begin, |
1592 | 0 | (int)(id.tz_end - id.tz_begin), |
1593 | 0 | id.tz_begin); |
1594 | 0 | } else { |
1595 | 0 | reset_ident_date(); |
1596 | 0 | } |
1597 | 0 | committer = fmt_ident(getenv("GIT_COMMITTER_NAME"), |
1598 | 0 | getenv("GIT_COMMITTER_EMAIL"), |
1599 | 0 | WANT_COMMITTER_IDENT, |
1600 | 0 | opts->ignore_date ? NULL : date.buf, |
1601 | 0 | IDENT_STRICT); |
1602 | 0 | strbuf_release(&date); |
1603 | 0 | } else { |
1604 | 0 | reset_ident_date(); |
1605 | 0 | } |
1606 | | |
1607 | 0 | if (opts->ignore_date) { |
1608 | 0 | struct ident_split id; |
1609 | 0 | char *name, *email; |
1610 | |
|
1611 | 0 | if (split_ident_line(&id, author, strlen(author)) < 0) { |
1612 | 0 | error(_("invalid author identity '%s'"), author); |
1613 | 0 | goto out; |
1614 | 0 | } |
1615 | 0 | name = xmemdupz(id.name_begin, id.name_end - id.name_begin); |
1616 | 0 | email = xmemdupz(id.mail_begin, id.mail_end - id.mail_begin); |
1617 | 0 | author = fmt_ident(name, email, WANT_AUTHOR_IDENT, NULL, |
1618 | 0 | IDENT_STRICT); |
1619 | 0 | free(name); |
1620 | 0 | free(email); |
1621 | 0 | } |
1622 | | |
1623 | 0 | if (commit_tree_extended(msg->buf, msg->len, &tree, parents, oid, |
1624 | 0 | author, committer, opts->gpg_sign, extra)) { |
1625 | 0 | res = error(_("failed to write commit object")); |
1626 | 0 | goto out; |
1627 | 0 | } |
1628 | | |
1629 | 0 | if (update_head_with_reflog(current_head, oid, opts->reflog_message, |
1630 | 0 | msg, &err)) { |
1631 | 0 | res = error("%s", err.buf); |
1632 | 0 | goto out; |
1633 | 0 | } |
1634 | | |
1635 | 0 | run_commit_hook(0, r->index_file, NULL, "post-commit", NULL); |
1636 | 0 | if (flags & AMEND_MSG) |
1637 | 0 | commit_post_rewrite(r, current_head, oid); |
1638 | |
|
1639 | 0 | out: |
1640 | 0 | free_commit_extra_headers(extra); |
1641 | 0 | strbuf_release(&err); |
1642 | 0 | strbuf_release(&commit_msg); |
1643 | 0 | free(amend_author); |
1644 | |
|
1645 | 0 | return res; |
1646 | 0 | } |
1647 | | |
1648 | | static int write_rebase_head(struct object_id *oid) |
1649 | 0 | { |
1650 | 0 | if (update_ref("rebase", "REBASE_HEAD", oid, |
1651 | 0 | NULL, REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) |
1652 | 0 | return error(_("could not update %s"), "REBASE_HEAD"); |
1653 | | |
1654 | 0 | return 0; |
1655 | 0 | } |
1656 | | |
1657 | | static int do_commit(struct repository *r, |
1658 | | const char *msg_file, const char *author, |
1659 | | struct replay_opts *opts, unsigned int flags, |
1660 | | struct object_id *oid) |
1661 | 0 | { |
1662 | 0 | int res = 1; |
1663 | |
|
1664 | 0 | if (!(flags & EDIT_MSG) && !(flags & VERIFY_MSG)) { |
1665 | 0 | struct object_id oid; |
1666 | 0 | struct strbuf sb = STRBUF_INIT; |
1667 | |
|
1668 | 0 | if (msg_file && strbuf_read_file(&sb, msg_file, 2048) < 0) |
1669 | 0 | return error_errno(_("unable to read commit message " |
1670 | 0 | "from '%s'"), |
1671 | 0 | msg_file); |
1672 | | |
1673 | 0 | res = try_to_commit(r, msg_file ? &sb : NULL, |
1674 | 0 | author, opts, flags, &oid); |
1675 | 0 | strbuf_release(&sb); |
1676 | 0 | if (!res) { |
1677 | 0 | refs_delete_ref(get_main_ref_store(r), "", |
1678 | 0 | "CHERRY_PICK_HEAD", NULL, 0); |
1679 | 0 | unlink(git_path_merge_msg(r)); |
1680 | 0 | if (!is_rebase_i(opts)) |
1681 | 0 | print_commit_summary(r, NULL, &oid, |
1682 | 0 | SUMMARY_SHOW_AUTHOR_DATE); |
1683 | 0 | return res; |
1684 | 0 | } |
1685 | 0 | } |
1686 | 0 | if (res == 1) { |
1687 | 0 | if (is_rebase_i(opts) && oid) |
1688 | 0 | if (write_rebase_head(oid)) |
1689 | 0 | return -1; |
1690 | 0 | return run_git_commit(msg_file, opts, flags); |
1691 | 0 | } |
1692 | | |
1693 | 0 | return res; |
1694 | 0 | } |
1695 | | |
1696 | | static int is_original_commit_empty(struct commit *commit) |
1697 | 0 | { |
1698 | 0 | const struct object_id *ptree_oid; |
1699 | |
|
1700 | 0 | if (repo_parse_commit(the_repository, commit)) |
1701 | 0 | return error(_("could not parse commit %s"), |
1702 | 0 | oid_to_hex(&commit->object.oid)); |
1703 | 0 | if (commit->parents) { |
1704 | 0 | struct commit *parent = commit->parents->item; |
1705 | 0 | if (repo_parse_commit(the_repository, parent)) |
1706 | 0 | return error(_("could not parse parent commit %s"), |
1707 | 0 | oid_to_hex(&parent->object.oid)); |
1708 | 0 | ptree_oid = get_commit_tree_oid(parent); |
1709 | 0 | } else { |
1710 | 0 | ptree_oid = the_hash_algo->empty_tree; /* commit is root */ |
1711 | 0 | } |
1712 | | |
1713 | 0 | return oideq(ptree_oid, get_commit_tree_oid(commit)); |
1714 | 0 | } |
1715 | | |
1716 | | /* |
1717 | | * Should empty commits be allowed? Return status: |
1718 | | * <0: Error in is_index_unchanged(r) or is_original_commit_empty(commit) |
1719 | | * 0: Halt on empty commit |
1720 | | * 1: Allow empty commit |
1721 | | * 2: Drop empty commit |
1722 | | */ |
1723 | | static int allow_empty(struct repository *r, |
1724 | | struct replay_opts *opts, |
1725 | | struct commit *commit) |
1726 | 0 | { |
1727 | 0 | int index_unchanged, originally_empty; |
1728 | | |
1729 | | /* |
1730 | | * Four cases: |
1731 | | * |
1732 | | * (1) we do not allow empty at all and error out. |
1733 | | * |
1734 | | * (2) we allow ones that were initially empty, and |
1735 | | * just drop the ones that become empty |
1736 | | * |
1737 | | * (3) we allow ones that were initially empty, but |
1738 | | * halt for the ones that become empty; |
1739 | | * |
1740 | | * (4) we allow both. |
1741 | | */ |
1742 | 0 | if (!opts->allow_empty) |
1743 | 0 | return 0; /* let "git commit" barf as necessary */ |
1744 | | |
1745 | 0 | index_unchanged = is_index_unchanged(r); |
1746 | 0 | if (index_unchanged < 0) |
1747 | 0 | return index_unchanged; |
1748 | 0 | if (!index_unchanged) |
1749 | 0 | return 0; /* we do not have to say --allow-empty */ |
1750 | | |
1751 | 0 | if (opts->keep_redundant_commits) |
1752 | 0 | return 1; |
1753 | | |
1754 | 0 | originally_empty = is_original_commit_empty(commit); |
1755 | 0 | if (originally_empty < 0) |
1756 | 0 | return originally_empty; |
1757 | 0 | if (originally_empty) |
1758 | 0 | return 1; |
1759 | 0 | else if (opts->drop_redundant_commits) |
1760 | 0 | return 2; |
1761 | 0 | else |
1762 | 0 | return 0; |
1763 | 0 | } |
1764 | | |
1765 | | static struct { |
1766 | | char c; |
1767 | | const char *str; |
1768 | | } todo_command_info[] = { |
1769 | | [TODO_PICK] = { 'p', "pick" }, |
1770 | | [TODO_REVERT] = { 0, "revert" }, |
1771 | | [TODO_EDIT] = { 'e', "edit" }, |
1772 | | [TODO_REWORD] = { 'r', "reword" }, |
1773 | | [TODO_FIXUP] = { 'f', "fixup" }, |
1774 | | [TODO_SQUASH] = { 's', "squash" }, |
1775 | | [TODO_EXEC] = { 'x', "exec" }, |
1776 | | [TODO_BREAK] = { 'b', "break" }, |
1777 | | [TODO_LABEL] = { 'l', "label" }, |
1778 | | [TODO_RESET] = { 't', "reset" }, |
1779 | | [TODO_MERGE] = { 'm', "merge" }, |
1780 | | [TODO_UPDATE_REF] = { 'u', "update-ref" }, |
1781 | | [TODO_NOOP] = { 0, "noop" }, |
1782 | | [TODO_DROP] = { 'd', "drop" }, |
1783 | | [TODO_COMMENT] = { 0, NULL }, |
1784 | | }; |
1785 | | |
1786 | | static const char *command_to_string(const enum todo_command command) |
1787 | 0 | { |
1788 | 0 | if (command < TODO_COMMENT) |
1789 | 0 | return todo_command_info[command].str; |
1790 | 0 | die(_("unknown command: %d"), command); |
1791 | 0 | } |
1792 | | |
1793 | | static char command_to_char(const enum todo_command command) |
1794 | 0 | { |
1795 | 0 | if (command < TODO_COMMENT) |
1796 | 0 | return todo_command_info[command].c; |
1797 | 0 | return comment_line_char; |
1798 | 0 | } |
1799 | | |
1800 | | static int is_noop(const enum todo_command command) |
1801 | 0 | { |
1802 | 0 | return TODO_NOOP <= command; |
1803 | 0 | } |
1804 | | |
1805 | | static int is_fixup(enum todo_command command) |
1806 | 0 | { |
1807 | 0 | return command == TODO_FIXUP || command == TODO_SQUASH; |
1808 | 0 | } |
1809 | | |
1810 | | /* Does this command create a (non-merge) commit? */ |
1811 | | static int is_pick_or_similar(enum todo_command command) |
1812 | 0 | { |
1813 | 0 | switch (command) { |
1814 | 0 | case TODO_PICK: |
1815 | 0 | case TODO_REVERT: |
1816 | 0 | case TODO_EDIT: |
1817 | 0 | case TODO_REWORD: |
1818 | 0 | case TODO_FIXUP: |
1819 | 0 | case TODO_SQUASH: |
1820 | 0 | return 1; |
1821 | 0 | default: |
1822 | 0 | return 0; |
1823 | 0 | } |
1824 | 0 | } |
1825 | | |
1826 | | enum todo_item_flags { |
1827 | | TODO_EDIT_MERGE_MSG = (1 << 0), |
1828 | | TODO_REPLACE_FIXUP_MSG = (1 << 1), |
1829 | | TODO_EDIT_FIXUP_MSG = (1 << 2), |
1830 | | }; |
1831 | | |
1832 | | static const char first_commit_msg_str[] = N_("This is the 1st commit message:"); |
1833 | | static const char nth_commit_msg_fmt[] = N_("This is the commit message #%d:"); |
1834 | | static const char skip_first_commit_msg_str[] = N_("The 1st commit message will be skipped:"); |
1835 | | static const char skip_nth_commit_msg_fmt[] = N_("The commit message #%d will be skipped:"); |
1836 | | static const char combined_commit_msg_fmt[] = N_("This is a combination of %d commits."); |
1837 | | |
1838 | | static int is_fixup_flag(enum todo_command command, unsigned flag) |
1839 | 0 | { |
1840 | 0 | return command == TODO_FIXUP && ((flag & TODO_REPLACE_FIXUP_MSG) || |
1841 | 0 | (flag & TODO_EDIT_FIXUP_MSG)); |
1842 | 0 | } |
1843 | | |
1844 | | /* |
1845 | | * Wrapper around strbuf_add_commented_lines() which avoids double |
1846 | | * commenting commit subjects. |
1847 | | */ |
1848 | | static void add_commented_lines(struct strbuf *buf, const void *str, size_t len) |
1849 | 0 | { |
1850 | 0 | const char *s = str; |
1851 | 0 | while (len > 0 && s[0] == comment_line_char) { |
1852 | 0 | size_t count; |
1853 | 0 | const char *n = memchr(s, '\n', len); |
1854 | 0 | if (!n) |
1855 | 0 | count = len; |
1856 | 0 | else |
1857 | 0 | count = n - s + 1; |
1858 | 0 | strbuf_add(buf, s, count); |
1859 | 0 | s += count; |
1860 | 0 | len -= count; |
1861 | 0 | } |
1862 | 0 | strbuf_add_commented_lines(buf, s, len, comment_line_char); |
1863 | 0 | } |
1864 | | |
1865 | | /* Does the current fixup chain contain a squash command? */ |
1866 | | static int seen_squash(struct replay_opts *opts) |
1867 | 0 | { |
1868 | 0 | return starts_with(opts->current_fixups.buf, "squash") || |
1869 | 0 | strstr(opts->current_fixups.buf, "\nsquash"); |
1870 | 0 | } |
1871 | | |
1872 | | static void update_comment_bufs(struct strbuf *buf1, struct strbuf *buf2, int n) |
1873 | 0 | { |
1874 | 0 | strbuf_setlen(buf1, 2); |
1875 | 0 | strbuf_addf(buf1, _(nth_commit_msg_fmt), n); |
1876 | 0 | strbuf_addch(buf1, '\n'); |
1877 | 0 | strbuf_setlen(buf2, 2); |
1878 | 0 | strbuf_addf(buf2, _(skip_nth_commit_msg_fmt), n); |
1879 | 0 | strbuf_addch(buf2, '\n'); |
1880 | 0 | } |
1881 | | |
1882 | | /* |
1883 | | * Comment out any un-commented commit messages, updating the message comments |
1884 | | * to say they will be skipped but do not comment out the empty lines that |
1885 | | * surround commit messages and their comments. |
1886 | | */ |
1887 | | static void update_squash_message_for_fixup(struct strbuf *msg) |
1888 | 0 | { |
1889 | 0 | void (*copy_lines)(struct strbuf *, const void *, size_t) = strbuf_add; |
1890 | 0 | struct strbuf buf1 = STRBUF_INIT, buf2 = STRBUF_INIT; |
1891 | 0 | const char *s, *start; |
1892 | 0 | char *orig_msg; |
1893 | 0 | size_t orig_msg_len; |
1894 | 0 | int i = 1; |
1895 | |
|
1896 | 0 | strbuf_addf(&buf1, "# %s\n", _(first_commit_msg_str)); |
1897 | 0 | strbuf_addf(&buf2, "# %s\n", _(skip_first_commit_msg_str)); |
1898 | 0 | s = start = orig_msg = strbuf_detach(msg, &orig_msg_len); |
1899 | 0 | while (s) { |
1900 | 0 | const char *next; |
1901 | 0 | size_t off; |
1902 | 0 | if (skip_prefix(s, buf1.buf, &next)) { |
1903 | | /* |
1904 | | * Copy the last message, preserving the blank line |
1905 | | * preceding the current line |
1906 | | */ |
1907 | 0 | off = (s > start + 1 && s[-2] == '\n') ? 1 : 0; |
1908 | 0 | copy_lines(msg, start, s - start - off); |
1909 | 0 | if (off) |
1910 | 0 | strbuf_addch(msg, '\n'); |
1911 | | /* |
1912 | | * The next message needs to be commented out but the |
1913 | | * message header is already commented out so just copy |
1914 | | * it and the blank line that follows it. |
1915 | | */ |
1916 | 0 | strbuf_addbuf(msg, &buf2); |
1917 | 0 | if (*next == '\n') |
1918 | 0 | strbuf_addch(msg, *next++); |
1919 | 0 | start = s = next; |
1920 | 0 | copy_lines = add_commented_lines; |
1921 | 0 | update_comment_bufs(&buf1, &buf2, ++i); |
1922 | 0 | } else if (skip_prefix(s, buf2.buf, &next)) { |
1923 | 0 | off = (s > start + 1 && s[-2] == '\n') ? 1 : 0; |
1924 | 0 | copy_lines(msg, start, s - start - off); |
1925 | 0 | start = s - off; |
1926 | 0 | s = next; |
1927 | 0 | copy_lines = strbuf_add; |
1928 | 0 | update_comment_bufs(&buf1, &buf2, ++i); |
1929 | 0 | } else { |
1930 | 0 | s = strchr(s, '\n'); |
1931 | 0 | if (s) |
1932 | 0 | s++; |
1933 | 0 | } |
1934 | 0 | } |
1935 | 0 | copy_lines(msg, start, orig_msg_len - (start - orig_msg)); |
1936 | 0 | free(orig_msg); |
1937 | 0 | strbuf_release(&buf1); |
1938 | 0 | strbuf_release(&buf2); |
1939 | 0 | } |
1940 | | |
1941 | | static int append_squash_message(struct strbuf *buf, const char *body, |
1942 | | enum todo_command command, struct replay_opts *opts, |
1943 | | unsigned flag) |
1944 | 0 | { |
1945 | 0 | const char *fixup_msg; |
1946 | 0 | size_t commented_len = 0, fixup_off; |
1947 | | /* |
1948 | | * amend is non-interactive and not normally used with fixup! |
1949 | | * or squash! commits, so only comment out those subjects when |
1950 | | * squashing commit messages. |
1951 | | */ |
1952 | 0 | if (starts_with(body, "amend!") || |
1953 | 0 | ((command == TODO_SQUASH || seen_squash(opts)) && |
1954 | 0 | (starts_with(body, "squash!") || starts_with(body, "fixup!")))) |
1955 | 0 | commented_len = commit_subject_length(body); |
1956 | |
|
1957 | 0 | strbuf_addf(buf, "\n%c ", comment_line_char); |
1958 | 0 | strbuf_addf(buf, _(nth_commit_msg_fmt), |
1959 | 0 | ++opts->current_fixup_count + 1); |
1960 | 0 | strbuf_addstr(buf, "\n\n"); |
1961 | 0 | strbuf_add_commented_lines(buf, body, commented_len, comment_line_char); |
1962 | | /* buf->buf may be reallocated so store an offset into the buffer */ |
1963 | 0 | fixup_off = buf->len; |
1964 | 0 | strbuf_addstr(buf, body + commented_len); |
1965 | | |
1966 | | /* fixup -C after squash behaves like squash */ |
1967 | 0 | if (is_fixup_flag(command, flag) && !seen_squash(opts)) { |
1968 | | /* |
1969 | | * We're replacing the commit message so we need to |
1970 | | * append the Signed-off-by: trailer if the user |
1971 | | * requested '--signoff'. |
1972 | | */ |
1973 | 0 | if (opts->signoff) |
1974 | 0 | append_signoff(buf, 0, 0); |
1975 | |
|
1976 | 0 | if ((command == TODO_FIXUP) && |
1977 | 0 | (flag & TODO_REPLACE_FIXUP_MSG) && |
1978 | 0 | (file_exists(rebase_path_fixup_msg()) || |
1979 | 0 | !file_exists(rebase_path_squash_msg()))) { |
1980 | 0 | fixup_msg = skip_blank_lines(buf->buf + fixup_off); |
1981 | 0 | if (write_message(fixup_msg, strlen(fixup_msg), |
1982 | 0 | rebase_path_fixup_msg(), 0) < 0) |
1983 | 0 | return error(_("cannot write '%s'"), |
1984 | 0 | rebase_path_fixup_msg()); |
1985 | 0 | } else { |
1986 | 0 | unlink(rebase_path_fixup_msg()); |
1987 | 0 | } |
1988 | 0 | } else { |
1989 | 0 | unlink(rebase_path_fixup_msg()); |
1990 | 0 | } |
1991 | | |
1992 | 0 | return 0; |
1993 | 0 | } |
1994 | | |
1995 | | static int update_squash_messages(struct repository *r, |
1996 | | enum todo_command command, |
1997 | | struct commit *commit, |
1998 | | struct replay_opts *opts, |
1999 | | unsigned flag) |
2000 | 0 | { |
2001 | 0 | struct strbuf buf = STRBUF_INIT; |
2002 | 0 | int res = 0; |
2003 | 0 | const char *message, *body; |
2004 | 0 | const char *encoding = get_commit_output_encoding(); |
2005 | |
|
2006 | 0 | if (opts->current_fixup_count > 0) { |
2007 | 0 | struct strbuf header = STRBUF_INIT; |
2008 | 0 | char *eol; |
2009 | |
|
2010 | 0 | if (strbuf_read_file(&buf, rebase_path_squash_msg(), 9) <= 0) |
2011 | 0 | return error(_("could not read '%s'"), |
2012 | 0 | rebase_path_squash_msg()); |
2013 | | |
2014 | 0 | eol = buf.buf[0] != comment_line_char ? |
2015 | 0 | buf.buf : strchrnul(buf.buf, '\n'); |
2016 | |
|
2017 | 0 | strbuf_addf(&header, "%c ", comment_line_char); |
2018 | 0 | strbuf_addf(&header, _(combined_commit_msg_fmt), |
2019 | 0 | opts->current_fixup_count + 2); |
2020 | 0 | strbuf_splice(&buf, 0, eol - buf.buf, header.buf, header.len); |
2021 | 0 | strbuf_release(&header); |
2022 | 0 | if (is_fixup_flag(command, flag) && !seen_squash(opts)) |
2023 | 0 | update_squash_message_for_fixup(&buf); |
2024 | 0 | } else { |
2025 | 0 | struct object_id head; |
2026 | 0 | struct commit *head_commit; |
2027 | 0 | const char *head_message, *body; |
2028 | |
|
2029 | 0 | if (repo_get_oid(r, "HEAD", &head)) |
2030 | 0 | return error(_("need a HEAD to fixup")); |
2031 | 0 | if (!(head_commit = lookup_commit_reference(r, &head))) |
2032 | 0 | return error(_("could not read HEAD")); |
2033 | 0 | if (!(head_message = repo_logmsg_reencode(r, head_commit, NULL, |
2034 | 0 | encoding))) |
2035 | 0 | return error(_("could not read HEAD's commit message")); |
2036 | | |
2037 | 0 | find_commit_subject(head_message, &body); |
2038 | 0 | if (command == TODO_FIXUP && !flag && write_message(body, strlen(body), |
2039 | 0 | rebase_path_fixup_msg(), 0) < 0) { |
2040 | 0 | repo_unuse_commit_buffer(r, head_commit, head_message); |
2041 | 0 | return error(_("cannot write '%s'"), rebase_path_fixup_msg()); |
2042 | 0 | } |
2043 | 0 | strbuf_addf(&buf, "%c ", comment_line_char); |
2044 | 0 | strbuf_addf(&buf, _(combined_commit_msg_fmt), 2); |
2045 | 0 | strbuf_addf(&buf, "\n%c ", comment_line_char); |
2046 | 0 | strbuf_addstr(&buf, is_fixup_flag(command, flag) ? |
2047 | 0 | _(skip_first_commit_msg_str) : |
2048 | 0 | _(first_commit_msg_str)); |
2049 | 0 | strbuf_addstr(&buf, "\n\n"); |
2050 | 0 | if (is_fixup_flag(command, flag)) |
2051 | 0 | strbuf_add_commented_lines(&buf, body, strlen(body), |
2052 | 0 | comment_line_char); |
2053 | 0 | else |
2054 | 0 | strbuf_addstr(&buf, body); |
2055 | |
|
2056 | 0 | repo_unuse_commit_buffer(r, head_commit, head_message); |
2057 | 0 | } |
2058 | | |
2059 | 0 | if (!(message = repo_logmsg_reencode(r, commit, NULL, encoding))) |
2060 | 0 | return error(_("could not read commit message of %s"), |
2061 | 0 | oid_to_hex(&commit->object.oid)); |
2062 | 0 | find_commit_subject(message, &body); |
2063 | |
|
2064 | 0 | if (command == TODO_SQUASH || is_fixup_flag(command, flag)) { |
2065 | 0 | res = append_squash_message(&buf, body, command, opts, flag); |
2066 | 0 | } else if (command == TODO_FIXUP) { |
2067 | 0 | strbuf_addf(&buf, "\n%c ", comment_line_char); |
2068 | 0 | strbuf_addf(&buf, _(skip_nth_commit_msg_fmt), |
2069 | 0 | ++opts->current_fixup_count + 1); |
2070 | 0 | strbuf_addstr(&buf, "\n\n"); |
2071 | 0 | strbuf_add_commented_lines(&buf, body, strlen(body), |
2072 | 0 | comment_line_char); |
2073 | 0 | } else |
2074 | 0 | return error(_("unknown command: %d"), command); |
2075 | 0 | repo_unuse_commit_buffer(r, commit, message); |
2076 | |
|
2077 | 0 | if (!res) |
2078 | 0 | res = write_message(buf.buf, buf.len, rebase_path_squash_msg(), |
2079 | 0 | 0); |
2080 | 0 | strbuf_release(&buf); |
2081 | |
|
2082 | 0 | if (!res) { |
2083 | 0 | strbuf_addf(&opts->current_fixups, "%s%s %s", |
2084 | 0 | opts->current_fixups.len ? "\n" : "", |
2085 | 0 | command_to_string(command), |
2086 | 0 | oid_to_hex(&commit->object.oid)); |
2087 | 0 | res = write_message(opts->current_fixups.buf, |
2088 | 0 | opts->current_fixups.len, |
2089 | 0 | rebase_path_current_fixups(), 0); |
2090 | 0 | } |
2091 | |
|
2092 | 0 | return res; |
2093 | 0 | } |
2094 | | |
2095 | | static void flush_rewritten_pending(void) |
2096 | 0 | { |
2097 | 0 | struct strbuf buf = STRBUF_INIT; |
2098 | 0 | struct object_id newoid; |
2099 | 0 | FILE *out; |
2100 | |
|
2101 | 0 | if (strbuf_read_file(&buf, rebase_path_rewritten_pending(), (GIT_MAX_HEXSZ + 1) * 2) > 0 && |
2102 | 0 | !repo_get_oid(the_repository, "HEAD", &newoid) && |
2103 | 0 | (out = fopen_or_warn(rebase_path_rewritten_list(), "a"))) { |
2104 | 0 | char *bol = buf.buf, *eol; |
2105 | |
|
2106 | 0 | while (*bol) { |
2107 | 0 | eol = strchrnul(bol, '\n'); |
2108 | 0 | fprintf(out, "%.*s %s\n", (int)(eol - bol), |
2109 | 0 | bol, oid_to_hex(&newoid)); |
2110 | 0 | if (!*eol) |
2111 | 0 | break; |
2112 | 0 | bol = eol + 1; |
2113 | 0 | } |
2114 | 0 | fclose(out); |
2115 | 0 | unlink(rebase_path_rewritten_pending()); |
2116 | 0 | } |
2117 | 0 | strbuf_release(&buf); |
2118 | 0 | } |
2119 | | |
2120 | | static void record_in_rewritten(struct object_id *oid, |
2121 | | enum todo_command next_command) |
2122 | 0 | { |
2123 | 0 | FILE *out = fopen_or_warn(rebase_path_rewritten_pending(), "a"); |
2124 | |
|
2125 | 0 | if (!out) |
2126 | 0 | return; |
2127 | | |
2128 | 0 | fprintf(out, "%s\n", oid_to_hex(oid)); |
2129 | 0 | fclose(out); |
2130 | |
|
2131 | 0 | if (!is_fixup(next_command)) |
2132 | 0 | flush_rewritten_pending(); |
2133 | 0 | } |
2134 | | |
2135 | 0 | static int should_edit(struct replay_opts *opts) { |
2136 | 0 | if (opts->edit < 0) |
2137 | | /* |
2138 | | * Note that we only handle the case of non-conflicted |
2139 | | * commits; continue_single_pick() handles the conflicted |
2140 | | * commits itself instead of calling this function. |
2141 | | */ |
2142 | 0 | return (opts->action == REPLAY_REVERT && isatty(0)) ? 1 : 0; |
2143 | 0 | return opts->edit; |
2144 | 0 | } |
2145 | | |
2146 | | static void refer_to_commit(struct replay_opts *opts, |
2147 | | struct strbuf *msgbuf, struct commit *commit) |
2148 | 0 | { |
2149 | 0 | if (opts->commit_use_reference) { |
2150 | 0 | struct pretty_print_context ctx = { |
2151 | 0 | .abbrev = DEFAULT_ABBREV, |
2152 | 0 | .date_mode.type = DATE_SHORT, |
2153 | 0 | }; |
2154 | 0 | repo_format_commit_message(the_repository, commit, |
2155 | 0 | "%h (%s, %ad)", msgbuf, &ctx); |
2156 | 0 | } else { |
2157 | 0 | strbuf_addstr(msgbuf, oid_to_hex(&commit->object.oid)); |
2158 | 0 | } |
2159 | 0 | } |
2160 | | |
2161 | | static int do_pick_commit(struct repository *r, |
2162 | | struct todo_item *item, |
2163 | | struct replay_opts *opts, |
2164 | | int final_fixup, int *check_todo) |
2165 | 0 | { |
2166 | 0 | unsigned int flags = should_edit(opts) ? EDIT_MSG : 0; |
2167 | 0 | const char *msg_file = should_edit(opts) ? NULL : git_path_merge_msg(r); |
2168 | 0 | struct object_id head; |
2169 | 0 | struct commit *base, *next, *parent; |
2170 | 0 | const char *base_label, *next_label; |
2171 | 0 | char *author = NULL; |
2172 | 0 | struct commit_message msg = { NULL, NULL, NULL, NULL }; |
2173 | 0 | struct strbuf msgbuf = STRBUF_INIT; |
2174 | 0 | int res, unborn = 0, reword = 0, allow, drop_commit; |
2175 | 0 | enum todo_command command = item->command; |
2176 | 0 | struct commit *commit = item->commit; |
2177 | |
|
2178 | 0 | if (opts->no_commit) { |
2179 | | /* |
2180 | | * We do not intend to commit immediately. We just want to |
2181 | | * merge the differences in, so let's compute the tree |
2182 | | * that represents the "current" state for the merge machinery |
2183 | | * to work on. |
2184 | | */ |
2185 | 0 | if (write_index_as_tree(&head, r->index, r->index_file, 0, NULL)) |
2186 | 0 | return error(_("your index file is unmerged.")); |
2187 | 0 | } else { |
2188 | 0 | unborn = repo_get_oid(r, "HEAD", &head); |
2189 | | /* Do we want to generate a root commit? */ |
2190 | 0 | if (is_pick_or_similar(command) && opts->have_squash_onto && |
2191 | 0 | oideq(&head, &opts->squash_onto)) { |
2192 | 0 | if (is_fixup(command)) |
2193 | 0 | return error(_("cannot fixup root commit")); |
2194 | 0 | flags |= CREATE_ROOT_COMMIT; |
2195 | 0 | unborn = 1; |
2196 | 0 | } else if (unborn) |
2197 | 0 | oidcpy(&head, the_hash_algo->empty_tree); |
2198 | 0 | if (index_differs_from(r, unborn ? empty_tree_oid_hex() : "HEAD", |
2199 | 0 | NULL, 0)) |
2200 | 0 | return error_dirty_index(r, opts); |
2201 | 0 | } |
2202 | 0 | discard_index(r->index); |
2203 | |
|
2204 | 0 | if (!commit->parents) |
2205 | 0 | parent = NULL; |
2206 | 0 | else if (commit->parents->next) { |
2207 | | /* Reverting or cherry-picking a merge commit */ |
2208 | 0 | int cnt; |
2209 | 0 | struct commit_list *p; |
2210 | |
|
2211 | 0 | if (!opts->mainline) |
2212 | 0 | return error(_("commit %s is a merge but no -m option was given."), |
2213 | 0 | oid_to_hex(&commit->object.oid)); |
2214 | | |
2215 | 0 | for (cnt = 1, p = commit->parents; |
2216 | 0 | cnt != opts->mainline && p; |
2217 | 0 | cnt++) |
2218 | 0 | p = p->next; |
2219 | 0 | if (cnt != opts->mainline || !p) |
2220 | 0 | return error(_("commit %s does not have parent %d"), |
2221 | 0 | oid_to_hex(&commit->object.oid), opts->mainline); |
2222 | 0 | parent = p->item; |
2223 | 0 | } else if (1 < opts->mainline) |
2224 | | /* |
2225 | | * Non-first parent explicitly specified as mainline for |
2226 | | * non-merge commit |
2227 | | */ |
2228 | 0 | return error(_("commit %s does not have parent %d"), |
2229 | 0 | oid_to_hex(&commit->object.oid), opts->mainline); |
2230 | 0 | else |
2231 | 0 | parent = commit->parents->item; |
2232 | | |
2233 | 0 | if (get_message(commit, &msg) != 0) |
2234 | 0 | return error(_("cannot get commit message for %s"), |
2235 | 0 | oid_to_hex(&commit->object.oid)); |
2236 | | |
2237 | 0 | if (opts->allow_ff && !is_fixup(command) && |
2238 | 0 | ((parent && oideq(&parent->object.oid, &head)) || |
2239 | 0 | (!parent && unborn))) { |
2240 | 0 | if (is_rebase_i(opts)) |
2241 | 0 | write_author_script(msg.message); |
2242 | 0 | res = fast_forward_to(r, &commit->object.oid, &head, unborn, |
2243 | 0 | opts); |
2244 | 0 | if (res || command != TODO_REWORD) |
2245 | 0 | goto leave; |
2246 | 0 | reword = 1; |
2247 | 0 | msg_file = NULL; |
2248 | 0 | goto fast_forward_edit; |
2249 | 0 | } |
2250 | 0 | if (parent && repo_parse_commit(r, parent) < 0) |
2251 | | /* TRANSLATORS: The first %s will be a "todo" command like |
2252 | | "revert" or "pick", the second %s a SHA1. */ |
2253 | 0 | return error(_("%s: cannot parse parent commit %s"), |
2254 | 0 | command_to_string(command), |
2255 | 0 | oid_to_hex(&parent->object.oid)); |
2256 | | |
2257 | | /* |
2258 | | * "commit" is an existing commit. We would want to apply |
2259 | | * the difference it introduces since its first parent "prev" |
2260 | | * on top of the current HEAD if we are cherry-pick. Or the |
2261 | | * reverse of it if we are revert. |
2262 | | */ |
2263 | | |
2264 | 0 | if (command == TODO_REVERT) { |
2265 | 0 | const char *orig_subject; |
2266 | |
|
2267 | 0 | base = commit; |
2268 | 0 | base_label = msg.label; |
2269 | 0 | next = parent; |
2270 | 0 | next_label = msg.parent_label; |
2271 | 0 | if (opts->commit_use_reference) { |
2272 | 0 | strbuf_addstr(&msgbuf, |
2273 | 0 | "# *** SAY WHY WE ARE REVERTING ON THE TITLE LINE ***"); |
2274 | 0 | } else if (skip_prefix(msg.subject, "Revert \"", &orig_subject) && |
2275 | | /* |
2276 | | * We don't touch pre-existing repeated reverts, because |
2277 | | * theoretically these can be nested arbitrarily deeply, |
2278 | | * thus requiring excessive complexity to deal with. |
2279 | | */ |
2280 | 0 | !starts_with(orig_subject, "Revert \"")) { |
2281 | 0 | strbuf_addstr(&msgbuf, "Reapply \""); |
2282 | 0 | strbuf_addstr(&msgbuf, orig_subject); |
2283 | 0 | } else { |
2284 | 0 | strbuf_addstr(&msgbuf, "Revert \""); |
2285 | 0 | strbuf_addstr(&msgbuf, msg.subject); |
2286 | 0 | strbuf_addstr(&msgbuf, "\""); |
2287 | 0 | } |
2288 | 0 | strbuf_addstr(&msgbuf, "\n\nThis reverts commit "); |
2289 | 0 | refer_to_commit(opts, &msgbuf, commit); |
2290 | |
|
2291 | 0 | if (commit->parents && commit->parents->next) { |
2292 | 0 | strbuf_addstr(&msgbuf, ", reversing\nchanges made to "); |
2293 | 0 | refer_to_commit(opts, &msgbuf, parent); |
2294 | 0 | } |
2295 | 0 | strbuf_addstr(&msgbuf, ".\n"); |
2296 | 0 | } else { |
2297 | 0 | const char *p; |
2298 | |
|
2299 | 0 | base = parent; |
2300 | 0 | base_label = msg.parent_label; |
2301 | 0 | next = commit; |
2302 | 0 | next_label = msg.label; |
2303 | | |
2304 | | /* Append the commit log message to msgbuf. */ |
2305 | 0 | if (find_commit_subject(msg.message, &p)) |
2306 | 0 | strbuf_addstr(&msgbuf, p); |
2307 | |
|
2308 | 0 | if (opts->record_origin) { |
2309 | 0 | strbuf_complete_line(&msgbuf); |
2310 | 0 | if (!has_conforming_footer(&msgbuf, NULL, 0)) |
2311 | 0 | strbuf_addch(&msgbuf, '\n'); |
2312 | 0 | strbuf_addstr(&msgbuf, cherry_picked_prefix); |
2313 | 0 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
2314 | 0 | strbuf_addstr(&msgbuf, ")\n"); |
2315 | 0 | } |
2316 | 0 | if (!is_fixup(command)) |
2317 | 0 | author = get_author(msg.message); |
2318 | 0 | } |
2319 | |
|
2320 | 0 | if (command == TODO_REWORD) |
2321 | 0 | reword = 1; |
2322 | 0 | else if (is_fixup(command)) { |
2323 | 0 | if (update_squash_messages(r, command, commit, |
2324 | 0 | opts, item->flags)) { |
2325 | 0 | res = -1; |
2326 | 0 | goto leave; |
2327 | 0 | } |
2328 | 0 | flags |= AMEND_MSG; |
2329 | 0 | if (!final_fixup) |
2330 | 0 | msg_file = rebase_path_squash_msg(); |
2331 | 0 | else if (file_exists(rebase_path_fixup_msg())) { |
2332 | 0 | flags |= VERBATIM_MSG; |
2333 | 0 | msg_file = rebase_path_fixup_msg(); |
2334 | 0 | } else { |
2335 | 0 | const char *dest = git_path_squash_msg(r); |
2336 | 0 | unlink(dest); |
2337 | 0 | if (copy_file(dest, rebase_path_squash_msg(), 0666)) { |
2338 | 0 | res = error(_("could not copy '%s' to '%s'"), |
2339 | 0 | rebase_path_squash_msg(), dest); |
2340 | 0 | goto leave; |
2341 | 0 | } |
2342 | 0 | unlink(git_path_merge_msg(r)); |
2343 | 0 | msg_file = dest; |
2344 | 0 | flags |= EDIT_MSG; |
2345 | 0 | } |
2346 | 0 | } |
2347 | | |
2348 | 0 | if (opts->signoff && !is_fixup(command)) |
2349 | 0 | append_signoff(&msgbuf, 0, 0); |
2350 | |
|
2351 | 0 | if (is_rebase_i(opts) && write_author_script(msg.message) < 0) |
2352 | 0 | res = -1; |
2353 | 0 | else if (!opts->strategy || |
2354 | 0 | !strcmp(opts->strategy, "recursive") || |
2355 | 0 | !strcmp(opts->strategy, "ort") || |
2356 | 0 | command == TODO_REVERT) { |
2357 | 0 | res = do_recursive_merge(r, base, next, base_label, next_label, |
2358 | 0 | &head, &msgbuf, opts); |
2359 | 0 | if (res < 0) |
2360 | 0 | goto leave; |
2361 | | |
2362 | 0 | res |= write_message(msgbuf.buf, msgbuf.len, |
2363 | 0 | git_path_merge_msg(r), 0); |
2364 | 0 | } else { |
2365 | 0 | struct commit_list *common = NULL; |
2366 | 0 | struct commit_list *remotes = NULL; |
2367 | |
|
2368 | 0 | res = write_message(msgbuf.buf, msgbuf.len, |
2369 | 0 | git_path_merge_msg(r), 0); |
2370 | |
|
2371 | 0 | commit_list_insert(base, &common); |
2372 | 0 | commit_list_insert(next, &remotes); |
2373 | 0 | res |= try_merge_command(r, opts->strategy, |
2374 | 0 | opts->xopts.nr, opts->xopts.v, |
2375 | 0 | common, oid_to_hex(&head), remotes); |
2376 | 0 | free_commit_list(common); |
2377 | 0 | free_commit_list(remotes); |
2378 | 0 | } |
2379 | | |
2380 | | /* |
2381 | | * If the merge was clean or if it failed due to conflict, we write |
2382 | | * CHERRY_PICK_HEAD for the subsequent invocation of commit to use. |
2383 | | * However, if the merge did not even start, then we don't want to |
2384 | | * write it at all. |
2385 | | */ |
2386 | 0 | if ((command == TODO_PICK || command == TODO_REWORD || |
2387 | 0 | command == TODO_EDIT) && !opts->no_commit && |
2388 | 0 | (res == 0 || res == 1) && |
2389 | 0 | update_ref(NULL, "CHERRY_PICK_HEAD", &commit->object.oid, NULL, |
2390 | 0 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) |
2391 | 0 | res = -1; |
2392 | 0 | if (command == TODO_REVERT && ((opts->no_commit && res == 0) || res == 1) && |
2393 | 0 | update_ref(NULL, "REVERT_HEAD", &commit->object.oid, NULL, |
2394 | 0 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) |
2395 | 0 | res = -1; |
2396 | |
|
2397 | 0 | if (res) { |
2398 | 0 | error(command == TODO_REVERT |
2399 | 0 | ? _("could not revert %s... %s") |
2400 | 0 | : _("could not apply %s... %s"), |
2401 | 0 | short_commit_name(r, commit), msg.subject); |
2402 | 0 | print_advice(r, res == 1, opts); |
2403 | 0 | repo_rerere(r, opts->allow_rerere_auto); |
2404 | 0 | goto leave; |
2405 | 0 | } |
2406 | | |
2407 | 0 | drop_commit = 0; |
2408 | 0 | allow = allow_empty(r, opts, commit); |
2409 | 0 | if (allow < 0) { |
2410 | 0 | res = allow; |
2411 | 0 | goto leave; |
2412 | 0 | } else if (allow == 1) { |
2413 | 0 | flags |= ALLOW_EMPTY; |
2414 | 0 | } else if (allow == 2) { |
2415 | 0 | drop_commit = 1; |
2416 | 0 | refs_delete_ref(get_main_ref_store(r), "", "CHERRY_PICK_HEAD", |
2417 | 0 | NULL, 0); |
2418 | 0 | unlink(git_path_merge_msg(r)); |
2419 | 0 | unlink(git_path_auto_merge(r)); |
2420 | 0 | fprintf(stderr, |
2421 | 0 | _("dropping %s %s -- patch contents already upstream\n"), |
2422 | 0 | oid_to_hex(&commit->object.oid), msg.subject); |
2423 | 0 | } /* else allow == 0 and there's nothing special to do */ |
2424 | 0 | if (!opts->no_commit && !drop_commit) { |
2425 | 0 | if (author || command == TODO_REVERT || (flags & AMEND_MSG)) |
2426 | 0 | res = do_commit(r, msg_file, author, opts, flags, |
2427 | 0 | commit? &commit->object.oid : NULL); |
2428 | 0 | else |
2429 | 0 | res = error(_("unable to parse commit author")); |
2430 | 0 | *check_todo = !!(flags & EDIT_MSG); |
2431 | 0 | if (!res && reword) { |
2432 | 0 | fast_forward_edit: |
2433 | 0 | res = run_git_commit(NULL, opts, EDIT_MSG | |
2434 | 0 | VERIFY_MSG | AMEND_MSG | |
2435 | 0 | (flags & ALLOW_EMPTY)); |
2436 | 0 | *check_todo = 1; |
2437 | 0 | } |
2438 | 0 | } |
2439 | | |
2440 | |
|
2441 | 0 | if (!res && final_fixup) { |
2442 | 0 | unlink(rebase_path_fixup_msg()); |
2443 | 0 | unlink(rebase_path_squash_msg()); |
2444 | 0 | unlink(rebase_path_current_fixups()); |
2445 | 0 | strbuf_reset(&opts->current_fixups); |
2446 | 0 | opts->current_fixup_count = 0; |
2447 | 0 | } |
2448 | |
|
2449 | 0 | leave: |
2450 | 0 | free_message(commit, &msg); |
2451 | 0 | free(author); |
2452 | 0 | strbuf_release(&msgbuf); |
2453 | 0 | update_abort_safety_file(); |
2454 | |
|
2455 | 0 | return res; |
2456 | 0 | } |
2457 | | |
2458 | | static int prepare_revs(struct replay_opts *opts) |
2459 | 0 | { |
2460 | | /* |
2461 | | * picking (but not reverting) ranges (but not individual revisions) |
2462 | | * should be done in reverse |
2463 | | */ |
2464 | 0 | if (opts->action == REPLAY_PICK && !opts->revs->no_walk) |
2465 | 0 | opts->revs->reverse ^= 1; |
2466 | |
|
2467 | 0 | if (prepare_revision_walk(opts->revs)) |
2468 | 0 | return error(_("revision walk setup failed")); |
2469 | | |
2470 | 0 | return 0; |
2471 | 0 | } |
2472 | | |
2473 | | static int read_and_refresh_cache(struct repository *r, |
2474 | | struct replay_opts *opts) |
2475 | 0 | { |
2476 | 0 | struct lock_file index_lock = LOCK_INIT; |
2477 | 0 | int index_fd = repo_hold_locked_index(r, &index_lock, 0); |
2478 | 0 | if (repo_read_index(r) < 0) { |
2479 | 0 | rollback_lock_file(&index_lock); |
2480 | 0 | return error(_("git %s: failed to read the index"), |
2481 | 0 | action_name(opts)); |
2482 | 0 | } |
2483 | 0 | refresh_index(r->index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL); |
2484 | |
|
2485 | 0 | if (index_fd >= 0) { |
2486 | 0 | if (write_locked_index(r->index, &index_lock, |
2487 | 0 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) { |
2488 | 0 | return error(_("git %s: failed to refresh the index"), |
2489 | 0 | action_name(opts)); |
2490 | 0 | } |
2491 | 0 | } |
2492 | | |
2493 | | /* |
2494 | | * If we are resolving merges in any way other than "ort", then |
2495 | | * expand the sparse index. |
2496 | | */ |
2497 | 0 | if (opts->strategy && strcmp(opts->strategy, "ort")) |
2498 | 0 | ensure_full_index(r->index); |
2499 | 0 | return 0; |
2500 | 0 | } |
2501 | | |
2502 | | void todo_list_release(struct todo_list *todo_list) |
2503 | 0 | { |
2504 | 0 | strbuf_release(&todo_list->buf); |
2505 | 0 | FREE_AND_NULL(todo_list->items); |
2506 | 0 | todo_list->nr = todo_list->alloc = 0; |
2507 | 0 | } |
2508 | | |
2509 | | static struct todo_item *append_new_todo(struct todo_list *todo_list) |
2510 | 0 | { |
2511 | 0 | ALLOC_GROW(todo_list->items, todo_list->nr + 1, todo_list->alloc); |
2512 | 0 | return todo_list->items + todo_list->nr++; |
2513 | 0 | } |
2514 | | |
2515 | | const char *todo_item_get_arg(struct todo_list *todo_list, |
2516 | | struct todo_item *item) |
2517 | 0 | { |
2518 | 0 | return todo_list->buf.buf + item->arg_offset; |
2519 | 0 | } |
2520 | | |
2521 | | static int is_command(enum todo_command command, const char **bol) |
2522 | 0 | { |
2523 | 0 | const char *str = todo_command_info[command].str; |
2524 | 0 | const char nick = todo_command_info[command].c; |
2525 | 0 | const char *p = *bol; |
2526 | |
|
2527 | 0 | return (skip_prefix(p, str, &p) || (nick && *p++ == nick)) && |
2528 | 0 | (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || !*p) && |
2529 | 0 | (*bol = p); |
2530 | 0 | } |
2531 | | |
2532 | | static int check_label_or_ref_arg(enum todo_command command, const char *arg) |
2533 | 0 | { |
2534 | 0 | switch (command) { |
2535 | 0 | case TODO_LABEL: |
2536 | | /* |
2537 | | * '#' is not a valid label as the merge command uses it to |
2538 | | * separate merge parents from the commit subject. |
2539 | | */ |
2540 | 0 | if (!strcmp(arg, "#") || |
2541 | 0 | check_refname_format(arg, REFNAME_ALLOW_ONELEVEL)) |
2542 | 0 | return error(_("'%s' is not a valid label"), arg); |
2543 | 0 | break; |
2544 | | |
2545 | 0 | case TODO_UPDATE_REF: |
2546 | 0 | if (check_refname_format(arg, REFNAME_ALLOW_ONELEVEL)) |
2547 | 0 | return error(_("'%s' is not a valid refname"), arg); |
2548 | 0 | if (check_refname_format(arg, 0)) |
2549 | 0 | return error(_("update-ref requires a fully qualified " |
2550 | 0 | "refname e.g. refs/heads/%s"), arg); |
2551 | 0 | break; |
2552 | | |
2553 | 0 | default: |
2554 | 0 | BUG("unexpected todo_command"); |
2555 | 0 | } |
2556 | | |
2557 | 0 | return 0; |
2558 | 0 | } |
2559 | | |
2560 | | static int parse_insn_line(struct repository *r, struct todo_item *item, |
2561 | | const char *buf, const char *bol, char *eol) |
2562 | 0 | { |
2563 | 0 | struct object_id commit_oid; |
2564 | 0 | char *end_of_object_name; |
2565 | 0 | int i, saved, status, padding; |
2566 | |
|
2567 | 0 | item->flags = 0; |
2568 | | |
2569 | | /* left-trim */ |
2570 | 0 | bol += strspn(bol, " \t"); |
2571 | |
|
2572 | 0 | if (bol == eol || *bol == '\r' || *bol == comment_line_char) { |
2573 | 0 | item->command = TODO_COMMENT; |
2574 | 0 | item->commit = NULL; |
2575 | 0 | item->arg_offset = bol - buf; |
2576 | 0 | item->arg_len = eol - bol; |
2577 | 0 | return 0; |
2578 | 0 | } |
2579 | | |
2580 | 0 | for (i = 0; i < TODO_COMMENT; i++) |
2581 | 0 | if (is_command(i, &bol)) { |
2582 | 0 | item->command = i; |
2583 | 0 | break; |
2584 | 0 | } |
2585 | 0 | if (i >= TODO_COMMENT) |
2586 | 0 | return error(_("invalid command '%.*s'"), |
2587 | 0 | (int)strcspn(bol, " \t\r\n"), bol); |
2588 | | |
2589 | | /* Eat up extra spaces/ tabs before object name */ |
2590 | 0 | padding = strspn(bol, " \t"); |
2591 | 0 | bol += padding; |
2592 | |
|
2593 | 0 | if (item->command == TODO_NOOP || item->command == TODO_BREAK) { |
2594 | 0 | if (bol != eol) |
2595 | 0 | return error(_("%s does not accept arguments: '%s'"), |
2596 | 0 | command_to_string(item->command), bol); |
2597 | 0 | item->commit = NULL; |
2598 | 0 | item->arg_offset = bol - buf; |
2599 | 0 | item->arg_len = eol - bol; |
2600 | 0 | return 0; |
2601 | 0 | } |
2602 | | |
2603 | 0 | if (!padding) |
2604 | 0 | return error(_("missing arguments for %s"), |
2605 | 0 | command_to_string(item->command)); |
2606 | | |
2607 | 0 | if (item->command == TODO_EXEC || item->command == TODO_LABEL || |
2608 | 0 | item->command == TODO_RESET || item->command == TODO_UPDATE_REF) { |
2609 | 0 | int ret = 0; |
2610 | |
|
2611 | 0 | item->commit = NULL; |
2612 | 0 | item->arg_offset = bol - buf; |
2613 | 0 | item->arg_len = (int)(eol - bol); |
2614 | 0 | if (item->command == TODO_LABEL || |
2615 | 0 | item->command == TODO_UPDATE_REF) { |
2616 | 0 | saved = *eol; |
2617 | 0 | *eol = '\0'; |
2618 | 0 | ret = check_label_or_ref_arg(item->command, bol); |
2619 | 0 | *eol = saved; |
2620 | 0 | } |
2621 | 0 | return ret; |
2622 | 0 | } |
2623 | | |
2624 | 0 | if (item->command == TODO_FIXUP) { |
2625 | 0 | if (skip_prefix(bol, "-C", &bol)) { |
2626 | 0 | bol += strspn(bol, " \t"); |
2627 | 0 | item->flags |= TODO_REPLACE_FIXUP_MSG; |
2628 | 0 | } else if (skip_prefix(bol, "-c", &bol)) { |
2629 | 0 | bol += strspn(bol, " \t"); |
2630 | 0 | item->flags |= TODO_EDIT_FIXUP_MSG; |
2631 | 0 | } |
2632 | 0 | } |
2633 | |
|
2634 | 0 | if (item->command == TODO_MERGE) { |
2635 | 0 | if (skip_prefix(bol, "-C", &bol)) |
2636 | 0 | bol += strspn(bol, " \t"); |
2637 | 0 | else if (skip_prefix(bol, "-c", &bol)) { |
2638 | 0 | bol += strspn(bol, " \t"); |
2639 | 0 | item->flags |= TODO_EDIT_MERGE_MSG; |
2640 | 0 | } else { |
2641 | 0 | item->flags |= TODO_EDIT_MERGE_MSG; |
2642 | 0 | item->commit = NULL; |
2643 | 0 | item->arg_offset = bol - buf; |
2644 | 0 | item->arg_len = (int)(eol - bol); |
2645 | 0 | return 0; |
2646 | 0 | } |
2647 | 0 | } |
2648 | | |
2649 | 0 | end_of_object_name = (char *) bol + strcspn(bol, " \t\n"); |
2650 | 0 | saved = *end_of_object_name; |
2651 | 0 | *end_of_object_name = '\0'; |
2652 | 0 | status = repo_get_oid(r, bol, &commit_oid); |
2653 | 0 | if (status < 0) |
2654 | 0 | error(_("could not parse '%s'"), bol); /* return later */ |
2655 | 0 | *end_of_object_name = saved; |
2656 | |
|
2657 | 0 | bol = end_of_object_name + strspn(end_of_object_name, " \t"); |
2658 | 0 | item->arg_offset = bol - buf; |
2659 | 0 | item->arg_len = (int)(eol - bol); |
2660 | |
|
2661 | 0 | if (status < 0) |
2662 | 0 | return status; |
2663 | | |
2664 | 0 | item->commit = lookup_commit_reference(r, &commit_oid); |
2665 | 0 | return item->commit ? 0 : -1; |
2666 | 0 | } |
2667 | | |
2668 | | int sequencer_get_last_command(struct repository *r UNUSED, enum replay_action *action) |
2669 | 99 | { |
2670 | 99 | const char *todo_file, *bol; |
2671 | 99 | struct strbuf buf = STRBUF_INIT; |
2672 | 99 | int ret = 0; |
2673 | | |
2674 | 99 | todo_file = git_path_todo_file(); |
2675 | 99 | if (strbuf_read_file(&buf, todo_file, 0) < 0) { |
2676 | 99 | if (errno == ENOENT || errno == ENOTDIR) |
2677 | 99 | return -1; |
2678 | 0 | else |
2679 | 0 | return error_errno("unable to open '%s'", todo_file); |
2680 | 99 | } |
2681 | 0 | bol = buf.buf + strspn(buf.buf, " \t\r\n"); |
2682 | 0 | if (is_command(TODO_PICK, &bol) && (*bol == ' ' || *bol == '\t')) |
2683 | 0 | *action = REPLAY_PICK; |
2684 | 0 | else if (is_command(TODO_REVERT, &bol) && |
2685 | 0 | (*bol == ' ' || *bol == '\t')) |
2686 | 0 | *action = REPLAY_REVERT; |
2687 | 0 | else |
2688 | 0 | ret = -1; |
2689 | |
|
2690 | 0 | strbuf_release(&buf); |
2691 | |
|
2692 | 0 | return ret; |
2693 | 99 | } |
2694 | | |
2695 | | int todo_list_parse_insn_buffer(struct repository *r, char *buf, |
2696 | | struct todo_list *todo_list) |
2697 | 0 | { |
2698 | 0 | struct todo_item *item; |
2699 | 0 | char *p = buf, *next_p; |
2700 | 0 | int i, res = 0, fixup_okay = file_exists(rebase_path_done()); |
2701 | |
|
2702 | 0 | todo_list->current = todo_list->nr = todo_list->total_nr = 0; |
2703 | |
|
2704 | 0 | for (i = 1; *p; i++, p = next_p) { |
2705 | 0 | char *eol = strchrnul(p, '\n'); |
2706 | |
|
2707 | 0 | next_p = *eol ? eol + 1 /* skip LF */ : eol; |
2708 | |
|
2709 | 0 | if (p != eol && eol[-1] == '\r') |
2710 | 0 | eol--; /* strip Carriage Return */ |
2711 | |
|
2712 | 0 | item = append_new_todo(todo_list); |
2713 | 0 | item->offset_in_buf = p - todo_list->buf.buf; |
2714 | 0 | if (parse_insn_line(r, item, buf, p, eol)) { |
2715 | 0 | res = error(_("invalid line %d: %.*s"), |
2716 | 0 | i, (int)(eol - p), p); |
2717 | 0 | item->command = TODO_COMMENT + 1; |
2718 | 0 | item->arg_offset = p - buf; |
2719 | 0 | item->arg_len = (int)(eol - p); |
2720 | 0 | item->commit = NULL; |
2721 | 0 | } |
2722 | |
|
2723 | 0 | if (item->command != TODO_COMMENT) |
2724 | 0 | todo_list->total_nr++; |
2725 | |
|
2726 | 0 | if (fixup_okay) |
2727 | 0 | ; /* do nothing */ |
2728 | 0 | else if (is_fixup(item->command)) |
2729 | 0 | res = error(_("cannot '%s' without a previous commit"), |
2730 | 0 | command_to_string(item->command)); |
2731 | 0 | else if (!is_noop(item->command)) |
2732 | 0 | fixup_okay = 1; |
2733 | 0 | } |
2734 | |
|
2735 | 0 | return res; |
2736 | 0 | } |
2737 | | |
2738 | | static int count_commands(struct todo_list *todo_list) |
2739 | 0 | { |
2740 | 0 | int count = 0, i; |
2741 | |
|
2742 | 0 | for (i = 0; i < todo_list->nr; i++) |
2743 | 0 | if (todo_list->items[i].command != TODO_COMMENT) |
2744 | 0 | count++; |
2745 | |
|
2746 | 0 | return count; |
2747 | 0 | } |
2748 | | |
2749 | | static int get_item_line_offset(struct todo_list *todo_list, int index) |
2750 | 0 | { |
2751 | 0 | return index < todo_list->nr ? |
2752 | 0 | todo_list->items[index].offset_in_buf : todo_list->buf.len; |
2753 | 0 | } |
2754 | | |
2755 | | static const char *get_item_line(struct todo_list *todo_list, int index) |
2756 | 0 | { |
2757 | 0 | return todo_list->buf.buf + get_item_line_offset(todo_list, index); |
2758 | 0 | } |
2759 | | |
2760 | | static int get_item_line_length(struct todo_list *todo_list, int index) |
2761 | 0 | { |
2762 | 0 | return get_item_line_offset(todo_list, index + 1) |
2763 | 0 | - get_item_line_offset(todo_list, index); |
2764 | 0 | } |
2765 | | |
2766 | | static ssize_t strbuf_read_file_or_whine(struct strbuf *sb, const char *path) |
2767 | 0 | { |
2768 | 0 | int fd; |
2769 | 0 | ssize_t len; |
2770 | |
|
2771 | 0 | fd = open(path, O_RDONLY); |
2772 | 0 | if (fd < 0) |
2773 | 0 | return error_errno(_("could not open '%s'"), path); |
2774 | 0 | len = strbuf_read(sb, fd, 0); |
2775 | 0 | close(fd); |
2776 | 0 | if (len < 0) |
2777 | 0 | return error(_("could not read '%s'."), path); |
2778 | 0 | return len; |
2779 | 0 | } |
2780 | | |
2781 | | static int have_finished_the_last_pick(void) |
2782 | 0 | { |
2783 | 0 | struct strbuf buf = STRBUF_INIT; |
2784 | 0 | const char *eol; |
2785 | 0 | const char *todo_path = git_path_todo_file(); |
2786 | 0 | int ret = 0; |
2787 | |
|
2788 | 0 | if (strbuf_read_file(&buf, todo_path, 0) < 0) { |
2789 | 0 | if (errno == ENOENT) { |
2790 | 0 | return 0; |
2791 | 0 | } else { |
2792 | 0 | error_errno("unable to open '%s'", todo_path); |
2793 | 0 | return 0; |
2794 | 0 | } |
2795 | 0 | } |
2796 | | /* If there is only one line then we are done */ |
2797 | 0 | eol = strchr(buf.buf, '\n'); |
2798 | 0 | if (!eol || !eol[1]) |
2799 | 0 | ret = 1; |
2800 | |
|
2801 | 0 | strbuf_release(&buf); |
2802 | |
|
2803 | 0 | return ret; |
2804 | 0 | } |
2805 | | |
2806 | | void sequencer_post_commit_cleanup(struct repository *r, int verbose) |
2807 | 7.22k | { |
2808 | 7.22k | struct replay_opts opts = REPLAY_OPTS_INIT; |
2809 | 7.22k | int need_cleanup = 0; |
2810 | | |
2811 | 7.22k | if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD")) { |
2812 | 0 | if (!refs_delete_ref(get_main_ref_store(r), "", |
2813 | 0 | "CHERRY_PICK_HEAD", NULL, 0) && |
2814 | 0 | verbose) |
2815 | 0 | warning(_("cancelling a cherry picking in progress")); |
2816 | 0 | opts.action = REPLAY_PICK; |
2817 | 0 | need_cleanup = 1; |
2818 | 0 | } |
2819 | | |
2820 | 7.22k | if (refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD")) { |
2821 | 0 | if (!refs_delete_ref(get_main_ref_store(r), "", "REVERT_HEAD", |
2822 | 0 | NULL, 0) && |
2823 | 0 | verbose) |
2824 | 0 | warning(_("cancelling a revert in progress")); |
2825 | 0 | opts.action = REPLAY_REVERT; |
2826 | 0 | need_cleanup = 1; |
2827 | 0 | } |
2828 | | |
2829 | 7.22k | unlink(git_path_auto_merge(r)); |
2830 | | |
2831 | 7.22k | if (!need_cleanup) |
2832 | 7.22k | return; |
2833 | | |
2834 | 0 | if (!have_finished_the_last_pick()) |
2835 | 0 | return; |
2836 | | |
2837 | 0 | sequencer_remove_state(&opts); |
2838 | 0 | } |
2839 | | |
2840 | | static void todo_list_write_total_nr(struct todo_list *todo_list) |
2841 | 0 | { |
2842 | 0 | FILE *f = fopen_or_warn(rebase_path_msgtotal(), "w"); |
2843 | |
|
2844 | 0 | if (f) { |
2845 | 0 | fprintf(f, "%d\n", todo_list->total_nr); |
2846 | 0 | fclose(f); |
2847 | 0 | } |
2848 | 0 | } |
2849 | | |
2850 | | static int read_populate_todo(struct repository *r, |
2851 | | struct todo_list *todo_list, |
2852 | | struct replay_opts *opts) |
2853 | 0 | { |
2854 | 0 | const char *todo_file = get_todo_path(opts); |
2855 | 0 | int res; |
2856 | |
|
2857 | 0 | strbuf_reset(&todo_list->buf); |
2858 | 0 | if (strbuf_read_file_or_whine(&todo_list->buf, todo_file) < 0) |
2859 | 0 | return -1; |
2860 | | |
2861 | 0 | res = todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list); |
2862 | 0 | if (res) { |
2863 | 0 | if (is_rebase_i(opts)) |
2864 | 0 | return error(_("please fix this using " |
2865 | 0 | "'git rebase --edit-todo'.")); |
2866 | 0 | return error(_("unusable instruction sheet: '%s'"), todo_file); |
2867 | 0 | } |
2868 | | |
2869 | 0 | if (!todo_list->nr && |
2870 | 0 | (!is_rebase_i(opts) || !file_exists(rebase_path_done()))) |
2871 | 0 | return error(_("no commits parsed.")); |
2872 | | |
2873 | 0 | if (!is_rebase_i(opts)) { |
2874 | 0 | enum todo_command valid = |
2875 | 0 | opts->action == REPLAY_PICK ? TODO_PICK : TODO_REVERT; |
2876 | 0 | int i; |
2877 | |
|
2878 | 0 | for (i = 0; i < todo_list->nr; i++) |
2879 | 0 | if (valid == todo_list->items[i].command) |
2880 | 0 | continue; |
2881 | 0 | else if (valid == TODO_PICK) |
2882 | 0 | return error(_("cannot cherry-pick during a revert.")); |
2883 | 0 | else |
2884 | 0 | return error(_("cannot revert during a cherry-pick.")); |
2885 | 0 | } |
2886 | | |
2887 | 0 | if (is_rebase_i(opts)) { |
2888 | 0 | struct todo_list done = TODO_LIST_INIT; |
2889 | |
|
2890 | 0 | if (strbuf_read_file(&done.buf, rebase_path_done(), 0) > 0 && |
2891 | 0 | !todo_list_parse_insn_buffer(r, done.buf.buf, &done)) |
2892 | 0 | todo_list->done_nr = count_commands(&done); |
2893 | 0 | else |
2894 | 0 | todo_list->done_nr = 0; |
2895 | |
|
2896 | 0 | todo_list->total_nr = todo_list->done_nr |
2897 | 0 | + count_commands(todo_list); |
2898 | 0 | todo_list_release(&done); |
2899 | |
|
2900 | 0 | todo_list_write_total_nr(todo_list); |
2901 | 0 | } |
2902 | |
|
2903 | 0 | return 0; |
2904 | 0 | } |
2905 | | |
2906 | | static int git_config_string_dup(char **dest, |
2907 | | const char *var, const char *value) |
2908 | 0 | { |
2909 | 0 | if (!value) |
2910 | 0 | return config_error_nonbool(var); |
2911 | 0 | free(*dest); |
2912 | 0 | *dest = xstrdup(value); |
2913 | 0 | return 0; |
2914 | 0 | } |
2915 | | |
2916 | | static int populate_opts_cb(const char *key, const char *value, |
2917 | | const struct config_context *ctx, |
2918 | | void *data) |
2919 | 0 | { |
2920 | 0 | struct replay_opts *opts = data; |
2921 | 0 | int error_flag = 1; |
2922 | |
|
2923 | 0 | if (!value) |
2924 | 0 | error_flag = 0; |
2925 | 0 | else if (!strcmp(key, "options.no-commit")) |
2926 | 0 | opts->no_commit = git_config_bool_or_int(key, value, ctx->kvi, &error_flag); |
2927 | 0 | else if (!strcmp(key, "options.edit")) |
2928 | 0 | opts->edit = git_config_bool_or_int(key, value, ctx->kvi, &error_flag); |
2929 | 0 | else if (!strcmp(key, "options.allow-empty")) |
2930 | 0 | opts->allow_empty = |
2931 | 0 | git_config_bool_or_int(key, value, ctx->kvi, &error_flag); |
2932 | 0 | else if (!strcmp(key, "options.allow-empty-message")) |
2933 | 0 | opts->allow_empty_message = |
2934 | 0 | git_config_bool_or_int(key, value, ctx->kvi, &error_flag); |
2935 | 0 | else if (!strcmp(key, "options.keep-redundant-commits")) |
2936 | 0 | opts->keep_redundant_commits = |
2937 | 0 | git_config_bool_or_int(key, value, ctx->kvi, &error_flag); |
2938 | 0 | else if (!strcmp(key, "options.signoff")) |
2939 | 0 | opts->signoff = git_config_bool_or_int(key, value, ctx->kvi, &error_flag); |
2940 | 0 | else if (!strcmp(key, "options.record-origin")) |
2941 | 0 | opts->record_origin = git_config_bool_or_int(key, value, ctx->kvi, &error_flag); |
2942 | 0 | else if (!strcmp(key, "options.allow-ff")) |
2943 | 0 | opts->allow_ff = git_config_bool_or_int(key, value, ctx->kvi, &error_flag); |
2944 | 0 | else if (!strcmp(key, "options.mainline")) |
2945 | 0 | opts->mainline = git_config_int(key, value, ctx->kvi); |
2946 | 0 | else if (!strcmp(key, "options.strategy")) |
2947 | 0 | git_config_string_dup(&opts->strategy, key, value); |
2948 | 0 | else if (!strcmp(key, "options.gpg-sign")) |
2949 | 0 | git_config_string_dup(&opts->gpg_sign, key, value); |
2950 | 0 | else if (!strcmp(key, "options.strategy-option")) { |
2951 | 0 | strvec_push(&opts->xopts, value); |
2952 | 0 | } else if (!strcmp(key, "options.allow-rerere-auto")) |
2953 | 0 | opts->allow_rerere_auto = |
2954 | 0 | git_config_bool_or_int(key, value, ctx->kvi, &error_flag) ? |
2955 | 0 | RERERE_AUTOUPDATE : RERERE_NOAUTOUPDATE; |
2956 | 0 | else if (!strcmp(key, "options.default-msg-cleanup")) { |
2957 | 0 | opts->explicit_cleanup = 1; |
2958 | 0 | opts->default_msg_cleanup = get_cleanup_mode(value, 1); |
2959 | 0 | } else |
2960 | 0 | return error(_("invalid key: %s"), key); |
2961 | | |
2962 | 0 | if (!error_flag) |
2963 | 0 | return error(_("invalid value for '%s': '%s'"), key, value); |
2964 | | |
2965 | 0 | return 0; |
2966 | 0 | } |
2967 | | |
2968 | | static void parse_strategy_opts(struct replay_opts *opts, char *raw_opts) |
2969 | 0 | { |
2970 | 0 | int i; |
2971 | 0 | int count; |
2972 | 0 | const char **argv; |
2973 | 0 | char *strategy_opts_string = raw_opts; |
2974 | |
|
2975 | 0 | if (*strategy_opts_string == ' ') |
2976 | 0 | strategy_opts_string++; |
2977 | |
|
2978 | 0 | count = split_cmdline(strategy_opts_string, &argv); |
2979 | 0 | if (count < 0) |
2980 | 0 | BUG("could not split '%s': %s", strategy_opts_string, |
2981 | 0 | split_cmdline_strerror(count)); |
2982 | 0 | for (i = 0; i < count; i++) { |
2983 | 0 | const char *arg = argv[i]; |
2984 | |
|
2985 | 0 | skip_prefix(arg, "--", &arg); |
2986 | 0 | strvec_push(&opts->xopts, arg); |
2987 | 0 | } |
2988 | 0 | free(argv); |
2989 | 0 | } |
2990 | | |
2991 | | static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf) |
2992 | 0 | { |
2993 | 0 | strbuf_reset(buf); |
2994 | 0 | if (!read_oneliner(buf, rebase_path_strategy(), 0)) |
2995 | 0 | return; |
2996 | 0 | opts->strategy = strbuf_detach(buf, NULL); |
2997 | 0 | if (!read_oneliner(buf, rebase_path_strategy_opts(), 0)) |
2998 | 0 | return; |
2999 | | |
3000 | 0 | parse_strategy_opts(opts, buf->buf); |
3001 | 0 | } |
3002 | | |
3003 | | static int read_populate_opts(struct replay_opts *opts) |
3004 | 0 | { |
3005 | 0 | if (is_rebase_i(opts)) { |
3006 | 0 | struct strbuf buf = STRBUF_INIT; |
3007 | 0 | int ret = 0; |
3008 | |
|
3009 | 0 | if (read_oneliner(&buf, rebase_path_gpg_sign_opt(), |
3010 | 0 | READ_ONELINER_SKIP_IF_EMPTY)) { |
3011 | 0 | if (!starts_with(buf.buf, "-S")) |
3012 | 0 | strbuf_reset(&buf); |
3013 | 0 | else { |
3014 | 0 | free(opts->gpg_sign); |
3015 | 0 | opts->gpg_sign = xstrdup(buf.buf + 2); |
3016 | 0 | } |
3017 | 0 | strbuf_reset(&buf); |
3018 | 0 | } |
3019 | |
|
3020 | 0 | if (read_oneliner(&buf, rebase_path_allow_rerere_autoupdate(), |
3021 | 0 | READ_ONELINER_SKIP_IF_EMPTY)) { |
3022 | 0 | if (!strcmp(buf.buf, "--rerere-autoupdate")) |
3023 | 0 | opts->allow_rerere_auto = RERERE_AUTOUPDATE; |
3024 | 0 | else if (!strcmp(buf.buf, "--no-rerere-autoupdate")) |
3025 | 0 | opts->allow_rerere_auto = RERERE_NOAUTOUPDATE; |
3026 | 0 | strbuf_reset(&buf); |
3027 | 0 | } |
3028 | |
|
3029 | 0 | if (file_exists(rebase_path_verbose())) |
3030 | 0 | opts->verbose = 1; |
3031 | |
|
3032 | 0 | if (file_exists(rebase_path_quiet())) |
3033 | 0 | opts->quiet = 1; |
3034 | |
|
3035 | 0 | if (file_exists(rebase_path_signoff())) { |
3036 | 0 | opts->allow_ff = 0; |
3037 | 0 | opts->signoff = 1; |
3038 | 0 | } |
3039 | |
|
3040 | 0 | if (file_exists(rebase_path_cdate_is_adate())) { |
3041 | 0 | opts->allow_ff = 0; |
3042 | 0 | opts->committer_date_is_author_date = 1; |
3043 | 0 | } |
3044 | |
|
3045 | 0 | if (file_exists(rebase_path_ignore_date())) { |
3046 | 0 | opts->allow_ff = 0; |
3047 | 0 | opts->ignore_date = 1; |
3048 | 0 | } |
3049 | |
|
3050 | 0 | if (file_exists(rebase_path_reschedule_failed_exec())) |
3051 | 0 | opts->reschedule_failed_exec = 1; |
3052 | 0 | else if (file_exists(rebase_path_no_reschedule_failed_exec())) |
3053 | 0 | opts->reschedule_failed_exec = 0; |
3054 | |
|
3055 | 0 | if (file_exists(rebase_path_drop_redundant_commits())) |
3056 | 0 | opts->drop_redundant_commits = 1; |
3057 | |
|
3058 | 0 | if (file_exists(rebase_path_keep_redundant_commits())) |
3059 | 0 | opts->keep_redundant_commits = 1; |
3060 | |
|
3061 | 0 | read_strategy_opts(opts, &buf); |
3062 | 0 | strbuf_reset(&buf); |
3063 | |
|
3064 | 0 | if (read_oneliner(&opts->current_fixups, |
3065 | 0 | rebase_path_current_fixups(), |
3066 | 0 | READ_ONELINER_SKIP_IF_EMPTY)) { |
3067 | 0 | const char *p = opts->current_fixups.buf; |
3068 | 0 | opts->current_fixup_count = 1; |
3069 | 0 | while ((p = strchr(p, '\n'))) { |
3070 | 0 | opts->current_fixup_count++; |
3071 | 0 | p++; |
3072 | 0 | } |
3073 | 0 | } |
3074 | |
|
3075 | 0 | if (read_oneliner(&buf, rebase_path_squash_onto(), 0)) { |
3076 | 0 | if (repo_get_oid_committish(the_repository, buf.buf, &opts->squash_onto) < 0) { |
3077 | 0 | ret = error(_("unusable squash-onto")); |
3078 | 0 | goto done_rebase_i; |
3079 | 0 | } |
3080 | 0 | opts->have_squash_onto = 1; |
3081 | 0 | } |
3082 | | |
3083 | 0 | done_rebase_i: |
3084 | 0 | strbuf_release(&buf); |
3085 | 0 | return ret; |
3086 | 0 | } |
3087 | | |
3088 | 0 | if (!file_exists(git_path_opts_file())) |
3089 | 0 | return 0; |
3090 | | /* |
3091 | | * The function git_parse_source(), called from git_config_from_file(), |
3092 | | * may die() in case of a syntactically incorrect file. We do not care |
3093 | | * about this case, though, because we wrote that file ourselves, so we |
3094 | | * are pretty certain that it is syntactically correct. |
3095 | | */ |
3096 | 0 | if (git_config_from_file(populate_opts_cb, git_path_opts_file(), opts) < 0) |
3097 | 0 | return error(_("malformed options sheet: '%s'"), |
3098 | 0 | git_path_opts_file()); |
3099 | 0 | return 0; |
3100 | 0 | } |
3101 | | |
3102 | | static void write_strategy_opts(struct replay_opts *opts) |
3103 | 0 | { |
3104 | 0 | struct strbuf buf = STRBUF_INIT; |
3105 | | |
3106 | | /* |
3107 | | * Quote strategy options so that they can be read correctly |
3108 | | * by split_cmdline(). |
3109 | | */ |
3110 | 0 | quote_cmdline(&buf, opts->xopts.v); |
3111 | 0 | write_file(rebase_path_strategy_opts(), "%s\n", buf.buf); |
3112 | 0 | strbuf_release(&buf); |
3113 | 0 | } |
3114 | | |
3115 | | int write_basic_state(struct replay_opts *opts, const char *head_name, |
3116 | | struct commit *onto, const struct object_id *orig_head) |
3117 | 0 | { |
3118 | 0 | if (head_name) |
3119 | 0 | write_file(rebase_path_head_name(), "%s\n", head_name); |
3120 | 0 | if (onto) |
3121 | 0 | write_file(rebase_path_onto(), "%s\n", |
3122 | 0 | oid_to_hex(&onto->object.oid)); |
3123 | 0 | if (orig_head) |
3124 | 0 | write_file(rebase_path_orig_head(), "%s\n", |
3125 | 0 | oid_to_hex(orig_head)); |
3126 | |
|
3127 | 0 | if (opts->quiet) |
3128 | 0 | write_file(rebase_path_quiet(), "%s", ""); |
3129 | 0 | if (opts->verbose) |
3130 | 0 | write_file(rebase_path_verbose(), "%s", ""); |
3131 | 0 | if (opts->strategy) |
3132 | 0 | write_file(rebase_path_strategy(), "%s\n", opts->strategy); |
3133 | 0 | if (opts->xopts.nr > 0) |
3134 | 0 | write_strategy_opts(opts); |
3135 | |
|
3136 | 0 | if (opts->allow_rerere_auto == RERERE_AUTOUPDATE) |
3137 | 0 | write_file(rebase_path_allow_rerere_autoupdate(), "--rerere-autoupdate\n"); |
3138 | 0 | else if (opts->allow_rerere_auto == RERERE_NOAUTOUPDATE) |
3139 | 0 | write_file(rebase_path_allow_rerere_autoupdate(), "--no-rerere-autoupdate\n"); |
3140 | |
|
3141 | 0 | if (opts->gpg_sign) |
3142 | 0 | write_file(rebase_path_gpg_sign_opt(), "-S%s\n", opts->gpg_sign); |
3143 | 0 | if (opts->signoff) |
3144 | 0 | write_file(rebase_path_signoff(), "--signoff\n"); |
3145 | 0 | if (opts->drop_redundant_commits) |
3146 | 0 | write_file(rebase_path_drop_redundant_commits(), "%s", ""); |
3147 | 0 | if (opts->keep_redundant_commits) |
3148 | 0 | write_file(rebase_path_keep_redundant_commits(), "%s", ""); |
3149 | 0 | if (opts->committer_date_is_author_date) |
3150 | 0 | write_file(rebase_path_cdate_is_adate(), "%s", ""); |
3151 | 0 | if (opts->ignore_date) |
3152 | 0 | write_file(rebase_path_ignore_date(), "%s", ""); |
3153 | 0 | if (opts->reschedule_failed_exec) |
3154 | 0 | write_file(rebase_path_reschedule_failed_exec(), "%s", ""); |
3155 | 0 | else |
3156 | 0 | write_file(rebase_path_no_reschedule_failed_exec(), "%s", ""); |
3157 | |
|
3158 | 0 | return 0; |
3159 | 0 | } |
3160 | | |
3161 | | static int walk_revs_populate_todo(struct todo_list *todo_list, |
3162 | | struct replay_opts *opts) |
3163 | 0 | { |
3164 | 0 | enum todo_command command = opts->action == REPLAY_PICK ? |
3165 | 0 | TODO_PICK : TODO_REVERT; |
3166 | 0 | const char *command_string = todo_command_info[command].str; |
3167 | 0 | const char *encoding; |
3168 | 0 | struct commit *commit; |
3169 | |
|
3170 | 0 | if (prepare_revs(opts)) |
3171 | 0 | return -1; |
3172 | | |
3173 | 0 | encoding = get_log_output_encoding(); |
3174 | |
|
3175 | 0 | while ((commit = get_revision(opts->revs))) { |
3176 | 0 | struct todo_item *item = append_new_todo(todo_list); |
3177 | 0 | const char *commit_buffer = repo_logmsg_reencode(the_repository, |
3178 | 0 | commit, NULL, |
3179 | 0 | encoding); |
3180 | 0 | const char *subject; |
3181 | 0 | int subject_len; |
3182 | |
|
3183 | 0 | item->command = command; |
3184 | 0 | item->commit = commit; |
3185 | 0 | item->arg_offset = 0; |
3186 | 0 | item->arg_len = 0; |
3187 | 0 | item->offset_in_buf = todo_list->buf.len; |
3188 | 0 | subject_len = find_commit_subject(commit_buffer, &subject); |
3189 | 0 | strbuf_addf(&todo_list->buf, "%s %s %.*s\n", command_string, |
3190 | 0 | short_commit_name(the_repository, commit), |
3191 | 0 | subject_len, subject); |
3192 | 0 | repo_unuse_commit_buffer(the_repository, commit, |
3193 | 0 | commit_buffer); |
3194 | 0 | } |
3195 | |
|
3196 | 0 | if (!todo_list->nr) |
3197 | 0 | return error(_("empty commit set passed")); |
3198 | | |
3199 | 0 | return 0; |
3200 | 0 | } |
3201 | | |
3202 | | static int create_seq_dir(struct repository *r) |
3203 | 0 | { |
3204 | 0 | enum replay_action action; |
3205 | 0 | const char *in_progress_error = NULL; |
3206 | 0 | const char *in_progress_advice = NULL; |
3207 | 0 | unsigned int advise_skip = |
3208 | 0 | refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD") || |
3209 | 0 | refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD"); |
3210 | |
|
3211 | 0 | if (!sequencer_get_last_command(r, &action)) { |
3212 | 0 | switch (action) { |
3213 | 0 | case REPLAY_REVERT: |
3214 | 0 | in_progress_error = _("revert is already in progress"); |
3215 | 0 | in_progress_advice = |
3216 | 0 | _("try \"git revert (--continue | %s--abort | --quit)\""); |
3217 | 0 | break; |
3218 | 0 | case REPLAY_PICK: |
3219 | 0 | in_progress_error = _("cherry-pick is already in progress"); |
3220 | 0 | in_progress_advice = |
3221 | 0 | _("try \"git cherry-pick (--continue | %s--abort | --quit)\""); |
3222 | 0 | break; |
3223 | 0 | default: |
3224 | 0 | BUG("unexpected action in create_seq_dir"); |
3225 | 0 | } |
3226 | 0 | } |
3227 | 0 | if (in_progress_error) { |
3228 | 0 | error("%s", in_progress_error); |
3229 | 0 | if (advice_enabled(ADVICE_SEQUENCER_IN_USE)) |
3230 | 0 | advise(in_progress_advice, |
3231 | 0 | advise_skip ? "--skip | " : ""); |
3232 | 0 | return -1; |
3233 | 0 | } |
3234 | 0 | if (mkdir(git_path_seq_dir(), 0777) < 0) |
3235 | 0 | return error_errno(_("could not create sequencer directory '%s'"), |
3236 | 0 | git_path_seq_dir()); |
3237 | | |
3238 | 0 | return 0; |
3239 | 0 | } |
3240 | | |
3241 | | static int save_head(const char *head) |
3242 | 0 | { |
3243 | 0 | return write_message(head, strlen(head), git_path_head_file(), 1); |
3244 | 0 | } |
3245 | | |
3246 | | static int rollback_is_safe(void) |
3247 | 0 | { |
3248 | 0 | struct strbuf sb = STRBUF_INIT; |
3249 | 0 | struct object_id expected_head, actual_head; |
3250 | |
|
3251 | 0 | if (strbuf_read_file(&sb, git_path_abort_safety_file(), 0) >= 0) { |
3252 | 0 | strbuf_trim(&sb); |
3253 | 0 | if (get_oid_hex(sb.buf, &expected_head)) { |
3254 | 0 | strbuf_release(&sb); |
3255 | 0 | die(_("could not parse %s"), git_path_abort_safety_file()); |
3256 | 0 | } |
3257 | 0 | strbuf_release(&sb); |
3258 | 0 | } |
3259 | 0 | else if (errno == ENOENT) |
3260 | 0 | oidclr(&expected_head); |
3261 | 0 | else |
3262 | 0 | die_errno(_("could not read '%s'"), git_path_abort_safety_file()); |
3263 | | |
3264 | 0 | if (repo_get_oid(the_repository, "HEAD", &actual_head)) |
3265 | 0 | oidclr(&actual_head); |
3266 | |
|
3267 | 0 | return oideq(&actual_head, &expected_head); |
3268 | 0 | } |
3269 | | |
3270 | | static int reset_merge(const struct object_id *oid) |
3271 | 0 | { |
3272 | 0 | struct child_process cmd = CHILD_PROCESS_INIT; |
3273 | |
|
3274 | 0 | cmd.git_cmd = 1; |
3275 | 0 | strvec_pushl(&cmd.args, "reset", "--merge", NULL); |
3276 | |
|
3277 | 0 | if (!is_null_oid(oid)) |
3278 | 0 | strvec_push(&cmd.args, oid_to_hex(oid)); |
3279 | |
|
3280 | 0 | return run_command(&cmd); |
3281 | 0 | } |
3282 | | |
3283 | | static int rollback_single_pick(struct repository *r) |
3284 | 0 | { |
3285 | 0 | struct object_id head_oid; |
3286 | |
|
3287 | 0 | if (!refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") && |
3288 | 0 | !refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD")) |
3289 | 0 | return error(_("no cherry-pick or revert in progress")); |
3290 | 0 | if (read_ref_full("HEAD", 0, &head_oid, NULL)) |
3291 | 0 | return error(_("cannot resolve HEAD")); |
3292 | 0 | if (is_null_oid(&head_oid)) |
3293 | 0 | return error(_("cannot abort from a branch yet to be born")); |
3294 | 0 | return reset_merge(&head_oid); |
3295 | 0 | } |
3296 | | |
3297 | | static int skip_single_pick(void) |
3298 | 0 | { |
3299 | 0 | struct object_id head; |
3300 | |
|
3301 | 0 | if (read_ref_full("HEAD", 0, &head, NULL)) |
3302 | 0 | return error(_("cannot resolve HEAD")); |
3303 | 0 | return reset_merge(&head); |
3304 | 0 | } |
3305 | | |
3306 | | int sequencer_rollback(struct repository *r, struct replay_opts *opts) |
3307 | 0 | { |
3308 | 0 | FILE *f; |
3309 | 0 | struct object_id oid; |
3310 | 0 | struct strbuf buf = STRBUF_INIT; |
3311 | 0 | const char *p; |
3312 | |
|
3313 | 0 | f = fopen(git_path_head_file(), "r"); |
3314 | 0 | if (!f && errno == ENOENT) { |
3315 | | /* |
3316 | | * There is no multiple-cherry-pick in progress. |
3317 | | * If CHERRY_PICK_HEAD or REVERT_HEAD indicates |
3318 | | * a single-cherry-pick in progress, abort that. |
3319 | | */ |
3320 | 0 | return rollback_single_pick(r); |
3321 | 0 | } |
3322 | 0 | if (!f) |
3323 | 0 | return error_errno(_("cannot open '%s'"), git_path_head_file()); |
3324 | 0 | if (strbuf_getline_lf(&buf, f)) { |
3325 | 0 | error(_("cannot read '%s': %s"), git_path_head_file(), |
3326 | 0 | ferror(f) ? strerror(errno) : _("unexpected end of file")); |
3327 | 0 | fclose(f); |
3328 | 0 | goto fail; |
3329 | 0 | } |
3330 | 0 | fclose(f); |
3331 | 0 | if (parse_oid_hex(buf.buf, &oid, &p) || *p != '\0') { |
3332 | 0 | error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"), |
3333 | 0 | git_path_head_file()); |
3334 | 0 | goto fail; |
3335 | 0 | } |
3336 | 0 | if (is_null_oid(&oid)) { |
3337 | 0 | error(_("cannot abort from a branch yet to be born")); |
3338 | 0 | goto fail; |
3339 | 0 | } |
3340 | | |
3341 | 0 | if (!rollback_is_safe()) { |
3342 | | /* Do not error, just do not rollback */ |
3343 | 0 | warning(_("You seem to have moved HEAD. " |
3344 | 0 | "Not rewinding, check your HEAD!")); |
3345 | 0 | } else |
3346 | 0 | if (reset_merge(&oid)) |
3347 | 0 | goto fail; |
3348 | 0 | strbuf_release(&buf); |
3349 | 0 | return sequencer_remove_state(opts); |
3350 | 0 | fail: |
3351 | 0 | strbuf_release(&buf); |
3352 | 0 | return -1; |
3353 | 0 | } |
3354 | | |
3355 | | int sequencer_skip(struct repository *r, struct replay_opts *opts) |
3356 | 0 | { |
3357 | 0 | enum replay_action action = -1; |
3358 | 0 | sequencer_get_last_command(r, &action); |
3359 | | |
3360 | | /* |
3361 | | * Check whether the subcommand requested to skip the commit is actually |
3362 | | * in progress and that it's safe to skip the commit. |
3363 | | * |
3364 | | * opts->action tells us which subcommand requested to skip the commit. |
3365 | | * If the corresponding .git/<ACTION>_HEAD exists, we know that the |
3366 | | * action is in progress and we can skip the commit. |
3367 | | * |
3368 | | * Otherwise we check that the last instruction was related to the |
3369 | | * particular subcommand we're trying to execute and barf if that's not |
3370 | | * the case. |
3371 | | * |
3372 | | * Finally we check that the rollback is "safe", i.e., has the HEAD |
3373 | | * moved? In this case, it doesn't make sense to "reset the merge" and |
3374 | | * "skip the commit" as the user already handled this by committing. But |
3375 | | * we'd not want to barf here, instead give advice on how to proceed. We |
3376 | | * only need to check that when .git/<ACTION>_HEAD doesn't exist because |
3377 | | * it gets removed when the user commits, so if it still exists we're |
3378 | | * sure the user can't have committed before. |
3379 | | */ |
3380 | 0 | switch (opts->action) { |
3381 | 0 | case REPLAY_REVERT: |
3382 | 0 | if (!refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD")) { |
3383 | 0 | if (action != REPLAY_REVERT) |
3384 | 0 | return error(_("no revert in progress")); |
3385 | 0 | if (!rollback_is_safe()) |
3386 | 0 | goto give_advice; |
3387 | 0 | } |
3388 | 0 | break; |
3389 | 0 | case REPLAY_PICK: |
3390 | 0 | if (!refs_ref_exists(get_main_ref_store(r), |
3391 | 0 | "CHERRY_PICK_HEAD")) { |
3392 | 0 | if (action != REPLAY_PICK) |
3393 | 0 | return error(_("no cherry-pick in progress")); |
3394 | 0 | if (!rollback_is_safe()) |
3395 | 0 | goto give_advice; |
3396 | 0 | } |
3397 | 0 | break; |
3398 | 0 | default: |
3399 | 0 | BUG("unexpected action in sequencer_skip"); |
3400 | 0 | } |
3401 | | |
3402 | 0 | if (skip_single_pick()) |
3403 | 0 | return error(_("failed to skip the commit")); |
3404 | 0 | if (!is_directory(git_path_seq_dir())) |
3405 | 0 | return 0; |
3406 | | |
3407 | 0 | return sequencer_continue(r, opts); |
3408 | | |
3409 | 0 | give_advice: |
3410 | 0 | error(_("there is nothing to skip")); |
3411 | |
|
3412 | 0 | if (advice_enabled(ADVICE_RESOLVE_CONFLICT)) { |
3413 | 0 | advise(_("have you committed already?\n" |
3414 | 0 | "try \"git %s --continue\""), |
3415 | 0 | action == REPLAY_REVERT ? "revert" : "cherry-pick"); |
3416 | 0 | } |
3417 | 0 | return -1; |
3418 | 0 | } |
3419 | | |
3420 | | static int save_todo(struct todo_list *todo_list, struct replay_opts *opts, |
3421 | | int reschedule) |
3422 | 0 | { |
3423 | 0 | struct lock_file todo_lock = LOCK_INIT; |
3424 | 0 | const char *todo_path = get_todo_path(opts); |
3425 | 0 | int next = todo_list->current, offset, fd; |
3426 | | |
3427 | | /* |
3428 | | * rebase -i writes "git-rebase-todo" without the currently executing |
3429 | | * command, appending it to "done" instead. |
3430 | | */ |
3431 | 0 | if (is_rebase_i(opts) && !reschedule) |
3432 | 0 | next++; |
3433 | |
|
3434 | 0 | fd = hold_lock_file_for_update(&todo_lock, todo_path, 0); |
3435 | 0 | if (fd < 0) |
3436 | 0 | return error_errno(_("could not lock '%s'"), todo_path); |
3437 | 0 | offset = get_item_line_offset(todo_list, next); |
3438 | 0 | if (write_in_full(fd, todo_list->buf.buf + offset, |
3439 | 0 | todo_list->buf.len - offset) < 0) |
3440 | 0 | return error_errno(_("could not write to '%s'"), todo_path); |
3441 | 0 | if (commit_lock_file(&todo_lock) < 0) |
3442 | 0 | return error(_("failed to finalize '%s'"), todo_path); |
3443 | | |
3444 | 0 | if (is_rebase_i(opts) && !reschedule && next > 0) { |
3445 | 0 | const char *done = rebase_path_done(); |
3446 | 0 | int fd = open(done, O_CREAT | O_WRONLY | O_APPEND, 0666); |
3447 | 0 | int ret = 0; |
3448 | |
|
3449 | 0 | if (fd < 0) |
3450 | 0 | return 0; |
3451 | 0 | if (write_in_full(fd, get_item_line(todo_list, next - 1), |
3452 | 0 | get_item_line_length(todo_list, next - 1)) |
3453 | 0 | < 0) |
3454 | 0 | ret = error_errno(_("could not write to '%s'"), done); |
3455 | 0 | if (close(fd) < 0) |
3456 | 0 | ret = error_errno(_("failed to finalize '%s'"), done); |
3457 | 0 | return ret; |
3458 | 0 | } |
3459 | 0 | return 0; |
3460 | 0 | } |
3461 | | |
3462 | | static int save_opts(struct replay_opts *opts) |
3463 | 0 | { |
3464 | 0 | const char *opts_file = git_path_opts_file(); |
3465 | 0 | int res = 0; |
3466 | |
|
3467 | 0 | if (opts->no_commit) |
3468 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3469 | 0 | "options.no-commit", "true"); |
3470 | 0 | if (opts->edit >= 0) |
3471 | 0 | res |= git_config_set_in_file_gently(opts_file, "options.edit", |
3472 | 0 | opts->edit ? "true" : "false"); |
3473 | 0 | if (opts->allow_empty) |
3474 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3475 | 0 | "options.allow-empty", "true"); |
3476 | 0 | if (opts->allow_empty_message) |
3477 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3478 | 0 | "options.allow-empty-message", "true"); |
3479 | 0 | if (opts->keep_redundant_commits) |
3480 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3481 | 0 | "options.keep-redundant-commits", "true"); |
3482 | 0 | if (opts->signoff) |
3483 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3484 | 0 | "options.signoff", "true"); |
3485 | 0 | if (opts->record_origin) |
3486 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3487 | 0 | "options.record-origin", "true"); |
3488 | 0 | if (opts->allow_ff) |
3489 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3490 | 0 | "options.allow-ff", "true"); |
3491 | 0 | if (opts->mainline) { |
3492 | 0 | struct strbuf buf = STRBUF_INIT; |
3493 | 0 | strbuf_addf(&buf, "%d", opts->mainline); |
3494 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3495 | 0 | "options.mainline", buf.buf); |
3496 | 0 | strbuf_release(&buf); |
3497 | 0 | } |
3498 | 0 | if (opts->strategy) |
3499 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3500 | 0 | "options.strategy", opts->strategy); |
3501 | 0 | if (opts->gpg_sign) |
3502 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3503 | 0 | "options.gpg-sign", opts->gpg_sign); |
3504 | 0 | for (size_t i = 0; i < opts->xopts.nr; i++) |
3505 | 0 | res |= git_config_set_multivar_in_file_gently(opts_file, |
3506 | 0 | "options.strategy-option", |
3507 | 0 | opts->xopts.v[i], "^$", 0); |
3508 | 0 | if (opts->allow_rerere_auto) |
3509 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3510 | 0 | "options.allow-rerere-auto", |
3511 | 0 | opts->allow_rerere_auto == RERERE_AUTOUPDATE ? |
3512 | 0 | "true" : "false"); |
3513 | |
|
3514 | 0 | if (opts->explicit_cleanup) |
3515 | 0 | res |= git_config_set_in_file_gently(opts_file, |
3516 | 0 | "options.default-msg-cleanup", |
3517 | 0 | describe_cleanup_mode(opts->default_msg_cleanup)); |
3518 | 0 | return res; |
3519 | 0 | } |
3520 | | |
3521 | | static int make_patch(struct repository *r, |
3522 | | struct commit *commit, |
3523 | | struct replay_opts *opts) |
3524 | 0 | { |
3525 | 0 | struct rev_info log_tree_opt; |
3526 | 0 | const char *subject; |
3527 | 0 | char hex[GIT_MAX_HEXSZ + 1]; |
3528 | 0 | int res = 0; |
3529 | |
|
3530 | 0 | if (!is_rebase_i(opts)) |
3531 | 0 | BUG("make_patch should only be called when rebasing"); |
3532 | | |
3533 | 0 | oid_to_hex_r(hex, &commit->object.oid); |
3534 | 0 | if (write_message(hex, strlen(hex), rebase_path_stopped_sha(), 1) < 0) |
3535 | 0 | return -1; |
3536 | 0 | res |= write_rebase_head(&commit->object.oid); |
3537 | |
|
3538 | 0 | memset(&log_tree_opt, 0, sizeof(log_tree_opt)); |
3539 | 0 | repo_init_revisions(r, &log_tree_opt, NULL); |
3540 | 0 | log_tree_opt.abbrev = 0; |
3541 | 0 | log_tree_opt.diff = 1; |
3542 | 0 | log_tree_opt.diffopt.output_format = DIFF_FORMAT_PATCH; |
3543 | 0 | log_tree_opt.disable_stdin = 1; |
3544 | 0 | log_tree_opt.no_commit_id = 1; |
3545 | 0 | log_tree_opt.diffopt.file = fopen(rebase_path_patch(), "w"); |
3546 | 0 | log_tree_opt.diffopt.use_color = GIT_COLOR_NEVER; |
3547 | 0 | if (!log_tree_opt.diffopt.file) |
3548 | 0 | res |= error_errno(_("could not open '%s'"), |
3549 | 0 | rebase_path_patch()); |
3550 | 0 | else { |
3551 | 0 | res |= log_tree_commit(&log_tree_opt, commit); |
3552 | 0 | fclose(log_tree_opt.diffopt.file); |
3553 | 0 | } |
3554 | |
|
3555 | 0 | if (!file_exists(rebase_path_message())) { |
3556 | 0 | const char *encoding = get_commit_output_encoding(); |
3557 | 0 | const char *commit_buffer = repo_logmsg_reencode(r, |
3558 | 0 | commit, NULL, |
3559 | 0 | encoding); |
3560 | 0 | find_commit_subject(commit_buffer, &subject); |
3561 | 0 | res |= write_message(subject, strlen(subject), rebase_path_message(), 1); |
3562 | 0 | repo_unuse_commit_buffer(r, commit, |
3563 | 0 | commit_buffer); |
3564 | 0 | } |
3565 | 0 | release_revisions(&log_tree_opt); |
3566 | |
|
3567 | 0 | return res; |
3568 | 0 | } |
3569 | | |
3570 | | static int intend_to_amend(void) |
3571 | 0 | { |
3572 | 0 | struct object_id head; |
3573 | 0 | char *p; |
3574 | |
|
3575 | 0 | if (repo_get_oid(the_repository, "HEAD", &head)) |
3576 | 0 | return error(_("cannot read HEAD")); |
3577 | | |
3578 | 0 | p = oid_to_hex(&head); |
3579 | 0 | return write_message(p, strlen(p), rebase_path_amend(), 1); |
3580 | 0 | } |
3581 | | |
3582 | | static int error_with_patch(struct repository *r, |
3583 | | struct commit *commit, |
3584 | | const char *subject, int subject_len, |
3585 | | struct replay_opts *opts, |
3586 | | int exit_code, int to_amend) |
3587 | 0 | { |
3588 | 0 | if (commit) { |
3589 | 0 | if (make_patch(r, commit, opts)) |
3590 | 0 | return -1; |
3591 | 0 | } else if (copy_file(rebase_path_message(), |
3592 | 0 | git_path_merge_msg(r), 0666)) |
3593 | 0 | return error(_("unable to copy '%s' to '%s'"), |
3594 | 0 | git_path_merge_msg(r), rebase_path_message()); |
3595 | | |
3596 | 0 | if (to_amend) { |
3597 | 0 | if (intend_to_amend()) |
3598 | 0 | return -1; |
3599 | | |
3600 | 0 | fprintf(stderr, |
3601 | 0 | _("You can amend the commit now, with\n" |
3602 | 0 | "\n" |
3603 | 0 | " git commit --amend %s\n" |
3604 | 0 | "\n" |
3605 | 0 | "Once you are satisfied with your changes, run\n" |
3606 | 0 | "\n" |
3607 | 0 | " git rebase --continue\n"), |
3608 | 0 | gpg_sign_opt_quoted(opts)); |
3609 | 0 | } else if (exit_code) { |
3610 | 0 | if (commit) |
3611 | 0 | fprintf_ln(stderr, _("Could not apply %s... %.*s"), |
3612 | 0 | short_commit_name(r, commit), subject_len, subject); |
3613 | 0 | else |
3614 | | /* |
3615 | | * We don't have the hash of the parent so |
3616 | | * just print the line from the todo file. |
3617 | | */ |
3618 | 0 | fprintf_ln(stderr, _("Could not merge %.*s"), |
3619 | 0 | subject_len, subject); |
3620 | 0 | } |
3621 | | |
3622 | 0 | return exit_code; |
3623 | 0 | } |
3624 | | |
3625 | | static int error_failed_squash(struct repository *r, |
3626 | | struct commit *commit, |
3627 | | struct replay_opts *opts, |
3628 | | int subject_len, |
3629 | | const char *subject) |
3630 | 0 | { |
3631 | 0 | if (copy_file(rebase_path_message(), rebase_path_squash_msg(), 0666)) |
3632 | 0 | return error(_("could not copy '%s' to '%s'"), |
3633 | 0 | rebase_path_squash_msg(), rebase_path_message()); |
3634 | 0 | unlink(git_path_merge_msg(r)); |
3635 | 0 | if (copy_file(git_path_merge_msg(r), rebase_path_message(), 0666)) |
3636 | 0 | return error(_("could not copy '%s' to '%s'"), |
3637 | 0 | rebase_path_message(), |
3638 | 0 | git_path_merge_msg(r)); |
3639 | 0 | return error_with_patch(r, commit, subject, subject_len, opts, 1, 0); |
3640 | 0 | } |
3641 | | |
3642 | | static int do_exec(struct repository *r, const char *command_line) |
3643 | 0 | { |
3644 | 0 | struct child_process cmd = CHILD_PROCESS_INIT; |
3645 | 0 | int dirty, status; |
3646 | |
|
3647 | 0 | fprintf(stderr, _("Executing: %s\n"), command_line); |
3648 | 0 | cmd.use_shell = 1; |
3649 | 0 | strvec_push(&cmd.args, command_line); |
3650 | 0 | status = run_command(&cmd); |
3651 | | |
3652 | | /* force re-reading of the cache */ |
3653 | 0 | discard_index(r->index); |
3654 | 0 | if (repo_read_index(r) < 0) |
3655 | 0 | return error(_("could not read index")); |
3656 | | |
3657 | 0 | dirty = require_clean_work_tree(r, "rebase", NULL, 1, 1); |
3658 | |
|
3659 | 0 | if (status) { |
3660 | 0 | warning(_("execution failed: %s\n%s" |
3661 | 0 | "You can fix the problem, and then run\n" |
3662 | 0 | "\n" |
3663 | 0 | " git rebase --continue\n" |
3664 | 0 | "\n"), |
3665 | 0 | command_line, |
3666 | 0 | dirty ? _("and made changes to the index and/or the " |
3667 | 0 | "working tree.\n") : ""); |
3668 | 0 | if (status == 127) |
3669 | | /* command not found */ |
3670 | 0 | status = 1; |
3671 | 0 | } else if (dirty) { |
3672 | 0 | warning(_("execution succeeded: %s\nbut " |
3673 | 0 | "left changes to the index and/or the working tree.\n" |
3674 | 0 | "Commit or stash your changes, and then run\n" |
3675 | 0 | "\n" |
3676 | 0 | " git rebase --continue\n" |
3677 | 0 | "\n"), command_line); |
3678 | 0 | status = 1; |
3679 | 0 | } |
3680 | |
|
3681 | 0 | return status; |
3682 | 0 | } |
3683 | | |
3684 | | __attribute__((format (printf, 2, 3))) |
3685 | | static int safe_append(const char *filename, const char *fmt, ...) |
3686 | 0 | { |
3687 | 0 | va_list ap; |
3688 | 0 | struct lock_file lock = LOCK_INIT; |
3689 | 0 | int fd = hold_lock_file_for_update(&lock, filename, |
3690 | 0 | LOCK_REPORT_ON_ERROR); |
3691 | 0 | struct strbuf buf = STRBUF_INIT; |
3692 | |
|
3693 | 0 | if (fd < 0) |
3694 | 0 | return -1; |
3695 | | |
3696 | 0 | if (strbuf_read_file(&buf, filename, 0) < 0 && errno != ENOENT) { |
3697 | 0 | error_errno(_("could not read '%s'"), filename); |
3698 | 0 | rollback_lock_file(&lock); |
3699 | 0 | return -1; |
3700 | 0 | } |
3701 | 0 | strbuf_complete(&buf, '\n'); |
3702 | 0 | va_start(ap, fmt); |
3703 | 0 | strbuf_vaddf(&buf, fmt, ap); |
3704 | 0 | va_end(ap); |
3705 | |
|
3706 | 0 | if (write_in_full(fd, buf.buf, buf.len) < 0) { |
3707 | 0 | error_errno(_("could not write to '%s'"), filename); |
3708 | 0 | strbuf_release(&buf); |
3709 | 0 | rollback_lock_file(&lock); |
3710 | 0 | return -1; |
3711 | 0 | } |
3712 | 0 | if (commit_lock_file(&lock) < 0) { |
3713 | 0 | strbuf_release(&buf); |
3714 | 0 | return error(_("failed to finalize '%s'"), filename); |
3715 | 0 | } |
3716 | | |
3717 | 0 | strbuf_release(&buf); |
3718 | 0 | return 0; |
3719 | 0 | } |
3720 | | |
3721 | | static int do_label(struct repository *r, const char *name, int len) |
3722 | 0 | { |
3723 | 0 | struct ref_store *refs = get_main_ref_store(r); |
3724 | 0 | struct ref_transaction *transaction; |
3725 | 0 | struct strbuf ref_name = STRBUF_INIT, err = STRBUF_INIT; |
3726 | 0 | struct strbuf msg = STRBUF_INIT; |
3727 | 0 | int ret = 0; |
3728 | 0 | struct object_id head_oid; |
3729 | |
|
3730 | 0 | if (len == 1 && *name == '#') |
3731 | 0 | return error(_("illegal label name: '%.*s'"), len, name); |
3732 | | |
3733 | 0 | strbuf_addf(&ref_name, "refs/rewritten/%.*s", len, name); |
3734 | 0 | strbuf_addf(&msg, "rebase (label) '%.*s'", len, name); |
3735 | |
|
3736 | 0 | transaction = ref_store_transaction_begin(refs, &err); |
3737 | 0 | if (!transaction) { |
3738 | 0 | error("%s", err.buf); |
3739 | 0 | ret = -1; |
3740 | 0 | } else if (repo_get_oid(r, "HEAD", &head_oid)) { |
3741 | 0 | error(_("could not read HEAD")); |
3742 | 0 | ret = -1; |
3743 | 0 | } else if (ref_transaction_update(transaction, ref_name.buf, &head_oid, |
3744 | 0 | NULL, 0, msg.buf, &err) < 0 || |
3745 | 0 | ref_transaction_commit(transaction, &err)) { |
3746 | 0 | error("%s", err.buf); |
3747 | 0 | ret = -1; |
3748 | 0 | } |
3749 | 0 | ref_transaction_free(transaction); |
3750 | 0 | strbuf_release(&err); |
3751 | 0 | strbuf_release(&msg); |
3752 | |
|
3753 | 0 | if (!ret) |
3754 | 0 | ret = safe_append(rebase_path_refs_to_delete(), |
3755 | 0 | "%s\n", ref_name.buf); |
3756 | 0 | strbuf_release(&ref_name); |
3757 | |
|
3758 | 0 | return ret; |
3759 | 0 | } |
3760 | | |
3761 | | static const char *sequencer_reflog_action(struct replay_opts *opts) |
3762 | 0 | { |
3763 | 0 | if (!opts->reflog_action) { |
3764 | 0 | opts->reflog_action = getenv(GIT_REFLOG_ACTION); |
3765 | 0 | opts->reflog_action = |
3766 | 0 | xstrdup(opts->reflog_action ? opts->reflog_action |
3767 | 0 | : action_name(opts)); |
3768 | 0 | } |
3769 | |
|
3770 | 0 | return opts->reflog_action; |
3771 | 0 | } |
3772 | | |
3773 | | __attribute__((format (printf, 3, 4))) |
3774 | | static const char *reflog_message(struct replay_opts *opts, |
3775 | | const char *sub_action, const char *fmt, ...) |
3776 | 0 | { |
3777 | 0 | va_list ap; |
3778 | 0 | static struct strbuf buf = STRBUF_INIT; |
3779 | |
|
3780 | 0 | va_start(ap, fmt); |
3781 | 0 | strbuf_reset(&buf); |
3782 | 0 | strbuf_addstr(&buf, sequencer_reflog_action(opts)); |
3783 | 0 | if (sub_action) |
3784 | 0 | strbuf_addf(&buf, " (%s)", sub_action); |
3785 | 0 | if (fmt) { |
3786 | 0 | strbuf_addstr(&buf, ": "); |
3787 | 0 | strbuf_vaddf(&buf, fmt, ap); |
3788 | 0 | } |
3789 | 0 | va_end(ap); |
3790 | |
|
3791 | 0 | return buf.buf; |
3792 | 0 | } |
3793 | | |
3794 | | static struct commit *lookup_label(struct repository *r, const char *label, |
3795 | | int len, struct strbuf *buf) |
3796 | 0 | { |
3797 | 0 | struct commit *commit; |
3798 | 0 | struct object_id oid; |
3799 | |
|
3800 | 0 | strbuf_reset(buf); |
3801 | 0 | strbuf_addf(buf, "refs/rewritten/%.*s", len, label); |
3802 | 0 | if (!read_ref(buf->buf, &oid)) { |
3803 | 0 | commit = lookup_commit_object(r, &oid); |
3804 | 0 | } else { |
3805 | | /* fall back to non-rewritten ref or commit */ |
3806 | 0 | strbuf_splice(buf, 0, strlen("refs/rewritten/"), "", 0); |
3807 | 0 | commit = lookup_commit_reference_by_name(buf->buf); |
3808 | 0 | } |
3809 | |
|
3810 | 0 | if (!commit) |
3811 | 0 | error(_("could not resolve '%s'"), buf->buf); |
3812 | |
|
3813 | 0 | return commit; |
3814 | 0 | } |
3815 | | |
3816 | | static int do_reset(struct repository *r, |
3817 | | const char *name, int len, |
3818 | | struct replay_opts *opts) |
3819 | 0 | { |
3820 | 0 | struct strbuf ref_name = STRBUF_INIT; |
3821 | 0 | struct object_id oid; |
3822 | 0 | struct lock_file lock = LOCK_INIT; |
3823 | 0 | struct tree_desc desc = { 0 }; |
3824 | 0 | struct tree *tree; |
3825 | 0 | struct unpack_trees_options unpack_tree_opts = { 0 }; |
3826 | 0 | int ret = 0; |
3827 | |
|
3828 | 0 | if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) |
3829 | 0 | return -1; |
3830 | | |
3831 | 0 | if (len == 10 && !strncmp("[new root]", name, len)) { |
3832 | 0 | if (!opts->have_squash_onto) { |
3833 | 0 | const char *hex; |
3834 | 0 | if (commit_tree("", 0, the_hash_algo->empty_tree, |
3835 | 0 | NULL, &opts->squash_onto, |
3836 | 0 | NULL, NULL)) |
3837 | 0 | return error(_("writing fake root commit")); |
3838 | 0 | opts->have_squash_onto = 1; |
3839 | 0 | hex = oid_to_hex(&opts->squash_onto); |
3840 | 0 | if (write_message(hex, strlen(hex), |
3841 | 0 | rebase_path_squash_onto(), 0)) |
3842 | 0 | return error(_("writing squash-onto")); |
3843 | 0 | } |
3844 | 0 | oidcpy(&oid, &opts->squash_onto); |
3845 | 0 | } else { |
3846 | 0 | int i; |
3847 | 0 | struct commit *commit; |
3848 | | |
3849 | | /* Determine the length of the label */ |
3850 | 0 | for (i = 0; i < len; i++) |
3851 | 0 | if (isspace(name[i])) |
3852 | 0 | break; |
3853 | 0 | len = i; |
3854 | |
|
3855 | 0 | commit = lookup_label(r, name, len, &ref_name); |
3856 | 0 | if (!commit) { |
3857 | 0 | ret = -1; |
3858 | 0 | goto cleanup; |
3859 | 0 | } |
3860 | 0 | oid = commit->object.oid; |
3861 | 0 | } |
3862 | | |
3863 | 0 | setup_unpack_trees_porcelain(&unpack_tree_opts, "reset"); |
3864 | 0 | unpack_tree_opts.head_idx = 1; |
3865 | 0 | unpack_tree_opts.src_index = r->index; |
3866 | 0 | unpack_tree_opts.dst_index = r->index; |
3867 | 0 | unpack_tree_opts.fn = oneway_merge; |
3868 | 0 | unpack_tree_opts.merge = 1; |
3869 | 0 | unpack_tree_opts.update = 1; |
3870 | 0 | unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */ |
3871 | 0 | unpack_tree_opts.skip_cache_tree_update = 1; |
3872 | 0 | init_checkout_metadata(&unpack_tree_opts.meta, name, &oid, NULL); |
3873 | |
|
3874 | 0 | if (repo_read_index_unmerged(r)) { |
3875 | 0 | ret = error_resolve_conflict(action_name(opts)); |
3876 | 0 | goto cleanup; |
3877 | 0 | } |
3878 | | |
3879 | 0 | if (!fill_tree_descriptor(r, &desc, &oid)) { |
3880 | 0 | ret = error(_("failed to find tree of %s"), oid_to_hex(&oid)); |
3881 | 0 | goto cleanup; |
3882 | 0 | } |
3883 | | |
3884 | 0 | if (unpack_trees(1, &desc, &unpack_tree_opts)) { |
3885 | 0 | ret = -1; |
3886 | 0 | goto cleanup; |
3887 | 0 | } |
3888 | | |
3889 | 0 | tree = parse_tree_indirect(&oid); |
3890 | 0 | prime_cache_tree(r, r->index, tree); |
3891 | |
|
3892 | 0 | if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) |
3893 | 0 | ret = error(_("could not write index")); |
3894 | |
|
3895 | 0 | if (!ret) |
3896 | 0 | ret = update_ref(reflog_message(opts, "reset", "'%.*s'", |
3897 | 0 | len, name), "HEAD", &oid, |
3898 | 0 | NULL, 0, UPDATE_REFS_MSG_ON_ERR); |
3899 | 0 | cleanup: |
3900 | 0 | free((void *)desc.buffer); |
3901 | 0 | if (ret < 0) |
3902 | 0 | rollback_lock_file(&lock); |
3903 | 0 | strbuf_release(&ref_name); |
3904 | 0 | clear_unpack_trees_porcelain(&unpack_tree_opts); |
3905 | 0 | return ret; |
3906 | 0 | } |
3907 | | |
3908 | | static int do_merge(struct repository *r, |
3909 | | struct commit *commit, |
3910 | | const char *arg, int arg_len, |
3911 | | int flags, int *check_todo, struct replay_opts *opts) |
3912 | 0 | { |
3913 | 0 | int run_commit_flags = 0; |
3914 | 0 | struct strbuf ref_name = STRBUF_INIT; |
3915 | 0 | struct commit *head_commit, *merge_commit, *i; |
3916 | 0 | struct commit_list *bases, *j; |
3917 | 0 | struct commit_list *to_merge = NULL, **tail = &to_merge; |
3918 | 0 | const char *strategy = !opts->xopts.nr && |
3919 | 0 | (!opts->strategy || |
3920 | 0 | !strcmp(opts->strategy, "recursive") || |
3921 | 0 | !strcmp(opts->strategy, "ort")) ? |
3922 | 0 | NULL : opts->strategy; |
3923 | 0 | struct merge_options o; |
3924 | 0 | int merge_arg_len, oneline_offset, can_fast_forward, ret, k; |
3925 | 0 | static struct lock_file lock; |
3926 | 0 | const char *p; |
3927 | |
|
3928 | 0 | if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) { |
3929 | 0 | ret = -1; |
3930 | 0 | goto leave_merge; |
3931 | 0 | } |
3932 | | |
3933 | 0 | head_commit = lookup_commit_reference_by_name("HEAD"); |
3934 | 0 | if (!head_commit) { |
3935 | 0 | ret = error(_("cannot merge without a current revision")); |
3936 | 0 | goto leave_merge; |
3937 | 0 | } |
3938 | | |
3939 | | /* |
3940 | | * For octopus merges, the arg starts with the list of revisions to be |
3941 | | * merged. The list is optionally followed by '#' and the oneline. |
3942 | | */ |
3943 | 0 | merge_arg_len = oneline_offset = arg_len; |
3944 | 0 | for (p = arg; p - arg < arg_len; p += strspn(p, " \t\n")) { |
3945 | 0 | if (!*p) |
3946 | 0 | break; |
3947 | 0 | if (*p == '#' && (!p[1] || isspace(p[1]))) { |
3948 | 0 | p += 1 + strspn(p + 1, " \t\n"); |
3949 | 0 | oneline_offset = p - arg; |
3950 | 0 | break; |
3951 | 0 | } |
3952 | 0 | k = strcspn(p, " \t\n"); |
3953 | 0 | if (!k) |
3954 | 0 | continue; |
3955 | 0 | merge_commit = lookup_label(r, p, k, &ref_name); |
3956 | 0 | if (!merge_commit) { |
3957 | 0 | ret = error(_("unable to parse '%.*s'"), k, p); |
3958 | 0 | goto leave_merge; |
3959 | 0 | } |
3960 | 0 | tail = &commit_list_insert(merge_commit, tail)->next; |
3961 | 0 | p += k; |
3962 | 0 | merge_arg_len = p - arg; |
3963 | 0 | } |
3964 | | |
3965 | 0 | if (!to_merge) { |
3966 | 0 | ret = error(_("nothing to merge: '%.*s'"), arg_len, arg); |
3967 | 0 | goto leave_merge; |
3968 | 0 | } |
3969 | | |
3970 | 0 | if (opts->have_squash_onto && |
3971 | 0 | oideq(&head_commit->object.oid, &opts->squash_onto)) { |
3972 | | /* |
3973 | | * When the user tells us to "merge" something into a |
3974 | | * "[new root]", let's simply fast-forward to the merge head. |
3975 | | */ |
3976 | 0 | rollback_lock_file(&lock); |
3977 | 0 | if (to_merge->next) |
3978 | 0 | ret = error(_("octopus merge cannot be executed on " |
3979 | 0 | "top of a [new root]")); |
3980 | 0 | else |
3981 | 0 | ret = fast_forward_to(r, &to_merge->item->object.oid, |
3982 | 0 | &head_commit->object.oid, 0, |
3983 | 0 | opts); |
3984 | 0 | goto leave_merge; |
3985 | 0 | } |
3986 | | |
3987 | | /* |
3988 | | * If HEAD is not identical to the first parent of the original merge |
3989 | | * commit, we cannot fast-forward. |
3990 | | */ |
3991 | 0 | can_fast_forward = opts->allow_ff && commit && commit->parents && |
3992 | 0 | oideq(&commit->parents->item->object.oid, |
3993 | 0 | &head_commit->object.oid); |
3994 | | |
3995 | | /* |
3996 | | * If any merge head is different from the original one, we cannot |
3997 | | * fast-forward. |
3998 | | */ |
3999 | 0 | if (can_fast_forward) { |
4000 | 0 | struct commit_list *p = commit->parents->next; |
4001 | |
|
4002 | 0 | for (j = to_merge; j && p; j = j->next, p = p->next) |
4003 | 0 | if (!oideq(&j->item->object.oid, |
4004 | 0 | &p->item->object.oid)) { |
4005 | 0 | can_fast_forward = 0; |
4006 | 0 | break; |
4007 | 0 | } |
4008 | | /* |
4009 | | * If the number of merge heads differs from the original merge |
4010 | | * commit, we cannot fast-forward. |
4011 | | */ |
4012 | 0 | if (j || p) |
4013 | 0 | can_fast_forward = 0; |
4014 | 0 | } |
4015 | |
|
4016 | 0 | if (can_fast_forward) { |
4017 | 0 | rollback_lock_file(&lock); |
4018 | 0 | ret = fast_forward_to(r, &commit->object.oid, |
4019 | 0 | &head_commit->object.oid, 0, opts); |
4020 | 0 | if (flags & TODO_EDIT_MERGE_MSG) |
4021 | 0 | goto fast_forward_edit; |
4022 | | |
4023 | 0 | goto leave_merge; |
4024 | 0 | } |
4025 | | |
4026 | 0 | if (commit) { |
4027 | 0 | const char *encoding = get_commit_output_encoding(); |
4028 | 0 | const char *message = repo_logmsg_reencode(r, commit, NULL, |
4029 | 0 | encoding); |
4030 | 0 | const char *body; |
4031 | 0 | int len; |
4032 | |
|
4033 | 0 | if (!message) { |
4034 | 0 | ret = error(_("could not get commit message of '%s'"), |
4035 | 0 | oid_to_hex(&commit->object.oid)); |
4036 | 0 | goto leave_merge; |
4037 | 0 | } |
4038 | 0 | write_author_script(message); |
4039 | 0 | find_commit_subject(message, &body); |
4040 | 0 | len = strlen(body); |
4041 | 0 | ret = write_message(body, len, git_path_merge_msg(r), 0); |
4042 | 0 | repo_unuse_commit_buffer(r, commit, message); |
4043 | 0 | if (ret) { |
4044 | 0 | error_errno(_("could not write '%s'"), |
4045 | 0 | git_path_merge_msg(r)); |
4046 | 0 | goto leave_merge; |
4047 | 0 | } |
4048 | 0 | } else { |
4049 | 0 | struct strbuf buf = STRBUF_INIT; |
4050 | 0 | int len; |
4051 | |
|
4052 | 0 | strbuf_addf(&buf, "author %s", git_author_info(0)); |
4053 | 0 | write_author_script(buf.buf); |
4054 | 0 | strbuf_reset(&buf); |
4055 | |
|
4056 | 0 | if (oneline_offset < arg_len) { |
4057 | 0 | p = arg + oneline_offset; |
4058 | 0 | len = arg_len - oneline_offset; |
4059 | 0 | } else { |
4060 | 0 | strbuf_addf(&buf, "Merge %s '%.*s'", |
4061 | 0 | to_merge->next ? "branches" : "branch", |
4062 | 0 | merge_arg_len, arg); |
4063 | 0 | p = buf.buf; |
4064 | 0 | len = buf.len; |
4065 | 0 | } |
4066 | |
|
4067 | 0 | ret = write_message(p, len, git_path_merge_msg(r), 0); |
4068 | 0 | strbuf_release(&buf); |
4069 | 0 | if (ret) { |
4070 | 0 | error_errno(_("could not write '%s'"), |
4071 | 0 | git_path_merge_msg(r)); |
4072 | 0 | goto leave_merge; |
4073 | 0 | } |
4074 | 0 | } |
4075 | | |
4076 | 0 | if (strategy || to_merge->next) { |
4077 | | /* Octopus merge */ |
4078 | 0 | struct child_process cmd = CHILD_PROCESS_INIT; |
4079 | |
|
4080 | 0 | if (read_env_script(&cmd.env)) { |
4081 | 0 | const char *gpg_opt = gpg_sign_opt_quoted(opts); |
4082 | |
|
4083 | 0 | ret = error(_(staged_changes_advice), gpg_opt, gpg_opt); |
4084 | 0 | goto leave_merge; |
4085 | 0 | } |
4086 | | |
4087 | 0 | if (opts->committer_date_is_author_date) |
4088 | 0 | strvec_pushf(&cmd.env, "GIT_COMMITTER_DATE=%s", |
4089 | 0 | opts->ignore_date ? |
4090 | 0 | "" : |
4091 | 0 | author_date_from_env(&cmd.env)); |
4092 | 0 | if (opts->ignore_date) |
4093 | 0 | strvec_push(&cmd.env, "GIT_AUTHOR_DATE="); |
4094 | |
|
4095 | 0 | cmd.git_cmd = 1; |
4096 | 0 | strvec_push(&cmd.args, "merge"); |
4097 | 0 | strvec_push(&cmd.args, "-s"); |
4098 | 0 | if (!strategy) |
4099 | 0 | strvec_push(&cmd.args, "octopus"); |
4100 | 0 | else { |
4101 | 0 | strvec_push(&cmd.args, strategy); |
4102 | 0 | for (k = 0; k < opts->xopts.nr; k++) |
4103 | 0 | strvec_pushf(&cmd.args, |
4104 | 0 | "-X%s", opts->xopts.v[k]); |
4105 | 0 | } |
4106 | 0 | if (!(flags & TODO_EDIT_MERGE_MSG)) |
4107 | 0 | strvec_push(&cmd.args, "--no-edit"); |
4108 | 0 | else |
4109 | 0 | strvec_push(&cmd.args, "--edit"); |
4110 | 0 | strvec_push(&cmd.args, "--no-ff"); |
4111 | 0 | strvec_push(&cmd.args, "--no-log"); |
4112 | 0 | strvec_push(&cmd.args, "--no-stat"); |
4113 | 0 | strvec_push(&cmd.args, "-F"); |
4114 | 0 | strvec_push(&cmd.args, git_path_merge_msg(r)); |
4115 | 0 | if (opts->gpg_sign) |
4116 | 0 | strvec_pushf(&cmd.args, "-S%s", opts->gpg_sign); |
4117 | 0 | else |
4118 | 0 | strvec_push(&cmd.args, "--no-gpg-sign"); |
4119 | | |
4120 | | /* Add the tips to be merged */ |
4121 | 0 | for (j = to_merge; j; j = j->next) |
4122 | 0 | strvec_push(&cmd.args, |
4123 | 0 | oid_to_hex(&j->item->object.oid)); |
4124 | |
|
4125 | 0 | strbuf_release(&ref_name); |
4126 | 0 | refs_delete_ref(get_main_ref_store(r), "", "CHERRY_PICK_HEAD", |
4127 | 0 | NULL, 0); |
4128 | 0 | rollback_lock_file(&lock); |
4129 | |
|
4130 | 0 | ret = run_command(&cmd); |
4131 | | |
4132 | | /* force re-reading of the cache */ |
4133 | 0 | if (!ret) { |
4134 | 0 | discard_index(r->index); |
4135 | 0 | if (repo_read_index(r) < 0) |
4136 | 0 | ret = error(_("could not read index")); |
4137 | 0 | } |
4138 | 0 | goto leave_merge; |
4139 | 0 | } |
4140 | | |
4141 | 0 | merge_commit = to_merge->item; |
4142 | 0 | bases = repo_get_merge_bases(r, head_commit, merge_commit); |
4143 | 0 | if (bases && oideq(&merge_commit->object.oid, |
4144 | 0 | &bases->item->object.oid)) { |
4145 | 0 | ret = 0; |
4146 | | /* skip merging an ancestor of HEAD */ |
4147 | 0 | goto leave_merge; |
4148 | 0 | } |
4149 | | |
4150 | 0 | write_message(oid_to_hex(&merge_commit->object.oid), the_hash_algo->hexsz, |
4151 | 0 | git_path_merge_head(r), 0); |
4152 | 0 | write_message("no-ff", 5, git_path_merge_mode(r), 0); |
4153 | |
|
4154 | 0 | bases = reverse_commit_list(bases); |
4155 | |
|
4156 | 0 | repo_read_index(r); |
4157 | 0 | init_merge_options(&o, r); |
4158 | 0 | o.branch1 = "HEAD"; |
4159 | 0 | o.branch2 = ref_name.buf; |
4160 | 0 | o.buffer_output = 2; |
4161 | |
|
4162 | 0 | if (!opts->strategy || !strcmp(opts->strategy, "ort")) { |
4163 | | /* |
4164 | | * TODO: Should use merge_incore_recursive() and |
4165 | | * merge_switch_to_result(), skipping the call to |
4166 | | * merge_switch_to_result() when we don't actually need to |
4167 | | * update the index and working copy immediately. |
4168 | | */ |
4169 | 0 | ret = merge_ort_recursive(&o, |
4170 | 0 | head_commit, merge_commit, bases, |
4171 | 0 | &i); |
4172 | 0 | } else { |
4173 | 0 | ret = merge_recursive(&o, head_commit, merge_commit, bases, |
4174 | 0 | &i); |
4175 | 0 | } |
4176 | 0 | if (ret <= 0) |
4177 | 0 | fputs(o.obuf.buf, stdout); |
4178 | 0 | strbuf_release(&o.obuf); |
4179 | 0 | if (ret < 0) { |
4180 | 0 | error(_("could not even attempt to merge '%.*s'"), |
4181 | 0 | merge_arg_len, arg); |
4182 | 0 | unlink(git_path_merge_msg(r)); |
4183 | 0 | goto leave_merge; |
4184 | 0 | } |
4185 | | /* |
4186 | | * The return value of merge_recursive() is 1 on clean, and 0 on |
4187 | | * unclean merge. |
4188 | | * |
4189 | | * Let's reverse that, so that do_merge() returns 0 upon success and |
4190 | | * 1 upon failed merge (keeping the return value -1 for the cases where |
4191 | | * we will want to reschedule the `merge` command). |
4192 | | */ |
4193 | 0 | ret = !ret; |
4194 | |
|
4195 | 0 | if (r->index->cache_changed && |
4196 | 0 | write_locked_index(r->index, &lock, COMMIT_LOCK)) { |
4197 | 0 | ret = error(_("merge: Unable to write new index file")); |
4198 | 0 | goto leave_merge; |
4199 | 0 | } |
4200 | | |
4201 | 0 | rollback_lock_file(&lock); |
4202 | 0 | if (ret) |
4203 | 0 | repo_rerere(r, opts->allow_rerere_auto); |
4204 | 0 | else |
4205 | | /* |
4206 | | * In case of problems, we now want to return a positive |
4207 | | * value (a negative one would indicate that the `merge` |
4208 | | * command needs to be rescheduled). |
4209 | | */ |
4210 | 0 | ret = !!run_git_commit(git_path_merge_msg(r), opts, |
4211 | 0 | run_commit_flags); |
4212 | |
|
4213 | 0 | if (!ret && flags & TODO_EDIT_MERGE_MSG) { |
4214 | 0 | fast_forward_edit: |
4215 | 0 | *check_todo = 1; |
4216 | 0 | run_commit_flags |= AMEND_MSG | EDIT_MSG | VERIFY_MSG; |
4217 | 0 | ret = !!run_git_commit(NULL, opts, run_commit_flags); |
4218 | 0 | } |
4219 | | |
4220 | |
|
4221 | 0 | leave_merge: |
4222 | 0 | strbuf_release(&ref_name); |
4223 | 0 | rollback_lock_file(&lock); |
4224 | 0 | free_commit_list(to_merge); |
4225 | 0 | return ret; |
4226 | 0 | } |
4227 | | |
4228 | | static int write_update_refs_state(struct string_list *refs_to_oids) |
4229 | 0 | { |
4230 | 0 | int result = 0; |
4231 | 0 | struct lock_file lock = LOCK_INIT; |
4232 | 0 | FILE *fp = NULL; |
4233 | 0 | struct string_list_item *item; |
4234 | 0 | char *path; |
4235 | |
|
4236 | 0 | path = rebase_path_update_refs(the_repository->gitdir); |
4237 | |
|
4238 | 0 | if (!refs_to_oids->nr) { |
4239 | 0 | if (unlink(path) && errno != ENOENT) |
4240 | 0 | result = error_errno(_("could not unlink: %s"), path); |
4241 | 0 | goto cleanup; |
4242 | 0 | } |
4243 | | |
4244 | 0 | if (safe_create_leading_directories(path)) { |
4245 | 0 | result = error(_("unable to create leading directories of %s"), |
4246 | 0 | path); |
4247 | 0 | goto cleanup; |
4248 | 0 | } |
4249 | | |
4250 | 0 | if (hold_lock_file_for_update(&lock, path, 0) < 0) { |
4251 | 0 | result = error(_("another 'rebase' process appears to be running; " |
4252 | 0 | "'%s.lock' already exists"), |
4253 | 0 | path); |
4254 | 0 | goto cleanup; |
4255 | 0 | } |
4256 | | |
4257 | 0 | fp = fdopen_lock_file(&lock, "w"); |
4258 | 0 | if (!fp) { |
4259 | 0 | result = error_errno(_("could not open '%s' for writing"), path); |
4260 | 0 | rollback_lock_file(&lock); |
4261 | 0 | goto cleanup; |
4262 | 0 | } |
4263 | | |
4264 | 0 | for_each_string_list_item(item, refs_to_oids) { |
4265 | 0 | struct update_ref_record *rec = item->util; |
4266 | 0 | fprintf(fp, "%s\n%s\n%s\n", item->string, |
4267 | 0 | oid_to_hex(&rec->before), oid_to_hex(&rec->after)); |
4268 | 0 | } |
4269 | |
|
4270 | 0 | result = commit_lock_file(&lock); |
4271 | |
|
4272 | 0 | cleanup: |
4273 | 0 | free(path); |
4274 | 0 | return result; |
4275 | 0 | } |
4276 | | |
4277 | | /* |
4278 | | * Parse the update-refs file for the current rebase, then remove the |
4279 | | * refs that do not appear in the todo_list (and have not had updated |
4280 | | * values stored) and add refs that are in the todo_list but not |
4281 | | * represented in the update-refs file. |
4282 | | * |
4283 | | * If there are changes to the update-refs list, then write the new state |
4284 | | * to disk. |
4285 | | */ |
4286 | | void todo_list_filter_update_refs(struct repository *r, |
4287 | | struct todo_list *todo_list) |
4288 | 0 | { |
4289 | 0 | int i; |
4290 | 0 | int updated = 0; |
4291 | 0 | struct string_list update_refs = STRING_LIST_INIT_DUP; |
4292 | |
|
4293 | 0 | sequencer_get_update_refs_state(r->gitdir, &update_refs); |
4294 | | |
4295 | | /* |
4296 | | * For each item in the update_refs list, if it has no updated |
4297 | | * value and does not appear in the todo_list, then remove it |
4298 | | * from the update_refs list. |
4299 | | */ |
4300 | 0 | for (i = 0; i < update_refs.nr; i++) { |
4301 | 0 | int j; |
4302 | 0 | int found = 0; |
4303 | 0 | const char *ref = update_refs.items[i].string; |
4304 | 0 | size_t reflen = strlen(ref); |
4305 | 0 | struct update_ref_record *rec = update_refs.items[i].util; |
4306 | | |
4307 | | /* OID already stored as updated. */ |
4308 | 0 | if (!is_null_oid(&rec->after)) |
4309 | 0 | continue; |
4310 | | |
4311 | 0 | for (j = 0; !found && j < todo_list->nr; j++) { |
4312 | 0 | struct todo_item *item = &todo_list->items[j]; |
4313 | 0 | const char *arg = todo_list->buf.buf + item->arg_offset; |
4314 | |
|
4315 | 0 | if (item->command != TODO_UPDATE_REF) |
4316 | 0 | continue; |
4317 | | |
4318 | 0 | if (item->arg_len != reflen || |
4319 | 0 | strncmp(arg, ref, reflen)) |
4320 | 0 | continue; |
4321 | | |
4322 | 0 | found = 1; |
4323 | 0 | } |
4324 | |
|
4325 | 0 | if (!found) { |
4326 | 0 | free(update_refs.items[i].string); |
4327 | 0 | free(update_refs.items[i].util); |
4328 | |
|
4329 | 0 | update_refs.nr--; |
4330 | 0 | MOVE_ARRAY(update_refs.items + i, update_refs.items + i + 1, update_refs.nr - i); |
4331 | |
|
4332 | 0 | updated = 1; |
4333 | 0 | i--; |
4334 | 0 | } |
4335 | 0 | } |
4336 | | |
4337 | | /* |
4338 | | * For each todo_item, check if its ref is in the update_refs list. |
4339 | | * If not, then add it as an un-updated ref. |
4340 | | */ |
4341 | 0 | for (i = 0; i < todo_list->nr; i++) { |
4342 | 0 | struct todo_item *item = &todo_list->items[i]; |
4343 | 0 | const char *arg = todo_list->buf.buf + item->arg_offset; |
4344 | 0 | int j, found = 0; |
4345 | |
|
4346 | 0 | if (item->command != TODO_UPDATE_REF) |
4347 | 0 | continue; |
4348 | | |
4349 | 0 | for (j = 0; !found && j < update_refs.nr; j++) { |
4350 | 0 | const char *ref = update_refs.items[j].string; |
4351 | |
|
4352 | 0 | found = strlen(ref) == item->arg_len && |
4353 | 0 | !strncmp(ref, arg, item->arg_len); |
4354 | 0 | } |
4355 | |
|
4356 | 0 | if (!found) { |
4357 | 0 | struct string_list_item *inserted; |
4358 | 0 | struct strbuf argref = STRBUF_INIT; |
4359 | |
|
4360 | 0 | strbuf_add(&argref, arg, item->arg_len); |
4361 | 0 | inserted = string_list_insert(&update_refs, argref.buf); |
4362 | 0 | inserted->util = init_update_ref_record(argref.buf); |
4363 | 0 | strbuf_release(&argref); |
4364 | 0 | updated = 1; |
4365 | 0 | } |
4366 | 0 | } |
4367 | |
|
4368 | 0 | if (updated) |
4369 | 0 | write_update_refs_state(&update_refs); |
4370 | 0 | string_list_clear(&update_refs, 1); |
4371 | 0 | } |
4372 | | |
4373 | | static int do_update_ref(struct repository *r, const char *refname) |
4374 | 0 | { |
4375 | 0 | struct string_list_item *item; |
4376 | 0 | struct string_list list = STRING_LIST_INIT_DUP; |
4377 | |
|
4378 | 0 | if (sequencer_get_update_refs_state(r->gitdir, &list)) |
4379 | 0 | return -1; |
4380 | | |
4381 | 0 | for_each_string_list_item(item, &list) { |
4382 | 0 | if (!strcmp(item->string, refname)) { |
4383 | 0 | struct update_ref_record *rec = item->util; |
4384 | 0 | if (read_ref("HEAD", &rec->after)) |
4385 | 0 | return -1; |
4386 | 0 | break; |
4387 | 0 | } |
4388 | 0 | } |
4389 | | |
4390 | 0 | write_update_refs_state(&list); |
4391 | 0 | string_list_clear(&list, 1); |
4392 | 0 | return 0; |
4393 | 0 | } |
4394 | | |
4395 | | static int do_update_refs(struct repository *r, int quiet) |
4396 | 0 | { |
4397 | 0 | int res = 0; |
4398 | 0 | struct string_list_item *item; |
4399 | 0 | struct string_list refs_to_oids = STRING_LIST_INIT_DUP; |
4400 | 0 | struct ref_store *refs = get_main_ref_store(r); |
4401 | 0 | struct strbuf update_msg = STRBUF_INIT; |
4402 | 0 | struct strbuf error_msg = STRBUF_INIT; |
4403 | |
|
4404 | 0 | if ((res = sequencer_get_update_refs_state(r->gitdir, &refs_to_oids))) |
4405 | 0 | return res; |
4406 | | |
4407 | 0 | for_each_string_list_item(item, &refs_to_oids) { |
4408 | 0 | struct update_ref_record *rec = item->util; |
4409 | 0 | int loop_res; |
4410 | |
|
4411 | 0 | loop_res = refs_update_ref(refs, "rewritten during rebase", |
4412 | 0 | item->string, |
4413 | 0 | &rec->after, &rec->before, |
4414 | 0 | 0, UPDATE_REFS_MSG_ON_ERR); |
4415 | 0 | res |= loop_res; |
4416 | |
|
4417 | 0 | if (quiet) |
4418 | 0 | continue; |
4419 | | |
4420 | 0 | if (loop_res) |
4421 | 0 | strbuf_addf(&error_msg, "\t%s\n", item->string); |
4422 | 0 | else |
4423 | 0 | strbuf_addf(&update_msg, "\t%s\n", item->string); |
4424 | 0 | } |
4425 | |
|
4426 | 0 | if (!quiet && |
4427 | 0 | (update_msg.len || error_msg.len)) { |
4428 | 0 | fprintf(stderr, |
4429 | 0 | _("Updated the following refs with %s:\n%s"), |
4430 | 0 | "--update-refs", |
4431 | 0 | update_msg.buf); |
4432 | |
|
4433 | 0 | if (res) |
4434 | 0 | fprintf(stderr, |
4435 | 0 | _("Failed to update the following refs with %s:\n%s"), |
4436 | 0 | "--update-refs", |
4437 | 0 | error_msg.buf); |
4438 | 0 | } |
4439 | |
|
4440 | 0 | string_list_clear(&refs_to_oids, 1); |
4441 | 0 | strbuf_release(&update_msg); |
4442 | 0 | strbuf_release(&error_msg); |
4443 | 0 | return res; |
4444 | 0 | } |
4445 | | |
4446 | | static int is_final_fixup(struct todo_list *todo_list) |
4447 | 0 | { |
4448 | 0 | int i = todo_list->current; |
4449 | |
|
4450 | 0 | if (!is_fixup(todo_list->items[i].command)) |
4451 | 0 | return 0; |
4452 | | |
4453 | 0 | while (++i < todo_list->nr) |
4454 | 0 | if (is_fixup(todo_list->items[i].command)) |
4455 | 0 | return 0; |
4456 | 0 | else if (!is_noop(todo_list->items[i].command)) |
4457 | 0 | break; |
4458 | 0 | return 1; |
4459 | 0 | } |
4460 | | |
4461 | | static enum todo_command peek_command(struct todo_list *todo_list, int offset) |
4462 | 0 | { |
4463 | 0 | int i; |
4464 | |
|
4465 | 0 | for (i = todo_list->current + offset; i < todo_list->nr; i++) |
4466 | 0 | if (!is_noop(todo_list->items[i].command)) |
4467 | 0 | return todo_list->items[i].command; |
4468 | | |
4469 | 0 | return -1; |
4470 | 0 | } |
4471 | | |
4472 | | void create_autostash(struct repository *r, const char *path) |
4473 | 0 | { |
4474 | 0 | struct strbuf buf = STRBUF_INIT; |
4475 | 0 | struct lock_file lock_file = LOCK_INIT; |
4476 | 0 | int fd; |
4477 | |
|
4478 | 0 | fd = repo_hold_locked_index(r, &lock_file, 0); |
4479 | 0 | refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL); |
4480 | 0 | if (0 <= fd) |
4481 | 0 | repo_update_index_if_able(r, &lock_file); |
4482 | 0 | rollback_lock_file(&lock_file); |
4483 | |
|
4484 | 0 | if (has_unstaged_changes(r, 1) || |
4485 | 0 | has_uncommitted_changes(r, 1)) { |
4486 | 0 | struct child_process stash = CHILD_PROCESS_INIT; |
4487 | 0 | struct reset_head_opts ropts = { .flags = RESET_HEAD_HARD }; |
4488 | 0 | struct object_id oid; |
4489 | |
|
4490 | 0 | strvec_pushl(&stash.args, |
4491 | 0 | "stash", "create", "autostash", NULL); |
4492 | 0 | stash.git_cmd = 1; |
4493 | 0 | stash.no_stdin = 1; |
4494 | 0 | strbuf_reset(&buf); |
4495 | 0 | if (capture_command(&stash, &buf, GIT_MAX_HEXSZ)) |
4496 | 0 | die(_("Cannot autostash")); |
4497 | 0 | strbuf_trim_trailing_newline(&buf); |
4498 | 0 | if (repo_get_oid(r, buf.buf, &oid)) |
4499 | 0 | die(_("Unexpected stash response: '%s'"), |
4500 | 0 | buf.buf); |
4501 | 0 | strbuf_reset(&buf); |
4502 | 0 | strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV); |
4503 | |
|
4504 | 0 | if (safe_create_leading_directories_const(path)) |
4505 | 0 | die(_("Could not create directory for '%s'"), |
4506 | 0 | path); |
4507 | 0 | write_file(path, "%s", oid_to_hex(&oid)); |
4508 | 0 | printf(_("Created autostash: %s\n"), buf.buf); |
4509 | 0 | if (reset_head(r, &ropts) < 0) |
4510 | 0 | die(_("could not reset --hard")); |
4511 | 0 | discard_index(r->index); |
4512 | 0 | if (repo_read_index(r) < 0) |
4513 | 0 | die(_("could not read index")); |
4514 | 0 | } |
4515 | 0 | strbuf_release(&buf); |
4516 | 0 | } |
4517 | | |
4518 | | static int apply_save_autostash_oid(const char *stash_oid, int attempt_apply) |
4519 | 0 | { |
4520 | 0 | struct child_process child = CHILD_PROCESS_INIT; |
4521 | 0 | int ret = 0; |
4522 | |
|
4523 | 0 | if (attempt_apply) { |
4524 | 0 | child.git_cmd = 1; |
4525 | 0 | child.no_stdout = 1; |
4526 | 0 | child.no_stderr = 1; |
4527 | 0 | strvec_push(&child.args, "stash"); |
4528 | 0 | strvec_push(&child.args, "apply"); |
4529 | 0 | strvec_push(&child.args, stash_oid); |
4530 | 0 | ret = run_command(&child); |
4531 | 0 | } |
4532 | |
|
4533 | 0 | if (attempt_apply && !ret) |
4534 | 0 | fprintf(stderr, _("Applied autostash.\n")); |
4535 | 0 | else { |
4536 | 0 | struct child_process store = CHILD_PROCESS_INIT; |
4537 | |
|
4538 | 0 | store.git_cmd = 1; |
4539 | 0 | strvec_push(&store.args, "stash"); |
4540 | 0 | strvec_push(&store.args, "store"); |
4541 | 0 | strvec_push(&store.args, "-m"); |
4542 | 0 | strvec_push(&store.args, "autostash"); |
4543 | 0 | strvec_push(&store.args, "-q"); |
4544 | 0 | strvec_push(&store.args, stash_oid); |
4545 | 0 | if (run_command(&store)) |
4546 | 0 | ret = error(_("cannot store %s"), stash_oid); |
4547 | 0 | else |
4548 | 0 | fprintf(stderr, |
4549 | 0 | _("%s\n" |
4550 | 0 | "Your changes are safe in the stash.\n" |
4551 | 0 | "You can run \"git stash pop\" or" |
4552 | 0 | " \"git stash drop\" at any time.\n"), |
4553 | 0 | attempt_apply ? |
4554 | 0 | _("Applying autostash resulted in conflicts.") : |
4555 | 0 | _("Autostash exists; creating a new stash entry.")); |
4556 | 0 | } |
4557 | |
|
4558 | 0 | return ret; |
4559 | 0 | } |
4560 | | |
4561 | | static int apply_save_autostash(const char *path, int attempt_apply) |
4562 | 7.22k | { |
4563 | 7.22k | struct strbuf stash_oid = STRBUF_INIT; |
4564 | 7.22k | int ret = 0; |
4565 | | |
4566 | 7.22k | if (!read_oneliner(&stash_oid, path, |
4567 | 7.22k | READ_ONELINER_SKIP_IF_EMPTY)) { |
4568 | 7.22k | strbuf_release(&stash_oid); |
4569 | 7.22k | return 0; |
4570 | 7.22k | } |
4571 | 0 | strbuf_trim(&stash_oid); |
4572 | |
|
4573 | 0 | ret = apply_save_autostash_oid(stash_oid.buf, attempt_apply); |
4574 | |
|
4575 | 0 | unlink(path); |
4576 | 0 | strbuf_release(&stash_oid); |
4577 | 0 | return ret; |
|