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