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