{"thread":{"id":"6997","subject":"[PATCH 2/2] Use OBJ_NONE to mean we don't have an object.","startedAt":"2007-02-27T15:00:46Z","lastAt":"2007-02-27T15:00:46Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"35677","messageId":"20070227150046.GB3242@spearce.org","threadId":"6997","inReplyTo":null,"subject":"[PATCH 2/2] Use OBJ_NONE to mean we don't have an object.","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-02-27T15:00:46Z","receivedAt":"2007-02-27T15:00:46Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Rather than returning -1 (which is not a valid object_type value)\nfrom sha1_object_info we now return OBJ_NONE.  Practically speaking\nthere is no logic change in this patch, its just a cleanup to ensure\nthe return value is always within object_type.\n\nThe type OBJ_NONE is already used in a few locations to mean \"don't\ngot that object\", as OBJ_NONE = 0 and standard initializations like\nxcalloc will automatically prefill OBJ_NONE.  This change is just\ncontinuing with that existing tradition.\n\nI'm planning on taking advantage of this cleanup later on in the\npack version 4 changes.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n builtin-cat-file.c     |    2 +-\n builtin-commit-tree.c  |    2 +-\n builtin-pack-objects.c |    4 ++--\n cache.h                |    2 +-\n diff.c                 |    2 +-\n fast-import.c          |    2 +-\n mktree.c               |    2 +-\n sha1_file.c            |   42 ++++++++++++++++++++++++++----------------\n 8 files changed, 34 insertions(+), 24 deletions(-)\n\ndiff --git a/builtin-cat-file.c b/builtin-cat-file.c\nindex d61d3d5..cb0d288 100644\n--- a/builtin-cat-file.c\n+++ b/builtin-cat-file.c\n@@ -120,7 +120,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)\n \n \tcase 'p':\n \t\ttype = sha1_object_info(sha1, NULL);\n-\t\tif (type < 0)\n+\t\tif (type == OBJ_NONE)\n \t\t\tdie(\"Not a valid object name %s\", argv[2]);\n \n \t\t/* custom pretty-print here */\ndiff --git a/builtin-commit-tree.c b/builtin-commit-tree.c\nindex 4a8d8d8..ed47804 100644\n--- a/builtin-commit-tree.c\n+++ b/builtin-commit-tree.c\n@@ -48,7 +48,7 @@ static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...)\n static void check_valid(unsigned char *sha1, enum object_type expect)\n {\n \tenum object_type type = sha1_object_info(sha1, NULL);\n-\tif (type < 0)\n+\tif (type == OBJ_NONE)\n \t\tdie(\"%s is not a valid object\", sha1_to_hex(sha1));\n \tif (type != expect)\n \t\tdie(\"%s is not a valid '%s' object\", sha1_to_hex(sha1),\ndiff --git a/builtin-pack-objects.c b/builtin-pack-objects.c\nindex 8cf24f4..273255e 100644\n--- a/builtin-pack-objects.c\n+++ b/builtin-pack-objects.c\n@@ -859,7 +859,7 @@ static void add_pbase_object(struct tree_desc *tree,\n \t\tif (entry.pathlen != cmplen ||\n \t\t    memcmp(entry.path, name, cmplen) ||\n \t\t    !has_sha1_file(entry.sha1) ||\n-\t\t    (type = sha1_object_info(entry.sha1, &size)) < 0)\n+\t\t    (type = sha1_object_info(entry.sha1, &size)) == OBJ_NONE)\n \t\t\tcontinue;\n \t\tif (name[cmplen] != '/') {\n \t\t\tunsigned hash = name_hash(fullname);\n@@ -1061,7 +1061,7 @@ static void check_object(struct object_entry *entry)\n \t}\n \n \tentry->type = sha1_object_info(entry->sha1, &entry->size);\n-\tif (entry->type < 0)\n+\tif (entry->type == OBJ_NONE)\n \t\tdie(\"unable to get type of object %s\",\n \t\t    sha1_to_hex(entry->sha1));\n }\ndiff --git a/cache.h b/cache.h\nindex 0117b7e..6223ed1 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -275,7 +275,7 @@ enum object_type {\n };\n \n /* Read and unpack a sha1 file into memory, write memory to a sha1 file */\n-extern int sha1_object_info(const unsigned char *, unsigned long *);\n+extern enum object_type sha1_object_info(const unsigned char *, unsigned long *);\n extern void * unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type, unsigned long *size);\n extern void * read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size);\n extern int hash_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *sha1);\ndiff --git a/diff.c b/diff.c\nindex f033eb0..5479602 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -1440,7 +1440,7 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)\n \t\t\t\treturn 0;\n \t\t\t}\n \t\t\ttype = sha1_object_info(s->sha1, &s->size);\n-\t\t\tif (type < 0)\n+\t\t\tif (type == OBJ_NONE)\n \t\t\t\tlocate_size_cache(s->sha1, 0, s->size);\n \t\t}\n \t\telse {\ndiff --git a/fast-import.c b/fast-import.c\nindex 65e99c2..aa4be8a 100644\n--- a/fast-import.c\n+++ b/fast-import.c\n@@ -1628,7 +1628,7 @@ static void file_change_m(struct branch *b)\n \t\t\t\tcommand_buf.buf, typename(oe->type));\n \t} else {\n \t\tenum object_type type = sha1_object_info(sha1, NULL);\n-\t\tif (type < 0)\n+\t\tif (type == OBJ_NONE)\n \t\t\tdie(\"Blob not found: %s\", command_buf.buf);\n \t\tif (type != OBJ_BLOB)\n \t\t\tdie(\"Not a blob (actually a %s): %s\",\ndiff --git a/mktree.c b/mktree.c\nindex d86dde8..742b445 100644\n--- a/mktree.c\n+++ b/mktree.c\n@@ -116,7 +116,7 @@ int main(int ac, char **av)\n \t\t    get_sha1_hex(ntr + 1, sha1))\n \t\t\tdie(\"input format error: %s\", sb.buf);\n \t\ttype = sha1_object_info(sha1, NULL);\n-\t\tif (type < 0)\n+\t\tif (type == OBJ_NONE)\n \t\t\tdie(\"object %s unavailable\", sha1_to_hex(sha1));\n \t\t*ntr++ = 0; /* now at the beginning of SHA1 */\n \t\tif (type != type_from_string(ptr))\ndiff --git a/sha1_file.c b/sha1_file.c\nindex 136e79b..4399a9c 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -1085,10 +1085,11 @@ static unsigned long get_delta_base(struct packed_git *p,\n }\n \n /* forward declaration for a mutually recursive function */\n-static int packed_object_info(struct packed_git *p, unsigned long offset,\n-\t\t\t      unsigned long *sizep);\n+static enum object_type packed_object_info(struct packed_git *p,\n+\tunsigned long offset,\n+\tunsigned long *sizep);\n \n-static int packed_delta_info(struct packed_git *p,\n+static enum object_type packed_delta_info(struct packed_git *p,\n \t\t\t     struct pack_window **w_curs,\n \t\t\t     unsigned long curpos,\n \t\t\t     enum object_type type,\n@@ -1215,8 +1216,9 @@ const char *packed_object_info_detail(struct packed_git *p,\n \t}\n }\n \n-static int packed_object_info(struct packed_git *p, unsigned long obj_offset,\n-\t\t\t      unsigned long *sizep)\n+static enum object_type packed_object_info(struct packed_git *p,\n+\tunsigned long obj_offset,\n+\tunsigned long *sizep)\n {\n \tstruct pack_window *w_curs = NULL;\n \tunsigned long size, curpos = obj_offset;\n@@ -1438,30 +1440,38 @@ struct packed_git *find_sha1_pack(const unsigned char *sha1,\n \t\n }\n \n-static int sha1_loose_object_info(const unsigned char *sha1, unsigned long *sizep)\n+static enum object_type sha1_loose_object_info(\n+\tconst unsigned char *sha1,\n+\tunsigned long *sizep)\n {\n-\tint status;\n+\tenum object_type type;\n \tunsigned long mapsize, size;\n \tvoid *map;\n \tz_stream stream;\n \tchar hdr[32];\n \n \tmap = map_sha1_file(sha1, &mapsize);\n-\tif (!map)\n-\t\treturn error(\"unable to find %s\", sha1_to_hex(sha1));\n-\tif (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)\n-\t\tstatus = error(\"unable to unpack %s header\",\n-\t\t\t       sha1_to_hex(sha1));\n-\telse if ((status = parse_sha1_header(hdr, &size)) < 0)\n-\t\tstatus = error(\"unable to parse %s header\", sha1_to_hex(sha1));\n+\tif (!map) {\n+\t\terror(\"unable to find %s\", sha1_to_hex(sha1));\n+\t\treturn OBJ_NONE;\n+\t}\n+\tif (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0) {\n+\t\terror(\"unable to unpack %s header\", sha1_to_hex(sha1));\n+\t\ttype = OBJ_NONE;\n+\t}\n+\telse if ((type = parse_sha1_header(hdr, &size)) < 0) {\n+\t\terror(\"unable to parse %s header\", sha1_to_hex(sha1));\n+\t\ttype = OBJ_NONE;\n+\t}\n \telse if (sizep)\n \t\t*sizep = size;\n \tinflateEnd(&stream);\n \tmunmap(map, mapsize);\n-\treturn status;\n+\treturn type;\n }\n \n-int sha1_object_info(const unsigned char *sha1, unsigned long *sizep)\n+enum object_type sha1_object_info(const unsigned char *sha1,\n+\tunsigned long *sizep)\n {\n \tstruct pack_entry e;\n \n-- \n1.5.0.2.775.g1a500\n"}]}