]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-mktree.c
mktree: use parse-options
[thirdparty/git.git] / builtin-mktree.c
CommitLineData
83f50539
JH
1/*
2 * GIT - the stupid content tracker
3 *
633e3556 4 * Copyright (c) Junio C Hamano, 2006, 2009
83f50539 5 */
633e3556 6#include "builtin.h"
83f50539 7#include "quote.h"
8e440259 8#include "tree.h"
1fdee85c 9#include "parse-options.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;
83f50539 54
f1696ee3 55 strbuf_init(&buf, size);
83f50539
JH
56 for (i = 0; i < used; i++) {
57 struct treeent *ent = entries[i];
d52bc661
PH
58 strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
59 strbuf_add(&buf, ent->sha1, 20);
83f50539 60 }
d52bc661
PH
61
62 write_sha1_file(buf.buf, buf.len, tree_type, sha1);
83f50539
JH
63}
64
1fdee85c
JH
65static const char *mktree_usage[] = {
66 "git mktree [-z]",
67 NULL
68};
83f50539 69
633e3556 70int cmd_mktree(int ac, const char **av, const char *prefix)
83f50539 71{
f285a2d7
BC
72 struct strbuf sb = STRBUF_INIT;
73 struct strbuf p_uq = STRBUF_INIT;
83f50539
JH
74 unsigned char sha1[20];
75 int line_termination = '\n';
1fdee85c
JH
76 const struct option option[] = {
77 OPT_SET_INT('z', NULL, &line_termination, "input is NUL terminated", '\0'),
78 OPT_END()
79 };
83f50539 80
1fdee85c 81 ac = parse_options(ac, av, option, mktree_usage, 0);
83f50539 82
7fb1011e 83 while (strbuf_getline(&sb, stdin, line_termination) != EOF) {
83f50539
JH
84 char *ptr, *ntr;
85 unsigned mode;
21666f1a 86 enum object_type type;
83f50539
JH
87 char *path;
88
83f50539 89 ptr = sb.buf;
633e3556
JH
90 /*
91 * Read non-recursive ls-tree output format:
92 * mode SP type SP sha1 TAB name
83f50539
JH
93 */
94 mode = strtoul(ptr, &ntr, 8);
95 if (ptr == ntr || !ntr || *ntr != ' ')
96 die("input format error: %s", sb.buf);
97 ptr = ntr + 1; /* type */
98 ntr = strchr(ptr, ' ');
b449f4cf 99 if (!ntr || sb.buf + sb.len <= ntr + 40 ||
83f50539
JH
100 ntr[41] != '\t' ||
101 get_sha1_hex(ntr + 1, sha1))
102 die("input format error: %s", sb.buf);
21666f1a
NP
103 type = sha1_object_info(sha1, NULL);
104 if (type < 0)
83f50539
JH
105 die("object %s unavailable", sha1_to_hex(sha1));
106 *ntr++ = 0; /* now at the beginning of SHA1 */
21666f1a
NP
107 if (type != type_from_string(ptr))
108 die("object type %s mismatch (%s)", ptr, typename(type));
83f50539 109
7fb1011e
PH
110 path = ntr + 41; /* at the beginning of name */
111 if (line_termination && path[0] == '"') {
112 strbuf_reset(&p_uq);
113 if (unquote_c_style(&p_uq, path, NULL)) {
114 die("invalid quoting");
115 }
116 path = p_uq.buf;
117 }
83f50539 118
7fb1011e 119 append_to_tree(mode, sha1, path);
83f50539 120 }
7fb1011e 121 strbuf_release(&p_uq);
e6c019d0 122 strbuf_release(&sb);
7fb1011e 123
83f50539
JH
124 write_tree(sha1);
125 puts(sha1_to_hex(sha1));
126 exit(0);
127}