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