summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 9ba6303)
raw | patch | inline | side by side (parent: 9ba6303)
author | Nicolas Pitre <nico@cam.org> | |
Mon, 26 Feb 2007 19:55:58 +0000 (14:55 -0500) | ||
committer | Junio C Hamano <junkio@cox.net> | |
Tue, 27 Feb 2007 09:34:21 +0000 (01:34 -0800) |
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>
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>
builtin-for-each-ref.c | patch | blob | history | |
builtin-pack-objects.c | patch | blob | history | |
fast-import.c | patch | blob | history | |
index-pack.c | patch | blob | history | |
object.c | patch | blob | history | |
object.h | patch | blob | history | |
sha1_file.c | patch | blob | history |
diff --git a/builtin-for-each-ref.c b/builtin-for-each-ref.c
index ac0b9f60882ff78b06c1ec25f017dfa5d5c8425e..14fff2b1c50b730416eae83f160a39e1006b0ec0 100644 (file)
--- a/builtin-for-each-ref.c
+++ b/builtin-for-each-ref.c
if (deref)
name++;
if (!strcmp(name, "objecttype"))
- v->s = type_names[obj->type];
+ v->s = typename(obj->type);
else if (!strcmp(name, "objectsize")) {
char *s = xmalloc(40);
sprintf(s, "%lu", sz);
diff --git a/builtin-pack-objects.c b/builtin-pack-objects.c
index 426ffddfff01bbdae1132cef1a875c61ed1c80e7..1c0461891597427e1086bc81e91ecefc0627b0a3 100644 (file)
--- a/builtin-pack-objects.c
+++ b/builtin-pack-objects.c
if (sha1_object_info(entry->sha1, type, &entry->size))
die("unable to get type of object %s",
sha1_to_hex(entry->sha1));
-
- if (!strcmp(type, commit_type)) {
- entry->type = OBJ_COMMIT;
- } else if (!strcmp(type, tree_type)) {
- entry->type = OBJ_TREE;
- } else if (!strcmp(type, blob_type)) {
- entry->type = OBJ_BLOB;
- } else if (!strcmp(type, tag_type)) {
- entry->type = OBJ_TAG;
- } else
- die("unable to pack object %s of type %s",
- sha1_to_hex(entry->sha1), type);
+ entry->type = type_from_string(type);
}
static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
diff --git a/fast-import.c b/fast-import.c
index 5d040fdb001c05daab19d4b6e239512cbdb07aba..aa9eac3925f4c7bb82899b8582fbe4d4fc80f8eb 100644 (file)
--- a/fast-import.c
+++ b/fast-import.c
SHA_CTX c;
z_stream s;
- hdrlen = sprintf((char*)hdr,"%s %lu", type_names[type],
+ hdrlen = sprintf((char*)hdr,"%s %lu", typename(type),
(unsigned long)datlen) + 1;
SHA1_Init(&c);
SHA1_Update(&c, hdr, hdrlen);
} else if (oe) {
if (oe->type != OBJ_BLOB)
die("Not a blob (actually a %s): %s",
- command_buf.buf, type_names[oe->type]);
+ command_buf.buf, typename(oe->type));
} else {
if (sha1_object_info(sha1, type, NULL))
die("Blob not found: %s", command_buf.buf);
char *buf;
buf = read_object_with_reference(b->sha1,
- type_names[OBJ_COMMIT], &size, b->sha1);
+ commit_type, &size, b->sha1);
if (!buf || size < 46)
die("Not a valid commit: %s", from);
if (memcmp("tree ", buf, 5)
char *buf;
buf = read_object_with_reference(sha1,
- type_names[OBJ_COMMIT], &size, sha1);
+ commit_type, &size, sha1);
if (!buf || size < 46)
die("Not a valid commit: %s", from);
free(buf);
size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
sp = new_data.buffer;
sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
- sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
+ sp += sprintf(sp, "type %s\n", commit_type);
sp += sprintf(sp, "tag %s\n", t->name);
sp += sprintf(sp, "tagger %s\n", tagger);
*sp++ = '\n';
diff --git a/index-pack.c b/index-pack.c
index fa9a0e74892ce35d8d9a3b9e0d4e2c1c0c9e8dab..4b527854eb11f0d19e562775019497f18ae914f1 100644 (file)
--- a/index-pack.c
+++ b/index-pack.c
data = read_sha1_file(d->base.sha1, type, &size);
if (!data)
continue;
- if (!strcmp(type, blob_type)) obj_type = OBJ_BLOB;
- else if (!strcmp(type, tree_type)) obj_type = OBJ_TREE;
- else if (!strcmp(type, commit_type)) obj_type = OBJ_COMMIT;
- else if (!strcmp(type, tag_type)) obj_type = OBJ_TAG;
- else die("base object %s is of type '%s'",
- sha1_to_hex(d->base.sha1), type);
+ obj_type = type_from_string(type);
find_delta_children(&d->base, &first, &last);
for (j = first; j <= last; j++) {
struct object_entry *child = objects + deltas[j].obj_no;
diff --git a/object.c b/object.c
index de244e206375d43f2bd327a0c3058ea24d53704e..37cedc02e870151de69ba1d4087e1dbc0f1bea7c 100644 (file)
--- a/object.c
+++ b/object.c
return obj_hash[idx];
}
-const char *type_names[] = {
- "none", "commit", "tree", "blob", "tag",
- "bad type 5", "bad type 6", "delta", "bad",
+static const char *object_type_strings[] = {
+ NULL, /* OBJ_NONE = 0 */
+ "commit", /* OBJ_COMMIT = 1 */
+ "tree", /* OBJ_TREE = 2 */
+ "blob", /* OBJ_BLOB = 3 */
+ "tag", /* OBJ_TAG = 4 */
};
+const char *typename(unsigned int type)
+{
+ if (type >= ARRAY_SIZE(object_type_strings))
+ return NULL;
+ return object_type_strings[type];
+}
+
+int type_from_string(const char *str)
+{
+ int i;
+
+ for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
+ if (!strcmp(str, object_type_strings[i]))
+ return i;
+ die("invalid object type \"%s\"", str);
+}
+
static unsigned int hash_obj(struct object *obj, unsigned int n)
{
unsigned int hash = *(unsigned int *)obj->sha1;
diff --git a/object.h b/object.h
index caee733cdeda28b1679848fa9be4be870420b54a..ade4dae4470b33a93375b2105016d157c231dd6e 100644 (file)
--- a/object.h
+++ b/object.h
};
extern int track_object_refs;
-extern const char *type_names[9];
+
+extern const char *typename(unsigned int type);
+extern int type_from_string(const char *str);
extern unsigned int get_max_object_index(void);
extern struct object *get_indexed_object(unsigned int);
-
-static inline const char *typename(unsigned int type)
-{
- return type_names[type > OBJ_BAD ? OBJ_BAD : type];
-}
-
extern struct object_refs *lookup_object_refs(struct object *);
/** Internal only **/
diff --git a/sha1_file.c b/sha1_file.c
index 8b7b68c45b4b72e21d4902bcd92ecd6b3d476ee6..423ff0acc54975753065eae9d8c11120db1946d8 100644 (file)
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -952,7 +952,7 @@ static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned lon
/* And generate the fake traditional header */
stream->total_out = 1 + snprintf(buffer, bufsiz, "%s %lu",
- type_names[type], size);
+ typename(type), size);
return 0;
}
case OBJ_TREE:
case OBJ_BLOB:
case OBJ_TAG:
- strcpy(type, type_names[kind]);
+ strcpy(type, typename(kind));
*store_size = 0; /* notyet */
unuse_pack(&w_curs);
return;
case OBJ_TREE:
case OBJ_BLOB:
case OBJ_TAG:
- strcpy(type, type_names[kind]);
+ strcpy(type, typename(kind));
if (sizep)
*sizep = size;
break;
case OBJ_TREE:
case OBJ_BLOB:
case OBJ_TAG:
- strcpy(type, type_names[kind]);
+ strcpy(type, typename(kind));
*sizep = size;
retval = unpack_compressed_entry(p, &w_curs, curpos, size);
break;
@@ -1739,16 +1739,7 @@ static void setup_object_header(z_stream *stream, const char *type, unsigned lon
/* nothing */;
return;
}
- if (!strcmp(type, blob_type))
- obj_type = OBJ_BLOB;
- else if (!strcmp(type, tree_type))
- obj_type = OBJ_TREE;
- else if (!strcmp(type, commit_type))
- obj_type = OBJ_COMMIT;
- else if (!strcmp(type, tag_type))
- obj_type = OBJ_TAG;
- else
- die("trying to generate bogus object of type '%s'", type);
+ obj_type = type_from_string(type);
hdrlen = write_binary_header(stream->next_out, obj_type, len);
stream->total_out = hdrlen;
stream->next_out += hdrlen;