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