12
#include "environment.h"
15
#include "parse-options.h"
17
#include "run-command.h"
22
#include "cache-tree.h"
26
#include "unpack-trees.h"
28
#include "object-name.h"
29
#include "preload-index.h"
32
#include "merge-recursive.h"
34
#include "notes-utils.h"
38
#include "string-list.h"
41
#include "repository.h"
47
static int linelen(const char *msg)
49
return strchrnul(msg, '\n') - msg;
55
static int str_isspace(const char *str)
65
PATCH_FORMAT_UNKNOWN = 0,
68
PATCH_FORMAT_STGIT_SERIES,
98
RESUME_SHOW_PATCH_RAW,
99
RESUME_SHOW_PATCH_DIFF,
104
STOP_ON_EMPTY_COMMIT = 0,
125
struct object_id orig_commit;
142
struct strvec git_apply_opts;
143
const char *resolvemsg;
144
int committer_date_is_author_date;
146
int allow_rerere_autoupdate;
147
const char *sign_commit;
154
static void am_state_init(struct am_state *state)
158
memset(state, 0, sizeof(*state));
160
state->dir = git_pathdup("rebase-apply");
164
git_config_get_bool("am.threeway", &state->threeway);
168
git_config_get_bool("am.messageid", &state->message_id);
170
state->scissors = SCISSORS_UNSET;
171
state->quoted_cr = quoted_cr_unset;
173
strvec_init(&state->git_apply_opts);
175
if (!git_config_get_bool("commit.gpgsign", &gpgsign))
176
state->sign_commit = gpgsign ? "" : NULL;
182
static void am_state_release(struct am_state *state)
185
free(state->author_name);
186
free(state->author_email);
187
free(state->author_date);
189
strvec_clear(&state->git_apply_opts);
192
static int am_option_parse_quoted_cr(const struct option *opt,
193
const char *arg, int unset)
195
BUG_ON_OPT_NEG(unset);
197
if (mailinfo_parse_quoted_cr_action(arg, opt->value) != 0)
198
return error(_("bad action '%s' for '%s'"), arg, "--quoted-cr");
202
static int am_option_parse_empty(const struct option *opt,
203
const char *arg, int unset)
205
int *opt_value = opt->value;
207
BUG_ON_OPT_NEG(unset);
209
if (!strcmp(arg, "stop"))
210
*opt_value = STOP_ON_EMPTY_COMMIT;
211
else if (!strcmp(arg, "drop"))
212
*opt_value = DROP_EMPTY_COMMIT;
213
else if (!strcmp(arg, "keep"))
214
*opt_value = KEEP_EMPTY_COMMIT;
216
return error(_("invalid value for '%s': '%s'"), "--empty", arg);
224
static inline const char *am_path(const struct am_state *state, const char *path)
226
return mkpath("%s/%s", state->dir, path);
232
static void write_state_text(const struct am_state *state,
233
const char *name, const char *string)
235
write_file(am_path(state, name), "%s", string);
238
static void write_state_count(const struct am_state *state,
239
const char *name, int value)
241
write_file(am_path(state, name), "%d", value);
244
static void write_state_bool(const struct am_state *state,
245
const char *name, int value)
247
write_state_text(state, name, value ? "t" : "f");
254
__attribute__((format (printf, 3, 4)))
255
static void say(const struct am_state *state, FILE *fp, const char *fmt, ...)
261
vfprintf(fp, fmt, ap);
270
static int am_in_progress(const struct am_state *state)
274
if (lstat(state->dir, &st) < 0 || !S_ISDIR(st.st_mode))
276
if (lstat(am_path(state, "last"), &st) || !S_ISREG(st.st_mode))
278
if (lstat(am_path(state, "next"), &st) || !S_ISREG(st.st_mode))
288
static int read_state_file(struct strbuf *sb, const struct am_state *state,
289
const char *file, int trim)
293
if (strbuf_read_file(sb, am_path(state, file), 0) >= 0) {
303
die_errno(_("could not read '%s'"), am_path(state, file));
322
static int read_am_author_script(struct am_state *state)
324
const char *filename = am_path(state, "author-script");
326
assert(!state->author_name);
327
assert(!state->author_email);
328
assert(!state->author_date);
330
return read_author_script(filename, &state->author_name,
331
&state->author_email, &state->author_date, 1);
338
static void write_author_script(const struct am_state *state)
340
struct strbuf sb = STRBUF_INIT;
342
strbuf_addstr(&sb, "GIT_AUTHOR_NAME=");
343
sq_quote_buf(&sb, state->author_name);
344
strbuf_addch(&sb, '\n');
346
strbuf_addstr(&sb, "GIT_AUTHOR_EMAIL=");
347
sq_quote_buf(&sb, state->author_email);
348
strbuf_addch(&sb, '\n');
350
strbuf_addstr(&sb, "GIT_AUTHOR_DATE=");
351
sq_quote_buf(&sb, state->author_date);
352
strbuf_addch(&sb, '\n');
354
write_state_text(state, "author-script", sb.buf);
366
static int read_commit_msg(struct am_state *state)
368
struct strbuf sb = STRBUF_INIT;
372
if (read_state_file(&sb, state, "final-commit", 0) < 0) {
377
state->msg = strbuf_detach(&sb, &state->msg_len);
384
static void write_commit_msg(const struct am_state *state)
386
const char *filename = am_path(state, "final-commit");
387
write_file_buf(filename, state->msg, state->msg_len);
393
static void am_load(struct am_state *state)
395
struct strbuf sb = STRBUF_INIT;
397
if (read_state_file(&sb, state, "next", 1) < 0)
398
BUG("state file 'next' does not exist");
399
state->cur = strtol(sb.buf, NULL, 10);
401
if (read_state_file(&sb, state, "last", 1) < 0)
402
BUG("state file 'last' does not exist");
403
state->last = strtol(sb.buf, NULL, 10);
405
if (read_am_author_script(state) < 0)
406
die(_("could not parse author script"));
408
read_commit_msg(state);
410
if (read_state_file(&sb, state, "original-commit", 1) < 0)
411
oidclr(&state->orig_commit, the_repository->hash_algo);
412
else if (get_oid_hex(sb.buf, &state->orig_commit) < 0)
413
die(_("could not parse %s"), am_path(state, "original-commit"));
415
read_state_file(&sb, state, "threeway", 1);
416
state->threeway = !strcmp(sb.buf, "t");
418
read_state_file(&sb, state, "quiet", 1);
419
state->quiet = !strcmp(sb.buf, "t");
421
read_state_file(&sb, state, "sign", 1);
422
state->signoff = !strcmp(sb.buf, "t");
424
read_state_file(&sb, state, "utf8", 1);
425
state->utf8 = !strcmp(sb.buf, "t");
427
if (file_exists(am_path(state, "rerere-autoupdate"))) {
428
read_state_file(&sb, state, "rerere-autoupdate", 1);
429
state->allow_rerere_autoupdate = strcmp(sb.buf, "t") ?
430
RERERE_NOAUTOUPDATE : RERERE_AUTOUPDATE;
432
state->allow_rerere_autoupdate = 0;
435
read_state_file(&sb, state, "keep", 1);
436
if (!strcmp(sb.buf, "t"))
437
state->keep = KEEP_TRUE;
438
else if (!strcmp(sb.buf, "b"))
439
state->keep = KEEP_NON_PATCH;
441
state->keep = KEEP_FALSE;
443
read_state_file(&sb, state, "messageid", 1);
444
state->message_id = !strcmp(sb.buf, "t");
446
read_state_file(&sb, state, "scissors", 1);
447
if (!strcmp(sb.buf, "t"))
448
state->scissors = SCISSORS_TRUE;
449
else if (!strcmp(sb.buf, "f"))
450
state->scissors = SCISSORS_FALSE;
452
state->scissors = SCISSORS_UNSET;
454
read_state_file(&sb, state, "quoted-cr", 1);
456
state->quoted_cr = quoted_cr_unset;
457
else if (mailinfo_parse_quoted_cr_action(sb.buf, &state->quoted_cr) != 0)
458
die(_("could not parse %s"), am_path(state, "quoted-cr"));
460
read_state_file(&sb, state, "apply-opt", 1);
461
strvec_clear(&state->git_apply_opts);
462
if (sq_dequote_to_strvec(sb.buf, &state->git_apply_opts) < 0)
463
die(_("could not parse %s"), am_path(state, "apply-opt"));
465
state->rebasing = !!file_exists(am_path(state, "rebasing"));
474
static void am_destroy(const struct am_state *state)
476
struct strbuf sb = STRBUF_INIT;
478
strbuf_addstr(&sb, state->dir);
479
remove_dir_recursively(&sb, 0);
486
static int run_applypatch_msg_hook(struct am_state *state)
492
if (!state->no_verify)
493
ret = run_hooks_l(the_repository, "applypatch-msg",
494
am_path(state, "final-commit"), NULL);
497
FREE_AND_NULL(state->msg);
498
if (read_commit_msg(state) < 0)
499
die(_("'%s' was deleted by the applypatch-msg hook"),
500
am_path(state, "final-commit"));
509
static int run_post_rewrite_hook(const struct am_state *state)
511
struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
513
strvec_push(&opt.args, "rebase");
514
opt.path_to_stdin = am_path(state, "rewritten");
516
return run_hooks_opt(the_repository, "post-rewrite", &opt);
525
static int copy_notes_for_rebase(const struct am_state *state)
527
struct notes_rewrite_cfg *c;
528
struct strbuf sb = STRBUF_INIT;
529
const char *invalid_line = _("Malformed input line: '%s'.");
530
const char *msg = "Notes added by 'git rebase'";
534
assert(state->rebasing);
536
c = init_copy_notes_for_rewrite("rebase");
540
fp = xfopen(am_path(state, "rewritten"), "r");
542
while (!strbuf_getline_lf(&sb, fp)) {
543
struct object_id from_obj, to_obj;
546
if (sb.len != the_hash_algo->hexsz * 2 + 1) {
547
ret = error(invalid_line, sb.buf);
551
if (parse_oid_hex(sb.buf, &from_obj, &p)) {
552
ret = error(invalid_line, sb.buf);
557
ret = error(invalid_line, sb.buf);
561
if (get_oid_hex(p + 1, &to_obj)) {
562
ret = error(invalid_line, sb.buf);
566
if (copy_note_for_rewrite(c, &from_obj, &to_obj))
567
ret = error(_("Failed to copy notes from '%s' to '%s'"),
568
oid_to_hex(&from_obj), oid_to_hex(&to_obj));
572
finish_copy_notes_for_rewrite(the_repository, c, msg);
585
static int is_mail(FILE *fp)
587
const char *header_regex = "^[!-9;-~]+:";
588
struct strbuf sb = STRBUF_INIT;
592
if (fseek(fp, 0L, SEEK_SET))
593
die_errno(_("fseek failed"));
595
if (regcomp(®ex, header_regex, REG_NOSUB | REG_EXTENDED))
596
die("invalid pattern: %s", header_regex);
598
while (!strbuf_getline(&sb, fp)) {
603
if (*sb.buf == '\t' || *sb.buf == ' ')
607
if (regexec(®ex, sb.buf, 0, NULL, 0)) {
624
static int detect_patch_format(const char **paths)
626
enum patch_format ret = PATCH_FORMAT_UNKNOWN;
627
struct strbuf l1 = STRBUF_INIT;
628
struct strbuf l2 = STRBUF_INIT;
629
struct strbuf l3 = STRBUF_INIT;
635
if (!*paths || !strcmp(*paths, "-") || is_directory(*paths))
636
return PATCH_FORMAT_MBOX;
643
fp = xfopen(*paths, "r");
645
while (!strbuf_getline(&l1, fp)) {
650
if (starts_with(l1.buf, "From ") || starts_with(l1.buf, "From: ")) {
651
ret = PATCH_FORMAT_MBOX;
655
if (starts_with(l1.buf, "# This series applies on GIT commit")) {
656
ret = PATCH_FORMAT_STGIT_SERIES;
660
if (!strcmp(l1.buf, "# HG changeset patch")) {
661
ret = PATCH_FORMAT_HG;
665
strbuf_getline(&l2, fp);
666
strbuf_getline(&l3, fp);
672
if (l1.len && !l2.len &&
673
(starts_with(l3.buf, "From:") ||
674
starts_with(l3.buf, "Author:") ||
675
starts_with(l3.buf, "Date:"))) {
676
ret = PATCH_FORMAT_STGIT;
680
if (l1.len && is_mail(fp)) {
681
ret = PATCH_FORMAT_MBOX;
697
static int split_mail_mbox(struct am_state *state, const char **paths,
698
int keep_cr, int mboxrd)
700
struct child_process cp = CHILD_PROCESS_INIT;
701
struct strbuf last = STRBUF_INIT;
705
strvec_push(&cp.args, "mailsplit");
706
strvec_pushf(&cp.args, "-d%d", state->prec);
707
strvec_pushf(&cp.args, "-o%s", state->dir);
708
strvec_push(&cp.args, "-b");
710
strvec_push(&cp.args, "--keep-cr");
712
strvec_push(&cp.args, "--mboxrd");
713
strvec_push(&cp.args, "--");
714
strvec_pushv(&cp.args, paths);
716
ret = capture_command(&cp, &last, 8);
721
state->last = strtol(last.buf, NULL, 10);
724
strbuf_release(&last);
733
typedef int (*mail_conv_fn)(FILE *out, FILE *in, int keep_cr);
741
static int split_mail_conv(mail_conv_fn fn, struct am_state *state,
742
const char **paths, int keep_cr)
744
static const char *stdin_only[] = {"-", NULL};
750
for (i = 0; *paths; paths++, i++) {
755
if (!strcmp(*paths, "-"))
758
in = fopen(*paths, "r");
761
return error_errno(_("could not open '%s' for reading"),
764
mail = mkpath("%s/%0*d", state->dir, state->prec, i + 1);
766
out = fopen(mail, "w");
770
return error_errno(_("could not open '%s' for writing"),
774
ret = fn(out, in, keep_cr);
781
return error(_("could not parse patch '%s'"), *paths);
793
static int stgit_patch_to_mail(FILE *out, FILE *in, int keep_cr UNUSED)
795
struct strbuf sb = STRBUF_INIT;
796
int subject_printed = 0;
798
while (!strbuf_getline_lf(&sb, in)) {
801
if (str_isspace(sb.buf))
803
else if (skip_prefix(sb.buf, "Author:", &str))
804
fprintf(out, "From:%s\n", str);
805
else if (starts_with(sb.buf, "From") || starts_with(sb.buf, "Date"))
806
fprintf(out, "%s\n", sb.buf);
807
else if (!subject_printed) {
808
fprintf(out, "Subject: %s\n", sb.buf);
811
fprintf(out, "\n%s\n", sb.buf);
817
while (strbuf_fread(&sb, 8192, in) > 0) {
818
fwrite(sb.buf, 1, sb.len, out);
835
static int split_mail_stgit_series(struct am_state *state, const char **paths,
838
const char *series_dir;
839
char *series_dir_buf;
841
struct strvec patches = STRVEC_INIT;
842
struct strbuf sb = STRBUF_INIT;
845
if (!paths[0] || paths[1])
846
return error(_("Only one StGIT patch series can be applied at once"));
848
series_dir_buf = xstrdup(*paths);
849
series_dir = dirname(series_dir_buf);
851
fp = fopen(*paths, "r");
853
return error_errno(_("could not open '%s' for reading"), *paths);
855
while (!strbuf_getline_lf(&sb, fp)) {
859
strvec_push(&patches, mkpath("%s/%s", series_dir, sb.buf));
864
free(series_dir_buf);
866
ret = split_mail_conv(stgit_patch_to_mail, state, patches.v, keep_cr);
868
strvec_clear(&patches);
876
static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr UNUSED)
878
struct strbuf sb = STRBUF_INIT;
881
while (!strbuf_getline_lf(&sb, in)) {
884
if (skip_prefix(sb.buf, "# User ", &str))
885
fprintf(out, "From: %s\n", str);
886
else if (skip_prefix(sb.buf, "# Date ", &str)) {
887
timestamp_t timestamp;
892
timestamp = parse_timestamp(str, &end, 10);
894
rc = error(_("invalid timestamp"));
898
if (!skip_prefix(end, " ", &str)) {
899
rc = error(_("invalid Date line"));
904
tz = strtol(str, &end, 10);
906
rc = error(_("invalid timezone offset"));
911
rc = error(_("invalid Date line"));
920
tz2 = labs(tz) / 3600 * 100 + labs(tz) % 3600 / 60;
924
fprintf(out, "Date: %s\n", show_date(timestamp, tz2, DATE_MODE(RFC2822)));
925
} else if (starts_with(sb.buf, "# ")) {
928
fprintf(out, "\n%s\n", sb.buf);
934
while (strbuf_fread(&sb, 8192, in) > 0) {
935
fwrite(sb.buf, 1, sb.len, out);
960
static int split_mail(struct am_state *state, enum patch_format patch_format,
961
const char **paths, int keep_cr)
965
git_config_get_bool("am.keepcr", &keep_cr);
968
switch (patch_format) {
969
case PATCH_FORMAT_MBOX:
970
return split_mail_mbox(state, paths, keep_cr, 0);
971
case PATCH_FORMAT_STGIT:
972
return split_mail_conv(stgit_patch_to_mail, state, paths, keep_cr);
973
case PATCH_FORMAT_STGIT_SERIES:
974
return split_mail_stgit_series(state, paths, keep_cr);
975
case PATCH_FORMAT_HG:
976
return split_mail_conv(hg_patch_to_mail, state, paths, keep_cr);
977
case PATCH_FORMAT_MBOXRD:
978
return split_mail_mbox(state, paths, keep_cr, 1);
980
BUG("invalid patch_format");
988
static void am_setup(struct am_state *state, enum patch_format patch_format,
989
const char **paths, int keep_cr)
991
struct object_id curr_head;
993
struct strbuf sb = STRBUF_INIT;
996
patch_format = detect_patch_format(paths);
999
fprintf_ln(stderr, _("Patch format detection failed."));
1003
if (mkdir(state->dir, 0777) < 0 && errno != EEXIST)
1004
die_errno(_("failed to create directory '%s'"), state->dir);
1005
refs_delete_ref(get_main_ref_store(the_repository), NULL,
1006
"REBASE_HEAD", NULL, REF_NO_DEREF);
1008
if (split_mail(state, patch_format, paths, keep_cr) < 0) {
1010
die(_("Failed to split patches."));
1013
if (state->rebasing)
1014
state->threeway = 1;
1016
write_state_bool(state, "threeway", state->threeway);
1017
write_state_bool(state, "quiet", state->quiet);
1018
write_state_bool(state, "sign", state->signoff);
1019
write_state_bool(state, "utf8", state->utf8);
1021
if (state->allow_rerere_autoupdate)
1022
write_state_bool(state, "rerere-autoupdate",
1023
state->allow_rerere_autoupdate == RERERE_AUTOUPDATE);
1025
switch (state->keep) {
1032
case KEEP_NON_PATCH:
1036
BUG("invalid value for state->keep");
1039
write_state_text(state, "keep", str);
1040
write_state_bool(state, "messageid", state->message_id);
1042
switch (state->scissors) {
1043
case SCISSORS_UNSET:
1046
case SCISSORS_FALSE:
1053
BUG("invalid value for state->scissors");
1055
write_state_text(state, "scissors", str);
1057
switch (state->quoted_cr) {
1058
case quoted_cr_unset:
1061
case quoted_cr_nowarn:
1064
case quoted_cr_warn:
1067
case quoted_cr_strip:
1071
BUG("invalid value for state->quoted_cr");
1073
write_state_text(state, "quoted-cr", str);
1075
sq_quote_argv(&sb, state->git_apply_opts.v);
1076
write_state_text(state, "apply-opt", sb.buf);
1078
if (state->rebasing)
1079
write_state_text(state, "rebasing", "");
1081
write_state_text(state, "applying", "");
1083
if (!repo_get_oid(the_repository, "HEAD", &curr_head)) {
1084
write_state_text(state, "abort-safety", oid_to_hex(&curr_head));
1085
if (!state->rebasing)
1086
refs_update_ref(get_main_ref_store(the_repository),
1087
"am", "ORIG_HEAD", &curr_head, NULL,
1089
UPDATE_REFS_DIE_ON_ERR);
1091
write_state_text(state, "abort-safety", "");
1092
if (!state->rebasing)
1093
refs_delete_ref(get_main_ref_store(the_repository),
1094
NULL, "ORIG_HEAD", NULL, 0);
1102
write_state_count(state, "next", state->cur);
1103
write_state_count(state, "last", state->last);
1105
strbuf_release(&sb);
1112
static void am_next(struct am_state *state)
1114
struct object_id head;
1116
FREE_AND_NULL(state->author_name);
1117
FREE_AND_NULL(state->author_email);
1118
FREE_AND_NULL(state->author_date);
1119
FREE_AND_NULL(state->msg);
1122
unlink(am_path(state, "author-script"));
1123
unlink(am_path(state, "final-commit"));
1125
oidclr(&state->orig_commit, the_repository->hash_algo);
1126
unlink(am_path(state, "original-commit"));
1127
refs_delete_ref(get_main_ref_store(the_repository), NULL,
1128
"REBASE_HEAD", NULL, REF_NO_DEREF);
1130
if (!repo_get_oid(the_repository, "HEAD", &head))
1131
write_state_text(state, "abort-safety", oid_to_hex(&head));
1133
write_state_text(state, "abort-safety", "");
1136
write_state_count(state, "next", state->cur);
1142
static const char *msgnum(const struct am_state *state)
1144
static struct strbuf sb = STRBUF_INIT;
1147
strbuf_addf(&sb, "%0*d", state->prec, state->cur);
1156
static void NORETURN die_user_resolve(const struct am_state *state)
1158
if (state->resolvemsg) {
1159
advise_if_enabled(ADVICE_MERGE_CONFLICT, "%s", state->resolvemsg);
1161
const char *cmdline = state->interactive ? "git am -i" : "git am";
1162
struct strbuf sb = STRBUF_INIT;
1164
strbuf_addf(&sb, _("When you have resolved this problem, run \"%s --continue\".\n"), cmdline);
1165
strbuf_addf(&sb, _("If you prefer to skip this patch, run \"%s --skip\" instead.\n"), cmdline);
1167
if (advice_enabled(ADVICE_AM_WORK_DIR) &&
1168
is_empty_or_missing_file(am_path(state, "patch")) &&
1169
!repo_index_has_changes(the_repository, NULL, NULL))
1170
strbuf_addf(&sb, _("To record the empty patch as an empty commit, run \"%s --allow-empty\".\n"), cmdline);
1172
strbuf_addf(&sb, _("To restore the original branch and stop patching, run \"%s --abort\"."), cmdline);
1174
advise_if_enabled(ADVICE_MERGE_CONFLICT, "%s", sb.buf);
1175
strbuf_release(&sb);
1184
static void am_append_signoff(struct am_state *state)
1186
struct strbuf sb = STRBUF_INIT;
1188
strbuf_attach(&sb, state->msg, state->msg_len, state->msg_len);
1189
append_signoff(&sb, 0, 0);
1190
state->msg = strbuf_detach(&sb, &state->msg_len);
1202
static int parse_mail(struct am_state *state, const char *mail)
1205
struct strbuf sb = STRBUF_INIT;
1206
struct strbuf msg = STRBUF_INIT;
1207
struct strbuf author_name = STRBUF_INIT;
1208
struct strbuf author_date = STRBUF_INIT;
1209
struct strbuf author_email = STRBUF_INIT;
1213
setup_mailinfo(&mi);
1216
mi.metainfo_charset = get_commit_output_encoding();
1218
mi.metainfo_charset = NULL;
1220
switch (state->keep) {
1224
mi.keep_subject = 1;
1226
case KEEP_NON_PATCH:
1227
mi.keep_non_patch_brackets_in_subject = 1;
1230
BUG("invalid value for state->keep");
1233
if (state->message_id)
1234
mi.add_message_id = 1;
1236
switch (state->scissors) {
1237
case SCISSORS_UNSET:
1239
case SCISSORS_FALSE:
1240
mi.use_scissors = 0;
1243
mi.use_scissors = 1;
1246
BUG("invalid value for state->scissors");
1249
switch (state->quoted_cr) {
1250
case quoted_cr_unset:
1252
case quoted_cr_nowarn:
1253
case quoted_cr_warn:
1254
case quoted_cr_strip:
1255
mi.quoted_cr = state->quoted_cr;
1258
BUG("invalid value for state->quoted_cr");
1261
mi.input = xfopen(mail, "r");
1262
mi.output = xfopen(am_path(state, "info"), "w");
1263
if (mailinfo(&mi, am_path(state, "msg"), am_path(state, "patch")))
1264
die("could not parse patch");
1269
if (mi.format_flowed)
1270
warning(_("Patch sent with format=flowed; "
1271
"space at the end of lines might be lost."));
1274
fp = xfopen(am_path(state, "info"), "r");
1275
while (!strbuf_getline_lf(&sb, fp)) {
1278
if (skip_prefix(sb.buf, "Subject: ", &x)) {
1280
strbuf_addch(&msg, '\n');
1281
strbuf_addstr(&msg, x);
1282
} else if (skip_prefix(sb.buf, "Author: ", &x))
1283
strbuf_addstr(&author_name, x);
1284
else if (skip_prefix(sb.buf, "Email: ", &x))
1285
strbuf_addstr(&author_email, x);
1286
else if (skip_prefix(sb.buf, "Date: ", &x))
1287
strbuf_addstr(&author_date, x);
1292
if (!strcmp(author_name.buf, "Mail System Internal Data")) {
1297
strbuf_addstr(&msg, "\n\n");
1298
strbuf_addbuf(&msg, &mi.log_message);
1299
strbuf_stripspace(&msg, NULL);
1301
assert(!state->author_name);
1302
state->author_name = strbuf_detach(&author_name, NULL);
1304
assert(!state->author_email);
1305
state->author_email = strbuf_detach(&author_email, NULL);
1307
assert(!state->author_date);
1308
state->author_date = strbuf_detach(&author_date, NULL);
1310
assert(!state->msg);
1311
state->msg = strbuf_detach(&msg, &state->msg_len);
1314
strbuf_release(&msg);
1315
strbuf_release(&author_date);
1316
strbuf_release(&author_email);
1317
strbuf_release(&author_name);
1318
strbuf_release(&sb);
1319
clear_mailinfo(&mi);
1327
static int get_mail_commit_oid(struct object_id *commit_id, const char *mail)
1329
struct strbuf sb = STRBUF_INIT;
1330
FILE *fp = xfopen(mail, "r");
1334
if (strbuf_getline_lf(&sb, fp) ||
1335
!skip_prefix(sb.buf, "From ", &x) ||
1336
get_oid_hex(x, commit_id) < 0)
1339
strbuf_release(&sb);
1348
static void get_commit_info(struct am_state *state, struct commit *commit)
1350
const char *buffer, *ident_line, *msg;
1352
struct ident_split id;
1354
buffer = repo_logmsg_reencode(the_repository, commit, NULL,
1355
get_commit_output_encoding());
1357
ident_line = find_commit_header(buffer, "author", &ident_len);
1359
die(_("missing author line in commit %s"),
1360
oid_to_hex(&commit->object.oid));
1361
if (split_ident_line(&id, ident_line, ident_len) < 0)
1362
die(_("invalid ident line: %.*s"), (int)ident_len, ident_line);
1364
assert(!state->author_name);
1366
state->author_name =
1367
xmemdupz(id.name_begin, id.name_end - id.name_begin);
1369
state->author_name = xstrdup("");
1371
assert(!state->author_email);
1373
state->author_email =
1374
xmemdupz(id.mail_begin, id.mail_end - id.mail_begin);
1376
state->author_email = xstrdup("");
1378
assert(!state->author_date);
1379
state->author_date = xstrdup(show_ident_date(&id, DATE_MODE(NORMAL)));
1381
assert(!state->msg);
1382
msg = strstr(buffer, "\n\n");
1384
die(_("unable to parse commit %s"), oid_to_hex(&commit->object.oid));
1385
state->msg = xstrdup(msg + 2);
1386
state->msg_len = strlen(state->msg);
1387
repo_unuse_commit_buffer(the_repository, commit, buffer);
1393
static void write_commit_patch(const struct am_state *state, struct commit *commit)
1395
struct rev_info rev_info;
1398
fp = xfopen(am_path(state, "patch"), "w");
1399
repo_init_revisions(the_repository, &rev_info, NULL);
1401
rev_info.abbrev = 0;
1402
rev_info.disable_stdin = 1;
1403
rev_info.show_root_diff = 1;
1404
rev_info.diffopt.output_format = DIFF_FORMAT_PATCH;
1405
rev_info.no_commit_id = 1;
1406
rev_info.diffopt.flags.binary = 1;
1407
rev_info.diffopt.flags.full_index = 1;
1408
rev_info.diffopt.use_color = 0;
1409
rev_info.diffopt.file = fp;
1410
rev_info.diffopt.close_file = 1;
1411
add_pending_object(&rev_info, &commit->object, "");
1412
diff_setup_done(&rev_info.diffopt);
1413
log_tree_commit(&rev_info, commit);
1414
release_revisions(&rev_info);
1421
static void write_index_patch(const struct am_state *state)
1424
struct object_id head;
1425
struct rev_info rev_info;
1428
if (!repo_get_oid(the_repository, "HEAD", &head)) {
1429
struct commit *commit = lookup_commit_or_die(&head, "HEAD");
1430
tree = repo_get_commit_tree(the_repository, commit);
1432
tree = lookup_tree(the_repository,
1433
the_repository->hash_algo->empty_tree);
1435
fp = xfopen(am_path(state, "patch"), "w");
1436
repo_init_revisions(the_repository, &rev_info, NULL);
1438
rev_info.disable_stdin = 1;
1439
rev_info.no_commit_id = 1;
1440
rev_info.diffopt.output_format = DIFF_FORMAT_PATCH;
1441
rev_info.diffopt.use_color = 0;
1442
rev_info.diffopt.file = fp;
1443
rev_info.diffopt.close_file = 1;
1444
add_pending_object(&rev_info, &tree->object, "");
1445
diff_setup_done(&rev_info.diffopt);
1446
run_diff_index(&rev_info, DIFF_INDEX_CACHED);
1447
release_revisions(&rev_info);
1459
static int parse_mail_rebase(struct am_state *state, const char *mail)
1461
struct commit *commit;
1462
struct object_id commit_oid;
1464
if (get_mail_commit_oid(&commit_oid, mail) < 0)
1465
die(_("could not parse %s"), mail);
1467
commit = lookup_commit_or_die(&commit_oid, mail);
1469
get_commit_info(state, commit);
1471
write_commit_patch(state, commit);
1473
oidcpy(&state->orig_commit, &commit_oid);
1474
write_state_text(state, "original-commit", oid_to_hex(&commit_oid));
1475
refs_update_ref(get_main_ref_store(the_repository), "am",
1476
"REBASE_HEAD", &commit_oid,
1477
NULL, REF_NO_DEREF, UPDATE_REFS_DIE_ON_ERR);
1486
static int run_apply(const struct am_state *state, const char *index_file)
1488
struct strvec apply_paths = STRVEC_INIT;
1489
struct strvec apply_opts = STRVEC_INIT;
1490
struct apply_state apply_state;
1492
int force_apply = 0;
1494
const char **apply_argv;
1496
if (init_apply_state(&apply_state, the_repository, NULL))
1497
BUG("init_apply_state() failed");
1499
strvec_push(&apply_opts, "apply");
1500
strvec_pushv(&apply_opts, state->git_apply_opts.v);
1507
DUP_ARRAY(apply_argv, apply_opts.v, apply_opts.nr);
1509
opts_left = apply_parse_options(apply_opts.nr, apply_argv,
1510
&apply_state, &force_apply, &options,
1514
die("unknown option passed through to git apply");
1517
apply_state.index_file = index_file;
1518
apply_state.cached = 1;
1520
apply_state.check_index = 1;
1526
if (state->threeway && !index_file)
1527
apply_state.apply_verbosity = verbosity_silent;
1529
if (check_apply_state(&apply_state, force_apply))
1530
BUG("check_apply_state() failed");
1532
strvec_push(&apply_paths, am_path(state, "patch"));
1534
res = apply_all_patches(&apply_state, apply_paths.nr, apply_paths.v, options);
1536
strvec_clear(&apply_paths);
1537
strvec_clear(&apply_opts);
1538
clear_apply_state(&apply_state);
1546
discard_index(the_repository->index);
1547
read_index_from(the_repository->index, index_file, get_git_dir());
1556
static int build_fake_ancestor(const struct am_state *state, const char *index_file)
1558
struct child_process cp = CHILD_PROCESS_INIT;
1561
strvec_push(&cp.args, "apply");
1562
strvec_pushv(&cp.args, state->git_apply_opts.v);
1563
strvec_pushf(&cp.args, "--build-fake-ancestor=%s", index_file);
1564
strvec_push(&cp.args, am_path(state, "patch"));
1566
if (run_command(&cp))
1575
static int fall_back_threeway(const struct am_state *state, const char *index_path)
1577
struct object_id their_tree, our_tree;
1578
struct object_id bases[1] = { 0 };
1579
struct merge_options o;
1580
struct commit *result;
1581
char *their_tree_name;
1583
if (repo_get_oid(the_repository, "HEAD", &our_tree) < 0)
1584
oidcpy(&our_tree, the_hash_algo->empty_tree);
1586
if (build_fake_ancestor(state, index_path))
1587
return error("could not build fake ancestor");
1589
discard_index(the_repository->index);
1590
read_index_from(the_repository->index, index_path, get_git_dir());
1592
if (write_index_as_tree(&bases[0], the_repository->index, index_path, 0, NULL))
1593
return error(_("Repository lacks necessary blobs to fall back on 3-way merge."));
1595
say(state, stdout, _("Using index info to reconstruct a base tree..."));
1597
if (!state->quiet) {
1602
struct rev_info rev_info;
1604
repo_init_revisions(the_repository, &rev_info, NULL);
1605
rev_info.diffopt.output_format = DIFF_FORMAT_NAME_STATUS;
1606
rev_info.diffopt.filter |= diff_filter_bit('A');
1607
rev_info.diffopt.filter |= diff_filter_bit('M');
1608
add_pending_oid(&rev_info, "HEAD", &our_tree, 0);
1609
diff_setup_done(&rev_info.diffopt);
1610
run_diff_index(&rev_info, DIFF_INDEX_CACHED);
1611
release_revisions(&rev_info);
1614
if (run_apply(state, index_path))
1615
return error(_("Did you hand edit your patch?\n"
1616
"It does not apply to blobs recorded in its index."));
1618
if (write_index_as_tree(&their_tree, the_repository->index, index_path, 0, NULL))
1619
return error("could not write tree");
1621
say(state, stdout, _("Falling back to patching base and 3-way merge..."));
1623
discard_index(the_repository->index);
1624
repo_read_index(the_repository);
1634
init_ui_merge_options(&o, the_repository);
1637
their_tree_name = xstrfmt("%.*s", linelen(state->msg), state->msg);
1638
o.branch2 = their_tree_name;
1639
o.detect_directory_renames = MERGE_DIRECTORY_RENAMES_NONE;
1644
if (merge_recursive_generic(&o, &our_tree, &their_tree, 1, bases, &result)) {
1645
repo_rerere(the_repository, state->allow_rerere_autoupdate);
1646
free(their_tree_name);
1647
return error(_("Failed to merge in the changes."));
1650
free(their_tree_name);
1659
static void do_commit(const struct am_state *state)
1661
struct object_id tree, parent, commit;
1662
const struct object_id *old_oid;
1663
struct commit_list *parents = NULL;
1664
const char *reflog_msg, *author, *committer = NULL;
1665
struct strbuf sb = STRBUF_INIT;
1667
if (!state->no_verify && run_hooks(the_repository, "pre-applypatch"))
1670
if (write_index_as_tree(&tree, the_repository->index, get_index_file(), 0, NULL))
1671
die(_("git write-tree failed to write a tree"));
1673
if (!repo_get_oid_commit(the_repository, "HEAD", &parent)) {
1675
commit_list_insert(lookup_commit(the_repository, &parent),
1679
say(state, stderr, _("applying to an empty history"));
1682
author = fmt_ident(state->author_name, state->author_email,
1684
state->ignore_date ? NULL : state->author_date,
1687
if (state->committer_date_is_author_date)
1688
committer = fmt_ident(getenv("GIT_COMMITTER_NAME"),
1689
getenv("GIT_COMMITTER_EMAIL"),
1690
WANT_COMMITTER_IDENT,
1691
state->ignore_date ? NULL
1692
: state->author_date,
1695
if (commit_tree_extended(state->msg, state->msg_len, &tree, parents,
1696
&commit, author, committer, state->sign_commit,
1698
die(_("failed to write commit object"));
1700
reflog_msg = getenv("GIT_REFLOG_ACTION");
1704
strbuf_addf(&sb, "%s: %.*s", reflog_msg, linelen(state->msg),
1707
refs_update_ref(get_main_ref_store(the_repository), sb.buf, "HEAD",
1708
&commit, old_oid, 0,
1709
UPDATE_REFS_DIE_ON_ERR);
1711
if (state->rebasing) {
1712
FILE *fp = xfopen(am_path(state, "rewritten"), "a");
1714
assert(!is_null_oid(&state->orig_commit));
1715
fprintf(fp, "%s ", oid_to_hex(&state->orig_commit));
1716
fprintf(fp, "%s\n", oid_to_hex(&commit));
1720
run_hooks(the_repository, "post-applypatch");
1722
free_commit_list(parents);
1723
strbuf_release(&sb);
1730
static void validate_resume_state(const struct am_state *state)
1733
die(_("cannot resume: %s does not exist."),
1734
am_path(state, "final-commit"));
1736
if (!state->author_name || !state->author_email || !state->author_date)
1737
die(_("cannot resume: %s does not exist."),
1738
am_path(state, "author-script"));
1748
static int do_interactive(struct am_state *state)
1755
puts(_("Commit Body is:"));
1756
puts("--------------------------");
1757
printf("%s", state->msg);
1758
puts("--------------------------");
1765
printf(_("Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all: "));
1766
if (!fgets(reply, sizeof(reply), stdin))
1767
die("unable to read from stdin; aborting");
1769
if (*reply == 'y' || *reply == 'Y') {
1771
} else if (*reply == 'a' || *reply == 'A') {
1772
state->interactive = 0;
1774
} else if (*reply == 'n' || *reply == 'N') {
1776
} else if (*reply == 'e' || *reply == 'E') {
1777
struct strbuf msg = STRBUF_INIT;
1779
if (!launch_editor(am_path(state, "final-commit"), &msg, NULL)) {
1781
state->msg = strbuf_detach(&msg, &state->msg_len);
1783
strbuf_release(&msg);
1784
} else if (*reply == 'v' || *reply == 'V') {
1785
const char *pager = git_pager(1);
1786
struct child_process cp = CHILD_PROCESS_INIT;
1790
prepare_pager_args(&cp, pager);
1791
strvec_push(&cp.args, am_path(state, "patch"));
1804
static void am_run(struct am_state *state, int resume)
1806
struct strbuf sb = STRBUF_INIT;
1808
unlink(am_path(state, "dirtyindex"));
1810
if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,
1811
NULL, NULL, NULL) < 0)
1812
die(_("unable to write index file"));
1814
if (repo_index_has_changes(the_repository, NULL, &sb)) {
1815
write_state_bool(state, "dirtyindex", 1);
1816
die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
1819
strbuf_release(&sb);
1821
while (state->cur <= state->last) {
1822
const char *mail = am_path(state, msgnum(state));
1828
if (!file_exists(mail))
1832
validate_resume_state(state);
1836
if (state->rebasing)
1837
skip = parse_mail_rebase(state, mail);
1839
skip = parse_mail(state, mail);
1845
am_append_signoff(state);
1847
write_author_script(state);
1848
write_commit_msg(state);
1851
if (state->interactive && do_interactive(state))
1855
if (is_empty_or_missing_file(am_path(state, "patch"))) {
1856
switch (state->empty_type) {
1857
case DROP_EMPTY_COMMIT:
1858
say(state, stdout, _("Skipping: %.*s"), linelen(state->msg), state->msg);
1861
case KEEP_EMPTY_COMMIT:
1863
say(state, stdout, _("Creating an empty commit: %.*s"),
1864
linelen(state->msg), state->msg);
1866
case STOP_ON_EMPTY_COMMIT:
1867
printf_ln(_("Patch is empty."));
1868
die_user_resolve(state);
1873
if (run_applypatch_msg_hook(state))
1878
say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
1880
apply_status = run_apply(state, NULL);
1882
if (apply_status && state->threeway) {
1883
struct strbuf sb = STRBUF_INIT;
1885
strbuf_addstr(&sb, am_path(state, "patch-merge-index"));
1886
apply_status = fall_back_threeway(state, sb.buf);
1887
strbuf_release(&sb);
1893
if (!apply_status &&
1894
!repo_index_has_changes(the_repository, NULL, NULL)) {
1895
say(state, stdout, _("No changes -- Patch already applied."));
1901
printf_ln(_("Patch failed at %s %.*s"), msgnum(state),
1902
linelen(state->msg), state->msg);
1904
if (advice_enabled(ADVICE_AM_WORK_DIR))
1905
advise(_("Use 'git am --show-current-patch=diff' to see the failed patch"));
1907
die_user_resolve(state);
1921
if (!is_empty_or_missing_file(am_path(state, "rewritten"))) {
1922
assert(state->rebasing);
1923
copy_notes_for_rebase(state);
1924
run_post_rewrite_hook(state);
1931
if (!state->rebasing) {
1933
run_auto_maintenance(state->quiet);
1943
static void am_resolve(struct am_state *state, int allow_empty)
1945
validate_resume_state(state);
1947
say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
1949
if (!repo_index_has_changes(the_repository, NULL, NULL)) {
1950
if (allow_empty && is_empty_or_missing_file(am_path(state, "patch"))) {
1951
printf_ln(_("No changes - recorded it as an empty commit."));
1953
printf_ln(_("No changes - did you forget to use 'git add'?\n"
1954
"If there is nothing left to stage, chances are that something else\n"
1955
"already introduced the same changes; you might want to skip this patch."));
1956
die_user_resolve(state);
1960
if (unmerged_index(the_repository->index)) {
1961
printf_ln(_("You still have unmerged paths in your index.\n"
1962
"You should 'git add' each file with resolved conflicts to mark them as such.\n"
1963
"You might run `git rm` on a file to accept \"deleted by them\" for it."));
1964
die_user_resolve(state);
1967
if (state->interactive) {
1968
write_index_patch(state);
1969
if (do_interactive(state))
1973
repo_rerere(the_repository, 0);
1988
static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
1990
struct lock_file lock_file = LOCK_INIT;
1991
struct unpack_trees_options opts;
1992
struct tree_desc t[2];
1994
if (parse_tree(head) || parse_tree(remote))
1997
repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
1999
refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);
2001
memset(&opts, 0, sizeof(opts));
2003
opts.src_index = the_repository->index;
2004
opts.dst_index = the_repository->index;
2007
opts.reset = reset ? UNPACK_RESET_PROTECT_UNTRACKED : 0;
2008
opts.preserve_ignored = 0;
2009
opts.fn = twoway_merge;
2010
init_tree_desc(&t[0], &head->object.oid, head->buffer, head->size);
2011
init_tree_desc(&t[1], &remote->object.oid, remote->buffer, remote->size);
2013
if (unpack_trees(2, t, &opts)) {
2014
rollback_lock_file(&lock_file);
2018
if (write_locked_index(the_repository->index, &lock_file, COMMIT_LOCK))
2019
die(_("unable to write new index file"));
2028
static int merge_tree(struct tree *tree)
2030
struct lock_file lock_file = LOCK_INIT;
2031
struct unpack_trees_options opts;
2032
struct tree_desc t[1];
2034
if (parse_tree(tree))
2037
repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
2039
memset(&opts, 0, sizeof(opts));
2041
opts.src_index = the_repository->index;
2042
opts.dst_index = the_repository->index;
2044
opts.fn = oneway_merge;
2045
init_tree_desc(&t[0], &tree->object.oid, tree->buffer, tree->size);
2047
if (unpack_trees(1, t, &opts)) {
2048
rollback_lock_file(&lock_file);
2052
if (write_locked_index(the_repository->index, &lock_file, COMMIT_LOCK))
2053
die(_("unable to write new index file"));
2062
static int clean_index(const struct object_id *head, const struct object_id *remote)
2064
struct tree *head_tree, *remote_tree, *index_tree;
2065
struct object_id index;
2067
head_tree = parse_tree_indirect(head);
2069
return error(_("Could not parse object '%s'."), oid_to_hex(head));
2071
remote_tree = parse_tree_indirect(remote);
2073
return error(_("Could not parse object '%s'."), oid_to_hex(remote));
2075
repo_read_index_unmerged(the_repository);
2077
if (fast_forward_to(head_tree, head_tree, 1))
2080
if (write_index_as_tree(&index, the_repository->index, get_index_file(), 0, NULL))
2083
index_tree = parse_tree_indirect(&index);
2085
return error(_("Could not parse object '%s'."), oid_to_hex(&index));
2087
if (fast_forward_to(index_tree, remote_tree, 0))
2090
if (merge_tree(remote_tree))
2093
remove_branch_state(the_repository, 0);
2101
static void am_rerere_clear(void)
2103
struct string_list merge_rr = STRING_LIST_INIT_DUP;
2104
rerere_clear(the_repository, &merge_rr);
2105
string_list_clear(&merge_rr, 1);
2111
static void am_skip(struct am_state *state)
2113
struct object_id head;
2117
if (repo_get_oid(the_repository, "HEAD", &head))
2118
oidcpy(&head, the_hash_algo->empty_tree);
2120
if (clean_index(&head, &head))
2121
die(_("failed to clean index"));
2123
if (state->rebasing) {
2124
FILE *fp = xfopen(am_path(state, "rewritten"), "a");
2126
assert(!is_null_oid(&state->orig_commit));
2127
fprintf(fp, "%s ", oid_to_hex(&state->orig_commit));
2128
fprintf(fp, "%s\n", oid_to_hex(&head));
2144
static int safe_to_abort(const struct am_state *state)
2146
struct strbuf sb = STRBUF_INIT;
2147
struct object_id abort_safety, head;
2149
if (file_exists(am_path(state, "dirtyindex")))
2152
if (read_state_file(&sb, state, "abort-safety", 1) > 0) {
2153
if (get_oid_hex(sb.buf, &abort_safety))
2154
die(_("could not parse %s"), am_path(state, "abort-safety"));
2156
oidclr(&abort_safety, the_repository->hash_algo);
2157
strbuf_release(&sb);
2159
if (repo_get_oid(the_repository, "HEAD", &head))
2160
oidclr(&head, the_repository->hash_algo);
2162
if (oideq(&head, &abort_safety))
2165
warning(_("You seem to have moved HEAD since the last 'am' failure.\n"
2166
"Not rewinding to ORIG_HEAD"));
2174
static void am_abort(struct am_state *state)
2176
struct object_id curr_head, orig_head;
2177
int has_curr_head, has_orig_head;
2180
if (!safe_to_abort(state)) {
2187
curr_branch = refs_resolve_refdup(get_main_ref_store(the_repository),
2188
"HEAD", 0, &curr_head, NULL);
2189
has_curr_head = curr_branch && !is_null_oid(&curr_head);
2191
oidcpy(&curr_head, the_hash_algo->empty_tree);
2193
has_orig_head = !repo_get_oid(the_repository, "ORIG_HEAD", &orig_head);
2195
oidcpy(&orig_head, the_hash_algo->empty_tree);
2197
if (clean_index(&curr_head, &orig_head))
2198
die(_("failed to clean index"));
2201
refs_update_ref(get_main_ref_store(the_repository),
2202
"am --abort", "HEAD", &orig_head,
2203
has_curr_head ? &curr_head : NULL, 0,
2204
UPDATE_REFS_DIE_ON_ERR);
2205
else if (curr_branch)
2206
refs_delete_ref(get_main_ref_store(the_repository), NULL,
2207
curr_branch, NULL, REF_NO_DEREF);
2213
static int show_patch(struct am_state *state, enum resume_type resume_mode)
2215
struct strbuf sb = STRBUF_INIT;
2216
const char *patch_path;
2219
if (!is_null_oid(&state->orig_commit)) {
2220
struct child_process cmd = CHILD_PROCESS_INIT;
2222
strvec_pushl(&cmd.args, "show", oid_to_hex(&state->orig_commit),
2225
return run_command(&cmd);
2228
switch (resume_mode) {
2229
case RESUME_SHOW_PATCH_RAW:
2230
patch_path = am_path(state, msgnum(state));
2232
case RESUME_SHOW_PATCH_DIFF:
2233
patch_path = am_path(state, "patch");
2236
BUG("invalid mode for --show-current-patch");
2239
len = strbuf_read_file(&sb, patch_path, 0);
2241
die_errno(_("failed to read '%s'"), patch_path);
2244
write_in_full(1, sb.buf, sb.len);
2245
strbuf_release(&sb);
2253
static int parse_opt_patchformat(const struct option *opt, const char *arg, int unset)
2255
int *opt_value = opt->value;
2258
*opt_value = PATCH_FORMAT_UNKNOWN;
2259
else if (!strcmp(arg, "mbox"))
2260
*opt_value = PATCH_FORMAT_MBOX;
2261
else if (!strcmp(arg, "stgit"))
2262
*opt_value = PATCH_FORMAT_STGIT;
2263
else if (!strcmp(arg, "stgit-series"))
2264
*opt_value = PATCH_FORMAT_STGIT_SERIES;
2265
else if (!strcmp(arg, "hg"))
2266
*opt_value = PATCH_FORMAT_HG;
2267
else if (!strcmp(arg, "mboxrd"))
2268
*opt_value = PATCH_FORMAT_MBOXRD;
2274
return error(_("invalid value for '%s': '%s'"),
2275
"--patch-format", arg);
2279
static int parse_opt_show_current_patch(const struct option *opt, const char *arg, int unset)
2281
int *opt_value = opt->value;
2283
BUG_ON_OPT_NEG(unset);
2286
*opt_value = opt->defval;
2287
else if (!strcmp(arg, "raw"))
2288
*opt_value = RESUME_SHOW_PATCH_RAW;
2289
else if (!strcmp(arg, "diff"))
2290
*opt_value = RESUME_SHOW_PATCH_DIFF;
2296
return error(_("invalid value for '%s': '%s'"),
2297
"--show-current-patch", arg);
2301
int cmd_am(int argc, const char **argv, const char *prefix)
2303
struct am_state state;
2306
int patch_format = PATCH_FORMAT_UNKNOWN;
2307
enum resume_type resume_mode = RESUME_FALSE;
2311
const char * const usage[] = {
2312
N_("git am [<options>] [(<mbox> | <Maildir>)...]"),
2313
N_("git am [<options>] (--continue | --skip | --abort)"),
2317
struct option options[] = {
2318
OPT_BOOL('i', "interactive", &state.interactive,
2319
N_("run interactively")),
2320
OPT_BOOL('n', "no-verify", &state.no_verify,
2321
N_("bypass pre-applypatch and applypatch-msg hooks")),
2322
OPT_HIDDEN_BOOL('b', "binary", &binary,
2323
N_("historical option -- no-op")),
2324
OPT_BOOL('3', "3way", &state.threeway,
2325
N_("allow fall back on 3way merging if needed")),
2326
OPT__QUIET(&state.quiet, N_("be quiet")),
2327
OPT_SET_INT('s', "signoff", &state.signoff,
2328
N_("add a Signed-off-by trailer to the commit message"),
2330
OPT_BOOL('u', "utf8", &state.utf8,
2331
N_("recode into utf8 (default)")),
2332
OPT_SET_INT('k', "keep", &state.keep,
2333
N_("pass -k flag to git-mailinfo"), KEEP_TRUE),
2334
OPT_SET_INT(0, "keep-non-patch", &state.keep,
2335
N_("pass -b flag to git-mailinfo"), KEEP_NON_PATCH),
2336
OPT_BOOL('m', "message-id", &state.message_id,
2337
N_("pass -m flag to git-mailinfo")),
2338
OPT_SET_INT(0, "keep-cr", &keep_cr,
2339
N_("pass --keep-cr flag to git-mailsplit for mbox format"),
2341
OPT_BOOL('c', "scissors", &state.scissors,
2342
N_("strip everything before a scissors line")),
2343
OPT_CALLBACK_F(0, "quoted-cr", &state.quoted_cr, N_("action"),
2344
N_("pass it through git-mailinfo"),
2345
PARSE_OPT_NONEG, am_option_parse_quoted_cr),
2346
OPT_PASSTHRU_ARGV(0, "whitespace", &state.git_apply_opts, N_("action"),
2347
N_("pass it through git-apply"),
2349
OPT_PASSTHRU_ARGV(0, "ignore-space-change", &state.git_apply_opts, NULL,
2350
N_("pass it through git-apply"),
2352
OPT_PASSTHRU_ARGV(0, "ignore-whitespace", &state.git_apply_opts, NULL,
2353
N_("pass it through git-apply"),
2355
OPT_PASSTHRU_ARGV(0, "directory", &state.git_apply_opts, N_("root"),
2356
N_("pass it through git-apply"),
2358
OPT_PASSTHRU_ARGV(0, "exclude", &state.git_apply_opts, N_("path"),
2359
N_("pass it through git-apply"),
2361
OPT_PASSTHRU_ARGV(0, "include", &state.git_apply_opts, N_("path"),
2362
N_("pass it through git-apply"),
2364
OPT_PASSTHRU_ARGV('C', NULL, &state.git_apply_opts, N_("n"),
2365
N_("pass it through git-apply"),
2367
OPT_PASSTHRU_ARGV('p', NULL, &state.git_apply_opts, N_("num"),
2368
N_("pass it through git-apply"),
2370
OPT_CALLBACK(0, "patch-format", &patch_format, N_("format"),
2371
N_("format the patch(es) are in"),
2372
parse_opt_patchformat),
2373
OPT_PASSTHRU_ARGV(0, "reject", &state.git_apply_opts, NULL,
2374
N_("pass it through git-apply"),
2376
OPT_STRING(0, "resolvemsg", &state.resolvemsg, NULL,
2377
N_("override error message when patch failure occurs")),
2378
OPT_CMDMODE(0, "continue", &resume_mode,
2379
N_("continue applying patches after resolving a conflict"),
2381
OPT_CMDMODE('r', "resolved", &resume_mode,
2382
N_("synonyms for --continue"),
2384
OPT_CMDMODE(0, "skip", &resume_mode,
2385
N_("skip the current patch"),
2387
OPT_CMDMODE(0, "abort", &resume_mode,
2388
N_("restore the original branch and abort the patching operation"),
2390
OPT_CMDMODE(0, "quit", &resume_mode,
2391
N_("abort the patching operation but keep HEAD where it is"),
2393
{ OPTION_CALLBACK, 0, "show-current-patch", &resume_mode,
2395
N_("show the patch being applied"),
2396
PARSE_OPT_CMDMODE | PARSE_OPT_OPTARG | PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
2397
parse_opt_show_current_patch, RESUME_SHOW_PATCH_RAW },
2398
OPT_CMDMODE(0, "retry", &resume_mode,
2399
N_("try to apply current patch again"),
2401
OPT_CMDMODE(0, "allow-empty", &resume_mode,
2402
N_("record the empty patch as an empty commit"),
2403
RESUME_ALLOW_EMPTY),
2404
OPT_BOOL(0, "committer-date-is-author-date",
2405
&state.committer_date_is_author_date,
2406
N_("lie about committer date")),
2407
OPT_BOOL(0, "ignore-date", &state.ignore_date,
2408
N_("use current timestamp for author date")),
2409
OPT_RERERE_AUTOUPDATE(&state.allow_rerere_autoupdate),
2410
{ OPTION_STRING, 'S', "gpg-sign", &state.sign_commit, N_("key-id"),
2411
N_("GPG-sign commits"),
2412
PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
2413
OPT_CALLBACK_F(0, "empty", &state.empty_type, "(stop|drop|keep)",
2414
N_("how to handle empty patches"),
2415
PARSE_OPT_NONEG, am_option_parse_empty),
2416
OPT_HIDDEN_BOOL(0, "rebasing", &state.rebasing,
2417
N_("(internal use for git-rebase)")),
2421
if (argc == 2 && !strcmp(argv[1], "-h"))
2422
usage_with_options(usage, options);
2424
git_config(git_default_config, NULL);
2426
am_state_init(&state);
2428
in_progress = am_in_progress(&state);
2432
argc = parse_options(argc, argv, prefix, options, usage, 0);
2435
fprintf_ln(stderr, _("The -b/--binary option has been a no-op for long time, and\n"
2436
"it will be removed. Please do not use it anymore."));
2439
git_committer_info(IDENT_STRICT);
2441
if (repo_read_index_preload(the_repository, NULL, 0) < 0)
2442
die(_("failed to read the index"));
2456
if (argc || (resume_mode == RESUME_FALSE && !isatty(0)))
2457
die(_("previous rebase directory %s still exists but mbox given."),
2460
if (resume_mode == RESUME_FALSE)
2461
resume_mode = RESUME_APPLY;
2463
if (state.signoff == SIGNOFF_EXPLICIT)
2464
am_append_signoff(&state);
2466
struct strvec paths = STRVEC_INIT;
2474
if (file_exists(state.dir) && !state.rebasing) {
2475
if (resume_mode == RESUME_ABORT || resume_mode == RESUME_QUIT) {
2477
am_state_release(&state);
2481
die(_("Stray %s directory found.\n"
2482
"Use \"git am --abort\" to remove it."),
2487
die(_("Resolve operation not in progress, we are not resuming."));
2489
for (i = 0; i < argc; i++) {
2490
if (is_absolute_path(argv[i]) || !prefix)
2491
strvec_push(&paths, argv[i]);
2493
strvec_push(&paths, mkpath("%s/%s", prefix, argv[i]));
2496
if (state.interactive && !paths.nr)
2497
die(_("interactive mode requires patches on the command line"));
2499
am_setup(&state, patch_format, paths.v, keep_cr);
2501
strvec_clear(&paths);
2504
switch (resume_mode) {
2511
case RESUME_RESOLVED:
2512
case RESUME_ALLOW_EMPTY:
2513
am_resolve(&state, resume_mode == RESUME_ALLOW_EMPTY ? 1 : 0);
2525
case RESUME_SHOW_PATCH_RAW:
2526
case RESUME_SHOW_PATCH_DIFF:
2527
ret = show_patch(&state, resume_mode);
2530
BUG("invalid resume value");
2533
am_state_release(&state);