X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=mktree.c;h=e0da110a98d3a7376dc78df71fabc10fc5664296;hb=2515f935b9c1a0afa1b555f173a37d6ad7878fff;hp=2e84889c02dd110caff0e896a6899f877ca863f1;hpb=ddb95de33e99d547c3b533aea12f18c9e4dd649e;p=git.git diff --git a/mktree.c b/mktree.c index 2e84889c0..e0da110a9 100644 --- a/mktree.c +++ b/mktree.c @@ -4,7 +4,6 @@ * Copyright (c) Junio C Hamano, 2006 */ #include "cache.h" -#include "strbuf.h" #include "quote.h" #include "tree.h" @@ -51,9 +50,8 @@ static void write_tree(unsigned char *sha1) qsort(entries, used, sizeof(*entries), ent_compare); for (size = i = 0; i < used; i++) size += 32 + entries[i]->len; - strbuf_init(&buf); - strbuf_grow(&buf, size); + strbuf_init(&buf, size); for (i = 0; i < used; i++) { struct treeent *ent = entries[i]; strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0'); @@ -68,6 +66,7 @@ static const char mktree_usage[] = "git-mktree [-z]"; int main(int ac, char **av) { struct strbuf sb; + struct strbuf p_uq; unsigned char sha1[20]; int line_termination = '\n'; @@ -83,16 +82,14 @@ int main(int ac, char **av) av++; } - strbuf_init(&sb); - while (1) { + strbuf_init(&sb, 0); + strbuf_init(&p_uq, 0); + while (strbuf_getline(&sb, stdin, line_termination) != EOF) { char *ptr, *ntr; unsigned mode; enum object_type type; char *path; - read_line(&sb, stdin, line_termination); - if (sb.eof) - break; ptr = sb.buf; /* Input is non-recursive ls-tree output format * mode SP type SP sha1 TAB name @@ -112,17 +109,21 @@ int main(int ac, char **av) *ntr++ = 0; /* now at the beginning of SHA1 */ if (type != type_from_string(ptr)) die("object type %s mismatch (%s)", ptr, typename(type)); - ntr += 41; /* at the beginning of name */ - if (line_termination && ntr[0] == '"') - path = unquote_c_style(ntr, NULL); - else - path = ntr; - append_to_tree(mode, sha1, path); + path = ntr + 41; /* at the beginning of name */ + if (line_termination && path[0] == '"') { + strbuf_reset(&p_uq); + if (unquote_c_style(&p_uq, path, NULL)) { + die("invalid quoting"); + } + path = p_uq.buf; + } - if (path != ntr) - free(path); + append_to_tree(mode, sha1, path); } + strbuf_release(&p_uq); + strbuf_release(&sb); + write_tree(sha1); puts(sha1_to_hex(sha1)); exit(0);