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