GIT 1.3.0-rc3
[debian-git.git] / mktree.c
blobab63cd99d4ae41310b97811cf738803afe16ff1f
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 memcpy(ent->sha1, sha1, 20);
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 char *buffer;
48 unsigned long size, offset;
49 int i;
51 qsort(entries, used, sizeof(*entries), ent_compare);
52 size = 100;
53 for (size = i = 0; i < used; i++)
54 size += 32 + entries[i]->len;
55 buffer = xmalloc(size);
56 offset = 0;
58 for (i = 0; i < used; i++) {
59 struct treeent *ent = entries[i];
61 if (offset + ent->len + 100 < size) {
62 size = alloc_nr(offset + ent->len + 100);
63 buffer = xrealloc(buffer, size);
65 offset += sprintf(buffer + offset, "%o ", ent->mode);
66 offset += sprintf(buffer + offset, "%s", ent->name);
67 buffer[offset++] = 0;
68 memcpy(buffer + offset, ent->sha1, 20);
69 offset += 20;
71 write_sha1_file(buffer, offset, tree_type, sha1);
74 static const char mktree_usage[] = "mktree [-z]";
76 int main(int ac, char **av)
78 struct strbuf sb;
79 unsigned char sha1[20];
80 int line_termination = '\n';
82 setup_git_directory();
84 while ((1 < ac) && av[1][0] == '-') {
85 char *arg = av[1];
86 if (!strcmp("-z", arg))
87 line_termination = 0;
88 else
89 usage(mktree_usage);
90 ac--;
91 av++;
94 strbuf_init(&sb);
95 while (1) {
96 int len;
97 char *ptr, *ntr;
98 unsigned mode;
99 char type[20];
100 char *path;
102 read_line(&sb, stdin, line_termination);
103 if (sb.eof)
104 break;
105 len = sb.len;
106 ptr = sb.buf;
107 /* Input is non-recursive ls-tree output format
108 * mode SP type SP sha1 TAB name
110 mode = strtoul(ptr, &ntr, 8);
111 if (ptr == ntr || !ntr || *ntr != ' ')
112 die("input format error: %s", sb.buf);
113 ptr = ntr + 1; /* type */
114 ntr = strchr(ptr, ' ');
115 if (!ntr || sb.buf + len <= ntr + 41 ||
116 ntr[41] != '\t' ||
117 get_sha1_hex(ntr + 1, sha1))
118 die("input format error: %s", sb.buf);
119 if (sha1_object_info(sha1, type, NULL))
120 die("object %s unavailable", sha1_to_hex(sha1));
121 *ntr++ = 0; /* now at the beginning of SHA1 */
122 if (strcmp(ptr, type))
123 die("object type %s mismatch (%s)", ptr, type);
124 ntr += 41; /* at the beginning of name */
125 if (line_termination && ntr[0] == '"')
126 path = unquote_c_style(ntr, NULL);
127 else
128 path = ntr;
130 append_to_tree(mode, sha1, path);
132 if (path != ntr)
133 free(path);
135 write_tree(sha1);
136 puts(sha1_to_hex(sha1));
137 exit(0);