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

Make git-mailsplit a builtin

Signed-off-by: Lukas Sandström <lukass@etek.chalmers.se> Signed-off-by: Junio C Hamano <junkio@cox.net>

Lukas Sandström committed Jun 13, 2006 at 22:21 UTC e690e84315de1bb806eb2292f8a87cbee30b75ab
4 files changed +48 -33
Makefile
+2 -3
@@ -144,7 +144,6 @@ SCRIPTS = $(patsubst %.sh,%,$(SCRIPT_SH)) \
144
145 # The ones that do not have to link with lcrypto, lz nor xdiff.
146 SIMPLE_PROGRAMS = \
147 - git-mailsplit$X \
147 git-stripspace$X git-daemon$X
148
149 # ... and all the rest that could be moved out of bindir to gitexecdir
@@ -165,7 +164,7 @@ PROGRAMS = \
164 git-describe$X git-merge-tree$X git-blame$X git-imap-send$X
165
166 BUILT_INS = git-log$X git-whatchanged$X git-show$X \
168 - git-count-objects$X git-diff$X git-push$X \
167 + git-count-objects$X git-diff$X git-push$X git-mailsplit$X \
168 git-grep$X git-add$X git-rm$X git-rev-list$X \
169 git-check-ref-format$X git-rev-parse$X \
170 git-init-db$X git-tar-tree$X git-upload-tar$X git-format-patch$X \
@@ -227,7 +226,7 @@ BUILTIN_OBJS = \
226 builtin-read-tree.o builtin-commit-tree.o \
227 builtin-apply.o builtin-show-branch.o builtin-diff-files.o \
228 builtin-diff-index.o builtin-diff-stages.o builtin-diff-tree.o \
230 - builtin-cat-file.o
229 + builtin-cat-file.o builtin-mailsplit.o
230
231 GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
232 LIBS = $(GITLIBS) -lz
builtin-mailsplit.c renamed
+42 -29
@@ -12,6 +12,7 @@
12 #include <string.h>
13 #include <stdio.h>
14 #include "cache.h"
15 +#include "builtin.h"
16
17 static const char git_mailsplit_usage[] =
18 "git-mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> <mbox>...";
@@ -102,14 +103,48 @@ static int split_one(FILE *mbox, const char *name, int allow_bare)
103 exit(1);
104 }
105
105 -int main(int argc, const char **argv)
106 +int split_mbox(const char **mbox, const char *dir, int allow_bare, int nr_prec, int skip)
107 {
107 - int nr = 0, nr_prec = 4;
108 + char *name = xmalloc(strlen(dir) + 2 + 3 * sizeof(skip));
109 + int ret = -1;
110 +
111 + while (*mbox) {
112 + const char *file = *mbox++;
113 + FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
114 + int file_done = 0;
115 +
116 + if ( !f ) {
117 + error("cannot open mbox %s", file);
118 + goto out;
119 + }
120 +
121 + if (fgets(buf, sizeof(buf), f) == NULL) {
122 + if (f == stdin)
123 + break; /* empty stdin is OK */
124 + error("cannot read mbox %s", file);
125 + goto out;
126 + }
127 +
128 + while (!file_done) {
129 + sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
130 + file_done = split_one(f, name, allow_bare);
131 + }
132 +
133 + if (f != stdin)
134 + fclose(f);
135 + }
136 + ret = skip;
137 +out:
138 + free(name);
139 + return ret;
140 +}
141 +int cmd_mailsplit(int argc, const char **argv, char **envp)
142 +{
143 + int nr = 0, nr_prec = 4, ret;
144 int allow_bare = 0;
145 const char *dir = NULL;
146 const char **argp;
147 static const char *stdin_only[] = { "-", NULL };
112 - char *name;
148
149 for (argp = argv+1; *argp; argp++) {
150 const char *arg = *argp;
@@ -158,31 +193,9 @@ int main(int argc, const char **argv)
193 argp = stdin_only;
194 }
195
161 - name = xmalloc(strlen(dir) + 2 + 3 * sizeof(nr));
162 -
163 - while (*argp) {
164 - const char *file = *argp++;
165 - FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
166 - int file_done = 0;
167 -
168 - if ( !f )
169 - die ("cannot open mbox %s", file);
170 -
171 - if (fgets(buf, sizeof(buf), f) == NULL) {
172 - if (f == stdin)
173 - break; /* empty stdin is OK */
174 - die("cannot read mbox %s", file);
175 - }
176 -
177 - while (!file_done) {
178 - sprintf(name, "%s/%0*d", dir, nr_prec, ++nr);
179 - file_done = split_one(f, name, allow_bare);
180 - }
181 -
182 - if (f != stdin)
183 - fclose(f);
184 - }
196 + ret = split_mbox(argp, dir, allow_bare, nr_prec, nr);
197 + if (ret != -1)
198 + printf("%d\n", ret);
199
186 - printf("%d\n", nr);
187 - return 0;
200 + return ret == -1;
201 }
builtin.h
+2
@@ -49,4 +49,6 @@ extern int cmd_rev_parse(int argc, const char **argv, char **envp);
49 extern int cmd_write_tree(int argc, const char **argv, char **envp);
50 extern int write_tree(unsigned char *sha1, int missing_ok, const char *prefix);
51
52 +extern int cmd_mailsplit(int argc, const char **argv, char **envp);
53 +extern int split_mbox(const char **mbox, const char *dir, int allow_bare, int nr_prec, int skip);
54 #endif
git.c
+2 -1
@@ -179,7 +179,8 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
179 { "diff-tree", cmd_diff_tree },
180 { "cat-file", cmd_cat_file },
181 { "rev-parse", cmd_rev_parse },
182 - { "write-tree", cmd_write_tree }
182 + { "write-tree", cmd_write_tree },
183 + { "mailsplit", cmd_mailsplit }
184 };
185 int i;
186