@matheusbsbs / matheus-bagliano / commits / 21666f1aae
Fork 0

convert object type handling from a string to a number

We currently have two parallel notation for dealing with object types in the code: a string and a numerical value. One of them is obviously redundent, and the most used one requires more stack space and a bunch of strcmp() all over the place. This is an initial step for the removal of the version using a char array found in object reading code paths. The patch is unfortunately large but there is no sane way to split it in smaller parts without breaking the system. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nicolas Pitre committed Feb 26, 2007 at 14:55 UTC 21666f1aae4e890d8f50924f9e80763b27e6a45d
37 files changed +266 -290
archive-tar.c
+2 -2
@@ -262,7 +262,7 @@ static int write_tar_entry(const unsigned char *sha1,
262 static struct strbuf path;
263 int filenamelen = strlen(filename);
264 void *buffer;
265 - char type[20];
265 + enum object_type type;
266 unsigned long size;
267
268 if (!path.alloc) {
@@ -283,7 +283,7 @@ static int write_tar_entry(const unsigned char *sha1,
283 buffer = NULL;
284 size = 0;
285 } else {
286 - buffer = read_sha1_file(sha1, type, &size);
286 + buffer = read_sha1_file(sha1, &type, &size);
287 if (!buffer)
288 die("cannot read %s", sha1_to_hex(sha1));
289 }
archive-zip.c
+2 -2
@@ -167,7 +167,7 @@ static int write_zip_entry(const unsigned char *sha1,
167 int pathlen;
168 unsigned char *out;
169 char *path;
170 - char type[20];
170 + enum object_type type;
171 void *buffer = NULL;
172 void *deflated = NULL;
173
@@ -195,7 +195,7 @@ static int write_zip_entry(const unsigned char *sha1,
195 if (S_ISREG(mode) && zlib_compression_level != 0)
196 method = 8;
197 result = 0;
198 - buffer = read_sha1_file(sha1, type, &size);
198 + buffer = read_sha1_file(sha1, &type, &size);
199 if (!buffer)
200 die("cannot read %s", sha1_to_hex(sha1));
201 crc = crc32(crc, buffer, size);
blob.c
+3 -3
@@ -30,18 +30,18 @@ int parse_blob_buffer(struct blob *item, void *buffer, unsigned long size)
30
31 int parse_blob(struct blob *item)
32 {
33 - char type[20];
33 + enum object_type type;
34 void *buffer;
35 unsigned long size;
36 int ret;
37
38 if (item->object.parsed)
39 return 0;
40 - buffer = read_sha1_file(item->object.sha1, type, &size);
40 + buffer = read_sha1_file(item->object.sha1, &type, &size);
41 if (!buffer)
42 return error("Could not read %s",
43 sha1_to_hex(item->object.sha1));
44 - if (strcmp(type, blob_type))
44 + if (type != OBJ_BLOB)
45 return error("Object %s not a blob",
46 sha1_to_hex(item->object.sha1));
47 ret = parse_blob_buffer(item, buffer, size);
builtin-apply.c
+4 -4
@@ -1912,11 +1912,11 @@ static int apply_binary(struct buffer_desc *desc, struct patch *patch)
1912
1913 if (has_sha1_file(sha1)) {
1914 /* We already have the postimage */
1915 - char type[10];
1915 + enum object_type type;
1916 unsigned long size;
1917
1918 free(desc->buffer);
1919 - desc->buffer = read_sha1_file(sha1, type, &size);
1919 + desc->buffer = read_sha1_file(sha1, &type, &size);
1920 if (!desc->buffer)
1921 return error("the necessary postimage %s for "
1922 "'%s' cannot be read",
@@ -1972,8 +1972,8 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
1972 buf = NULL;
1973 if (cached) {
1974 if (ce) {
1975 - char type[20];
1976 - buf = read_sha1_file(ce->sha1, type, &size);
1975 + enum object_type type;
1976 + buf = read_sha1_file(ce->sha1, &type, &size);
1977 if (!buf)
1978 return error("read of %s failed",
1979 patch->old_name);
builtin-blame.c
+8 -10
@@ -87,9 +87,9 @@ struct origin {
87 static char *fill_origin_blob(struct origin *o, mmfile_t *file)
88 {
89 if (!o->file.ptr) {
90 - char type[10];
90 + enum object_type type;
91 num_read_blob++;
92 - file->ptr = read_sha1_file(o->blob_sha1, type,
92 + file->ptr = read_sha1_file(o->blob_sha1, &type,
93 (unsigned long *)(&(file->size)));
94 o->file = *file;
95 }
@@ -263,7 +263,6 @@ static struct origin *get_origin(struct scoreboard *sb,
263 static int fill_blob_sha1(struct origin *origin)
264 {
265 unsigned mode;
266 - char type[10];
266
267 if (!is_null_sha1(origin->blob_sha1))
268 return 0;
@@ -271,8 +270,7 @@ static int fill_blob_sha1(struct origin *origin)
270 origin->path,
271 origin->blob_sha1, &mode))
272 goto error_out;
274 - if (sha1_object_info(origin->blob_sha1, type, NULL) ||
275 - strcmp(type, blob_type))
273 + if (sha1_object_info(origin->blob_sha1, NULL) != OBJ_BLOB)
274 goto error_out;
275 return 0;
276 error_out:
@@ -1322,10 +1320,10 @@ static void get_commit_info(struct commit *commit,
1320 * we now need to populate them for output.
1321 */
1322 if (!commit->buffer) {
1325 - char type[20];
1323 + enum object_type type;
1324 unsigned long size;
1325 commit->buffer =
1328 - read_sha1_file(commit->object.sha1, type, &size);
1326 + read_sha1_file(commit->object.sha1, &type, &size);
1327 }
1328 ret->author = author_buf;
1329 get_ac_line(commit->buffer, "\nauthor ",
@@ -2006,7 +2004,7 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
2004 buf[fin_size] = 0;
2005 origin->file.ptr = buf;
2006 origin->file.size = fin_size;
2009 - pretend_sha1_file(buf, fin_size, blob_type, origin->blob_sha1);
2007 + pretend_sha1_file(buf, fin_size, OBJ_BLOB, origin->blob_sha1);
2008 commit->util = origin;
2009
2010 /*
@@ -2068,7 +2066,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
2066 int show_stats = 0;
2067 const char *revs_file = NULL;
2068 const char *final_commit_name = NULL;
2071 - char type[10];
2069 + enum object_type type;
2070 const char *bottomtop = NULL;
2071 const char *contents_from = NULL;
2072
@@ -2302,7 +2300,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
2300 if (fill_blob_sha1(o))
2301 die("no such path %s in %s", path, final_commit_name);
2302
2305 - sb.final_buf = read_sha1_file(o->blob_sha1, type,
2303 + sb.final_buf = read_sha1_file(o->blob_sha1, &type,
2304 &sb.final_buf_size);
2305 }
2306 num_read_blob++;
builtin-cat-file.c
+11 -8
@@ -79,7 +79,7 @@ static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long
79 int cmd_cat_file(int argc, const char **argv, const char *prefix)
80 {
81 unsigned char sha1[20];
82 - char type[20];
82 + enum object_type type;
83 void *buf;
84 unsigned long size;
85 int opt;
@@ -100,14 +100,16 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
100 buf = NULL;
101 switch (opt) {
102 case 't':
103 - if (!sha1_object_info(sha1, type, NULL)) {
104 - printf("%s\n", type);
103 + type = sha1_object_info(sha1, NULL);
104 + if (type > 0) {
105 + printf("%s\n", typename(type));
106 return 0;
107 }
108 break;
109
110 case 's':
110 - if (!sha1_object_info(sha1, type, &size)) {
111 + type = sha1_object_info(sha1, &size);
112 + if (type > 0) {
113 printf("%lu\n", size);
114 return 0;
115 }
@@ -117,17 +119,18 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
119 return !has_sha1_file(sha1);
120
121 case 'p':
120 - if (sha1_object_info(sha1, type, NULL))
122 + type = sha1_object_info(sha1, NULL);
123 + if (type < 0)
124 die("Not a valid object name %s", argv[2]);
125
126 /* custom pretty-print here */
124 - if (!strcmp(type, tree_type))
127 + if (type == OBJ_TREE)
128 return cmd_ls_tree(2, argv + 1, NULL);
129
127 - buf = read_sha1_file(sha1, type, &size);
130 + buf = read_sha1_file(sha1, &type, &size);
131 if (!buf)
132 die("Cannot read object %s", argv[2]);
130 - if (!strcmp(type, tag_type)) {
133 + if (type == OBJ_TAG) {
134 pprint_tag(sha1, buf, size);
135 return 0;
136 }
builtin-commit-tree.c
+3 -4
@@ -47,11 +47,10 @@ static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...)
47
48 static void check_valid(unsigned char *sha1, const char *expect)
49 {
50 - char type[20];
51 -
52 - if (sha1_object_info(sha1, type, NULL))
50 + enum object_type type = sha1_object_info(sha1, NULL);
51 + if (type < 0)
52 die("%s is not a valid object", sha1_to_hex(sha1));
54 - if (expect && strcmp(type, expect))
53 + if (expect && type != type_from_string(expect))
54 die("%s is not a valid '%s' object", sha1_to_hex(sha1),
55 expect);
56 }
builtin-for-each-ref.c
+2 -2
@@ -173,8 +173,8 @@ static void verify_format(const char *format)
173 */
174 static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned long *sz, int *eaten)
175 {
176 - char type[20];
177 - void *buf = read_sha1_file(sha1, type, sz);
176 + enum object_type type;
177 + void *buf = read_sha1_file(sha1, &type, sz);
178
179 if (buf)
180 *obj = parse_object_buffer(sha1, type, *sz, buf, eaten);
builtin-grep.c
+4 -4
@@ -84,11 +84,11 @@ static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char
84 {
85 unsigned long size;
86 char *data;
87 - char type[20];
87 + enum object_type type;
88 char *to_free = NULL;
89 int hit;
90
91 - data = read_sha1_file(sha1, type, &size);
91 + data = read_sha1_file(sha1, &type, &size);
92 if (!data) {
93 error("'%s': unable to read %s", name, sha1_to_hex(sha1));
94 return 0;
@@ -380,10 +380,10 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
380 else if (S_ISREG(entry.mode))
381 hit |= grep_sha1(opt, entry.sha1, path_buf, tn_len);
382 else if (S_ISDIR(entry.mode)) {
383 - char type[20];
383 + enum object_type type;
384 struct tree_desc sub;
385 void *data;
386 - data = read_sha1_file(entry.sha1, type, &sub.size);
386 + data = read_sha1_file(entry.sha1, &type, &sub.size);
387 if (!data)
388 die("unable to read tree (%s)",
389 sha1_to_hex(entry.sha1));
builtin-log.c
+2 -2
@@ -89,8 +89,8 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
89 static int show_object(const unsigned char *sha1, int suppress_header)
90 {
91 unsigned long size;
92 - char type[20];
93 - char *buf = read_sha1_file(sha1, type, &size);
92 + enum object_type type;
93 + char *buf = read_sha1_file(sha1, &type, &size);
94 int offset = 0;
95
96 if (!buf)
builtin-pack-objects.c
+15 -17
@@ -230,8 +230,8 @@ static unsigned char *find_packed_object_name(struct packed_git *p,
230 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
231 {
232 unsigned long othersize, delta_size;
233 - char type[10];
234 - void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize);
233 + enum object_type type;
234 + void *otherbuf = read_sha1_file(entry->delta->sha1, &type, &othersize);
235 void *delta_buf;
236
237 if (!otherbuf)
@@ -375,7 +375,7 @@ static unsigned long write_object(struct sha1file *f,
375 struct object_entry *entry)
376 {
377 unsigned long size;
378 - char type[10];
378 + enum object_type type;
379 void *buf;
380 unsigned char header[10];
381 unsigned hdrlen, datalen;
@@ -416,7 +416,7 @@ static unsigned long write_object(struct sha1file *f,
416 }
417
418 if (!to_reuse) {
419 - buf = read_sha1_file(entry->sha1, type, &size);
419 + buf = read_sha1_file(entry->sha1, &type, &size);
420 if (!buf)
421 die("unable to read %s", sha1_to_hex(entry->sha1));
422 if (size != entry->size)
@@ -765,7 +765,7 @@ static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
765 struct pbase_tree_cache *ent, *nent;
766 void *data;
767 unsigned long size;
768 - char type[20];
768 + enum object_type type;
769 int neigh;
770 int my_ix = pbase_tree_cache_ix(sha1);
771 int available_ix = -1;
@@ -792,10 +792,10 @@ static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
792 /* Did not find one. Either we got a bogus request or
793 * we need to read and perhaps cache.
794 */
795 - data = read_sha1_file(sha1, type, &size);
795 + data = read_sha1_file(sha1, &type, &size);
796 if (!data)
797 return NULL;
798 - if (strcmp(type, tree_type)) {
798 + if (type != OBJ_TREE) {
799 free(data);
800 return NULL;
801 }
@@ -854,19 +854,19 @@ static void add_pbase_object(struct tree_desc *tree,
854
855 while (tree_entry(tree,&entry)) {
856 unsigned long size;
857 - char type[20];
857 + enum object_type type;
858
859 if (entry.pathlen != cmplen ||
860 memcmp(entry.path, name, cmplen) ||
861 !has_sha1_file(entry.sha1) ||
862 - sha1_object_info(entry.sha1, type, &size))
862 + (type = sha1_object_info(entry.sha1, &size)) < 0)
863 continue;
864 if (name[cmplen] != '/') {
865 unsigned hash = name_hash(fullname);
866 add_object_entry(entry.sha1, hash, 1);
867 return;
868 }
869 - if (!strcmp(type, tree_type)) {
869 + if (type == OBJ_TREE) {
870 struct tree_desc sub;
871 struct pbase_tree_cache *tree;
872 const char *down = name+cmplen+1;
@@ -978,8 +978,6 @@ static void add_preferred_base(unsigned char *sha1)
978
979 static void check_object(struct object_entry *entry)
980 {
981 - char type[20];
982 -
981 if (entry->in_pack && !entry->preferred_base) {
982 struct packed_git *p = entry->in_pack;
983 struct pack_window *w_curs = NULL;
@@ -1062,10 +1060,10 @@ static void check_object(struct object_entry *entry)
1060 /* Otherwise we would do the usual */
1061 }
1062
1065 - if (sha1_object_info(entry->sha1, type, &entry->size))
1063 + entry->type = sha1_object_info(entry->sha1, &entry->size);
1064 + if (entry->type < 0)
1065 die("unable to get type of object %s",
1066 sha1_to_hex(entry->sha1));
1068 - entry->type = type_from_string(type);
1067 }
1068
1069 static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
@@ -1195,7 +1193,7 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,
1193 struct object_entry *trg_entry = trg->entry;
1194 struct object_entry *src_entry = src->entry;
1195 unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
1198 - char type[10];
1196 + enum object_type type;
1197 void *delta_buf;
1198
1199 /* Don't bother doing diffs between different types */
@@ -1246,13 +1244,13 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,
1244
1245 /* Load data if not already done */
1246 if (!trg->data) {
1249 - trg->data = read_sha1_file(trg_entry->sha1, type, &sz);
1247 + trg->data = read_sha1_file(trg_entry->sha1, &type, &sz);
1248 if (sz != trg_size)
1249 die("object %s inconsistent object length (%lu vs %lu)",
1250 sha1_to_hex(trg_entry->sha1), sz, trg_size);
1251 }
1252 if (!src->data) {
1255 - src->data = read_sha1_file(src_entry->sha1, type, &sz);
1253 + src->data = read_sha1_file(src_entry->sha1, &type, &sz);
1254 if (sz != src_size)
1255 die("object %s inconsistent object length (%lu vs %lu)",
1256 sha1_to_hex(src_entry->sha1), sz, src_size);
builtin-prune.c
+3 -8
@@ -10,15 +10,10 @@ static int show_only;
10
11 static int prune_object(char *path, const char *filename, const unsigned char *sha1)
12 {
13 - char buf[20];
14 - const char *type;
15 -
13 if (show_only) {
17 - if (sha1_object_info(sha1, buf, NULL))
18 - type = "unknown";
19 - else
20 - type = buf;
21 - printf("%s %s\n", sha1_to_hex(sha1), type);
14 + enum object_type type = sha1_object_info(sha1, NULL);
15 + printf("%s %s\n", sha1_to_hex(sha1),
16 + (type > 0) ? typename(type) : "unknown");
17 return 0;
18 }
19 unlink(mkpath("%s/%s", path, filename));
builtin-reflog.c
+2 -2
@@ -55,8 +55,8 @@ static int tree_is_complete(const unsigned char *sha1)
55 desc.buf = tree->buffer;
56 desc.size = tree->size;
57 if (!desc.buf) {
58 - char type[20];
59 - void *data = read_sha1_file(sha1, type, &desc.size);
58 + enum object_type type;
59 + void *data = read_sha1_file(sha1, &type, &desc.size);
60 if (!data) {
61 tree->object.flags |= INCOMPLETE;
62 return 0;
builtin-unpack-objects.c
+15 -24
@@ -119,18 +119,18 @@ struct obj_info {
119
120 static struct obj_info *obj_list;
121
122 -static void added_object(unsigned nr, const char *type, void *data,
123 - unsigned long size);
122 +static void added_object(unsigned nr, enum object_type type,
123 + void *data, unsigned long size);
124
125 -static void write_object(unsigned nr, void *buf, unsigned long size,
126 - const char *type)
125 +static void write_object(unsigned nr, enum object_type type,
126 + void *buf, unsigned long size)
127 {
128 - if (write_sha1_file(buf, size, type, obj_list[nr].sha1) < 0)
128 + if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0)
129 die("failed to write object");
130 added_object(nr, type, buf, size);
131 }
132
133 -static void resolve_delta(unsigned nr, const char *type,
133 +static void resolve_delta(unsigned nr, enum object_type type,
134 void *base, unsigned long base_size,
135 void *delta, unsigned long delta_size)
136 {
@@ -143,12 +143,12 @@ static void resolve_delta(unsigned nr, const char *type,
143 if (!result)
144 die("failed to apply delta");
145 free(delta);
146 - write_object(nr, result, result_size, type);
146 + write_object(nr, type, result, result_size);
147 free(result);
148 }
149
150 -static void added_object(unsigned nr, const char *type, void *data,
151 - unsigned long size)
150 +static void added_object(unsigned nr, enum object_type type,
151 + void *data, unsigned long size)
152 {
153 struct delta_info **p = &delta_list;
154 struct delta_info *info;
@@ -167,33 +167,24 @@ static void added_object(unsigned nr, const char *type, void *data,
167 }
168 }
169
170 -static void unpack_non_delta_entry(enum object_type kind, unsigned long size,
170 +static void unpack_non_delta_entry(enum object_type type, unsigned long size,
171 unsigned nr)
172 {
173 void *buf = get_data(size);
174 - const char *type;
175 -
176 - switch (kind) {
177 - case OBJ_COMMIT: type = commit_type; break;
178 - case OBJ_TREE: type = tree_type; break;
179 - case OBJ_BLOB: type = blob_type; break;
180 - case OBJ_TAG: type = tag_type; break;
181 - default: die("bad type %d", kind);
182 - }
174 +
175 if (!dry_run && buf)
184 - write_object(nr, buf, size, type);
176 + write_object(nr, type, buf, size);
177 free(buf);
178 }
179
188 -static void unpack_delta_entry(enum object_type kind, unsigned long delta_size,
180 +static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
181 unsigned nr)
182 {
183 void *delta_data, *base;
184 unsigned long base_size;
193 - char type[20];
185 unsigned char base_sha1[20];
186
196 - if (kind == OBJ_REF_DELTA) {
187 + if (type == OBJ_REF_DELTA) {
188 hashcpy(base_sha1, fill(20));
189 use(20);
190 delta_data = get_data(delta_size);
@@ -255,7 +246,7 @@ static void unpack_delta_entry(enum object_type kind, unsigned long delta_size,
246 }
247 }
248
258 - base = read_sha1_file(base_sha1, type, &base_size);
249 + base = read_sha1_file(base_sha1, &type, &base_size);
250 if (!base) {
251 error("failed to read delta-pack base object %s",
252 sha1_to_hex(base_sha1));
cache.h
+18 -18
@@ -262,13 +262,25 @@ int adjust_shared_perm(const char *path);
262 int safe_create_leading_directories(char *path);
263 char *enter_repo(char *path, int strict);
264
265 +enum object_type {
266 + OBJ_NONE = 0,
267 + OBJ_COMMIT = 1,
268 + OBJ_TREE = 2,
269 + OBJ_BLOB = 3,
270 + OBJ_TAG = 4,
271 + /* 5 for future expansion */
272 + OBJ_OFS_DELTA = 6,
273 + OBJ_REF_DELTA = 7,
274 + OBJ_BAD,
275 +};
276 +
277 /* Read and unpack a sha1 file into memory, write memory to a sha1 file */
266 -extern int sha1_object_info(const unsigned char *, char *, unsigned long *);
267 -extern void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size);
268 -extern void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size);
278 +extern int sha1_object_info(const unsigned char *, unsigned long *);
279 +extern void * unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type, unsigned long *size);
280 +extern void * read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size);
281 extern int hash_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *sha1);
282 extern int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *return_sha1);
271 -extern int pretend_sha1_file(void *, unsigned long, const char *, unsigned char *);
283 +extern int pretend_sha1_file(void *, unsigned long, enum object_type, unsigned char *);
284
285 extern int check_sha1_signature(const unsigned char *sha1, void *buf, unsigned long size, const char *type);
286
@@ -285,18 +297,6 @@ extern int legacy_loose_object(unsigned char *);
297 extern int has_pack_file(const unsigned char *sha1);
298 extern int has_pack_index(const unsigned char *sha1);
299
288 -enum object_type {
289 - OBJ_NONE = 0,
290 - OBJ_COMMIT = 1,
291 - OBJ_TREE = 2,
292 - OBJ_BLOB = 3,
293 - OBJ_TAG = 4,
294 - /* 5 for future expansion */
295 - OBJ_OFS_DELTA = 6,
296 - OBJ_REF_DELTA = 7,
297 - OBJ_BAD,
298 -};
299 -
300 extern signed char hexval_table[256];
301 static inline unsigned int hexval(unsigned int c)
302 {
@@ -422,9 +422,9 @@ extern struct packed_git *add_packed_git(char *, int, int);
422 extern int num_packed_objects(const struct packed_git *p);
423 extern int nth_packed_object_sha1(const struct packed_git *, int, unsigned char*);
424 extern unsigned long find_pack_entry_one(const unsigned char *, struct packed_git *);
425 -extern void *unpack_entry(struct packed_git *, unsigned long, char *, unsigned long *);
425 +extern void *unpack_entry(struct packed_git *, unsigned long, enum object_type *, unsigned long *);
426 extern unsigned long unpack_object_header_gently(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);
427 -extern void packed_object_info_detail(struct packed_git *, unsigned long, char *, unsigned long *, unsigned long *, unsigned int *, unsigned char *);
427 +extern const char *packed_object_info_detail(struct packed_git *, unsigned long, unsigned long *, unsigned long *, unsigned int *, unsigned char *);
428
429 /* Dumb servers support */
430 extern int update_server_info(int);
combine-diff.c
+3 -3
@@ -92,14 +92,14 @@ struct sline {
92 static char *grab_blob(const unsigned char *sha1, unsigned long *size)
93 {
94 char *blob;
95 - char type[20];
95 + enum object_type type;
96 if (is_null_sha1(sha1)) {
97 /* deleted blob */
98 *size = 0;
99 return xcalloc(1, 1);
100 }
101 - blob = read_sha1_file(sha1, type, size);
102 - if (strcmp(type, blob_type))
101 + blob = read_sha1_file(sha1, &type, size);
102 + if (type != OBJ_BLOB)
103 die("object '%s' is not a blob!", sha1_to_hex(sha1));
104 return blob;
105 }
commit.c
+3 -3
@@ -342,18 +342,18 @@ int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
342
343 int parse_commit(struct commit *item)
344 {
345 - char type[20];
345 + enum object_type type;
346 void *buffer;
347 unsigned long size;
348 int ret;
349
350 if (item->object.parsed)
351 return 0;
352 - buffer = read_sha1_file(item->object.sha1, type, &size);
352 + buffer = read_sha1_file(item->object.sha1, &type, &size);
353 if (!buffer)
354 return error("Could not read %s",
355 sha1_to_hex(item->object.sha1));
356 - if (strcmp(type, commit_type)) {
356 + if (type != OBJ_COMMIT) {
357 free(buffer);
358 return error("Object %s not a commit",
359 sha1_to_hex(item->object.sha1));
convert-objects.c
+6 -6
@@ -284,27 +284,27 @@ static void convert_commit(void *buffer, unsigned long size, unsigned char *resu
284 static struct entry * convert_entry(unsigned char *sha1)
285 {
286 struct entry *entry = lookup_entry(sha1);
287 - char type[20];
287 + enum object_type type;
288 void *buffer, *data;
289 unsigned long size;
290
291 if (entry->converted)
292 return entry;
293 - data = read_sha1_file(sha1, type, &size);
293 + data = read_sha1_file(sha1, &type, &size);
294 if (!data)
295 die("unable to read object %s", sha1_to_hex(sha1));
296
297 buffer = xmalloc(size);
298 memcpy(buffer, data, size);
299
300 - if (!strcmp(type, blob_type)) {
300 + if (type == OBJ_BLOB) {
301 write_sha1_file(buffer, size, blob_type, entry->new_sha1);
302 - } else if (!strcmp(type, tree_type))
302 + } else if (type == OBJ_TREE)
303 convert_tree(buffer, size, entry->new_sha1);
304 - else if (!strcmp(type, commit_type))
304 + else if (type == OBJ_COMMIT)
305 convert_commit(buffer, size, entry->new_sha1);
306 else
307 - die("unknown object type '%s' in %s", type, sha1_to_hex(sha1));
307 + die("unknown object type %d in %s", type, sha1_to_hex(sha1));
308 entry->converted = 1;
309 free(buffer);
310 free(data);
diff.c
+4 -3
@@ -1430,7 +1430,7 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
1430 }
1431 }
1432 else {
1433 - char type[20];
1433 + enum object_type type;
1434 struct sha1_size_cache *e;
1435
1436 if (size_only) {
@@ -1439,11 +1439,12 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
1439 s->size = e->size;
1440 return 0;
1441 }
1442 - if (!sha1_object_info(s->sha1, type, &s->size))
1442 + type = sha1_object_info(s->sha1, &s->size);
1443 + if (type < 0)
1444 locate_size_cache(s->sha1, 0, s->size);
1445 }
1446 else {
1446 - s->data = read_sha1_file(s->sha1, type, &s->size);
1447 + s->data = read_sha1_file(s->sha1, &type, &s->size);
1448 s->should_free = 1;
1449 }
1450 }
entry.c
+3 -3
@@ -68,10 +68,10 @@ static int write_entry(struct cache_entry *ce, char *path, struct checkout *stat
68 void *new;
69 unsigned long size;
70 long wrote;
71 - char type[20];
71 + enum object_type type;
72
73 - new = read_sha1_file(ce->sha1, type, &size);
74 - if (!new || strcmp(type, blob_type)) {
73 + new = read_sha1_file(ce->sha1, &type, &size);
74 + if (!new || type != OBJ_BLOB) {
75 if (new)
76 free(new);
77 return error("git-checkout-index: unable to read sha1 file of %s (%s)",
fast-import.c
+9 -9
@@ -1008,11 +1008,11 @@ static void *gfi_unpack_entry(
1008 struct object_entry *oe,
1009 unsigned long *sizep)
1010 {
1011 - static char type[20];
1011 + enum object_type type;
1012 struct packed_git *p = all_packs[oe->pack_id];
1013 if (p == pack_data)
1014 p->pack_size = pack_size + 20;
1015 - return unpack_entry(p, oe->offset, type, sizep);
1015 + return unpack_entry(p, oe->offset, &type, sizep);
1016 }
1017
1018 static const char *get_mode(const char *str, uint16_t *modep)
@@ -1049,9 +1049,9 @@ static void load_tree(struct tree_entry *root)
1049 t->delta_depth = 0;
1050 buf = gfi_unpack_entry(myoe, &size);
1051 } else {
1052 - char type[20];
1053 - buf = read_sha1_file(sha1, type, &size);
1054 - if (!buf || strcmp(type, tree_type))
1052 + enum object_type type;
1053 + buf = read_sha1_file(sha1, &type, &size);
1054 + if (!buf || type != OBJ_TREE)
1055 die("Can't load tree %s", sha1_to_hex(sha1));
1056 }
1057
@@ -1573,7 +1573,6 @@ static void file_change_m(struct branch *b)
1573 struct object_entry *oe = oe;
1574 unsigned char sha1[20];
1575 uint16_t mode, inline_data = 0;
1576 - char type[20];
1576
1577 p = get_mode(p, &mode);
1578 if (!p)
@@ -1628,11 +1627,12 @@ static void file_change_m(struct branch *b)
1627 die("Not a blob (actually a %s): %s",
1628 command_buf.buf, typename(oe->type));
1629 } else {
1631 - if (sha1_object_info(sha1, type, NULL))
1630 + enum object_type type = sha1_object_info(sha1, NULL);
1631 + if (type < 0)
1632 die("Blob not found: %s", command_buf.buf);
1633 - if (strcmp(blob_type, type))
1633 + if (type != OBJ_BLOB)
1634 die("Not a blob (actually a %s): %s",
1635 - command_buf.buf, type);
1635 + typename(type), command_buf.buf);
1636 }
1637
1638 tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
http-push.c
+3 -3
@@ -479,7 +479,7 @@ static void start_put(struct transfer_request *request)
479 char *hex = sha1_to_hex(request->obj->sha1);
480 struct active_request_slot *slot;
481 char *posn;
482 - char type[20];
482 + enum object_type type;
483 char hdr[50];
484 void *unpacked;
485 unsigned long len;
@@ -487,8 +487,8 @@ static void start_put(struct transfer_request *request)
487 ssize_t size;
488 z_stream stream;
489
490 - unpacked = read_sha1_file(request->obj->sha1, type, &len);
491 - hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
490 + unpacked = read_sha1_file(request->obj->sha1, &type, &len);
491 + hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
492
493 /* Set it up */
494 memset(&stream, 0, sizeof(stream));
index-pack.c
+4 -6
@@ -595,25 +595,23 @@ static void fix_unresolved_deltas(int nr_unresolved)
595 struct delta_entry *d = sorted_by_pos[i];
596 void *data;
597 unsigned long size;
598 - char type[10];
599 - enum object_type obj_type;
598 + enum object_type type;
599 int j, first, last;
600
601 if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
602 continue;
604 - data = read_sha1_file(d->base.sha1, type, &size);
603 + data = read_sha1_file(d->base.sha1, &type, &size);
604 if (!data)
605 continue;
606
608 - obj_type = type_from_string(type);
607 find_delta_children(&d->base, &first, &last);
608 for (j = first; j <= last; j++) {
609 struct object_entry *child = objects + deltas[j].obj_no;
610 if (child->real_type == OBJ_REF_DELTA)
613 - resolve_delta(child, data, size, obj_type);
611 + resolve_delta(child, data, size, type);
612 }
613
616 - append_obj_to_pack(data, size, obj_type);
614 + append_obj_to_pack(data, size, type);
615 free(data);
616 if (verbose)
617 percent = display_progress(nr_resolved_deltas,
merge-file.c
+5 -5
@@ -7,12 +7,12 @@ static int fill_mmfile_blob(mmfile_t *f, struct blob *obj)
7 {
8 void *buf;
9 unsigned long size;
10 - char type[20];
10 + enum object_type type;
11
12 - buf = read_sha1_file(obj->object.sha1, type, &size);
12 + buf = read_sha1_file(obj->object.sha1, &type, &size);
13 if (!buf)
14 return -1;
15 - if (strcmp(type, blob_type))
15 + if (type != OBJ_BLOB)
16 return -1;
17 f->ptr = buf;
18 f->size = size;
@@ -86,12 +86,12 @@ void *merge_file(struct blob *base, struct blob *our, struct blob *their, unsign
86 * modified in the other branch!
87 */
88 if (!our || !their) {
89 - char type[20];
89 + enum object_type type;
90 if (base)
91 return NULL;
92 if (!our)
93 our = their;
94 - return read_sha1_file(our->object.sha1, type, size);
94 + return read_sha1_file(our->object.sha1, &type, size);
95 }
96
97 if (fill_mmfile_blob(&f1, our) < 0)
merge-recursive.c
+7 -7
@@ -560,14 +560,14 @@ static void update_file_flags(const unsigned char *sha,
560 update_wd = 0;
561
562 if (update_wd) {
563 - char type[20];
563 + enum object_type type;
564 void *buf;
565 unsigned long size;
566
567 - buf = read_sha1_file(sha, type, &size);
567 + buf = read_sha1_file(sha, &type, &size);
568 if (!buf)
569 die("cannot read object %s '%s'", sha1_to_hex(sha), path);
570 - if (strcmp(type, blob_type) != 0)
570 + if (type != OBJ_BLOB)
571 die("blob expected for %s '%s'", sha1_to_hex(sha), path);
572
573 if (S_ISREG(mode)) {
@@ -620,7 +620,7 @@ struct merge_file_info
620 static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
621 {
622 unsigned long size;
623 - char type[20];
623 + enum object_type type;
624
625 if (!hashcmp(sha1, null_sha1)) {
626 mm->ptr = xstrdup("");
@@ -628,8 +628,8 @@ static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
628 return;
629 }
630
631 - mm->ptr = read_sha1_file(sha1, type, &size);
632 - if (!mm->ptr || strcmp(type, blob_type))
631 + mm->ptr = read_sha1_file(sha1, &type, &size);
632 + if (!mm->ptr || type != OBJ_BLOB)
633 die("unable to read blob object %s", sha1_to_hex(sha1));
634 mm->size = size;
635 }
@@ -1213,7 +1213,7 @@ static int merge(struct commit *h1,
1213
1214 tree->object.parsed = 1;
1215 tree->object.type = OBJ_TREE;
1216 - pretend_sha1_file(NULL, 0, tree_type, tree->object.sha1);
1216 + pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
1217 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
1218 }
1219
merge-tree.c
+4 -4
@@ -57,11 +57,11 @@ extern void *merge_file(struct blob *, struct blob *, struct blob *, unsigned lo
57
58 static void *result(struct merge_list *entry, unsigned long *size)
59 {
60 - char type[20];
60 + enum object_type type;
61 struct blob *base, *our, *their;
62
63 if (!entry->stage)
64 - return read_sha1_file(entry->blob->object.sha1, type, size);
64 + return read_sha1_file(entry->blob->object.sha1, &type, size);
65 base = NULL;
66 if (entry->stage == 1) {
67 base = entry->blob;
@@ -80,10 +80,10 @@ static void *result(struct merge_list *entry, unsigned long *size)
80
81 static void *origin(struct merge_list *entry, unsigned long *size)
82 {
83 - char type[20];
83 + enum object_type type;
84 while (entry) {
85 if (entry->stage == 2)
86 - return read_sha1_file(entry->blob->object.sha1, type, size);
86 + return read_sha1_file(entry->blob->object.sha1, &type, size);
87 entry = entry->link;
88 }
89 return NULL;
mktag.c
+4 -4
@@ -27,13 +27,13 @@
27 static int verify_object(unsigned char *sha1, const char *expected_type)
28 {
29 int ret = -1;
30 - char type[100];
30 + enum object_type type;
31 unsigned long size;
32 - void *buffer = read_sha1_file(sha1, type, &size);
32 + void *buffer = read_sha1_file(sha1, &type, &size);
33
34 if (buffer) {
35 - if (!strcmp(type, expected_type))
36 - ret = check_sha1_signature(sha1, buffer, size, type);
35 + if (type == type_from_string(expected_type))
36 + ret = check_sha1_signature(sha1, buffer, size, expected_type);
37 free(buffer);
38 }
39 return ret;
mktree.c
+5 -4
@@ -95,7 +95,7 @@ int main(int ac, char **av)
95 int len;
96 char *ptr, *ntr;
97 unsigned mode;
98 - char type[20];
98 + enum object_type type;
99 char *path;
100
101 read_line(&sb, stdin, line_termination);
@@ -115,11 +115,12 @@ int main(int ac, char **av)
115 ntr[41] != '\t' ||
116 get_sha1_hex(ntr + 1, sha1))
117 die("input format error: %s", sb.buf);
118 - if (sha1_object_info(sha1, type, NULL))
118 + type = sha1_object_info(sha1, NULL);
119 + if (type < 0)
120 die("object %s unavailable", sha1_to_hex(sha1));
121 *ntr++ = 0; /* now at the beginning of SHA1 */
121 - if (strcmp(ptr, type))
122 - die("object type %s mismatch (%s)", ptr, type);
122 + if (type != type_from_string(ptr))
123 + die("object type %s mismatch (%s)", ptr, typename(type));
124 ntr += 41; /* at the beginning of name */
125 if (line_termination && ntr[0] == '"')
126 path = unquote_c_style(ntr, NULL);
object.c
+8 -8
@@ -158,23 +158,23 @@ struct object *lookup_unknown_object(const unsigned char *sha1)
158 return obj;
159 }
160
161 -struct object *parse_object_buffer(const unsigned char *sha1, const char *type, unsigned long size, void *buffer, int *eaten_p)
161 +struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
162 {
163 struct object *obj;
164 int eaten = 0;
165
166 - if (!strcmp(type, blob_type)) {
166 + if (type == OBJ_BLOB) {
167 struct blob *blob = lookup_blob(sha1);
168 parse_blob_buffer(blob, buffer, size);
169 obj = &blob->object;
170 - } else if (!strcmp(type, tree_type)) {
170 + } else if (type == OBJ_TREE) {
171 struct tree *tree = lookup_tree(sha1);
172 obj = &tree->object;
173 if (!tree->object.parsed) {
174 parse_tree_buffer(tree, buffer, size);
175 eaten = 1;
176 }
177 - } else if (!strcmp(type, commit_type)) {
177 + } else if (type == OBJ_COMMIT) {
178 struct commit *commit = lookup_commit(sha1);
179 parse_commit_buffer(commit, buffer, size);
180 if (!commit->buffer) {
@@ -182,7 +182,7 @@ struct object *parse_object_buffer(const unsigned char *sha1, const char *type,
182 eaten = 1;
183 }
184 obj = &commit->object;
185 - } else if (!strcmp(type, tag_type)) {
185 + } else if (type == OBJ_TAG) {
186 struct tag *tag = lookup_tag(sha1);
187 parse_tag_buffer(tag, buffer, size);
188 obj = &tag->object;
@@ -196,13 +196,13 @@ struct object *parse_object_buffer(const unsigned char *sha1, const char *type,
196 struct object *parse_object(const unsigned char *sha1)
197 {
198 unsigned long size;
199 - char type[20];
199 + enum object_type type;
200 int eaten;
201 - void *buffer = read_sha1_file(sha1, type, &size);
201 + void *buffer = read_sha1_file(sha1, &type, &size);
202
203 if (buffer) {
204 struct object *obj;
205 - if (check_sha1_signature(sha1, buffer, size, type) < 0)
205 + if (check_sha1_signature(sha1, buffer, size, typename(type)) < 0)
206 printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
207
208 obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
object.h
+1 -1
@@ -59,7 +59,7 @@ struct object *parse_object(const unsigned char *sha1);
59 * parsing it. eaten_p indicates if the object has a borrowed copy
60 * of buffer and the caller should not free() it.
61 */
62 -struct object *parse_object_buffer(const unsigned char *sha1, const char *type, unsigned long size, void *buffer, int *eaten_p);
62 +struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p);
63
64 /** Returns the object, with potentially excess memory allocated. **/
65 struct object *lookup_unknown_object(const unsigned char *sha1);
pack-check.c
+7 -7
@@ -43,7 +43,7 @@ static int verify_packfile(struct packed_git *p,
43 for (i = err = 0; i < nr_objects; i++) {
44 unsigned char sha1[20];
45 void *data;
46 - char type[20];
46 + enum object_type type;
47 unsigned long size, offset;
48
49 if (nth_packed_object_sha1(p, i, sha1))
@@ -51,13 +51,13 @@ static int verify_packfile(struct packed_git *p,
51 offset = find_pack_entry_one(sha1, p);
52 if (!offset)
53 die("internal error pack-check find-pack-entry-one");
54 - data = unpack_entry(p, offset, type, &size);
54 + data = unpack_entry(p, offset, &type, &size);
55 if (!data) {
56 err = error("cannot unpack %s from %s",
57 sha1_to_hex(sha1), p->pack_name);
58 continue;
59 }
60 - if (check_sha1_signature(sha1, data, size, type)) {
60 + if (check_sha1_signature(sha1, data, size, typename(type))) {
61 err = error("packed %s from %s is corrupt",
62 sha1_to_hex(sha1), p->pack_name);
63 free(data);
@@ -82,7 +82,7 @@ static void show_pack_info(struct packed_git *p)
82
83 for (i = 0; i < nr_objects; i++) {
84 unsigned char sha1[20], base_sha1[20];
85 - char type[20];
85 + const char *type;
86 unsigned long size;
87 unsigned long store_size;
88 unsigned long offset;
@@ -94,9 +94,9 @@ static void show_pack_info(struct packed_git *p)
94 if (!offset)
95 die("internal error pack-check find-pack-entry-one");
96
97 - packed_object_info_detail(p, offset, type, &size, &store_size,
98 - &delta_chain_length,
99 - base_sha1);
97 + type = packed_object_info_detail(p, offset, &size, &store_size,
98 + &delta_chain_length,
99 + base_sha1);
100 printf("%s ", sha1_to_hex(sha1));
101 if (!delta_chain_length)
102 printf("%-6s %lu %lu\n", type, size, offset);
read-cache.c
+2 -2
@@ -72,7 +72,7 @@ static int ce_compare_link(struct cache_entry *ce, unsigned long expected_size)
72 char *target;
73 void *buffer;
74 unsigned long size;
75 - char type[10];
75 + enum object_type type;
76 int len;
77
78 target = xmalloc(expected_size);
@@ -81,7 +81,7 @@ static int ce_compare_link(struct cache_entry *ce, unsigned long expected_size)
81 free(target);
82 return -1;
83 }
84 - buffer = read_sha1_file(ce->sha1, type, &size);
84 + buffer = read_sha1_file(ce->sha1, &type, &size);
85 if (!buffer) {
86 free(target);
87 return -1;
sha1_file.c
+82 -90
@@ -983,26 +983,27 @@ static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size
983 * too permissive for what we want to check. So do an anal
984 * object header parse by hand.
985 */
986 -static int parse_sha1_header(char *hdr, char *type, unsigned long *sizep)
986 +static int parse_sha1_header(const char *hdr, unsigned long *sizep)
987 {
988 + char type[10];
989 int i;
990 unsigned long size;
991
992 /*
993 * The type can be at most ten bytes (including the
994 * terminating '\0' that we add), and is followed by
994 - * a space.
995 + * a space.
996 */
996 - i = 10;
997 + i = 0;
998 for (;;) {
999 char c = *hdr++;
1000 if (c == ' ')
1001 break;
1001 - if (!--i)
1002 + type[i++] = c;
1003 + if (i >= sizeof(type))
1004 return -1;
1003 - *type++ = c;
1005 }
1005 - *type = 0;
1006 + type[i] = 0;
1007
1008 /*
1009 * The length must follow immediately, and be in canonical
@@ -1025,17 +1026,17 @@ static int parse_sha1_header(char *hdr, char *type, unsigned long *sizep)
1026 /*
1027 * The length must be followed by a zero byte
1028 */
1028 - return *hdr ? -1 : 0;
1029 + return *hdr ? -1 : type_from_string(type);
1030 }
1031
1031 -void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
1032 +void * unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type, unsigned long *size)
1033 {
1034 int ret;
1035 z_stream stream;
1036 char hdr[8192];
1037
1038 ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
1038 - if (ret < Z_OK || parse_sha1_header(hdr, type, size) < 0)
1039 + if (ret < Z_OK || (*type = parse_sha1_header(hdr, size)) < 0)
1040 return NULL;
1041
1042 return unpack_sha1_rest(&stream, hdr, *size);
@@ -1044,7 +1045,7 @@ void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned l
1045 static unsigned long get_delta_base(struct packed_git *p,
1046 struct pack_window **w_curs,
1047 unsigned long *curpos,
1047 - enum object_type kind,
1048 + enum object_type type,
1049 unsigned long delta_obj_offset)
1050 {
1051 unsigned char *base_info = use_pack(p, w_curs, *curpos, NULL);
@@ -1056,7 +1057,7 @@ static unsigned long get_delta_base(struct packed_git *p,
1057 * that is assured. An OFS_DELTA longer than the hash size
1058 * is stupid, as then a REF_DELTA would be smaller to store.
1059 */
1059 - if (kind == OBJ_OFS_DELTA) {
1060 + if (type == OBJ_OFS_DELTA) {
1061 unsigned used = 0;
1062 unsigned char c = base_info[used++];
1063 base_offset = c & 127;
@@ -1071,7 +1072,7 @@ static unsigned long get_delta_base(struct packed_git *p,
1072 if (base_offset >= delta_obj_offset)
1073 die("delta base offset out of bound");
1074 *curpos += used;
1074 - } else if (kind == OBJ_REF_DELTA) {
1075 + } else if (type == OBJ_REF_DELTA) {
1076 /* The base entry _must_ be in the same pack */
1077 base_offset = find_pack_entry_one(base_info, p);
1078 if (!base_offset)
@@ -1085,28 +1086,25 @@ static unsigned long get_delta_base(struct packed_git *p,
1086
1087 /* forward declaration for a mutually recursive function */
1088 static int packed_object_info(struct packed_git *p, unsigned long offset,
1088 - char *type, unsigned long *sizep);
1089 + unsigned long *sizep);
1090
1091 static int packed_delta_info(struct packed_git *p,
1092 struct pack_window **w_curs,
1093 unsigned long curpos,
1093 - enum object_type kind,
1094 + enum object_type type,
1095 unsigned long obj_offset,
1095 - char *type,
1096 unsigned long *sizep)
1097 {
1098 unsigned long base_offset;
1099
1100 - base_offset = get_delta_base(p, w_curs, &curpos, kind, obj_offset);
1100 + base_offset = get_delta_base(p, w_curs, &curpos, type, obj_offset);
1101 + type = packed_object_info(p, base_offset, NULL);
1102
1103 /* We choose to only get the type of the base object and
1104 * ignore potentially corrupt pack file that expects the delta
1105 * based on a base with a wrong size. This saves tons of
1106 * inflate() calls.
1107 */
1107 - if (packed_object_info(p, base_offset, type, NULL))
1108 - die("cannot get info for delta-pack base");
1109 -
1108 if (sizep) {
1109 const unsigned char *data;
1110 unsigned char delta_head[20], *in;
@@ -1141,7 +1139,8 @@ static int packed_delta_info(struct packed_git *p,
1139 /* Read the result size */
1140 *sizep = get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
1141 }
1144 - return 0;
1142 +
1143 + return type;
1144 }
1145
1146 static int unpack_object_header(struct packed_git *p,
@@ -1169,38 +1168,36 @@ static int unpack_object_header(struct packed_git *p,
1168 return type;
1169 }
1170
1172 -void packed_object_info_detail(struct packed_git *p,
1173 - unsigned long obj_offset,
1174 - char *type,
1175 - unsigned long *size,
1176 - unsigned long *store_size,
1177 - unsigned int *delta_chain_length,
1178 - unsigned char *base_sha1)
1171 +const char *packed_object_info_detail(struct packed_git *p,
1172 + unsigned long obj_offset,
1173 + unsigned long *size,
1174 + unsigned long *store_size,
1175 + unsigned int *delta_chain_length,
1176 + unsigned char *base_sha1)
1177 {
1178 struct pack_window *w_curs = NULL;
1179 unsigned long curpos, dummy;
1180 unsigned char *next_sha1;
1183 - enum object_type kind;
1181 + enum object_type type;
1182
1183 *delta_chain_length = 0;
1184 curpos = obj_offset;
1187 - kind = unpack_object_header(p, &w_curs, &curpos, size);
1185 + type = unpack_object_header(p, &w_curs, &curpos, size);
1186
1187 for (;;) {
1190 - switch (kind) {
1188 + switch (type) {
1189 default:
1190 die("pack %s contains unknown object type %d",
1193 - p->pack_name, kind);
1191 + p->pack_name, type);
1192 case OBJ_COMMIT:
1193 case OBJ_TREE:
1194 case OBJ_BLOB:
1195 case OBJ_TAG:
1198 - strcpy(type, typename(kind));
1196 *store_size = 0; /* notyet */
1197 unuse_pack(&w_curs);
1201 - return;
1198 + return typename(type);
1199 case OBJ_OFS_DELTA:
1203 - obj_offset = get_delta_base(p, &w_curs, &curpos, kind, obj_offset);
1200 + obj_offset = get_delta_base(p, &w_curs, &curpos, type, obj_offset);
1201 if (*delta_chain_length == 0) {
1202 /* TODO: find base_sha1 as pointed by curpos */
1203 }
@@ -1214,39 +1211,38 @@ void packed_object_info_detail(struct packed_git *p,
1211 }
1212 (*delta_chain_length)++;
1213 curpos = obj_offset;
1217 - kind = unpack_object_header(p, &w_curs, &curpos, &dummy);
1214 + type = unpack_object_header(p, &w_curs, &curpos, &dummy);
1215 }
1216 }
1217
1218 static int packed_object_info(struct packed_git *p, unsigned long obj_offset,
1222 - char *type, unsigned long *sizep)
1219 + unsigned long *sizep)
1220 {
1221 struct pack_window *w_curs = NULL;
1222 unsigned long size, curpos = obj_offset;
1226 - enum object_type kind;
1223 + enum object_type type;
1224
1228 - kind = unpack_object_header(p, &w_curs, &curpos, &size);
1225 + type = unpack_object_header(p, &w_curs, &curpos, &size);
1226
1230 - switch (kind) {
1227 + switch (type) {
1228 case OBJ_OFS_DELTA:
1229 case OBJ_REF_DELTA:
1233 - packed_delta_info(p, &w_curs, curpos, kind,
1234 - obj_offset, type, sizep);
1230 + type = packed_delta_info(p, &w_curs, curpos,
1231 + type, obj_offset, sizep);
1232 break;
1233 case OBJ_COMMIT:
1234 case OBJ_TREE:
1235 case OBJ_BLOB:
1236 case OBJ_TAG:
1240 - strcpy(type, typename(kind));
1237 if (sizep)
1238 *sizep = size;
1239 break;
1240 default:
1241 die("pack %s contains unknown object type %d",
1246 - p->pack_name, kind);
1242 + p->pack_name, type);
1243 }
1244 unuse_pack(&w_curs);
1249 - return 0;
1245 + return type;
1246 }
1247
1248 static void *unpack_compressed_entry(struct packed_git *p,
@@ -1284,15 +1280,14 @@ static void *unpack_delta_entry(struct packed_git *p,
1280 struct pack_window **w_curs,
1281 unsigned long curpos,
1282 unsigned long delta_size,
1287 - enum object_type kind,
1283 unsigned long obj_offset,
1289 - char *type,
1284 + enum object_type *type,
1285 unsigned long *sizep)
1286 {
1287 void *delta_data, *result, *base;
1293 - unsigned long result_size, base_size, base_offset;
1288 + unsigned long base_size, base_offset;
1289
1295 - base_offset = get_delta_base(p, w_curs, &curpos, kind, obj_offset);
1290 + base_offset = get_delta_base(p, w_curs, &curpos, *type, obj_offset);
1291 base = unpack_entry(p, base_offset, type, &base_size);
1292 if (!base)
1293 die("failed to read delta base object at %lu from %s",
@@ -1301,43 +1296,39 @@ static void *unpack_delta_entry(struct packed_git *p,
1296 delta_data = unpack_compressed_entry(p, w_curs, curpos, delta_size);
1297 result = patch_delta(base, base_size,
1298 delta_data, delta_size,
1304 - &result_size);
1299 + sizep);
1300 if (!result)
1301 die("failed to apply delta");
1302 free(delta_data);
1303 free(base);
1309 - *sizep = result_size;
1304 return result;
1305 }
1306
1307 void *unpack_entry(struct packed_git *p, unsigned long obj_offset,
1314 - char *type, unsigned long *sizep)
1308 + enum object_type *type, unsigned long *sizep)
1309 {
1310 struct pack_window *w_curs = NULL;
1317 - unsigned long size, curpos = obj_offset;
1318 - enum object_type kind;
1319 - void *retval;
1311 + unsigned long curpos = obj_offset;
1312 + void *data;
1313
1321 - kind = unpack_object_header(p, &w_curs, &curpos, &size);
1322 - switch (kind) {
1314 + *type = unpack_object_header(p, &w_curs, &curpos, sizep);
1315 + switch (*type) {
1316 case OBJ_OFS_DELTA:
1317 case OBJ_REF_DELTA:
1325 - retval = unpack_delta_entry(p, &w_curs, curpos, size,
1326 - kind, obj_offset, type, sizep);
1318 + data = unpack_delta_entry(p, &w_curs, curpos, *sizep,
1319 + obj_offset, type, sizep);
1320 break;
1321 case OBJ_COMMIT:
1322 case OBJ_TREE:
1323 case OBJ_BLOB:
1324 case OBJ_TAG:
1332 - strcpy(type, typename(kind));
1333 - *sizep = size;
1334 - retval = unpack_compressed_entry(p, &w_curs, curpos, size);
1325 + data = unpack_compressed_entry(p, &w_curs, curpos, *sizep);
1326 break;
1327 default:
1337 - die("unknown object type %i in %s", kind, p->pack_name);
1328 + die("unknown object type %i in %s", *type, p->pack_name);
1329 }
1330 unuse_pack(&w_curs);
1340 - return retval;
1331 + return data;
1332 }
1333
1334 int num_packed_objects(const struct packed_git *p)
@@ -1444,10 +1435,10 @@ struct packed_git *find_sha1_pack(const unsigned char *sha1,
1435 return p;
1436 }
1437 return NULL;
1447 -
1438 +
1439 }
1440
1450 -static int sha1_loose_object_info(const unsigned char *sha1, char *type, unsigned long *sizep)
1441 +static int sha1_loose_object_info(const unsigned char *sha1, unsigned long *sizep)
1442 {
1443 int status;
1444 unsigned long mapsize, size;
@@ -1461,31 +1452,29 @@ static int sha1_loose_object_info(const unsigned char *sha1, char *type, unsigne
1452 if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
1453 status = error("unable to unpack %s header",
1454 sha1_to_hex(sha1));
1464 - else if (parse_sha1_header(hdr, type, &size) < 0)
1455 + else if ((status = parse_sha1_header(hdr, &size)) < 0)
1456 status = error("unable to parse %s header", sha1_to_hex(sha1));
1466 - else {
1467 - status = 0;
1468 - if (sizep)
1469 - *sizep = size;
1470 - }
1457 + else if (sizep)
1458 + *sizep = size;
1459 inflateEnd(&stream);
1460 munmap(map, mapsize);
1461 return status;
1462 }
1463
1476 -int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep)
1464 +int sha1_object_info(const unsigned char *sha1, unsigned long *sizep)
1465 {
1466 struct pack_entry e;
1467
1468 if (!find_pack_entry(sha1, &e, NULL)) {
1469 reprepare_packed_git();
1470 if (!find_pack_entry(sha1, &e, NULL))
1483 - return sha1_loose_object_info(sha1, type, sizep);
1471 + return sha1_loose_object_info(sha1, sizep);
1472 }
1485 - return packed_object_info(e.p, e.offset, type, sizep);
1473 + return packed_object_info(e.p, e.offset, sizep);
1474 }
1475
1488 -static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned long *size)
1476 +static void *read_packed_sha1(const unsigned char *sha1,
1477 + enum object_type *type, unsigned long *size)
1478 {
1479 struct pack_entry e;
1480
@@ -1503,7 +1492,7 @@ static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned lo
1492 */
1493 static struct cached_object {
1494 unsigned char sha1[20];
1506 - const char *type;
1495 + enum object_type type;
1496 void *buf;
1497 unsigned long size;
1498 } *cached_objects;
@@ -1521,11 +1510,12 @@ static struct cached_object *find_cached_object(const unsigned char *sha1)
1510 return NULL;
1511 }
1512
1524 -int pretend_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *sha1)
1513 +int pretend_sha1_file(void *buf, unsigned long len, enum object_type type,
1514 + unsigned char *sha1)
1515 {
1516 struct cached_object *co;
1517
1528 - hash_sha1_file(buf, len, type, sha1);
1518 + hash_sha1_file(buf, len, typename(type), sha1);
1519 if (has_sha1_file(sha1) || find_cached_object(sha1))
1520 return 0;
1521 if (cached_object_alloc <= cached_object_nr) {
@@ -1536,14 +1526,15 @@ int pretend_sha1_file(void *buf, unsigned long len, const char *type, unsigned c
1526 }
1527 co = &cached_objects[cached_object_nr++];
1528 co->size = len;
1539 - co->type = strdup(type);
1529 + co->type = type;
1530 co->buf = xmalloc(len);
1531 memcpy(co->buf, buf, len);
1532 hashcpy(co->sha1, sha1);
1533 return 0;
1534 }
1535
1546 -void *read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
1536 +void *read_sha1_file(const unsigned char *sha1, enum object_type *type,
1537 + unsigned long *size)
1538 {
1539 unsigned long mapsize;
1540 void *map, *buf;
@@ -1554,7 +1545,7 @@ void *read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
1545 buf = xmalloc(co->size + 1);
1546 memcpy(buf, co->buf, co->size);
1547 ((char*)buf)[co->size] = 0;
1557 - strcpy(type, co->type);
1548 + *type = co->type;
1549 *size = co->size;
1550 return buf;
1551 }
@@ -1573,33 +1564,34 @@ void *read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
1564 }
1565
1566 void *read_object_with_reference(const unsigned char *sha1,
1576 - const char *required_type,
1567 + const char *required_type_name,
1568 unsigned long *size,
1569 unsigned char *actual_sha1_return)
1570 {
1580 - char type[20];
1571 + enum object_type type, required_type;
1572 void *buffer;
1573 unsigned long isize;
1574 unsigned char actual_sha1[20];
1575
1576 + required_type = type_from_string(required_type_name);
1577 hashcpy(actual_sha1, sha1);
1578 while (1) {
1579 int ref_length = -1;
1580 const char *ref_type = NULL;
1581
1590 - buffer = read_sha1_file(actual_sha1, type, &isize);
1582 + buffer = read_sha1_file(actual_sha1, &type, &isize);
1583 if (!buffer)
1584 return NULL;
1593 - if (!strcmp(type, required_type)) {
1585 + if (type == required_type) {
1586 *size = isize;
1587 if (actual_sha1_return)
1588 hashcpy(actual_sha1_return, actual_sha1);
1589 return buffer;
1590 }
1591 /* Handle references */
1600 - else if (!strcmp(type, commit_type))
1592 + else if (type == OBJ_COMMIT)
1593 ref_type = "tree ";
1602 - else if (!strcmp(type, tag_type))
1594 + else if (type == OBJ_TAG)
1595 ref_type = "object ";
1596 else {
1597 free(buffer);
@@ -1841,17 +1833,17 @@ static void *repack_object(const unsigned char *sha1, unsigned long *objsize)
1833 z_stream stream;
1834 unsigned char *unpacked;
1835 unsigned long len;
1844 - char type[20];
1836 + enum object_type type;
1837 char hdr[32];
1838 int hdrlen;
1839 void *buf;
1840
1841 /* need to unpack and recompress it by itself */
1850 - unpacked = read_packed_sha1(sha1, type, &len);
1842 + unpacked = read_packed_sha1(sha1, &type, &len);
1843 if (!unpacked)
1844 error("cannot read sha1_file for %s", sha1_to_hex(sha1));
1845
1854 - hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
1846 + hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
1847
1848 /* Set it up */
1849 memset(&stream, 0, sizeof(stream));
tag.c
+3 -3
@@ -85,18 +85,18 @@ int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
85
86 int parse_tag(struct tag *item)
87 {
88 - char type[20];
88 + enum object_type type;
89 void *data;
90 unsigned long size;
91 int ret;
92
93 if (item->object.parsed)
94 return 0;
95 - data = read_sha1_file(item->object.sha1, type, &size);
95 + data = read_sha1_file(item->object.sha1, &type, &size);
96 if (!data)
97 return error("Could not read %s",
98 sha1_to_hex(item->object.sha1));
99 - if (strcmp(type, tag_type)) {
99 + if (type != OBJ_TAG) {
100 free(data);
101 return error("Object %s not a tag",
102 sha1_to_hex(item->object.sha1));
tree-diff.c
+3 -3
@@ -139,13 +139,13 @@ static void show_entry(struct diff_options *opt, const char *prefix, struct tree
139 const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
140
141 if (opt->recursive && S_ISDIR(mode)) {
142 - char type[20];
142 + enum object_type type;
143 char *newbase = malloc_base(base, path, strlen(path));
144 struct tree_desc inner;
145 void *tree;
146
147 - tree = read_sha1_file(sha1, type, &inner.size);
148 - if (!tree || strcmp(type, tree_type))
147 + tree = read_sha1_file(sha1, &type, &inner.size);
148 + if (!tree || type != OBJ_TREE)
149 die("corrupt tree sha %s", sha1_to_hex(sha1));
150
151 inner.buf = tree;
tree.c
+3 -3
@@ -190,17 +190,17 @@ int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
190
191 int parse_tree(struct tree *item)
192 {
193 - char type[20];
193 + enum object_type type;
194 void *buffer;
195 unsigned long size;
196
197 if (item->object.parsed)
198 return 0;
199 - buffer = read_sha1_file(item->object.sha1, type, &size);
199 + buffer = read_sha1_file(item->object.sha1, &type, &size);
200 if (!buffer)
201 return error("Could not read %s",
202 sha1_to_hex(item->object.sha1));
203 - if (strcmp(type, tree_type)) {
203 + if (type != OBJ_TREE) {
204 free(buffer);
205 return error("Object %s not a tree",
206 sha1_to_hex(item->object.sha1));
unpack-file.c
+3 -3
@@ -5,12 +5,12 @@ static char *create_temp_file(unsigned char *sha1)
5 {
6 static char path[50];
7 void *buf;
8 - char type[100];
8 + enum object_type type;
9 unsigned long size;
10 int fd;
11
12 - buf = read_sha1_file(sha1, type, &size);
13 - if (!buf || strcmp(type, blob_type))
12 + buf = read_sha1_file(sha1, &type, &size);
13 + if (!buf || type != OBJ_BLOB)
14 die("unable to read blob object %s", sha1_to_hex(sha1));
15
16 strcpy(path, ".merge_file_XXXXXX");