From 8ed05fb5e90f8ad2051ff7071b7425a53c594892 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Sandstr=C3=B6m?= Date: Tue, 13 Jun 2006 22:21:42 +0200 Subject: [PATCH 1/6] Make git-write-tree a builtin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lukas Sandström Signed-off-by: Junio C Hamano --- Makefile | 6 +- write-tree.c => builtin-write-tree.c | 96 ++++++++++++++++------------ builtin.h | 3 + git.c | 3 +- 4 files changed, 64 insertions(+), 44 deletions(-) rename write-tree.c => builtin-write-tree.c (66%) diff --git a/Makefile b/Makefile index ea8cd283e2..6e9ab57999 100644 --- a/Makefile +++ b/Makefile @@ -159,7 +159,7 @@ PROGRAMS = \ 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-upload-pack$X git-verify-pack$X \ git-update-ref$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 @@ -170,7 +170,7 @@ BUILT_INS = git-log$X git-whatchanged$X git-show$X \ git-check-ref-format$X git-rev-parse$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-read-tree$X git-commit-tree$X git-write-tree$X \ git-apply$X git-show-branch$X git-diff-files$X \ git-diff-index$X git-diff-stages$X git-diff-tree$X git-cat-file$X @@ -223,7 +223,7 @@ BUILTIN_OBJS = \ 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-ls-files.o builtin-ls-tree.o builtin-write-tree.o \ builtin-read-tree.o builtin-commit-tree.o \ builtin-apply.o builtin-show-branch.o builtin-diff-files.o \ builtin-diff-index.o builtin-diff-stages.o builtin-diff-tree.o \ diff --git a/write-tree.c b/builtin-write-tree.c similarity index 66% rename from write-tree.c rename to builtin-write-tree.c index bd07da6183..c3aac36024 100644 --- a/write-tree.c +++ b/builtin-write-tree.c @@ -3,29 +3,72 @@ * * Copyright (C) Linus Torvalds, 2005 */ +#include "builtin.h" #include "cache.h" #include "tree.h" #include "cache-tree.h" -static int missing_ok = 0; -static char *prefix = NULL; - static const char write_tree_usage[] = "git-write-tree [--missing-ok] [--prefix=/]"; -static struct lock_file lock_file; - -int main(int argc, char **argv) +int write_tree(unsigned char *sha1, int missing_ok, const char *prefix) { int entries, was_valid, newfd; + /* We can't free this memory, it becomes part of a linked list parsed atexit() */ + struct lock_file *lock_file = xmalloc(sizeof(struct lock_file)); + + newfd = hold_lock_file_for_update(lock_file, get_index_file()); + + entries = read_cache(); + if (entries < 0) + die("git-write-tree: error reading cache"); + + if (!active_cache_tree) + active_cache_tree = cache_tree(); + + was_valid = cache_tree_fully_valid(active_cache_tree); + + if (!was_valid) { + if (cache_tree_update(active_cache_tree, + active_cache, active_nr, + missing_ok, 0) < 0) + die("git-write-tree: error building trees"); + if (0 <= newfd) { + if (!write_cache(newfd, active_cache, active_nr)) + commit_lock_file(lock_file); + } + /* Not being able to write is fine -- we are only interested + * in updating the cache-tree part, and if the next caller + * ends up using the old index with unupdated cache-tree part + * it misses the work we did here, but that is just a + * performance penalty and not a big deal. + */ + } + + if (prefix) { + struct cache_tree *subtree = + cache_tree_find(active_cache_tree, prefix); + memcpy(sha1, subtree->sha1, 20); + } + else + memcpy(sha1, active_cache_tree->sha1, 20); + + rollback_lock_file(lock_file); + + return 0; +} + +int cmd_write_tree(int argc, const char **argv, char **envp) +{ + int missing_ok = 0, ret; + const char *prefix = NULL; + unsigned char sha1[20]; + setup_git_directory(); - newfd = hold_lock_file_for_update(&lock_file, get_index_file()); - entries = read_cache(); - while (1 < argc) { - char *arg = argv[1]; + const char *arg = argv[1]; if (!strcmp(arg, "--missing-ok")) missing_ok = 1; else if (!strncmp(arg, "--prefix=", 9)) @@ -38,35 +81,8 @@ int main(int argc, char **argv) if (argc > 2) die("too many options"); - if (entries < 0) - die("git-write-tree: error reading cache"); + ret = write_tree(sha1, missing_ok, prefix); + printf("%s\n", sha1_to_hex(sha1)); - if (!active_cache_tree) - active_cache_tree = cache_tree(); - - was_valid = cache_tree_fully_valid(active_cache_tree); - if (!was_valid) { - if (cache_tree_update(active_cache_tree, - active_cache, active_nr, - missing_ok, 0) < 0) - die("git-write-tree: error building trees"); - if (0 <= newfd) { - if (!write_cache(newfd, active_cache, active_nr)) - commit_lock_file(&lock_file); - } - /* Not being able to write is fine -- we are only interested - * in updating the cache-tree part, and if the next caller - * ends up using the old index with unupdated cache-tree part - * it misses the work we did here, but that is just a - * performance penalty and not a big deal. - */ - } - if (prefix) { - struct cache_tree *subtree = - cache_tree_find(active_cache_tree, prefix); - printf("%s\n", sha1_to_hex(subtree->sha1)); - } - else - printf("%s\n", sha1_to_hex(active_cache_tree->sha1)); - return 0; + return ret; } diff --git a/builtin.h b/builtin.h index b9f36beb66..885422e9f8 100644 --- a/builtin.h +++ b/builtin.h @@ -46,4 +46,7 @@ extern int cmd_diff_tree(int argc, const char **argv, char **envp); extern int cmd_cat_file(int argc, const char **argv, char **envp); extern int cmd_rev_parse(int argc, const char **argv, char **envp); +extern int cmd_write_tree(int argc, const char **argv, char **envp); +extern int write_tree(unsigned char *sha1, int missing_ok, const char *prefix); + #endif diff --git a/git.c b/git.c index 329ebec78c..b4b01326a9 100644 --- a/git.c +++ b/git.c @@ -178,7 +178,8 @@ static void handle_internal_command(int argc, const char **argv, char **envp) { "diff-stages", cmd_diff_stages }, { "diff-tree", cmd_diff_tree }, { "cat-file", cmd_cat_file }, - { "rev-parse", cmd_rev_parse } + { "rev-parse", cmd_rev_parse }, + { "write-tree", cmd_write_tree } }; int i; From e690e84315de1bb806eb2292f8a87cbee30b75ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Sandstr=C3=B6m?= Date: Tue, 13 Jun 2006 22:21:46 +0200 Subject: [PATCH 2/6] Make git-mailsplit a builtin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lukas Sandström Signed-off-by: Junio C Hamano --- Makefile | 5 +-- mailsplit.c => builtin-mailsplit.c | 71 ++++++++++++++++++------------ builtin.h | 2 + git.c | 3 +- 4 files changed, 48 insertions(+), 33 deletions(-) rename mailsplit.c => builtin-mailsplit.c (84%) diff --git a/Makefile b/Makefile index 6e9ab57999..8febf69054 100644 --- a/Makefile +++ b/Makefile @@ -144,7 +144,6 @@ 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 # ... and all the rest that could be moved out of bindir to gitexecdir @@ -165,7 +164,7 @@ PROGRAMS = \ 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-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-check-ref-format$X git-rev-parse$X \ git-init-db$X git-tar-tree$X git-upload-tar$X git-format-patch$X \ @@ -227,7 +226,7 @@ BUILTIN_OBJS = \ builtin-read-tree.o builtin-commit-tree.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 GITLIBS = $(LIB_FILE) $(XDIFF_LIB) LIBS = $(GITLIBS) -lz diff --git a/mailsplit.c b/builtin-mailsplit.c similarity index 84% rename from mailsplit.c rename to builtin-mailsplit.c index 70a569c12a..e2a0058435 100644 --- a/mailsplit.c +++ b/builtin-mailsplit.c @@ -12,6 +12,7 @@ #include #include #include "cache.h" +#include "builtin.h" static const char git_mailsplit_usage[] = "git-mailsplit [-d] [-f] [-b] -o ..."; @@ -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; @@ -158,31 +193,9 @@ int main(int argc, const char **argv) argp = stdin_only; } - name = xmalloc(strlen(dir) + 2 + 3 * sizeof(nr)); + ret = split_mbox(argp, dir, allow_bare, nr_prec, nr); + if (ret != -1) + printf("%d\n", ret); - 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); - } - - printf("%d\n", nr); - return 0; + return ret == -1; } diff --git a/builtin.h b/builtin.h index 885422e9f8..92e1e1b51c 100644 --- a/builtin.h +++ b/builtin.h @@ -49,4 +49,6 @@ extern int cmd_rev_parse(int argc, const char **argv, char **envp); extern int cmd_write_tree(int argc, const char **argv, char **envp); extern int write_tree(unsigned char *sha1, int missing_ok, const char *prefix); +extern int cmd_mailsplit(int argc, const char **argv, char **envp); +extern int split_mbox(const char **mbox, const char *dir, int allow_bare, int nr_prec, int skip); #endif diff --git a/git.c b/git.c index b4b01326a9..39dc40a691 100644 --- a/git.c +++ b/git.c @@ -179,7 +179,8 @@ static void handle_internal_command(int argc, const char **argv, char **envp) { "diff-tree", cmd_diff_tree }, { "cat-file", cmd_cat_file }, { "rev-parse", cmd_rev_parse }, - { "write-tree", cmd_write_tree } + { "write-tree", cmd_write_tree }, + { "mailsplit", cmd_mailsplit } }; int i; From 34488e3c375d159506d5d98e012f3b36e7cd9fe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Sandstr=C3=B6m?= Date: Tue, 13 Jun 2006 22:21:50 +0200 Subject: [PATCH 3/6] Make git-mailinfo a builtin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [jc: with a bit of constness tightening] Signed-off-by: Lukas Sandström Signed-off-by: Junio C Hamano --- Makefile | 14 ++---- mailinfo.c => builtin-mailinfo.c | 85 +++++++++++++++++++------------- builtin.h | 5 ++ git.c | 3 +- 4 files changed, 61 insertions(+), 46 deletions(-) rename mailinfo.c => builtin-mailinfo.c (94%) diff --git a/Makefile b/Makefile index 8febf69054..18bd94074c 100644 --- a/Makefile +++ b/Makefile @@ -151,7 +151,7 @@ PROGRAMS = \ git-checkout-index$X git-clone-pack$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 \ @@ -166,7 +166,7 @@ PROGRAMS = \ BUILT_INS = git-log$X git-whatchanged$X git-show$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-check-ref-format$X git-rev-parse$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-write-tree$X \ @@ -223,7 +223,7 @@ BUILTIN_OBJS = \ 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-write-tree.o \ - builtin-read-tree.o builtin-commit-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-mailsplit.o @@ -379,9 +379,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 @@ -564,10 +562,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 diff --git a/mailinfo.c b/builtin-mailinfo.c similarity index 94% rename from mailinfo.c rename to builtin-mailinfo.c index d9b74f30de..821642a7af 100644 --- a/mailinfo.c +++ b/builtin-mailinfo.c @@ -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]; @@ -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; @@ -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) { @@ -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); } @@ -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; @@ -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 @@ -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)) @@ -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; @@ -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; @@ -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=] msg patch 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 @@ -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]); } diff --git a/builtin.h b/builtin.h index 92e1e1b51c..46671e6eae 100644 --- a/builtin.h +++ b/builtin.h @@ -1,6 +1,8 @@ #ifndef BUILTIN_H #define BUILTIN_H +#include + #ifndef PATH_MAX # define PATH_MAX 4096 #endif @@ -51,4 +53,7 @@ extern int write_tree(unsigned char *sha1, int missing_ok, const char *prefix); extern int cmd_mailsplit(int argc, const char **argv, char **envp); extern int split_mbox(const char **mbox, const char *dir, int allow_bare, int nr_prec, int skip); + +extern int cmd_mailinfo(int argc, const char **argv, char **envp); +extern int mailinfo(FILE *in, FILE *out, int ks, const char *encoding, const char *msg, const char *patch); #endif diff --git a/git.c b/git.c index 39dc40a691..89d4cd2e34 100644 --- a/git.c +++ b/git.c @@ -180,7 +180,8 @@ static void handle_internal_command(int argc, const char **argv, char **envp) { "cat-file", cmd_cat_file }, { "rev-parse", cmd_rev_parse }, { "write-tree", cmd_write_tree }, - { "mailsplit", cmd_mailsplit } + { "mailsplit", cmd_mailsplit }, + { "mailinfo", cmd_mailinfo } }; int i; From 7499c996156ea99702fe13e203b97ae60d8868ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Sandstr=C3=B6m?= Date: Tue, 13 Jun 2006 22:21:53 +0200 Subject: [PATCH 4/6] Make git-stripspace a builtin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lukas Sandström Signed-off-by: Junio C Hamano --- Makefile | 6 +++--- stripspace.c => builtin-stripspace.c | 16 +++++++++++----- builtin.h | 3 +++ git.c | 3 ++- 4 files changed, 19 insertions(+), 9 deletions(-) rename stripspace.c => builtin-stripspace.c (75%) diff --git a/Makefile b/Makefile index 18bd94074c..2e093af6ae 100644 --- a/Makefile +++ b/Makefile @@ -144,7 +144,7 @@ SCRIPTS = $(patsubst %.sh,%,$(SCRIPT_SH)) \ # The ones that do not have to link with lcrypto, lz nor xdiff. SIMPLE_PROGRAMS = \ - git-stripspace$X git-daemon$X + git-daemon$X # ... and all the rest that could be moved out of bindir to gitexecdir PROGRAMS = \ @@ -165,7 +165,7 @@ PROGRAMS = \ BUILT_INS = git-log$X git-whatchanged$X git-show$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-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 \ @@ -226,7 +226,7 @@ BUILTIN_OBJS = \ 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-mailsplit.o + builtin-cat-file.o builtin-mailsplit.o builtin-stripspace.o GITLIBS = $(LIB_FILE) $(XDIFF_LIB) LIBS = $(GITLIBS) -lz diff --git a/stripspace.c b/builtin-stripspace.c similarity index 75% rename from stripspace.c rename to builtin-stripspace.c index 65a6346452..2ce1264f7b 100644 --- a/stripspace.c +++ b/builtin-stripspace.c @@ -1,6 +1,7 @@ #include #include #include +#include "builtin.h" /* * Remove empty lines from the beginning and end. @@ -28,21 +29,21 @@ 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) @@ -50,6 +51,11 @@ int main(int argc, char **argv) empties++; } if (incomplete) - putchar('\n'); + fputc('\n', out); +} + +int cmd_stripspace(int argc, const char **argv, char **envp) +{ + stripspace(stdin, stdout); return 0; } diff --git a/builtin.h b/builtin.h index 46671e6eae..e50379d45a 100644 --- a/builtin.h +++ b/builtin.h @@ -56,4 +56,7 @@ extern int split_mbox(const char **mbox, const char *dir, int allow_bare, int nr extern int cmd_mailinfo(int argc, const char **argv, char **envp); extern int mailinfo(FILE *in, FILE *out, int ks, const char *encoding, const char *msg, const char *patch); + +extern int cmd_stripspace(int argc, const char **argv, char **envp); +extern void stripspace(FILE *in, FILE *out); #endif diff --git a/git.c b/git.c index 89d4cd2e34..f00a07272a 100644 --- a/git.c +++ b/git.c @@ -181,7 +181,8 @@ static void handle_internal_command(int argc, const char **argv, char **envp) { "rev-parse", cmd_rev_parse }, { "write-tree", cmd_write_tree }, { "mailsplit", cmd_mailsplit }, - { "mailinfo", cmd_mailinfo } + { "mailinfo", cmd_mailinfo }, + { "stripspace", cmd_stripspace } }; int i; From fefe81c9964a12394d6ea81ce474ff402f0eb070 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Sandstr=C3=B6m?= Date: Tue, 13 Jun 2006 22:21:57 +0200 Subject: [PATCH 5/6] Make git-update-index a builtin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lukas Sandström Signed-off-by: Junio C Hamano --- Makefile | 6 +++--- update-index.c => builtin-update-index.c | 19 ++++++++++++------- builtin.h | 1 + git.c | 3 ++- 4 files changed, 18 insertions(+), 11 deletions(-) rename update-index.c => builtin-update-index.c (97%) diff --git a/Makefile b/Makefile index 2e093af6ae..915f130306 100644 --- a/Makefile +++ b/Makefile @@ -157,7 +157,7 @@ PROGRAMS = \ 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-unpack-objects$X git-update-server-info$X \ git-upload-pack$X git-verify-pack$X \ git-update-ref$X git-symbolic-ref$X \ git-name-rev$X git-pack-redundant$X git-repo-config$X git-var$X \ @@ -170,7 +170,7 @@ BUILT_INS = git-log$X git-whatchanged$X git-show$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-write-tree$X \ - git-apply$X git-show-branch$X git-diff-files$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 @@ -221,7 +221,7 @@ 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-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 \ diff --git a/update-index.c b/builtin-update-index.c similarity index 97% rename from update-index.c rename to builtin-update-index.c index fbccc4a67b..325cd0944d 100644 --- a/update-index.c +++ b/builtin-update-index.c @@ -8,6 +8,7 @@ #include "quote.h" #include "cache-tree.h" #include "tree-walk.h" +#include "builtin.h" /* * Default to not allowing changes to the list of files. The @@ -186,8 +187,6 @@ static void chmod_path(int flip, const char *path) die("git-update-index: cannot chmod %cx '%s'", flip, path); } -static struct lock_file lock_file; - static void update_one(const char *path, const char *prefix, int prefix_length) { const char *p = prefix_path(prefix, prefix_length, path); @@ -238,7 +237,7 @@ static void read_index_info(int line_termination) * (2) mode SP type SP sha1 TAB path * The second format is to stuff git-ls-tree output * into the index file. - * + * * (3) mode SP sha1 SP stage TAB path * This format is to put higher order stages into the * index file and matches git-ls-files --stage output. @@ -477,7 +476,7 @@ static int do_reupdate(int ac, const char **av, return 0; } -int main(int argc, const char **argv) +int cmd_update_index(int argc, const char **argv, char **envp) { int i, newfd, entries, has_errors = 0, line_termination = '\n'; int allow_options = 1; @@ -486,12 +485,16 @@ int main(int argc, const char **argv) int prefix_length = prefix ? strlen(prefix) : 0; char set_executable_bit = 0; unsigned int refresh_flags = 0; + struct lock_file *lock_file; git_config(git_default_config); - newfd = hold_lock_file_for_update(&lock_file, get_index_file()); + /* We can't free this memory, it becomes part of a linked list parsed atexit() */ + lock_file = xmalloc(sizeof(struct lock_file)); + + newfd = hold_lock_file_for_update(lock_file, get_index_file()); if (newfd < 0) - die("unable to create new index file"); + die("unable to create new cachefile"); entries = read_cache(); if (entries < 0) @@ -645,9 +648,11 @@ int main(int argc, const char **argv) finish: if (active_cache_changed) { if (write_cache(newfd, active_cache, active_nr) || - commit_lock_file(&lock_file)) + commit_lock_file(lock_file)) die("Unable to write new index file"); } + rollback_lock_file(lock_file); + return has_errors ? 1 : 0; } diff --git a/builtin.h b/builtin.h index e50379d45a..03ffdc35e7 100644 --- a/builtin.h +++ b/builtin.h @@ -47,6 +47,7 @@ extern int cmd_diff_stages(int argc, const char **argv, char **envp); extern int cmd_diff_tree(int argc, const char **argv, char **envp); extern int cmd_cat_file(int argc, const char **argv, char **envp); extern int cmd_rev_parse(int argc, const char **argv, char **envp); +extern int cmd_update_index(int argc, const char **argv, char **envp); extern int cmd_write_tree(int argc, const char **argv, char **envp); extern int write_tree(unsigned char *sha1, int missing_ok, const char *prefix); diff --git a/git.c b/git.c index f00a07272a..28c7641291 100644 --- a/git.c +++ b/git.c @@ -182,7 +182,8 @@ static void handle_internal_command(int argc, const char **argv, char **envp) { "write-tree", cmd_write_tree }, { "mailsplit", cmd_mailsplit }, { "mailinfo", cmd_mailinfo }, - { "stripspace", cmd_stripspace } + { "stripspace", cmd_stripspace }, + { "update-index", cmd_update_index } }; int i; From 854b4629f97fb216a91805706cd61b33beb49172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Sandstr=C3=B6m?= Date: Tue, 13 Jun 2006 22:22:00 +0200 Subject: [PATCH 6/6] Make git-update-ref a builtin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lukas Sandström Signed-off-by: Junio C Hamano --- Makefile | 7 ++++--- update-ref.c => builtin-update-ref.c | 5 ++++- builtin.h | 1 + git.c | 3 ++- 4 files changed, 11 insertions(+), 5 deletions(-) rename update-ref.c => builtin-update-ref.c (88%) diff --git a/Makefile b/Makefile index 915f130306..99c9ec2054 100644 --- a/Makefile +++ b/Makefile @@ -159,11 +159,11 @@ PROGRAMS = \ git-ssh-upload$X git-unpack-file$X \ git-unpack-objects$X git-update-server-info$X \ git-upload-pack$X git-verify-pack$X \ - git-update-ref$X git-symbolic-ref$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 \ +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 \ @@ -226,7 +226,8 @@ BUILTIN_OBJS = \ 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-mailsplit.o builtin-stripspace.o + builtin-cat-file.o builtin-mailsplit.o builtin-stripspace.o \ + builtin-update-ref.o GITLIBS = $(LIB_FILE) $(XDIFF_LIB) LIBS = $(GITLIBS) -lz diff --git a/update-ref.c b/builtin-update-ref.c similarity index 88% rename from update-ref.c rename to builtin-update-ref.c index a1e6bb90fe..00333c7e7c 100644 --- a/update-ref.c +++ b/builtin-update-ref.c @@ -1,10 +1,11 @@ #include "cache.h" #include "refs.h" +#include "builtin.h" static const char git_update_ref_usage[] = "git-update-ref [] [-m ]"; -int main(int argc, char **argv) +int cmd_update_ref(int argc, const char **argv, char **envp) { const char *refname=NULL, *value=NULL, *oldval=NULL, *msg=NULL; struct ref_lock *lock; @@ -52,5 +53,7 @@ int main(int argc, char **argv) return 1; if (write_ref_sha1(lock, sha1, msg) < 0) return 1; + + /* write_ref_sha1 always unlocks the ref, no need to do it explicitly */ return 0; } diff --git a/builtin.h b/builtin.h index 03ffdc35e7..f12d5e68f6 100644 --- a/builtin.h +++ b/builtin.h @@ -48,6 +48,7 @@ extern int cmd_diff_tree(int argc, const char **argv, char **envp); extern int cmd_cat_file(int argc, const char **argv, char **envp); extern int cmd_rev_parse(int argc, const char **argv, char **envp); extern int cmd_update_index(int argc, const char **argv, char **envp); +extern int cmd_update_ref(int argc, const char **argv, char **envp); extern int cmd_write_tree(int argc, const char **argv, char **envp); extern int write_tree(unsigned char *sha1, int missing_ok, const char *prefix); diff --git a/git.c b/git.c index 28c7641291..94e9a4a4b9 100644 --- a/git.c +++ b/git.c @@ -183,7 +183,8 @@ static void handle_internal_command(int argc, const char **argv, char **envp) { "mailsplit", cmd_mailsplit }, { "mailinfo", cmd_mailinfo }, { "stripspace", cmd_stripspace }, - { "update-index", cmd_update_index } + { "update-index", cmd_update_index }, + { "update-ref", cmd_update_ref } }; int i;