Subject: Re: [PATCH 2/2] sha1_file: remove read_packed_sha1()

From: Junio C Hamano <[email-masked]>
Date: Fri, 11 Aug 2017 15:06:54 -0700

Jonathan Tan <[email-masked]> writes:

> Use read_object() in its place instead. This avoids duplication of code.
>
> This makes force_object_loose() slightly slower (because of a redundant
> check of loose object storage), but only in the error case.
>
> Signed-off-by: Jonathan Tan <[email-masked]>
> ---
>  sha1_file.c | 26 +-------------------------
>  1 file changed, 1 insertion(+), 25 deletions(-)

The original code insisted on reading from pack and never from a
loose object, because it knew it would return early when it found a
loose version.  Now we allow a loose one to appear in the middle of
force_object_loose() operation and happily read from it when we do
not see a pack entry for the object---presumably because we are
racing with another simultanous repack process, or something?---and
then write it out as a new (and identical) loose object, which would
not do any harm.

So this is not strictly a no-op conversion; I have a gut feeling
that it would make it more robust, not less, in the presence of
another racing repack process, but I haven't really thought through
race scenarios that may make difference in its behaviour.

> diff --git a/sha1_file.c b/sha1_file.c
> index 910109fd9..0f758eabf 100644
> --- a/sha1_file.c
> +++ b/sha1_file.c
> @@ -3062,30 +3062,6 @@ int sha1_object_info(const unsigned char *sha1, unsigned long *sizep)
>  	return type;
>  }
>  
> -static void *read_packed_sha1(const unsigned char *sha1,
> -			      enum object_type *type, unsigned long *size)
> -{
> -	struct pack_entry e;
> -	void *data;
> -
> -	if (!find_pack_entry(sha1, &e))
> -		return NULL;
> -	data = cache_or_unpack_entry(e.p, e.offset, size, type);
> -	if (!data) {
> -		/*
> -		 * We're probably in deep shit, but let's try to fetch
> -		 * the required object anyway from another pack or loose.
> -		 * This should happen only in the presence of a corrupted
> -		 * pack, and is better than failing outright.
> -		 */
> -		error("failed to read object %s at offset %"PRIuMAX" from %s",
> -		      sha1_to_hex(sha1), (uintmax_t)e.offset, e.p->pack_name);
> -		mark_bad_packed_object(e.p, sha1);
> -		data = read_object(sha1, type, size);
> -	}
> -	return data;
> -}
> -
>  int pretend_sha1_file(void *buf, unsigned long len, enum object_type type,
>  		      unsigned char *sha1)
>  {
> @@ -3468,7 +3444,7 @@ int force_object_loose(const unsigned char *sha1, time_t mtime)
>  
>  	if (has_loose_object(sha1))
>  		return 0;
> -	buf = read_packed_sha1(sha1, &type, &len);
> +	buf = read_object(sha1, &type, &len);
>  	if (!buf)
>  		return error("cannot read sha1_file for %s", sha1_to_hex(sha1));
>  	hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(type), len) + 1;