mktree: fix a memory leak in write_tree()
[git/jrn.git] / mktree.c
blobbb23c4425b483317c3cbe4775c10b967a2822af4
1 /*
2 * GIT - the stupid content tracker
4 * Copyright (c) Junio C Hamano, 2006
5 */
6 #include "cache.h"
7 #include "strbuf.h"
8 #include "quote.h"
9 #include "tree.h"
11 static struct treeent {
12 unsigned mode;
13 unsigned char sha1[20];
14 int len;
15 char name[FLEX_ARRAY];
16 } **entries;
17 static int alloc, used;
19 static void append_to_tree(unsigned mode, unsigned char *sha1, char *path)
21 struct treeent *ent;
22 int len = strlen(path);
23 if (strchr(path, '/'))
24 die("path %s contains slash", path);
26 if (alloc <= used) {
27 alloc = alloc_nr(used);
28 entries = xrealloc(entries, sizeof(*entries) * alloc);
30 ent = entries[used++] = xmalloc(sizeof(**entries) + len + 1);
31 ent->mode = mode;
32 ent->len = len;
33 hashcpy(ent->sha1, sha1);
34 memcpy(ent->name, path, len+1);
37 static int ent_compare(const void *a_, const void *b_)
39 struct treeent *a = *(struct treeent **)a_;
40 struct treeent *b = *(struct treeent **)b_;
41 return base_name_compare(a->name, a->len, a->mode,
42 b->name, b->len, b->mode);
45 static void write_tree(unsigned char *sha1)
47 struct strbuf buf;
48 size_t size;
49 int i;
51 qsort(entries, used, sizeof(*entries), ent_compare);
52 for (size = i = 0; i < used; i++)
53 size += 32 + entries[i]->len;
54 strbuf_init(&buf);
55 strbuf_grow(&buf, size);
57 for (i = 0; i < used; i++) {
58 struct treeent *ent = entries[i];
59 strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
60 strbuf_add(&buf, ent->sha1, 20);
63 write_sha1_file(buf.buf, buf.len, tree_type, sha1);
64 strbuf_release(&buf);
67 static const char mktree_usage[] = "git-mktree [-z]";
69 int main(int ac, char **av)
71 struct strbuf sb;
72 unsigned char sha1[20];
73 int line_termination = '\n';
75 setup_git_directory();
77 while ((1 < ac) && av[1][0] == '-') {
78 char *arg = av[1];
79 if (!strcmp("-z", arg))
80 line_termination = 0;
81 else
82 usage(mktree_usage);
83 ac--;
84 av++;
87 strbuf_init(&sb);
88 while (1) {
89 char *ptr, *ntr;
90 unsigned mode;
91 enum object_type type;
92 char *path;
94 read_line(&sb, stdin, line_termination);
95 if (sb.eof)
96 break;
97 ptr = sb.buf;
98 /* Input is non-recursive ls-tree output format
99 * mode SP type SP sha1 TAB name
101 mode = strtoul(ptr, &ntr, 8);
102 if (ptr == ntr || !ntr || *ntr != ' ')
103 die("input format error: %s", sb.buf);
104 ptr = ntr + 1; /* type */
105 ntr = strchr(ptr, ' ');
106 if (!ntr || sb.buf + sb.len <= ntr + 40 ||
107 ntr[41] != '\t' ||
108 get_sha1_hex(ntr + 1, sha1))
109 die("input format error: %s", sb.buf);
110 type = sha1_object_info(sha1, NULL);
111 if (type < 0)
112 die("object %s unavailable", sha1_to_hex(sha1));
113 *ntr++ = 0; /* now at the beginning of SHA1 */
114 if (type != type_from_string(ptr))
115 die("object type %s mismatch (%s)", ptr, typename(type));
116 ntr += 41; /* at the beginning of name */
117 if (line_termination && ntr[0] == '"')
118 path = unquote_c_style(ntr, NULL);
119 else
120 path = ntr;
122 append_to_tree(mode, sha1, path);
124 if (path != ntr)
125 free(path);
127 write_tree(sha1);
128 puts(sha1_to_hex(sha1));
129 exit(0);