]> git.ipfire.org Git - thirdparty/git.git/blame - mktree.c
Merge branch 'master' into ph/strbuf
[thirdparty/git.git] / mktree.c
CommitLineData
83f50539
JH
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"
8e440259 9#include "tree.h"
83f50539
JH
10
11static struct treeent {
12 unsigned mode;
13 unsigned char sha1[20];
14 int len;
15 char name[FLEX_ARRAY];
16} **entries;
17static int alloc, used;
18
19static void append_to_tree(unsigned mode, unsigned char *sha1, char *path)
20{
21 struct treeent *ent;
22 int len = strlen(path);
23 if (strchr(path, '/'))
24 die("path %s contains slash", path);
25
26 if (alloc <= used) {
27 alloc = alloc_nr(used);
28 entries = xrealloc(entries, sizeof(*entries) * alloc);
29 }
30 ent = entries[used++] = xmalloc(sizeof(**entries) + len + 1);
31 ent->mode = mode;
32 ent->len = len;
e702496e 33 hashcpy(ent->sha1, sha1);
83f50539
JH
34 memcpy(ent->name, path, len+1);
35}
36
37static int ent_compare(const void *a_, const void *b_)
38{
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);
43}
44
45static void write_tree(unsigned char *sha1)
46{
d52bc661
PH
47 struct strbuf buf;
48 size_t size;
83f50539
JH
49 int i;
50
51 qsort(entries, used, sizeof(*entries), ent_compare);
83f50539
JH
52 for (size = i = 0; i < used; i++)
53 size += 32 + entries[i]->len;
d52bc661
PH
54 strbuf_init(&buf);
55 strbuf_grow(&buf, size);
83f50539
JH
56
57 for (i = 0; i < used; i++) {
58 struct treeent *ent = entries[i];
d52bc661
PH
59 strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
60 strbuf_add(&buf, ent->sha1, 20);
83f50539 61 }
d52bc661
PH
62
63 write_sha1_file(buf.buf, buf.len, tree_type, sha1);
83f50539
JH
64}
65
15e593e4 66static const char mktree_usage[] = "git-mktree [-z]";
83f50539
JH
67
68int main(int ac, char **av)
69{
70 struct strbuf sb;
71 unsigned char sha1[20];
72 int line_termination = '\n';
73
74 setup_git_directory();
75
76 while ((1 < ac) && av[1][0] == '-') {
77 char *arg = av[1];
78 if (!strcmp("-z", arg))
79 line_termination = 0;
80 else
81 usage(mktree_usage);
82 ac--;
83 av++;
84 }
85
86 strbuf_init(&sb);
87 while (1) {
83f50539
JH
88 char *ptr, *ntr;
89 unsigned mode;
21666f1a 90 enum object_type type;
83f50539
JH
91 char *path;
92
93 read_line(&sb, stdin, line_termination);
94 if (sb.eof)
95 break;
83f50539
JH
96 ptr = sb.buf;
97 /* Input is non-recursive ls-tree output format
98 * mode SP type SP sha1 TAB name
99 */
100 mode = strtoul(ptr, &ntr, 8);
101 if (ptr == ntr || !ntr || *ntr != ' ')
102 die("input format error: %s", sb.buf);
103 ptr = ntr + 1; /* type */
104 ntr = strchr(ptr, ' ');
b449f4cf 105 if (!ntr || sb.buf + sb.len <= ntr + 40 ||
83f50539
JH
106 ntr[41] != '\t' ||
107 get_sha1_hex(ntr + 1, sha1))
108 die("input format error: %s", sb.buf);
21666f1a
NP
109 type = sha1_object_info(sha1, NULL);
110 if (type < 0)
83f50539
JH
111 die("object %s unavailable", sha1_to_hex(sha1));
112 *ntr++ = 0; /* now at the beginning of SHA1 */
21666f1a
NP
113 if (type != type_from_string(ptr))
114 die("object type %s mismatch (%s)", ptr, typename(type));
83f50539
JH
115 ntr += 41; /* at the beginning of name */
116 if (line_termination && ntr[0] == '"')
117 path = unquote_c_style(ntr, NULL);
118 else
119 path = ntr;
120
121 append_to_tree(mode, sha1, path);
122
123 if (path != ntr)
124 free(path);
125 }
126 write_tree(sha1);
127 puts(sha1_to_hex(sha1));
128 exit(0);
129}