Skip to content

Commit

Permalink
Merge branch 'js/lsfix'
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Junio C Hamano committed Jun 22, 2006
2 parents c5c2374 + 928e47e commit 5ab2c0a
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 127 deletions.
40 changes: 17 additions & 23 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -144,34 +144,33 @@ SCRIPTS = $(patsubst %.sh,%,$(SCRIPT_SH)) \

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

# ... and all the rest that could be moved out of bindir to gitexecdir
PROGRAMS = \
git-checkout-index$X \
git-convert-objects$X git-fetch-pack$X git-fsck-objects$X \
git-hash-object$X git-index-pack$X git-local-fetch$X \
git-mailinfo$X git-merge-base$X \
git-merge-base$X \
git-merge-index$X git-mktag$X git-mktree$X git-pack-objects$X git-patch-id$X \
git-peek-remote$X git-prune-packed$X git-receive-pack$X \
git-send-pack$X git-shell$X \
git-show-index$X git-ssh-fetch$X \
git-ssh-upload$X git-unpack-file$X \
git-unpack-objects$X git-update-index$X git-update-server-info$X \
git-upload-pack$X git-verify-pack$X git-write-tree$X \
git-update-ref$X git-symbolic-ref$X \
git-unpack-objects$X git-update-server-info$X \
git-upload-pack$X git-verify-pack$X \
git-symbolic-ref$X \
git-name-rev$X git-pack-redundant$X git-repo-config$X git-var$X \
git-describe$X git-merge-tree$X git-blame$X git-imap-send$X

BUILT_INS = git-log$X git-whatchanged$X git-show$X \
git-count-objects$X git-diff$X git-push$X \
git-grep$X git-add$X git-rm$X git-rev-list$X \
git-check-ref-format$X git-rev-parse$X \
BUILT_INS = git-log$X git-whatchanged$X git-show$X git-update-ref$X \
git-count-objects$X git-diff$X git-push$X git-mailsplit$X \
git-grep$X git-add$X git-rm$X git-rev-list$X git-stripspace$X \
git-check-ref-format$X git-rev-parse$X git-mailinfo$X \
git-init-db$X git-tar-tree$X git-upload-tar$X git-format-patch$X \
git-ls-files$X git-ls-tree$X git-get-tar-commit-id$X \
git-read-tree$X git-commit-tree$X \
git-apply$X git-show-branch$X git-diff-files$X \
git-read-tree$X git-commit-tree$X git-write-tree$X \
git-apply$X git-show-branch$X git-diff-files$X git-update-index$X \
git-diff-index$X git-diff-stages$X git-diff-tree$X git-cat-file$X

# what 'all' will build and 'install' will install, in gitexecdir
Expand Down Expand Up @@ -222,12 +221,13 @@ BUILTIN_OBJS = \
builtin-log.o builtin-help.o builtin-count.o builtin-diff.o builtin-push.o \
builtin-grep.o builtin-add.o builtin-rev-list.o builtin-check-ref-format.o \
builtin-rm.o builtin-init-db.o builtin-rev-parse.o \
builtin-tar-tree.o builtin-upload-tar.o \
builtin-ls-files.o builtin-ls-tree.o \
builtin-read-tree.o builtin-commit-tree.o \
builtin-tar-tree.o builtin-upload-tar.o builtin-update-index.o \
builtin-ls-files.o builtin-ls-tree.o builtin-write-tree.o \
builtin-read-tree.o builtin-commit-tree.o builtin-mailinfo.o \
builtin-apply.o builtin-show-branch.o builtin-diff-files.o \
builtin-diff-index.o builtin-diff-stages.o builtin-diff-tree.o \
builtin-cat-file.o
builtin-cat-file.o builtin-mailsplit.o builtin-stripspace.o \
builtin-update-ref.o

GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
LIBS = $(GITLIBS) -lz
Expand Down Expand Up @@ -380,9 +380,7 @@ ifdef NEEDS_LIBICONV
else
ICONV_LINK =
endif
LIB_4_ICONV = $(ICONV_LINK) -liconv
else
LIB_4_ICONV =
LIBS += $(ICONV_LINK) -liconv
endif
ifdef NEEDS_SOCKET
LIBS += -lsocket
Expand Down Expand Up @@ -566,10 +564,6 @@ $(SIMPLE_PROGRAMS) : git-%$X : %.o
$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
$(LIB_FILE) $(SIMPLE_LIB)

git-mailinfo$X: mailinfo.o $(LIB_FILE)
$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
$(LIB_FILE) $(SIMPLE_LIB) $(LIB_4_ICONV)

git-local-fetch$X: fetch.o
git-ssh-fetch$X: rsh.o fetch.o
git-ssh-upload$X: rsh.o
Expand Down
85 changes: 50 additions & 35 deletions mailinfo.c → builtin-mailinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
#endif
#include "git-compat-util.h"
#include "cache.h"
#include "builtin.h"

static FILE *cmitmsg, *patchfile;
static FILE *cmitmsg, *patchfile, *fin, *fout;

static int keep_subject = 0;
static char *metainfo_charset = NULL;
static const char *metainfo_charset = NULL;
static char line[1000];
static char date[1000];
static char name[1000];
Expand Down Expand Up @@ -49,7 +50,7 @@ static int bogus_from(char *line)

/* This is fallback, so do not bother if we already have an
* e-mail address.
*/
*/
if (*email)
return 0;

Expand Down Expand Up @@ -322,13 +323,13 @@ static char *cleanup_subject(char *subject)
if (remove <= len *2) {
subject = p+1;
continue;
}
}
break;
}
eatspace(subject);
return subject;
}
}
}

static void cleanup_space(char *buf)
{
Expand Down Expand Up @@ -648,7 +649,7 @@ static void handle_info(void)
cleanup_space(email);
cleanup_space(sub);

printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n",
fprintf(fout, "Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n",
name, email, sub, date);
}

Expand Down Expand Up @@ -685,7 +686,7 @@ static int handle_commit_msg(int *seen)
continue;

fputs(line, cmitmsg);
} while (fgets(line, sizeof(line), stdin) != NULL);
} while (fgets(line, sizeof(line), fin) != NULL);
fclose(cmitmsg);
cmitmsg = NULL;
return 0;
Expand All @@ -706,7 +707,7 @@ static void handle_patch(void)
decode_transfer_encoding(line);
fputs(line, patchfile);
patch_lines++;
} while (fgets(line, sizeof(line), stdin) != NULL);
} while (fgets(line, sizeof(line), fin) != NULL);
}

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

while (fgets(line, sizeof(line), stdin) != NULL) {
while (fgets(line, sizeof(line), fin) != NULL) {
again:
n++;
if (is_multipart_boundary(line))
Expand All @@ -740,7 +741,7 @@ static void handle_multipart_body(void)
int part_num = 0;

/* Skip up to the first boundary */
while (fgets(line, sizeof(line), stdin) != NULL)
while (fgets(line, sizeof(line), fin) != NULL)
if (is_multipart_boundary(line)) {
part_num = 1;
break;
Expand All @@ -749,7 +750,7 @@ static void handle_multipart_body(void)
return;
/* We are on boundary line. Start slurping the subhead. */
while (1) {
int hdr = read_one_header_line(line, sizeof(line), stdin);
int hdr = read_one_header_line(line, sizeof(line), fin);
if (!hdr) {
if (handle_multipart_one_part(&seen) < 0)
return;
Expand Down Expand Up @@ -781,10 +782,45 @@ static void handle_body(void)
}
}

int mailinfo(FILE *in, FILE *out, int ks, const char *encoding,
const char *msg, const char *patch)
{
keep_subject = ks;
metainfo_charset = encoding;
fin = in;
fout = out;

cmitmsg = fopen(msg, "w");
if (!cmitmsg) {
perror(msg);
return -1;
}
patchfile = fopen(patch, "w");
if (!patchfile) {
perror(patch);
fclose(cmitmsg);
return -1;
}
while (1) {
int hdr = read_one_header_line(line, sizeof(line), fin);
if (!hdr) {
if (multipart_boundary[0])
handle_multipart_body();
else
handle_body();
handle_info();
break;
}
check_header_line(line);
}

return 0;
}

static const char mailinfo_usage[] =
"git-mailinfo [-k] [-u | --encoding=<encoding>] msg patch <mail >info";

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

if (argc != 3)
usage(mailinfo_usage);
cmitmsg = fopen(argv[1], "w");
if (!cmitmsg) {
perror(argv[1]);
exit(1);
}
patchfile = fopen(argv[2], "w");
if (!patchfile) {
perror(argv[2]);
exit(1);
}
while (1) {
int hdr = read_one_header_line(line, sizeof(line), stdin);
if (!hdr) {
if (multipart_boundary[0])
handle_multipart_body();
else
handle_body();
handle_info();
break;
}
check_header_line(line);
}
return 0;

return !!mailinfo(stdin, stdout, keep_subject, metainfo_charset, argv[1], argv[2]);
}
71 changes: 42 additions & 29 deletions mailsplit.c → builtin-mailsplit.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string.h>
#include <stdio.h>
#include "cache.h"
#include "builtin.h"

static const char git_mailsplit_usage[] =
"git-mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> <mbox>...";
Expand Down Expand Up @@ -102,14 +103,48 @@ static int split_one(FILE *mbox, const char *name, int allow_bare)
exit(1);
}

int main(int argc, const char **argv)
int split_mbox(const char **mbox, const char *dir, int allow_bare, int nr_prec, int skip)
{
int nr = 0, nr_prec = 4;
char *name = xmalloc(strlen(dir) + 2 + 3 * sizeof(skip));
int ret = -1;

while (*mbox) {
const char *file = *mbox++;
FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
int file_done = 0;

if ( !f ) {
error("cannot open mbox %s", file);
goto out;
}

if (fgets(buf, sizeof(buf), f) == NULL) {
if (f == stdin)
break; /* empty stdin is OK */
error("cannot read mbox %s", file);
goto out;
}

while (!file_done) {
sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
file_done = split_one(f, name, allow_bare);
}

if (f != stdin)
fclose(f);
}
ret = skip;
out:
free(name);
return ret;
}
int cmd_mailsplit(int argc, const char **argv, char **envp)
{
int nr = 0, nr_prec = 4, ret;
int allow_bare = 0;
const char *dir = NULL;
const char **argp;
static const char *stdin_only[] = { "-", NULL };
char *name;

for (argp = argv+1; *argp; argp++) {
const char *arg = *argp;
Expand Down Expand Up @@ -158,31 +193,9 @@ int main(int argc, const char **argv)
argp = stdin_only;
}

name = xmalloc(strlen(dir) + 2 + 3 * sizeof(nr));

while (*argp) {
const char *file = *argp++;
FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
int file_done = 0;

if ( !f )
die ("cannot open mbox %s", file);

if (fgets(buf, sizeof(buf), f) == NULL) {
if (f == stdin)
break; /* empty stdin is OK */
die("cannot read mbox %s", file);
}

while (!file_done) {
sprintf(name, "%s/%0*d", dir, nr_prec, ++nr);
file_done = split_one(f, name, allow_bare);
}

if (f != stdin)
fclose(f);
}
ret = split_mbox(argp, dir, allow_bare, nr_prec, nr);
if (ret != -1)
printf("%d\n", ret);

printf("%d\n", nr);
return 0;
return ret == -1;
}
16 changes: 11 additions & 5 deletions stripspace.c → builtin-stripspace.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "builtin.h"

/*
* Remove empty lines from the beginning and end.
Expand Down Expand Up @@ -28,28 +29,33 @@ static int cleanup(char *line)
return 1;
}

int main(int argc, char **argv)
void stripspace(FILE *in, FILE *out)
{
int empties = -1;
int incomplete = 0;
char line[1024];

while (fgets(line, sizeof(line), stdin)) {
while (fgets(line, sizeof(line), in)) {
incomplete = cleanup(line);

/* Not just an empty line? */
if (line[0] != '\n') {
if (empties > 0)
putchar('\n');
fputc('\n', out);
empties = 0;
fputs(line, stdout);
fputs(line, out);
continue;
}
if (empties < 0)
continue;
empties++;
}
if (incomplete)
putchar('\n');
fputc('\n', out);
}

int cmd_stripspace(int argc, const char **argv, char **envp)
{
stripspace(stdin, stdout);
return 0;
}
Loading

0 comments on commit 5ab2c0a

Please sign in to comment.