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