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