4 * Based on git-am.sh by Junio C Hamano.
9 #include "parse-options.h"
11 #include "run-command.h"
14 * Like strbuf_getline(), but treats both '\n' and "\r\n" as line terminators.
16 static int strbuf_getline_crlf(struct strbuf
*sb
, FILE *fp
)
18 if (strbuf_getwholeline(sb
, fp
, '\n'))
20 if (sb
->buf
[sb
->len
- 1] == '\n') {
21 strbuf_setlen(sb
, sb
->len
- 1);
22 if (sb
->len
> 0 && sb
->buf
[sb
->len
- 1] == '\r')
23 strbuf_setlen(sb
, sb
->len
- 1);
29 PATCH_FORMAT_UNKNOWN
= 0,
34 /* state directory path */
37 /* current and last patch numbers, 1-indexed */
41 /* number of digits in patch filename */
46 * Initializes am_state with the default values. The state directory is set to
49 static void am_state_init(struct am_state
*state
, const char *dir
)
51 memset(state
, 0, sizeof(*state
));
54 state
->dir
= xstrdup(dir
);
60 * Releases memory allocated by an am_state.
62 static void am_state_release(struct am_state
*state
)
68 * Returns path relative to the am_state directory.
70 static inline const char *am_path(const struct am_state
*state
, const char *path
)
72 return mkpath("%s/%s", state
->dir
, path
);
76 * Returns 1 if there is an am session in progress, 0 otherwise.
78 static int am_in_progress(const struct am_state
*state
)
82 if (lstat(state
->dir
, &st
) < 0 || !S_ISDIR(st
.st_mode
))
84 if (lstat(am_path(state
, "last"), &st
) || !S_ISREG(st
.st_mode
))
86 if (lstat(am_path(state
, "next"), &st
) || !S_ISREG(st
.st_mode
))
92 * Reads the contents of `file` in the `state` directory into `sb`. Returns the
93 * number of bytes read on success, -1 if the file does not exist. If `trim` is
94 * set, trailing whitespace will be removed.
96 static int read_state_file(struct strbuf
*sb
, const struct am_state
*state
,
97 const char *file
, int trim
)
101 if (strbuf_read_file(sb
, am_path(state
, file
), 0) >= 0) {
111 die_errno(_("could not read '%s'"), am_path(state
, file
));
115 * Loads state from disk.
117 static void am_load(struct am_state
*state
)
119 struct strbuf sb
= STRBUF_INIT
;
121 if (read_state_file(&sb
, state
, "next", 1) < 0)
122 die("BUG: state file 'next' does not exist");
123 state
->cur
= strtol(sb
.buf
, NULL
, 10);
125 if (read_state_file(&sb
, state
, "last", 1) < 0)
126 die("BUG: state file 'last' does not exist");
127 state
->last
= strtol(sb
.buf
, NULL
, 10);
133 * Removes the am_state directory, forcefully terminating the current am
136 static void am_destroy(const struct am_state
*state
)
138 struct strbuf sb
= STRBUF_INIT
;
140 strbuf_addstr(&sb
, state
->dir
);
141 remove_dir_recursively(&sb
, 0);
146 * Determines if the file looks like a piece of RFC2822 mail by grabbing all
147 * non-indented lines and checking if they look like they begin with valid
148 * header field names.
150 * Returns 1 if the file looks like a piece of mail, 0 otherwise.
152 static int is_mail(FILE *fp
)
154 const char *header_regex
= "^[!-9;-~]+:";
155 struct strbuf sb
= STRBUF_INIT
;
159 if (fseek(fp
, 0L, SEEK_SET
))
160 die_errno(_("fseek failed"));
162 if (regcomp(®ex
, header_regex
, REG_NOSUB
| REG_EXTENDED
))
163 die("invalid pattern: %s", header_regex
);
165 while (!strbuf_getline_crlf(&sb
, fp
)) {
167 break; /* End of header */
169 /* Ignore indented folded lines */
170 if (*sb
.buf
== '\t' || *sb
.buf
== ' ')
173 /* It's a header if it matches header_regex */
174 if (regexec(®ex
, sb
.buf
, 0, NULL
, 0)) {
187 * Attempts to detect the patch_format of the patches contained in `paths`,
188 * returning the PATCH_FORMAT_* enum value. Returns PATCH_FORMAT_UNKNOWN if
191 static int detect_patch_format(const char **paths
)
193 enum patch_format ret
= PATCH_FORMAT_UNKNOWN
;
194 struct strbuf l1
= STRBUF_INIT
;
198 * We default to mbox format if input is from stdin and for directories
200 if (!*paths
|| !strcmp(*paths
, "-") || is_directory(*paths
))
201 return PATCH_FORMAT_MBOX
;
204 * Otherwise, check the first few lines of the first patch, starting
205 * from the first non-blank line, to try to detect its format.
208 fp
= xfopen(*paths
, "r");
210 while (!strbuf_getline_crlf(&l1
, fp
)) {
215 if (starts_with(l1
.buf
, "From ") || starts_with(l1
.buf
, "From: ")) {
216 ret
= PATCH_FORMAT_MBOX
;
220 if (l1
.len
&& is_mail(fp
)) {
221 ret
= PATCH_FORMAT_MBOX
;
232 * Splits out individual email patches from `paths`, where each path is either
233 * a mbox file or a Maildir. Returns 0 on success, -1 on failure.
235 static int split_mail_mbox(struct am_state
*state
, const char **paths
)
237 struct child_process cp
= CHILD_PROCESS_INIT
;
238 struct strbuf last
= STRBUF_INIT
;
241 argv_array_push(&cp
.args
, "mailsplit");
242 argv_array_pushf(&cp
.args
, "-d%d", state
->prec
);
243 argv_array_pushf(&cp
.args
, "-o%s", state
->dir
);
244 argv_array_push(&cp
.args
, "-b");
245 argv_array_push(&cp
.args
, "--");
246 argv_array_pushv(&cp
.args
, paths
);
248 if (capture_command(&cp
, &last
, 8))
252 state
->last
= strtol(last
.buf
, NULL
, 10);
258 * Splits a list of files/directories into individual email patches. Each path
259 * in `paths` must be a file/directory that is formatted according to
262 * Once split out, the individual email patches will be stored in the state
263 * directory, with each patch's filename being its index, padded to state->prec
266 * state->cur will be set to the index of the first mail, and state->last will
267 * be set to the index of the last mail.
269 * Returns 0 on success, -1 on failure.
271 static int split_mail(struct am_state
*state
, enum patch_format patch_format
,
274 switch (patch_format
) {
275 case PATCH_FORMAT_MBOX
:
276 return split_mail_mbox(state
, paths
);
278 die("BUG: invalid patch_format");
284 * Setup a new am session for applying patches
286 static void am_setup(struct am_state
*state
, enum patch_format patch_format
,
290 patch_format
= detect_patch_format(paths
);
293 fprintf_ln(stderr
, _("Patch format detection failed."));
297 if (mkdir(state
->dir
, 0777) < 0 && errno
!= EEXIST
)
298 die_errno(_("failed to create directory '%s'"), state
->dir
);
300 if (split_mail(state
, patch_format
, paths
) < 0) {
302 die(_("Failed to split patches."));
306 * NOTE: Since the "next" and "last" files determine if an am_state
307 * session is in progress, they should be written last.
310 write_file(am_path(state
, "next"), 1, "%d", state
->cur
);
312 write_file(am_path(state
, "last"), 1, "%d", state
->last
);
316 * Increments the patch pointer, and cleans am_state for the application of the
319 static void am_next(struct am_state
*state
)
322 write_file(am_path(state
, "next"), 1, "%d", state
->cur
);
326 * Applies all queued mail.
328 static void am_run(struct am_state
*state
)
330 while (state
->cur
<= state
->last
) {
332 /* NEEDSWORK: Patch application not implemented yet */
341 * parse_options() callback that validates and sets opt->value to the
342 * PATCH_FORMAT_* enum value corresponding to `arg`.
344 static int parse_opt_patchformat(const struct option
*opt
, const char *arg
, int unset
)
346 int *opt_value
= opt
->value
;
348 if (!strcmp(arg
, "mbox"))
349 *opt_value
= PATCH_FORMAT_MBOX
;
351 return error(_("Invalid value for --patch-format: %s"), arg
);
355 int cmd_am(int argc
, const char **argv
, const char *prefix
)
357 struct am_state state
;
358 int patch_format
= PATCH_FORMAT_UNKNOWN
;
360 const char * const usage
[] = {
361 N_("git am [options] [(<mbox>|<Maildir>)...]"),
365 struct option options
[] = {
366 OPT_CALLBACK(0, "patch-format", &patch_format
, N_("format"),
367 N_("format the patch(es) are in"),
368 parse_opt_patchformat
),
373 * NEEDSWORK: Once all the features of git-am.sh have been
374 * re-implemented in builtin/am.c, this preamble can be removed.
376 if (!getenv("_GIT_USE_BUILTIN_AM")) {
377 const char *path
= mkpath("%s/git-am", git_exec_path());
379 if (sane_execvp(path
, (char **)argv
) < 0)
380 die_errno("could not exec %s", path
);
382 prefix
= setup_git_directory();
383 trace_repo_setup(prefix
);
387 git_config(git_default_config
, NULL
);
389 am_state_init(&state
, git_path("rebase-apply"));
391 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
393 if (am_in_progress(&state
))
396 struct argv_array paths
= ARGV_ARRAY_INIT
;
399 for (i
= 0; i
< argc
; i
++) {
400 if (is_absolute_path(argv
[i
]) || !prefix
)
401 argv_array_push(&paths
, argv
[i
]);
403 argv_array_push(&paths
, mkpath("%s/%s", prefix
, argv
[i
]));
406 am_setup(&state
, patch_format
, paths
.argv
);
408 argv_array_clear(&paths
);
413 am_state_release(&state
);