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