@matheusbsbs / matheus-bagliano / commits / d63bd9a217
Fork 0

Teach mailsplit about Maildir's

Signed-off-by: Fernando J. Pereda <ferdy@gentoo.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Fernando J. Pereda committed May 25, 2007 at 00:15 UTC d63bd9a217c0f9284e4195495dc0d078f5e6577e
4 files changed +133 -30
Documentation/git-am.txt
+5 -3
@@ -12,7 +12,8 @@ SYNOPSIS
12 'git-am' [--signoff] [--dotest=<dir>] [--keep] [--utf8 | --no-utf8]
13 [--3way] [--interactive] [--binary]
14 [--whitespace=<option>] [-C<n>] [-p<n>]
15 - <mbox>...
15 + <mbox>|<Maildir>...
16 +
17 'git-am' [--skip | --resolved]
18
19 DESCRIPTION
@@ -23,9 +24,10 @@ current branch.
24
25 OPTIONS
26 -------
26 -<mbox>...::
27 +<mbox>|<Maildir>...::
28 The list of mailbox files to read patches from. If you do not
28 - supply this argument, reads from the standard input.
29 + supply this argument, reads from the standard input. If you supply
30 + directories, they'll be treated as Maildirs.
31
32 -s, --signoff::
33 Add `Signed-off-by:` line to the commit message, using
Documentation/git-mailsplit.txt
+10 -3
@@ -7,12 +7,15 @@ git-mailsplit - Simple UNIX mbox splitter program
7
8 SYNOPSIS
9 --------
10 -'git-mailsplit' [-b] [-f<nn>] [-d<prec>] -o<directory> [--] [<mbox>...]
10 +'git-mailsplit' [-b] [-f<nn>] [-d<prec>] -o<directory> [--] [<mbox>|<Maildir>...]
11
12 DESCRIPTION
13 -----------
14 -Splits a mbox file into a list of files: "0001" "0002" .. in the specified
15 -directory so you can process them further from there.
14 +Splits a mbox file or a Maildir into a list of files: "0001" "0002" .. in the
15 +specified directory so you can process them further from there.
16 +
17 +IMPORTANT: Maildir splitting relies upon filenames being sorted to output
18 +patches in the correct order.
19
20 OPTIONS
21 -------
@@ -20,6 +23,10 @@ OPTIONS
23 Mbox file to split. If not given, the mbox is read from
24 the standard input.
25
26 +<Maildir>::
27 + Root of the Maildir to split. This directory should contain the cur, tmp
28 + and new subdirectories.
29 +
30 <directory>::
31 Directory in which to place the individual messages.
32
builtin-mailsplit.c
+117 -23
@@ -6,9 +6,10 @@
6 */
7 #include "cache.h"
8 #include "builtin.h"
9 +#include "path-list.h"
10
11 static const char git_mailsplit_usage[] =
11 -"git-mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> <mbox>...";
12 +"git-mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> <mbox>|<Maildir>...";
13
14 static int is_from_line(const char *line, int len)
15 {
@@ -96,44 +97,107 @@ static int split_one(FILE *mbox, const char *name, int allow_bare)
97 exit(1);
98 }
99
99 -int split_mbox(const char **mbox, const char *dir, int allow_bare, int nr_prec, int skip)
100 +static int populate_maildir_list(struct path_list *list, const char *path)
101 {
101 - char *name = xmalloc(strlen(dir) + 2 + 3 * sizeof(skip));
102 + DIR *dir;
103 + struct dirent *dent;
104 +
105 + if ((dir = opendir(path)) == NULL) {
106 + error("cannot opendir %s (%s)", path, strerror(errno));
107 + return -1;
108 + }
109 +
110 + while ((dent = readdir(dir)) != NULL) {
111 + if (dent->d_name[0] == '.')
112 + continue;
113 + path_list_insert(dent->d_name, list);
114 + }
115 +
116 + closedir(dir);
117 +
118 + return 0;
119 +}
120 +
121 +static int split_maildir(const char *maildir, const char *dir,
122 + int nr_prec, int skip)
123 +{
124 + char file[PATH_MAX];
125 + char curdir[PATH_MAX];
126 + char name[PATH_MAX];
127 int ret = -1;
128 + int i;
129 + struct path_list list = {NULL, 0, 0, 1};
130
104 - while (*mbox) {
105 - const char *file = *mbox++;
106 - FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
107 - int file_done = 0;
131 + snprintf(curdir, sizeof(curdir), "%s/cur", maildir);
132 + if (populate_maildir_list(&list, curdir) < 0)
133 + goto out;
134
109 - if ( !f ) {
110 - error("cannot open mbox %s", file);
135 + for (i = 0; i < list.nr; i++) {
136 + FILE *f;
137 + snprintf(file, sizeof(file), "%s/%s", curdir, list.items[i].path);
138 + f = fopen(file, "r");
139 + if (!f) {
140 + error("cannot open mail %s (%s)", file, strerror(errno));
141 goto out;
142 }
143
144 if (fgets(buf, sizeof(buf), f) == NULL) {
115 - if (f == stdin)
116 - break; /* empty stdin is OK */
117 - error("cannot read mbox %s", file);
145 + error("cannot read mail %s (%s)", file, strerror(errno));
146 goto out;
147 }
148
121 - while (!file_done) {
122 - sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
123 - file_done = split_one(f, name, allow_bare);
149 + sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
150 + split_one(f, name, 1);
151 +
152 + fclose(f);
153 + }
154 +
155 + path_list_clear(&list, 1);
156 +
157 + ret = skip;
158 +out:
159 + return ret;
160 +}
161 +
162 +int split_mbox(const char *file, const char *dir, int allow_bare,
163 + int nr_prec, int skip)
164 +{
165 + char name[PATH_MAX];
166 + int ret = -1;
167 +
168 + FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
169 + int file_done = 0;
170 +
171 + if (!f) {
172 + error("cannot open mbox %s", file);
173 + goto out;
174 + }
175 +
176 + if (fgets(buf, sizeof(buf), f) == NULL) {
177 + /* empty stdin is OK */
178 + if (f != stdin) {
179 + error("cannot read mbox %s", file);
180 + goto out;
181 }
182 + file_done = 1;
183 + }
184
126 - if (f != stdin)
127 - fclose(f);
185 + while (!file_done) {
186 + sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
187 + file_done = split_one(f, name, allow_bare);
188 }
189 +
190 + if (f != stdin)
191 + fclose(f);
192 +
193 ret = skip;
194 out:
131 - free(name);
195 return ret;
196 }
197 +
198 int cmd_mailsplit(int argc, const char **argv, const char *prefix)
199 {
136 - int nr = 0, nr_prec = 4, ret;
200 + int nr = 0, nr_prec = 4, num = 0;
201 int allow_bare = 0;
202 const char *dir = NULL;
203 const char **argp;
@@ -186,9 +250,39 @@ int cmd_mailsplit(int argc, const char **argv, const char *prefix)
250 argp = stdin_only;
251 }
252
189 - ret = split_mbox(argp, dir, allow_bare, nr_prec, nr);
190 - if (ret != -1)
191 - printf("%d\n", ret);
253 + while (*argp) {
254 + const char *arg = *argp++;
255 + struct stat argstat;
256 + int ret = 0;
257 +
258 + if (arg[0] == '-' && arg[1] == 0) {
259 + ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
260 + if (ret < 0) {
261 + error("cannot split patches from stdin");
262 + return 1;
263 + }
264 + num += ret;
265 + continue;
266 + }
267 +
268 + if (stat(arg, &argstat) == -1) {
269 + error("cannot stat %s (%s)", arg, strerror(errno));
270 + return 1;
271 + }
272 +
273 + if (S_ISDIR(argstat.st_mode))
274 + ret = split_maildir(arg, dir, nr_prec, nr);
275 + else
276 + ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
277 +
278 + if (ret < 0) {
279 + error("cannot split patches from %s", arg);
280 + return 1;
281 + }
282 + num += ret;
283 + }
284 +
285 + printf("%d\n", num);
286
193 - return ret == -1;
287 + return 0;
288 }
builtin.h
+1 -1
@@ -8,7 +8,7 @@ extern const char git_usage_string[];
8
9 extern void help_unknown_cmd(const char *cmd);
10 extern int mailinfo(FILE *in, FILE *out, int ks, const char *encoding, const char *msg, const char *patch);
11 -extern int split_mbox(const char **mbox, const char *dir, int allow_bare, int nr_prec, int skip);
11 +extern int split_mbox(const char *file, const char *dir, int allow_bare, int nr_prec, int skip);
12 extern void stripspace(FILE *in, FILE *out);
13 extern int write_tree(unsigned char *sha1, int missing_ok, const char *prefix);
14 extern void prune_packed_objects(int);