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{
47struct strbuf buf;
48size_t size;
49int i;
5051
qsort(entries, used, sizeof(*entries), ent_compare);
52for (size = i = 0; i < used; i++)
53size += 32 + entries[i]->len;
5455
strbuf_init(&buf, size);
56for (i = 0; i < used; i++) {
57struct treeent *ent = entries[i];
58strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
59strbuf_add(&buf, ent->sha1, 20);
60}
6162
write_sha1_file(buf.buf, buf.len, tree_type, sha1);
63}
6465
static const char mktree_usage[] = "git-mktree [-z]";
6667
int main(int ac, char **av)
68{
69struct strbuf sb;
70unsigned char sha1[20];
71int line_termination = '\n';
7273
setup_git_directory();
7475
while ((1 < ac) && av[1][0] == '-') {
76char *arg = av[1];
77if (!strcmp("-z", arg))
78line_termination = 0;
79else
80usage(mktree_usage);
81ac--;
82av++;
83}
8485
strbuf_init(&sb, 0);
86while (1) {
87char *ptr, *ntr;
88unsigned mode;
89enum object_type type;
90char *path;
9192
read_line(&sb, stdin, line_termination);
93if (sb.eof)
94break;
95ptr = sb.buf;
96/* Input is non-recursive ls-tree output format
97* mode SP type SP sha1 TAB name
98*/
99mode = strtoul(ptr, &ntr, 8);
100if (ptr == ntr || !ntr || *ntr != ' ')
101die("input format error: %s", sb.buf);
102ptr = ntr + 1; /* type */
103ntr = strchr(ptr, ' ');
104if (!ntr || sb.buf + sb.len <= ntr + 40 ||
105ntr[41] != '\t' ||
106get_sha1_hex(ntr + 1, sha1))
107die("input format error: %s", sb.buf);
108type = sha1_object_info(sha1, NULL);
109if (type < 0)
110die("object %s unavailable", sha1_to_hex(sha1));
111*ntr++ = 0; /* now at the beginning of SHA1 */
112if (type != type_from_string(ptr))
113die("object type %s mismatch (%s)", ptr, typename(type));
114ntr += 41; /* at the beginning of name */
115if (line_termination && ntr[0] == '"')
116path = unquote_c_style(ntr, NULL);
117else
118path = ntr;
119120
append_to_tree(mode, sha1, path);
121122
if (path != ntr)
123free(path);
124}
125write_tree(sha1);
126puts(sha1_to_hex(sha1));
127exit(0);
128}