Commit | Line | Data |
---|---|---|
26ae337b | 1 | #include "cache.h" |
697cc8ef | 2 | #include "lockfile.h" |
26ae337b | 3 | #include "sequencer.h" |
26ae337b | 4 | #include "dir.h" |
043a4492 RR |
5 | #include "object.h" |
6 | #include "commit.h" | |
7 | #include "tag.h" | |
8 | #include "run-command.h" | |
9 | #include "exec_cmd.h" | |
10 | #include "utf8.h" | |
11 | #include "cache-tree.h" | |
12 | #include "diff.h" | |
13 | #include "revision.h" | |
14 | #include "rerere.h" | |
15 | #include "merge-recursive.h" | |
16 | #include "refs.h" | |
b27cfb0d | 17 | #include "argv-array.h" |
043a4492 RR |
18 | |
19 | #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION" | |
26ae337b | 20 | |
5ed75e2a | 21 | const char sign_off_header[] = "Signed-off-by: "; |
cd650a4e | 22 | static const char cherry_picked_prefix[] = "(cherry picked from commit "; |
5ed75e2a | 23 | |
8a2a0f53 JS |
24 | GIT_PATH_FUNC(git_path_seq_dir, "sequencer") |
25 | ||
26 | static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo") | |
27 | static GIT_PATH_FUNC(git_path_opts_file, "sequencer/opts") | |
28 | static GIT_PATH_FUNC(git_path_head_file, "sequencer/head") | |
f932729c | 29 | |
285abf56 JS |
30 | static const char *get_dir(const struct replay_opts *opts) |
31 | { | |
32 | return git_path_seq_dir(); | |
33 | } | |
34 | ||
c0246501 JS |
35 | static const char *get_todo_path(const struct replay_opts *opts) |
36 | { | |
37 | return git_path_todo_file(); | |
38 | } | |
39 | ||
b971e04f BC |
40 | static int is_rfc2822_line(const char *buf, int len) |
41 | { | |
42 | int i; | |
43 | ||
44 | for (i = 0; i < len; i++) { | |
45 | int ch = buf[i]; | |
46 | if (ch == ':') | |
47 | return 1; | |
48 | if (!isalnum(ch) && ch != '-') | |
49 | break; | |
50 | } | |
51 | ||
52 | return 0; | |
53 | } | |
54 | ||
55 | static int is_cherry_picked_from_line(const char *buf, int len) | |
56 | { | |
57 | /* | |
58 | * We only care that it looks roughly like (cherry picked from ...) | |
59 | */ | |
60 | return len > strlen(cherry_picked_prefix) + 1 && | |
59556548 | 61 | starts_with(buf, cherry_picked_prefix) && buf[len - 1] == ')'; |
b971e04f BC |
62 | } |
63 | ||
bab4d109 BC |
64 | /* |
65 | * Returns 0 for non-conforming footer | |
66 | * Returns 1 for conforming footer | |
67 | * Returns 2 when sob exists within conforming footer | |
68 | * Returns 3 when sob exists within conforming footer as last entry | |
69 | */ | |
70 | static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, | |
71 | int ignore_footer) | |
b971e04f BC |
72 | { |
73 | char prev; | |
74 | int i, k; | |
75 | int len = sb->len - ignore_footer; | |
76 | const char *buf = sb->buf; | |
bab4d109 | 77 | int found_sob = 0; |
b971e04f BC |
78 | |
79 | /* footer must end with newline */ | |
80 | if (!len || buf[len - 1] != '\n') | |
81 | return 0; | |
82 | ||
83 | prev = '\0'; | |
84 | for (i = len - 1; i > 0; i--) { | |
85 | char ch = buf[i]; | |
86 | if (prev == '\n' && ch == '\n') /* paragraph break */ | |
87 | break; | |
88 | prev = ch; | |
89 | } | |
90 | ||
91 | /* require at least one blank line */ | |
92 | if (prev != '\n' || buf[i] != '\n') | |
93 | return 0; | |
94 | ||
95 | /* advance to start of last paragraph */ | |
96 | while (i < len - 1 && buf[i] == '\n') | |
97 | i++; | |
98 | ||
99 | for (; i < len; i = k) { | |
bab4d109 BC |
100 | int found_rfc2822; |
101 | ||
b971e04f BC |
102 | for (k = i; k < len && buf[k] != '\n'; k++) |
103 | ; /* do nothing */ | |
104 | k++; | |
105 | ||
bab4d109 BC |
106 | found_rfc2822 = is_rfc2822_line(buf + i, k - i - 1); |
107 | if (found_rfc2822 && sob && | |
108 | !strncmp(buf + i, sob->buf, sob->len)) | |
109 | found_sob = k; | |
110 | ||
111 | if (!(found_rfc2822 || | |
112 | is_cherry_picked_from_line(buf + i, k - i - 1))) | |
b971e04f BC |
113 | return 0; |
114 | } | |
bab4d109 BC |
115 | if (found_sob == i) |
116 | return 3; | |
117 | if (found_sob) | |
118 | return 2; | |
b971e04f BC |
119 | return 1; |
120 | } | |
5ed75e2a | 121 | |
2863584f | 122 | int sequencer_remove_state(struct replay_opts *opts) |
26ae337b | 123 | { |
285abf56 | 124 | struct strbuf dir = STRBUF_INIT; |
03a4e260 JS |
125 | int i; |
126 | ||
127 | free(opts->gpg_sign); | |
128 | free(opts->strategy); | |
129 | for (i = 0; i < opts->xopts_nr; i++) | |
130 | free(opts->xopts[i]); | |
131 | free(opts->xopts); | |
26ae337b | 132 | |
285abf56 JS |
133 | strbuf_addf(&dir, "%s", get_dir(opts)); |
134 | remove_dir_recursively(&dir, 0); | |
135 | strbuf_release(&dir); | |
2863584f JS |
136 | |
137 | return 0; | |
26ae337b | 138 | } |
043a4492 RR |
139 | |
140 | static const char *action_name(const struct replay_opts *opts) | |
141 | { | |
142 | return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick"; | |
143 | } | |
144 | ||
043a4492 RR |
145 | struct commit_message { |
146 | char *parent_label; | |
7b35eaf8 JK |
147 | char *label; |
148 | char *subject; | |
043a4492 RR |
149 | const char *message; |
150 | }; | |
151 | ||
39755964 JS |
152 | static const char *short_commit_name(struct commit *commit) |
153 | { | |
154 | return find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV); | |
155 | } | |
156 | ||
043a4492 RR |
157 | static int get_message(struct commit *commit, struct commit_message *out) |
158 | { | |
043a4492 | 159 | const char *abbrev, *subject; |
7b35eaf8 | 160 | int subject_len; |
043a4492 | 161 | |
7b35eaf8 | 162 | out->message = logmsg_reencode(commit, NULL, get_commit_output_encoding()); |
39755964 | 163 | abbrev = short_commit_name(commit); |
043a4492 RR |
164 | |
165 | subject_len = find_commit_subject(out->message, &subject); | |
166 | ||
7b35eaf8 JK |
167 | out->subject = xmemdupz(subject, subject_len); |
168 | out->label = xstrfmt("%s... %s", abbrev, out->subject); | |
169 | out->parent_label = xstrfmt("parent of %s", out->label); | |
170 | ||
043a4492 RR |
171 | return 0; |
172 | } | |
173 | ||
d74a4e57 | 174 | static void free_message(struct commit *commit, struct commit_message *msg) |
043a4492 RR |
175 | { |
176 | free(msg->parent_label); | |
7b35eaf8 JK |
177 | free(msg->label); |
178 | free(msg->subject); | |
b66103c3 | 179 | unuse_commit_buffer(commit, msg->message); |
043a4492 RR |
180 | } |
181 | ||
ed727b19 | 182 | static void print_advice(int show_hint, struct replay_opts *opts) |
043a4492 RR |
183 | { |
184 | char *msg = getenv("GIT_CHERRY_PICK_HELP"); | |
185 | ||
186 | if (msg) { | |
187 | fprintf(stderr, "%s\n", msg); | |
188 | /* | |
41ccfdd9 | 189 | * A conflict has occurred but the porcelain |
043a4492 RR |
190 | * (typically rebase --interactive) wants to take care |
191 | * of the commit itself so remove CHERRY_PICK_HEAD | |
192 | */ | |
f932729c | 193 | unlink(git_path_cherry_pick_head()); |
043a4492 RR |
194 | return; |
195 | } | |
196 | ||
ed727b19 PH |
197 | if (show_hint) { |
198 | if (opts->no_commit) | |
199 | advise(_("after resolving the conflicts, mark the corrected paths\n" | |
200 | "with 'git add <paths>' or 'git rm <paths>'")); | |
201 | else | |
202 | advise(_("after resolving the conflicts, mark the corrected paths\n" | |
203 | "with 'git add <paths>' or 'git rm <paths>'\n" | |
204 | "and commit the result with 'git commit'")); | |
205 | } | |
043a4492 RR |
206 | } |
207 | ||
4ef3d8f0 | 208 | static int write_message(struct strbuf *msgbuf, const char *filename) |
043a4492 RR |
209 | { |
210 | static struct lock_file msg_file; | |
211 | ||
4ef3d8f0 JS |
212 | int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0); |
213 | if (msg_fd < 0) | |
214 | return error_errno(_("Could not lock '%s'"), filename); | |
043a4492 | 215 | if (write_in_full(msg_fd, msgbuf->buf, msgbuf->len) < 0) |
4ef3d8f0 | 216 | return error_errno(_("Could not write to %s"), filename); |
043a4492 RR |
217 | strbuf_release(msgbuf); |
218 | if (commit_lock_file(&msg_file) < 0) | |
4ef3d8f0 JS |
219 | return error(_("Error wrapping up %s."), filename); |
220 | ||
221 | return 0; | |
043a4492 RR |
222 | } |
223 | ||
224 | static struct tree *empty_tree(void) | |
225 | { | |
cba595bd | 226 | return lookup_tree(EMPTY_TREE_SHA1_BIN); |
043a4492 RR |
227 | } |
228 | ||
229 | static int error_dirty_index(struct replay_opts *opts) | |
230 | { | |
231 | if (read_cache_unmerged()) | |
232 | return error_resolve_conflict(action_name(opts)); | |
233 | ||
e635d5ce JS |
234 | error(_("Your local changes would be overwritten by %s."), |
235 | action_name(opts)); | |
043a4492 RR |
236 | |
237 | if (advice_commit_before_merge) | |
238 | advise(_("Commit your changes or stash them to proceed.")); | |
239 | return -1; | |
240 | } | |
241 | ||
334ae397 | 242 | static int fast_forward_to(const unsigned char *to, const unsigned char *from, |
eb4be1cb | 243 | int unborn, struct replay_opts *opts) |
043a4492 | 244 | { |
d668d16c | 245 | struct ref_transaction *transaction; |
eb4be1cb | 246 | struct strbuf sb = STRBUF_INIT; |
d668d16c | 247 | struct strbuf err = STRBUF_INIT; |
043a4492 RR |
248 | |
249 | read_cache(); | |
db699a8a | 250 | if (checkout_fast_forward(from, to, 1)) |
0e408fc3 | 251 | return -1; /* the callee should have complained already */ |
651ab9f5 | 252 | |
e8d7c390 | 253 | strbuf_addf(&sb, _("%s: fast-forward"), action_name(opts)); |
d668d16c RS |
254 | |
255 | transaction = ref_transaction_begin(&err); | |
256 | if (!transaction || | |
257 | ref_transaction_update(transaction, "HEAD", | |
258 | to, unborn ? null_sha1 : from, | |
1d147bdf | 259 | 0, sb.buf, &err) || |
db7516ab | 260 | ref_transaction_commit(transaction, &err)) { |
d668d16c RS |
261 | ref_transaction_free(transaction); |
262 | error("%s", err.buf); | |
263 | strbuf_release(&sb); | |
264 | strbuf_release(&err); | |
265 | return -1; | |
266 | } | |
651ab9f5 | 267 | |
eb4be1cb | 268 | strbuf_release(&sb); |
d668d16c RS |
269 | strbuf_release(&err); |
270 | ref_transaction_free(transaction); | |
271 | return 0; | |
043a4492 RR |
272 | } |
273 | ||
75c961b7 JH |
274 | void append_conflicts_hint(struct strbuf *msgbuf) |
275 | { | |
276 | int i; | |
277 | ||
261f315b JH |
278 | strbuf_addch(msgbuf, '\n'); |
279 | strbuf_commented_addf(msgbuf, "Conflicts:\n"); | |
75c961b7 JH |
280 | for (i = 0; i < active_nr;) { |
281 | const struct cache_entry *ce = active_cache[i++]; | |
282 | if (ce_stage(ce)) { | |
261f315b | 283 | strbuf_commented_addf(msgbuf, "\t%s\n", ce->name); |
75c961b7 JH |
284 | while (i < active_nr && !strcmp(ce->name, |
285 | active_cache[i]->name)) | |
286 | i++; | |
287 | } | |
288 | } | |
289 | } | |
290 | ||
043a4492 RR |
291 | static int do_recursive_merge(struct commit *base, struct commit *next, |
292 | const char *base_label, const char *next_label, | |
293 | unsigned char *head, struct strbuf *msgbuf, | |
294 | struct replay_opts *opts) | |
295 | { | |
296 | struct merge_options o; | |
297 | struct tree *result, *next_tree, *base_tree, *head_tree; | |
03b86647 | 298 | int clean; |
03a4e260 | 299 | char **xopt; |
043a4492 RR |
300 | static struct lock_file index_lock; |
301 | ||
03b86647 | 302 | hold_locked_index(&index_lock, 1); |
043a4492 RR |
303 | |
304 | read_cache(); | |
305 | ||
306 | init_merge_options(&o); | |
307 | o.ancestor = base ? base_label : "(empty tree)"; | |
308 | o.branch1 = "HEAD"; | |
309 | o.branch2 = next ? next_label : "(empty tree)"; | |
310 | ||
311 | head_tree = parse_tree_indirect(head); | |
312 | next_tree = next ? next->tree : empty_tree(); | |
313 | base_tree = base ? base->tree : empty_tree(); | |
314 | ||
315 | for (xopt = opts->xopts; xopt != opts->xopts + opts->xopts_nr; xopt++) | |
316 | parse_merge_opt(&o, *xopt); | |
317 | ||
318 | clean = merge_trees(&o, | |
319 | head_tree, | |
320 | next_tree, base_tree, &result); | |
548009c0 | 321 | strbuf_release(&o.obuf); |
f241ff0d JS |
322 | if (clean < 0) |
323 | return clean; | |
043a4492 RR |
324 | |
325 | if (active_cache_changed && | |
03b86647 | 326 | write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) |
043a4492 | 327 | /* TRANSLATORS: %s will be "revert" or "cherry-pick" */ |
c527b55e JS |
328 | return error(_("%s: Unable to write new index file"), |
329 | action_name(opts)); | |
043a4492 RR |
330 | rollback_lock_file(&index_lock); |
331 | ||
5ed75e2a | 332 | if (opts->signoff) |
bab4d109 | 333 | append_signoff(msgbuf, 0, 0); |
5ed75e2a | 334 | |
75c961b7 JH |
335 | if (!clean) |
336 | append_conflicts_hint(msgbuf); | |
043a4492 RR |
337 | |
338 | return !clean; | |
339 | } | |
340 | ||
b27cfb0d NH |
341 | static int is_index_unchanged(void) |
342 | { | |
343 | unsigned char head_sha1[20]; | |
344 | struct commit *head_commit; | |
345 | ||
7695d118 | 346 | if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_sha1, NULL)) |
b27cfb0d NH |
347 | return error(_("Could not resolve HEAD commit\n")); |
348 | ||
349 | head_commit = lookup_commit(head_sha1); | |
4b580061 NH |
350 | |
351 | /* | |
352 | * If head_commit is NULL, check_commit, called from | |
353 | * lookup_commit, would have indicated that head_commit is not | |
354 | * a commit object already. parse_commit() will return failure | |
355 | * without further complaints in such a case. Otherwise, if | |
356 | * the commit is invalid, parse_commit() will complain. So | |
357 | * there is nothing for us to say here. Just return failure. | |
358 | */ | |
359 | if (parse_commit(head_commit)) | |
360 | return -1; | |
b27cfb0d NH |
361 | |
362 | if (!active_cache_tree) | |
363 | active_cache_tree = cache_tree(); | |
364 | ||
365 | if (!cache_tree_fully_valid(active_cache_tree)) | |
d0cfc3e8 | 366 | if (cache_tree_update(&the_index, 0)) |
b27cfb0d NH |
367 | return error(_("Unable to update cache tree\n")); |
368 | ||
ed1c9977 | 369 | return !hashcmp(active_cache_tree->sha1, head_commit->tree->object.oid.hash); |
b27cfb0d NH |
370 | } |
371 | ||
043a4492 RR |
372 | /* |
373 | * If we are cherry-pick, and if the merge did not result in | |
374 | * hand-editing, we will hit this commit and inherit the original | |
375 | * author date and name. | |
376 | * If we are revert, or if our cherry-pick results in a hand merge, | |
377 | * we had better say that the current user is responsible for that. | |
378 | */ | |
ac2b0e8f JH |
379 | static int run_git_commit(const char *defmsg, struct replay_opts *opts, |
380 | int allow_empty) | |
043a4492 | 381 | { |
b27cfb0d NH |
382 | struct argv_array array; |
383 | int rc; | |
17d65f03 | 384 | const char *value; |
b27cfb0d NH |
385 | |
386 | argv_array_init(&array); | |
387 | argv_array_push(&array, "commit"); | |
388 | argv_array_push(&array, "-n"); | |
043a4492 | 389 | |
3bdd5522 JK |
390 | if (opts->gpg_sign) |
391 | argv_array_pushf(&array, "-S%s", opts->gpg_sign); | |
043a4492 | 392 | if (opts->signoff) |
b27cfb0d | 393 | argv_array_push(&array, "-s"); |
043a4492 | 394 | if (!opts->edit) { |
b27cfb0d NH |
395 | argv_array_push(&array, "-F"); |
396 | argv_array_push(&array, defmsg); | |
17d65f03 MG |
397 | if (!opts->signoff && |
398 | !opts->record_origin && | |
399 | git_config_get_value("commit.cleanup", &value)) | |
400 | argv_array_push(&array, "--cleanup=verbatim"); | |
043a4492 | 401 | } |
b27cfb0d | 402 | |
ac2b0e8f | 403 | if (allow_empty) |
b27cfb0d | 404 | argv_array_push(&array, "--allow-empty"); |
df478b74 | 405 | |
4bee9584 CW |
406 | if (opts->allow_empty_message) |
407 | argv_array_push(&array, "--allow-empty-message"); | |
408 | ||
b27cfb0d NH |
409 | rc = run_command_v_opt(array.argv, RUN_GIT_CMD); |
410 | argv_array_clear(&array); | |
411 | return rc; | |
412 | } | |
413 | ||
414 | static int is_original_commit_empty(struct commit *commit) | |
415 | { | |
416 | const unsigned char *ptree_sha1; | |
417 | ||
418 | if (parse_commit(commit)) | |
419 | return error(_("Could not parse commit %s\n"), | |
f2fd0760 | 420 | oid_to_hex(&commit->object.oid)); |
b27cfb0d NH |
421 | if (commit->parents) { |
422 | struct commit *parent = commit->parents->item; | |
423 | if (parse_commit(parent)) | |
424 | return error(_("Could not parse parent commit %s\n"), | |
f2fd0760 | 425 | oid_to_hex(&parent->object.oid)); |
ed1c9977 | 426 | ptree_sha1 = parent->tree->object.oid.hash; |
b27cfb0d NH |
427 | } else { |
428 | ptree_sha1 = EMPTY_TREE_SHA1_BIN; /* commit is root */ | |
043a4492 | 429 | } |
043a4492 | 430 | |
ed1c9977 | 431 | return !hashcmp(ptree_sha1, commit->tree->object.oid.hash); |
043a4492 RR |
432 | } |
433 | ||
ac2b0e8f JH |
434 | /* |
435 | * Do we run "git commit" with "--allow-empty"? | |
436 | */ | |
437 | static int allow_empty(struct replay_opts *opts, struct commit *commit) | |
438 | { | |
439 | int index_unchanged, empty_commit; | |
440 | ||
441 | /* | |
442 | * Three cases: | |
443 | * | |
444 | * (1) we do not allow empty at all and error out. | |
445 | * | |
446 | * (2) we allow ones that were initially empty, but | |
447 | * forbid the ones that become empty; | |
448 | * | |
449 | * (3) we allow both. | |
450 | */ | |
451 | if (!opts->allow_empty) | |
452 | return 0; /* let "git commit" barf as necessary */ | |
453 | ||
454 | index_unchanged = is_index_unchanged(); | |
455 | if (index_unchanged < 0) | |
456 | return index_unchanged; | |
457 | if (!index_unchanged) | |
458 | return 0; /* we do not have to say --allow-empty */ | |
459 | ||
460 | if (opts->keep_redundant_commits) | |
461 | return 1; | |
462 | ||
463 | empty_commit = is_original_commit_empty(commit); | |
464 | if (empty_commit < 0) | |
465 | return empty_commit; | |
466 | if (!empty_commit) | |
467 | return 0; | |
468 | else | |
469 | return 1; | |
470 | } | |
471 | ||
004fefa7 JS |
472 | enum todo_command { |
473 | TODO_PICK = 0, | |
474 | TODO_REVERT | |
475 | }; | |
476 | ||
477 | static const char *todo_command_strings[] = { | |
478 | "pick", | |
479 | "revert" | |
480 | }; | |
481 | ||
482 | static const char *command_to_string(const enum todo_command command) | |
483 | { | |
484 | if (command < ARRAY_SIZE(todo_command_strings)) | |
485 | return todo_command_strings[command]; | |
486 | die("Unknown command: %d", command); | |
487 | } | |
488 | ||
489 | ||
490 | static int do_pick_commit(enum todo_command command, struct commit *commit, | |
491 | struct replay_opts *opts) | |
043a4492 RR |
492 | { |
493 | unsigned char head[20]; | |
494 | struct commit *base, *next, *parent; | |
495 | const char *base_label, *next_label; | |
d74a4e57 | 496 | struct commit_message msg = { NULL, NULL, NULL, NULL }; |
043a4492 | 497 | struct strbuf msgbuf = STRBUF_INIT; |
c8d1351d | 498 | int res, unborn = 0, allow; |
043a4492 RR |
499 | |
500 | if (opts->no_commit) { | |
501 | /* | |
502 | * We do not intend to commit immediately. We just want to | |
503 | * merge the differences in, so let's compute the tree | |
504 | * that represents the "current" state for merge-recursive | |
505 | * to work on. | |
506 | */ | |
507 | if (write_cache_as_tree(head, 0, NULL)) | |
f74087f6 | 508 | return error(_("Your index file is unmerged.")); |
043a4492 | 509 | } else { |
334ae397 MZ |
510 | unborn = get_sha1("HEAD", head); |
511 | if (unborn) | |
512 | hashcpy(head, EMPTY_TREE_SHA1_BIN); | |
513 | if (index_differs_from(unborn ? EMPTY_TREE_SHA1_HEX : "HEAD", 0)) | |
043a4492 RR |
514 | return error_dirty_index(opts); |
515 | } | |
516 | discard_cache(); | |
517 | ||
518 | if (!commit->parents) { | |
519 | parent = NULL; | |
520 | } | |
521 | else if (commit->parents->next) { | |
522 | /* Reverting or cherry-picking a merge commit */ | |
523 | int cnt; | |
524 | struct commit_list *p; | |
525 | ||
526 | if (!opts->mainline) | |
527 | return error(_("Commit %s is a merge but no -m option was given."), | |
f2fd0760 | 528 | oid_to_hex(&commit->object.oid)); |
043a4492 RR |
529 | |
530 | for (cnt = 1, p = commit->parents; | |
531 | cnt != opts->mainline && p; | |
532 | cnt++) | |
533 | p = p->next; | |
534 | if (cnt != opts->mainline || !p) | |
535 | return error(_("Commit %s does not have parent %d"), | |
f2fd0760 | 536 | oid_to_hex(&commit->object.oid), opts->mainline); |
043a4492 RR |
537 | parent = p->item; |
538 | } else if (0 < opts->mainline) | |
539 | return error(_("Mainline was specified but commit %s is not a merge."), | |
f2fd0760 | 540 | oid_to_hex(&commit->object.oid)); |
043a4492 RR |
541 | else |
542 | parent = commit->parents->item; | |
543 | ||
334ae397 | 544 | if (opts->allow_ff && |
ed1c9977 | 545 | ((parent && !hashcmp(parent->object.oid.hash, head)) || |
334ae397 | 546 | (!parent && unborn))) |
ed1c9977 | 547 | return fast_forward_to(commit->object.oid.hash, head, unborn, opts); |
043a4492 RR |
548 | |
549 | if (parent && parse_commit(parent) < 0) | |
004fefa7 JS |
550 | /* TRANSLATORS: The first %s will be a "todo" command like |
551 | "revert" or "pick", the second %s a SHA1. */ | |
043a4492 | 552 | return error(_("%s: cannot parse parent commit %s"), |
004fefa7 JS |
553 | command_to_string(command), |
554 | oid_to_hex(&parent->object.oid)); | |
043a4492 RR |
555 | |
556 | if (get_message(commit, &msg) != 0) | |
557 | return error(_("Cannot get commit message for %s"), | |
f2fd0760 | 558 | oid_to_hex(&commit->object.oid)); |
043a4492 RR |
559 | |
560 | /* | |
561 | * "commit" is an existing commit. We would want to apply | |
562 | * the difference it introduces since its first parent "prev" | |
563 | * on top of the current HEAD if we are cherry-pick. Or the | |
564 | * reverse of it if we are revert. | |
565 | */ | |
566 | ||
004fefa7 | 567 | if (command == TODO_REVERT) { |
043a4492 RR |
568 | base = commit; |
569 | base_label = msg.label; | |
570 | next = parent; | |
571 | next_label = msg.parent_label; | |
572 | strbuf_addstr(&msgbuf, "Revert \""); | |
573 | strbuf_addstr(&msgbuf, msg.subject); | |
574 | strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit "); | |
f2fd0760 | 575 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
043a4492 RR |
576 | |
577 | if (commit->parents && commit->parents->next) { | |
578 | strbuf_addstr(&msgbuf, ", reversing\nchanges made to "); | |
f2fd0760 | 579 | strbuf_addstr(&msgbuf, oid_to_hex(&parent->object.oid)); |
043a4492 RR |
580 | } |
581 | strbuf_addstr(&msgbuf, ".\n"); | |
582 | } else { | |
583 | const char *p; | |
584 | ||
585 | base = parent; | |
586 | base_label = msg.parent_label; | |
587 | next = commit; | |
588 | next_label = msg.label; | |
589 | ||
590 | /* | |
591 | * Append the commit log message to msgbuf; it starts | |
592 | * after the tree, parent, author, committer | |
593 | * information followed by "\n\n". | |
594 | */ | |
595 | p = strstr(msg.message, "\n\n"); | |
88ef402f JS |
596 | if (p) |
597 | strbuf_addstr(&msgbuf, skip_blank_lines(p + 2)); | |
043a4492 RR |
598 | |
599 | if (opts->record_origin) { | |
bab4d109 | 600 | if (!has_conforming_footer(&msgbuf, NULL, 0)) |
b971e04f | 601 | strbuf_addch(&msgbuf, '\n'); |
cd650a4e | 602 | strbuf_addstr(&msgbuf, cherry_picked_prefix); |
f2fd0760 | 603 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
043a4492 RR |
604 | strbuf_addstr(&msgbuf, ")\n"); |
605 | } | |
606 | } | |
607 | ||
004fefa7 | 608 | if (!opts->strategy || !strcmp(opts->strategy, "recursive") || command == TODO_REVERT) { |
043a4492 RR |
609 | res = do_recursive_merge(base, next, base_label, next_label, |
610 | head, &msgbuf, opts); | |
f241ff0d JS |
611 | if (res < 0) |
612 | return res; | |
4ef3d8f0 | 613 | res |= write_message(&msgbuf, git_path_merge_msg()); |
043a4492 RR |
614 | } else { |
615 | struct commit_list *common = NULL; | |
616 | struct commit_list *remotes = NULL; | |
617 | ||
4ef3d8f0 | 618 | res = write_message(&msgbuf, git_path_merge_msg()); |
043a4492 RR |
619 | |
620 | commit_list_insert(base, &common); | |
621 | commit_list_insert(next, &remotes); | |
03a4e260 JS |
622 | res |= try_merge_command(opts->strategy, |
623 | opts->xopts_nr, (const char **)opts->xopts, | |
043a4492 RR |
624 | common, sha1_to_hex(head), remotes); |
625 | free_commit_list(common); | |
626 | free_commit_list(remotes); | |
627 | } | |
628 | ||
629 | /* | |
630 | * If the merge was clean or if it failed due to conflict, we write | |
631 | * CHERRY_PICK_HEAD for the subsequent invocation of commit to use. | |
632 | * However, if the merge did not even start, then we don't want to | |
633 | * write it at all. | |
634 | */ | |
004fefa7 | 635 | if (command == TODO_PICK && !opts->no_commit && (res == 0 || res == 1) && |
dbfad033 JS |
636 | update_ref(NULL, "CHERRY_PICK_HEAD", commit->object.oid.hash, NULL, |
637 | REF_NODEREF, UPDATE_REFS_MSG_ON_ERR)) | |
638 | res = -1; | |
004fefa7 | 639 | if (command == TODO_REVERT && ((opts->no_commit && res == 0) || res == 1) && |
dbfad033 JS |
640 | update_ref(NULL, "REVERT_HEAD", commit->object.oid.hash, NULL, |
641 | REF_NODEREF, UPDATE_REFS_MSG_ON_ERR)) | |
642 | res = -1; | |
043a4492 RR |
643 | |
644 | if (res) { | |
004fefa7 | 645 | error(command == TODO_REVERT |
043a4492 RR |
646 | ? _("could not revert %s... %s") |
647 | : _("could not apply %s... %s"), | |
39755964 | 648 | short_commit_name(commit), msg.subject); |
ed727b19 | 649 | print_advice(res == 1, opts); |
043a4492 | 650 | rerere(opts->allow_rerere_auto); |
c8d1351d | 651 | goto leave; |
043a4492 RR |
652 | } |
653 | ||
c8d1351d | 654 | allow = allow_empty(opts, commit); |
706728a3 FC |
655 | if (allow < 0) { |
656 | res = allow; | |
657 | goto leave; | |
658 | } | |
c8d1351d | 659 | if (!opts->no_commit) |
f932729c | 660 | res = run_git_commit(git_path_merge_msg(), opts, allow); |
c8d1351d FC |
661 | |
662 | leave: | |
d74a4e57 | 663 | free_message(commit, &msg); |
043a4492 RR |
664 | |
665 | return res; | |
666 | } | |
667 | ||
c3e8618c | 668 | static int prepare_revs(struct replay_opts *opts) |
043a4492 | 669 | { |
a73e22e9 MZ |
670 | /* |
671 | * picking (but not reverting) ranges (but not individual revisions) | |
672 | * should be done in reverse | |
673 | */ | |
674 | if (opts->action == REPLAY_PICK && !opts->revs->no_walk) | |
043a4492 RR |
675 | opts->revs->reverse ^= 1; |
676 | ||
677 | if (prepare_revision_walk(opts->revs)) | |
c3e8618c | 678 | return error(_("revision walk setup failed")); |
043a4492 RR |
679 | |
680 | if (!opts->revs->commits) | |
c3e8618c JS |
681 | return error(_("empty commit set passed")); |
682 | return 0; | |
043a4492 RR |
683 | } |
684 | ||
0d9c6dc9 | 685 | static int read_and_refresh_cache(struct replay_opts *opts) |
043a4492 RR |
686 | { |
687 | static struct lock_file index_lock; | |
688 | int index_fd = hold_locked_index(&index_lock, 0); | |
49fb937e JS |
689 | if (read_index_preload(&the_index, NULL) < 0) { |
690 | rollback_lock_file(&index_lock); | |
0d9c6dc9 JS |
691 | return error(_("git %s: failed to read the index"), |
692 | action_name(opts)); | |
49fb937e | 693 | } |
043a4492 | 694 | refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL); |
33c297aa | 695 | if (the_index.cache_changed && index_fd >= 0) { |
49fb937e JS |
696 | if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) { |
697 | rollback_lock_file(&index_lock); | |
0d9c6dc9 JS |
698 | return error(_("git %s: failed to refresh the index"), |
699 | action_name(opts)); | |
49fb937e | 700 | } |
043a4492 RR |
701 | } |
702 | rollback_lock_file(&index_lock); | |
0d9c6dc9 | 703 | return 0; |
043a4492 RR |
704 | } |
705 | ||
004fefa7 JS |
706 | struct todo_item { |
707 | enum todo_command command; | |
708 | struct commit *commit; | |
709 | size_t offset_in_buf; | |
710 | }; | |
711 | ||
712 | struct todo_list { | |
713 | struct strbuf buf; | |
714 | struct todo_item *items; | |
715 | int nr, alloc, current; | |
716 | }; | |
717 | ||
718 | #define TODO_LIST_INIT { STRBUF_INIT } | |
719 | ||
720 | static void todo_list_release(struct todo_list *todo_list) | |
043a4492 | 721 | { |
004fefa7 JS |
722 | strbuf_release(&todo_list->buf); |
723 | free(todo_list->items); | |
724 | todo_list->items = NULL; | |
725 | todo_list->nr = todo_list->alloc = 0; | |
726 | } | |
043a4492 | 727 | |
004fefa7 JS |
728 | static struct todo_item *append_new_todo(struct todo_list *todo_list) |
729 | { | |
730 | ALLOC_GROW(todo_list->items, todo_list->nr + 1, todo_list->alloc); | |
731 | return todo_list->items + todo_list->nr++; | |
043a4492 RR |
732 | } |
733 | ||
004fefa7 | 734 | static int parse_insn_line(struct todo_item *item, const char *bol, char *eol) |
043a4492 RR |
735 | { |
736 | unsigned char commit_sha1[20]; | |
043a4492 | 737 | char *end_of_object_name; |
004fefa7 JS |
738 | int i, saved, status, padding; |
739 | ||
740 | for (i = 0; i < ARRAY_SIZE(todo_command_strings); i++) | |
741 | if (skip_prefix(bol, todo_command_strings[i], &bol)) { | |
742 | item->command = i; | |
743 | break; | |
744 | } | |
745 | if (i >= ARRAY_SIZE(todo_command_strings)) | |
746 | return -1; | |
043a4492 RR |
747 | |
748 | /* Eat up extra spaces/ tabs before object name */ | |
749 | padding = strspn(bol, " \t"); | |
750 | if (!padding) | |
004fefa7 | 751 | return -1; |
043a4492 RR |
752 | bol += padding; |
753 | ||
004fefa7 | 754 | end_of_object_name = (char *) bol + strcspn(bol, " \t\n"); |
043a4492 RR |
755 | saved = *end_of_object_name; |
756 | *end_of_object_name = '\0'; | |
757 | status = get_sha1(bol, commit_sha1); | |
758 | *end_of_object_name = saved; | |
759 | ||
043a4492 | 760 | if (status < 0) |
004fefa7 | 761 | return -1; |
043a4492 | 762 | |
004fefa7 JS |
763 | item->commit = lookup_commit_reference(commit_sha1); |
764 | return !item->commit; | |
043a4492 RR |
765 | } |
766 | ||
004fefa7 | 767 | static int parse_insn_buffer(char *buf, struct todo_list *todo_list) |
043a4492 | 768 | { |
004fefa7 JS |
769 | struct todo_item *item; |
770 | char *p = buf, *next_p; | |
771 | int i, res = 0; | |
043a4492 | 772 | |
004fefa7 | 773 | for (i = 1; *p; i++, p = next_p) { |
043a4492 | 774 | char *eol = strchrnul(p, '\n'); |
004fefa7 JS |
775 | |
776 | next_p = *eol ? eol + 1 /* skip LF */ : eol; | |
777 | ||
6307041d JS |
778 | if (p != eol && eol[-1] == '\r') |
779 | eol--; /* strip Carriage Return */ | |
780 | ||
004fefa7 JS |
781 | item = append_new_todo(todo_list); |
782 | item->offset_in_buf = p - todo_list->buf.buf; | |
783 | if (parse_insn_line(item, p, eol)) { | |
784 | res = error(_("Invalid line %d: %.*s"), | |
785 | i, (int)(eol - p), p); | |
786 | item->command = -1; | |
787 | } | |
043a4492 | 788 | } |
004fefa7 | 789 | if (!todo_list->nr) |
043a4492 | 790 | return error(_("No commits parsed.")); |
004fefa7 | 791 | return res; |
043a4492 RR |
792 | } |
793 | ||
004fefa7 | 794 | static int read_populate_todo(struct todo_list *todo_list, |
043a4492 RR |
795 | struct replay_opts *opts) |
796 | { | |
c0246501 | 797 | const char *todo_file = get_todo_path(opts); |
043a4492 RR |
798 | int fd, res; |
799 | ||
004fefa7 | 800 | strbuf_reset(&todo_list->buf); |
c0246501 | 801 | fd = open(todo_file, O_RDONLY); |
043a4492 | 802 | if (fd < 0) |
c0246501 | 803 | return error_errno(_("Could not open %s"), todo_file); |
004fefa7 | 804 | if (strbuf_read(&todo_list->buf, fd, 0) < 0) { |
043a4492 | 805 | close(fd); |
c0246501 | 806 | return error(_("Could not read %s."), todo_file); |
043a4492 RR |
807 | } |
808 | close(fd); | |
809 | ||
004fefa7 JS |
810 | res = parse_insn_buffer(todo_list->buf.buf, todo_list); |
811 | if (!res) { | |
812 | enum todo_command valid = | |
813 | opts->action == REPLAY_PICK ? TODO_PICK : TODO_REVERT; | |
814 | int i; | |
815 | ||
816 | for (i = 0; i < todo_list->nr; i++) | |
817 | if (valid == todo_list->items[i].command) | |
818 | continue; | |
819 | else if (valid == TODO_PICK) | |
820 | return error(_("Cannot cherry-pick during a revert.")); | |
821 | else | |
822 | return error(_("Cannot revert during a cherry-pick.")); | |
823 | } | |
824 | ||
043a4492 | 825 | if (res) |
c0246501 | 826 | return error(_("Unusable instruction sheet: %s"), todo_file); |
0ae42a03 | 827 | return 0; |
043a4492 RR |
828 | } |
829 | ||
03a4e260 JS |
830 | static int git_config_string_dup(char **dest, |
831 | const char *var, const char *value) | |
832 | { | |
833 | if (!value) | |
834 | return config_error_nonbool(var); | |
835 | free(*dest); | |
836 | *dest = xstrdup(value); | |
837 | return 0; | |
838 | } | |
839 | ||
043a4492 RR |
840 | static int populate_opts_cb(const char *key, const char *value, void *data) |
841 | { | |
842 | struct replay_opts *opts = data; | |
843 | int error_flag = 1; | |
844 | ||
845 | if (!value) | |
846 | error_flag = 0; | |
847 | else if (!strcmp(key, "options.no-commit")) | |
848 | opts->no_commit = git_config_bool_or_int(key, value, &error_flag); | |
849 | else if (!strcmp(key, "options.edit")) | |
850 | opts->edit = git_config_bool_or_int(key, value, &error_flag); | |
851 | else if (!strcmp(key, "options.signoff")) | |
852 | opts->signoff = git_config_bool_or_int(key, value, &error_flag); | |
853 | else if (!strcmp(key, "options.record-origin")) | |
854 | opts->record_origin = git_config_bool_or_int(key, value, &error_flag); | |
855 | else if (!strcmp(key, "options.allow-ff")) | |
856 | opts->allow_ff = git_config_bool_or_int(key, value, &error_flag); | |
857 | else if (!strcmp(key, "options.mainline")) | |
858 | opts->mainline = git_config_int(key, value); | |
859 | else if (!strcmp(key, "options.strategy")) | |
03a4e260 | 860 | git_config_string_dup(&opts->strategy, key, value); |
3253553e | 861 | else if (!strcmp(key, "options.gpg-sign")) |
03a4e260 | 862 | git_config_string_dup(&opts->gpg_sign, key, value); |
043a4492 RR |
863 | else if (!strcmp(key, "options.strategy-option")) { |
864 | ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc); | |
865 | opts->xopts[opts->xopts_nr++] = xstrdup(value); | |
866 | } else | |
867 | return error(_("Invalid key: %s"), key); | |
868 | ||
869 | if (!error_flag) | |
870 | return error(_("Invalid value for %s: %s"), key, value); | |
871 | ||
872 | return 0; | |
873 | } | |
874 | ||
5adf9bdc | 875 | static int read_populate_opts(struct replay_opts *opts) |
043a4492 | 876 | { |
f932729c | 877 | if (!file_exists(git_path_opts_file())) |
0d00da7b JS |
878 | return 0; |
879 | /* | |
880 | * The function git_parse_source(), called from git_config_from_file(), | |
881 | * may die() in case of a syntactically incorrect file. We do not care | |
882 | * about this case, though, because we wrote that file ourselves, so we | |
883 | * are pretty certain that it is syntactically correct. | |
884 | */ | |
5adf9bdc | 885 | if (git_config_from_file(populate_opts_cb, git_path_opts_file(), opts) < 0) |
0d00da7b JS |
886 | return error(_("Malformed options sheet: %s"), |
887 | git_path_opts_file()); | |
888 | return 0; | |
043a4492 RR |
889 | } |
890 | ||
004fefa7 | 891 | static int walk_revs_populate_todo(struct todo_list *todo_list, |
043a4492 RR |
892 | struct replay_opts *opts) |
893 | { | |
004fefa7 JS |
894 | enum todo_command command = opts->action == REPLAY_PICK ? |
895 | TODO_PICK : TODO_REVERT; | |
896 | const char *command_string = todo_command_strings[command]; | |
043a4492 | 897 | struct commit *commit; |
043a4492 | 898 | |
34b0528b JS |
899 | if (prepare_revs(opts)) |
900 | return -1; | |
043a4492 | 901 | |
004fefa7 JS |
902 | while ((commit = get_revision(opts->revs))) { |
903 | struct todo_item *item = append_new_todo(todo_list); | |
904 | const char *commit_buffer = get_commit_buffer(commit, NULL); | |
905 | const char *subject; | |
906 | int subject_len; | |
907 | ||
908 | item->command = command; | |
909 | item->commit = commit; | |
910 | item->offset_in_buf = todo_list->buf.len; | |
911 | subject_len = find_commit_subject(commit_buffer, &subject); | |
912 | strbuf_addf(&todo_list->buf, "%s %s %.*s\n", command_string, | |
913 | short_commit_name(commit), subject_len, subject); | |
914 | unuse_commit_buffer(commit, commit_buffer); | |
915 | } | |
34b0528b | 916 | return 0; |
043a4492 RR |
917 | } |
918 | ||
919 | static int create_seq_dir(void) | |
920 | { | |
f932729c | 921 | if (file_exists(git_path_seq_dir())) { |
043a4492 RR |
922 | error(_("a cherry-pick or revert is already in progress")); |
923 | advise(_("try \"git cherry-pick (--continue | --quit | --abort)\"")); | |
924 | return -1; | |
925 | } | |
f932729c | 926 | else if (mkdir(git_path_seq_dir(), 0777) < 0) |
f6e82b0d JS |
927 | return error_errno(_("Could not create sequencer directory %s"), |
928 | git_path_seq_dir()); | |
043a4492 RR |
929 | return 0; |
930 | } | |
931 | ||
311fd397 | 932 | static int save_head(const char *head) |
043a4492 | 933 | { |
043a4492 RR |
934 | static struct lock_file head_lock; |
935 | struct strbuf buf = STRBUF_INIT; | |
936 | int fd; | |
937 | ||
311fd397 JS |
938 | fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), 0); |
939 | if (fd < 0) { | |
940 | rollback_lock_file(&head_lock); | |
941 | return error_errno(_("Could not lock HEAD")); | |
942 | } | |
043a4492 | 943 | strbuf_addf(&buf, "%s\n", head); |
311fd397 JS |
944 | if (write_in_full(fd, buf.buf, buf.len) < 0) { |
945 | rollback_lock_file(&head_lock); | |
946 | return error_errno(_("Could not write to %s"), | |
947 | git_path_head_file()); | |
948 | } | |
949 | if (commit_lock_file(&head_lock) < 0) { | |
950 | rollback_lock_file(&head_lock); | |
951 | return error(_("Error wrapping up %s."), git_path_head_file()); | |
952 | } | |
953 | return 0; | |
043a4492 RR |
954 | } |
955 | ||
956 | static int reset_for_rollback(const unsigned char *sha1) | |
957 | { | |
958 | const char *argv[4]; /* reset --merge <arg> + NULL */ | |
959 | argv[0] = "reset"; | |
960 | argv[1] = "--merge"; | |
961 | argv[2] = sha1_to_hex(sha1); | |
962 | argv[3] = NULL; | |
963 | return run_command_v_opt(argv, RUN_GIT_CMD); | |
964 | } | |
965 | ||
966 | static int rollback_single_pick(void) | |
967 | { | |
968 | unsigned char head_sha1[20]; | |
969 | ||
f932729c JK |
970 | if (!file_exists(git_path_cherry_pick_head()) && |
971 | !file_exists(git_path_revert_head())) | |
043a4492 | 972 | return error(_("no cherry-pick or revert in progress")); |
7695d118 | 973 | if (read_ref_full("HEAD", 0, head_sha1, NULL)) |
043a4492 RR |
974 | return error(_("cannot resolve HEAD")); |
975 | if (is_null_sha1(head_sha1)) | |
976 | return error(_("cannot abort from a branch yet to be born")); | |
977 | return reset_for_rollback(head_sha1); | |
978 | } | |
979 | ||
2863584f | 980 | int sequencer_rollback(struct replay_opts *opts) |
043a4492 | 981 | { |
043a4492 RR |
982 | FILE *f; |
983 | unsigned char sha1[20]; | |
984 | struct strbuf buf = STRBUF_INIT; | |
985 | ||
f932729c | 986 | f = fopen(git_path_head_file(), "r"); |
043a4492 RR |
987 | if (!f && errno == ENOENT) { |
988 | /* | |
989 | * There is no multiple-cherry-pick in progress. | |
990 | * If CHERRY_PICK_HEAD or REVERT_HEAD indicates | |
991 | * a single-cherry-pick in progress, abort that. | |
992 | */ | |
993 | return rollback_single_pick(); | |
994 | } | |
995 | if (!f) | |
6c979c74 | 996 | return error_errno(_("cannot open %s"), git_path_head_file()); |
8f309aeb | 997 | if (strbuf_getline_lf(&buf, f)) { |
f932729c JK |
998 | error(_("cannot read %s: %s"), git_path_head_file(), |
999 | ferror(f) ? strerror(errno) : _("unexpected end of file")); | |
043a4492 RR |
1000 | fclose(f); |
1001 | goto fail; | |
1002 | } | |
1003 | fclose(f); | |
1004 | if (get_sha1_hex(buf.buf, sha1) || buf.buf[40] != '\0') { | |
1005 | error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"), | |
f932729c | 1006 | git_path_head_file()); |
043a4492 RR |
1007 | goto fail; |
1008 | } | |
0f974e21 MG |
1009 | if (is_null_sha1(sha1)) { |
1010 | error(_("cannot abort from a branch yet to be born")); | |
1011 | goto fail; | |
1012 | } | |
043a4492 RR |
1013 | if (reset_for_rollback(sha1)) |
1014 | goto fail; | |
043a4492 | 1015 | strbuf_release(&buf); |
2863584f | 1016 | return sequencer_remove_state(opts); |
043a4492 RR |
1017 | fail: |
1018 | strbuf_release(&buf); | |
1019 | return -1; | |
1020 | } | |
1021 | ||
004fefa7 | 1022 | static int save_todo(struct todo_list *todo_list, struct replay_opts *opts) |
043a4492 | 1023 | { |
043a4492 | 1024 | static struct lock_file todo_lock; |
004fefa7 JS |
1025 | const char *todo_path = get_todo_path(opts); |
1026 | int next = todo_list->current, offset, fd; | |
043a4492 | 1027 | |
004fefa7 | 1028 | fd = hold_lock_file_for_update(&todo_lock, todo_path, 0); |
221675de | 1029 | if (fd < 0) |
004fefa7 JS |
1030 | return error_errno(_("Could not lock '%s'"), todo_path); |
1031 | offset = next < todo_list->nr ? | |
1032 | todo_list->items[next].offset_in_buf : todo_list->buf.len; | |
1033 | if (write_in_full(fd, todo_list->buf.buf + offset, | |
1034 | todo_list->buf.len - offset) < 0) | |
1035 | return error_errno(_("Could not write to '%s'"), todo_path); | |
1036 | if (commit_lock_file(&todo_lock) < 0) | |
1037 | return error(_("Error wrapping up %s."), todo_path); | |
221675de | 1038 | return 0; |
043a4492 RR |
1039 | } |
1040 | ||
88d5a271 | 1041 | static int save_opts(struct replay_opts *opts) |
043a4492 | 1042 | { |
f932729c | 1043 | const char *opts_file = git_path_opts_file(); |
88d5a271 | 1044 | int res = 0; |
043a4492 RR |
1045 | |
1046 | if (opts->no_commit) | |
88d5a271 | 1047 | res |= git_config_set_in_file_gently(opts_file, "options.no-commit", "true"); |
043a4492 | 1048 | if (opts->edit) |
88d5a271 | 1049 | res |= git_config_set_in_file_gently(opts_file, "options.edit", "true"); |
043a4492 | 1050 | if (opts->signoff) |
88d5a271 | 1051 | res |= git_config_set_in_file_gently(opts_file, "options.signoff", "true"); |
043a4492 | 1052 | if (opts->record_origin) |
88d5a271 | 1053 | res |= git_config_set_in_file_gently(opts_file, "options.record-origin", "true"); |
043a4492 | 1054 | if (opts->allow_ff) |
88d5a271 | 1055 | res |= git_config_set_in_file_gently(opts_file, "options.allow-ff", "true"); |
043a4492 RR |
1056 | if (opts->mainline) { |
1057 | struct strbuf buf = STRBUF_INIT; | |
1058 | strbuf_addf(&buf, "%d", opts->mainline); | |
88d5a271 | 1059 | res |= git_config_set_in_file_gently(opts_file, "options.mainline", buf.buf); |
043a4492 RR |
1060 | strbuf_release(&buf); |
1061 | } | |
1062 | if (opts->strategy) | |
88d5a271 | 1063 | res |= git_config_set_in_file_gently(opts_file, "options.strategy", opts->strategy); |
3253553e | 1064 | if (opts->gpg_sign) |
88d5a271 | 1065 | res |= git_config_set_in_file_gently(opts_file, "options.gpg-sign", opts->gpg_sign); |
043a4492 RR |
1066 | if (opts->xopts) { |
1067 | int i; | |
1068 | for (i = 0; i < opts->xopts_nr; i++) | |
88d5a271 | 1069 | res |= git_config_set_multivar_in_file_gently(opts_file, |
043a4492 RR |
1070 | "options.strategy-option", |
1071 | opts->xopts[i], "^$", 0); | |
1072 | } | |
88d5a271 | 1073 | return res; |
043a4492 RR |
1074 | } |
1075 | ||
004fefa7 | 1076 | static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts) |
043a4492 | 1077 | { |
043a4492 RR |
1078 | int res; |
1079 | ||
1080 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); | |
1081 | if (opts->allow_ff) | |
1082 | assert(!(opts->signoff || opts->no_commit || | |
1083 | opts->record_origin || opts->edit)); | |
0d9c6dc9 JS |
1084 | if (read_and_refresh_cache(opts)) |
1085 | return -1; | |
043a4492 | 1086 | |
004fefa7 JS |
1087 | while (todo_list->current < todo_list->nr) { |
1088 | struct todo_item *item = todo_list->items + todo_list->current; | |
1089 | if (save_todo(todo_list, opts)) | |
221675de | 1090 | return -1; |
004fefa7 JS |
1091 | res = do_pick_commit(item->command, item->commit, opts); |
1092 | todo_list->current++; | |
043a4492 RR |
1093 | if (res) |
1094 | return res; | |
1095 | } | |
1096 | ||
1097 | /* | |
1098 | * Sequence of picks finished successfully; cleanup by | |
1099 | * removing the .git/sequencer directory | |
1100 | */ | |
2863584f | 1101 | return sequencer_remove_state(opts); |
043a4492 RR |
1102 | } |
1103 | ||
1104 | static int continue_single_pick(void) | |
1105 | { | |
1106 | const char *argv[] = { "commit", NULL }; | |
1107 | ||
f932729c JK |
1108 | if (!file_exists(git_path_cherry_pick_head()) && |
1109 | !file_exists(git_path_revert_head())) | |
043a4492 RR |
1110 | return error(_("no cherry-pick or revert in progress")); |
1111 | return run_command_v_opt(argv, RUN_GIT_CMD); | |
1112 | } | |
1113 | ||
2863584f | 1114 | int sequencer_continue(struct replay_opts *opts) |
043a4492 | 1115 | { |
004fefa7 JS |
1116 | struct todo_list todo_list = TODO_LIST_INIT; |
1117 | int res; | |
043a4492 | 1118 | |
2863584f JS |
1119 | if (read_and_refresh_cache(opts)) |
1120 | return -1; | |
1121 | ||
c0246501 | 1122 | if (!file_exists(get_todo_path(opts))) |
043a4492 | 1123 | return continue_single_pick(); |
004fefa7 | 1124 | if (read_populate_opts(opts)) |
0ae42a03 | 1125 | return -1; |
004fefa7 JS |
1126 | if ((res = read_populate_todo(&todo_list, opts))) |
1127 | goto release_todo_list; | |
043a4492 RR |
1128 | |
1129 | /* Verify that the conflict has been resolved */ | |
f932729c JK |
1130 | if (file_exists(git_path_cherry_pick_head()) || |
1131 | file_exists(git_path_revert_head())) { | |
004fefa7 JS |
1132 | res = continue_single_pick(); |
1133 | if (res) | |
1134 | goto release_todo_list; | |
043a4492 | 1135 | } |
004fefa7 JS |
1136 | if (index_differs_from("HEAD", 0)) { |
1137 | res = error_dirty_index(opts); | |
1138 | goto release_todo_list; | |
1139 | } | |
1140 | todo_list.current++; | |
1141 | res = pick_commits(&todo_list, opts); | |
1142 | release_todo_list: | |
1143 | todo_list_release(&todo_list); | |
1144 | return res; | |
043a4492 RR |
1145 | } |
1146 | ||
1147 | static int single_pick(struct commit *cmit, struct replay_opts *opts) | |
1148 | { | |
1149 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); | |
004fefa7 JS |
1150 | return do_pick_commit(opts->action == REPLAY_PICK ? |
1151 | TODO_PICK : TODO_REVERT, cmit, opts); | |
043a4492 RR |
1152 | } |
1153 | ||
1154 | int sequencer_pick_revisions(struct replay_opts *opts) | |
1155 | { | |
004fefa7 | 1156 | struct todo_list todo_list = TODO_LIST_INIT; |
043a4492 | 1157 | unsigned char sha1[20]; |
004fefa7 | 1158 | int i, res; |
043a4492 | 1159 | |
2863584f | 1160 | assert(opts->revs); |
0d9c6dc9 JS |
1161 | if (read_and_refresh_cache(opts)) |
1162 | return -1; | |
043a4492 | 1163 | |
21246dbb MV |
1164 | for (i = 0; i < opts->revs->pending.nr; i++) { |
1165 | unsigned char sha1[20]; | |
1166 | const char *name = opts->revs->pending.objects[i].name; | |
1167 | ||
1168 | /* This happens when using --stdin. */ | |
1169 | if (!strlen(name)) | |
1170 | continue; | |
1171 | ||
1172 | if (!get_sha1(name, sha1)) { | |
7c0b0d8d JH |
1173 | if (!lookup_commit_reference_gently(sha1, 1)) { |
1174 | enum object_type type = sha1_object_info(sha1, NULL); | |
b9b946d4 JS |
1175 | return error(_("%s: can't cherry-pick a %s"), |
1176 | name, typename(type)); | |
7c0b0d8d | 1177 | } |
21246dbb | 1178 | } else |
b9b946d4 | 1179 | return error(_("%s: bad revision"), name); |
21246dbb MV |
1180 | } |
1181 | ||
043a4492 RR |
1182 | /* |
1183 | * If we were called as "git cherry-pick <commit>", just | |
1184 | * cherry-pick/revert it, set CHERRY_PICK_HEAD / | |
1185 | * REVERT_HEAD, and don't touch the sequencer state. | |
1186 | * This means it is possible to cherry-pick in the middle | |
1187 | * of a cherry-pick sequence. | |
1188 | */ | |
1189 | if (opts->revs->cmdline.nr == 1 && | |
1190 | opts->revs->cmdline.rev->whence == REV_CMD_REV && | |
1191 | opts->revs->no_walk && | |
1192 | !opts->revs->cmdline.rev->flags) { | |
1193 | struct commit *cmit; | |
1194 | if (prepare_revision_walk(opts->revs)) | |
b9b946d4 | 1195 | return error(_("revision walk setup failed")); |
043a4492 RR |
1196 | cmit = get_revision(opts->revs); |
1197 | if (!cmit || get_revision(opts->revs)) | |
b9b946d4 | 1198 | return error("BUG: expected exactly one commit from walk"); |
043a4492 RR |
1199 | return single_pick(cmit, opts); |
1200 | } | |
1201 | ||
1202 | /* | |
1203 | * Start a new cherry-pick/ revert sequence; but | |
1204 | * first, make sure that an existing one isn't in | |
1205 | * progress | |
1206 | */ | |
1207 | ||
34b0528b JS |
1208 | if (walk_revs_populate_todo(&todo_list, opts) || |
1209 | create_seq_dir() < 0) | |
043a4492 | 1210 | return -1; |
0f974e21 MG |
1211 | if (get_sha1("HEAD", sha1) && (opts->action == REPLAY_REVERT)) |
1212 | return error(_("Can't revert as initial commit")); | |
311fd397 JS |
1213 | if (save_head(sha1_to_hex(sha1))) |
1214 | return -1; | |
88d5a271 JS |
1215 | if (save_opts(opts)) |
1216 | return -1; | |
004fefa7 JS |
1217 | res = pick_commits(&todo_list, opts); |
1218 | todo_list_release(&todo_list); | |
1219 | return res; | |
043a4492 | 1220 | } |
5ed75e2a | 1221 | |
bab4d109 | 1222 | void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag) |
5ed75e2a | 1223 | { |
bab4d109 | 1224 | unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP; |
5ed75e2a | 1225 | struct strbuf sob = STRBUF_INIT; |
bab4d109 | 1226 | int has_footer; |
5ed75e2a MV |
1227 | |
1228 | strbuf_addstr(&sob, sign_off_header); | |
1229 | strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"), | |
1230 | getenv("GIT_COMMITTER_EMAIL"))); | |
1231 | strbuf_addch(&sob, '\n'); | |
bab4d109 BC |
1232 | |
1233 | /* | |
1234 | * If the whole message buffer is equal to the sob, pretend that we | |
1235 | * found a conforming footer with a matching sob | |
1236 | */ | |
1237 | if (msgbuf->len - ignore_footer == sob.len && | |
1238 | !strncmp(msgbuf->buf, sob.buf, sob.len)) | |
1239 | has_footer = 3; | |
1240 | else | |
1241 | has_footer = has_conforming_footer(msgbuf, &sob, ignore_footer); | |
1242 | ||
33f2f9ab BC |
1243 | if (!has_footer) { |
1244 | const char *append_newlines = NULL; | |
1245 | size_t len = msgbuf->len - ignore_footer; | |
1246 | ||
8c613fd5 BC |
1247 | if (!len) { |
1248 | /* | |
1249 | * The buffer is completely empty. Leave foom for | |
1250 | * the title and body to be filled in by the user. | |
1251 | */ | |
33f2f9ab | 1252 | append_newlines = "\n\n"; |
8c613fd5 BC |
1253 | } else if (msgbuf->buf[len - 1] != '\n') { |
1254 | /* | |
1255 | * Incomplete line. Complete the line and add a | |
1256 | * blank one so that there is an empty line between | |
1257 | * the message body and the sob. | |
1258 | */ | |
1259 | append_newlines = "\n\n"; | |
1260 | } else if (len == 1) { | |
1261 | /* | |
1262 | * Buffer contains a single newline. Add another | |
1263 | * so that we leave room for the title and body. | |
1264 | */ | |
1265 | append_newlines = "\n"; | |
1266 | } else if (msgbuf->buf[len - 2] != '\n') { | |
1267 | /* | |
1268 | * Buffer ends with a single newline. Add another | |
1269 | * so that there is an empty line between the message | |
1270 | * body and the sob. | |
1271 | */ | |
33f2f9ab | 1272 | append_newlines = "\n"; |
8c613fd5 | 1273 | } /* else, the buffer already ends with two newlines. */ |
33f2f9ab BC |
1274 | |
1275 | if (append_newlines) | |
1276 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, | |
1277 | append_newlines, strlen(append_newlines)); | |
5ed75e2a | 1278 | } |
bab4d109 BC |
1279 | |
1280 | if (has_footer != 3 && (!no_dup_sob || has_footer != 2)) | |
1281 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, | |
1282 | sob.buf, sob.len); | |
1283 | ||
5ed75e2a MV |
1284 | strbuf_release(&sob); |
1285 | } |