summaryrefslogtreecommitdiff
path: root/mktree.c
diff options
context:
space:
mode:
authorPierre Habouzit <madcoder@debian.org>2007-09-20 00:42:14 +0200
committerJunio C Hamano <gitster@pobox.com>2007-09-20 23:32:18 -0700
commit7fb1011e610a28518959b1d2d48cea17ecc32048 (patch)
tree2f07cfdbd6e14dd3e98e1325c7f30c325ea4c785 /mktree.c
parentc76689df6c64a1e987bd779bd71a2042b5131fb9 (diff)
downloadgit-7fb1011e610a28518959b1d2d48cea17ecc32048.tar.gz
Rework unquote_c_style to work on a strbuf.
If the gain is not obvious in the diffstat, the resulting code is more readable, _and_ in checkout-index/update-index we now reuse the same buffer to unquote strings instead of always freeing/mallocing. This also is more coherent with the next patch that reworks quoting functions. The quoting function is also made more efficient scanning for backslashes and treating portions of strings without a backslash at once. Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Diffstat (limited to 'mktree.c')
-rw-r--r--mktree.c25
1 files changed, 14 insertions, 11 deletions
diff --git a/mktree.c b/mktree.c
index 9c137dec45..e0da110a98 100644
--- a/mktree.c
+++ b/mktree.c
@@ -66,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';
@@ -82,14 +83,13 @@ int main(int ac, char **av)
}
strbuf_init(&sb, 0);
- while (1) {
+ strbuf_init(&p_uq, 0);
+ while (strbuf_getline(&sb, stdin, line_termination) != EOF) {
char *ptr, *ntr;
unsigned mode;
enum object_type type;
char *path;
- if (strbuf_getline(&sb, stdin, line_termination) == EOF)
- break;
ptr = sb.buf;
/* Input is non-recursive ls-tree output format
* mode SP type SP sha1 TAB name
@@ -109,18 +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);