]>
Commit | Line | Data |
---|---|---|
73c2779f PT |
1 | /* |
2 | * Builtin "git am" | |
3 | * | |
4 | * Based on git-am.sh by Junio C Hamano. | |
5 | */ | |
f8adbec9 | 6 | #define USE_THE_INDEX_COMPATIBILITY_MACROS |
73c2779f | 7 | #include "cache.h" |
b2141fc1 | 8 | #include "config.h" |
73c2779f | 9 | #include "builtin.h" |
d807c4a0 | 10 | #include "exec-cmd.h" |
8c3bd9e2 PT |
11 | #include "parse-options.h" |
12 | #include "dir.h" | |
11c2177f | 13 | #include "run-command.h" |
3e20dcf3 | 14 | #include "quote.h" |
db86e61c | 15 | #include "tempfile.h" |
38a824fe | 16 | #include "lockfile.h" |
c9e8d960 PT |
17 | #include "cache-tree.h" |
18 | #include "refs.h" | |
19 | #include "commit.h" | |
32a5fcbf PT |
20 | #include "diff.h" |
21 | #include "diffcore.h" | |
9990080c PT |
22 | #include "unpack-trees.h" |
23 | #include "branch.h" | |
eb898b83 | 24 | #include "sequencer.h" |
84f3de28 PT |
25 | #include "revision.h" |
26 | #include "merge-recursive.h" | |
df2760a5 | 27 | #include "log-tree.h" |
88b291fe | 28 | #include "notes-utils.h" |
f1cb96d6 | 29 | #include "rerere.h" |
7ff26832 | 30 | #include "prompt.h" |
4b98bae2 | 31 | #include "mailinfo.h" |
edfac5eb | 32 | #include "apply.h" |
a77598ef | 33 | #include "string-list.h" |
3836d88a | 34 | #include "packfile.h" |
f86bcc7b | 35 | #include "repository.h" |
3e20dcf3 | 36 | |
38a824fe PT |
37 | /** |
38 | * Returns the length of the first line of msg. | |
39 | */ | |
40 | static int linelen(const char *msg) | |
41 | { | |
42 | return strchrnul(msg, '\n') - msg; | |
43 | } | |
44 | ||
5ae41c79 PT |
45 | /** |
46 | * Returns true if `str` consists of only whitespace, false otherwise. | |
47 | */ | |
48 | static int str_isspace(const char *str) | |
49 | { | |
50 | for (; *str; str++) | |
51 | if (!isspace(*str)) | |
52 | return 0; | |
53 | ||
54 | return 1; | |
55 | } | |
56 | ||
11c2177f PT |
57 | enum patch_format { |
58 | PATCH_FORMAT_UNKNOWN = 0, | |
5ae41c79 | 59 | PATCH_FORMAT_MBOX, |
336108c1 | 60 | PATCH_FORMAT_STGIT, |
94cd175c | 61 | PATCH_FORMAT_STGIT_SERIES, |
d9925d1a EW |
62 | PATCH_FORMAT_HG, |
63 | PATCH_FORMAT_MBOXRD | |
11c2177f | 64 | }; |
8c3bd9e2 | 65 | |
4f1b6961 PT |
66 | enum keep_type { |
67 | KEEP_FALSE = 0, | |
68 | KEEP_TRUE, /* pass -k flag to git-mailinfo */ | |
69 | KEEP_NON_PATCH /* pass -b flag to git-mailinfo */ | |
70 | }; | |
71 | ||
9b646617 PT |
72 | enum scissors_type { |
73 | SCISSORS_UNSET = -1, | |
74 | SCISSORS_FALSE = 0, /* pass --no-scissors to git-mailinfo */ | |
75 | SCISSORS_TRUE /* pass --scissors to git-mailinfo */ | |
76 | }; | |
77 | ||
b5e82359 PT |
78 | enum signoff_type { |
79 | SIGNOFF_FALSE = 0, | |
80 | SIGNOFF_TRUE = 1, | |
81 | SIGNOFF_EXPLICIT /* --signoff was set on the command-line */ | |
82 | }; | |
83 | ||
f3b48228 PB |
84 | enum show_patch_type { |
85 | SHOW_PATCH_RAW = 0, | |
aa416b22 | 86 | SHOW_PATCH_DIFF = 1, |
f3b48228 PB |
87 | }; |
88 | ||
8c3bd9e2 PT |
89 | struct am_state { |
90 | /* state directory path */ | |
91 | char *dir; | |
92 | ||
93 | /* current and last patch numbers, 1-indexed */ | |
94 | int cur; | |
95 | int last; | |
11c2177f | 96 | |
3e20dcf3 PT |
97 | /* commit metadata and message */ |
98 | char *author_name; | |
99 | char *author_email; | |
100 | char *author_date; | |
101 | char *msg; | |
102 | size_t msg_len; | |
103 | ||
13b97ea5 | 104 | /* when --rebasing, records the original commit the patch came from */ |
8c88769b | 105 | struct object_id orig_commit; |
13b97ea5 | 106 | |
11c2177f PT |
107 | /* number of digits in patch filename */ |
108 | int prec; | |
5d28cf78 PT |
109 | |
110 | /* various operating modes and command line options */ | |
7ff26832 | 111 | int interactive; |
84f3de28 | 112 | int threeway; |
5d28cf78 | 113 | int quiet; |
b5e82359 | 114 | int signoff; /* enum signoff_type */ |
ef7ee16d | 115 | int utf8; |
4f1b6961 | 116 | int keep; /* enum keep_type */ |
702cbaad | 117 | int message_id; |
9b646617 | 118 | int scissors; /* enum scissors_type */ |
59b519ab | 119 | int quoted_cr; /* enum quoted_cr_action */ |
22f9b7f3 | 120 | struct strvec git_apply_opts; |
2d83109a | 121 | const char *resolvemsg; |
0cd4bcba | 122 | int committer_date_is_author_date; |
f07adb62 | 123 | int ignore_date; |
f1cb96d6 | 124 | int allow_rerere_autoupdate; |
7e35dacb | 125 | const char *sign_commit; |
35bdcc59 | 126 | int rebasing; |
8c3bd9e2 PT |
127 | }; |
128 | ||
129 | /** | |
16d2676c | 130 | * Initializes am_state with the default values. |
8c3bd9e2 | 131 | */ |
16d2676c | 132 | static void am_state_init(struct am_state *state) |
8c3bd9e2 | 133 | { |
7e35dacb PT |
134 | int gpgsign; |
135 | ||
8c3bd9e2 PT |
136 | memset(state, 0, sizeof(*state)); |
137 | ||
16d2676c | 138 | state->dir = git_pathdup("rebase-apply"); |
11c2177f PT |
139 | |
140 | state->prec = 4; | |
ef7ee16d | 141 | |
e97a5e76 RL |
142 | git_config_get_bool("am.threeway", &state->threeway); |
143 | ||
ef7ee16d | 144 | state->utf8 = 1; |
702cbaad PT |
145 | |
146 | git_config_get_bool("am.messageid", &state->message_id); | |
9b646617 PT |
147 | |
148 | state->scissors = SCISSORS_UNSET; | |
59b519ab | 149 | state->quoted_cr = quoted_cr_unset; |
257e8cec | 150 | |
22f9b7f3 | 151 | strvec_init(&state->git_apply_opts); |
7e35dacb PT |
152 | |
153 | if (!git_config_get_bool("commit.gpgsign", &gpgsign)) | |
154 | state->sign_commit = gpgsign ? "" : NULL; | |
8c3bd9e2 PT |
155 | } |
156 | ||
157 | /** | |
158 | * Releases memory allocated by an am_state. | |
159 | */ | |
160 | static void am_state_release(struct am_state *state) | |
161 | { | |
162 | free(state->dir); | |
3e20dcf3 PT |
163 | free(state->author_name); |
164 | free(state->author_email); | |
165 | free(state->author_date); | |
166 | free(state->msg); | |
22f9b7f3 | 167 | strvec_clear(&state->git_apply_opts); |
8c3bd9e2 PT |
168 | } |
169 | ||
59b519ab ĐTCD |
170 | static int am_option_parse_quoted_cr(const struct option *opt, |
171 | const char *arg, int unset) | |
172 | { | |
173 | BUG_ON_OPT_NEG(unset); | |
174 | ||
175 | if (mailinfo_parse_quoted_cr_action(arg, opt->value) != 0) | |
176 | return error(_("bad action '%s' for '%s'"), arg, "--quoted-cr"); | |
177 | return 0; | |
178 | } | |
179 | ||
8c3bd9e2 PT |
180 | /** |
181 | * Returns path relative to the am_state directory. | |
182 | */ | |
183 | static inline const char *am_path(const struct am_state *state, const char *path) | |
184 | { | |
185 | return mkpath("%s/%s", state->dir, path); | |
186 | } | |
187 | ||
25b763ba JH |
188 | /** |
189 | * For convenience to call write_file() | |
190 | */ | |
1dad879a RS |
191 | static void write_state_text(const struct am_state *state, |
192 | const char *name, const char *string) | |
25b763ba | 193 | { |
1dad879a | 194 | write_file(am_path(state, name), "%s", string); |
25b763ba JH |
195 | } |
196 | ||
1dad879a RS |
197 | static void write_state_count(const struct am_state *state, |
198 | const char *name, int value) | |
25b763ba | 199 | { |
1dad879a | 200 | write_file(am_path(state, name), "%d", value); |
25b763ba JH |
201 | } |
202 | ||
1dad879a RS |
203 | static void write_state_bool(const struct am_state *state, |
204 | const char *name, int value) | |
25b763ba | 205 | { |
1dad879a | 206 | write_state_text(state, name, value ? "t" : "f"); |
25b763ba JH |
207 | } |
208 | ||
5d28cf78 PT |
209 | /** |
210 | * If state->quiet is false, calls fprintf(fp, fmt, ...), and appends a newline | |
211 | * at the end. | |
212 | */ | |
48ca53ca | 213 | __attribute__((format (printf, 3, 4))) |
5d28cf78 PT |
214 | static void say(const struct am_state *state, FILE *fp, const char *fmt, ...) |
215 | { | |
216 | va_list ap; | |
217 | ||
218 | va_start(ap, fmt); | |
219 | if (!state->quiet) { | |
220 | vfprintf(fp, fmt, ap); | |
221 | putc('\n', fp); | |
222 | } | |
223 | va_end(ap); | |
224 | } | |
225 | ||
8c3bd9e2 PT |
226 | /** |
227 | * Returns 1 if there is an am session in progress, 0 otherwise. | |
228 | */ | |
229 | static int am_in_progress(const struct am_state *state) | |
230 | { | |
231 | struct stat st; | |
232 | ||
233 | if (lstat(state->dir, &st) < 0 || !S_ISDIR(st.st_mode)) | |
234 | return 0; | |
235 | if (lstat(am_path(state, "last"), &st) || !S_ISREG(st.st_mode)) | |
236 | return 0; | |
237 | if (lstat(am_path(state, "next"), &st) || !S_ISREG(st.st_mode)) | |
238 | return 0; | |
239 | return 1; | |
240 | } | |
241 | ||
242 | /** | |
243 | * Reads the contents of `file` in the `state` directory into `sb`. Returns the | |
244 | * number of bytes read on success, -1 if the file does not exist. If `trim` is | |
245 | * set, trailing whitespace will be removed. | |
246 | */ | |
247 | static int read_state_file(struct strbuf *sb, const struct am_state *state, | |
248 | const char *file, int trim) | |
249 | { | |
250 | strbuf_reset(sb); | |
251 | ||
252 | if (strbuf_read_file(sb, am_path(state, file), 0) >= 0) { | |
253 | if (trim) | |
254 | strbuf_trim(sb); | |
255 | ||
256 | return sb->len; | |
257 | } | |
258 | ||
259 | if (errno == ENOENT) | |
260 | return -1; | |
261 | ||
262 | die_errno(_("could not read '%s'"), am_path(state, file)); | |
263 | } | |
264 | ||
3e20dcf3 PT |
265 | /** |
266 | * Reads and parses the state directory's "author-script" file, and sets | |
267 | * state->author_name, state->author_email and state->author_date accordingly. | |
268 | * Returns 0 on success, -1 if the file could not be parsed. | |
269 | * | |
270 | * The author script is of the format: | |
271 | * | |
272 | * GIT_AUTHOR_NAME='$author_name' | |
273 | * GIT_AUTHOR_EMAIL='$author_email' | |
274 | * GIT_AUTHOR_DATE='$author_date' | |
275 | * | |
276 | * where $author_name, $author_email and $author_date are quoted. We are strict | |
277 | * with our parsing, as the file was meant to be eval'd in the old git-am.sh | |
278 | * script, and thus if the file differs from what this function expects, it is | |
279 | * better to bail out than to do something that the user does not expect. | |
280 | */ | |
a75d3513 | 281 | static int read_am_author_script(struct am_state *state) |
3e20dcf3 PT |
282 | { |
283 | const char *filename = am_path(state, "author-script"); | |
3e20dcf3 PT |
284 | |
285 | assert(!state->author_name); | |
286 | assert(!state->author_email); | |
287 | assert(!state->author_date); | |
288 | ||
bcd33ec2 PW |
289 | return read_author_script(filename, &state->author_name, |
290 | &state->author_email, &state->author_date, 1); | |
3e20dcf3 PT |
291 | } |
292 | ||
293 | /** | |
294 | * Saves state->author_name, state->author_email and state->author_date in the | |
295 | * state directory's "author-script" file. | |
296 | */ | |
297 | static void write_author_script(const struct am_state *state) | |
298 | { | |
299 | struct strbuf sb = STRBUF_INIT; | |
300 | ||
301 | strbuf_addstr(&sb, "GIT_AUTHOR_NAME="); | |
302 | sq_quote_buf(&sb, state->author_name); | |
303 | strbuf_addch(&sb, '\n'); | |
304 | ||
305 | strbuf_addstr(&sb, "GIT_AUTHOR_EMAIL="); | |
306 | sq_quote_buf(&sb, state->author_email); | |
307 | strbuf_addch(&sb, '\n'); | |
308 | ||
309 | strbuf_addstr(&sb, "GIT_AUTHOR_DATE="); | |
310 | sq_quote_buf(&sb, state->author_date); | |
311 | strbuf_addch(&sb, '\n'); | |
312 | ||
25b763ba | 313 | write_state_text(state, "author-script", sb.buf); |
3e20dcf3 PT |
314 | |
315 | strbuf_release(&sb); | |
316 | } | |
317 | ||
318 | /** | |
319 | * Reads the commit message from the state directory's "final-commit" file, | |
320 | * setting state->msg to its contents and state->msg_len to the length of its | |
321 | * contents in bytes. | |
322 | * | |
323 | * Returns 0 on success, -1 if the file does not exist. | |
324 | */ | |
325 | static int read_commit_msg(struct am_state *state) | |
326 | { | |
327 | struct strbuf sb = STRBUF_INIT; | |
328 | ||
329 | assert(!state->msg); | |
330 | ||
331 | if (read_state_file(&sb, state, "final-commit", 0) < 0) { | |
332 | strbuf_release(&sb); | |
333 | return -1; | |
334 | } | |
335 | ||
336 | state->msg = strbuf_detach(&sb, &state->msg_len); | |
337 | return 0; | |
338 | } | |
339 | ||
340 | /** | |
341 | * Saves state->msg in the state directory's "final-commit" file. | |
342 | */ | |
343 | static void write_commit_msg(const struct am_state *state) | |
344 | { | |
3e20dcf3 | 345 | const char *filename = am_path(state, "final-commit"); |
e78d5d49 | 346 | write_file_buf(filename, state->msg, state->msg_len); |
3e20dcf3 PT |
347 | } |
348 | ||
8c3bd9e2 PT |
349 | /** |
350 | * Loads state from disk. | |
351 | */ | |
352 | static void am_load(struct am_state *state) | |
353 | { | |
354 | struct strbuf sb = STRBUF_INIT; | |
355 | ||
356 | if (read_state_file(&sb, state, "next", 1) < 0) | |
033abf97 | 357 | BUG("state file 'next' does not exist"); |
8c3bd9e2 PT |
358 | state->cur = strtol(sb.buf, NULL, 10); |
359 | ||
360 | if (read_state_file(&sb, state, "last", 1) < 0) | |
033abf97 | 361 | BUG("state file 'last' does not exist"); |
8c3bd9e2 PT |
362 | state->last = strtol(sb.buf, NULL, 10); |
363 | ||
a75d3513 | 364 | if (read_am_author_script(state) < 0) |
3e20dcf3 PT |
365 | die(_("could not parse author script")); |
366 | ||
367 | read_commit_msg(state); | |
368 | ||
13b97ea5 | 369 | if (read_state_file(&sb, state, "original-commit", 1) < 0) |
8c88769b | 370 | oidclr(&state->orig_commit); |
371 | else if (get_oid_hex(sb.buf, &state->orig_commit) < 0) | |
13b97ea5 PT |
372 | die(_("could not parse %s"), am_path(state, "original-commit")); |
373 | ||
84f3de28 PT |
374 | read_state_file(&sb, state, "threeway", 1); |
375 | state->threeway = !strcmp(sb.buf, "t"); | |
376 | ||
5d28cf78 PT |
377 | read_state_file(&sb, state, "quiet", 1); |
378 | state->quiet = !strcmp(sb.buf, "t"); | |
379 | ||
eb898b83 PT |
380 | read_state_file(&sb, state, "sign", 1); |
381 | state->signoff = !strcmp(sb.buf, "t"); | |
382 | ||
ef7ee16d PT |
383 | read_state_file(&sb, state, "utf8", 1); |
384 | state->utf8 = !strcmp(sb.buf, "t"); | |
385 | ||
fd4a3f48 PW |
386 | if (file_exists(am_path(state, "rerere-autoupdate"))) { |
387 | read_state_file(&sb, state, "rerere-autoupdate", 1); | |
388 | state->allow_rerere_autoupdate = strcmp(sb.buf, "t") ? | |
389 | RERERE_NOAUTOUPDATE : RERERE_AUTOUPDATE; | |
390 | } else { | |
391 | state->allow_rerere_autoupdate = 0; | |
392 | } | |
393 | ||
4f1b6961 PT |
394 | read_state_file(&sb, state, "keep", 1); |
395 | if (!strcmp(sb.buf, "t")) | |
396 | state->keep = KEEP_TRUE; | |
397 | else if (!strcmp(sb.buf, "b")) | |
398 | state->keep = KEEP_NON_PATCH; | |
399 | else | |
400 | state->keep = KEEP_FALSE; | |
401 | ||
702cbaad PT |
402 | read_state_file(&sb, state, "messageid", 1); |
403 | state->message_id = !strcmp(sb.buf, "t"); | |
404 | ||
9b646617 PT |
405 | read_state_file(&sb, state, "scissors", 1); |
406 | if (!strcmp(sb.buf, "t")) | |
407 | state->scissors = SCISSORS_TRUE; | |
408 | else if (!strcmp(sb.buf, "f")) | |
409 | state->scissors = SCISSORS_FALSE; | |
410 | else | |
411 | state->scissors = SCISSORS_UNSET; | |
412 | ||
59b519ab ĐTCD |
413 | read_state_file(&sb, state, "quoted-cr", 1); |
414 | if (!*sb.buf) | |
415 | state->quoted_cr = quoted_cr_unset; | |
416 | else if (mailinfo_parse_quoted_cr_action(sb.buf, &state->quoted_cr) != 0) | |
417 | die(_("could not parse %s"), am_path(state, "quoted-cr")); | |
418 | ||
257e8cec | 419 | read_state_file(&sb, state, "apply-opt", 1); |
22f9b7f3 | 420 | strvec_clear(&state->git_apply_opts); |
2745b6b4 | 421 | if (sq_dequote_to_strvec(sb.buf, &state->git_apply_opts) < 0) |
257e8cec PT |
422 | die(_("could not parse %s"), am_path(state, "apply-opt")); |
423 | ||
35bdcc59 PT |
424 | state->rebasing = !!file_exists(am_path(state, "rebasing")); |
425 | ||
8c3bd9e2 PT |
426 | strbuf_release(&sb); |
427 | } | |
428 | ||
429 | /** | |
430 | * Removes the am_state directory, forcefully terminating the current am | |
431 | * session. | |
432 | */ | |
433 | static void am_destroy(const struct am_state *state) | |
434 | { | |
435 | struct strbuf sb = STRBUF_INIT; | |
436 | ||
437 | strbuf_addstr(&sb, state->dir); | |
438 | remove_dir_recursively(&sb, 0); | |
439 | strbuf_release(&sb); | |
440 | } | |
441 | ||
b8803d8f PT |
442 | /** |
443 | * Runs applypatch-msg hook. Returns its exit code. | |
444 | */ | |
445 | static int run_applypatch_msg_hook(struct am_state *state) | |
446 | { | |
447 | int ret; | |
448 | ||
449 | assert(state->msg); | |
450 | ret = run_hook_le(NULL, "applypatch-msg", am_path(state, "final-commit"), NULL); | |
451 | ||
452 | if (!ret) { | |
6a83d902 | 453 | FREE_AND_NULL(state->msg); |
b8803d8f PT |
454 | if (read_commit_msg(state) < 0) |
455 | die(_("'%s' was deleted by the applypatch-msg hook"), | |
456 | am_path(state, "final-commit")); | |
457 | } | |
458 | ||
459 | return ret; | |
460 | } | |
461 | ||
13b97ea5 PT |
462 | /** |
463 | * Runs post-rewrite hook. Returns it exit code. | |
464 | */ | |
465 | static int run_post_rewrite_hook(const struct am_state *state) | |
466 | { | |
467 | struct child_process cp = CHILD_PROCESS_INIT; | |
468 | const char *hook = find_hook("post-rewrite"); | |
469 | int ret; | |
470 | ||
471 | if (!hook) | |
472 | return 0; | |
473 | ||
22f9b7f3 JK |
474 | strvec_push(&cp.args, hook); |
475 | strvec_push(&cp.args, "rebase"); | |
13b97ea5 PT |
476 | |
477 | cp.in = xopen(am_path(state, "rewritten"), O_RDONLY); | |
478 | cp.stdout_to_stderr = 1; | |
6206286e | 479 | cp.trace2_hook_name = "post-rewrite"; |
13b97ea5 PT |
480 | |
481 | ret = run_command(&cp); | |
482 | ||
483 | close(cp.in); | |
484 | return ret; | |
485 | } | |
486 | ||
88b291fe PT |
487 | /** |
488 | * Reads the state directory's "rewritten" file, and copies notes from the old | |
489 | * commits listed in the file to their rewritten commits. | |
490 | * | |
491 | * Returns 0 on success, -1 on failure. | |
492 | */ | |
493 | static int copy_notes_for_rebase(const struct am_state *state) | |
494 | { | |
495 | struct notes_rewrite_cfg *c; | |
496 | struct strbuf sb = STRBUF_INIT; | |
497 | const char *invalid_line = _("Malformed input line: '%s'."); | |
498 | const char *msg = "Notes added by 'git rebase'"; | |
499 | FILE *fp; | |
500 | int ret = 0; | |
501 | ||
502 | assert(state->rebasing); | |
503 | ||
504 | c = init_copy_notes_for_rewrite("rebase"); | |
505 | if (!c) | |
506 | return 0; | |
507 | ||
508 | fp = xfopen(am_path(state, "rewritten"), "r"); | |
509 | ||
8f309aeb | 510 | while (!strbuf_getline_lf(&sb, fp)) { |
8c88769b | 511 | struct object_id from_obj, to_obj; |
24dd363e | 512 | const char *p; |
88b291fe | 513 | |
24dd363e | 514 | if (sb.len != the_hash_algo->hexsz * 2 + 1) { |
88b291fe PT |
515 | ret = error(invalid_line, sb.buf); |
516 | goto finish; | |
517 | } | |
518 | ||
24dd363e | 519 | if (parse_oid_hex(sb.buf, &from_obj, &p)) { |
88b291fe PT |
520 | ret = error(invalid_line, sb.buf); |
521 | goto finish; | |
522 | } | |
523 | ||
24dd363e | 524 | if (*p != ' ') { |
88b291fe PT |
525 | ret = error(invalid_line, sb.buf); |
526 | goto finish; | |
527 | } | |
528 | ||
24dd363e | 529 | if (get_oid_hex(p + 1, &to_obj)) { |
88b291fe PT |
530 | ret = error(invalid_line, sb.buf); |
531 | goto finish; | |
532 | } | |
533 | ||
bb7e4739 | 534 | if (copy_note_for_rewrite(c, &from_obj, &to_obj)) |
88b291fe | 535 | ret = error(_("Failed to copy notes from '%s' to '%s'"), |
8c88769b | 536 | oid_to_hex(&from_obj), oid_to_hex(&to_obj)); |
88b291fe PT |
537 | } |
538 | ||
539 | finish: | |
1d18d758 | 540 | finish_copy_notes_for_rewrite(the_repository, c, msg); |
88b291fe PT |
541 | fclose(fp); |
542 | strbuf_release(&sb); | |
543 | return ret; | |
544 | } | |
545 | ||
c29807b2 PT |
546 | /** |
547 | * Determines if the file looks like a piece of RFC2822 mail by grabbing all | |
548 | * non-indented lines and checking if they look like they begin with valid | |
549 | * header field names. | |
550 | * | |
551 | * Returns 1 if the file looks like a piece of mail, 0 otherwise. | |
552 | */ | |
553 | static int is_mail(FILE *fp) | |
554 | { | |
555 | const char *header_regex = "^[!-9;-~]+:"; | |
556 | struct strbuf sb = STRBUF_INIT; | |
557 | regex_t regex; | |
558 | int ret = 1; | |
559 | ||
560 | if (fseek(fp, 0L, SEEK_SET)) | |
561 | die_errno(_("fseek failed")); | |
562 | ||
563 | if (regcomp(®ex, header_regex, REG_NOSUB | REG_EXTENDED)) | |
564 | die("invalid pattern: %s", header_regex); | |
565 | ||
1a0c8dfd | 566 | while (!strbuf_getline(&sb, fp)) { |
c29807b2 PT |
567 | if (!sb.len) |
568 | break; /* End of header */ | |
569 | ||
570 | /* Ignore indented folded lines */ | |
571 | if (*sb.buf == '\t' || *sb.buf == ' ') | |
572 | continue; | |
573 | ||
574 | /* It's a header if it matches header_regex */ | |
575 | if (regexec(®ex, sb.buf, 0, NULL, 0)) { | |
576 | ret = 0; | |
577 | goto done; | |
578 | } | |
579 | } | |
580 | ||
581 | done: | |
582 | regfree(®ex); | |
583 | strbuf_release(&sb); | |
584 | return ret; | |
585 | } | |
586 | ||
587 | /** | |
588 | * Attempts to detect the patch_format of the patches contained in `paths`, | |
589 | * returning the PATCH_FORMAT_* enum value. Returns PATCH_FORMAT_UNKNOWN if | |
590 | * detection fails. | |
591 | */ | |
592 | static int detect_patch_format(const char **paths) | |
593 | { | |
594 | enum patch_format ret = PATCH_FORMAT_UNKNOWN; | |
595 | struct strbuf l1 = STRBUF_INIT; | |
5ae41c79 PT |
596 | struct strbuf l2 = STRBUF_INIT; |
597 | struct strbuf l3 = STRBUF_INIT; | |
c29807b2 PT |
598 | FILE *fp; |
599 | ||
600 | /* | |
601 | * We default to mbox format if input is from stdin and for directories | |
602 | */ | |
603 | if (!*paths || !strcmp(*paths, "-") || is_directory(*paths)) | |
604 | return PATCH_FORMAT_MBOX; | |
605 | ||
606 | /* | |
607 | * Otherwise, check the first few lines of the first patch, starting | |
608 | * from the first non-blank line, to try to detect its format. | |
609 | */ | |
610 | ||
611 | fp = xfopen(*paths, "r"); | |
612 | ||
1a0c8dfd | 613 | while (!strbuf_getline(&l1, fp)) { |
c29807b2 PT |
614 | if (l1.len) |
615 | break; | |
616 | } | |
617 | ||
618 | if (starts_with(l1.buf, "From ") || starts_with(l1.buf, "From: ")) { | |
619 | ret = PATCH_FORMAT_MBOX; | |
620 | goto done; | |
621 | } | |
622 | ||
336108c1 PT |
623 | if (starts_with(l1.buf, "# This series applies on GIT commit")) { |
624 | ret = PATCH_FORMAT_STGIT_SERIES; | |
625 | goto done; | |
626 | } | |
627 | ||
94cd175c PT |
628 | if (!strcmp(l1.buf, "# HG changeset patch")) { |
629 | ret = PATCH_FORMAT_HG; | |
630 | goto done; | |
631 | } | |
632 | ||
1a0c8dfd | 633 | strbuf_getline(&l2, fp); |
1a0c8dfd | 634 | strbuf_getline(&l3, fp); |
5ae41c79 PT |
635 | |
636 | /* | |
637 | * If the second line is empty and the third is a From, Author or Date | |
638 | * entry, this is likely an StGit patch. | |
639 | */ | |
640 | if (l1.len && !l2.len && | |
641 | (starts_with(l3.buf, "From:") || | |
642 | starts_with(l3.buf, "Author:") || | |
643 | starts_with(l3.buf, "Date:"))) { | |
644 | ret = PATCH_FORMAT_STGIT; | |
645 | goto done; | |
646 | } | |
647 | ||
c29807b2 PT |
648 | if (l1.len && is_mail(fp)) { |
649 | ret = PATCH_FORMAT_MBOX; | |
650 | goto done; | |
651 | } | |
652 | ||
653 | done: | |
654 | fclose(fp); | |
655 | strbuf_release(&l1); | |
542627a4 RS |
656 | strbuf_release(&l2); |
657 | strbuf_release(&l3); | |
c29807b2 PT |
658 | return ret; |
659 | } | |
660 | ||
11c2177f PT |
661 | /** |
662 | * Splits out individual email patches from `paths`, where each path is either | |
663 | * a mbox file or a Maildir. Returns 0 on success, -1 on failure. | |
664 | */ | |
d9925d1a EW |
665 | static int split_mail_mbox(struct am_state *state, const char **paths, |
666 | int keep_cr, int mboxrd) | |
11c2177f PT |
667 | { |
668 | struct child_process cp = CHILD_PROCESS_INIT; | |
669 | struct strbuf last = STRBUF_INIT; | |
1b090735 | 670 | int ret; |
11c2177f PT |
671 | |
672 | cp.git_cmd = 1; | |
22f9b7f3 JK |
673 | strvec_push(&cp.args, "mailsplit"); |
674 | strvec_pushf(&cp.args, "-d%d", state->prec); | |
675 | strvec_pushf(&cp.args, "-o%s", state->dir); | |
676 | strvec_push(&cp.args, "-b"); | |
5d123a40 | 677 | if (keep_cr) |
22f9b7f3 | 678 | strvec_push(&cp.args, "--keep-cr"); |
d9925d1a | 679 | if (mboxrd) |
22f9b7f3 JK |
680 | strvec_push(&cp.args, "--mboxrd"); |
681 | strvec_push(&cp.args, "--"); | |
682 | strvec_pushv(&cp.args, paths); | |
11c2177f | 683 | |
1b090735 RS |
684 | ret = capture_command(&cp, &last, 8); |
685 | if (ret) | |
686 | goto exit; | |
11c2177f PT |
687 | |
688 | state->cur = 1; | |
689 | state->last = strtol(last.buf, NULL, 10); | |
690 | ||
1b090735 RS |
691 | exit: |
692 | strbuf_release(&last); | |
693 | return ret ? -1 : 0; | |
11c2177f PT |
694 | } |
695 | ||
5ae41c79 PT |
696 | /** |
697 | * Callback signature for split_mail_conv(). The foreign patch should be | |
698 | * read from `in`, and the converted patch (in RFC2822 mail format) should be | |
699 | * written to `out`. Return 0 on success, or -1 on failure. | |
700 | */ | |
701 | typedef int (*mail_conv_fn)(FILE *out, FILE *in, int keep_cr); | |
702 | ||
703 | /** | |
704 | * Calls `fn` for each file in `paths` to convert the foreign patch to the | |
705 | * RFC2822 mail format suitable for parsing with git-mailinfo. | |
706 | * | |
707 | * Returns 0 on success, -1 on failure. | |
708 | */ | |
709 | static int split_mail_conv(mail_conv_fn fn, struct am_state *state, | |
710 | const char **paths, int keep_cr) | |
711 | { | |
712 | static const char *stdin_only[] = {"-", NULL}; | |
713 | int i; | |
714 | ||
715 | if (!*paths) | |
716 | paths = stdin_only; | |
717 | ||
718 | for (i = 0; *paths; paths++, i++) { | |
719 | FILE *in, *out; | |
720 | const char *mail; | |
721 | int ret; | |
722 | ||
723 | if (!strcmp(*paths, "-")) | |
724 | in = stdin; | |
725 | else | |
726 | in = fopen(*paths, "r"); | |
727 | ||
728 | if (!in) | |
6e59e9c0 NTND |
729 | return error_errno(_("could not open '%s' for reading"), |
730 | *paths); | |
5ae41c79 PT |
731 | |
732 | mail = mkpath("%s/%0*d", state->dir, state->prec, i + 1); | |
733 | ||
734 | out = fopen(mail, "w"); | |
ac8ce18d RS |
735 | if (!out) { |
736 | if (in != stdin) | |
737 | fclose(in); | |
6e59e9c0 NTND |
738 | return error_errno(_("could not open '%s' for writing"), |
739 | mail); | |
ac8ce18d | 740 | } |
5ae41c79 PT |
741 | |
742 | ret = fn(out, in, keep_cr); | |
743 | ||
744 | fclose(out); | |
ac8ce18d RS |
745 | if (in != stdin) |
746 | fclose(in); | |
5ae41c79 PT |
747 | |
748 | if (ret) | |
749 | return error(_("could not parse patch '%s'"), *paths); | |
750 | } | |
751 | ||
752 | state->cur = 1; | |
753 | state->last = i; | |
754 | return 0; | |
755 | } | |
756 | ||
757 | /** | |
758 | * A split_mail_conv() callback that converts an StGit patch to an RFC2822 | |
759 | * message suitable for parsing with git-mailinfo. | |
760 | */ | |
761 | static int stgit_patch_to_mail(FILE *out, FILE *in, int keep_cr) | |
762 | { | |
763 | struct strbuf sb = STRBUF_INIT; | |
764 | int subject_printed = 0; | |
765 | ||
8f309aeb | 766 | while (!strbuf_getline_lf(&sb, in)) { |
5ae41c79 PT |
767 | const char *str; |
768 | ||
769 | if (str_isspace(sb.buf)) | |
770 | continue; | |
771 | else if (skip_prefix(sb.buf, "Author:", &str)) | |
772 | fprintf(out, "From:%s\n", str); | |
773 | else if (starts_with(sb.buf, "From") || starts_with(sb.buf, "Date")) | |
774 | fprintf(out, "%s\n", sb.buf); | |
775 | else if (!subject_printed) { | |
776 | fprintf(out, "Subject: %s\n", sb.buf); | |
777 | subject_printed = 1; | |
778 | } else { | |
779 | fprintf(out, "\n%s\n", sb.buf); | |
780 | break; | |
781 | } | |
782 | } | |
783 | ||
784 | strbuf_reset(&sb); | |
785 | while (strbuf_fread(&sb, 8192, in) > 0) { | |
786 | fwrite(sb.buf, 1, sb.len, out); | |
787 | strbuf_reset(&sb); | |
788 | } | |
789 | ||
790 | strbuf_release(&sb); | |
791 | return 0; | |
792 | } | |
793 | ||
336108c1 PT |
794 | /** |
795 | * This function only supports a single StGit series file in `paths`. | |
796 | * | |
797 | * Given an StGit series file, converts the StGit patches in the series into | |
798 | * RFC2822 messages suitable for parsing with git-mailinfo, and queues them in | |
799 | * the state directory. | |
800 | * | |
801 | * Returns 0 on success, -1 on failure. | |
802 | */ | |
803 | static int split_mail_stgit_series(struct am_state *state, const char **paths, | |
804 | int keep_cr) | |
805 | { | |
806 | const char *series_dir; | |
807 | char *series_dir_buf; | |
808 | FILE *fp; | |
22f9b7f3 | 809 | struct strvec patches = STRVEC_INIT; |
336108c1 PT |
810 | struct strbuf sb = STRBUF_INIT; |
811 | int ret; | |
812 | ||
813 | if (!paths[0] || paths[1]) | |
814 | return error(_("Only one StGIT patch series can be applied at once")); | |
815 | ||
816 | series_dir_buf = xstrdup(*paths); | |
817 | series_dir = dirname(series_dir_buf); | |
818 | ||
819 | fp = fopen(*paths, "r"); | |
820 | if (!fp) | |
6e59e9c0 | 821 | return error_errno(_("could not open '%s' for reading"), *paths); |
336108c1 | 822 | |
8f309aeb | 823 | while (!strbuf_getline_lf(&sb, fp)) { |
336108c1 PT |
824 | if (*sb.buf == '#') |
825 | continue; /* skip comment lines */ | |
826 | ||
22f9b7f3 | 827 | strvec_push(&patches, mkpath("%s/%s", series_dir, sb.buf)); |
336108c1 PT |
828 | } |
829 | ||
830 | fclose(fp); | |
831 | strbuf_release(&sb); | |
832 | free(series_dir_buf); | |
833 | ||
d70a9eb6 | 834 | ret = split_mail_conv(stgit_patch_to_mail, state, patches.v, keep_cr); |
336108c1 | 835 | |
22f9b7f3 | 836 | strvec_clear(&patches); |
336108c1 PT |
837 | return ret; |
838 | } | |
839 | ||
94cd175c PT |
840 | /** |
841 | * A split_patches_conv() callback that converts a mercurial patch to a RFC2822 | |
842 | * message suitable for parsing with git-mailinfo. | |
843 | */ | |
844 | static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr) | |
845 | { | |
846 | struct strbuf sb = STRBUF_INIT; | |
b36474ff | 847 | int rc = 0; |
94cd175c | 848 | |
8f309aeb | 849 | while (!strbuf_getline_lf(&sb, in)) { |
94cd175c PT |
850 | const char *str; |
851 | ||
852 | if (skip_prefix(sb.buf, "# User ", &str)) | |
853 | fprintf(out, "From: %s\n", str); | |
854 | else if (skip_prefix(sb.buf, "# Date ", &str)) { | |
dddbad72 | 855 | timestamp_t timestamp; |
94cd175c PT |
856 | long tz, tz2; |
857 | char *end; | |
858 | ||
859 | errno = 0; | |
1aeb7e75 | 860 | timestamp = parse_timestamp(str, &end, 10); |
b36474ff RS |
861 | if (errno) { |
862 | rc = error(_("invalid timestamp")); | |
863 | goto exit; | |
864 | } | |
94cd175c | 865 | |
b36474ff RS |
866 | if (!skip_prefix(end, " ", &str)) { |
867 | rc = error(_("invalid Date line")); | |
868 | goto exit; | |
869 | } | |
94cd175c PT |
870 | |
871 | errno = 0; | |
872 | tz = strtol(str, &end, 10); | |
b36474ff RS |
873 | if (errno) { |
874 | rc = error(_("invalid timezone offset")); | |
875 | goto exit; | |
876 | } | |
94cd175c | 877 | |
b36474ff RS |
878 | if (*end) { |
879 | rc = error(_("invalid Date line")); | |
880 | goto exit; | |
881 | } | |
94cd175c PT |
882 | |
883 | /* | |
884 | * mercurial's timezone is in seconds west of UTC, | |
885 | * however git's timezone is in hours + minutes east of | |
886 | * UTC. Convert it. | |
887 | */ | |
888 | tz2 = labs(tz) / 3600 * 100 + labs(tz) % 3600 / 60; | |
889 | if (tz > 0) | |
890 | tz2 = -tz2; | |
891 | ||
892 | fprintf(out, "Date: %s\n", show_date(timestamp, tz2, DATE_MODE(RFC2822))); | |
893 | } else if (starts_with(sb.buf, "# ")) { | |
894 | continue; | |
895 | } else { | |
896 | fprintf(out, "\n%s\n", sb.buf); | |
897 | break; | |
898 | } | |
899 | } | |
900 | ||
901 | strbuf_reset(&sb); | |
902 | while (strbuf_fread(&sb, 8192, in) > 0) { | |
903 | fwrite(sb.buf, 1, sb.len, out); | |
904 | strbuf_reset(&sb); | |
905 | } | |
b36474ff | 906 | exit: |
94cd175c | 907 | strbuf_release(&sb); |
b36474ff | 908 | return rc; |
94cd175c PT |
909 | } |
910 | ||
11c2177f PT |
911 | /** |
912 | * Splits a list of files/directories into individual email patches. Each path | |
913 | * in `paths` must be a file/directory that is formatted according to | |
914 | * `patch_format`. | |
915 | * | |
916 | * Once split out, the individual email patches will be stored in the state | |
917 | * directory, with each patch's filename being its index, padded to state->prec | |
918 | * digits. | |
919 | * | |
920 | * state->cur will be set to the index of the first mail, and state->last will | |
921 | * be set to the index of the last mail. | |
922 | * | |
5d123a40 PT |
923 | * Set keep_cr to 0 to convert all lines ending with \r\n to end with \n, 1 |
924 | * to disable this behavior, -1 to use the default configured setting. | |
925 | * | |
11c2177f PT |
926 | * Returns 0 on success, -1 on failure. |
927 | */ | |
928 | static int split_mail(struct am_state *state, enum patch_format patch_format, | |
5d123a40 | 929 | const char **paths, int keep_cr) |
11c2177f | 930 | { |
5d123a40 PT |
931 | if (keep_cr < 0) { |
932 | keep_cr = 0; | |
933 | git_config_get_bool("am.keepcr", &keep_cr); | |
934 | } | |
935 | ||
11c2177f PT |
936 | switch (patch_format) { |
937 | case PATCH_FORMAT_MBOX: | |
d9925d1a | 938 | return split_mail_mbox(state, paths, keep_cr, 0); |
5ae41c79 PT |
939 | case PATCH_FORMAT_STGIT: |
940 | return split_mail_conv(stgit_patch_to_mail, state, paths, keep_cr); | |
336108c1 PT |
941 | case PATCH_FORMAT_STGIT_SERIES: |
942 | return split_mail_stgit_series(state, paths, keep_cr); | |
94cd175c PT |
943 | case PATCH_FORMAT_HG: |
944 | return split_mail_conv(hg_patch_to_mail, state, paths, keep_cr); | |
d9925d1a EW |
945 | case PATCH_FORMAT_MBOXRD: |
946 | return split_mail_mbox(state, paths, keep_cr, 1); | |
11c2177f | 947 | default: |
033abf97 | 948 | BUG("invalid patch_format"); |
11c2177f PT |
949 | } |
950 | return -1; | |
951 | } | |
952 | ||
8c3bd9e2 PT |
953 | /** |
954 | * Setup a new am session for applying patches | |
955 | */ | |
11c2177f | 956 | static void am_setup(struct am_state *state, enum patch_format patch_format, |
5d123a40 | 957 | const char **paths, int keep_cr) |
8c3bd9e2 | 958 | { |
8c88769b | 959 | struct object_id curr_head; |
4f1b6961 | 960 | const char *str; |
257e8cec | 961 | struct strbuf sb = STRBUF_INIT; |
33388a71 | 962 | |
c29807b2 PT |
963 | if (!patch_format) |
964 | patch_format = detect_patch_format(paths); | |
965 | ||
966 | if (!patch_format) { | |
967 | fprintf_ln(stderr, _("Patch format detection failed.")); | |
968 | exit(128); | |
969 | } | |
970 | ||
8c3bd9e2 PT |
971 | if (mkdir(state->dir, 0777) < 0 && errno != EEXIST) |
972 | die_errno(_("failed to create directory '%s'"), state->dir); | |
fbd7a232 | 973 | delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF); |
8c3bd9e2 | 974 | |
5d123a40 | 975 | if (split_mail(state, patch_format, paths, keep_cr) < 0) { |
11c2177f PT |
976 | am_destroy(state); |
977 | die(_("Failed to split patches.")); | |
978 | } | |
979 | ||
35bdcc59 PT |
980 | if (state->rebasing) |
981 | state->threeway = 1; | |
982 | ||
25b763ba JH |
983 | write_state_bool(state, "threeway", state->threeway); |
984 | write_state_bool(state, "quiet", state->quiet); | |
985 | write_state_bool(state, "sign", state->signoff); | |
986 | write_state_bool(state, "utf8", state->utf8); | |
ef7ee16d | 987 | |
fd4a3f48 PW |
988 | if (state->allow_rerere_autoupdate) |
989 | write_state_bool(state, "rerere-autoupdate", | |
990 | state->allow_rerere_autoupdate == RERERE_AUTOUPDATE); | |
991 | ||
4f1b6961 PT |
992 | switch (state->keep) { |
993 | case KEEP_FALSE: | |
994 | str = "f"; | |
995 | break; | |
996 | case KEEP_TRUE: | |
997 | str = "t"; | |
998 | break; | |
999 | case KEEP_NON_PATCH: | |
1000 | str = "b"; | |
1001 | break; | |
1002 | default: | |
033abf97 | 1003 | BUG("invalid value for state->keep"); |
4f1b6961 PT |
1004 | } |
1005 | ||
25b763ba JH |
1006 | write_state_text(state, "keep", str); |
1007 | write_state_bool(state, "messageid", state->message_id); | |
702cbaad | 1008 | |
9b646617 PT |
1009 | switch (state->scissors) { |
1010 | case SCISSORS_UNSET: | |
1011 | str = ""; | |
1012 | break; | |
1013 | case SCISSORS_FALSE: | |
1014 | str = "f"; | |
1015 | break; | |
1016 | case SCISSORS_TRUE: | |
1017 | str = "t"; | |
1018 | break; | |
1019 | default: | |
033abf97 | 1020 | BUG("invalid value for state->scissors"); |
9b646617 | 1021 | } |
25b763ba | 1022 | write_state_text(state, "scissors", str); |
9b646617 | 1023 | |
59b519ab ĐTCD |
1024 | switch (state->quoted_cr) { |
1025 | case quoted_cr_unset: | |
1026 | str = ""; | |
1027 | break; | |
1028 | case quoted_cr_nowarn: | |
1029 | str = "nowarn"; | |
1030 | break; | |
1031 | case quoted_cr_warn: | |
1032 | str = "warn"; | |
1033 | break; | |
1034 | case quoted_cr_strip: | |
1035 | str = "strip"; | |
1036 | break; | |
1037 | default: | |
1038 | BUG("invalid value for state->quoted_cr"); | |
1039 | } | |
1040 | write_state_text(state, "quoted-cr", str); | |
1041 | ||
d70a9eb6 | 1042 | sq_quote_argv(&sb, state->git_apply_opts.v); |
25b763ba | 1043 | write_state_text(state, "apply-opt", sb.buf); |
257e8cec | 1044 | |
35bdcc59 | 1045 | if (state->rebasing) |
25b763ba | 1046 | write_state_text(state, "rebasing", ""); |
35bdcc59 | 1047 | else |
25b763ba | 1048 | write_state_text(state, "applying", ""); |
35bdcc59 | 1049 | |
8c88769b | 1050 | if (!get_oid("HEAD", &curr_head)) { |
1051 | write_state_text(state, "abort-safety", oid_to_hex(&curr_head)); | |
35bdcc59 | 1052 | if (!state->rebasing) |
ae077771 | 1053 | update_ref("am", "ORIG_HEAD", &curr_head, NULL, 0, |
1054 | UPDATE_REFS_DIE_ON_ERR); | |
33388a71 | 1055 | } else { |
25b763ba | 1056 | write_state_text(state, "abort-safety", ""); |
35bdcc59 | 1057 | if (!state->rebasing) |
755b49ae | 1058 | delete_ref(NULL, "ORIG_HEAD", NULL, 0); |
33388a71 PT |
1059 | } |
1060 | ||
8c3bd9e2 PT |
1061 | /* |
1062 | * NOTE: Since the "next" and "last" files determine if an am_state | |
1063 | * session is in progress, they should be written last. | |
1064 | */ | |
1065 | ||
25b763ba JH |
1066 | write_state_count(state, "next", state->cur); |
1067 | write_state_count(state, "last", state->last); | |
257e8cec PT |
1068 | |
1069 | strbuf_release(&sb); | |
8c3bd9e2 PT |
1070 | } |
1071 | ||
1072 | /** | |
1073 | * Increments the patch pointer, and cleans am_state for the application of the | |
1074 | * next patch. | |
1075 | */ | |
1076 | static void am_next(struct am_state *state) | |
1077 | { | |
8c88769b | 1078 | struct object_id head; |
33388a71 | 1079 | |
88ce3ef6 ÆAB |
1080 | FREE_AND_NULL(state->author_name); |
1081 | FREE_AND_NULL(state->author_email); | |
1082 | FREE_AND_NULL(state->author_date); | |
1083 | FREE_AND_NULL(state->msg); | |
3e20dcf3 PT |
1084 | state->msg_len = 0; |
1085 | ||
1086 | unlink(am_path(state, "author-script")); | |
1087 | unlink(am_path(state, "final-commit")); | |
1088 | ||
8c88769b | 1089 | oidclr(&state->orig_commit); |
13b97ea5 | 1090 | unlink(am_path(state, "original-commit")); |
fbd7a232 | 1091 | delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF); |
13b97ea5 | 1092 | |
8c88769b | 1093 | if (!get_oid("HEAD", &head)) |
1094 | write_state_text(state, "abort-safety", oid_to_hex(&head)); | |
33388a71 | 1095 | else |
25b763ba | 1096 | write_state_text(state, "abort-safety", ""); |
33388a71 | 1097 | |
8c3bd9e2 | 1098 | state->cur++; |
25b763ba | 1099 | write_state_count(state, "next", state->cur); |
8c3bd9e2 PT |
1100 | } |
1101 | ||
3e20dcf3 PT |
1102 | /** |
1103 | * Returns the filename of the current patch email. | |
1104 | */ | |
1105 | static const char *msgnum(const struct am_state *state) | |
1106 | { | |
1107 | static struct strbuf sb = STRBUF_INIT; | |
1108 | ||
1109 | strbuf_reset(&sb); | |
1110 | strbuf_addf(&sb, "%0*d", state->prec, state->cur); | |
1111 | ||
1112 | return sb.buf; | |
1113 | } | |
1114 | ||
2d83109a PT |
1115 | /** |
1116 | * Dies with a user-friendly message on how to proceed after resolving the | |
1117 | * problem. This message can be overridden with state->resolvemsg. | |
1118 | */ | |
1119 | static void NORETURN die_user_resolve(const struct am_state *state) | |
1120 | { | |
1121 | if (state->resolvemsg) { | |
1122 | printf_ln("%s", state->resolvemsg); | |
1123 | } else { | |
7ff26832 | 1124 | const char *cmdline = state->interactive ? "git am -i" : "git am"; |
2d83109a PT |
1125 | |
1126 | printf_ln(_("When you have resolved this problem, run \"%s --continue\"."), cmdline); | |
1127 | printf_ln(_("If you prefer to skip this patch, run \"%s --skip\" instead."), cmdline); | |
1128 | printf_ln(_("To restore the original branch and stop patching, run \"%s --abort\"."), cmdline); | |
1129 | } | |
1130 | ||
1131 | exit(128); | |
1132 | } | |
1133 | ||
0fb3c4fc GB |
1134 | /** |
1135 | * Appends signoff to the "msg" field of the am_state. | |
1136 | */ | |
1137 | static void am_append_signoff(struct am_state *state) | |
aab84542 | 1138 | { |
0fb3c4fc | 1139 | struct strbuf sb = STRBUF_INIT; |
aab84542 | 1140 | |
0fb3c4fc | 1141 | strbuf_attach(&sb, state->msg, state->msg_len, state->msg_len); |
735285b4 | 1142 | append_signoff(&sb, 0, 0); |
b5e82359 PT |
1143 | state->msg = strbuf_detach(&sb, &state->msg_len); |
1144 | } | |
1145 | ||
3e20dcf3 PT |
1146 | /** |
1147 | * Parses `mail` using git-mailinfo, extracting its patch and authorship info. | |
1148 | * state->msg will be set to the patch message. state->author_name, | |
1149 | * state->author_email and state->author_date will be set to the patch author's | |
1150 | * name, email and date respectively. The patch body will be written to the | |
1151 | * state directory's "patch" file. | |
1152 | * | |
1153 | * Returns 1 if the patch should be skipped, 0 otherwise. | |
1154 | */ | |
1155 | static int parse_mail(struct am_state *state, const char *mail) | |
1156 | { | |
1157 | FILE *fp; | |
3e20dcf3 PT |
1158 | struct strbuf sb = STRBUF_INIT; |
1159 | struct strbuf msg = STRBUF_INIT; | |
1160 | struct strbuf author_name = STRBUF_INIT; | |
1161 | struct strbuf author_date = STRBUF_INIT; | |
1162 | struct strbuf author_email = STRBUF_INIT; | |
1163 | int ret = 0; | |
4b98bae2 | 1164 | struct mailinfo mi; |
3e20dcf3 | 1165 | |
4b98bae2 | 1166 | setup_mailinfo(&mi); |
3e20dcf3 | 1167 | |
4b98bae2 JH |
1168 | if (state->utf8) |
1169 | mi.metainfo_charset = get_commit_output_encoding(); | |
1170 | else | |
1171 | mi.metainfo_charset = NULL; | |
4f1b6961 PT |
1172 | |
1173 | switch (state->keep) { | |
1174 | case KEEP_FALSE: | |
1175 | break; | |
1176 | case KEEP_TRUE: | |
4b98bae2 | 1177 | mi.keep_subject = 1; |
4f1b6961 PT |
1178 | break; |
1179 | case KEEP_NON_PATCH: | |
4b98bae2 | 1180 | mi.keep_non_patch_brackets_in_subject = 1; |
4f1b6961 PT |
1181 | break; |
1182 | default: | |
033abf97 | 1183 | BUG("invalid value for state->keep"); |
4f1b6961 PT |
1184 | } |
1185 | ||
702cbaad | 1186 | if (state->message_id) |
4b98bae2 | 1187 | mi.add_message_id = 1; |
702cbaad | 1188 | |
9b646617 PT |
1189 | switch (state->scissors) { |
1190 | case SCISSORS_UNSET: | |
1191 | break; | |
1192 | case SCISSORS_FALSE: | |
4b98bae2 | 1193 | mi.use_scissors = 0; |
9b646617 PT |
1194 | break; |
1195 | case SCISSORS_TRUE: | |
4b98bae2 | 1196 | mi.use_scissors = 1; |
9b646617 PT |
1197 | break; |
1198 | default: | |
033abf97 | 1199 | BUG("invalid value for state->scissors"); |
9b646617 PT |
1200 | } |
1201 | ||
59b519ab ĐTCD |
1202 | switch (state->quoted_cr) { |
1203 | case quoted_cr_unset: | |
1204 | break; | |
1205 | case quoted_cr_nowarn: | |
1206 | case quoted_cr_warn: | |
1207 | case quoted_cr_strip: | |
1208 | mi.quoted_cr = state->quoted_cr; | |
1209 | break; | |
1210 | default: | |
1211 | BUG("invalid value for state->quoted_cr"); | |
1212 | } | |
1213 | ||
23a9e071 NTND |
1214 | mi.input = xfopen(mail, "r"); |
1215 | mi.output = xfopen(am_path(state, "info"), "w"); | |
4b98bae2 | 1216 | if (mailinfo(&mi, am_path(state, "msg"), am_path(state, "patch"))) |
3e20dcf3 PT |
1217 | die("could not parse patch"); |
1218 | ||
4b98bae2 JH |
1219 | fclose(mi.input); |
1220 | fclose(mi.output); | |
3e20dcf3 | 1221 | |
3aa4d81f RS |
1222 | if (mi.format_flowed) |
1223 | warning(_("Patch sent with format=flowed; " | |
1224 | "space at the end of lines might be lost.")); | |
1225 | ||
3e20dcf3 PT |
1226 | /* Extract message and author information */ |
1227 | fp = xfopen(am_path(state, "info"), "r"); | |
8f309aeb | 1228 | while (!strbuf_getline_lf(&sb, fp)) { |
3e20dcf3 PT |
1229 | const char *x; |
1230 | ||
1231 | if (skip_prefix(sb.buf, "Subject: ", &x)) { | |
1232 | if (msg.len) | |
1233 | strbuf_addch(&msg, '\n'); | |
1234 | strbuf_addstr(&msg, x); | |
1235 | } else if (skip_prefix(sb.buf, "Author: ", &x)) | |
1236 | strbuf_addstr(&author_name, x); | |
1237 | else if (skip_prefix(sb.buf, "Email: ", &x)) | |
1238 | strbuf_addstr(&author_email, x); | |
1239 | else if (skip_prefix(sb.buf, "Date: ", &x)) | |
1240 | strbuf_addstr(&author_date, x); | |
1241 | } | |
1242 | fclose(fp); | |
1243 | ||
1244 | /* Skip pine's internal folder data */ | |
1245 | if (!strcmp(author_name.buf, "Mail System Internal Data")) { | |
1246 | ret = 1; | |
1247 | goto finish; | |
1248 | } | |
1249 | ||
e3b1e3bd | 1250 | if (is_empty_or_missing_file(am_path(state, "patch"))) { |
6c486862 | 1251 | printf_ln(_("Patch is empty.")); |
2d83109a | 1252 | die_user_resolve(state); |
3e20dcf3 PT |
1253 | } |
1254 | ||
1255 | strbuf_addstr(&msg, "\n\n"); | |
4b98bae2 | 1256 | strbuf_addbuf(&msg, &mi.log_message); |
63af4a84 | 1257 | strbuf_stripspace(&msg, 0); |
3e20dcf3 PT |
1258 | |
1259 | assert(!state->author_name); | |
1260 | state->author_name = strbuf_detach(&author_name, NULL); | |
1261 | ||
1262 | assert(!state->author_email); | |
1263 | state->author_email = strbuf_detach(&author_email, NULL); | |
1264 | ||
1265 | assert(!state->author_date); | |
1266 | state->author_date = strbuf_detach(&author_date, NULL); | |
1267 | ||
1268 | assert(!state->msg); | |
1269 | state->msg = strbuf_detach(&msg, &state->msg_len); | |
1270 | ||
1271 | finish: | |
1272 | strbuf_release(&msg); | |
1273 | strbuf_release(&author_date); | |
1274 | strbuf_release(&author_email); | |
1275 | strbuf_release(&author_name); | |
1276 | strbuf_release(&sb); | |
4b98bae2 | 1277 | clear_mailinfo(&mi); |
3e20dcf3 PT |
1278 | return ret; |
1279 | } | |
1280 | ||
df2760a5 PT |
1281 | /** |
1282 | * Sets commit_id to the commit hash where the mail was generated from. | |
1283 | * Returns 0 on success, -1 on failure. | |
1284 | */ | |
8c88769b | 1285 | static int get_mail_commit_oid(struct object_id *commit_id, const char *mail) |
df2760a5 PT |
1286 | { |
1287 | struct strbuf sb = STRBUF_INIT; | |
1288 | FILE *fp = xfopen(mail, "r"); | |
1289 | const char *x; | |
5b34ba41 | 1290 | int ret = 0; |
df2760a5 | 1291 | |
5b34ba41 JS |
1292 | if (strbuf_getline_lf(&sb, fp) || |
1293 | !skip_prefix(sb.buf, "From ", &x) || | |
1294 | get_oid_hex(x, commit_id) < 0) | |
1295 | ret = -1; | |
df2760a5 PT |
1296 | |
1297 | strbuf_release(&sb); | |
1298 | fclose(fp); | |
5b34ba41 | 1299 | return ret; |
df2760a5 PT |
1300 | } |
1301 | ||
1302 | /** | |
1303 | * Sets state->msg, state->author_name, state->author_email, state->author_date | |
1304 | * to the commit's respective info. | |
1305 | */ | |
1306 | static void get_commit_info(struct am_state *state, struct commit *commit) | |
1307 | { | |
2e2bbb96 | 1308 | const char *buffer, *ident_line, *msg; |
df2760a5 | 1309 | size_t ident_len; |
721f5f1e | 1310 | struct ident_split id; |
df2760a5 PT |
1311 | |
1312 | buffer = logmsg_reencode(commit, NULL, get_commit_output_encoding()); | |
1313 | ||
1314 | ident_line = find_commit_header(buffer, "author", &ident_len); | |
0dfed92d JK |
1315 | if (!ident_line) |
1316 | die(_("missing author line in commit %s"), | |
1317 | oid_to_hex(&commit->object.oid)); | |
721f5f1e | 1318 | if (split_ident_line(&id, ident_line, ident_len) < 0) |
2e2bbb96 | 1319 | die(_("invalid ident line: %.*s"), (int)ident_len, ident_line); |
df2760a5 PT |
1320 | |
1321 | assert(!state->author_name); | |
721f5f1e | 1322 | if (id.name_begin) |
2e2bbb96 | 1323 | state->author_name = |
721f5f1e JK |
1324 | xmemdupz(id.name_begin, id.name_end - id.name_begin); |
1325 | else | |
df2760a5 PT |
1326 | state->author_name = xstrdup(""); |
1327 | ||
1328 | assert(!state->author_email); | |
721f5f1e | 1329 | if (id.mail_begin) |
2e2bbb96 | 1330 | state->author_email = |
721f5f1e JK |
1331 | xmemdupz(id.mail_begin, id.mail_end - id.mail_begin); |
1332 | else | |
df2760a5 PT |
1333 | state->author_email = xstrdup(""); |
1334 | ||
df2760a5 | 1335 | assert(!state->author_date); |
721f5f1e | 1336 | state->author_date = xstrdup(show_ident_date(&id, DATE_MODE(NORMAL))); |
df2760a5 PT |
1337 | |
1338 | assert(!state->msg); | |
1339 | msg = strstr(buffer, "\n\n"); | |
1340 | if (!msg) | |
f2fd0760 | 1341 | die(_("unable to parse commit %s"), oid_to_hex(&commit->object.oid)); |
df2760a5 PT |
1342 | state->msg = xstrdup(msg + 2); |
1343 | state->msg_len = strlen(state->msg); | |
f131db9e | 1344 | unuse_commit_buffer(commit, buffer); |
df2760a5 PT |
1345 | } |
1346 | ||
1347 | /** | |
1348 | * Writes `commit` as a patch to the state directory's "patch" file. | |
1349 | */ | |
1350 | static void write_commit_patch(const struct am_state *state, struct commit *commit) | |
1351 | { | |
1352 | struct rev_info rev_info; | |
1353 | FILE *fp; | |
1354 | ||
1355 | fp = xfopen(am_path(state, "patch"), "w"); | |
2abf3503 | 1356 | repo_init_revisions(the_repository, &rev_info, NULL); |
df2760a5 PT |
1357 | rev_info.diff = 1; |
1358 | rev_info.abbrev = 0; | |
1359 | rev_info.disable_stdin = 1; | |
1360 | rev_info.show_root_diff = 1; | |
1361 | rev_info.diffopt.output_format = DIFF_FORMAT_PATCH; | |
1362 | rev_info.no_commit_id = 1; | |
0d1e0e78 BW |
1363 | rev_info.diffopt.flags.binary = 1; |
1364 | rev_info.diffopt.flags.full_index = 1; | |
df2760a5 PT |
1365 | rev_info.diffopt.use_color = 0; |
1366 | rev_info.diffopt.file = fp; | |
1367 | rev_info.diffopt.close_file = 1; | |
1368 | add_pending_object(&rev_info, &commit->object, ""); | |
1369 | diff_setup_done(&rev_info.diffopt); | |
1370 | log_tree_commit(&rev_info, commit); | |
1371 | } | |
1372 | ||
7ff26832 PT |
1373 | /** |
1374 | * Writes the diff of the index against HEAD as a patch to the state | |
1375 | * directory's "patch" file. | |
1376 | */ | |
1377 | static void write_index_patch(const struct am_state *state) | |
1378 | { | |
1379 | struct tree *tree; | |
8c88769b | 1380 | struct object_id head; |
7ff26832 PT |
1381 | struct rev_info rev_info; |
1382 | FILE *fp; | |
1383 | ||
7663e438 JK |
1384 | if (!get_oid("HEAD", &head)) { |
1385 | struct commit *commit = lookup_commit_or_die(&head, "HEAD"); | |
1386 | tree = get_commit_tree(commit); | |
1387 | } else | |
f86bcc7b SB |
1388 | tree = lookup_tree(the_repository, |
1389 | the_repository->hash_algo->empty_tree); | |
7ff26832 PT |
1390 | |
1391 | fp = xfopen(am_path(state, "patch"), "w"); | |
2abf3503 | 1392 | repo_init_revisions(the_repository, &rev_info, NULL); |
7ff26832 PT |
1393 | rev_info.diff = 1; |
1394 | rev_info.disable_stdin = 1; | |
1395 | rev_info.no_commit_id = 1; | |
1396 | rev_info.diffopt.output_format = DIFF_FORMAT_PATCH; | |
1397 | rev_info.diffopt.use_color = 0; | |
1398 | rev_info.diffopt.file = fp; | |
1399 | rev_info.diffopt.close_file = 1; | |
1400 | add_pending_object(&rev_info, &tree->object, ""); | |
1401 | diff_setup_done(&rev_info.diffopt); | |
1402 | run_diff_index(&rev_info, 1); | |
1403 | } | |
1404 | ||
df2760a5 PT |
1405 | /** |
1406 | * Like parse_mail(), but parses the mail by looking up its commit ID | |
1407 | * directly. This is used in --rebasing mode to bypass git-mailinfo's munging | |
1408 | * of patches. | |
1409 | * | |
13b97ea5 PT |
1410 | * state->orig_commit will be set to the original commit ID. |
1411 | * | |
df2760a5 PT |
1412 | * Will always return 0 as the patch should never be skipped. |
1413 | */ | |
1414 | static int parse_mail_rebase(struct am_state *state, const char *mail) | |
1415 | { | |
1416 | struct commit *commit; | |
8c88769b | 1417 | struct object_id commit_oid; |
df2760a5 | 1418 | |
8c88769b | 1419 | if (get_mail_commit_oid(&commit_oid, mail) < 0) |
df2760a5 PT |
1420 | die(_("could not parse %s"), mail); |
1421 | ||
bc83266a | 1422 | commit = lookup_commit_or_die(&commit_oid, mail); |
df2760a5 PT |
1423 | |
1424 | get_commit_info(state, commit); | |
1425 | ||
1426 | write_commit_patch(state, commit); | |
1427 | ||
8c88769b | 1428 | oidcpy(&state->orig_commit, &commit_oid); |
1429 | write_state_text(state, "original-commit", oid_to_hex(&commit_oid)); | |
fbd7a232 NTND |
1430 | update_ref("am", "REBASE_HEAD", &commit_oid, |
1431 | NULL, REF_NO_DEREF, UPDATE_REFS_DIE_ON_ERR); | |
13b97ea5 | 1432 | |
df2760a5 PT |
1433 | return 0; |
1434 | } | |
1435 | ||
38a824fe | 1436 | /** |
84f3de28 PT |
1437 | * Applies current patch with git-apply. Returns 0 on success, -1 otherwise. If |
1438 | * `index_file` is not NULL, the patch will be applied to that index. | |
38a824fe | 1439 | */ |
84f3de28 | 1440 | static int run_apply(const struct am_state *state, const char *index_file) |
38a824fe | 1441 | { |
22f9b7f3 JK |
1442 | struct strvec apply_paths = STRVEC_INIT; |
1443 | struct strvec apply_opts = STRVEC_INIT; | |
edfac5eb CC |
1444 | struct apply_state apply_state; |
1445 | int res, opts_left; | |
edfac5eb CC |
1446 | int force_apply = 0; |
1447 | int options = 0; | |
1448 | ||
82ea77ec | 1449 | if (init_apply_state(&apply_state, the_repository, NULL)) |
033abf97 | 1450 | BUG("init_apply_state() failed"); |
edfac5eb | 1451 | |
22f9b7f3 | 1452 | strvec_push(&apply_opts, "apply"); |
d70a9eb6 | 1453 | strvec_pushv(&apply_opts, state->git_apply_opts.v); |
edfac5eb | 1454 | |
d70a9eb6 | 1455 | opts_left = apply_parse_options(apply_opts.nr, apply_opts.v, |
edfac5eb CC |
1456 | &apply_state, &force_apply, &options, |
1457 | NULL); | |
1458 | ||
1459 | if (opts_left != 0) | |
1460 | die("unknown option passed through to git apply"); | |
1461 | ||
1462 | if (index_file) { | |
1463 | apply_state.index_file = index_file; | |
1464 | apply_state.cached = 1; | |
1465 | } else | |
1466 | apply_state.check_index = 1; | |
84f3de28 PT |
1467 | |
1468 | /* | |
1469 | * If we are allowed to fall back on 3-way merge, don't give false | |
1470 | * errors during the initial attempt. | |
1471 | */ | |
edfac5eb CC |
1472 | if (state->threeway && !index_file) |
1473 | apply_state.apply_verbosity = verbosity_silent; | |
84f3de28 | 1474 | |
edfac5eb | 1475 | if (check_apply_state(&apply_state, force_apply)) |
033abf97 | 1476 | BUG("check_apply_state() failed"); |
84f3de28 | 1477 | |
22f9b7f3 | 1478 | strvec_push(&apply_paths, am_path(state, "patch")); |
257e8cec | 1479 | |
d70a9eb6 | 1480 | res = apply_all_patches(&apply_state, apply_paths.nr, apply_paths.v, options); |
84f3de28 | 1481 | |
22f9b7f3 JK |
1482 | strvec_clear(&apply_paths); |
1483 | strvec_clear(&apply_opts); | |
edfac5eb | 1484 | clear_apply_state(&apply_state); |
38a824fe | 1485 | |
edfac5eb CC |
1486 | if (res) |
1487 | return res; | |
38a824fe | 1488 | |
edfac5eb CC |
1489 | if (index_file) { |
1490 | /* Reload index as apply_all_patches() will have modified it. */ | |
1491 | discard_cache(); | |
1492 | read_cache_from(index_file); | |
1493 | } | |
84f3de28 PT |
1494 | |
1495 | return 0; | |
1496 | } | |
1497 | ||
1498 | /** | |
1499 | * Builds an index that contains just the blobs needed for a 3way merge. | |
1500 | */ | |
1501 | static int build_fake_ancestor(const struct am_state *state, const char *index_file) | |
1502 | { | |
1503 | struct child_process cp = CHILD_PROCESS_INIT; | |
1504 | ||
1505 | cp.git_cmd = 1; | |
22f9b7f3 | 1506 | strvec_push(&cp.args, "apply"); |
d70a9eb6 | 1507 | strvec_pushv(&cp.args, state->git_apply_opts.v); |
22f9b7f3 JK |
1508 | strvec_pushf(&cp.args, "--build-fake-ancestor=%s", index_file); |
1509 | strvec_push(&cp.args, am_path(state, "patch")); | |
84f3de28 PT |
1510 | |
1511 | if (run_command(&cp)) | |
1512 | return -1; | |
1513 | ||
1514 | return 0; | |
1515 | } | |
1516 | ||
1517 | /** | |
1518 | * Attempt a threeway merge, using index_path as the temporary index. | |
1519 | */ | |
1520 | static int fall_back_threeway(const struct am_state *state, const char *index_path) | |
1521 | { | |
3f338f43 JS |
1522 | struct object_id orig_tree, their_tree, our_tree; |
1523 | const struct object_id *bases[1] = { &orig_tree }; | |
1524 | struct merge_options o; | |
1525 | struct commit *result; | |
1526 | char *their_tree_name; | |
84f3de28 | 1527 | |
3f338f43 | 1528 | if (get_oid("HEAD", &our_tree) < 0) |
d41836a0 | 1529 | oidcpy(&our_tree, the_hash_algo->empty_tree); |
84f3de28 PT |
1530 | |
1531 | if (build_fake_ancestor(state, index_path)) | |
1532 | return error("could not build fake ancestor"); | |
1533 | ||
1534 | discard_cache(); | |
1535 | read_cache_from(index_path); | |
1536 | ||
fc5cb99f | 1537 | if (write_index_as_tree(&orig_tree, &the_index, index_path, 0, NULL)) |
84f3de28 PT |
1538 | return error(_("Repository lacks necessary blobs to fall back on 3-way merge.")); |
1539 | ||
1540 | say(state, stdout, _("Using index info to reconstruct a base tree...")); | |
1541 | ||
1542 | if (!state->quiet) { | |
1543 | /* | |
1544 | * List paths that needed 3-way fallback, so that the user can | |
1545 | * review them with extra care to spot mismerges. | |
1546 | */ | |
1547 | struct rev_info rev_info; | |
84f3de28 | 1548 | |
2abf3503 | 1549 | repo_init_revisions(the_repository, &rev_info, NULL); |
84f3de28 | 1550 | rev_info.diffopt.output_format = DIFF_FORMAT_NAME_STATUS; |
cdb5330a NTND |
1551 | rev_info.diffopt.filter |= diff_filter_bit('A'); |
1552 | rev_info.diffopt.filter |= diff_filter_bit('M'); | |
a58a1b01 | 1553 | add_pending_oid(&rev_info, "HEAD", &our_tree, 0); |
84f3de28 PT |
1554 | diff_setup_done(&rev_info.diffopt); |
1555 | run_diff_index(&rev_info, 1); | |
1556 | } | |
1557 | ||
1558 | if (run_apply(state, index_path)) | |
1559 | return error(_("Did you hand edit your patch?\n" | |
1560 | "It does not apply to blobs recorded in its index.")); | |
1561 | ||
fc5cb99f | 1562 | if (write_index_as_tree(&their_tree, &the_index, index_path, 0, NULL)) |
84f3de28 PT |
1563 | return error("could not write tree"); |
1564 | ||
1565 | say(state, stdout, _("Falling back to patching base and 3-way merge...")); | |
1566 | ||
38a824fe PT |
1567 | discard_cache(); |
1568 | read_cache(); | |
1569 | ||
84f3de28 PT |
1570 | /* |
1571 | * This is not so wrong. Depending on which base we picked, orig_tree | |
715a51bc | 1572 | * may be wildly different from ours, but their_tree has the same set of |
84f3de28 PT |
1573 | * wildly different changes in parts the patch did not touch, so |
1574 | * recursive ends up canceling them, saying that we reverted all those | |
1575 | * changes. | |
1576 | */ | |
1577 | ||
0d6caa2d | 1578 | init_merge_options(&o, the_repository); |
3f338f43 JS |
1579 | |
1580 | o.branch1 = "HEAD"; | |
1581 | their_tree_name = xstrfmt("%.*s", linelen(state->msg), state->msg); | |
1582 | o.branch2 = their_tree_name; | |
8e012516 | 1583 | o.detect_directory_renames = MERGE_DIRECTORY_RENAMES_NONE; |
3f338f43 JS |
1584 | |
1585 | if (state->quiet) | |
1586 | o.verbosity = 0; | |
1587 | ||
1588 | if (merge_recursive_generic(&o, &our_tree, &their_tree, 1, bases, &result)) { | |
35843b11 | 1589 | repo_rerere(the_repository, state->allow_rerere_autoupdate); |
3f338f43 | 1590 | free(their_tree_name); |
84f3de28 PT |
1591 | return error(_("Failed to merge in the changes.")); |
1592 | } | |
1593 | ||
3f338f43 | 1594 | free(their_tree_name); |
38a824fe PT |
1595 | return 0; |
1596 | } | |
1597 | ||
c9e8d960 PT |
1598 | /** |
1599 | * Commits the current index with state->msg as the commit message and | |
1600 | * state->author_name, state->author_email and state->author_date as the author | |
1601 | * information. | |
1602 | */ | |
1603 | static void do_commit(const struct am_state *state) | |
1604 | { | |
8c88769b | 1605 | struct object_id tree, parent, commit; |
1606 | const struct object_id *old_oid; | |
c9e8d960 | 1607 | struct commit_list *parents = NULL; |
e8cbe211 | 1608 | const char *reflog_msg, *author, *committer = NULL; |
c9e8d960 PT |
1609 | struct strbuf sb = STRBUF_INIT; |
1610 | ||
6c24c5c0 PT |
1611 | if (run_hook_le(NULL, "pre-applypatch", NULL)) |
1612 | exit(1); | |
1613 | ||
fc5cb99f | 1614 | if (write_cache_as_tree(&tree, 0, NULL)) |
c9e8d960 PT |
1615 | die(_("git write-tree failed to write a tree")); |
1616 | ||
e82caf38 | 1617 | if (!get_oid_commit("HEAD", &parent)) { |
8c88769b | 1618 | old_oid = &parent; |
c1f5eb49 SB |
1619 | commit_list_insert(lookup_commit(the_repository, &parent), |
1620 | &parents); | |
c9e8d960 | 1621 | } else { |
8c88769b | 1622 | old_oid = NULL; |
5d28cf78 | 1623 | say(state, stderr, _("applying to an empty history")); |
c9e8d960 PT |
1624 | } |
1625 | ||
1626 | author = fmt_ident(state->author_name, state->author_email, | |
39ab4d09 | 1627 | WANT_AUTHOR_IDENT, |
f07adb62 PT |
1628 | state->ignore_date ? NULL : state->author_date, |
1629 | IDENT_STRICT); | |
c9e8d960 | 1630 | |
0cd4bcba | 1631 | if (state->committer_date_is_author_date) |
2020451c JK |
1632 | committer = fmt_ident(getenv("GIT_COMMITTER_NAME"), |
1633 | getenv("GIT_COMMITTER_EMAIL"), | |
1634 | WANT_COMMITTER_IDENT, | |
e8cbe211 PW |
1635 | state->ignore_date ? NULL |
1636 | : state->author_date, | |
1637 | IDENT_STRICT); | |
1638 | ||
1639 | if (commit_tree_extended(state->msg, state->msg_len, &tree, parents, | |
1640 | &commit, author, committer, state->sign_commit, | |
1641 | NULL)) | |
c9e8d960 PT |
1642 | die(_("failed to write commit object")); |
1643 | ||
1644 | reflog_msg = getenv("GIT_REFLOG_ACTION"); | |
1645 | if (!reflog_msg) | |
1646 | reflog_msg = "am"; | |
1647 | ||
1648 | strbuf_addf(&sb, "%s: %.*s", reflog_msg, linelen(state->msg), | |
1649 | state->msg); | |
1650 | ||
ae077771 | 1651 | update_ref(sb.buf, "HEAD", &commit, old_oid, 0, |
1652 | UPDATE_REFS_DIE_ON_ERR); | |
c9e8d960 | 1653 | |
13b97ea5 PT |
1654 | if (state->rebasing) { |
1655 | FILE *fp = xfopen(am_path(state, "rewritten"), "a"); | |
1656 | ||
8c88769b | 1657 | assert(!is_null_oid(&state->orig_commit)); |
1658 | fprintf(fp, "%s ", oid_to_hex(&state->orig_commit)); | |
1659 | fprintf(fp, "%s\n", oid_to_hex(&commit)); | |
13b97ea5 PT |
1660 | fclose(fp); |
1661 | } | |
1662 | ||
7088f807 PT |
1663 | run_hook_le(NULL, "post-applypatch", NULL); |
1664 | ||
c9e8d960 PT |
1665 | strbuf_release(&sb); |
1666 | } | |
1667 | ||
240bfd2d PT |
1668 | /** |
1669 | * Validates the am_state for resuming -- the "msg" and authorship fields must | |
1670 | * be filled up. | |
1671 | */ | |
1672 | static void validate_resume_state(const struct am_state *state) | |
1673 | { | |
1674 | if (!state->msg) | |
1675 | die(_("cannot resume: %s does not exist."), | |
1676 | am_path(state, "final-commit")); | |
1677 | ||
1678 | if (!state->author_name || !state->author_email || !state->author_date) | |
1679 | die(_("cannot resume: %s does not exist."), | |
1680 | am_path(state, "author-script")); | |
1681 | } | |
1682 | ||
7ff26832 PT |
1683 | /** |
1684 | * Interactively prompt the user on whether the current patch should be | |
1685 | * applied. | |
1686 | * | |
1687 | * Returns 0 if the user chooses to apply the patch, 1 if the user chooses to | |
1688 | * skip it. | |
1689 | */ | |
1690 | static int do_interactive(struct am_state *state) | |
1691 | { | |
1692 | assert(state->msg); | |
1693 | ||
7ff26832 | 1694 | for (;;) { |
97387c8b | 1695 | char reply[64]; |
7ff26832 PT |
1696 | |
1697 | puts(_("Commit Body is:")); | |
1698 | puts("--------------------------"); | |
1699 | printf("%s", state->msg); | |
1700 | puts("--------------------------"); | |
1701 | ||
1702 | /* | |
1703 | * TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a] | |
1704 | * in your translation. The program will only accept English | |
1705 | * input at this point. | |
1706 | */ | |
97387c8b JK |
1707 | printf(_("Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all: ")); |
1708 | if (!fgets(reply, sizeof(reply), stdin)) | |
1709 | die("unable to read from stdin; aborting"); | |
7ff26832 | 1710 | |
1db65f32 | 1711 | if (*reply == 'y' || *reply == 'Y') { |
7ff26832 PT |
1712 | return 0; |
1713 | } else if (*reply == 'a' || *reply == 'A') { | |
1714 | state->interactive = 0; | |
1715 | return 0; | |
1716 | } else if (*reply == 'n' || *reply == 'N') { | |
1717 | return 1; | |
1718 | } else if (*reply == 'e' || *reply == 'E') { | |
1719 | struct strbuf msg = STRBUF_INIT; | |
1720 | ||
1721 | if (!launch_editor(am_path(state, "final-commit"), &msg, NULL)) { | |
1722 | free(state->msg); | |
1723 | state->msg = strbuf_detach(&msg, &state->msg_len); | |
1724 | } | |
1725 | strbuf_release(&msg); | |
1726 | } else if (*reply == 'v' || *reply == 'V') { | |
1727 | const char *pager = git_pager(1); | |
1728 | struct child_process cp = CHILD_PROCESS_INIT; | |
1729 | ||
1730 | if (!pager) | |
1731 | pager = "cat"; | |
708b8cc9 | 1732 | prepare_pager_args(&cp, pager); |
22f9b7f3 | 1733 | strvec_push(&cp.args, am_path(state, "patch")); |
7ff26832 PT |
1734 | run_command(&cp); |
1735 | } | |
1736 | } | |
1737 | } | |
1738 | ||
8c3bd9e2 PT |
1739 | /** |
1740 | * Applies all queued mail. | |
8c7b1563 PT |
1741 | * |
1742 | * If `resume` is true, we are "resuming". The "msg" and authorship fields, as | |
1743 | * well as the state directory's "patch" file is used as-is for applying the | |
1744 | * patch and committing it. | |
8c3bd9e2 | 1745 | */ |
8c7b1563 | 1746 | static void am_run(struct am_state *state, int resume) |
8c3bd9e2 | 1747 | { |
32a5fcbf | 1748 | struct strbuf sb = STRBUF_INIT; |
c9e8d960 | 1749 | |
33388a71 PT |
1750 | unlink(am_path(state, "dirtyindex")); |
1751 | ||
22184497 TG |
1752 | if (refresh_and_write_cache(REFRESH_QUIET, 0, 0) < 0) |
1753 | die(_("unable to write index file")); | |
38a824fe | 1754 | |
150fe065 | 1755 | if (repo_index_has_changes(the_repository, NULL, &sb)) { |
25b763ba | 1756 | write_state_bool(state, "dirtyindex", 1); |
32a5fcbf | 1757 | die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf); |
33388a71 | 1758 | } |
32a5fcbf PT |
1759 | |
1760 | strbuf_release(&sb); | |
1761 | ||
8c3bd9e2 | 1762 | while (state->cur <= state->last) { |
3e20dcf3 | 1763 | const char *mail = am_path(state, msgnum(state)); |
84f3de28 | 1764 | int apply_status; |
3e20dcf3 | 1765 | |
4d9c7e6f JK |
1766 | reset_ident_date(); |
1767 | ||
3e20dcf3 PT |
1768 | if (!file_exists(mail)) |
1769 | goto next; | |
1770 | ||
8c7b1563 PT |
1771 | if (resume) { |
1772 | validate_resume_state(state); | |
8c7b1563 | 1773 | } else { |
df2760a5 PT |
1774 | int skip; |
1775 | ||
1776 | if (state->rebasing) | |
1777 | skip = parse_mail_rebase(state, mail); | |
1778 | else | |
1779 | skip = parse_mail(state, mail); | |
1780 | ||
1781 | if (skip) | |
8c7b1563 | 1782 | goto next; /* mail should be skipped */ |
3e20dcf3 | 1783 | |
b7cc7051 GB |
1784 | if (state->signoff) |
1785 | am_append_signoff(state); | |
1786 | ||
8c7b1563 PT |
1787 | write_author_script(state); |
1788 | write_commit_msg(state); | |
1789 | } | |
8c3bd9e2 | 1790 | |
7ff26832 PT |
1791 | if (state->interactive && do_interactive(state)) |
1792 | goto next; | |
1793 | ||
b8803d8f PT |
1794 | if (run_applypatch_msg_hook(state)) |
1795 | exit(1); | |
1796 | ||
5d28cf78 | 1797 | say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg); |
38a824fe | 1798 | |
84f3de28 PT |
1799 | apply_status = run_apply(state, NULL); |
1800 | ||
1801 | if (apply_status && state->threeway) { | |
1802 | struct strbuf sb = STRBUF_INIT; | |
1803 | ||
1804 | strbuf_addstr(&sb, am_path(state, "patch-merge-index")); | |
1805 | apply_status = fall_back_threeway(state, sb.buf); | |
1806 | strbuf_release(&sb); | |
1807 | ||
1808 | /* | |
1809 | * Applying the patch to an earlier tree and merging | |
1810 | * the result may have produced the same tree as ours. | |
1811 | */ | |
e1f8694f | 1812 | if (!apply_status && |
150fe065 | 1813 | !repo_index_has_changes(the_repository, NULL, NULL)) { |
84f3de28 PT |
1814 | say(state, stdout, _("No changes -- Patch already applied.")); |
1815 | goto next; | |
1816 | } | |
1817 | } | |
1818 | ||
1819 | if (apply_status) { | |
38a824fe PT |
1820 | printf_ln(_("Patch failed at %s %.*s"), msgnum(state), |
1821 | linelen(state->msg), state->msg); | |
1822 | ||
38a824fe | 1823 | if (advice_amworkdir) |
aa416b22 | 1824 | advise(_("Use 'git am --show-current-patch=diff' to see the failed patch")); |
38a824fe | 1825 | |
2d83109a | 1826 | die_user_resolve(state); |
38a824fe PT |
1827 | } |
1828 | ||
c9e8d960 | 1829 | do_commit(state); |
8c3bd9e2 | 1830 | |
3e20dcf3 | 1831 | next: |
8c3bd9e2 | 1832 | am_next(state); |
852a1710 PT |
1833 | |
1834 | if (resume) | |
1835 | am_load(state); | |
1836 | resume = 0; | |
8c3bd9e2 PT |
1837 | } |
1838 | ||
e3b1e3bd | 1839 | if (!is_empty_or_missing_file(am_path(state, "rewritten"))) { |
13b97ea5 | 1840 | assert(state->rebasing); |
88b291fe | 1841 | copy_notes_for_rebase(state); |
13b97ea5 PT |
1842 | run_post_rewrite_hook(state); |
1843 | } | |
1844 | ||
35bdcc59 PT |
1845 | /* |
1846 | * In rebasing mode, it's up to the caller to take care of | |
1847 | * housekeeping. | |
1848 | */ | |
1849 | if (!state->rebasing) { | |
1850 | am_destroy(state); | |
2d511cfc | 1851 | close_object_store(the_repository->objects); |
a95ce124 | 1852 | run_auto_maintenance(state->quiet); |
35bdcc59 | 1853 | } |
8c3bd9e2 | 1854 | } |
73c2779f | 1855 | |
240bfd2d PT |
1856 | /** |
1857 | * Resume the current am session after patch application failure. The user did | |
1858 | * all the hard work, and we do not have to do any patch application. Just | |
1859 | * trust and commit what the user has in the index and working tree. | |
1860 | */ | |
1861 | static void am_resolve(struct am_state *state) | |
1862 | { | |
1863 | validate_resume_state(state); | |
1864 | ||
5d28cf78 | 1865 | say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg); |
240bfd2d | 1866 | |
150fe065 | 1867 | if (!repo_index_has_changes(the_repository, NULL, NULL)) { |
240bfd2d PT |
1868 | printf_ln(_("No changes - did you forget to use 'git add'?\n" |
1869 | "If there is nothing left to stage, chances are that something else\n" | |
1870 | "already introduced the same changes; you might want to skip this patch.")); | |
2d83109a | 1871 | die_user_resolve(state); |
240bfd2d PT |
1872 | } |
1873 | ||
1874 | if (unmerged_cache()) { | |
1875 | printf_ln(_("You still have unmerged paths in your index.\n" | |
6c486862 JNA |
1876 | "You should 'git add' each file with resolved conflicts to mark them as such.\n" |
1877 | "You might run `git rm` on a file to accept \"deleted by them\" for it.")); | |
2d83109a | 1878 | die_user_resolve(state); |
240bfd2d PT |
1879 | } |
1880 | ||
7ff26832 PT |
1881 | if (state->interactive) { |
1882 | write_index_patch(state); | |
1883 | if (do_interactive(state)) | |
1884 | goto next; | |
1885 | } | |
1886 | ||
35843b11 | 1887 | repo_rerere(the_repository, 0); |
f1cb96d6 | 1888 | |
240bfd2d PT |
1889 | do_commit(state); |
1890 | ||
7ff26832 | 1891 | next: |
240bfd2d | 1892 | am_next(state); |
852a1710 | 1893 | am_load(state); |
8c7b1563 | 1894 | am_run(state, 0); |
240bfd2d PT |
1895 | } |
1896 | ||
9990080c PT |
1897 | /** |
1898 | * Performs a checkout fast-forward from `head` to `remote`. If `reset` is | |
1899 | * true, any unmerged entries will be discarded. Returns 0 on success, -1 on | |
1900 | * failure. | |
1901 | */ | |
1902 | static int fast_forward_to(struct tree *head, struct tree *remote, int reset) | |
1903 | { | |
837e34eb | 1904 | struct lock_file lock_file = LOCK_INIT; |
9990080c PT |
1905 | struct unpack_trees_options opts; |
1906 | struct tree_desc t[2]; | |
1907 | ||
1908 | if (parse_tree(head) || parse_tree(remote)) | |
1909 | return -1; | |
1910 | ||
837e34eb | 1911 | hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR); |
9990080c PT |
1912 | |
1913 | refresh_cache(REFRESH_QUIET); | |
1914 | ||
1915 | memset(&opts, 0, sizeof(opts)); | |
1916 | opts.head_idx = 1; | |
1917 | opts.src_index = &the_index; | |
1918 | opts.dst_index = &the_index; | |
1919 | opts.update = 1; | |
1920 | opts.merge = 1; | |
1921 | opts.reset = reset; | |
1922 | opts.fn = twoway_merge; | |
1923 | init_tree_desc(&t[0], head->buffer, head->size); | |
1924 | init_tree_desc(&t[1], remote->buffer, remote->size); | |
1925 | ||
1926 | if (unpack_trees(2, t, &opts)) { | |
837e34eb | 1927 | rollback_lock_file(&lock_file); |
9990080c PT |
1928 | return -1; |
1929 | } | |
1930 | ||
837e34eb | 1931 | if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) |
9990080c PT |
1932 | die(_("unable to write new index file")); |
1933 | ||
1934 | return 0; | |
1935 | } | |
1936 | ||
3ecc7040 PT |
1937 | /** |
1938 | * Merges a tree into the index. The index's stat info will take precedence | |
1939 | * over the merged tree's. Returns 0 on success, -1 on failure. | |
1940 | */ | |
1941 | static int merge_tree(struct tree *tree) | |
1942 | { | |
837e34eb | 1943 | struct lock_file lock_file = LOCK_INIT; |
3ecc7040 PT |
1944 | struct unpack_trees_options opts; |
1945 | struct tree_desc t[1]; | |
1946 | ||
1947 | if (parse_tree(tree)) | |
1948 | return -1; | |
1949 | ||
837e34eb | 1950 | hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR); |
3ecc7040 PT |
1951 | |
1952 | memset(&opts, 0, sizeof(opts)); | |
1953 | opts.head_idx = 1; | |
1954 | opts.src_index = &the_index; | |
1955 | opts.dst_index = &the_index; | |
1956 | opts.merge = 1; | |
1957 | opts.fn = oneway_merge; | |
1958 | init_tree_desc(&t[0], tree->buffer, tree->size); | |
1959 | ||
1960 | if (unpack_trees(1, t, &opts)) { | |
837e34eb | 1961 | rollback_lock_file(&lock_file); |
3ecc7040 PT |
1962 | return -1; |
1963 | } | |
1964 | ||
837e34eb | 1965 | if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) |
3ecc7040 PT |
1966 | die(_("unable to write new index file")); |
1967 | ||
1968 | return 0; | |
1969 | } | |
1970 | ||
9990080c PT |
1971 | /** |
1972 | * Clean the index without touching entries that are not modified between | |
1973 | * `head` and `remote`. | |
1974 | */ | |
8c88769b | 1975 | static int clean_index(const struct object_id *head, const struct object_id *remote) |
9990080c | 1976 | { |
9990080c | 1977 | struct tree *head_tree, *remote_tree, *index_tree; |
8c88769b | 1978 | struct object_id index; |
9990080c | 1979 | |
a9dbc179 | 1980 | head_tree = parse_tree_indirect(head); |
9990080c | 1981 | if (!head_tree) |
8c88769b | 1982 | return error(_("Could not parse object '%s'."), oid_to_hex(head)); |
9990080c | 1983 | |
a9dbc179 | 1984 | remote_tree = parse_tree_indirect(remote); |
9990080c | 1985 | if (!remote_tree) |
8c88769b | 1986 | return error(_("Could not parse object '%s'."), oid_to_hex(remote)); |
9990080c PT |
1987 | |
1988 | read_cache_unmerged(); | |
1989 | ||
1990 | if (fast_forward_to(head_tree, head_tree, 1)) | |
1991 | return -1; | |
1992 | ||
fc5cb99f | 1993 | if (write_cache_as_tree(&index, 0, NULL)) |
9990080c PT |
1994 | return -1; |
1995 | ||
a9dbc179 | 1996 | index_tree = parse_tree_indirect(&index); |
9990080c | 1997 | if (!index_tree) |
8c88769b | 1998 | return error(_("Could not parse object '%s'."), oid_to_hex(&index)); |
9990080c PT |
1999 | |
2000 | if (fast_forward_to(index_tree, remote_tree, 0)) | |
2001 | return -1; | |
2002 | ||
3ecc7040 | 2003 | if (merge_tree(remote_tree)) |
9990080c | 2004 | return -1; |
9990080c | 2005 | |
f4a4b9ac | 2006 | remove_branch_state(the_repository, 0); |
9990080c PT |
2007 | |
2008 | return 0; | |
2009 | } | |
2010 | ||
f1cb96d6 PT |
2011 | /** |
2012 | * Resets rerere's merge resolution metadata. | |
2013 | */ | |
2014 | static void am_rerere_clear(void) | |
2015 | { | |
2016 | struct string_list merge_rr = STRING_LIST_INIT_DUP; | |
55e6b354 | 2017 | rerere_clear(the_repository, &merge_rr); |
f1cb96d6 PT |
2018 | string_list_clear(&merge_rr, 1); |
2019 | } | |
2020 | ||
9990080c PT |
2021 | /** |
2022 | * Resume the current am session by skipping the current patch. | |
2023 | */ | |
2024 | static void am_skip(struct am_state *state) | |
2025 | { | |
8c88769b | 2026 | struct object_id head; |
9990080c | 2027 | |
f1cb96d6 PT |
2028 | am_rerere_clear(); |
2029 | ||
8c88769b | 2030 | if (get_oid("HEAD", &head)) |
d41836a0 | 2031 | oidcpy(&head, the_hash_algo->empty_tree); |
9990080c | 2032 | |
8c88769b | 2033 | if (clean_index(&head, &head)) |
9990080c PT |
2034 | die(_("failed to clean index")); |
2035 | ||
45339f74 EN |
2036 | if (state->rebasing) { |
2037 | FILE *fp = xfopen(am_path(state, "rewritten"), "a"); | |
2038 | ||
2039 | assert(!is_null_oid(&state->orig_commit)); | |
2040 | fprintf(fp, "%s ", oid_to_hex(&state->orig_commit)); | |
2041 | fprintf(fp, "%s\n", oid_to_hex(&head)); | |
2042 | fclose(fp); | |
2043 | } | |
2044 | ||
9990080c | 2045 | am_next(state); |
852a1710 | 2046 | am_load(state); |
9990080c PT |
2047 | am_run(state, 0); |
2048 | } | |
2049 | ||
33388a71 PT |
2050 | /** |
2051 | * Returns true if it is safe to reset HEAD to the ORIG_HEAD, false otherwise. | |
2052 | * | |
2053 | * It is not safe to reset HEAD when: | |
2054 | * 1. git-am previously failed because the index was dirty. | |
2055 | * 2. HEAD has moved since git-am previously failed. | |
2056 | */ | |
2057 | static int safe_to_abort(const struct am_state *state) | |
2058 | { | |
2059 | struct strbuf sb = STRBUF_INIT; | |
8c88769b | 2060 | struct object_id abort_safety, head; |
33388a71 PT |
2061 | |
2062 | if (file_exists(am_path(state, "dirtyindex"))) | |
2063 | return 0; | |
2064 | ||
2065 | if (read_state_file(&sb, state, "abort-safety", 1) > 0) { | |
8c88769b | 2066 | if (get_oid_hex(sb.buf, &abort_safety)) |
ccd71b2f | 2067 | die(_("could not parse %s"), am_path(state, "abort-safety")); |
33388a71 | 2068 | } else |
8c88769b | 2069 | oidclr(&abort_safety); |
28ac7aa7 | 2070 | strbuf_release(&sb); |
33388a71 | 2071 | |
8c88769b | 2072 | if (get_oid("HEAD", &head)) |
2073 | oidclr(&head); | |
33388a71 | 2074 | |
4a7e27e9 | 2075 | if (oideq(&head, &abort_safety)) |
33388a71 PT |
2076 | return 1; |
2077 | ||
1868331f | 2078 | warning(_("You seem to have moved HEAD since the last 'am' failure.\n" |
33388a71 PT |
2079 | "Not rewinding to ORIG_HEAD")); |
2080 | ||
2081 | return 0; | |
2082 | } | |
2083 | ||
2084 | /** | |
2085 | * Aborts the current am session if it is safe to do so. | |
2086 | */ | |
2087 | static void am_abort(struct am_state *state) | |
2088 | { | |
8c88769b | 2089 | struct object_id curr_head, orig_head; |
33388a71 PT |
2090 | int has_curr_head, has_orig_head; |
2091 | char *curr_branch; | |
2092 | ||
2093 | if (!safe_to_abort(state)) { | |
2094 | am_destroy(state); | |
2095 | return; | |
2096 | } | |
2097 | ||
f1cb96d6 PT |
2098 | am_rerere_clear(); |
2099 | ||
0f2dc722 | 2100 | curr_branch = resolve_refdup("HEAD", 0, &curr_head, NULL); |
57e0ef0e | 2101 | has_curr_head = curr_branch && !is_null_oid(&curr_head); |
33388a71 | 2102 | if (!has_curr_head) |
d41836a0 | 2103 | oidcpy(&curr_head, the_hash_algo->empty_tree); |
33388a71 | 2104 | |
8c88769b | 2105 | has_orig_head = !get_oid("ORIG_HEAD", &orig_head); |
33388a71 | 2106 | if (!has_orig_head) |
d41836a0 | 2107 | oidcpy(&orig_head, the_hash_algo->empty_tree); |
33388a71 | 2108 | |
8c88769b | 2109 | clean_index(&curr_head, &orig_head); |
33388a71 PT |
2110 | |
2111 | if (has_orig_head) | |
ae077771 | 2112 | update_ref("am --abort", "HEAD", &orig_head, |
2113 | has_curr_head ? &curr_head : NULL, 0, | |
2114 | UPDATE_REFS_DIE_ON_ERR); | |
33388a71 | 2115 | else if (curr_branch) |
91774afc | 2116 | delete_ref(NULL, curr_branch, NULL, REF_NO_DEREF); |
33388a71 PT |
2117 | |
2118 | free(curr_branch); | |
2119 | am_destroy(state); | |
2120 | } | |
2121 | ||
f3b48228 | 2122 | static int show_patch(struct am_state *state, enum show_patch_type sub_mode) |
984913a2 NTND |
2123 | { |
2124 | struct strbuf sb = STRBUF_INIT; | |
2125 | const char *patch_path; | |
2126 | int len; | |
2127 | ||
66335298 NTND |
2128 | if (!is_null_oid(&state->orig_commit)) { |
2129 | const char *av[4] = { "show", NULL, "--", NULL }; | |
2130 | char *new_oid_str; | |
2131 | int ret; | |
2132 | ||
2133 | av[1] = new_oid_str = xstrdup(oid_to_hex(&state->orig_commit)); | |
2134 | ret = run_command_v_opt(av, RUN_GIT_CMD); | |
2135 | free(new_oid_str); | |
2136 | return ret; | |
2137 | } | |
2138 | ||
f3b48228 PB |
2139 | switch (sub_mode) { |
2140 | case SHOW_PATCH_RAW: | |
2141 | patch_path = am_path(state, msgnum(state)); | |
2142 | break; | |
aa416b22 PB |
2143 | case SHOW_PATCH_DIFF: |
2144 | patch_path = am_path(state, "patch"); | |
2145 | break; | |
f3b48228 PB |
2146 | default: |
2147 | BUG("invalid mode for --show-current-patch"); | |
2148 | } | |
2149 | ||
984913a2 NTND |
2150 | len = strbuf_read_file(&sb, patch_path, 0); |
2151 | if (len < 0) | |
2152 | die_errno(_("failed to read '%s'"), patch_path); | |
2153 | ||
2154 | setup_pager(); | |
2155 | write_in_full(1, sb.buf, sb.len); | |
2156 | strbuf_release(&sb); | |
2157 | return 0; | |
2158 | } | |
2159 | ||
11c2177f PT |
2160 | /** |
2161 | * parse_options() callback that validates and sets opt->value to the | |
2162 | * PATCH_FORMAT_* enum value corresponding to `arg`. | |
2163 | */ | |
2164 | static int parse_opt_patchformat(const struct option *opt, const char *arg, int unset) | |
2165 | { | |
2166 | int *opt_value = opt->value; | |
2167 | ||
fce56648 JK |
2168 | if (unset) |
2169 | *opt_value = PATCH_FORMAT_UNKNOWN; | |
2170 | else if (!strcmp(arg, "mbox")) | |
11c2177f | 2171 | *opt_value = PATCH_FORMAT_MBOX; |
5ae41c79 PT |
2172 | else if (!strcmp(arg, "stgit")) |
2173 | *opt_value = PATCH_FORMAT_STGIT; | |
336108c1 PT |
2174 | else if (!strcmp(arg, "stgit-series")) |
2175 | *opt_value = PATCH_FORMAT_STGIT_SERIES; | |
94cd175c PT |
2176 | else if (!strcmp(arg, "hg")) |
2177 | *opt_value = PATCH_FORMAT_HG; | |
d9925d1a EW |
2178 | else if (!strcmp(arg, "mboxrd")) |
2179 | *opt_value = PATCH_FORMAT_MBOXRD; | |
5a59a230 NTND |
2180 | /* |
2181 | * Please update $__git_patchformat in git-completion.bash | |
2182 | * when you add new options | |
2183 | */ | |
11c2177f PT |
2184 | else |
2185 | return error(_("Invalid value for --patch-format: %s"), arg); | |
2186 | return 0; | |
2187 | } | |
2188 | ||
e8ef1e8d | 2189 | enum resume_type { |
240bfd2d | 2190 | RESUME_FALSE = 0, |
8c7b1563 | 2191 | RESUME_APPLY, |
9990080c | 2192 | RESUME_RESOLVED, |
33388a71 | 2193 | RESUME_SKIP, |
65ed8ff3 | 2194 | RESUME_ABORT, |
9ca488c0 | 2195 | RESUME_QUIT, |
984913a2 | 2196 | RESUME_SHOW_PATCH |
240bfd2d PT |
2197 | }; |
2198 | ||
e8ef1e8d PB |
2199 | struct resume_mode { |
2200 | enum resume_type mode; | |
f3b48228 | 2201 | enum show_patch_type sub_mode; |
e8ef1e8d PB |
2202 | }; |
2203 | ||
f3b48228 PB |
2204 | static int parse_opt_show_current_patch(const struct option *opt, const char *arg, int unset) |
2205 | { | |
2206 | int *opt_value = opt->value; | |
2207 | struct resume_mode *resume = container_of(opt_value, struct resume_mode, mode); | |
2208 | ||
2209 | /* | |
2210 | * Please update $__git_showcurrentpatch in git-completion.bash | |
2211 | * when you add new options | |
2212 | */ | |
2213 | const char *valid_modes[] = { | |
aa416b22 | 2214 | [SHOW_PATCH_DIFF] = "diff", |
f3b48228 PB |
2215 | [SHOW_PATCH_RAW] = "raw" |
2216 | }; | |
2217 | int new_value = SHOW_PATCH_RAW; | |
2218 | ||
8d2aa8df JK |
2219 | BUG_ON_OPT_NEG(unset); |
2220 | ||
f3b48228 PB |
2221 | if (arg) { |
2222 | for (new_value = 0; new_value < ARRAY_SIZE(valid_modes); new_value++) { | |
2223 | if (!strcmp(arg, valid_modes[new_value])) | |
2224 | break; | |
2225 | } | |
2226 | if (new_value >= ARRAY_SIZE(valid_modes)) | |
2227 | return error(_("Invalid value for --show-current-patch: %s"), arg); | |
2228 | } | |
2229 | ||
2230 | if (resume->mode == RESUME_SHOW_PATCH && new_value != resume->sub_mode) | |
2231 | return error(_("--show-current-patch=%s is incompatible with " | |
2232 | "--show-current-patch=%s"), | |
2233 | arg, valid_modes[resume->sub_mode]); | |
2234 | ||
2235 | resume->mode = RESUME_SHOW_PATCH; | |
2236 | resume->sub_mode = new_value; | |
2237 | return 0; | |
2238 | } | |
2239 | ||
434c64df RMM |
2240 | static int git_am_config(const char *k, const char *v, void *cb) |
2241 | { | |
2242 | int status; | |
2243 | ||
2244 | status = git_gpg_config(k, v, NULL); | |
2245 | if (status) | |
2246 | return status; | |
2247 | ||
2248 | return git_default_config(k, v, NULL); | |
2249 | } | |
2250 | ||
73c2779f PT |
2251 | int cmd_am(int argc, const char **argv, const char *prefix) |
2252 | { | |
8c3bd9e2 | 2253 | struct am_state state; |
c2676cde | 2254 | int binary = -1; |
5d123a40 | 2255 | int keep_cr = -1; |
11c2177f | 2256 | int patch_format = PATCH_FORMAT_UNKNOWN; |
e8ef1e8d | 2257 | struct resume_mode resume = { .mode = RESUME_FALSE }; |
852a1710 | 2258 | int in_progress; |
984913a2 | 2259 | int ret = 0; |
8c3bd9e2 PT |
2260 | |
2261 | const char * const usage[] = { | |
d65fdc9c | 2262 | N_("git am [<options>] [(<mbox> | <Maildir>)...]"), |
d96a0313 | 2263 | N_("git am [<options>] (--continue | --skip | --abort)"), |
8c3bd9e2 PT |
2264 | NULL |
2265 | }; | |
2266 | ||
2267 | struct option options[] = { | |
7ff26832 PT |
2268 | OPT_BOOL('i', "interactive", &state.interactive, |
2269 | N_("run interactively")), | |
c2676cde | 2270 | OPT_HIDDEN_BOOL('b', "binary", &binary, |
1fb5a0ea | 2271 | N_("historical option -- no-op")), |
84f3de28 PT |
2272 | OPT_BOOL('3', "3way", &state.threeway, |
2273 | N_("allow fall back on 3way merging if needed")), | |
5d28cf78 | 2274 | OPT__QUIET(&state.quiet, N_("be quiet")), |
b5e82359 | 2275 | OPT_SET_INT('s', "signoff", &state.signoff, |
3abd4a67 | 2276 | N_("add a Signed-off-by trailer to the commit message"), |
b5e82359 | 2277 | SIGNOFF_EXPLICIT), |
ef7ee16d PT |
2278 | OPT_BOOL('u', "utf8", &state.utf8, |
2279 | N_("recode into utf8 (default)")), | |
4f1b6961 PT |
2280 | OPT_SET_INT('k', "keep", &state.keep, |
2281 | N_("pass -k flag to git-mailinfo"), KEEP_TRUE), | |
2282 | OPT_SET_INT(0, "keep-non-patch", &state.keep, | |
2283 | N_("pass -b flag to git-mailinfo"), KEEP_NON_PATCH), | |
702cbaad PT |
2284 | OPT_BOOL('m', "message-id", &state.message_id, |
2285 | N_("pass -m flag to git-mailinfo")), | |
3e4a67b4 NTND |
2286 | OPT_SET_INT_F(0, "keep-cr", &keep_cr, |
2287 | N_("pass --keep-cr flag to git-mailsplit for mbox format"), | |
2288 | 1, PARSE_OPT_NONEG), | |
2289 | OPT_SET_INT_F(0, "no-keep-cr", &keep_cr, | |
2290 | N_("do not pass --keep-cr flag to git-mailsplit independent of am.keepcr"), | |
2291 | 0, PARSE_OPT_NONEG), | |
9b646617 PT |
2292 | OPT_BOOL('c', "scissors", &state.scissors, |
2293 | N_("strip everything before a scissors line")), | |
59b519ab ĐTCD |
2294 | OPT_CALLBACK_F(0, "quoted-cr", &state.quoted_cr, N_("action"), |
2295 | N_("pass it through git-mailinfo"), | |
2296 | PARSE_OPT_NONEG, am_option_parse_quoted_cr), | |
257e8cec PT |
2297 | OPT_PASSTHRU_ARGV(0, "whitespace", &state.git_apply_opts, N_("action"), |
2298 | N_("pass it through git-apply"), | |
2299 | 0), | |
2300 | OPT_PASSTHRU_ARGV(0, "ignore-space-change", &state.git_apply_opts, NULL, | |
2301 | N_("pass it through git-apply"), | |
2302 | PARSE_OPT_NOARG), | |
2303 | OPT_PASSTHRU_ARGV(0, "ignore-whitespace", &state.git_apply_opts, NULL, | |
2304 | N_("pass it through git-apply"), | |
2305 | PARSE_OPT_NOARG), | |
2306 | OPT_PASSTHRU_ARGV(0, "directory", &state.git_apply_opts, N_("root"), | |
2307 | N_("pass it through git-apply"), | |
2308 | 0), | |
2309 | OPT_PASSTHRU_ARGV(0, "exclude", &state.git_apply_opts, N_("path"), | |
2310 | N_("pass it through git-apply"), | |
2311 | 0), | |
2312 | OPT_PASSTHRU_ARGV(0, "include", &state.git_apply_opts, N_("path"), | |
2313 | N_("pass it through git-apply"), | |
2314 | 0), | |
2315 | OPT_PASSTHRU_ARGV('C', NULL, &state.git_apply_opts, N_("n"), | |
2316 | N_("pass it through git-apply"), | |
2317 | 0), | |
2318 | OPT_PASSTHRU_ARGV('p', NULL, &state.git_apply_opts, N_("num"), | |
2319 | N_("pass it through git-apply"), | |
2320 | 0), | |
11c2177f PT |
2321 | OPT_CALLBACK(0, "patch-format", &patch_format, N_("format"), |
2322 | N_("format the patch(es) are in"), | |
2323 | parse_opt_patchformat), | |
257e8cec PT |
2324 | OPT_PASSTHRU_ARGV(0, "reject", &state.git_apply_opts, NULL, |
2325 | N_("pass it through git-apply"), | |
2326 | PARSE_OPT_NOARG), | |
2d83109a PT |
2327 | OPT_STRING(0, "resolvemsg", &state.resolvemsg, NULL, |
2328 | N_("override error message when patch failure occurs")), | |
e8ef1e8d | 2329 | OPT_CMDMODE(0, "continue", &resume.mode, |
240bfd2d PT |
2330 | N_("continue applying patches after resolving a conflict"), |
2331 | RESUME_RESOLVED), | |
e8ef1e8d | 2332 | OPT_CMDMODE('r', "resolved", &resume.mode, |
240bfd2d PT |
2333 | N_("synonyms for --continue"), |
2334 | RESUME_RESOLVED), | |
e8ef1e8d | 2335 | OPT_CMDMODE(0, "skip", &resume.mode, |
9990080c PT |
2336 | N_("skip the current patch"), |
2337 | RESUME_SKIP), | |
e8ef1e8d | 2338 | OPT_CMDMODE(0, "abort", &resume.mode, |
e73fe3dd | 2339 | N_("restore the original branch and abort the patching operation"), |
33388a71 | 2340 | RESUME_ABORT), |
e8ef1e8d | 2341 | OPT_CMDMODE(0, "quit", &resume.mode, |
e73fe3dd | 2342 | N_("abort the patching operation but keep HEAD where it is"), |
65ed8ff3 | 2343 | RESUME_QUIT), |
f3b48228 | 2344 | { OPTION_CALLBACK, 0, "show-current-patch", &resume.mode, |
aa416b22 | 2345 | "(diff|raw)", |
f3b48228 PB |
2346 | N_("show the patch being applied"), |
2347 | PARSE_OPT_CMDMODE | PARSE_OPT_OPTARG | PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP, | |
2348 | parse_opt_show_current_patch, RESUME_SHOW_PATCH }, | |
0cd4bcba PT |
2349 | OPT_BOOL(0, "committer-date-is-author-date", |
2350 | &state.committer_date_is_author_date, | |
2351 | N_("lie about committer date")), | |
f07adb62 PT |
2352 | OPT_BOOL(0, "ignore-date", &state.ignore_date, |
2353 | N_("use current timestamp for author date")), | |
f1cb96d6 | 2354 | OPT_RERERE_AUTOUPDATE(&state.allow_rerere_autoupdate), |
7e35dacb PT |
2355 | { OPTION_STRING, 'S', "gpg-sign", &state.sign_commit, N_("key-id"), |
2356 | N_("GPG-sign commits"), | |
2357 | PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, | |
35bdcc59 PT |
2358 | OPT_HIDDEN_BOOL(0, "rebasing", &state.rebasing, |
2359 | N_("(internal use for git-rebase)")), | |
8c3bd9e2 PT |
2360 | OPT_END() |
2361 | }; | |
73c2779f | 2362 | |
f3a2fffe JK |
2363 | if (argc == 2 && !strcmp(argv[1], "-h")) |
2364 | usage_with_options(usage, options); | |
2365 | ||
434c64df | 2366 | git_config(git_am_config, NULL); |
8c3bd9e2 | 2367 | |
16d2676c | 2368 | am_state_init(&state); |
8c3bd9e2 | 2369 | |
852a1710 PT |
2370 | in_progress = am_in_progress(&state); |
2371 | if (in_progress) | |
2372 | am_load(&state); | |
2373 | ||
8c3bd9e2 PT |
2374 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
2375 | ||
c2676cde PT |
2376 | if (binary >= 0) |
2377 | fprintf_ln(stderr, _("The -b/--binary option has been a no-op for long time, and\n" | |
2378 | "it will be removed. Please do not use it anymore.")); | |
2379 | ||
5e4f9cff PT |
2380 | /* Ensure a valid committer ident can be constructed */ |
2381 | git_committer_info(IDENT_STRICT); | |
2382 | ||
e1ff0a32 | 2383 | if (repo_read_index_preload(the_repository, NULL, 0) < 0) |
38a824fe PT |
2384 | die(_("failed to read the index")); |
2385 | ||
852a1710 | 2386 | if (in_progress) { |
8d185503 PT |
2387 | /* |
2388 | * Catch user error to feed us patches when there is a session | |
2389 | * in progress: | |
2390 | * | |
2391 | * 1. mbox path(s) are provided on the command-line. | |
2392 | * 2. stdin is not a tty: the user is trying to feed us a patch | |
2393 | * from standard input. This is somewhat unreliable -- stdin | |
2394 | * could be /dev/null for example and the caller did not | |
2395 | * intend to feed us a patch but wanted to continue | |
2396 | * unattended. | |
2397 | */ | |
e8ef1e8d | 2398 | if (argc || (resume.mode == RESUME_FALSE && !isatty(0))) |
8d185503 PT |
2399 | die(_("previous rebase directory %s still exists but mbox given."), |
2400 | state.dir); | |
2401 | ||
e8ef1e8d PB |
2402 | if (resume.mode == RESUME_FALSE) |
2403 | resume.mode = RESUME_APPLY; | |
b5e82359 PT |
2404 | |
2405 | if (state.signoff == SIGNOFF_EXPLICIT) | |
2406 | am_append_signoff(&state); | |
8c7b1563 | 2407 | } else { |
22f9b7f3 | 2408 | struct strvec paths = STRVEC_INIT; |
11c2177f PT |
2409 | int i; |
2410 | ||
6d42ac29 PT |
2411 | /* |
2412 | * Handle stray state directory in the independent-run case. In | |
2413 | * the --rebasing case, it is up to the caller to take care of | |
2414 | * stray directories. | |
2415 | */ | |
2416 | if (file_exists(state.dir) && !state.rebasing) { | |
e8ef1e8d | 2417 | if (resume.mode == RESUME_ABORT || resume.mode == RESUME_QUIT) { |
6d42ac29 PT |
2418 | am_destroy(&state); |
2419 | am_state_release(&state); | |
2420 | return 0; | |
2421 | } | |
2422 | ||
2423 | die(_("Stray %s directory found.\n" | |
2424 | "Use \"git am --abort\" to remove it."), | |
2425 | state.dir); | |
2426 | } | |
2427 | ||
e8ef1e8d | 2428 | if (resume.mode) |
240bfd2d PT |
2429 | die(_("Resolve operation not in progress, we are not resuming.")); |
2430 | ||
11c2177f PT |
2431 | for (i = 0; i < argc; i++) { |
2432 | if (is_absolute_path(argv[i]) || !prefix) | |
22f9b7f3 | 2433 | strvec_push(&paths, argv[i]); |
11c2177f | 2434 | else |
22f9b7f3 | 2435 | strvec_push(&paths, mkpath("%s/%s", prefix, argv[i])); |
11c2177f PT |
2436 | } |
2437 | ||
d70a9eb6 | 2438 | if (state.interactive && !paths.nr) |
6e7baf24 JK |
2439 | die(_("interactive mode requires patches on the command line")); |
2440 | ||
d70a9eb6 | 2441 | am_setup(&state, patch_format, paths.v, keep_cr); |
11c2177f | 2442 | |
22f9b7f3 | 2443 | strvec_clear(&paths); |
11c2177f | 2444 | } |
8c3bd9e2 | 2445 | |
e8ef1e8d | 2446 | switch (resume.mode) { |
240bfd2d | 2447 | case RESUME_FALSE: |
8c7b1563 PT |
2448 | am_run(&state, 0); |
2449 | break; | |
2450 | case RESUME_APPLY: | |
2451 | am_run(&state, 1); | |
240bfd2d PT |
2452 | break; |
2453 | case RESUME_RESOLVED: | |
2454 | am_resolve(&state); | |
2455 | break; | |
9990080c PT |
2456 | case RESUME_SKIP: |
2457 | am_skip(&state); | |
2458 | break; | |
33388a71 PT |
2459 | case RESUME_ABORT: |
2460 | am_abort(&state); | |
2461 | break; | |
65ed8ff3 NTND |
2462 | case RESUME_QUIT: |
2463 | am_rerere_clear(); | |
2464 | am_destroy(&state); | |
2465 | break; | |
984913a2 | 2466 | case RESUME_SHOW_PATCH: |
f3b48228 | 2467 | ret = show_patch(&state, resume.sub_mode); |
984913a2 | 2468 | break; |
240bfd2d | 2469 | default: |
033abf97 | 2470 | BUG("invalid resume value"); |
240bfd2d | 2471 | } |
8c3bd9e2 PT |
2472 | |
2473 | am_state_release(&state); | |
2474 | ||
984913a2 | 2475 | return ret; |
73c2779f | 2476 | } |