]>
Commit | Line | Data |
---|---|---|
26ae337b RR |
1 | #include "cache.h" |
2 | #include "sequencer.h" | |
26ae337b | 3 | #include "dir.h" |
043a4492 RR |
4 | #include "object.h" |
5 | #include "commit.h" | |
6 | #include "tag.h" | |
7 | #include "run-command.h" | |
8 | #include "exec_cmd.h" | |
9 | #include "utf8.h" | |
10 | #include "cache-tree.h" | |
11 | #include "diff.h" | |
12 | #include "revision.h" | |
13 | #include "rerere.h" | |
14 | #include "merge-recursive.h" | |
15 | #include "refs.h" | |
b27cfb0d | 16 | #include "argv-array.h" |
043a4492 RR |
17 | |
18 | #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION" | |
26ae337b | 19 | |
d596118d | 20 | void remove_sequencer_state(void) |
26ae337b RR |
21 | { |
22 | struct strbuf seq_dir = STRBUF_INIT; | |
26ae337b RR |
23 | |
24 | strbuf_addf(&seq_dir, "%s", git_path(SEQ_DIR)); | |
d596118d | 25 | remove_dir_recursively(&seq_dir, 0); |
26ae337b | 26 | strbuf_release(&seq_dir); |
26ae337b | 27 | } |
043a4492 RR |
28 | |
29 | static const char *action_name(const struct replay_opts *opts) | |
30 | { | |
31 | return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick"; | |
32 | } | |
33 | ||
34 | static char *get_encoding(const char *message); | |
35 | ||
36 | struct commit_message { | |
37 | char *parent_label; | |
38 | const char *label; | |
39 | const char *subject; | |
40 | char *reencoded_message; | |
41 | const char *message; | |
42 | }; | |
43 | ||
44 | static int get_message(struct commit *commit, struct commit_message *out) | |
45 | { | |
46 | const char *encoding; | |
47 | const char *abbrev, *subject; | |
48 | int abbrev_len, subject_len; | |
49 | char *q; | |
50 | ||
51 | if (!commit->buffer) | |
52 | return -1; | |
53 | encoding = get_encoding(commit->buffer); | |
54 | if (!encoding) | |
55 | encoding = "UTF-8"; | |
56 | if (!git_commit_encoding) | |
57 | git_commit_encoding = "UTF-8"; | |
58 | ||
59 | out->reencoded_message = NULL; | |
60 | out->message = commit->buffer; | |
61 | if (strcmp(encoding, git_commit_encoding)) | |
62 | out->reencoded_message = reencode_string(commit->buffer, | |
63 | git_commit_encoding, encoding); | |
64 | if (out->reencoded_message) | |
65 | out->message = out->reencoded_message; | |
66 | ||
67 | abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV); | |
68 | abbrev_len = strlen(abbrev); | |
69 | ||
70 | subject_len = find_commit_subject(out->message, &subject); | |
71 | ||
72 | out->parent_label = xmalloc(strlen("parent of ") + abbrev_len + | |
73 | strlen("... ") + subject_len + 1); | |
74 | q = out->parent_label; | |
75 | q = mempcpy(q, "parent of ", strlen("parent of ")); | |
76 | out->label = q; | |
77 | q = mempcpy(q, abbrev, abbrev_len); | |
78 | q = mempcpy(q, "... ", strlen("... ")); | |
79 | out->subject = q; | |
80 | q = mempcpy(q, subject, subject_len); | |
81 | *q = '\0'; | |
82 | return 0; | |
83 | } | |
84 | ||
85 | static void free_message(struct commit_message *msg) | |
86 | { | |
87 | free(msg->parent_label); | |
88 | free(msg->reencoded_message); | |
89 | } | |
90 | ||
91 | static char *get_encoding(const char *message) | |
92 | { | |
93 | const char *p = message, *eol; | |
94 | ||
95 | while (*p && *p != '\n') { | |
96 | for (eol = p + 1; *eol && *eol != '\n'; eol++) | |
97 | ; /* do nothing */ | |
98 | if (!prefixcmp(p, "encoding ")) { | |
99 | char *result = xmalloc(eol - 8 - p); | |
100 | strlcpy(result, p + 9, eol - 8 - p); | |
101 | return result; | |
102 | } | |
103 | p = eol; | |
104 | if (*p == '\n') | |
105 | p++; | |
106 | } | |
107 | return NULL; | |
108 | } | |
109 | ||
110 | static void write_cherry_pick_head(struct commit *commit, const char *pseudoref) | |
111 | { | |
112 | const char *filename; | |
113 | int fd; | |
114 | struct strbuf buf = STRBUF_INIT; | |
115 | ||
116 | strbuf_addf(&buf, "%s\n", sha1_to_hex(commit->object.sha1)); | |
117 | ||
118 | filename = git_path("%s", pseudoref); | |
119 | fd = open(filename, O_WRONLY | O_CREAT, 0666); | |
120 | if (fd < 0) | |
121 | die_errno(_("Could not open '%s' for writing"), filename); | |
122 | if (write_in_full(fd, buf.buf, buf.len) != buf.len || close(fd)) | |
123 | die_errno(_("Could not write to '%s'"), filename); | |
124 | strbuf_release(&buf); | |
125 | } | |
126 | ||
ed727b19 | 127 | static void print_advice(int show_hint, struct replay_opts *opts) |
043a4492 RR |
128 | { |
129 | char *msg = getenv("GIT_CHERRY_PICK_HELP"); | |
130 | ||
131 | if (msg) { | |
132 | fprintf(stderr, "%s\n", msg); | |
133 | /* | |
134 | * A conflict has occured but the porcelain | |
135 | * (typically rebase --interactive) wants to take care | |
136 | * of the commit itself so remove CHERRY_PICK_HEAD | |
137 | */ | |
138 | unlink(git_path("CHERRY_PICK_HEAD")); | |
139 | return; | |
140 | } | |
141 | ||
ed727b19 PH |
142 | if (show_hint) { |
143 | if (opts->no_commit) | |
144 | advise(_("after resolving the conflicts, mark the corrected paths\n" | |
145 | "with 'git add <paths>' or 'git rm <paths>'")); | |
146 | else | |
147 | advise(_("after resolving the conflicts, mark the corrected paths\n" | |
148 | "with 'git add <paths>' or 'git rm <paths>'\n" | |
149 | "and commit the result with 'git commit'")); | |
150 | } | |
043a4492 RR |
151 | } |
152 | ||
153 | static void write_message(struct strbuf *msgbuf, const char *filename) | |
154 | { | |
155 | static struct lock_file msg_file; | |
156 | ||
157 | int msg_fd = hold_lock_file_for_update(&msg_file, filename, | |
158 | LOCK_DIE_ON_ERROR); | |
159 | if (write_in_full(msg_fd, msgbuf->buf, msgbuf->len) < 0) | |
160 | die_errno(_("Could not write to %s"), filename); | |
161 | strbuf_release(msgbuf); | |
162 | if (commit_lock_file(&msg_file) < 0) | |
163 | die(_("Error wrapping up %s"), filename); | |
164 | } | |
165 | ||
166 | static struct tree *empty_tree(void) | |
167 | { | |
168 | return lookup_tree((const unsigned char *)EMPTY_TREE_SHA1_BIN); | |
169 | } | |
170 | ||
171 | static int error_dirty_index(struct replay_opts *opts) | |
172 | { | |
173 | if (read_cache_unmerged()) | |
174 | return error_resolve_conflict(action_name(opts)); | |
175 | ||
176 | /* Different translation strings for cherry-pick and revert */ | |
177 | if (opts->action == REPLAY_PICK) | |
178 | error(_("Your local changes would be overwritten by cherry-pick.")); | |
179 | else | |
180 | error(_("Your local changes would be overwritten by revert.")); | |
181 | ||
182 | if (advice_commit_before_merge) | |
183 | advise(_("Commit your changes or stash them to proceed.")); | |
184 | return -1; | |
185 | } | |
186 | ||
187 | static int fast_forward_to(const unsigned char *to, const unsigned char *from) | |
188 | { | |
189 | struct ref_lock *ref_lock; | |
190 | ||
191 | read_cache(); | |
192 | if (checkout_fast_forward(from, to)) | |
193 | exit(1); /* the callee should have complained already */ | |
194 | ref_lock = lock_any_ref_for_update("HEAD", from, 0); | |
195 | return write_ref_sha1(ref_lock, to, "cherry-pick"); | |
196 | } | |
197 | ||
198 | static int do_recursive_merge(struct commit *base, struct commit *next, | |
199 | const char *base_label, const char *next_label, | |
200 | unsigned char *head, struct strbuf *msgbuf, | |
201 | struct replay_opts *opts) | |
202 | { | |
203 | struct merge_options o; | |
204 | struct tree *result, *next_tree, *base_tree, *head_tree; | |
205 | int clean, index_fd; | |
206 | const char **xopt; | |
207 | static struct lock_file index_lock; | |
208 | ||
209 | index_fd = hold_locked_index(&index_lock, 1); | |
210 | ||
211 | read_cache(); | |
212 | ||
213 | init_merge_options(&o); | |
214 | o.ancestor = base ? base_label : "(empty tree)"; | |
215 | o.branch1 = "HEAD"; | |
216 | o.branch2 = next ? next_label : "(empty tree)"; | |
217 | ||
218 | head_tree = parse_tree_indirect(head); | |
219 | next_tree = next ? next->tree : empty_tree(); | |
220 | base_tree = base ? base->tree : empty_tree(); | |
221 | ||
222 | for (xopt = opts->xopts; xopt != opts->xopts + opts->xopts_nr; xopt++) | |
223 | parse_merge_opt(&o, *xopt); | |
224 | ||
225 | clean = merge_trees(&o, | |
226 | head_tree, | |
227 | next_tree, base_tree, &result); | |
228 | ||
229 | if (active_cache_changed && | |
230 | (write_cache(index_fd, active_cache, active_nr) || | |
231 | commit_locked_index(&index_lock))) | |
232 | /* TRANSLATORS: %s will be "revert" or "cherry-pick" */ | |
233 | die(_("%s: Unable to write new index file"), action_name(opts)); | |
234 | rollback_lock_file(&index_lock); | |
235 | ||
236 | if (!clean) { | |
237 | int i; | |
238 | strbuf_addstr(msgbuf, "\nConflicts:\n\n"); | |
239 | for (i = 0; i < active_nr;) { | |
240 | struct cache_entry *ce = active_cache[i++]; | |
241 | if (ce_stage(ce)) { | |
242 | strbuf_addch(msgbuf, '\t'); | |
243 | strbuf_addstr(msgbuf, ce->name); | |
244 | strbuf_addch(msgbuf, '\n'); | |
245 | while (i < active_nr && !strcmp(ce->name, | |
246 | active_cache[i]->name)) | |
247 | i++; | |
248 | } | |
249 | } | |
250 | } | |
251 | ||
252 | return !clean; | |
253 | } | |
254 | ||
b27cfb0d NH |
255 | static int is_index_unchanged(void) |
256 | { | |
257 | unsigned char head_sha1[20]; | |
258 | struct commit *head_commit; | |
259 | ||
260 | if (!resolve_ref_unsafe("HEAD", head_sha1, 1, NULL)) | |
261 | return error(_("Could not resolve HEAD commit\n")); | |
262 | ||
263 | head_commit = lookup_commit(head_sha1); | |
4b580061 NH |
264 | |
265 | /* | |
266 | * If head_commit is NULL, check_commit, called from | |
267 | * lookup_commit, would have indicated that head_commit is not | |
268 | * a commit object already. parse_commit() will return failure | |
269 | * without further complaints in such a case. Otherwise, if | |
270 | * the commit is invalid, parse_commit() will complain. So | |
271 | * there is nothing for us to say here. Just return failure. | |
272 | */ | |
273 | if (parse_commit(head_commit)) | |
274 | return -1; | |
b27cfb0d NH |
275 | |
276 | if (!active_cache_tree) | |
277 | active_cache_tree = cache_tree(); | |
278 | ||
279 | if (!cache_tree_fully_valid(active_cache_tree)) | |
280 | if (cache_tree_update(active_cache_tree, active_cache, | |
281 | active_nr, 0)) | |
282 | return error(_("Unable to update cache tree\n")); | |
283 | ||
284 | return !hashcmp(active_cache_tree->sha1, head_commit->tree->object.sha1); | |
285 | } | |
286 | ||
043a4492 RR |
287 | /* |
288 | * If we are cherry-pick, and if the merge did not result in | |
289 | * hand-editing, we will hit this commit and inherit the original | |
290 | * author date and name. | |
291 | * If we are revert, or if our cherry-pick results in a hand merge, | |
292 | * we had better say that the current user is responsible for that. | |
293 | */ | |
294 | static int run_git_commit(const char *defmsg, struct replay_opts *opts) | |
295 | { | |
b27cfb0d NH |
296 | struct argv_array array; |
297 | int rc; | |
298 | ||
299 | argv_array_init(&array); | |
300 | argv_array_push(&array, "commit"); | |
301 | argv_array_push(&array, "-n"); | |
043a4492 | 302 | |
043a4492 | 303 | if (opts->signoff) |
b27cfb0d | 304 | argv_array_push(&array, "-s"); |
043a4492 | 305 | if (!opts->edit) { |
b27cfb0d NH |
306 | argv_array_push(&array, "-F"); |
307 | argv_array_push(&array, defmsg); | |
043a4492 | 308 | } |
b27cfb0d | 309 | |
df478b74 | 310 | if (opts->allow_empty) |
b27cfb0d | 311 | argv_array_push(&array, "--allow-empty"); |
df478b74 | 312 | |
b27cfb0d NH |
313 | rc = run_command_v_opt(array.argv, RUN_GIT_CMD); |
314 | argv_array_clear(&array); | |
315 | return rc; | |
316 | } | |
317 | ||
318 | static int is_original_commit_empty(struct commit *commit) | |
319 | { | |
320 | const unsigned char *ptree_sha1; | |
321 | ||
322 | if (parse_commit(commit)) | |
323 | return error(_("Could not parse commit %s\n"), | |
324 | sha1_to_hex(commit->object.sha1)); | |
325 | if (commit->parents) { | |
326 | struct commit *parent = commit->parents->item; | |
327 | if (parse_commit(parent)) | |
328 | return error(_("Could not parse parent commit %s\n"), | |
329 | sha1_to_hex(parent->object.sha1)); | |
330 | ptree_sha1 = parent->tree->object.sha1; | |
331 | } else { | |
332 | ptree_sha1 = EMPTY_TREE_SHA1_BIN; /* commit is root */ | |
333 | } | |
043a4492 | 334 | |
b27cfb0d | 335 | return !hashcmp(ptree_sha1, commit->tree->object.sha1); |
043a4492 RR |
336 | } |
337 | ||
338 | static int do_pick_commit(struct commit *commit, struct replay_opts *opts) | |
339 | { | |
340 | unsigned char head[20]; | |
341 | struct commit *base, *next, *parent; | |
342 | const char *base_label, *next_label; | |
343 | struct commit_message msg = { NULL, NULL, NULL, NULL, NULL }; | |
344 | char *defmsg = NULL; | |
345 | struct strbuf msgbuf = STRBUF_INIT; | |
346 | int res; | |
b27cfb0d NH |
347 | int empty_commit; |
348 | int index_unchanged; | |
043a4492 RR |
349 | |
350 | if (opts->no_commit) { | |
351 | /* | |
352 | * We do not intend to commit immediately. We just want to | |
353 | * merge the differences in, so let's compute the tree | |
354 | * that represents the "current" state for merge-recursive | |
355 | * to work on. | |
356 | */ | |
357 | if (write_cache_as_tree(head, 0, NULL)) | |
358 | die (_("Your index file is unmerged.")); | |
359 | } else { | |
360 | if (get_sha1("HEAD", head)) | |
361 | return error(_("You do not have a valid HEAD")); | |
362 | if (index_differs_from("HEAD", 0)) | |
363 | return error_dirty_index(opts); | |
364 | } | |
365 | discard_cache(); | |
366 | ||
367 | if (!commit->parents) { | |
368 | parent = NULL; | |
369 | } | |
370 | else if (commit->parents->next) { | |
371 | /* Reverting or cherry-picking a merge commit */ | |
372 | int cnt; | |
373 | struct commit_list *p; | |
374 | ||
375 | if (!opts->mainline) | |
376 | return error(_("Commit %s is a merge but no -m option was given."), | |
377 | sha1_to_hex(commit->object.sha1)); | |
378 | ||
379 | for (cnt = 1, p = commit->parents; | |
380 | cnt != opts->mainline && p; | |
381 | cnt++) | |
382 | p = p->next; | |
383 | if (cnt != opts->mainline || !p) | |
384 | return error(_("Commit %s does not have parent %d"), | |
385 | sha1_to_hex(commit->object.sha1), opts->mainline); | |
386 | parent = p->item; | |
387 | } else if (0 < opts->mainline) | |
388 | return error(_("Mainline was specified but commit %s is not a merge."), | |
389 | sha1_to_hex(commit->object.sha1)); | |
390 | else | |
391 | parent = commit->parents->item; | |
392 | ||
393 | if (opts->allow_ff && parent && !hashcmp(parent->object.sha1, head)) | |
394 | return fast_forward_to(commit->object.sha1, head); | |
395 | ||
396 | if (parent && parse_commit(parent) < 0) | |
397 | /* TRANSLATORS: The first %s will be "revert" or | |
398 | "cherry-pick", the second %s a SHA1 */ | |
399 | return error(_("%s: cannot parse parent commit %s"), | |
400 | action_name(opts), sha1_to_hex(parent->object.sha1)); | |
401 | ||
402 | if (get_message(commit, &msg) != 0) | |
403 | return error(_("Cannot get commit message for %s"), | |
404 | sha1_to_hex(commit->object.sha1)); | |
405 | ||
406 | /* | |
407 | * "commit" is an existing commit. We would want to apply | |
408 | * the difference it introduces since its first parent "prev" | |
409 | * on top of the current HEAD if we are cherry-pick. Or the | |
410 | * reverse of it if we are revert. | |
411 | */ | |
412 | ||
413 | defmsg = git_pathdup("MERGE_MSG"); | |
414 | ||
415 | if (opts->action == REPLAY_REVERT) { | |
416 | base = commit; | |
417 | base_label = msg.label; | |
418 | next = parent; | |
419 | next_label = msg.parent_label; | |
420 | strbuf_addstr(&msgbuf, "Revert \""); | |
421 | strbuf_addstr(&msgbuf, msg.subject); | |
422 | strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit "); | |
423 | strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1)); | |
424 | ||
425 | if (commit->parents && commit->parents->next) { | |
426 | strbuf_addstr(&msgbuf, ", reversing\nchanges made to "); | |
427 | strbuf_addstr(&msgbuf, sha1_to_hex(parent->object.sha1)); | |
428 | } | |
429 | strbuf_addstr(&msgbuf, ".\n"); | |
430 | } else { | |
431 | const char *p; | |
432 | ||
433 | base = parent; | |
434 | base_label = msg.parent_label; | |
435 | next = commit; | |
436 | next_label = msg.label; | |
437 | ||
438 | /* | |
439 | * Append the commit log message to msgbuf; it starts | |
440 | * after the tree, parent, author, committer | |
441 | * information followed by "\n\n". | |
442 | */ | |
443 | p = strstr(msg.message, "\n\n"); | |
444 | if (p) { | |
445 | p += 2; | |
446 | strbuf_addstr(&msgbuf, p); | |
447 | } | |
448 | ||
449 | if (opts->record_origin) { | |
450 | strbuf_addstr(&msgbuf, "(cherry picked from commit "); | |
451 | strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1)); | |
452 | strbuf_addstr(&msgbuf, ")\n"); | |
453 | } | |
454 | } | |
455 | ||
456 | if (!opts->strategy || !strcmp(opts->strategy, "recursive") || opts->action == REPLAY_REVERT) { | |
457 | res = do_recursive_merge(base, next, base_label, next_label, | |
458 | head, &msgbuf, opts); | |
459 | write_message(&msgbuf, defmsg); | |
460 | } else { | |
461 | struct commit_list *common = NULL; | |
462 | struct commit_list *remotes = NULL; | |
463 | ||
464 | write_message(&msgbuf, defmsg); | |
465 | ||
466 | commit_list_insert(base, &common); | |
467 | commit_list_insert(next, &remotes); | |
468 | res = try_merge_command(opts->strategy, opts->xopts_nr, opts->xopts, | |
469 | common, sha1_to_hex(head), remotes); | |
470 | free_commit_list(common); | |
471 | free_commit_list(remotes); | |
472 | } | |
473 | ||
b27cfb0d NH |
474 | empty_commit = is_original_commit_empty(commit); |
475 | if (empty_commit < 0) | |
476 | return empty_commit; | |
477 | ||
043a4492 RR |
478 | /* |
479 | * If the merge was clean or if it failed due to conflict, we write | |
480 | * CHERRY_PICK_HEAD for the subsequent invocation of commit to use. | |
481 | * However, if the merge did not even start, then we don't want to | |
482 | * write it at all. | |
483 | */ | |
484 | if (opts->action == REPLAY_PICK && !opts->no_commit && (res == 0 || res == 1)) | |
485 | write_cherry_pick_head(commit, "CHERRY_PICK_HEAD"); | |
486 | if (opts->action == REPLAY_REVERT && ((opts->no_commit && res == 0) || res == 1)) | |
487 | write_cherry_pick_head(commit, "REVERT_HEAD"); | |
488 | ||
489 | if (res) { | |
490 | error(opts->action == REPLAY_REVERT | |
491 | ? _("could not revert %s... %s") | |
492 | : _("could not apply %s... %s"), | |
493 | find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), | |
494 | msg.subject); | |
ed727b19 | 495 | print_advice(res == 1, opts); |
043a4492 RR |
496 | rerere(opts->allow_rerere_auto); |
497 | } else { | |
b27cfb0d NH |
498 | index_unchanged = is_index_unchanged(); |
499 | /* | |
500 | * If index_unchanged is less than 0, that indicates we either | |
501 | * couldn't parse HEAD or the index, so error out here. | |
502 | */ | |
503 | if (index_unchanged < 0) | |
504 | return index_unchanged; | |
505 | ||
506 | if (!empty_commit && !opts->keep_redundant_commits && index_unchanged) | |
507 | /* | |
508 | * The head tree and the index match | |
509 | * meaning the commit is empty. Since it wasn't created | |
510 | * empty (based on the previous test), we can conclude | |
511 | * the commit has been made redundant. Since we don't | |
512 | * want to keep redundant commits, we can just return | |
513 | * here, skipping this commit | |
514 | */ | |
515 | return 0; | |
516 | ||
043a4492 RR |
517 | if (!opts->no_commit) |
518 | res = run_git_commit(defmsg, opts); | |
519 | } | |
520 | ||
521 | free_message(&msg); | |
522 | free(defmsg); | |
523 | ||
524 | return res; | |
525 | } | |
526 | ||
527 | static void prepare_revs(struct replay_opts *opts) | |
528 | { | |
529 | if (opts->action != REPLAY_REVERT) | |
530 | opts->revs->reverse ^= 1; | |
531 | ||
532 | if (prepare_revision_walk(opts->revs)) | |
533 | die(_("revision walk setup failed")); | |
534 | ||
535 | if (!opts->revs->commits) | |
536 | die(_("empty commit set passed")); | |
537 | } | |
538 | ||
539 | static void read_and_refresh_cache(struct replay_opts *opts) | |
540 | { | |
541 | static struct lock_file index_lock; | |
542 | int index_fd = hold_locked_index(&index_lock, 0); | |
543 | if (read_index_preload(&the_index, NULL) < 0) | |
544 | die(_("git %s: failed to read the index"), action_name(opts)); | |
545 | refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL); | |
546 | if (the_index.cache_changed) { | |
547 | if (write_index(&the_index, index_fd) || | |
548 | commit_locked_index(&index_lock)) | |
549 | die(_("git %s: failed to refresh the index"), action_name(opts)); | |
550 | } | |
551 | rollback_lock_file(&index_lock); | |
552 | } | |
553 | ||
554 | /* | |
555 | * Append a commit to the end of the commit_list. | |
556 | * | |
557 | * next starts by pointing to the variable that holds the head of an | |
558 | * empty commit_list, and is updated to point to the "next" field of | |
559 | * the last item on the list as new commits are appended. | |
560 | * | |
561 | * Usage example: | |
562 | * | |
563 | * struct commit_list *list; | |
564 | * struct commit_list **next = &list; | |
565 | * | |
566 | * next = commit_list_append(c1, next); | |
567 | * next = commit_list_append(c2, next); | |
568 | * assert(commit_list_count(list) == 2); | |
569 | * return list; | |
570 | */ | |
571 | static struct commit_list **commit_list_append(struct commit *commit, | |
572 | struct commit_list **next) | |
573 | { | |
574 | struct commit_list *new = xmalloc(sizeof(struct commit_list)); | |
575 | new->item = commit; | |
576 | *next = new; | |
577 | new->next = NULL; | |
578 | return &new->next; | |
579 | } | |
580 | ||
581 | static int format_todo(struct strbuf *buf, struct commit_list *todo_list, | |
582 | struct replay_opts *opts) | |
583 | { | |
584 | struct commit_list *cur = NULL; | |
585 | const char *sha1_abbrev = NULL; | |
586 | const char *action_str = opts->action == REPLAY_REVERT ? "revert" : "pick"; | |
587 | const char *subject; | |
588 | int subject_len; | |
589 | ||
590 | for (cur = todo_list; cur; cur = cur->next) { | |
591 | sha1_abbrev = find_unique_abbrev(cur->item->object.sha1, DEFAULT_ABBREV); | |
592 | subject_len = find_commit_subject(cur->item->buffer, &subject); | |
593 | strbuf_addf(buf, "%s %s %.*s\n", action_str, sha1_abbrev, | |
594 | subject_len, subject); | |
595 | } | |
596 | return 0; | |
597 | } | |
598 | ||
599 | static struct commit *parse_insn_line(char *bol, char *eol, struct replay_opts *opts) | |
600 | { | |
601 | unsigned char commit_sha1[20]; | |
602 | enum replay_action action; | |
603 | char *end_of_object_name; | |
604 | int saved, status, padding; | |
605 | ||
606 | if (!prefixcmp(bol, "pick")) { | |
607 | action = REPLAY_PICK; | |
608 | bol += strlen("pick"); | |
609 | } else if (!prefixcmp(bol, "revert")) { | |
610 | action = REPLAY_REVERT; | |
611 | bol += strlen("revert"); | |
612 | } else | |
613 | return NULL; | |
614 | ||
615 | /* Eat up extra spaces/ tabs before object name */ | |
616 | padding = strspn(bol, " \t"); | |
617 | if (!padding) | |
618 | return NULL; | |
619 | bol += padding; | |
620 | ||
621 | end_of_object_name = bol + strcspn(bol, " \t\n"); | |
622 | saved = *end_of_object_name; | |
623 | *end_of_object_name = '\0'; | |
624 | status = get_sha1(bol, commit_sha1); | |
625 | *end_of_object_name = saved; | |
626 | ||
627 | /* | |
628 | * Verify that the action matches up with the one in | |
629 | * opts; we don't support arbitrary instructions | |
630 | */ | |
631 | if (action != opts->action) { | |
632 | const char *action_str; | |
633 | action_str = action == REPLAY_REVERT ? "revert" : "cherry-pick"; | |
634 | error(_("Cannot %s during a %s"), action_str, action_name(opts)); | |
635 | return NULL; | |
636 | } | |
637 | ||
638 | if (status < 0) | |
639 | return NULL; | |
640 | ||
641 | return lookup_commit_reference(commit_sha1); | |
642 | } | |
643 | ||
644 | static int parse_insn_buffer(char *buf, struct commit_list **todo_list, | |
645 | struct replay_opts *opts) | |
646 | { | |
647 | struct commit_list **next = todo_list; | |
648 | struct commit *commit; | |
649 | char *p = buf; | |
650 | int i; | |
651 | ||
652 | for (i = 1; *p; i++) { | |
653 | char *eol = strchrnul(p, '\n'); | |
654 | commit = parse_insn_line(p, eol, opts); | |
655 | if (!commit) | |
656 | return error(_("Could not parse line %d."), i); | |
657 | next = commit_list_append(commit, next); | |
658 | p = *eol ? eol + 1 : eol; | |
659 | } | |
660 | if (!*todo_list) | |
661 | return error(_("No commits parsed.")); | |
662 | return 0; | |
663 | } | |
664 | ||
665 | static void read_populate_todo(struct commit_list **todo_list, | |
666 | struct replay_opts *opts) | |
667 | { | |
668 | const char *todo_file = git_path(SEQ_TODO_FILE); | |
669 | struct strbuf buf = STRBUF_INIT; | |
670 | int fd, res; | |
671 | ||
672 | fd = open(todo_file, O_RDONLY); | |
673 | if (fd < 0) | |
674 | die_errno(_("Could not open %s"), todo_file); | |
675 | if (strbuf_read(&buf, fd, 0) < 0) { | |
676 | close(fd); | |
677 | strbuf_release(&buf); | |
678 | die(_("Could not read %s."), todo_file); | |
679 | } | |
680 | close(fd); | |
681 | ||
682 | res = parse_insn_buffer(buf.buf, todo_list, opts); | |
683 | strbuf_release(&buf); | |
684 | if (res) | |
685 | die(_("Unusable instruction sheet: %s"), todo_file); | |
686 | } | |
687 | ||
688 | static int populate_opts_cb(const char *key, const char *value, void *data) | |
689 | { | |
690 | struct replay_opts *opts = data; | |
691 | int error_flag = 1; | |
692 | ||
693 | if (!value) | |
694 | error_flag = 0; | |
695 | else if (!strcmp(key, "options.no-commit")) | |
696 | opts->no_commit = git_config_bool_or_int(key, value, &error_flag); | |
697 | else if (!strcmp(key, "options.edit")) | |
698 | opts->edit = git_config_bool_or_int(key, value, &error_flag); | |
699 | else if (!strcmp(key, "options.signoff")) | |
700 | opts->signoff = git_config_bool_or_int(key, value, &error_flag); | |
701 | else if (!strcmp(key, "options.record-origin")) | |
702 | opts->record_origin = git_config_bool_or_int(key, value, &error_flag); | |
703 | else if (!strcmp(key, "options.allow-ff")) | |
704 | opts->allow_ff = git_config_bool_or_int(key, value, &error_flag); | |
705 | else if (!strcmp(key, "options.mainline")) | |
706 | opts->mainline = git_config_int(key, value); | |
707 | else if (!strcmp(key, "options.strategy")) | |
708 | git_config_string(&opts->strategy, key, value); | |
709 | else if (!strcmp(key, "options.strategy-option")) { | |
710 | ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc); | |
711 | opts->xopts[opts->xopts_nr++] = xstrdup(value); | |
712 | } else | |
713 | return error(_("Invalid key: %s"), key); | |
714 | ||
715 | if (!error_flag) | |
716 | return error(_("Invalid value for %s: %s"), key, value); | |
717 | ||
718 | return 0; | |
719 | } | |
720 | ||
721 | static void read_populate_opts(struct replay_opts **opts_ptr) | |
722 | { | |
723 | const char *opts_file = git_path(SEQ_OPTS_FILE); | |
724 | ||
725 | if (!file_exists(opts_file)) | |
726 | return; | |
727 | if (git_config_from_file(populate_opts_cb, opts_file, *opts_ptr) < 0) | |
728 | die(_("Malformed options sheet: %s"), opts_file); | |
729 | } | |
730 | ||
731 | static void walk_revs_populate_todo(struct commit_list **todo_list, | |
732 | struct replay_opts *opts) | |
733 | { | |
734 | struct commit *commit; | |
735 | struct commit_list **next; | |
736 | ||
737 | prepare_revs(opts); | |
738 | ||
739 | next = todo_list; | |
740 | while ((commit = get_revision(opts->revs))) | |
741 | next = commit_list_append(commit, next); | |
742 | } | |
743 | ||
744 | static int create_seq_dir(void) | |
745 | { | |
746 | const char *seq_dir = git_path(SEQ_DIR); | |
747 | ||
748 | if (file_exists(seq_dir)) { | |
749 | error(_("a cherry-pick or revert is already in progress")); | |
750 | advise(_("try \"git cherry-pick (--continue | --quit | --abort)\"")); | |
751 | return -1; | |
752 | } | |
753 | else if (mkdir(seq_dir, 0777) < 0) | |
754 | die_errno(_("Could not create sequencer directory %s"), seq_dir); | |
755 | return 0; | |
756 | } | |
757 | ||
758 | static void save_head(const char *head) | |
759 | { | |
760 | const char *head_file = git_path(SEQ_HEAD_FILE); | |
761 | static struct lock_file head_lock; | |
762 | struct strbuf buf = STRBUF_INIT; | |
763 | int fd; | |
764 | ||
765 | fd = hold_lock_file_for_update(&head_lock, head_file, LOCK_DIE_ON_ERROR); | |
766 | strbuf_addf(&buf, "%s\n", head); | |
767 | if (write_in_full(fd, buf.buf, buf.len) < 0) | |
768 | die_errno(_("Could not write to %s"), head_file); | |
769 | if (commit_lock_file(&head_lock) < 0) | |
770 | die(_("Error wrapping up %s."), head_file); | |
771 | } | |
772 | ||
773 | static int reset_for_rollback(const unsigned char *sha1) | |
774 | { | |
775 | const char *argv[4]; /* reset --merge <arg> + NULL */ | |
776 | argv[0] = "reset"; | |
777 | argv[1] = "--merge"; | |
778 | argv[2] = sha1_to_hex(sha1); | |
779 | argv[3] = NULL; | |
780 | return run_command_v_opt(argv, RUN_GIT_CMD); | |
781 | } | |
782 | ||
783 | static int rollback_single_pick(void) | |
784 | { | |
785 | unsigned char head_sha1[20]; | |
786 | ||
787 | if (!file_exists(git_path("CHERRY_PICK_HEAD")) && | |
788 | !file_exists(git_path("REVERT_HEAD"))) | |
789 | return error(_("no cherry-pick or revert in progress")); | |
790 | if (read_ref_full("HEAD", head_sha1, 0, NULL)) | |
791 | return error(_("cannot resolve HEAD")); | |
792 | if (is_null_sha1(head_sha1)) | |
793 | return error(_("cannot abort from a branch yet to be born")); | |
794 | return reset_for_rollback(head_sha1); | |
795 | } | |
796 | ||
797 | static int sequencer_rollback(struct replay_opts *opts) | |
798 | { | |
799 | const char *filename; | |
800 | FILE *f; | |
801 | unsigned char sha1[20]; | |
802 | struct strbuf buf = STRBUF_INIT; | |
803 | ||
804 | filename = git_path(SEQ_HEAD_FILE); | |
805 | f = fopen(filename, "r"); | |
806 | if (!f && errno == ENOENT) { | |
807 | /* | |
808 | * There is no multiple-cherry-pick in progress. | |
809 | * If CHERRY_PICK_HEAD or REVERT_HEAD indicates | |
810 | * a single-cherry-pick in progress, abort that. | |
811 | */ | |
812 | return rollback_single_pick(); | |
813 | } | |
814 | if (!f) | |
815 | return error(_("cannot open %s: %s"), filename, | |
816 | strerror(errno)); | |
817 | if (strbuf_getline(&buf, f, '\n')) { | |
818 | error(_("cannot read %s: %s"), filename, ferror(f) ? | |
819 | strerror(errno) : _("unexpected end of file")); | |
820 | fclose(f); | |
821 | goto fail; | |
822 | } | |
823 | fclose(f); | |
824 | if (get_sha1_hex(buf.buf, sha1) || buf.buf[40] != '\0') { | |
825 | error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"), | |
826 | filename); | |
827 | goto fail; | |
828 | } | |
829 | if (reset_for_rollback(sha1)) | |
830 | goto fail; | |
831 | remove_sequencer_state(); | |
832 | strbuf_release(&buf); | |
833 | return 0; | |
834 | fail: | |
835 | strbuf_release(&buf); | |
836 | return -1; | |
837 | } | |
838 | ||
839 | static void save_todo(struct commit_list *todo_list, struct replay_opts *opts) | |
840 | { | |
841 | const char *todo_file = git_path(SEQ_TODO_FILE); | |
842 | static struct lock_file todo_lock; | |
843 | struct strbuf buf = STRBUF_INIT; | |
844 | int fd; | |
845 | ||
846 | fd = hold_lock_file_for_update(&todo_lock, todo_file, LOCK_DIE_ON_ERROR); | |
847 | if (format_todo(&buf, todo_list, opts) < 0) | |
848 | die(_("Could not format %s."), todo_file); | |
849 | if (write_in_full(fd, buf.buf, buf.len) < 0) { | |
850 | strbuf_release(&buf); | |
851 | die_errno(_("Could not write to %s"), todo_file); | |
852 | } | |
853 | if (commit_lock_file(&todo_lock) < 0) { | |
854 | strbuf_release(&buf); | |
855 | die(_("Error wrapping up %s."), todo_file); | |
856 | } | |
857 | strbuf_release(&buf); | |
858 | } | |
859 | ||
860 | static void save_opts(struct replay_opts *opts) | |
861 | { | |
862 | const char *opts_file = git_path(SEQ_OPTS_FILE); | |
863 | ||
864 | if (opts->no_commit) | |
865 | git_config_set_in_file(opts_file, "options.no-commit", "true"); | |
866 | if (opts->edit) | |
867 | git_config_set_in_file(opts_file, "options.edit", "true"); | |
868 | if (opts->signoff) | |
869 | git_config_set_in_file(opts_file, "options.signoff", "true"); | |
870 | if (opts->record_origin) | |
871 | git_config_set_in_file(opts_file, "options.record-origin", "true"); | |
872 | if (opts->allow_ff) | |
873 | git_config_set_in_file(opts_file, "options.allow-ff", "true"); | |
874 | if (opts->mainline) { | |
875 | struct strbuf buf = STRBUF_INIT; | |
876 | strbuf_addf(&buf, "%d", opts->mainline); | |
877 | git_config_set_in_file(opts_file, "options.mainline", buf.buf); | |
878 | strbuf_release(&buf); | |
879 | } | |
880 | if (opts->strategy) | |
881 | git_config_set_in_file(opts_file, "options.strategy", opts->strategy); | |
882 | if (opts->xopts) { | |
883 | int i; | |
884 | for (i = 0; i < opts->xopts_nr; i++) | |
885 | git_config_set_multivar_in_file(opts_file, | |
886 | "options.strategy-option", | |
887 | opts->xopts[i], "^$", 0); | |
888 | } | |
889 | } | |
890 | ||
891 | static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts) | |
892 | { | |
893 | struct commit_list *cur; | |
894 | int res; | |
895 | ||
896 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); | |
897 | if (opts->allow_ff) | |
898 | assert(!(opts->signoff || opts->no_commit || | |
899 | opts->record_origin || opts->edit)); | |
900 | read_and_refresh_cache(opts); | |
901 | ||
902 | for (cur = todo_list; cur; cur = cur->next) { | |
903 | save_todo(cur, opts); | |
904 | res = do_pick_commit(cur->item, opts); | |
905 | if (res) | |
906 | return res; | |
907 | } | |
908 | ||
909 | /* | |
910 | * Sequence of picks finished successfully; cleanup by | |
911 | * removing the .git/sequencer directory | |
912 | */ | |
913 | remove_sequencer_state(); | |
914 | return 0; | |
915 | } | |
916 | ||
917 | static int continue_single_pick(void) | |
918 | { | |
919 | const char *argv[] = { "commit", NULL }; | |
920 | ||
921 | if (!file_exists(git_path("CHERRY_PICK_HEAD")) && | |
922 | !file_exists(git_path("REVERT_HEAD"))) | |
923 | return error(_("no cherry-pick or revert in progress")); | |
924 | return run_command_v_opt(argv, RUN_GIT_CMD); | |
925 | } | |
926 | ||
927 | static int sequencer_continue(struct replay_opts *opts) | |
928 | { | |
929 | struct commit_list *todo_list = NULL; | |
930 | ||
931 | if (!file_exists(git_path(SEQ_TODO_FILE))) | |
932 | return continue_single_pick(); | |
933 | read_populate_opts(&opts); | |
934 | read_populate_todo(&todo_list, opts); | |
935 | ||
936 | /* Verify that the conflict has been resolved */ | |
937 | if (file_exists(git_path("CHERRY_PICK_HEAD")) || | |
938 | file_exists(git_path("REVERT_HEAD"))) { | |
939 | int ret = continue_single_pick(); | |
940 | if (ret) | |
941 | return ret; | |
942 | } | |
943 | if (index_differs_from("HEAD", 0)) | |
944 | return error_dirty_index(opts); | |
945 | todo_list = todo_list->next; | |
946 | return pick_commits(todo_list, opts); | |
947 | } | |
948 | ||
949 | static int single_pick(struct commit *cmit, struct replay_opts *opts) | |
950 | { | |
951 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); | |
952 | return do_pick_commit(cmit, opts); | |
953 | } | |
954 | ||
955 | int sequencer_pick_revisions(struct replay_opts *opts) | |
956 | { | |
957 | struct commit_list *todo_list = NULL; | |
958 | unsigned char sha1[20]; | |
959 | ||
960 | if (opts->subcommand == REPLAY_NONE) | |
961 | assert(opts->revs); | |
962 | ||
963 | read_and_refresh_cache(opts); | |
964 | ||
965 | /* | |
966 | * Decide what to do depending on the arguments; a fresh | |
967 | * cherry-pick should be handled differently from an existing | |
968 | * one that is being continued | |
969 | */ | |
970 | if (opts->subcommand == REPLAY_REMOVE_STATE) { | |
971 | remove_sequencer_state(); | |
972 | return 0; | |
973 | } | |
974 | if (opts->subcommand == REPLAY_ROLLBACK) | |
975 | return sequencer_rollback(opts); | |
976 | if (opts->subcommand == REPLAY_CONTINUE) | |
977 | return sequencer_continue(opts); | |
978 | ||
979 | /* | |
980 | * If we were called as "git cherry-pick <commit>", just | |
981 | * cherry-pick/revert it, set CHERRY_PICK_HEAD / | |
982 | * REVERT_HEAD, and don't touch the sequencer state. | |
983 | * This means it is possible to cherry-pick in the middle | |
984 | * of a cherry-pick sequence. | |
985 | */ | |
986 | if (opts->revs->cmdline.nr == 1 && | |
987 | opts->revs->cmdline.rev->whence == REV_CMD_REV && | |
988 | opts->revs->no_walk && | |
989 | !opts->revs->cmdline.rev->flags) { | |
990 | struct commit *cmit; | |
991 | if (prepare_revision_walk(opts->revs)) | |
992 | die(_("revision walk setup failed")); | |
993 | cmit = get_revision(opts->revs); | |
994 | if (!cmit || get_revision(opts->revs)) | |
995 | die("BUG: expected exactly one commit from walk"); | |
996 | return single_pick(cmit, opts); | |
997 | } | |
998 | ||
999 | /* | |
1000 | * Start a new cherry-pick/ revert sequence; but | |
1001 | * first, make sure that an existing one isn't in | |
1002 | * progress | |
1003 | */ | |
1004 | ||
1005 | walk_revs_populate_todo(&todo_list, opts); | |
1006 | if (create_seq_dir() < 0) | |
1007 | return -1; | |
1008 | if (get_sha1("HEAD", sha1)) { | |
1009 | if (opts->action == REPLAY_REVERT) | |
1010 | return error(_("Can't revert as initial commit")); | |
1011 | return error(_("Can't cherry-pick into empty head")); | |
1012 | } | |
1013 | save_head(sha1_to_hex(sha1)); | |
1014 | save_opts(opts); | |
1015 | return pick_commits(todo_list, opts); | |
1016 | } |