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