formalize typename(), and add its reverse type_from_string()
Sometime typename() is used, sometimes type_names[] is accessed directly. Let's enforce typename() all the time which allows for validating the type. Also let's add a function to go from a name to a type and use it instead of manual memcpy() when appropriate. 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
df8436622fb553f468180b61032fe34bd6712752
7 files changed
+39
-48
builtin-for-each-ref.c
+1
-1
@@ -196,7 +196,7 @@ static void grab_common_values(struct atom_value *val, int deref, struct object
196
if (deref)
197
name++;
198
if (!strcmp(name, "objecttype"))
199
- v->s = type_names[obj->type];
199
+ v->s = typename(obj->type);
200
else if (!strcmp(name, "objectsize")) {
201
char *s = xmalloc(40);
202
sprintf(s, "%lu", sz);
builtin-pack-objects.c
+1
-12
@@ -1065,18 +1065,7 @@ static void check_object(struct object_entry *entry)
1065
if (sha1_object_info(entry->sha1, type, &entry->size))
1066
die("unable to get type of object %s",
1067
sha1_to_hex(entry->sha1));
1068
-
1069
- if (!strcmp(type, commit_type)) {
1070
- entry->type = OBJ_COMMIT;
1071
- } else if (!strcmp(type, tree_type)) {
1072
- entry->type = OBJ_TREE;
1073
- } else if (!strcmp(type, blob_type)) {
1074
- entry->type = OBJ_BLOB;
1075
- } else if (!strcmp(type, tag_type)) {
1076
- entry->type = OBJ_TAG;
1077
- } else
1078
- die("unable to pack object %s of type %s",
1079
- sha1_to_hex(entry->sha1), type);
1068
+ entry->type = type_from_string(type);
1069
}
1070
1071
static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
fast-import.c
+5
-5
@@ -891,7 +891,7 @@ static int store_object(
891
SHA_CTX c;
892
z_stream s;
893
894
- hdrlen = sprintf((char*)hdr,"%s %lu", type_names[type],
894
+ hdrlen = sprintf((char*)hdr,"%s %lu", typename(type),
895
(unsigned long)datlen) + 1;
896
SHA1_Init(&c);
897
SHA1_Update(&c, hdr, hdrlen);
@@ -1626,7 +1626,7 @@ static void file_change_m(struct branch *b)
1626
} else if (oe) {
1627
if (oe->type != OBJ_BLOB)
1628
die("Not a blob (actually a %s): %s",
1629
- command_buf.buf, type_names[oe->type]);
1629
+ command_buf.buf, typename(oe->type));
1630
} else {
1631
if (sha1_object_info(sha1, type, NULL))
1632
die("Blob not found: %s", command_buf.buf);
@@ -1711,7 +1711,7 @@ static void cmd_from(struct branch *b)
1711
char *buf;
1712
1713
buf = read_object_with_reference(b->sha1,
1714
- type_names[OBJ_COMMIT], &size, b->sha1);
1714
+ commit_type, &size, b->sha1);
1715
if (!buf || size < 46)
1716
die("Not a valid commit: %s", from);
1717
if (memcmp("tree ", buf, 5)
@@ -1895,7 +1895,7 @@ static void cmd_new_tag(void)
1895
char *buf;
1896
1897
buf = read_object_with_reference(sha1,
1898
- type_names[OBJ_COMMIT], &size, sha1);
1898
+ commit_type, &size, sha1);
1899
if (!buf || size < 46)
1900
die("Not a valid commit: %s", from);
1901
free(buf);
@@ -1916,7 +1916,7 @@ static void cmd_new_tag(void)
1916
size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
1917
sp = new_data.buffer;
1918
sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
1919
- sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
1919
+ sp += sprintf(sp, "type %s\n", commit_type);
1920
sp += sprintf(sp, "tag %s\n", t->name);
1921
sp += sprintf(sp, "tagger %s\n", tagger);
1922
*sp++ = '\n';
index-pack.c
+1
-6
@@ -604,13 +604,8 @@ static void fix_unresolved_deltas(int nr_unresolved)
604
data = read_sha1_file(d->base.sha1, type, &size);
605
if (!data)
606
continue;
607
- if (!strcmp(type, blob_type)) obj_type = OBJ_BLOB;
608
- else if (!strcmp(type, tree_type)) obj_type = OBJ_TREE;
609
- else if (!strcmp(type, commit_type)) obj_type = OBJ_COMMIT;
610
- else if (!strcmp(type, tag_type)) obj_type = OBJ_TAG;
611
- else die("base object %s is of type '%s'",
612
- sha1_to_hex(d->base.sha1), type);
607
608
+ obj_type = type_from_string(type);
609
find_delta_children(&d->base, &first, &last);
610
for (j = first; j <= last; j++) {
611
struct object_entry *child = objects + deltas[j].obj_no;
object.c
+23
-3
@@ -18,11 +18,31 @@ struct object *get_indexed_object(unsigned int idx)
18
return obj_hash[idx];
19
}
20
21
-const char *type_names[] = {
22
- "none", "commit", "tree", "blob", "tag",
23
- "bad type 5", "bad type 6", "delta", "bad",
21
+static const char *object_type_strings[] = {
22
+ NULL, /* OBJ_NONE = 0 */
23
+ "commit", /* OBJ_COMMIT = 1 */
24
+ "tree", /* OBJ_TREE = 2 */
25
+ "blob", /* OBJ_BLOB = 3 */
26
+ "tag", /* OBJ_TAG = 4 */
27
};
28
29
+const char *typename(unsigned int type)
30
+{
31
+ if (type >= ARRAY_SIZE(object_type_strings))
32
+ return NULL;
33
+ return object_type_strings[type];
34
+}
35
+
36
+int type_from_string(const char *str)
37
+{
38
+ int i;
39
+
40
+ for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
41
+ if (!strcmp(str, object_type_strings[i]))
42
+ return i;
43
+ die("invalid object type \"%s\"", str);
44
+}
45
+
46
static unsigned int hash_obj(struct object *obj, unsigned int n)
47
{
48
unsigned int hash = *(unsigned int *)obj->sha1;
object.h
+3
-7
@@ -36,16 +36,12 @@ struct object {
36
};
37
38
extern int track_object_refs;
39
-extern const char *type_names[9];
39
+
40
+extern const char *typename(unsigned int type);
41
+extern int type_from_string(const char *str);
42
43
extern unsigned int get_max_object_index(void);
44
extern struct object *get_indexed_object(unsigned int);
43
-
44
-static inline const char *typename(unsigned int type)
45
-{
46
- return type_names[type > OBJ_BAD ? OBJ_BAD : type];
47
-}
48
-
45
extern struct object_refs *lookup_object_refs(struct object *);
46
47
/** Internal only **/
sha1_file.c
+5
-14
@@ -952,7 +952,7 @@ static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned lon
952
953
/* And generate the fake traditional header */
954
stream->total_out = 1 + snprintf(buffer, bufsiz, "%s %lu",
955
- type_names[type], size);
955
+ typename(type), size);
956
return 0;
957
}
958
@@ -1195,7 +1195,7 @@ void packed_object_info_detail(struct packed_git *p,
1195
case OBJ_TREE:
1196
case OBJ_BLOB:
1197
case OBJ_TAG:
1198
- strcpy(type, type_names[kind]);
1198
+ strcpy(type, typename(kind));
1199
*store_size = 0; /* notyet */
1200
unuse_pack(&w_curs);
1201
return;
@@ -1237,7 +1237,7 @@ static int packed_object_info(struct packed_git *p, unsigned long obj_offset,
1237
case OBJ_TREE:
1238
case OBJ_BLOB:
1239
case OBJ_TAG:
1240
- strcpy(type, type_names[kind]);
1240
+ strcpy(type, typename(kind));
1241
if (sizep)
1242
*sizep = size;
1243
break;
@@ -1329,7 +1329,7 @@ void *unpack_entry(struct packed_git *p, unsigned long obj_offset,
1329
case OBJ_TREE:
1330
case OBJ_BLOB:
1331
case OBJ_TAG:
1332
- strcpy(type, type_names[kind]);
1332
+ strcpy(type, typename(kind));
1333
*sizep = size;
1334
retval = unpack_compressed_entry(p, &w_curs, curpos, size);
1335
break;
@@ -1739,16 +1739,7 @@ static void setup_object_header(z_stream *stream, const char *type, unsigned lon
1739
/* nothing */;
1740
return;
1741
}
1742
- if (!strcmp(type, blob_type))
1743
- obj_type = OBJ_BLOB;
1744
- else if (!strcmp(type, tree_type))
1745
- obj_type = OBJ_TREE;
1746
- else if (!strcmp(type, commit_type))
1747
- obj_type = OBJ_COMMIT;
1748
- else if (!strcmp(type, tag_type))
1749
- obj_type = OBJ_TAG;
1750
- else
1751
- die("trying to generate bogus object of type '%s'", type);
1742
+ obj_type = type_from_string(type);
1743
hdrlen = write_binary_header(stream->next_out, obj_type, len);
1744
stream->total_out = hdrlen;
1745
stream->next_out += hdrlen;