git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v10 1/4] sha1_file: support reading from a loose object of unknown type

From
karthik nayak <karthik.188@gmail.com>
Date
May 5, 2015, 02:21 UTC
Message-ID
<55482936.7020205@gmail.com>
In-Reply-To
<CAPig+cTitnC7zvWJEwdo2cMWNE+3ODjd++81bYdg-qTc0bzgog@mail.gmail.com>
On 05/05/2015 05:04 AM, Eric Sunshine wrote:
Show 92 quoted lines
> On Sun, May 3, 2015 at 10:29 AM, Karthik Nayak <karthik.188@gmail.com> wrote:
>> Update sha1_loose_object_info() to optionally allow it to read
>> from a loose object file of unknown/bogus type; as the function
>> usually returns the type of the object it read in the form of enum
>> for known types, add an optional "typename" field to receive the
>> name of the type in textual form and a flag to indicate the reading
>> of a loose object file of unknown/bogus type.
>>
>> Add parse_sha1_header_extended() which acts as a wrapper around
>> parse_sha1_header() allowing more information to be obtained.
>>
>> Add unpack_sha1_header_to_strbuf() to unpack sha1 headers of
>> unknown/corrupt objects which have a unknown sha1 header size to
>> a strbuf structure. This was written by Junio C Hamano but tested
>> by me.
>> ---
>> diff --git a/cache.h b/cache.h
>> index 3d3244b..38419c3 100644
>> --- a/cache.h
>> +++ b/cache.h
>> @@ -1564,6 +1564,33 @@ int unpack_sha1_header(git_zstream *stream, unsigned char *map, unsigned long ma
>> +static int unpack_sha1_header_to_strbuf(git_zstream *stream, unsigned char *map,
>> +                                       unsigned long mapsize, void *buffer,
>> +                                       unsigned long bufsiz, struct strbuf *header)
>> +{
>> +       unsigned char *cp;
>> +       int status;
>> +
>> +       status = unpack_sha1_header(stream, map, mapsize, buffer, bufsiz);
>> +
>> +       /*
>> +        * Check if entire header is unpacked in the first iteration.
>> +        */
>
> Nit: You could save some precious vertical screen real-estate by using
> one-line /* comment style */.
>
>> +       if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
>> +               return 0;
>> +
>> @@ -1614,27 +1641,38 @@ static void *unpack_sha1_rest(git_zstream *stream, void *buffer, unsigned long s
>>    * too permissive for what we want to check. So do an anal
>>    * object header parse by hand.
>>    */
>> -int parse_sha1_header(const char *hdr, unsigned long *sizep)
>> +static int parse_sha1_header_extended(const char *hdr, struct object_info *oi,
>> +                              unsigned int flags)
>>   {
>> [...]
>> +
>> +       type = type_from_string_gently(type_buf, type_len, 1);
>> +       if (oi->typename)
>> +               strbuf_add(oi->typename, type_buf, type_len);
>> +       /*
>> +        * Set type to 0 if its an unknown object and
>> +        * we're obtaining the type using '--allow-unkown-type'
>> +        * option.
>> +        */
>> +       if ((flags & LOOKUP_UNKNOWN_OBJECT) && (type < 0))
>> +               type = 0;
>
> The comment says exactly what the code already says, thus it adds no
> value. A better comment would explain _why_ type is set to 0 under
> these conditions.
>
>> +       else if (type < 0)
>> +               die("invalid object type");
>> +       if (oi->typep)
>> +               *oi->typep = type;
>>
>>          /*
>>           * The length must follow immediately, and be in canonical
>> @@ -1652,12 +1690,24 @@ int parse_sha1_header(const char *hdr, unsigned long *sizep)
>>                          size = size * 10 + c;
>>                  }
>>          }
>> -       *sizep = size;
>> +
>> +       if (oi->sizep)
>> +               *oi->sizep = size;
>>
>>          /*
>>           * The length must be followed by a zero byte
>>           */
>
> Nit: Save precious vertical screen real-estate with one-line /*
> comment style */.
>
>> -       return *hdr ? -1 : type_from_string(type);
>> +       return *hdr ? -1 : type;
>> +}
>> +
Thanks for your suggestions Eric.
Previous: Eric SunshineNext: Junio C Hamano
Message 11 of 22 in “cat-file: add support for "-allow-unknown-type"”
  1. 0/4 cat-file: add support for "-allow-unknown-type"karthik nayak, May 3, 2015
  2. 1/4 sha1_file: support reading from a loose object of unknown typeKarthik Nayak, May 3, 2015
  3. 2/4 cat-file: make the options mutually exclusiveKarthik Nayak, May 3, 2015
  4. 3/4 cat-file: teach cat-file a '--allow-unknown-type' optionKarthik Nayak, May 3, 2015
  5. Eric SunshineMay 5, 2015
  6. 4/4 t1006: add tests for git cat-file --allow-unknown-typeKarthik Nayak, May 3, 2015
  7. Eric SunshineMay 5, 2015
  8. karthik nayakMay 5, 2015
  9. Junio C HamanoMay 6, 2015
  10. Eric SunshineMay 4, 2015
  11. karthik nayakMay 5, 2015
  12. Junio C HamanoMay 4, 2015
  13. Junio C HamanoMay 4, 2015
  14. Eric SunshineMay 4, 2015
  15. Eric SunshineMay 4, 2015
  16. karthik nayakMay 4, 2015
  17. karthik nayakMay 6, 2015
  18. Junio C HamanoMay 6, 2015
  19. Karthik NayakMay 7, 2015
  20. Junio C HamanoMay 7, 2015
  21. karthik nayakMay 4, 2015
  22. Junio C HamanoMay 4, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.