Make git-mailinfo a builtin
[jc: with a bit of constness tightening] 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
34488e3c375d159506d5d98e012f3b36e7cd9fe7
4 files changed
+61
-46
Makefile
+4
-10
@@ -151,7 +151,7 @@ PROGRAMS = \
151
git-checkout-index$X git-clone-pack$X \
152
git-convert-objects$X git-fetch-pack$X git-fsck-objects$X \
153
git-hash-object$X git-index-pack$X git-local-fetch$X \
154
- git-mailinfo$X git-merge-base$X \
154
+ git-merge-base$X \
155
git-merge-index$X git-mktag$X git-mktree$X git-pack-objects$X git-patch-id$X \
156
git-peek-remote$X git-prune-packed$X git-receive-pack$X \
157
git-send-pack$X git-shell$X \
@@ -166,7 +166,7 @@ PROGRAMS = \
166
BUILT_INS = git-log$X git-whatchanged$X git-show$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 \
169
+ git-check-ref-format$X git-rev-parse$X git-mailinfo$X \
170
git-init-db$X git-tar-tree$X git-upload-tar$X git-format-patch$X \
171
git-ls-files$X git-ls-tree$X git-get-tar-commit-id$X \
172
git-read-tree$X git-commit-tree$X git-write-tree$X \
@@ -223,7 +223,7 @@ BUILTIN_OBJS = \
223
builtin-rm.o builtin-init-db.o builtin-rev-parse.o \
224
builtin-tar-tree.o builtin-upload-tar.o \
225
builtin-ls-files.o builtin-ls-tree.o builtin-write-tree.o \
226
- builtin-read-tree.o builtin-commit-tree.o \
226
+ builtin-read-tree.o builtin-commit-tree.o builtin-mailinfo.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 \
229
builtin-cat-file.o builtin-mailsplit.o
@@ -379,9 +379,7 @@ ifdef NEEDS_LIBICONV
379
else
380
ICONV_LINK =
381
endif
382
- LIB_4_ICONV = $(ICONV_LINK) -liconv
383
-else
384
- LIB_4_ICONV =
382
+ LIBS += $(ICONV_LINK) -liconv
383
endif
384
ifdef NEEDS_SOCKET
385
LIBS += -lsocket
@@ -564,10 +562,6 @@ $(SIMPLE_PROGRAMS) : git-%$X : %.o
562
$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
563
$(LIB_FILE) $(SIMPLE_LIB)
564
567
-git-mailinfo$X: mailinfo.o $(LIB_FILE)
568
- $(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
569
- $(LIB_FILE) $(SIMPLE_LIB) $(LIB_4_ICONV)
570
-
565
git-local-fetch$X: fetch.o
566
git-ssh-fetch$X: rsh.o fetch.o
567
git-ssh-upload$X: rsh.o
builtin-mailinfo.c
renamed
+50
-35
@@ -12,11 +12,12 @@
12
#endif
13
#include "git-compat-util.h"
14
#include "cache.h"
15
+#include "builtin.h"
16
16
-static FILE *cmitmsg, *patchfile;
17
+static FILE *cmitmsg, *patchfile, *fin, *fout;
18
19
static int keep_subject = 0;
19
-static char *metainfo_charset = NULL;
20
+static const char *metainfo_charset = NULL;
21
static char line[1000];
22
static char date[1000];
23
static char name[1000];
@@ -49,7 +50,7 @@ static int bogus_from(char *line)
50
51
/* This is fallback, so do not bother if we already have an
52
* e-mail address.
52
- */
53
+ */
54
if (*email)
55
return 0;
56
@@ -322,13 +323,13 @@ static char *cleanup_subject(char *subject)
323
if (remove <= len *2) {
324
subject = p+1;
325
continue;
325
- }
326
+ }
327
break;
328
}
329
eatspace(subject);
330
return subject;
331
}
331
-}
332
+}
333
334
static void cleanup_space(char *buf)
335
{
@@ -648,7 +649,7 @@ static void handle_info(void)
649
cleanup_space(email);
650
cleanup_space(sub);
651
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",
653
name, email, sub, date);
654
}
655
@@ -685,7 +686,7 @@ static int handle_commit_msg(int *seen)
686
continue;
687
688
fputs(line, cmitmsg);
688
- } while (fgets(line, sizeof(line), stdin) != NULL);
689
+ } while (fgets(line, sizeof(line), fin) != NULL);
690
fclose(cmitmsg);
691
cmitmsg = NULL;
692
return 0;
@@ -706,7 +707,7 @@ static void handle_patch(void)
707
decode_transfer_encoding(line);
708
fputs(line, patchfile);
709
patch_lines++;
709
- } while (fgets(line, sizeof(line), stdin) != NULL);
710
+ } while (fgets(line, sizeof(line), fin) != NULL);
711
}
712
713
/* multipart boundary and transfer encoding are set up for us, and we
@@ -719,7 +720,7 @@ static int handle_multipart_one_part(int *seen)
720
{
721
int n = 0;
722
722
- while (fgets(line, sizeof(line), stdin) != NULL) {
723
+ while (fgets(line, sizeof(line), fin) != NULL) {
724
again:
725
n++;
726
if (is_multipart_boundary(line))
@@ -740,7 +741,7 @@ static void handle_multipart_body(void)
741
int part_num = 0;
742
743
/* Skip up to the first boundary */
743
- while (fgets(line, sizeof(line), stdin) != NULL)
744
+ while (fgets(line, sizeof(line), fin) != NULL)
745
if (is_multipart_boundary(line)) {
746
part_num = 1;
747
break;
@@ -749,7 +750,7 @@ static void handle_multipart_body(void)
750
return;
751
/* We are on boundary line. Start slurping the subhead. */
752
while (1) {
752
- int hdr = read_one_header_line(line, sizeof(line), stdin);
753
+ int hdr = read_one_header_line(line, sizeof(line), fin);
754
if (!hdr) {
755
if (handle_multipart_one_part(&seen) < 0)
756
return;
@@ -781,10 +782,45 @@ static void handle_body(void)
782
}
783
}
784
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
+
820
static const char mailinfo_usage[] =
821
"git-mailinfo [-k] [-u | --encoding=<encoding>] msg patch <mail >info";
822
787
-int main(int argc, char **argv)
823
+int cmd_mailinfo(int argc, const char **argv, char **envp)
824
{
825
/* NEEDSWORK: might want to do the optional .git/ directory
826
* discovery
@@ -805,27 +841,6 @@ int main(int argc, char **argv)
841
842
if (argc != 3)
843
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]);
846
}
builtin.h
+5
@@ -1,6 +1,8 @@
1
#ifndef BUILTIN_H
2
#define BUILTIN_H
3
4
+#include <stdio.h>
5
+
6
#ifndef PATH_MAX
7
# define PATH_MAX 4096
8
#endif
@@ -51,4 +53,7 @@ extern int write_tree(unsigned char *sha1, int missing_ok, const char *prefix);
53
54
extern int cmd_mailsplit(int argc, const char **argv, char **envp);
55
extern int split_mbox(const char **mbox, const char *dir, int allow_bare, int nr_prec, int skip);
56
+
57
+extern int cmd_mailinfo(int argc, const char **argv, char **envp);
58
+extern int mailinfo(FILE *in, FILE *out, int ks, const char *encoding, const char *msg, const char *patch);
59
#endif
git.c
+2
-1
@@ -180,7 +180,8 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
180
{ "cat-file", cmd_cat_file },
181
{ "rev-parse", cmd_rev_parse },
182
{ "write-tree", cmd_write_tree },
183
- { "mailsplit", cmd_mailsplit }
183
+ { "mailsplit", cmd_mailsplit },
184
+ { "mailinfo", cmd_mailinfo }
185
};
186
int i;
187