read_sha1_file(): report correct name of packfile with a corrupt object
Clarify the error reporting logic by moving the normal codepath (i.e. we read the object we wanted to read correctly) up and return early. The logic to report the name of the packfile with a corrupt object, introduced by e8b15e6 (sha1_file: Show the the type and path to corrupt objects, 2010-06-10), was totally bogus. The function that knows which bad object came from what packfile is has_packed_and_bad(); make it report which packfile the problem was found. "Corrupt" is already an adjective, e.g. an object is "corrupt"; we do not have to say "corrupted object". Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Oct 28, 2010 at 11:13 UTC
b6c4ceccb3d6026c612fa55865c51374b26eb176
1 file changed
+24
-17
sha1_file.c
+24
-17
@@ -1003,7 +1003,7 @@ static void mark_bad_packed_object(struct packed_git *p,
1003
p->num_bad_objects++;
1004
}
1005
1006
-static int has_packed_and_bad(const unsigned char *sha1)
1006
+static const struct packed_git *has_packed_and_bad(const unsigned char *sha1)
1007
{
1008
struct packed_git *p;
1009
unsigned i;
@@ -1011,8 +1011,8 @@ static int has_packed_and_bad(const unsigned char *sha1)
1011
for (p = packed_git; p; p = p->next)
1012
for (i = 0; i < p->num_bad_objects; i++)
1013
if (!hashcmp(sha1, p->bad_object_sha1 + 20 * i))
1014
- return 1;
1015
- return 0;
1014
+ return p;
1015
+ return NULL;
1016
}
1017
1018
int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long size, const char *type)
@@ -2079,6 +2079,11 @@ static void *read_object(const unsigned char *sha1, enum object_type *type,
2079
return read_packed_sha1(sha1, type, size);
2080
}
2081
2082
+/*
2083
+ * This function dies on corrupt objects; the callers who want to
2084
+ * deal with them should arrange to call read_object() and give error
2085
+ * messages themselves.
2086
+ */
2087
void *read_sha1_file_repl(const unsigned char *sha1,
2088
enum object_type *type,
2089
unsigned long *size,
@@ -2087,28 +2092,30 @@ void *read_sha1_file_repl(const unsigned char *sha1,
2092
const unsigned char *repl = lookup_replace_object(sha1);
2093
void *data = read_object(repl, type, size);
2094
char *path;
2095
+ const struct packed_git *p;
2096
+
2097
+ if (data) {
2098
+ if (replacement)
2099
+ *replacement = repl;
2100
+ return data;
2101
+ }
2102
2103
/* die if we replaced an object with one that does not exist */
2092
- if (!data && repl != sha1)
2104
+ if (repl != sha1)
2105
die("replacement %s not found for %s",
2106
sha1_to_hex(repl), sha1_to_hex(sha1));
2107
2096
- /* legacy behavior is to die on corrupted objects */
2097
- if (!data) {
2098
- if (has_loose_object(repl)) {
2099
- path = sha1_file_name(sha1);
2100
- die("loose object %s (stored in %s) is corrupted", sha1_to_hex(repl), path);
2101
- }
2102
- if (has_packed_and_bad(repl)) {
2103
- path = sha1_pack_name(sha1);
2104
- die("packed object %s (stored in %s) is corrupted", sha1_to_hex(repl), path);
2105
- }
2108
+ if (has_loose_object(repl)) {
2109
+ path = sha1_file_name(sha1);
2110
+ die("loose object %s (stored in %s) is corrupt",
2111
+ sha1_to_hex(repl), path);
2112
}
2113
2108
- if (replacement)
2109
- *replacement = repl;
2114
+ if ((p = has_packed_and_bad(repl)) != NULL)
2115
+ die("packed object %s (stored in %s) is corrupt",
2116
+ sha1_to_hex(repl), p->pack_name);
2117
2111
- return data;
2118
+ return NULL;
2119
}
2120
2121
void *read_object_with_reference(const unsigned char *sha1,