1/*
2* GIT - the stupid content tracker
3*
4* Copyright (c) Junio C Hamano, 2006
5*/
6#include "cache.h"
7#include "strbuf.h"
8#include "quote.h"
9#include "tree.h"
1011
static struct treeent {
12unsigned mode;
13unsigned char sha1[20];
14int len;
15char name[FLEX_ARRAY];
16} **entries;
17static int alloc, used;
1819
static void append_to_tree(unsigned mode, unsigned char *sha1, char *path)
20{
21struct treeent *ent;
22int len = strlen(path);
23if (strchr(path, '/'))
24die("path %s contains slash", path);
2526
if (alloc <= used) {
27alloc = alloc_nr(used);
28entries = xrealloc(entries, sizeof(*entries) * alloc);
29}
30ent = entries[used++] = xmalloc(sizeof(**entries) + len + 1);
31ent->mode = mode;
32ent->len = len;
33hashcpy(ent->sha1, sha1);
34memcpy(ent->name, path, len+1);
35}
3637
static int ent_compare(const void *a_, const void *b_)
38{
39struct treeent *a = *(struct treeent **)a_;
40struct treeent *b = *(struct treeent **)b_;
41return base_name_compare(a->name, a->len, a->mode,
42b->name, b->len, b->mode);
43}
4445
static void write_tree(unsigned char *sha1)
46{
47char *buffer;
48unsigned long size, offset;
49int i;
5051
qsort(entries, used, sizeof(*entries), ent_compare);
52for (size = i = 0; i < used; i++)
53size += 32 + entries[i]->len;
54buffer = xmalloc(size);
55offset = 0;
5657
for (i = 0; i < used; i++) {
58struct treeent *ent = entries[i];
5960
if (offset + ent->len + 100 < size) {
61size = alloc_nr(offset + ent->len + 100);
62buffer = xrealloc(buffer, size);
63}
64offset += sprintf(buffer + offset, "%o ", ent->mode);
65offset += sprintf(buffer + offset, "%s", ent->name);
66buffer[offset++] = 0;
67hashcpy((unsigned char*)buffer + offset, ent->sha1);
68offset += 20;
69}
70write_sha1_file(buffer, offset, tree_type, sha1);
71}
7273
static const char mktree_usage[] = "git-mktree [-z]";
7475
int main(int ac, char **av)
76{
77struct strbuf sb;
78unsigned char sha1[20];
79int line_termination = '\n';
8081
setup_git_directory();
8283
while ((1 < ac) && av[1][0] == '-') {
84char *arg = av[1];
85if (!strcmp("-z", arg))
86line_termination = 0;
87else
88usage(mktree_usage);
89ac--;
90av++;
91}
9293
strbuf_init(&sb);
94while (1) {
95int len;
96char *ptr, *ntr;
97unsigned mode;
98enum object_type type;
99char *path;
100101
read_line(&sb, stdin, line_termination);
102if (sb.eof)
103break;
104len = sb.len;
105ptr = sb.buf;
106/* Input is non-recursive ls-tree output format
107* mode SP type SP sha1 TAB name
108*/
109mode = strtoul(ptr, &ntr, 8);
110if (ptr == ntr || !ntr || *ntr != ' ')
111die("input format error: %s", sb.buf);
112ptr = ntr + 1; /* type */
113ntr = strchr(ptr, ' ');
114if (!ntr || sb.buf + len <= ntr + 41 ||
115ntr[41] != '\t' ||
116get_sha1_hex(ntr + 1, sha1))
117die("input format error: %s", sb.buf);
118type = sha1_object_info(sha1, NULL);
119if (type < 0)
120die("object %s unavailable", sha1_to_hex(sha1));
121*ntr++ = 0; /* now at the beginning of SHA1 */
122if (type != type_from_string(ptr))
123die("object type %s mismatch (%s)", ptr, typename(type));
124ntr += 41; /* at the beginning of name */
125if (line_termination && ntr[0] == '"')
126path = unquote_c_style(ntr, NULL);
127else
128path = ntr;
129130
append_to_tree(mode, sha1, path);
131132
if (path != ntr)
133free(path);
134}
135write_tree(sha1);
136puts(sha1_to_hex(sha1));
137exit(0);
138}