]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/mailsplit.c
Merge branch 'sb/dco-indentation-fix'
[thirdparty/git.git] / builtin / mailsplit.c
CommitLineData
2744b234
LT
1/*
2 * Totally braindamaged mbox splitter program.
3 *
4 * It just splits a mbox into a list of files: "0001" "0002" ..
5 * so you can process them further from there.
6 */
8b73edf4 7#include "cache.h"
e690e843 8#include "builtin.h"
c455c87c 9#include "string-list.h"
c8f373a5 10#include "strbuf.h"
2744b234 11
8b73edf4 12static const char git_mailsplit_usage[] =
0adda936 13"git mailsplit [-d<prec>] [-f<n>] [-b] [--keep-cr] -o<directory> [(<mbox>|<Maildir>)...]";
2744b234
LT
14
15static int is_from_line(const char *line, int len)
16{
17 const char *colon;
18
19 if (len < 20 || memcmp("From ", line, 5))
20 return 0;
21
22 colon = line + len - 2;
23 line += 5;
24 for (;;) {
25 if (colon < line)
26 return 0;
27 if (*--colon == ':')
28 break;
29 }
30
31 if (!isdigit(colon[-4]) ||
32 !isdigit(colon[-2]) ||
33 !isdigit(colon[-1]) ||
34 !isdigit(colon[ 1]) ||
35 !isdigit(colon[ 2]))
36 return 0;
37
38 /* year */
39 if (strtol(colon+3, NULL, 10) <= 90)
40 return 0;
41
42 /* Ok, close enough */
43 return 1;
44}
45
c8f373a5 46static struct strbuf buf = STRBUF_INIT;
c2ca1d79 47static int keep_cr;
8b73edf4
JH
48
49/* Called with the first line (potentially partial)
50 * already in buf[] -- normally that should begin with
51 * the Unix "From " line. Write it into the specified
52 * file.
53 */
b3f041fb 54static int split_one(FILE *mbox, const char *name, int allow_bare)
2744b234 55{
13b08125 56 FILE *output;
8b73edf4
JH
57 int fd;
58 int status = 0;
c8f373a5 59 int is_bare = !is_from_line(buf.buf, buf.len);
2744b234 60
13b08125 61 if (is_bare && !allow_bare) {
13b08125
SB
62 fprintf(stderr, "corrupt mailbox\n");
63 exit(1);
64 }
8b73edf4
JH
65 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
66 if (fd < 0)
0721c314 67 die_errno("cannot open output file '%s'", name);
41698375 68 output = xfdopen(fd, "w");
2744b234 69
8b73edf4
JH
70 /* Copy it out, while searching for a line that begins with
71 * "From " and having something that looks like a date format.
2744b234 72 */
8b73edf4 73 for (;;) {
c2ca1d79
JH
74 if (!keep_cr && buf.len > 1 && buf.buf[buf.len-1] == '\n' &&
75 buf.buf[buf.len-2] == '\r') {
76 strbuf_setlen(&buf, buf.len-2);
77 strbuf_addch(&buf, '\n');
78 }
79
c8f373a5 80 if (fwrite(buf.buf, 1, buf.len, output) != buf.len)
0721c314 81 die_errno("cannot write output");
8b73edf4 82
c8f373a5 83 if (strbuf_getwholeline(&buf, mbox, '\n')) {
8b73edf4
JH
84 if (feof(mbox)) {
85 status = 1;
86 break;
87 }
0721c314 88 die_errno("cannot read mbox");
8b73edf4 89 }
c8f373a5 90 if (!is_bare && is_from_line(buf.buf, buf.len))
8b73edf4
JH
91 break; /* done with one message */
92 }
93 fclose(output);
94 return status;
2744b234
LT
95}
96
c455c87c 97static int populate_maildir_list(struct string_list *list, const char *path)
2744b234 98{
d63bd9a2
FP
99 DIR *dir;
100 struct dirent *dent;
d50a4bc4
GP
101 char name[PATH_MAX];
102 char *subs[] = { "cur", "new", NULL };
103 char **sub;
104
105 for (sub = subs; *sub; ++sub) {
106 snprintf(name, sizeof(name), "%s/%s", path, *sub);
107 if ((dir = opendir(name)) == NULL) {
108 if (errno == ENOENT)
109 continue;
110 error("cannot opendir %s (%s)", name, strerror(errno));
111 return -1;
112 }
d63bd9a2 113
d50a4bc4
GP
114 while ((dent = readdir(dir)) != NULL) {
115 if (dent->d_name[0] == '.')
116 continue;
117 snprintf(name, sizeof(name), "%s/%s", *sub, dent->d_name);
78a395d3 118 string_list_insert(list, name);
d50a4bc4 119 }
d63bd9a2 120
d50a4bc4 121 closedir(dir);
d63bd9a2
FP
122 }
123
d63bd9a2
FP
124 return 0;
125}
126
18505c34
JK
127static int maildir_filename_cmp(const char *a, const char *b)
128{
129 while (*a && *b) {
130 if (isdigit(*a) && isdigit(*b)) {
131 long int na, nb;
132 na = strtol(a, (char **)&a, 10);
133 nb = strtol(b, (char **)&b, 10);
134 if (na != nb)
135 return na - nb;
136 /* strtol advanced our pointers */
137 }
138 else {
139 if (*a != *b)
140 return (unsigned char)*a - (unsigned char)*b;
141 a++;
142 b++;
143 }
144 }
145 return (unsigned char)*a - (unsigned char)*b;
146}
147
d63bd9a2
FP
148static int split_maildir(const char *maildir, const char *dir,
149 int nr_prec, int skip)
150{
151 char file[PATH_MAX];
d63bd9a2 152 char name[PATH_MAX];
e690e843 153 int ret = -1;
d63bd9a2 154 int i;
183113a5 155 struct string_list list = STRING_LIST_INIT_DUP;
e690e843 156
18505c34
JK
157 list.cmp = maildir_filename_cmp;
158
d50a4bc4 159 if (populate_maildir_list(&list, maildir) < 0)
d63bd9a2 160 goto out;
e690e843 161
d63bd9a2
FP
162 for (i = 0; i < list.nr; i++) {
163 FILE *f;
c455c87c 164 snprintf(file, sizeof(file), "%s/%s", maildir, list.items[i].string);
d63bd9a2
FP
165 f = fopen(file, "r");
166 if (!f) {
167 error("cannot open mail %s (%s)", file, strerror(errno));
e690e843
LS
168 goto out;
169 }
170
c8f373a5 171 if (strbuf_getwholeline(&buf, f, '\n')) {
d63bd9a2 172 error("cannot read mail %s (%s)", file, strerror(errno));
e690e843
LS
173 goto out;
174 }
175
d63bd9a2
FP
176 sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
177 split_one(f, name, 1);
178
179 fclose(f);
180 }
181
d63bd9a2
FP
182 ret = skip;
183out:
c455c87c 184 string_list_clear(&list, 1);
d63bd9a2
FP
185 return ret;
186}
187
fcd056a6
JH
188static int split_mbox(const char *file, const char *dir, int allow_bare,
189 int nr_prec, int skip)
d63bd9a2
FP
190{
191 char name[PATH_MAX];
192 int ret = -1;
f88a545a 193 int peek;
d63bd9a2
FP
194
195 FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
196 int file_done = 0;
197
198 if (!f) {
199 error("cannot open mbox %s", file);
200 goto out;
201 }
202
f88a545a
SS
203 do {
204 peek = fgetc(f);
205 } while (isspace(peek));
206 ungetc(peek, f);
207
c8f373a5 208 if (strbuf_getwholeline(&buf, f, '\n')) {
d63bd9a2
FP
209 /* empty stdin is OK */
210 if (f != stdin) {
211 error("cannot read mbox %s", file);
212 goto out;
e690e843 213 }
d63bd9a2
FP
214 file_done = 1;
215 }
e690e843 216
d63bd9a2
FP
217 while (!file_done) {
218 sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
219 file_done = split_one(f, name, allow_bare);
e690e843 220 }
d63bd9a2
FP
221
222 if (f != stdin)
223 fclose(f);
224
e690e843
LS
225 ret = skip;
226out:
e690e843
LS
227 return ret;
228}
d63bd9a2 229
a633fca0 230int cmd_mailsplit(int argc, const char **argv, const char *prefix)
e690e843 231{
d63bd9a2 232 int nr = 0, nr_prec = 4, num = 0;
b3f041fb
PA
233 int allow_bare = 0;
234 const char *dir = NULL;
235 const char **argp;
236 static const char *stdin_only[] = { "-", NULL };
8b73edf4 237
b3f041fb
PA
238 for (argp = argv+1; *argp; argp++) {
239 const char *arg = *argp;
8b73edf4
JH
240
241 if (arg[0] != '-')
242 break;
243 /* do flags here */
b3f041fb
PA
244 if ( arg[1] == 'd' ) {
245 nr_prec = strtol(arg+2, NULL, 10);
8b73edf4
JH
246 if (nr_prec < 3 || 10 <= nr_prec)
247 usage(git_mailsplit_usage);
248 continue;
b3f041fb
PA
249 } else if ( arg[1] == 'f' ) {
250 nr = strtol(arg+2, NULL, 10);
aa481d38
JN
251 } else if ( arg[1] == 'h' ) {
252 usage(git_mailsplit_usage);
b3f041fb
PA
253 } else if ( arg[1] == 'b' && !arg[2] ) {
254 allow_bare = 1;
c2ca1d79
JH
255 } else if (!strcmp(arg, "--keep-cr")) {
256 keep_cr = 1;
b3f041fb
PA
257 } else if ( arg[1] == 'o' && arg[2] ) {
258 dir = arg+2;
259 } else if ( arg[1] == '-' && !arg[2] ) {
260 argp++; /* -- marks end of options */
261 break;
262 } else {
263 die("unknown option: %s", arg);
8b73edf4 264 }
2744b234 265 }
8b73edf4 266
b3f041fb
PA
267 if ( !dir ) {
268 /* Backwards compatibility: if no -o specified, accept
269 <mbox> <dir> or just <dir> */
270 switch (argc - (argp-argv)) {
271 case 1:
272 dir = argp[0];
273 argp = stdin_only;
274 break;
275 case 2:
276 stdin_only[0] = argp[0];
277 dir = argp[1];
278 argp = stdin_only;
279 break;
280 default:
281 usage(git_mailsplit_usage);
282 }
283 } else {
284 /* New usage: if no more argument, parse stdin */
285 if ( !*argp )
286 argp = stdin_only;
2744b234 287 }
8b73edf4 288
d63bd9a2
FP
289 while (*argp) {
290 const char *arg = *argp++;
291 struct stat argstat;
292 int ret = 0;
293
294 if (arg[0] == '-' && arg[1] == 0) {
295 ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
296 if (ret < 0) {
297 error("cannot split patches from stdin");
298 return 1;
299 }
b3327180
JH
300 num += (ret - nr);
301 nr = ret;
d63bd9a2
FP
302 continue;
303 }
304
305 if (stat(arg, &argstat) == -1) {
306 error("cannot stat %s (%s)", arg, strerror(errno));
307 return 1;
308 }
309
310 if (S_ISDIR(argstat.st_mode))
311 ret = split_maildir(arg, dir, nr_prec, nr);
312 else
313 ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
314
315 if (ret < 0) {
316 error("cannot split patches from %s", arg);
317 return 1;
318 }
b3327180
JH
319 num += (ret - nr);
320 nr = ret;
d63bd9a2
FP
321 }
322
323 printf("%d\n", num);
b3f041fb 324
d63bd9a2 325 return 0;
2744b234 326}