builtin-mktree.c: use a helper function to handle one line of input
[git.git] / builtin-mktree.c
blob133ab4b0f88aac70a78bd0f3382672236a1bd174
1 /*
2 * GIT - the stupid content tracker
4 * Copyright (c) Junio C Hamano, 2006, 2009
5 */
6 #include "builtin.h"
7 #include "quote.h"
8 #include "tree.h"
9 #include "parse-options.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 hashcpy(ent->sha1, sha1);
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 struct strbuf buf;
48 size_t size;
49 int i;
51 qsort(entries, used, sizeof(*entries), ent_compare);
52 for (size = i = 0; i < used; i++)
53 size += 32 + entries[i]->len;
55 strbuf_init(&buf, size);
56 for (i = 0; i < used; i++) {
57 struct treeent *ent = entries[i];
58 strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
59 strbuf_add(&buf, ent->sha1, 20);
62 write_sha1_file(buf.buf, buf.len, tree_type, sha1);
65 static const char *mktree_usage[] = {
66 "git mktree [-z]",
67 NULL
70 static void mktree_line(char *buf, size_t len, int line_termination)
72 char *ptr, *ntr;
73 unsigned mode;
74 enum object_type type;
75 char *path;
76 unsigned char sha1[20];
78 ptr = buf;
80 * Read non-recursive ls-tree output format:
81 * mode SP type SP sha1 TAB name
83 mode = strtoul(ptr, &ntr, 8);
84 if (ptr == ntr || !ntr || *ntr != ' ')
85 die("input format error: %s", buf);
86 ptr = ntr + 1; /* type */
87 ntr = strchr(ptr, ' ');
88 if (!ntr || buf + len <= ntr + 40 ||
89 ntr[41] != '\t' ||
90 get_sha1_hex(ntr + 1, sha1))
91 die("input format error: %s", buf);
92 type = sha1_object_info(sha1, NULL);
93 if (type < 0)
94 die("object %s unavailable", sha1_to_hex(sha1));
95 *ntr++ = 0; /* now at the beginning of SHA1 */
96 if (type != type_from_string(ptr))
97 die("object type %s mismatch (%s)", ptr, typename(type));
99 path = ntr + 41; /* at the beginning of name */
100 if (line_termination && path[0] == '"') {
101 struct strbuf p_uq = STRBUF_INIT;
102 if (unquote_c_style(&p_uq, path, NULL))
103 die("invalid quoting");
104 path = strbuf_detach(&p_uq, NULL);
106 append_to_tree(mode, sha1, path);
109 int cmd_mktree(int ac, const char **av, const char *prefix)
111 struct strbuf sb = STRBUF_INIT;
112 unsigned char sha1[20];
113 int line_termination = '\n';
114 const struct option option[] = {
115 OPT_SET_INT('z', NULL, &line_termination, "input is NUL terminated", '\0'),
116 OPT_END()
119 ac = parse_options(ac, av, option, mktree_usage, 0);
121 while (strbuf_getline(&sb, stdin, line_termination) != EOF)
122 mktree_line(sb.buf, sb.len, line_termination);
124 strbuf_release(&sb);
126 write_tree(sha1);
127 puts(sha1_to_hex(sha1));
128 exit(0);