1/*
2* GIT - the stupid content tracker
3*
4* Copyright (c) Junio C Hamano, 2006
5*/
6#include "cache.h"
7#include "quote.h"
8#include "tree.h"
9#include "exec_cmd.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 = STRBUF_INIT;
70struct strbuf p_uq = STRBUF_INIT;
71unsigned char sha1[20];
72int line_termination = '\n';
7374
git_extract_argv0_path(av[0]);
7576
setup_git_directory();
7778
while ((1 < ac) && av[1][0] == '-') {
79char *arg = av[1];
80if (!strcmp("-z", arg))
81line_termination = 0;
82else
83usage(mktree_usage);
84ac--;
85av++;
86}
8788
while (strbuf_getline(&sb, stdin, line_termination) != EOF) {
89char *ptr, *ntr;
90unsigned mode;
91enum object_type type;
92char *path;
9394
ptr = sb.buf;
95/* Input is non-recursive ls-tree output format
96* mode SP type SP sha1 TAB name
97*/
98mode = strtoul(ptr, &ntr, 8);
99if (ptr == ntr || !ntr || *ntr != ' ')
100die("input format error: %s", sb.buf);
101ptr = ntr + 1; /* type */
102ntr = strchr(ptr, ' ');
103if (!ntr || sb.buf + sb.len <= ntr + 40 ||
104ntr[41] != '\t' ||
105get_sha1_hex(ntr + 1, sha1))
106die("input format error: %s", sb.buf);
107type = sha1_object_info(sha1, NULL);
108if (type < 0)
109die("object %s unavailable", sha1_to_hex(sha1));
110*ntr++ = 0; /* now at the beginning of SHA1 */
111if (type != type_from_string(ptr))
112die("object type %s mismatch (%s)", ptr, typename(type));
113114
path = ntr + 41; /* at the beginning of name */
115if (line_termination && path[0] == '"') {
116strbuf_reset(&p_uq);
117if (unquote_c_style(&p_uq, path, NULL)) {
118die("invalid quoting");
119}
120path = p_uq.buf;
121}
122123
append_to_tree(mode, sha1, path);
124}
125strbuf_release(&p_uq);
126strbuf_release(&sb);
127128
write_tree(sha1);
129puts(sha1_to_hex(sha1));
130exit(0);
131}