Skip to content

Commit b6c4cec

Browse files
committed
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 <[email protected]>
1 parent 87b5054 commit b6c4cec

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

sha1_file.c

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,16 +1003,16 @@ static void mark_bad_packed_object(struct packed_git *p,
10031003
p->num_bad_objects++;
10041004
}
10051005

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)
10071007
{
10081008
struct packed_git *p;
10091009
unsigned i;
10101010

10111011
for (p = packed_git; p; p = p->next)
10121012
for (i = 0; i < p->num_bad_objects; i++)
10131013
if (!hashcmp(sha1, p->bad_object_sha1 + 20 * i))
1014-
return 1;
1015-
return 0;
1014+
return p;
1015+
return NULL;
10161016
}
10171017

10181018
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,
20792079
return read_packed_sha1(sha1, type, size);
20802080
}
20812081

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+
*/
20822087
void *read_sha1_file_repl(const unsigned char *sha1,
20832088
enum object_type *type,
20842089
unsigned long *size,
@@ -2087,28 +2092,30 @@ void *read_sha1_file_repl(const unsigned char *sha1,
20872092
const unsigned char *repl = lookup_replace_object(sha1);
20882093
void *data = read_object(repl, type, size);
20892094
char *path;
2095+
const struct packed_git *p;
2096+
2097+
if (data) {
2098+
if (replacement)
2099+
*replacement = repl;
2100+
return data;
2101+
}
20902102

20912103
/* die if we replaced an object with one that does not exist */
2092-
if (!data && repl != sha1)
2104+
if (repl != sha1)
20932105
die("replacement %s not found for %s",
20942106
sha1_to_hex(repl), sha1_to_hex(sha1));
20952107

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);
21062112
}
21072113

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);
21102117

2111-
return data;
2118+
return NULL;
21122119
}
21132120

21142121
void *read_object_with_reference(const unsigned char *sha1,

0 commit comments

Comments
 (0)