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

Re: Manually decoding a git object

From
Thomas Rast <trast@inf.ethz.ch>
Date
Feb 20, 2012, 08:29 UTC
Message-ID
<87mx8dj4at.fsf@thomas.inf.ethz.ch>
In-Reply-To
<A716A27D407F401DAAC373027D5CFF2A@PhilipOakley>
"Philip Oakley" <philipoakley@iee.org> writes:
Show 6 quoted lines
> If I have a renamed file which is a git object, such a "Git_Object", was
> 8c-something-or-other, what is the easiest way of examining / decoding /
> recreating the original file (either as its sha1, or a cat-file).
>
> I don't appear to be able to unzip the file in its raw format...  I'm using
> Msysgit on windows XP.

The SHA1 is over the decompressed object contents. The file simply holds a zlib-compressed stream of those contents. (It's pretty much like gzip without the file header.)

You can use any bindings to zlib and something that does sha1, e.g. in python:

  $ cd g/.git/objects/aa/  # my git.git
  $ ls
  592bda986a8380b64acd8cbb3d5bdfcbc0834d  6322a757bee31919f54edcc127608a3d724c99
  $ python
  Python 2.7.2 (default, Aug 19 2011, 20:41:43) [GCC] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import hashlib
  >>> hashlib.sha1(open('592bda986a8380b64acd8cbb3d5bdfcbc0834d').read().decode('zlib')).digest().encode('hex')
  'aa592bda986a8380b64acd8cbb3d5bdfcbc0834d'
Notice that the first byte of the hash goes into the directory name.
-- 
Thomas Rast
trast@{inf,student}.ethz.ch
Previous: Philip OakleyNext: Philip Oakley
Message 13 of 17 in “Document format of basic Git objects”
  1. Document format of basic Git objectsNguyễn Thái Ngọc Duy, Feb 15, 2012
  2. Jonathan NiederFeb 15, 2012
  3. Junio C HamanoFeb 15, 2012
  4. Junio C HamanoFeb 16, 2012
  5. Document format of basic Git objectsNguyễn Thái Ngọc Duy, Feb 19, 2012
  6. Junio C HamanoFeb 19, 2012
  7. Junio C HamanoFeb 19, 2012
  8. Nguyen Thai Ngoc DuyFeb 20, 2012
  9. Jeff KingFeb 20, 2012
  10. Manually decoding a git objectPhilip Oakley, Feb 19, 2012
  11. 徐迪Feb 20, 2012
  12. Philip OakleyFeb 20, 2012
  13. Thomas RastFeb 20, 2012
  14. Philip OakleyFeb 20, 2012
  15. Thomas RastFeb 20, 2012
  16. 徐迪Feb 20, 2012
  17. Philip OakleyFeb 20, 2012

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.