Skip to content

Commit

Permalink
Remove TYPE_* constant macros and use object_type enums consistently.
Browse files Browse the repository at this point in the history
This updates the type-enumeration constants introduced to reduce
the memory footprint of "struct object" to match the type bits
already used in the packfile format, by removing the former
(i.e. TYPE_* constant macros) and using the latter (i.e. enum
object_type) throughout the code for consistency.

Eventually we can stop passing around the "type strings"
entirely, and this will help - no confusion about two different
integer enumeration.

Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
  • Loading branch information
Linus Torvalds authored and Junio C Hamano committed Jul 13, 2006
1 parent d3ba675 commit 1974632
Show file tree
Hide file tree
Showing 22 changed files with 87 additions and 92 deletions.
6 changes: 3 additions & 3 deletions blob.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ struct blob *lookup_blob(const unsigned char *sha1)
if (!obj) {
struct blob *ret = alloc_blob_node();
created_object(sha1, &ret->object);
ret->object.type = TYPE_BLOB;
ret->object.type = OBJ_BLOB;
return ret;
}
if (!obj->type)
obj->type = TYPE_BLOB;
if (obj->type != TYPE_BLOB) {
obj->type = OBJ_BLOB;
if (obj->type != OBJ_BLOB) {
error("Object %s is a %s, not a blob",
sha1_to_hex(sha1), typename(obj->type));
return NULL;
Expand Down
6 changes: 3 additions & 3 deletions builtin-diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ int cmd_diff(int argc, const char **argv, char **envp)
obj = deref_tag(obj, NULL, 0);
if (!obj)
die("invalid object '%s' given.", name);
if (obj->type == TYPE_COMMIT)
if (obj->type == OBJ_COMMIT)
obj = &((struct commit *)obj)->tree->object;
if (obj->type == TYPE_TREE) {
if (obj->type == OBJ_TREE) {
if (ARRAY_SIZE(ent) <= ents)
die("more than %d trees given: '%s'",
(int) ARRAY_SIZE(ent), name);
Expand All @@ -297,7 +297,7 @@ int cmd_diff(int argc, const char **argv, char **envp)
ents++;
continue;
}
if (obj->type == TYPE_BLOB) {
if (obj->type == OBJ_BLOB) {
if (2 <= blobs)
die("more than two blobs given: '%s'", name);
memcpy(blob[blobs].sha1, obj->sha1, 20);
Expand Down
2 changes: 1 addition & 1 deletion builtin-fmt-merge-msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ static void shortlog(const char *name, unsigned char *sha1,
int flags = UNINTERESTING | TREECHANGE | SEEN | SHOWN | ADDED;

branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
if (!branch || branch->type != TYPE_COMMIT)
if (!branch || branch->type != OBJ_COMMIT)
return;

setup_revisions(0, NULL, rev, NULL);
Expand Down
4 changes: 2 additions & 2 deletions builtin-grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -891,9 +891,9 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
static int grep_object(struct grep_opt *opt, const char **paths,
struct object *obj, const char *name)
{
if (obj->type == TYPE_BLOB)
if (obj->type == OBJ_BLOB)
return grep_sha1(opt, obj->sha1, name);
if (obj->type == TYPE_COMMIT || obj->type == TYPE_TREE) {
if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
struct tree_desc tree;
void *data;
int hit;
Expand Down
6 changes: 3 additions & 3 deletions builtin-rev-list.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,16 @@ static void show_commit_list(struct rev_info *revs)
const char *name = pending->name;
if (obj->flags & (UNINTERESTING | SEEN))
continue;
if (obj->type == TYPE_TAG) {
if (obj->type == OBJ_TAG) {
obj->flags |= SEEN;
add_object_array(obj, name, &objects);
continue;
}
if (obj->type == TYPE_TREE) {
if (obj->type == OBJ_TREE) {
process_tree((struct tree *)obj, &objects, NULL, name);
continue;
}
if (obj->type == TYPE_BLOB) {
if (obj->type == OBJ_BLOB) {
process_blob((struct blob *)obj, &objects, NULL, name);
continue;
}
Expand Down
6 changes: 3 additions & 3 deletions commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static struct commit *check_commit(struct object *obj,
const unsigned char *sha1,
int quiet)
{
if (obj->type != TYPE_COMMIT) {
if (obj->type != OBJ_COMMIT) {
if (!quiet)
error("Object %s is a %s, not a commit",
sha1_to_hex(sha1), typename(obj->type));
Expand Down Expand Up @@ -86,11 +86,11 @@ struct commit *lookup_commit(const unsigned char *sha1)
if (!obj) {
struct commit *ret = alloc_commit_node();
created_object(sha1, &ret->object);
ret->object.type = TYPE_COMMIT;
ret->object.type = OBJ_COMMIT;
return ret;
}
if (!obj->type)
obj->type = TYPE_COMMIT;
obj->type = OBJ_COMMIT;
return check_commit(obj, sha1, 0);
}

Expand Down
2 changes: 1 addition & 1 deletion describe.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static int get_name(const char *path, const unsigned char *sha1)
* Otherwise only annotated tags are used.
*/
if (!strncmp(path, "refs/tags/", 10)) {
if (object->type == TYPE_TAG)
if (object->type == OBJ_TAG)
prio = 2;
else
prio = 1;
Expand Down
10 changes: 5 additions & 5 deletions fetch-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static int rev_list_insert_ref(const char *path, const unsigned char *sha1)
{
struct object *o = deref_tag(parse_object(sha1), path, 0);

if (o && o->type == TYPE_COMMIT)
if (o && o->type == OBJ_COMMIT)
rev_list_push((struct commit *)o, SEEN);

return 0;
Expand Down Expand Up @@ -256,14 +256,14 @@ static int mark_complete(const char *path, const unsigned char *sha1)
{
struct object *o = parse_object(sha1);

while (o && o->type == TYPE_TAG) {
while (o && o->type == OBJ_TAG) {
struct tag *t = (struct tag *) o;
if (!t->tagged)
break; /* broken repository */
o->flags |= COMPLETE;
o = parse_object(t->tagged->sha1);
}
if (o && o->type == TYPE_COMMIT) {
if (o && o->type == OBJ_COMMIT) {
struct commit *commit = (struct commit *)o;
commit->object.flags |= COMPLETE;
insert_by_date(commit, &complete);
Expand Down Expand Up @@ -357,7 +357,7 @@ static int everything_local(struct ref **refs, int nr_match, char **match)
* in sync with the other side at some time after
* that (it is OK if we guess wrong here).
*/
if (o->type == TYPE_COMMIT) {
if (o->type == OBJ_COMMIT) {
struct commit *commit = (struct commit *)o;
if (!cutoff || cutoff < commit->date)
cutoff = commit->date;
Expand All @@ -376,7 +376,7 @@ static int everything_local(struct ref **refs, int nr_match, char **match)
struct object *o = deref_tag(lookup_object(ref->old_sha1),
NULL, 0);

if (!o || o->type != TYPE_COMMIT || !(o->flags & COMPLETE))
if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
continue;

if (!(o->flags & SEEN)) {
Expand Down
8 changes: 4 additions & 4 deletions fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,20 @@ static struct object_list **process_queue_end = &process_queue;

static int process_object(struct object *obj)
{
if (obj->type == TYPE_COMMIT) {
if (obj->type == OBJ_COMMIT) {
if (process_commit((struct commit *)obj))
return -1;
return 0;
}
if (obj->type == TYPE_TREE) {
if (obj->type == OBJ_TREE) {
if (process_tree((struct tree *)obj))
return -1;
return 0;
}
if (obj->type == TYPE_BLOB) {
if (obj->type == OBJ_BLOB) {
return 0;
}
if (obj->type == TYPE_TAG) {
if (obj->type == OBJ_TAG) {
if (process_tag((struct tag *)obj))
return -1;
return 0;
Expand Down
10 changes: 5 additions & 5 deletions fsck-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,13 @@ static int fsck_sha1(unsigned char *sha1)
if (obj->flags & SEEN)
return 0;
obj->flags |= SEEN;
if (obj->type == TYPE_BLOB)
if (obj->type == OBJ_BLOB)
return 0;
if (obj->type == TYPE_TREE)
if (obj->type == OBJ_TREE)
return fsck_tree((struct tree *) obj);
if (obj->type == TYPE_COMMIT)
if (obj->type == OBJ_COMMIT)
return fsck_commit((struct commit *) obj);
if (obj->type == TYPE_TAG)
if (obj->type == OBJ_TAG)
return fsck_tag((struct tag *) obj);
/* By now, parse_object() would've returned NULL instead. */
return objerror(obj, "unknown type '%d' (internal fsck error)", obj->type);
Expand Down Expand Up @@ -472,7 +472,7 @@ static int fsck_cache_tree(struct cache_tree *it)
}
mark_reachable(obj, REACHABLE);
obj->used = 1;
if (obj->type != TYPE_TREE)
if (obj->type != OBJ_TREE)
err |= objerror(obj, "non-tree in cache-tree");
}
for (i = 0; i < it->subtree_nr; i++)
Expand Down
12 changes: 6 additions & 6 deletions http-push.c
Original file line number Diff line number Diff line change
Expand Up @@ -1784,16 +1784,16 @@ static int get_delta(struct rev_info *revs, struct remote_lock *lock)

if (obj->flags & (UNINTERESTING | SEEN))
continue;
if (obj->type == TYPE_TAG) {
if (obj->type == OBJ_TAG) {
obj->flags |= SEEN;
p = add_one_object(obj, p);
continue;
}
if (obj->type == TYPE_TREE) {
if (obj->type == OBJ_TREE) {
p = process_tree((struct tree *)obj, p, NULL, name);
continue;
}
if (obj->type == TYPE_BLOB) {
if (obj->type == OBJ_BLOB) {
p = process_blob((struct blob *)obj, p, NULL, name);
continue;
}
Expand Down Expand Up @@ -1960,12 +1960,12 @@ static int ref_newer(const unsigned char *new_sha1,
* old. Otherwise we require --force.
*/
o = deref_tag(parse_object(old_sha1), NULL, 0);
if (!o || o->type != TYPE_COMMIT)
if (!o || o->type != OBJ_COMMIT)
return 0;
old = (struct commit *) o;

o = deref_tag(parse_object(new_sha1), NULL, 0);
if (!o || o->type != TYPE_COMMIT)
if (!o || o->type != OBJ_COMMIT)
return 0;
new = (struct commit *) o;

Expand Down Expand Up @@ -2044,7 +2044,7 @@ static void add_remote_info_ref(struct remote_ls_ctx *ls)
fwrite_buffer(ref_info, 1, len, buf);
free(ref_info);

if (o->type == TYPE_TAG) {
if (o->type == OBJ_TAG) {
o = deref_tag(o, ls->dentry_name, 0);
if (o) {
len = strlen(ls->dentry_name) + 45;
Expand Down
8 changes: 4 additions & 4 deletions name-rev.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ static int name_ref(const char *path, const unsigned char *sha1)
if (tags_only && strncmp(path, "refs/tags/", 10))
return 0;

while (o && o->type == TYPE_TAG) {
while (o && o->type == OBJ_TAG) {
struct tag *t = (struct tag *) o;
if (!t->tagged)
break; /* broken repository */
o = parse_object(t->tagged->sha1);
deref = 1;
}
if (o && o->type == TYPE_COMMIT) {
if (o && o->type == OBJ_COMMIT) {
struct commit *commit = (struct commit *)o;

if (!strncmp(path, "refs/heads/", 11))
Expand All @@ -111,7 +111,7 @@ static const char* get_rev_name(struct object *o)
struct rev_name *n;
struct commit *c;

if (o->type != TYPE_COMMIT)
if (o->type != OBJ_COMMIT)
return "undefined";
c = (struct commit *) o;
n = c->util;
Expand Down Expand Up @@ -172,7 +172,7 @@ int main(int argc, char **argv)
}

o = deref_tag(parse_object(sha1), *argv, 0);
if (!o || o->type != TYPE_COMMIT) {
if (!o || o->type != OBJ_COMMIT) {
fprintf(stderr, "Could not get commit for %s. Skipping.\n",
*argv);
continue;
Expand Down
7 changes: 4 additions & 3 deletions object.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ struct object *get_indexed_object(unsigned int idx)
}

const char *type_names[] = {
"none", "blob", "tree", "commit", "bad"
"none", "commit", "tree", "blob", "tag",
"bad type 5", "bad type 6", "delta", "bad",
};

static unsigned int hash_obj(struct object *obj, unsigned int n)
Expand Down Expand Up @@ -88,7 +89,7 @@ void created_object(const unsigned char *sha1, struct object *obj)
{
obj->parsed = 0;
obj->used = 0;
obj->type = TYPE_NONE;
obj->type = OBJ_NONE;
obj->flags = 0;
memcpy(obj->sha1, sha1, 20);

Expand Down Expand Up @@ -131,7 +132,7 @@ struct object *lookup_unknown_object(const unsigned char *sha1)
if (!obj) {
union any_object *ret = xcalloc(1, sizeof(*ret));
created_object(sha1, &ret->object);
ret->object.type = TYPE_NONE;
ret->object.type = OBJ_NONE;
return &ret->object;
}
return obj;
Expand Down
23 changes: 15 additions & 8 deletions object.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@ struct object_array {
#define TYPE_BITS 3
#define FLAG_BITS 27

#define TYPE_NONE 0
#define TYPE_BLOB 1
#define TYPE_TREE 2
#define TYPE_COMMIT 3
#define TYPE_TAG 4
#define TYPE_BAD 5
/*
* The object type is stored in 3 bits.
*/
enum object_type {
OBJ_NONE = 0,
OBJ_COMMIT = 1,
OBJ_TREE = 2,
OBJ_BLOB = 3,
OBJ_TAG = 4,
/* 5/6 for future expansion */
OBJ_DELTA = 7,
OBJ_BAD,
};

struct object {
unsigned parsed : 1;
Expand All @@ -40,14 +47,14 @@ struct object {
};

extern int track_object_refs;
extern const char *type_names[];
extern const char *type_names[9];

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 > TYPE_TAG ? TYPE_BAD : type];
return type_names[type > OBJ_BAD ? OBJ_BAD : type];
}

extern struct object_refs *lookup_object_refs(struct object *);
Expand Down
15 changes: 1 addition & 14 deletions pack.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
#ifndef PACK_H
#define PACK_H

/*
* The packed object type is stored in 3 bits.
* The type value 0 is a reserved prefix if ever there is more than 7
* object types, or any future format extensions.
*/
enum object_type {
OBJ_EXT = 0,
OBJ_COMMIT = 1,
OBJ_TREE = 2,
OBJ_BLOB = 3,
OBJ_TAG = 4,
/* 5/6 for future expansion */
OBJ_DELTA = 7,
};
#include "object.h"

/*
* Packed object header
Expand Down
Loading

0 comments on commit 1974632

Please sign in to comment.