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

git-am support for naked email messages (take 2)

This allows git-am to accept single-message files as well as mboxes. Unlike the previous version, this one doesn't need to be explicitly told which one it is; rather, it looks to see if the first line is a From line and uses it to select mbox mode or not. I moved the logic to do all this into git-mailsplit, which got a new user interface as result, although the old interface is still available for backwards compatibility. [jc: applied with two obvious fixes.] Signed-off-by: H. Peter Anvin <hpa@zytor.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

H. Peter Anvin committed Dec 13, 2005 at 22:39 UTC b3f041fb0f7de167dbb6711b0a231d36c4b5de08
3 files changed +75 -42
Documentation/git-mailsplit.txt
+8 -1
@@ -7,7 +7,7 @@ git-mailsplit - Totally braindamaged mbox splitter program.
7
8 SYNOPSIS
9 --------
10 -'git-mailsplit' [-d<prec>] [<mbox>] <directory>
10 +'git-mailsplit' [-b] [-f<nn>] [-d<prec>] -o<directory> [--] [<mbox>...]
11
12 DESCRIPTION
13 -----------
@@ -23,11 +23,18 @@ OPTIONS
23 <directory>::
24 Directory in which to place the individual messages.
25
26 +-b::
27 + If any file doesn't begin with a From line, assume it is a
28 + single mail message instead of signalling error.
29 +
30 -d<prec>::
31 Instead of the default 4 digits with leading zeros,
32 different precision can be specified for the generated
33 filenames.
34
35 +-f<nn>::
36 + Skip the first <nn> numbers, for example if -f3 is specified,
37 + start the numbering with 0004.
38
39 Author
40 ------
git-am.sh
+1 -4
@@ -164,10 +164,7 @@ else
164 # Start afresh.
165 mkdir -p "$dotest" || exit
166
167 - # cat does the right thing for us, including '-' to mean
168 - # standard input.
169 - cat "$@" |
170 - git-mailsplit -d$prec "$dotest/" >"$dotest/last" || {
167 + git-mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
168 rm -fr "$dotest"
169 exit 1
170 }
mailsplit.c
+66 -37
@@ -15,7 +15,7 @@
15 #include "cache.h"
16
17 static const char git_mailsplit_usage[] =
18 -"git-mailsplit [-d<prec>] [<mbox>] <directory>";
18 +"git-mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> <mbox>...";
19
20 static int is_from_line(const char *line, int len)
21 {
@@ -56,14 +56,15 @@ static char buf[4096];
56 * the Unix "From " line. Write it into the specified
57 * file.
58 */
59 -static int split_one(FILE *mbox, const char *name)
59 +static int split_one(FILE *mbox, const char *name, int allow_bare)
60 {
61 FILE *output = NULL;
62 int len = strlen(buf);
63 int fd;
64 int status = 0;
65 + int is_bare = !is_from_line(buf, len);
66
66 - if (!is_from_line(buf, len))
67 + if (is_bare && !allow_bare)
68 goto corrupt;
69
70 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
@@ -88,7 +89,7 @@ static int split_one(FILE *mbox, const char *name)
89 die("cannot read mbox");
90 }
91 len = strlen(buf);
91 - if (!is_partial && is_from_line(buf, len))
92 + if (!is_partial && !is_bare && is_from_line(buf, len))
93 break; /* done with one message */
94 }
95 fclose(output);
@@ -104,54 +105,82 @@ static int split_one(FILE *mbox, const char *name)
105
106 int main(int argc, const char **argv)
107 {
107 - int i, nr, nr_prec = 4;
108 - FILE *mbox = NULL;
108 + int nr = 0, nr_prec = 4;
109 + int allow_bare = 0;
110 + const char *dir = NULL;
111 + const char **argp;
112 + static const char *stdin_only[] = { "-", NULL };
113 + char *name;
114
110 - for (i = 1; i < argc; i++) {
111 - const char *arg = argv[i];
115 + for (argp = argv+1; *argp; argp++) {
116 + const char *arg = *argp;
117
118 if (arg[0] != '-')
119 break;
120 /* do flags here */
116 - if (!strncmp(arg, "-d", 2)) {
117 - nr_prec = strtol(arg + 2, NULL, 10);
121 + if ( arg[1] == 'd' ) {
122 + nr_prec = strtol(arg+2, NULL, 10);
123 if (nr_prec < 3 || 10 <= nr_prec)
124 usage(git_mailsplit_usage);
125 continue;
126 + } else if ( arg[1] == 'f' ) {
127 + nr = strtol(arg+2, NULL, 10);
128 + } else if ( arg[1] == 'b' && !arg[2] ) {
129 + allow_bare = 1;
130 + } else if ( arg[1] == 'o' && arg[2] ) {
131 + dir = arg+2;
132 + } else if ( arg[1] == '-' && !arg[2] ) {
133 + argp++; /* -- marks end of options */
134 + break;
135 + } else {
136 + die("unknown option: %s", arg);
137 }
138 }
139
124 - /* Either one remaining arg (dir), or two (mbox and dir) */
125 - switch (argc - i) {
126 - case 1:
127 - mbox = stdin;
128 - break;
129 - case 2:
130 - if ((mbox = fopen(argv[i], "r")) == NULL)
131 - die("cannot open mbox %s for reading", argv[i]);
132 - break;
133 - default:
134 - usage(git_mailsplit_usage);
140 + if ( !dir ) {
141 + /* Backwards compatibility: if no -o specified, accept
142 + <mbox> <dir> or just <dir> */
143 + switch (argc - (argp-argv)) {
144 + case 1:
145 + dir = argp[0];
146 + argp = stdin_only;
147 + break;
148 + case 2:
149 + stdin_only[0] = argp[0];
150 + dir = argp[1];
151 + argp = stdin_only;
152 + break;
153 + default:
154 + usage(git_mailsplit_usage);
155 + }
156 + } else {
157 + /* New usage: if no more argument, parse stdin */
158 + if ( !*argp )
159 + argp = stdin_only;
160 }
136 - if (chdir(argv[argc - 1]) < 0)
137 - usage(git_mailsplit_usage);
161
139 - nr = 0;
140 - if (fgets(buf, sizeof(buf), mbox) == NULL)
141 - die("cannot read mbox");
162 + name = xmalloc(strlen(dir) + 2 + 3 * sizeof(nr));
163
143 - for (;;) {
144 - char name[10];
164 + while (*argp) {
165 + const char *file = *argp++;
166 + FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "rt");
167 + int file_done = 0;
168
146 - sprintf(name, "%0*d", nr_prec, ++nr);
147 - switch (split_one(mbox, name)) {
148 - case 0:
149 - break;
150 - case 1:
151 - printf("%d\n", nr);
152 - return 0;
153 - default:
154 - exit(1);
169 + if ( !f )
170 + die ("cannot open mbox %s", file);
171 +
172 + if (fgets(buf, sizeof(buf), f) == NULL)
173 + die("cannot read mbox %s", file);
174 +
175 + while (!file_done) {
176 + sprintf(name, "%s/%0*d", dir, nr_prec, ++nr);
177 + file_done = split_one(f, name, allow_bare);
178 }
179 +
180 + if (f != stdin)
181 + fclose(f);
182 }
183 +
184 + printf("%d\n", nr);
185 + return 0;
186 }