fast-import: Use strbuf API, and simplify cmd_data()
[git/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{
47 char *buffer;
48 unsigned long size, offset;
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;
54 buffer = xmalloc(size);
55 offset = 0;
56
57 for (i = 0; i < used; i++) {
58 struct treeent *ent = entries[i];
59
60 if (offset + ent->len + 100 < size) {
61 size = alloc_nr(offset + ent->len + 100);
62 buffer = xrealloc(buffer, size);
63 }
64 offset += sprintf(buffer + offset, "%o ", ent->mode);
65 offset += sprintf(buffer + offset, "%s", ent->name);
66 buffer[offset++] = 0;
e702496e 67 hashcpy((unsigned char*)buffer + offset, ent->sha1);
83f50539
JH
68 offset += 20;
69 }
8e440259 70 write_sha1_file(buffer, offset, tree_type, sha1);
83f50539
JH
71}
72
15e593e4 73static const char mktree_usage[] = "git-mktree [-z]";
83f50539
JH
74
75int main(int ac, char **av)
76{
77 struct strbuf sb;
78 unsigned char sha1[20];
79 int line_termination = '\n';
80
81 setup_git_directory();
82
83 while ((1 < ac) && av[1][0] == '-') {
84 char *arg = av[1];
85 if (!strcmp("-z", arg))
86 line_termination = 0;
87 else
88 usage(mktree_usage);
89 ac--;
90 av++;
91 }
92
93 strbuf_init(&sb);
94 while (1) {
83f50539
JH
95 char *ptr, *ntr;
96 unsigned mode;
21666f1a 97 enum object_type type;
83f50539
JH
98 char *path;
99
100 read_line(&sb, stdin, line_termination);
101 if (sb.eof)
102 break;
83f50539
JH
103 ptr = sb.buf;
104 /* Input is non-recursive ls-tree output format
105 * mode SP type SP sha1 TAB name
106 */
107 mode = strtoul(ptr, &ntr, 8);
108 if (ptr == ntr || !ntr || *ntr != ' ')
109 die("input format error: %s", sb.buf);
110 ptr = ntr + 1; /* type */
111 ntr = strchr(ptr, ' ');
b449f4cf 112 if (!ntr || sb.buf + sb.len <= ntr + 40 ||
83f50539
JH
113 ntr[41] != '\t' ||
114 get_sha1_hex(ntr + 1, sha1))
115 die("input format error: %s", sb.buf);
21666f1a
NP
116 type = sha1_object_info(sha1, NULL);
117 if (type < 0)
83f50539
JH
118 die("object %s unavailable", sha1_to_hex(sha1));
119 *ntr++ = 0; /* now at the beginning of SHA1 */
21666f1a
NP
120 if (type != type_from_string(ptr))
121 die("object type %s mismatch (%s)", ptr, typename(type));
83f50539
JH
122 ntr += 41; /* at the beginning of name */
123 if (line_termination && ntr[0] == '"')
124 path = unquote_c_style(ntr, NULL);
125 else
126 path = ntr;
127
128 append_to_tree(mode, sha1, path);
129
130 if (path != ntr)
131 free(path);
132 }
133 write_tree(sha1);
134 puts(sha1_to_hex(sha1));
135 exit(0);
136}