summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: df84366)
raw | patch | inline | side by side (parent: df84366)
author | Nicolas Pitre <nico@cam.org> | |
Mon, 26 Feb 2007 19:55:59 +0000 (14:55 -0500) | ||
committer | Junio C Hamano <junkio@cox.net> | |
Tue, 27 Feb 2007 09:34:21 +0000 (01:34 -0800) |
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>
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>
37 files changed:
diff --git a/archive-tar.c b/archive-tar.c
index 7d52a061f4f8b5d29c52912889ca0d6ee46e4ee0..d9c30d33dc286fa2926c80f647304687ba13295b 100644 (file)
--- a/archive-tar.c
+++ b/archive-tar.c
static struct strbuf path;
int filenamelen = strlen(filename);
void *buffer;
- char type[20];
+ enum object_type type;
unsigned long size;
if (!path.alloc) {
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));
}
diff --git a/archive-zip.c b/archive-zip.c
index f31b8ed8236c3fcad10ac9a84e2f508f11e1a6cf..7c4984886f14aaba5d6a71c3b9213934b45d713a 100644 (file)
--- a/archive-zip.c
+++ b/archive-zip.c
int pathlen;
unsigned char *out;
char *path;
- char type[20];
+ enum object_type type;
void *buffer = NULL;
void *deflated = NULL;
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);
index 9776beac5827a4c705eddde426817dcc40c525bc..0a9ea417b8af4b06871eae8c47168dc178e9bb29 100644 (file)
--- a/blob.c
+++ b/blob.c
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);
diff --git a/builtin-apply.c b/builtin-apply.c
index b84d747e3f3117570db5a2089c2abe67a611cb02..38f647510a13f51e63a35aaf442aeb154e34915c 100644 (file)
--- a/builtin-apply.c
+++ b/builtin-apply.c
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",
@@ -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);
diff --git a/builtin-blame.c b/builtin-blame.c
index 530b97f97d2661d53a39d2101eeadd86be2e6b00..9f7dd4e19f042ce91c4c21fb73e8e3f69ca78a76 100644 (file)
--- a/builtin-blame.c
+++ b/builtin-blame.c
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;
}
static int fill_blob_sha1(struct origin *origin)
{
unsigned mode;
- char type[10];
if (!is_null_sha1(origin->blob_sha1))
return 0;
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:
* 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 ",
@@ -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;
/*
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;
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++;
diff --git a/builtin-cat-file.c b/builtin-cat-file.c
index 6c16bfa1ae4ae6207a74a8caa95564c649c4b0dd..d61d3d5b74ae23bb1ab10b23f518fa7a85ac5481 100644 (file)
--- a/builtin-cat-file.c
+++ b/builtin-cat-file.c
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;
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;
}
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;
}
diff --git a/builtin-commit-tree.c b/builtin-commit-tree.c
index 2a818a0a2c218b5b98d82ab0d8be0c65c1edf9c0..04f61d5101f2526853ece2fc7387dd800c5b23c2 100644 (file)
--- a/builtin-commit-tree.c
+++ b/builtin-commit-tree.c
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);
}
diff --git a/builtin-for-each-ref.c b/builtin-for-each-ref.c
index 14fff2b1c50b730416eae83f160a39e1006b0ec0..b11ca928d6ac353de888f0e80d24578c5803d769 100644 (file)
--- a/builtin-for-each-ref.c
+++ b/builtin-for-each-ref.c
*/
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);
diff --git a/builtin-grep.c b/builtin-grep.c
index f35f2d023cf56cb3c75c817455cfc53a98175f54..96b70227cf073517c309dbc8b042085f1040dab5 100644 (file)
--- a/builtin-grep.c
+++ b/builtin-grep.c
@@ -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;
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));
diff --git a/builtin-log.c b/builtin-log.c
index f43790cbce39fdc465d92ec83785191772dcf1cb..1c9f7d02a8746d1d33347b7baaa85e4153a61bbb 100644 (file)
--- a/builtin-log.c
+++ b/builtin-log.c
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)
diff --git a/builtin-pack-objects.c b/builtin-pack-objects.c
index 1c0461891597427e1086bc81e91ecefc0627b0a3..8cf24f407915e6988b86a0ee40a4884dab9f5358 100644 (file)
--- a/builtin-pack-objects.c
+++ b/builtin-pack-objects.c
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)
struct object_entry *entry)
{
unsigned long size;
- char type[10];
+ enum object_type type;
void *buf;
unsigned char header[10];
unsigned hdrlen, datalen;
}
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)
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;
/* 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;
}
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;
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;
/* 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)
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 */
/* 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);
diff --git a/builtin-prune.c b/builtin-prune.c
index 6f0ba0d04d789f17e3f0136a3591a9465af7e71d..09864b7a6d52bfb0855735418486e046438ecee4 100644 (file)
--- a/builtin-prune.c
+++ b/builtin-prune.c
static int prune_object(char *path, const char *filename, const unsigned char *sha1)
{
- char buf[20];
- const char *type;
-
if (show_only) {
- if (sha1_object_info(sha1, buf, NULL))
- type = "unknown";
- else
- type = buf;
- printf("%s %s\n", sha1_to_hex(sha1), type);
+ enum object_type type = sha1_object_info(sha1, NULL);
+ printf("%s %s\n", sha1_to_hex(sha1),
+ (type > 0) ? typename(type) : "unknown");
return 0;
}
unlink(mkpath("%s/%s", path, filename));
diff --git a/builtin-reflog.c b/builtin-reflog.c
index cefb40da812819ed6c8af245f676b0365e23d3ea..186aabce042a1d6e5d83141495a854db09223bf8 100644 (file)
--- a/builtin-reflog.c
+++ b/builtin-reflog.c
desc.buf = tree->buffer;
desc.size = tree->size;
if (!desc.buf) {
- char type[20];
- void *data = read_sha1_file(sha1, type, &desc.size);
+ enum object_type type;
+ void *data = read_sha1_file(sha1, &type, &desc.size);
if (!data) {
tree->object.flags |= INCOMPLETE;
return 0;
index 8f8e898516e0ec73fde063ae17d26a26612c5c20..3956c5633448a5c29c60cad370ec7da6a8bfeb64 100644 (file)
--- a/builtin-unpack-objects.c
+++ b/builtin-unpack-objects.c
static struct obj_info *obj_list;
-static void added_object(unsigned nr, const char *type, void *data,
- unsigned long size);
+static void added_object(unsigned nr, enum object_type type,
+ void *data, unsigned long size);
-static void write_object(unsigned nr, void *buf, unsigned long size,
- const char *type)
+static void write_object(unsigned nr, enum object_type type,
+ void *buf, unsigned long size)
{
- if (write_sha1_file(buf, size, type, obj_list[nr].sha1) < 0)
+ if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0)
die("failed to write object");
added_object(nr, type, buf, size);
}
-static void resolve_delta(unsigned nr, const char *type,
+static void resolve_delta(unsigned nr, enum object_type type,
void *base, unsigned long base_size,
void *delta, unsigned long delta_size)
{
if (!result)
die("failed to apply delta");
free(delta);
- write_object(nr, result, result_size, type);
+ write_object(nr, type, result, result_size);
free(result);
}
-static void added_object(unsigned nr, const char *type, void *data,
- unsigned long size)
+static void added_object(unsigned nr, enum object_type type,
+ void *data, unsigned long size)
{
struct delta_info **p = &delta_list;
struct delta_info *info;
}
}
-static void unpack_non_delta_entry(enum object_type kind, unsigned long size,
+static void unpack_non_delta_entry(enum object_type type, unsigned long size,
unsigned nr)
{
void *buf = get_data(size);
- const char *type;
-
- switch (kind) {
- case OBJ_COMMIT: type = commit_type; break;
- case OBJ_TREE: type = tree_type; break;
- case OBJ_BLOB: type = blob_type; break;
- case OBJ_TAG: type = tag_type; break;
- default: die("bad type %d", kind);
- }
+
if (!dry_run && buf)
- write_object(nr, buf, size, type);
+ write_object(nr, type, buf, size);
free(buf);
}
-static void unpack_delta_entry(enum object_type kind, unsigned long delta_size,
+static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
unsigned nr)
{
void *delta_data, *base;
unsigned long base_size;
- char type[20];
unsigned char base_sha1[20];
- if (kind == OBJ_REF_DELTA) {
+ if (type == OBJ_REF_DELTA) {
hashcpy(base_sha1, fill(20));
use(20);
delta_data = get_data(delta_size);
}
}
- base = read_sha1_file(base_sha1, type, &base_size);
+ base = read_sha1_file(base_sha1, &type, &base_size);
if (!base) {
error("failed to read delta-pack base object %s",
sha1_to_hex(base_sha1));
index 8bbc14299d6a504c5d7d202aae5455afaa9295cc..0117b7eeddc2878c0ce7b593e06d89f276243c57 100644 (file)
--- a/cache.h
+++ b/cache.h
int safe_create_leading_directories(char *path);
char *enter_repo(char *path, int strict);
+enum object_type {
+ OBJ_NONE = 0,
+ OBJ_COMMIT = 1,
+ OBJ_TREE = 2,
+ OBJ_BLOB = 3,
+ OBJ_TAG = 4,
+ /* 5 for future expansion */
+ OBJ_OFS_DELTA = 6,
+ OBJ_REF_DELTA = 7,
+ OBJ_BAD,
+};
+
/* Read and unpack a sha1 file into memory, write memory to a sha1 file */
-extern int sha1_object_info(const unsigned char *, char *, unsigned long *);
-extern void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size);
-extern void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size);
+extern int sha1_object_info(const unsigned char *, unsigned long *);
+extern void * unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type, unsigned long *size);
+extern void * read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size);
extern int hash_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *sha1);
extern int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *return_sha1);
-extern int pretend_sha1_file(void *, unsigned long, const char *, unsigned char *);
+extern int pretend_sha1_file(void *, unsigned long, enum object_type, unsigned char *);
extern int check_sha1_signature(const unsigned char *sha1, void *buf, unsigned long size, const char *type);
extern int has_pack_file(const unsigned char *sha1);
extern int has_pack_index(const unsigned char *sha1);
-enum object_type {
- OBJ_NONE = 0,
- OBJ_COMMIT = 1,
- OBJ_TREE = 2,
- OBJ_BLOB = 3,
- OBJ_TAG = 4,
- /* 5 for future expansion */
- OBJ_OFS_DELTA = 6,
- OBJ_REF_DELTA = 7,
- OBJ_BAD,
-};
-
extern signed char hexval_table[256];
static inline unsigned int hexval(unsigned int c)
{
extern int num_packed_objects(const struct packed_git *p);
extern int nth_packed_object_sha1(const struct packed_git *, int, unsigned char*);
extern unsigned long find_pack_entry_one(const unsigned char *, struct packed_git *);
-extern void *unpack_entry(struct packed_git *, unsigned long, char *, unsigned long *);
+extern void *unpack_entry(struct packed_git *, unsigned long, enum object_type *, unsigned long *);
extern unsigned long unpack_object_header_gently(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);
-extern void packed_object_info_detail(struct packed_git *, unsigned long, char *, unsigned long *, unsigned long *, unsigned int *, unsigned char *);
+extern const char *packed_object_info_detail(struct packed_git *, unsigned long, unsigned long *, unsigned long *, unsigned int *, unsigned char *);
/* Dumb servers support */
extern int update_server_info(int);
diff --git a/combine-diff.c b/combine-diff.c
index 044633d1643a533498d3f5aba86a35a4611f6ca7..9daa0cb9a95fe6c2176a927f093d8788d0195240 100644 (file)
--- a/combine-diff.c
+++ b/combine-diff.c
static char *grab_blob(const unsigned char *sha1, unsigned long *size)
{
char *blob;
- char type[20];
+ enum object_type type;
if (is_null_sha1(sha1)) {
/* deleted blob */
*size = 0;
return xcalloc(1, 1);
}
- blob = read_sha1_file(sha1, type, size);
- if (strcmp(type, blob_type))
+ blob = read_sha1_file(sha1, &type, size);
+ if (type != OBJ_BLOB)
die("object '%s' is not a blob!", sha1_to_hex(sha1));
return blob;
}
diff --git a/commit.c b/commit.c
index 8d279b0b635f58482b5a45c6a8a0086744b0735d..da515a49738c3c8e3065da08e468a93bf976f1e4 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -342,18 +342,18 @@ int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
int parse_commit(struct commit *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, commit_type)) {
+ if (type != OBJ_COMMIT) {
free(buffer);
return error("Object %s not a commit",
sha1_to_hex(item->object.sha1));
diff --git a/convert-objects.c b/convert-objects.c
index a63013298566fd3dc21275a90ca976227de8c7f0..b5f41ae2e38c1f7856d7f060955eed5da0aea036 100644 (file)
--- a/convert-objects.c
+++ b/convert-objects.c
@@ -284,27 +284,27 @@ static void convert_commit(void *buffer, unsigned long size, unsigned char *resu
static struct entry * convert_entry(unsigned char *sha1)
{
struct entry *entry = lookup_entry(sha1);
- char type[20];
+ enum object_type type;
void *buffer, *data;
unsigned long size;
if (entry->converted)
return entry;
- data = read_sha1_file(sha1, type, &size);
+ data = read_sha1_file(sha1, &type, &size);
if (!data)
die("unable to read object %s", sha1_to_hex(sha1));
buffer = xmalloc(size);
memcpy(buffer, data, size);
- if (!strcmp(type, blob_type)) {
+ if (type == OBJ_BLOB) {
write_sha1_file(buffer, size, blob_type, entry->new_sha1);
- } else if (!strcmp(type, tree_type))
+ } else if (type == OBJ_TREE)
convert_tree(buffer, size, entry->new_sha1);
- else if (!strcmp(type, commit_type))
+ else if (type == OBJ_COMMIT)
convert_commit(buffer, size, entry->new_sha1);
else
- die("unknown object type '%s' in %s", type, sha1_to_hex(sha1));
+ die("unknown object type %d in %s", type, sha1_to_hex(sha1));
entry->converted = 1;
free(buffer);
free(data);
index d1eae7214d863d5a87735059d509afcdf60507d7..f033eb0622ec8c8fd5cecd8b0de23edcc0b24723 100644 (file)
--- a/diff.c
+++ b/diff.c
}
}
else {
- char type[20];
+ enum object_type type;
struct sha1_size_cache *e;
if (size_only) {
s->size = e->size;
return 0;
}
- if (!sha1_object_info(s->sha1, type, &s->size))
+ type = sha1_object_info(s->sha1, &s->size);
+ if (type < 0)
locate_size_cache(s->sha1, 0, s->size);
}
else {
- s->data = read_sha1_file(s->sha1, type, &s->size);
+ s->data = read_sha1_file(s->sha1, &type, &s->size);
s->should_free = 1;
}
}
index 472a9ef32191f211347a98d4ab7d06a06de043ec..21b5f2e26d77a2e7b10a6336d9e14db72b1f7e45 100644 (file)
--- a/entry.c
+++ b/entry.c
@@ -68,10 +68,10 @@ static int write_entry(struct cache_entry *ce, char *path, struct checkout *stat
void *new;
unsigned long size;
long wrote;
- char type[20];
+ enum object_type type;
- new = read_sha1_file(ce->sha1, type, &size);
- if (!new || strcmp(type, blob_type)) {
+ new = read_sha1_file(ce->sha1, &type, &size);
+ if (!new || type != OBJ_BLOB) {
if (new)
free(new);
return error("git-checkout-index: unable to read sha1 file of %s (%s)",
diff --git a/fast-import.c b/fast-import.c
index aa9eac3925f4c7bb82899b8582fbe4d4fc80f8eb..65e99c2e8b14840f70c027deb360d5d3c5292959 100644 (file)
--- a/fast-import.c
+++ b/fast-import.c
struct object_entry *oe,
unsigned long *sizep)
{
- static char type[20];
+ enum object_type type;
struct packed_git *p = all_packs[oe->pack_id];
if (p == pack_data)
p->pack_size = pack_size + 20;
- return unpack_entry(p, oe->offset, type, sizep);
+ return unpack_entry(p, oe->offset, &type, sizep);
}
static const char *get_mode(const char *str, uint16_t *modep)
t->delta_depth = 0;
buf = gfi_unpack_entry(myoe, &size);
} else {
- char type[20];
- buf = read_sha1_file(sha1, type, &size);
- if (!buf || strcmp(type, tree_type))
+ enum object_type type;
+ buf = read_sha1_file(sha1, &type, &size);
+ if (!buf || type != OBJ_TREE)
die("Can't load tree %s", sha1_to_hex(sha1));
}
struct object_entry *oe = oe;
unsigned char sha1[20];
uint16_t mode, inline_data = 0;
- char type[20];
p = get_mode(p, &mode);
if (!p)
die("Not a blob (actually a %s): %s",
command_buf.buf, typename(oe->type));
} else {
- if (sha1_object_info(sha1, type, NULL))
+ enum object_type type = sha1_object_info(sha1, NULL);
+ if (type < 0)
die("Blob not found: %s", command_buf.buf);
- if (strcmp(blob_type, type))
+ if (type != OBJ_BLOB)
die("Not a blob (actually a %s): %s",
- command_buf.buf, type);
+ typename(type), command_buf.buf);
}
tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
diff --git a/http-push.c b/http-push.c
index 9ad6fd00b0bd96580bcebc5aa9ea1713e2b801cd..0fd73dbf587559a7245a56240259de96638434b3 100644 (file)
--- a/http-push.c
+++ b/http-push.c
char *hex = sha1_to_hex(request->obj->sha1);
struct active_request_slot *slot;
char *posn;
- char type[20];
+ enum object_type type;
char hdr[50];
void *unpacked;
unsigned long len;
ssize_t size;
z_stream stream;
- unpacked = read_sha1_file(request->obj->sha1, type, &len);
- hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
+ unpacked = read_sha1_file(request->obj->sha1, &type, &len);
+ hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
/* Set it up */
memset(&stream, 0, sizeof(stream));
diff --git a/index-pack.c b/index-pack.c
index 4b527854eb11f0d19e562775019497f18ae914f1..c56458ebd57841d1fe8d0a2b1a1927d840295344 100644 (file)
--- a/index-pack.c
+++ b/index-pack.c
struct delta_entry *d = sorted_by_pos[i];
void *data;
unsigned long size;
- char type[10];
- enum object_type obj_type;
+ enum object_type type;
int j, first, last;
if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
continue;
- data = read_sha1_file(d->base.sha1, type, &size);
+ data = read_sha1_file(d->base.sha1, &type, &size);
if (!data)
continue;
- 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;
if (child->real_type == OBJ_REF_DELTA)
- resolve_delta(child, data, size, obj_type);
+ resolve_delta(child, data, size, type);
}
- append_obj_to_pack(data, size, obj_type);
+ append_obj_to_pack(data, size, type);
free(data);
if (verbose)
percent = display_progress(nr_resolved_deltas,
diff --git a/merge-file.c b/merge-file.c
index 69dc1ebbf7685f3dd30dcca64974eabba8ee137b..748d15c0e04c0d63fbe586ad59c795ddaf3dec92 100644 (file)
--- a/merge-file.c
+++ b/merge-file.c
{
void *buf;
unsigned long size;
- char type[20];
+ enum object_type type;
- buf = read_sha1_file(obj->object.sha1, type, &size);
+ buf = read_sha1_file(obj->object.sha1, &type, &size);
if (!buf)
return -1;
- if (strcmp(type, blob_type))
+ if (type != OBJ_BLOB)
return -1;
f->ptr = buf;
f->size = size;
@@ -86,12 +86,12 @@ void *merge_file(struct blob *base, struct blob *our, struct blob *their, unsign
* modified in the other branch!
*/
if (!our || !their) {
- char type[20];
+ enum object_type type;
if (base)
return NULL;
if (!our)
our = their;
- return read_sha1_file(our->object.sha1, type, size);
+ return read_sha1_file(our->object.sha1, &type, size);
}
if (fill_mmfile_blob(&f1, our) < 0)
diff --git a/merge-recursive.c b/merge-recursive.c
index 397a7ad85b4968b9376438f12d0c3c70b7aed880..87cd8709bbc99db559f3892c048ec111673a2566 100644 (file)
--- a/merge-recursive.c
+++ b/merge-recursive.c
update_wd = 0;
if (update_wd) {
- char type[20];
+ enum object_type type;
void *buf;
unsigned long size;
- buf = read_sha1_file(sha, type, &size);
+ buf = read_sha1_file(sha, &type, &size);
if (!buf)
die("cannot read object %s '%s'", sha1_to_hex(sha), path);
- if (strcmp(type, blob_type) != 0)
+ if (type != OBJ_BLOB)
die("blob expected for %s '%s'", sha1_to_hex(sha), path);
if (S_ISREG(mode)) {
static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
{
unsigned long size;
- char type[20];
+ enum object_type type;
if (!hashcmp(sha1, null_sha1)) {
mm->ptr = xstrdup("");
return;
}
- mm->ptr = read_sha1_file(sha1, type, &size);
- if (!mm->ptr || strcmp(type, blob_type))
+ mm->ptr = read_sha1_file(sha1, &type, &size);
+ if (!mm->ptr || type != OBJ_BLOB)
die("unable to read blob object %s", sha1_to_hex(sha1));
mm->size = size;
}
tree->object.parsed = 1;
tree->object.type = OBJ_TREE;
- pretend_sha1_file(NULL, 0, tree_type, tree->object.sha1);
+ pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
merged_common_ancestors = make_virtual_commit(tree, "ancestor");
}
diff --git a/merge-tree.c b/merge-tree.c
index 692ede0e3db7df66b2e5943dc58224f34c59fd8c..b2867ba7226ea6ff69876f8f20da87d200fe5fca 100644 (file)
--- a/merge-tree.c
+++ b/merge-tree.c
@@ -57,11 +57,11 @@ extern void *merge_file(struct blob *, struct blob *, struct blob *, unsigned lo
static void *result(struct merge_list *entry, unsigned long *size)
{
- char type[20];
+ enum object_type type;
struct blob *base, *our, *their;
if (!entry->stage)
- return read_sha1_file(entry->blob->object.sha1, type, size);
+ return read_sha1_file(entry->blob->object.sha1, &type, size);
base = NULL;
if (entry->stage == 1) {
base = entry->blob;
static void *origin(struct merge_list *entry, unsigned long *size)
{
- char type[20];
+ enum object_type type;
while (entry) {
if (entry->stage == 2)
- return read_sha1_file(entry->blob->object.sha1, type, size);
+ return read_sha1_file(entry->blob->object.sha1, &type, size);
entry = entry->link;
}
return NULL;
index 3448a5dde7cbd795b08db872180f03dcfb66b99a..931011121e7276513b55fc41691c65a0d924119d 100644 (file)
--- a/mktag.c
+++ b/mktag.c
static int verify_object(unsigned char *sha1, const char *expected_type)
{
int ret = -1;
- char type[100];
+ enum object_type type;
unsigned long size;
- void *buffer = read_sha1_file(sha1, type, &size);
+ void *buffer = read_sha1_file(sha1, &type, &size);
if (buffer) {
- if (!strcmp(type, expected_type))
- ret = check_sha1_signature(sha1, buffer, size, type);
+ if (type == type_from_string(expected_type))
+ ret = check_sha1_signature(sha1, buffer, size, expected_type);
free(buffer);
}
return ret;
diff --git a/mktree.c b/mktree.c
index 56205d1e0053cdcb7236bde199817a5529d3f433..d86dde89d63e21994fd2538d5ac3a21ead3a7338 100644 (file)
--- a/mktree.c
+++ b/mktree.c
int len;
char *ptr, *ntr;
unsigned mode;
- char type[20];
+ enum object_type type;
char *path;
read_line(&sb, stdin, line_termination);
ntr[41] != '\t' ||
get_sha1_hex(ntr + 1, sha1))
die("input format error: %s", sb.buf);
- if (sha1_object_info(sha1, type, NULL))
+ type = sha1_object_info(sha1, NULL);
+ if (type < 0)
die("object %s unavailable", sha1_to_hex(sha1));
*ntr++ = 0; /* now at the beginning of SHA1 */
- if (strcmp(ptr, type))
- die("object type %s mismatch (%s)", ptr, type);
+ if (type != type_from_string(ptr))
+ die("object type %s mismatch (%s)", ptr, typename(type));
ntr += 41; /* at the beginning of name */
if (line_termination && ntr[0] == '"')
path = unquote_c_style(ntr, NULL);
diff --git a/object.c b/object.c
index 37cedc02e870151de69ba1d4087e1dbc0f1bea7c..0e67af33bf2bb5b81db77b47b6836a9bc2609461 100644 (file)
--- a/object.c
+++ b/object.c
return obj;
}
-struct object *parse_object_buffer(const unsigned char *sha1, const char *type, unsigned long size, void *buffer, int *eaten_p)
+struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
{
struct object *obj;
int eaten = 0;
- if (!strcmp(type, blob_type)) {
+ if (type == OBJ_BLOB) {
struct blob *blob = lookup_blob(sha1);
parse_blob_buffer(blob, buffer, size);
obj = &blob->object;
- } else if (!strcmp(type, tree_type)) {
+ } else if (type == OBJ_TREE) {
struct tree *tree = lookup_tree(sha1);
obj = &tree->object;
if (!tree->object.parsed) {
parse_tree_buffer(tree, buffer, size);
eaten = 1;
}
- } else if (!strcmp(type, commit_type)) {
+ } else if (type == OBJ_COMMIT) {
struct commit *commit = lookup_commit(sha1);
parse_commit_buffer(commit, buffer, size);
if (!commit->buffer) {
eaten = 1;
}
obj = &commit->object;
- } else if (!strcmp(type, tag_type)) {
+ } else if (type == OBJ_TAG) {
struct tag *tag = lookup_tag(sha1);
parse_tag_buffer(tag, buffer, size);
obj = &tag->object;
@@ -196,13 +196,13 @@ struct object *parse_object_buffer(const unsigned char *sha1, const char *type,
struct object *parse_object(const unsigned char *sha1)
{
unsigned long size;
- char type[20];
+ enum object_type type;
int eaten;
- void *buffer = read_sha1_file(sha1, type, &size);
+ void *buffer = read_sha1_file(sha1, &type, &size);
if (buffer) {
struct object *obj;
- if (check_sha1_signature(sha1, buffer, size, type) < 0)
+ if (check_sha1_signature(sha1, buffer, size, typename(type)) < 0)
printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
diff --git a/object.h b/object.h
index ade4dae4470b33a93375b2105016d157c231dd6e..749a6f589059bc773966e2b6942971e6e475b28a 100644 (file)
--- a/object.h
+++ b/object.h
* parsing it. eaten_p indicates if the object has a borrowed copy
* of buffer and the caller should not free() it.
*/
-struct object *parse_object_buffer(const unsigned char *sha1, const char *type, unsigned long size, void *buffer, int *eaten_p);
+struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p);
/** Returns the object, with potentially excess memory allocated. **/
struct object *lookup_unknown_object(const unsigned char *sha1);
diff --git a/pack-check.c b/pack-check.c
index 08a9fd8dc09056c1b21cb7414f2467cffcdef077..f248ac8c7aa0efb976528e5f7941fef1f6b5bc91 100644 (file)
--- a/pack-check.c
+++ b/pack-check.c
for (i = err = 0; i < nr_objects; i++) {
unsigned char sha1[20];
void *data;
- char type[20];
+ enum object_type type;
unsigned long size, offset;
if (nth_packed_object_sha1(p, i, sha1))
offset = find_pack_entry_one(sha1, p);
if (!offset)
die("internal error pack-check find-pack-entry-one");
- data = unpack_entry(p, offset, type, &size);
+ data = unpack_entry(p, offset, &type, &size);
if (!data) {
err = error("cannot unpack %s from %s",
sha1_to_hex(sha1), p->pack_name);
continue;
}
- if (check_sha1_signature(sha1, data, size, type)) {
+ if (check_sha1_signature(sha1, data, size, typename(type))) {
err = error("packed %s from %s is corrupt",
sha1_to_hex(sha1), p->pack_name);
free(data);
for (i = 0; i < nr_objects; i++) {
unsigned char sha1[20], base_sha1[20];
- char type[20];
+ const char *type;
unsigned long size;
unsigned long store_size;
unsigned long offset;
if (!offset)
die("internal error pack-check find-pack-entry-one");
- packed_object_info_detail(p, offset, type, &size, &store_size,
- &delta_chain_length,
- base_sha1);
+ type = packed_object_info_detail(p, offset, &size, &store_size,
+ &delta_chain_length,
+ base_sha1);
printf("%s ", sha1_to_hex(sha1));
if (!delta_chain_length)
printf("%-6s %lu %lu\n", type, size, offset);
diff --git a/read-cache.c b/read-cache.c
index 605b35239674c72a272e1c0fc1fb886c4b75be46..d63746476e8922ab0105347cbfdaea1823156563 100644 (file)
--- a/read-cache.c
+++ b/read-cache.c
char *target;
void *buffer;
unsigned long size;
- char type[10];
+ enum object_type type;
int len;
target = xmalloc(expected_size);
free(target);
return -1;
}
- buffer = read_sha1_file(ce->sha1, type, &size);
+ buffer = read_sha1_file(ce->sha1, &type, &size);
if (!buffer) {
free(target);
return -1;
diff --git a/sha1_file.c b/sha1_file.c
index 423ff0acc54975753065eae9d8c11120db1946d8..38316147345ad98faa5bce94db55f67f47498d7a 100644 (file)
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -983,26 +983,27 @@ static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size
* too permissive for what we want to check. So do an anal
* object header parse by hand.
*/
-static int parse_sha1_header(char *hdr, char *type, unsigned long *sizep)
+static int parse_sha1_header(const char *hdr, unsigned long *sizep)
{
+ char type[10];
int i;
unsigned long size;
/*
* The type can be at most ten bytes (including the
* terminating '\0' that we add), and is followed by
- * a space.
+ * a space.
*/
- i = 10;
+ i = 0;
for (;;) {
char c = *hdr++;
if (c == ' ')
break;
- if (!--i)
+ type[i++] = c;
+ if (i >= sizeof(type))
return -1;
- *type++ = c;
}
- *type = 0;
+ type[i] = 0;
/*
* The length must follow immediately, and be in canonical
/*
* The length must be followed by a zero byte
*/
- return *hdr ? -1 : 0;
+ return *hdr ? -1 : type_from_string(type);
}
-void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
+void * unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type, unsigned long *size)
{
int ret;
z_stream stream;
char hdr[8192];
ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
- if (ret < Z_OK || parse_sha1_header(hdr, type, size) < 0)
+ if (ret < Z_OK || (*type = parse_sha1_header(hdr, size)) < 0)
return NULL;
return unpack_sha1_rest(&stream, hdr, *size);
@@ -1044,7 +1045,7 @@ void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned l
static unsigned long get_delta_base(struct packed_git *p,
struct pack_window **w_curs,
unsigned long *curpos,
- enum object_type kind,
+ enum object_type type,
unsigned long delta_obj_offset)
{
unsigned char *base_info = use_pack(p, w_curs, *curpos, NULL);
* that is assured. An OFS_DELTA longer than the hash size
* is stupid, as then a REF_DELTA would be smaller to store.
*/
- if (kind == OBJ_OFS_DELTA) {
+ if (type == OBJ_OFS_DELTA) {
unsigned used = 0;
unsigned char c = base_info[used++];
base_offset = c & 127;
if (base_offset >= delta_obj_offset)
die("delta base offset out of bound");
*curpos += used;
- } else if (kind == OBJ_REF_DELTA) {
+ } else if (type == OBJ_REF_DELTA) {
/* The base entry _must_ be in the same pack */
base_offset = find_pack_entry_one(base_info, p);
if (!base_offset)
/* forward declaration for a mutually recursive function */
static int packed_object_info(struct packed_git *p, unsigned long offset,
- char *type, unsigned long *sizep);
+ unsigned long *sizep);
static int packed_delta_info(struct packed_git *p,
struct pack_window **w_curs,
unsigned long curpos,
- enum object_type kind,
+ enum object_type type,
unsigned long obj_offset,
- char *type,
unsigned long *sizep)
{
unsigned long base_offset;
- base_offset = get_delta_base(p, w_curs, &curpos, kind, obj_offset);
+ base_offset = get_delta_base(p, w_curs, &curpos, type, obj_offset);
+ type = packed_object_info(p, base_offset, NULL);
/* We choose to only get the type of the base object and
* ignore potentially corrupt pack file that expects the delta
* based on a base with a wrong size. This saves tons of
* inflate() calls.
*/
- if (packed_object_info(p, base_offset, type, NULL))
- die("cannot get info for delta-pack base");
-
if (sizep) {
const unsigned char *data;
unsigned char delta_head[20], *in;
/* Read the result size */
*sizep = get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
}
- return 0;
+
+ return type;
}
static int unpack_object_header(struct packed_git *p,
return type;
}
-void packed_object_info_detail(struct packed_git *p,
- unsigned long obj_offset,
- char *type,
- unsigned long *size,
- unsigned long *store_size,
- unsigned int *delta_chain_length,
- unsigned char *base_sha1)
+const char *packed_object_info_detail(struct packed_git *p,
+ unsigned long obj_offset,
+ unsigned long *size,
+ unsigned long *store_size,
+ unsigned int *delta_chain_length,
+ unsigned char *base_sha1)
{
struct pack_window *w_curs = NULL;
unsigned long curpos, dummy;
unsigned char *next_sha1;
- enum object_type kind;
+ enum object_type type;
*delta_chain_length = 0;
curpos = obj_offset;
- kind = unpack_object_header(p, &w_curs, &curpos, size);
+ type = unpack_object_header(p, &w_curs, &curpos, size);
for (;;) {
- switch (kind) {
+ switch (type) {
default:
die("pack %s contains unknown object type %d",
- p->pack_name, kind);
+ p->pack_name, type);
case OBJ_COMMIT:
case OBJ_TREE:
case OBJ_BLOB:
case OBJ_TAG:
- strcpy(type, typename(kind));
*store_size = 0; /* notyet */
unuse_pack(&w_curs);
- return;
+ return typename(type);
case OBJ_OFS_DELTA:
- obj_offset = get_delta_base(p, &w_curs, &curpos, kind, obj_offset);
+ obj_offset = get_delta_base(p, &w_curs, &curpos, type, obj_offset);
if (*delta_chain_length == 0) {
/* TODO: find base_sha1 as pointed by curpos */
}
}
(*delta_chain_length)++;
curpos = obj_offset;
- kind = unpack_object_header(p, &w_curs, &curpos, &dummy);
+ type = unpack_object_header(p, &w_curs, &curpos, &dummy);
}
}
static int packed_object_info(struct packed_git *p, unsigned long obj_offset,
- char *type, unsigned long *sizep)
+ unsigned long *sizep)
{
struct pack_window *w_curs = NULL;
unsigned long size, curpos = obj_offset;
- enum object_type kind;
+ enum object_type type;
- kind = unpack_object_header(p, &w_curs, &curpos, &size);
+ type = unpack_object_header(p, &w_curs, &curpos, &size);
- switch (kind) {
+ switch (type) {
case OBJ_OFS_DELTA:
case OBJ_REF_DELTA:
- packed_delta_info(p, &w_curs, curpos, kind,
- obj_offset, type, sizep);
+ type = packed_delta_info(p, &w_curs, curpos,
+ type, obj_offset, sizep);
break;
case OBJ_COMMIT:
case OBJ_TREE:
case OBJ_BLOB:
case OBJ_TAG:
- strcpy(type, typename(kind));
if (sizep)
*sizep = size;
break;
default:
die("pack %s contains unknown object type %d",
- p->pack_name, kind);
+ p->pack_name, type);
}
unuse_pack(&w_curs);
- return 0;
+ return type;
}
static void *unpack_compressed_entry(struct packed_git *p,
struct pack_window **w_curs,
unsigned long curpos,
unsigned long delta_size,
- enum object_type kind,
unsigned long obj_offset,
- char *type,
+ enum object_type *type,
unsigned long *sizep)
{
void *delta_data, *result, *base;
- unsigned long result_size, base_size, base_offset;
+ unsigned long base_size, base_offset;
- base_offset = get_delta_base(p, w_curs, &curpos, kind, obj_offset);
+ base_offset = get_delta_base(p, w_curs, &curpos, *type, obj_offset);
base = unpack_entry(p, base_offset, type, &base_size);
if (!base)
die("failed to read delta base object at %lu from %s",
delta_data = unpack_compressed_entry(p, w_curs, curpos, delta_size);
result = patch_delta(base, base_size,
delta_data, delta_size,
- &result_size);
+ sizep);
if (!result)
die("failed to apply delta");
free(delta_data);
free(base);
- *sizep = result_size;
return result;
}
void *unpack_entry(struct packed_git *p, unsigned long obj_offset,
- char *type, unsigned long *sizep)
+ enum object_type *type, unsigned long *sizep)
{
struct pack_window *w_curs = NULL;
- unsigned long size, curpos = obj_offset;
- enum object_type kind;
- void *retval;
+ unsigned long curpos = obj_offset;
+ void *data;
- kind = unpack_object_header(p, &w_curs, &curpos, &size);
- switch (kind) {
+ *type = unpack_object_header(p, &w_curs, &curpos, sizep);
+ switch (*type) {
case OBJ_OFS_DELTA:
case OBJ_REF_DELTA:
- retval = unpack_delta_entry(p, &w_curs, curpos, size,
- kind, obj_offset, type, sizep);
+ data = unpack_delta_entry(p, &w_curs, curpos, *sizep,
+ obj_offset, type, sizep);
break;
case OBJ_COMMIT:
case OBJ_TREE:
case OBJ_BLOB:
case OBJ_TAG:
- strcpy(type, typename(kind));
- *sizep = size;
- retval = unpack_compressed_entry(p, &w_curs, curpos, size);
+ data = unpack_compressed_entry(p, &w_curs, curpos, *sizep);
break;
default:
- die("unknown object type %i in %s", kind, p->pack_name);
+ die("unknown object type %i in %s", *type, p->pack_name);
}
unuse_pack(&w_curs);
- return retval;
+ return data;
}
int num_packed_objects(const struct packed_git *p)
return p;
}
return NULL;
-
+
}
-static int sha1_loose_object_info(const unsigned char *sha1, char *type, unsigned long *sizep)
+static int sha1_loose_object_info(const unsigned char *sha1, unsigned long *sizep)
{
int status;
unsigned long mapsize, size;
@@ -1461,31 +1452,29 @@ static int sha1_loose_object_info(const unsigned char *sha1, char *type, unsigne
if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
status = error("unable to unpack %s header",
sha1_to_hex(sha1));
- else if (parse_sha1_header(hdr, type, &size) < 0)
+ else if ((status = parse_sha1_header(hdr, &size)) < 0)
status = error("unable to parse %s header", sha1_to_hex(sha1));
- else {
- status = 0;
- if (sizep)
- *sizep = size;
- }
+ else if (sizep)
+ *sizep = size;
inflateEnd(&stream);
munmap(map, mapsize);
return status;
}
-int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep)
+int sha1_object_info(const unsigned char *sha1, unsigned long *sizep)
{
struct pack_entry e;
if (!find_pack_entry(sha1, &e, NULL)) {
reprepare_packed_git();
if (!find_pack_entry(sha1, &e, NULL))
- return sha1_loose_object_info(sha1, type, sizep);
+ return sha1_loose_object_info(sha1, sizep);
}
- return packed_object_info(e.p, e.offset, type, sizep);
+ return packed_object_info(e.p, e.offset, sizep);
}
-static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned long *size)
+static void *read_packed_sha1(const unsigned char *sha1,
+ enum object_type *type, unsigned long *size)
{
struct pack_entry e;
@@ -1503,7 +1492,7 @@ static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned lo
*/
static struct cached_object {
unsigned char sha1[20];
- const char *type;
+ enum object_type type;
void *buf;
unsigned long size;
} *cached_objects;
return NULL;
}
-int pretend_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *sha1)
+int pretend_sha1_file(void *buf, unsigned long len, enum object_type type,
+ unsigned char *sha1)
{
struct cached_object *co;
- hash_sha1_file(buf, len, type, sha1);
+ hash_sha1_file(buf, len, typename(type), sha1);
if (has_sha1_file(sha1) || find_cached_object(sha1))
return 0;
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
}
co = &cached_objects[cached_object_nr++];
co->size = len;
- co->type = strdup(type);
+ co->type = type;
co->buf = xmalloc(len);
memcpy(co->buf, buf, len);
hashcpy(co->sha1, sha1);
return 0;
}
-void *read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
+void *read_sha1_file(const unsigned char *sha1, enum object_type *type,
+ unsigned long *size)
{
unsigned long mapsize;
void *map, *buf;
@@ -1554,7 +1545,7 @@ void *read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
buf = xmalloc(co->size + 1);
memcpy(buf, co->buf, co->size);
((char*)buf)[co->size] = 0;
- strcpy(type, co->type);
+ *type = co->type;
*size = co->size;
return buf;
}
@@ -1573,33 +1564,34 @@ void *read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
}
void *read_object_with_reference(const unsigned char *sha1,
- const char *required_type,
+ const char *required_type_name,
unsigned long *size,
unsigned char *actual_sha1_return)
{
- char type[20];
+ enum object_type type, required_type;
void *buffer;
unsigned long isize;
unsigned char actual_sha1[20];
+ required_type = type_from_string(required_type_name);
hashcpy(actual_sha1, sha1);
while (1) {
int ref_length = -1;
const char *ref_type = NULL;
- buffer = read_sha1_file(actual_sha1, type, &isize);
+ buffer = read_sha1_file(actual_sha1, &type, &isize);
if (!buffer)
return NULL;
- if (!strcmp(type, required_type)) {
+ if (type == required_type) {
*size = isize;
if (actual_sha1_return)
hashcpy(actual_sha1_return, actual_sha1);
return buffer;
}
/* Handle references */
- else if (!strcmp(type, commit_type))
+ else if (type == OBJ_COMMIT)
ref_type = "tree ";
- else if (!strcmp(type, tag_type))
+ else if (type == OBJ_TAG)
ref_type = "object ";
else {
free(buffer);
@@ -1841,17 +1833,17 @@ static void *repack_object(const unsigned char *sha1, unsigned long *objsize)
z_stream stream;
unsigned char *unpacked;
unsigned long len;
- char type[20];
+ enum object_type type;
char hdr[32];
int hdrlen;
void *buf;
/* need to unpack and recompress it by itself */
- unpacked = read_packed_sha1(sha1, type, &len);
+ unpacked = read_packed_sha1(sha1, &type, &len);
if (!unpacked)
error("cannot read sha1_file for %s", sha1_to_hex(sha1));
- hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
+ hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
/* Set it up */
memset(&stream, 0, sizeof(stream));
index 864ac1bb602b1af07301d0b5f15d31e904b343a6..30ffc17e4130249b11f1b1066ee382ba1cd9e08a 100644 (file)
--- a/tag.c
+++ b/tag.c
int parse_tag(struct tag *item)
{
- char type[20];
+ enum object_type type;
void *data;
unsigned long size;
int ret;
if (item->object.parsed)
return 0;
- data = read_sha1_file(item->object.sha1, type, &size);
+ data = read_sha1_file(item->object.sha1, &type, &size);
if (!data)
return error("Could not read %s",
sha1_to_hex(item->object.sha1));
- if (strcmp(type, tag_type)) {
+ if (type != OBJ_TAG) {
free(data);
return error("Object %s not a tag",
sha1_to_hex(item->object.sha1));
diff --git a/tree-diff.c b/tree-diff.c
index 37d235e06e2cbfbd761fd02d7e73648a14a60daf..c8275823d0eb976e95b256f2bce5497f45d5da77 100644 (file)
--- a/tree-diff.c
+++ b/tree-diff.c
@@ -139,13 +139,13 @@ static void show_entry(struct diff_options *opt, const char *prefix, struct tree
const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
if (opt->recursive && S_ISDIR(mode)) {
- char type[20];
+ enum object_type type;
char *newbase = malloc_base(base, path, strlen(path));
struct tree_desc inner;
void *tree;
- tree = read_sha1_file(sha1, type, &inner.size);
- if (!tree || strcmp(type, tree_type))
+ tree = read_sha1_file(sha1, &type, &inner.size);
+ if (!tree || type != OBJ_TREE)
die("corrupt tree sha %s", sha1_to_hex(sha1));
inner.buf = tree;
index b6f02fecc46ec4633dc1ee75f38bc90761a4fbe3..46923ee61bcce99e677abcc37e233359d39cc9fc 100644 (file)
--- a/tree.c
+++ b/tree.c
int parse_tree(struct tree *item)
{
- char type[20];
+ enum object_type type;
void *buffer;
unsigned long size;
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, tree_type)) {
+ if (type != OBJ_TREE) {
free(buffer);
return error("Object %s not a tree",
sha1_to_hex(item->object.sha1));
diff --git a/unpack-file.c b/unpack-file.c
index d24acc2a67c4b7ba112bd192680b137f30a06003..25c56b374ae01ba890ee243368077c1316d9f0ba 100644 (file)
--- a/unpack-file.c
+++ b/unpack-file.c
{
static char path[50];
void *buf;
- char type[100];
+ enum object_type type;
unsigned long size;
int fd;
- buf = read_sha1_file(sha1, type, &size);
- if (!buf || strcmp(type, blob_type))
+ buf = read_sha1_file(sha1, &type, &size);
+ if (!buf || type != OBJ_BLOB)
die("unable to read blob object %s", sha1_to_hex(sha1));
strcpy(path, ".merge_file_XXXXXX");