Skip to content

Commit

Permalink
convert object type handling from a string to a number
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Nicolas Pitre authored and Junio C Hamano committed Feb 27, 2007
1 parent df84366 commit 21666f1
Show file tree
Hide file tree
Showing 37 changed files with 266 additions and 290 deletions.
4 changes: 2 additions & 2 deletions archive-tar.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ static int write_tar_entry(const unsigned char *sha1,
static struct strbuf path;
int filenamelen = strlen(filename);
void *buffer;
char type[20];
enum object_type type;
unsigned long size;

if (!path.alloc) {
Expand All @@ -283,7 +283,7 @@ static int write_tar_entry(const unsigned char *sha1,
buffer = NULL;
size = 0;
} else {
buffer = read_sha1_file(sha1, type, &size);
buffer = read_sha1_file(sha1, &type, &size);
if (!buffer)
die("cannot read %s", sha1_to_hex(sha1));
}
Expand Down
4 changes: 2 additions & 2 deletions archive-zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ static int write_zip_entry(const unsigned char *sha1,
int pathlen;
unsigned char *out;
char *path;
char type[20];
enum object_type type;
void *buffer = NULL;
void *deflated = NULL;

Expand Down Expand Up @@ -195,7 +195,7 @@ static int write_zip_entry(const unsigned char *sha1,
if (S_ISREG(mode) && zlib_compression_level != 0)
method = 8;
result = 0;
buffer = read_sha1_file(sha1, type, &size);
buffer = read_sha1_file(sha1, &type, &size);
if (!buffer)
die("cannot read %s", sha1_to_hex(sha1));
crc = crc32(crc, buffer, size);
Expand Down
6 changes: 3 additions & 3 deletions blob.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ int parse_blob_buffer(struct blob *item, void *buffer, unsigned long size)

int parse_blob(struct blob *item)
{
char type[20];
enum object_type type;
void *buffer;
unsigned long size;
int ret;

if (item->object.parsed)
return 0;
buffer = read_sha1_file(item->object.sha1, type, &size);
buffer = read_sha1_file(item->object.sha1, &type, &size);
if (!buffer)
return error("Could not read %s",
sha1_to_hex(item->object.sha1));
if (strcmp(type, blob_type))
if (type != OBJ_BLOB)
return error("Object %s not a blob",
sha1_to_hex(item->object.sha1));
ret = parse_blob_buffer(item, buffer, size);
Expand Down
8 changes: 4 additions & 4 deletions builtin-apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -1912,11 +1912,11 @@ static int apply_binary(struct buffer_desc *desc, struct patch *patch)

if (has_sha1_file(sha1)) {
/* We already have the postimage */
char type[10];
enum object_type type;
unsigned long size;

free(desc->buffer);
desc->buffer = read_sha1_file(sha1, type, &size);
desc->buffer = read_sha1_file(sha1, &type, &size);
if (!desc->buffer)
return error("the necessary postimage %s for "
"'%s' cannot be read",
Expand Down Expand Up @@ -1972,8 +1972,8 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
buf = NULL;
if (cached) {
if (ce) {
char type[20];
buf = read_sha1_file(ce->sha1, type, &size);
enum object_type type;
buf = read_sha1_file(ce->sha1, &type, &size);
if (!buf)
return error("read of %s failed",
patch->old_name);
Expand Down
18 changes: 8 additions & 10 deletions builtin-blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ struct origin {
static char *fill_origin_blob(struct origin *o, mmfile_t *file)
{
if (!o->file.ptr) {
char type[10];
enum object_type type;
num_read_blob++;
file->ptr = read_sha1_file(o->blob_sha1, type,
file->ptr = read_sha1_file(o->blob_sha1, &type,
(unsigned long *)(&(file->size)));
o->file = *file;
}
Expand Down Expand Up @@ -263,16 +263,14 @@ static struct origin *get_origin(struct scoreboard *sb,
static int fill_blob_sha1(struct origin *origin)
{
unsigned mode;
char type[10];

if (!is_null_sha1(origin->blob_sha1))
return 0;
if (get_tree_entry(origin->commit->object.sha1,
origin->path,
origin->blob_sha1, &mode))
goto error_out;
if (sha1_object_info(origin->blob_sha1, type, NULL) ||
strcmp(type, blob_type))
if (sha1_object_info(origin->blob_sha1, NULL) != OBJ_BLOB)
goto error_out;
return 0;
error_out:
Expand Down Expand Up @@ -1322,10 +1320,10 @@ static void get_commit_info(struct commit *commit,
* we now need to populate them for output.
*/
if (!commit->buffer) {
char type[20];
enum object_type type;
unsigned long size;
commit->buffer =
read_sha1_file(commit->object.sha1, type, &size);
read_sha1_file(commit->object.sha1, &type, &size);
}
ret->author = author_buf;
get_ac_line(commit->buffer, "\nauthor ",
Expand Down Expand Up @@ -2006,7 +2004,7 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
buf[fin_size] = 0;
origin->file.ptr = buf;
origin->file.size = fin_size;
pretend_sha1_file(buf, fin_size, blob_type, origin->blob_sha1);
pretend_sha1_file(buf, fin_size, OBJ_BLOB, origin->blob_sha1);
commit->util = origin;

/*
Expand Down Expand Up @@ -2068,7 +2066,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
int show_stats = 0;
const char *revs_file = NULL;
const char *final_commit_name = NULL;
char type[10];
enum object_type type;
const char *bottomtop = NULL;
const char *contents_from = NULL;

Expand Down Expand Up @@ -2302,7 +2300,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
if (fill_blob_sha1(o))
die("no such path %s in %s", path, final_commit_name);

sb.final_buf = read_sha1_file(o->blob_sha1, type,
sb.final_buf = read_sha1_file(o->blob_sha1, &type,
&sb.final_buf_size);
}
num_read_blob++;
Expand Down
19 changes: 11 additions & 8 deletions builtin-cat-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long
int cmd_cat_file(int argc, const char **argv, const char *prefix)
{
unsigned char sha1[20];
char type[20];
enum object_type type;
void *buf;
unsigned long size;
int opt;
Expand All @@ -100,14 +100,16 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
buf = NULL;
switch (opt) {
case 't':
if (!sha1_object_info(sha1, type, NULL)) {
printf("%s\n", type);
type = sha1_object_info(sha1, NULL);
if (type > 0) {
printf("%s\n", typename(type));
return 0;
}
break;

case 's':
if (!sha1_object_info(sha1, type, &size)) {
type = sha1_object_info(sha1, &size);
if (type > 0) {
printf("%lu\n", size);
return 0;
}
Expand All @@ -117,17 +119,18 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
return !has_sha1_file(sha1);

case 'p':
if (sha1_object_info(sha1, type, NULL))
type = sha1_object_info(sha1, NULL);
if (type < 0)
die("Not a valid object name %s", argv[2]);

/* custom pretty-print here */
if (!strcmp(type, tree_type))
if (type == OBJ_TREE)
return cmd_ls_tree(2, argv + 1, NULL);

buf = read_sha1_file(sha1, type, &size);
buf = read_sha1_file(sha1, &type, &size);
if (!buf)
die("Cannot read object %s", argv[2]);
if (!strcmp(type, tag_type)) {
if (type == OBJ_TAG) {
pprint_tag(sha1, buf, size);
return 0;
}
Expand Down
7 changes: 3 additions & 4 deletions builtin-commit-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...)

static void check_valid(unsigned char *sha1, const char *expect)
{
char type[20];

if (sha1_object_info(sha1, type, NULL))
enum object_type type = sha1_object_info(sha1, NULL);
if (type < 0)
die("%s is not a valid object", sha1_to_hex(sha1));
if (expect && strcmp(type, expect))
if (expect && type != type_from_string(expect))
die("%s is not a valid '%s' object", sha1_to_hex(sha1),
expect);
}
Expand Down
4 changes: 2 additions & 2 deletions builtin-for-each-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ static void verify_format(const char *format)
*/
static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned long *sz, int *eaten)
{
char type[20];
void *buf = read_sha1_file(sha1, type, sz);
enum object_type type;
void *buf = read_sha1_file(sha1, &type, sz);

if (buf)
*obj = parse_object_buffer(sha1, type, *sz, buf, eaten);
Expand Down
8 changes: 4 additions & 4 deletions builtin-grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char
{
unsigned long size;
char *data;
char type[20];
enum object_type type;
char *to_free = NULL;
int hit;

data = read_sha1_file(sha1, type, &size);
data = read_sha1_file(sha1, &type, &size);
if (!data) {
error("'%s': unable to read %s", name, sha1_to_hex(sha1));
return 0;
Expand Down Expand Up @@ -380,10 +380,10 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
else if (S_ISREG(entry.mode))
hit |= grep_sha1(opt, entry.sha1, path_buf, tn_len);
else if (S_ISDIR(entry.mode)) {
char type[20];
enum object_type type;
struct tree_desc sub;
void *data;
data = read_sha1_file(entry.sha1, type, &sub.size);
data = read_sha1_file(entry.sha1, &type, &sub.size);
if (!data)
die("unable to read tree (%s)",
sha1_to_hex(entry.sha1));
Expand Down
4 changes: 2 additions & 2 deletions builtin-log.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
static int show_object(const unsigned char *sha1, int suppress_header)
{
unsigned long size;
char type[20];
char *buf = read_sha1_file(sha1, type, &size);
enum object_type type;
char *buf = read_sha1_file(sha1, &type, &size);
int offset = 0;

if (!buf)
Expand Down
32 changes: 15 additions & 17 deletions builtin-pack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ static unsigned char *find_packed_object_name(struct packed_git *p,
static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
{
unsigned long othersize, delta_size;
char type[10];
void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize);
enum object_type type;
void *otherbuf = read_sha1_file(entry->delta->sha1, &type, &othersize);
void *delta_buf;

if (!otherbuf)
Expand Down Expand Up @@ -375,7 +375,7 @@ static unsigned long write_object(struct sha1file *f,
struct object_entry *entry)
{
unsigned long size;
char type[10];
enum object_type type;
void *buf;
unsigned char header[10];
unsigned hdrlen, datalen;
Expand Down Expand Up @@ -416,7 +416,7 @@ static unsigned long write_object(struct sha1file *f,
}

if (!to_reuse) {
buf = read_sha1_file(entry->sha1, type, &size);
buf = read_sha1_file(entry->sha1, &type, &size);
if (!buf)
die("unable to read %s", sha1_to_hex(entry->sha1));
if (size != entry->size)
Expand Down Expand Up @@ -765,7 +765,7 @@ static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
struct pbase_tree_cache *ent, *nent;
void *data;
unsigned long size;
char type[20];
enum object_type type;
int neigh;
int my_ix = pbase_tree_cache_ix(sha1);
int available_ix = -1;
Expand All @@ -792,10 +792,10 @@ static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
/* Did not find one. Either we got a bogus request or
* we need to read and perhaps cache.
*/
data = read_sha1_file(sha1, type, &size);
data = read_sha1_file(sha1, &type, &size);
if (!data)
return NULL;
if (strcmp(type, tree_type)) {
if (type != OBJ_TREE) {
free(data);
return NULL;
}
Expand Down Expand Up @@ -854,19 +854,19 @@ static void add_pbase_object(struct tree_desc *tree,

while (tree_entry(tree,&entry)) {
unsigned long size;
char type[20];
enum object_type type;

if (entry.pathlen != cmplen ||
memcmp(entry.path, name, cmplen) ||
!has_sha1_file(entry.sha1) ||
sha1_object_info(entry.sha1, type, &size))
(type = sha1_object_info(entry.sha1, &size)) < 0)
continue;
if (name[cmplen] != '/') {
unsigned hash = name_hash(fullname);
add_object_entry(entry.sha1, hash, 1);
return;
}
if (!strcmp(type, tree_type)) {
if (type == OBJ_TREE) {
struct tree_desc sub;
struct pbase_tree_cache *tree;
const char *down = name+cmplen+1;
Expand Down Expand Up @@ -978,8 +978,6 @@ static void add_preferred_base(unsigned char *sha1)

static void check_object(struct object_entry *entry)
{
char type[20];

if (entry->in_pack && !entry->preferred_base) {
struct packed_git *p = entry->in_pack;
struct pack_window *w_curs = NULL;
Expand Down Expand Up @@ -1062,10 +1060,10 @@ static void check_object(struct object_entry *entry)
/* Otherwise we would do the usual */
}

if (sha1_object_info(entry->sha1, type, &entry->size))
entry->type = sha1_object_info(entry->sha1, &entry->size);
if (entry->type < 0)
die("unable to get type of object %s",
sha1_to_hex(entry->sha1));
entry->type = type_from_string(type);
}

static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
Expand Down Expand Up @@ -1195,7 +1193,7 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,
struct object_entry *trg_entry = trg->entry;
struct object_entry *src_entry = src->entry;
unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
char type[10];
enum object_type type;
void *delta_buf;

/* Don't bother doing diffs between different types */
Expand Down Expand Up @@ -1246,13 +1244,13 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,

/* Load data if not already done */
if (!trg->data) {
trg->data = read_sha1_file(trg_entry->sha1, type, &sz);
trg->data = read_sha1_file(trg_entry->sha1, &type, &sz);
if (sz != trg_size)
die("object %s inconsistent object length (%lu vs %lu)",
sha1_to_hex(trg_entry->sha1), sz, trg_size);
}
if (!src->data) {
src->data = read_sha1_file(src_entry->sha1, type, &sz);
src->data = read_sha1_file(src_entry->sha1, &type, &sz);
if (sz != src_size)
die("object %s inconsistent object length (%lu vs %lu)",
sha1_to_hex(src_entry->sha1), sz, src_size);
Expand Down
Loading

0 comments on commit 21666f1

Please sign in to comment.