Imported Upstream version 2.0.1
[platform/upstream/git.git] / builtin / mktree.c
1 /*
2  * GIT - the stupid content tracker
3  *
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"
10
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;
18
19 static 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         ALLOC_GROW(entries, used + 1, alloc);
27         ent = entries[used++] = xmalloc(sizeof(**entries) + len + 1);
28         ent->mode = mode;
29         ent->len = len;
30         hashcpy(ent->sha1, sha1);
31         memcpy(ent->name, path, len+1);
32 }
33
34 static int ent_compare(const void *a_, const void *b_)
35 {
36         struct treeent *a = *(struct treeent **)a_;
37         struct treeent *b = *(struct treeent **)b_;
38         return base_name_compare(a->name, a->len, a->mode,
39                                  b->name, b->len, b->mode);
40 }
41
42 static void write_tree(unsigned char *sha1)
43 {
44         struct strbuf buf;
45         size_t size;
46         int i;
47
48         qsort(entries, used, sizeof(*entries), ent_compare);
49         for (size = i = 0; i < used; i++)
50                 size += 32 + entries[i]->len;
51
52         strbuf_init(&buf, size);
53         for (i = 0; i < used; i++) {
54                 struct treeent *ent = entries[i];
55                 strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
56                 strbuf_add(&buf, ent->sha1, 20);
57         }
58
59         write_sha1_file(buf.buf, buf.len, tree_type, sha1);
60         strbuf_release(&buf);
61 }
62
63 static const char *mktree_usage[] = {
64         N_("git mktree [-z] [--missing] [--batch]"),
65         NULL
66 };
67
68 static void mktree_line(char *buf, size_t len, int line_termination, int allow_missing)
69 {
70         char *ptr, *ntr;
71         unsigned mode;
72         enum object_type mode_type; /* object type derived from mode */
73         enum object_type obj_type; /* object type derived from sha */
74         char *path;
75         unsigned char sha1[20];
76
77         ptr = buf;
78         /*
79          * Read non-recursive ls-tree output format:
80          *     mode SP type SP sha1 TAB name
81          */
82         mode = strtoul(ptr, &ntr, 8);
83         if (ptr == ntr || !ntr || *ntr != ' ')
84                 die("input format error: %s", buf);
85         ptr = ntr + 1; /* type */
86         ntr = strchr(ptr, ' ');
87         if (!ntr || buf + len <= ntr + 40 ||
88             ntr[41] != '\t' ||
89             get_sha1_hex(ntr + 1, sha1))
90                 die("input format error: %s", buf);
91
92         /* It is perfectly normal if we do not have a commit from a submodule */
93         if (S_ISGITLINK(mode))
94                 allow_missing = 1;
95
96
97         *ntr++ = 0; /* now at the beginning of SHA1 */
98
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);
105         }
106
107         /*
108          * Object type is redundantly derivable three ways.
109          * These should all agree.
110          */
111         mode_type = object_type(mode);
112         if (mode_type != type_from_string(ptr)) {
113                 die("entry '%s' object type (%s) doesn't match mode type (%s)",
114                         path, ptr, typename(mode_type));
115         }
116
117         /* Check the type of object identified by sha1 */
118         obj_type = sha1_object_info(sha1, NULL);
119         if (obj_type < 0) {
120                 if (allow_missing) {
121                         ; /* no problem - missing objects are presumed to be of the right type */
122                 } else {
123                         die("entry '%s' object %s is unavailable", path, sha1_to_hex(sha1));
124                 }
125         } else {
126                 if (obj_type != mode_type) {
127                         /*
128                          * The object exists but is of the wrong type.
129                          * This is a problem regardless of allow_missing
130                          * because the new tree entry will never be correct.
131                          */
132                         die("entry '%s' object %s is a %s but specified type was (%s)",
133                                 path, sha1_to_hex(sha1), typename(obj_type), typename(mode_type));
134                 }
135         }
136
137         append_to_tree(mode, sha1, path);
138 }
139
140 int cmd_mktree(int ac, const char **av, const char *prefix)
141 {
142         struct strbuf sb = STRBUF_INIT;
143         unsigned char sha1[20];
144         int line_termination = '\n';
145         int allow_missing = 0;
146         int is_batch_mode = 0;
147         int got_eof = 0;
148
149         const struct option option[] = {
150                 OPT_SET_INT('z', NULL, &line_termination, N_("input is NUL terminated"), '\0'),
151                 OPT_SET_INT( 0 , "missing", &allow_missing, N_("allow missing objects"), 1),
152                 OPT_SET_INT( 0 , "batch", &is_batch_mode, N_("allow creation of more than one tree"), 1),
153                 OPT_END()
154         };
155
156         ac = parse_options(ac, av, prefix, option, mktree_usage, 0);
157
158         while (!got_eof) {
159                 while (1) {
160                         if (strbuf_getline(&sb, stdin, line_termination) == EOF) {
161                                 got_eof = 1;
162                                 break;
163                         }
164                         if (sb.buf[0] == '\0') {
165                                 /* empty lines denote tree boundaries in batch mode */
166                                 if (is_batch_mode)
167                                         break;
168                                 die("input format error: (blank line only valid in batch mode)");
169                         }
170                         mktree_line(sb.buf, sb.len, line_termination, allow_missing);
171                 }
172                 if (is_batch_mode && got_eof && used < 1) {
173                         /*
174                          * Execution gets here if the last tree entry is terminated with a
175                          * new-line.  The final new-line has been made optional to be
176                          * consistent with the original non-batch behaviour of mktree.
177                          */
178                         ; /* skip creating an empty tree */
179                 } else {
180                         write_tree(sha1);
181                         puts(sha1_to_hex(sha1));
182                         fflush(stdout);
183                 }
184                 used=0; /* reset tree entry buffer for re-use in batch mode */
185         }
186         strbuf_release(&sb);
187         exit(0);
188 }