Skip to content

Commit 5ab2c0a

Browse files
author
Junio C Hamano
committed
Merge branch 'js/lsfix'
* js/lsfix: Initialize lock_file struct to all zero. Make git-update-ref a builtin Make git-update-index a builtin Make git-stripspace a builtin Make git-mailinfo a builtin Make git-mailsplit a builtin Make git-write-tree a builtin
2 parents c5c2374 + 928e47e commit 5ab2c0a

9 files changed

+200
-127
lines changed

Makefile

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -144,34 +144,33 @@ SCRIPTS = $(patsubst %.sh,%,$(SCRIPT_SH)) \
144144

145145
# The ones that do not have to link with lcrypto, lz nor xdiff.
146146
SIMPLE_PROGRAMS = \
147-
git-mailsplit$X \
148-
git-stripspace$X git-daemon$X
147+
git-daemon$X
149148

150149
# ... and all the rest that could be moved out of bindir to gitexecdir
151150
PROGRAMS = \
152151
git-checkout-index$X \
153152
git-convert-objects$X git-fetch-pack$X git-fsck-objects$X \
154153
git-hash-object$X git-index-pack$X git-local-fetch$X \
155-
git-mailinfo$X git-merge-base$X \
154+
git-merge-base$X \
156155
git-merge-index$X git-mktag$X git-mktree$X git-pack-objects$X git-patch-id$X \
157156
git-peek-remote$X git-prune-packed$X git-receive-pack$X \
158157
git-send-pack$X git-shell$X \
159158
git-show-index$X git-ssh-fetch$X \
160159
git-ssh-upload$X git-unpack-file$X \
161-
git-unpack-objects$X git-update-index$X git-update-server-info$X \
162-
git-upload-pack$X git-verify-pack$X git-write-tree$X \
163-
git-update-ref$X git-symbolic-ref$X \
160+
git-unpack-objects$X git-update-server-info$X \
161+
git-upload-pack$X git-verify-pack$X \
162+
git-symbolic-ref$X \
164163
git-name-rev$X git-pack-redundant$X git-repo-config$X git-var$X \
165164
git-describe$X git-merge-tree$X git-blame$X git-imap-send$X
166165

167-
BUILT_INS = git-log$X git-whatchanged$X git-show$X \
168-
git-count-objects$X git-diff$X git-push$X \
169-
git-grep$X git-add$X git-rm$X git-rev-list$X \
170-
git-check-ref-format$X git-rev-parse$X \
166+
BUILT_INS = git-log$X git-whatchanged$X git-show$X git-update-ref$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 git-stripspace$X \
169+
git-check-ref-format$X git-rev-parse$X git-mailinfo$X \
171170
git-init-db$X git-tar-tree$X git-upload-tar$X git-format-patch$X \
172171
git-ls-files$X git-ls-tree$X git-get-tar-commit-id$X \
173-
git-read-tree$X git-commit-tree$X \
174-
git-apply$X git-show-branch$X git-diff-files$X \
172+
git-read-tree$X git-commit-tree$X git-write-tree$X \
173+
git-apply$X git-show-branch$X git-diff-files$X git-update-index$X \
175174
git-diff-index$X git-diff-stages$X git-diff-tree$X git-cat-file$X
176175

177176
# what 'all' will build and 'install' will install, in gitexecdir
@@ -222,12 +221,13 @@ BUILTIN_OBJS = \
222221
builtin-log.o builtin-help.o builtin-count.o builtin-diff.o builtin-push.o \
223222
builtin-grep.o builtin-add.o builtin-rev-list.o builtin-check-ref-format.o \
224223
builtin-rm.o builtin-init-db.o builtin-rev-parse.o \
225-
builtin-tar-tree.o builtin-upload-tar.o \
226-
builtin-ls-files.o builtin-ls-tree.o \
227-
builtin-read-tree.o builtin-commit-tree.o \
224+
builtin-tar-tree.o builtin-upload-tar.o builtin-update-index.o \
225+
builtin-ls-files.o builtin-ls-tree.o builtin-write-tree.o \
226+
builtin-read-tree.o builtin-commit-tree.o builtin-mailinfo.o \
228227
builtin-apply.o builtin-show-branch.o builtin-diff-files.o \
229228
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 builtin-stripspace.o \
230+
builtin-update-ref.o
231231

232232
GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
233233
LIBS = $(GITLIBS) -lz
@@ -380,9 +380,7 @@ ifdef NEEDS_LIBICONV
380380
else
381381
ICONV_LINK =
382382
endif
383-
LIB_4_ICONV = $(ICONV_LINK) -liconv
384-
else
385-
LIB_4_ICONV =
383+
LIBS += $(ICONV_LINK) -liconv
386384
endif
387385
ifdef NEEDS_SOCKET
388386
LIBS += -lsocket
@@ -566,10 +564,6 @@ $(SIMPLE_PROGRAMS) : git-%$X : %.o
566564
$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
567565
$(LIB_FILE) $(SIMPLE_LIB)
568566

569-
git-mailinfo$X: mailinfo.o $(LIB_FILE)
570-
$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
571-
$(LIB_FILE) $(SIMPLE_LIB) $(LIB_4_ICONV)
572-
573567
git-local-fetch$X: fetch.o
574568
git-ssh-fetch$X: rsh.o fetch.o
575569
git-ssh-upload$X: rsh.o

mailinfo.c renamed to builtin-mailinfo.c

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
#endif
1313
#include "git-compat-util.h"
1414
#include "cache.h"
15+
#include "builtin.h"
1516

16-
static FILE *cmitmsg, *patchfile;
17+
static FILE *cmitmsg, *patchfile, *fin, *fout;
1718

1819
static int keep_subject = 0;
19-
static char *metainfo_charset = NULL;
20+
static const char *metainfo_charset = NULL;
2021
static char line[1000];
2122
static char date[1000];
2223
static char name[1000];
@@ -49,7 +50,7 @@ static int bogus_from(char *line)
4950

5051
/* This is fallback, so do not bother if we already have an
5152
* e-mail address.
52-
*/
53+
*/
5354
if (*email)
5455
return 0;
5556

@@ -322,13 +323,13 @@ static char *cleanup_subject(char *subject)
322323
if (remove <= len *2) {
323324
subject = p+1;
324325
continue;
325-
}
326+
}
326327
break;
327328
}
328329
eatspace(subject);
329330
return subject;
330331
}
331-
}
332+
}
332333

333334
static void cleanup_space(char *buf)
334335
{
@@ -648,7 +649,7 @@ static void handle_info(void)
648649
cleanup_space(email);
649650
cleanup_space(sub);
650651

651-
printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n",
652+
fprintf(fout, "Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n",
652653
name, email, sub, date);
653654
}
654655

@@ -685,7 +686,7 @@ static int handle_commit_msg(int *seen)
685686
continue;
686687

687688
fputs(line, cmitmsg);
688-
} while (fgets(line, sizeof(line), stdin) != NULL);
689+
} while (fgets(line, sizeof(line), fin) != NULL);
689690
fclose(cmitmsg);
690691
cmitmsg = NULL;
691692
return 0;
@@ -706,7 +707,7 @@ static void handle_patch(void)
706707
decode_transfer_encoding(line);
707708
fputs(line, patchfile);
708709
patch_lines++;
709-
} while (fgets(line, sizeof(line), stdin) != NULL);
710+
} while (fgets(line, sizeof(line), fin) != NULL);
710711
}
711712

712713
/* multipart boundary and transfer encoding are set up for us, and we
@@ -719,7 +720,7 @@ static int handle_multipart_one_part(int *seen)
719720
{
720721
int n = 0;
721722

722-
while (fgets(line, sizeof(line), stdin) != NULL) {
723+
while (fgets(line, sizeof(line), fin) != NULL) {
723724
again:
724725
n++;
725726
if (is_multipart_boundary(line))
@@ -740,7 +741,7 @@ static void handle_multipart_body(void)
740741
int part_num = 0;
741742

742743
/* Skip up to the first boundary */
743-
while (fgets(line, sizeof(line), stdin) != NULL)
744+
while (fgets(line, sizeof(line), fin) != NULL)
744745
if (is_multipart_boundary(line)) {
745746
part_num = 1;
746747
break;
@@ -749,7 +750,7 @@ static void handle_multipart_body(void)
749750
return;
750751
/* We are on boundary line. Start slurping the subhead. */
751752
while (1) {
752-
int hdr = read_one_header_line(line, sizeof(line), stdin);
753+
int hdr = read_one_header_line(line, sizeof(line), fin);
753754
if (!hdr) {
754755
if (handle_multipart_one_part(&seen) < 0)
755756
return;
@@ -781,10 +782,45 @@ static void handle_body(void)
781782
}
782783
}
783784

785+
int mailinfo(FILE *in, FILE *out, int ks, const char *encoding,
786+
const char *msg, const char *patch)
787+
{
788+
keep_subject = ks;
789+
metainfo_charset = encoding;
790+
fin = in;
791+
fout = out;
792+
793+
cmitmsg = fopen(msg, "w");
794+
if (!cmitmsg) {
795+
perror(msg);
796+
return -1;
797+
}
798+
patchfile = fopen(patch, "w");
799+
if (!patchfile) {
800+
perror(patch);
801+
fclose(cmitmsg);
802+
return -1;
803+
}
804+
while (1) {
805+
int hdr = read_one_header_line(line, sizeof(line), fin);
806+
if (!hdr) {
807+
if (multipart_boundary[0])
808+
handle_multipart_body();
809+
else
810+
handle_body();
811+
handle_info();
812+
break;
813+
}
814+
check_header_line(line);
815+
}
816+
817+
return 0;
818+
}
819+
784820
static const char mailinfo_usage[] =
785821
"git-mailinfo [-k] [-u | --encoding=<encoding>] msg patch <mail >info";
786822

787-
int main(int argc, char **argv)
823+
int cmd_mailinfo(int argc, const char **argv, char **envp)
788824
{
789825
/* NEEDSWORK: might want to do the optional .git/ directory
790826
* discovery
@@ -805,27 +841,6 @@ int main(int argc, char **argv)
805841

806842
if (argc != 3)
807843
usage(mailinfo_usage);
808-
cmitmsg = fopen(argv[1], "w");
809-
if (!cmitmsg) {
810-
perror(argv[1]);
811-
exit(1);
812-
}
813-
patchfile = fopen(argv[2], "w");
814-
if (!patchfile) {
815-
perror(argv[2]);
816-
exit(1);
817-
}
818-
while (1) {
819-
int hdr = read_one_header_line(line, sizeof(line), stdin);
820-
if (!hdr) {
821-
if (multipart_boundary[0])
822-
handle_multipart_body();
823-
else
824-
handle_body();
825-
handle_info();
826-
break;
827-
}
828-
check_header_line(line);
829-
}
830-
return 0;
844+
845+
return !!mailinfo(stdin, stdout, keep_subject, metainfo_charset, argv[1], argv[2]);
831846
}

mailsplit.c renamed to builtin-mailsplit.c

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <string.h>
1313
#include <stdio.h>
1414
#include "cache.h"
15+
#include "builtin.h"
1516

1617
static const char git_mailsplit_usage[] =
1718
"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)
102103
exit(1);
103104
}
104105

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)
106107
{
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;
108144
int allow_bare = 0;
109145
const char *dir = NULL;
110146
const char **argp;
111147
static const char *stdin_only[] = { "-", NULL };
112-
char *name;
113148

114149
for (argp = argv+1; *argp; argp++) {
115150
const char *arg = *argp;
@@ -158,31 +193,9 @@ int main(int argc, const char **argv)
158193
argp = stdin_only;
159194
}
160195

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);
185199

186-
printf("%d\n", nr);
187-
return 0;
200+
return ret == -1;
188201
}

stripspace.c renamed to builtin-stripspace.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22
#include <string.h>
33
#include <ctype.h>
4+
#include "builtin.h"
45

56
/*
67
* Remove empty lines from the beginning and end.
@@ -28,28 +29,33 @@ static int cleanup(char *line)
2829
return 1;
2930
}
3031

31-
int main(int argc, char **argv)
32+
void stripspace(FILE *in, FILE *out)
3233
{
3334
int empties = -1;
3435
int incomplete = 0;
3536
char line[1024];
3637

37-
while (fgets(line, sizeof(line), stdin)) {
38+
while (fgets(line, sizeof(line), in)) {
3839
incomplete = cleanup(line);
3940

4041
/* Not just an empty line? */
4142
if (line[0] != '\n') {
4243
if (empties > 0)
43-
putchar('\n');
44+
fputc('\n', out);
4445
empties = 0;
45-
fputs(line, stdout);
46+
fputs(line, out);
4647
continue;
4748
}
4849
if (empties < 0)
4950
continue;
5051
empties++;
5152
}
5253
if (incomplete)
53-
putchar('\n');
54+
fputc('\n', out);
55+
}
56+
57+
int cmd_stripspace(int argc, const char **argv, char **envp)
58+
{
59+
stripspace(stdin, stdout);
5460
return 0;
5561
}

0 commit comments

Comments
 (0)