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