Skip to content

Commit

Permalink
Merge branch 'np/types'
Browse files Browse the repository at this point in the history
* np/types:
  Cleanup check_valid in commit-tree.
  make sure enum object_type is signed
  get rid of lookup_object_type()
  convert object type handling from a string to a number
  formalize typename(), and add its reverse type_from_string()
  sha1_file.c: don't ignore an error condition in sha1_loose_object_info()
  sha1_file.c: cleanup "offset" usage
  sha1_file.c: cleanup hdr usage
  • Loading branch information
Junio C Hamano committed Feb 28, 2007
2 parents fbe3d87 + 66035a6 commit 597388f
Show file tree
Hide file tree
Showing 37 changed files with 372 additions and 418 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
15 changes: 7 additions & 8 deletions builtin-commit-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@ static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...)
memcpy(buf + size, one_line, len);
}

static void check_valid(unsigned char *sha1, const char *expect)
static void check_valid(unsigned char *sha1, enum object_type 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 (type != expect)
die("%s is not a valid '%s' object", sha1_to_hex(sha1),
expect);
typename(expect));
}

/*
Expand Down Expand Up @@ -101,7 +100,7 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
if (get_sha1(argv[1], tree_sha1))
die("Not a valid object name %s", argv[1]);

check_valid(tree_sha1, tree_type);
check_valid(tree_sha1, OBJ_TREE);
for (i = 2; i < argc; i += 2) {
const char *a, *b;
a = argv[i]; b = argv[i+1];
Expand All @@ -112,7 +111,7 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
die("Too many parents (%d max)", MAXPARENT);
if (get_sha1(b, parent_sha1[parents]))
die("Not a valid object name %s", b);
check_valid(parent_sha1[parents], commit_type);
check_valid(parent_sha1[parents], OBJ_COMMIT);
if (new_parent(parents))
parents++;
}
Expand Down
6 changes: 3 additions & 3 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 All @@ -196,7 +196,7 @@ static void grab_common_values(struct atom_value *val, int deref, struct object
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);
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
Loading

0 comments on commit 597388f

Please sign in to comment.