From: Thomas Rast <trast@inf.ethz.ch>
To: Philip Oakley <philipoakley@iee.org>
Cc: "Git List" <git@vger.kernel.org>, 徐迪 <xudifsd@gmail.com>
Subject: Re: Manually decoding a git object
Date: Mon, 20 Feb 2012 11:56:37 +0100 [thread overview]
Message-ID: <871uppbwnu.fsf@thomas.inf.ethz.ch> (raw)
In-Reply-To: <5D290031F60941368FB1A36464201DCF@PhilipOakley> (Philip Oakley's message of "Mon, 20 Feb 2012 10:19:49 +0000")
Philip Oakley <philipoakley@iee.org> writes:
> From: "Thomas Rast" <trast@inf.ethz.ch> Sent: Monday, February 20,
> 2012 8:29 AM
>>
>> 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.
>>
>
> At the moment I'm in a Catch 22 situation where I can't make the first
> step of examining the deflated contents, so I can't do all those next
> steps to get the sha1 etc.. Have I misunderstood your suggestions?
Huh? The method I showed does not rely on knowing the SHA1. The fact
that I used it on a properly filed away (by its SHA1) object file is
immaterial, if perhaps confusing.
I can untangle that python expression for you:
hashlib.sha1(foo).digest() gives the SHA1 digest of the string foo, as a (binary) string
foo.encode('hex') turns foo from (binary) string into its hex representation
open('filename').read() opens the file called filename, and returns its whole contents
foo.decode('zlib') applies the zlib decompressor to foo, and returns the resulting data
So that trick works for any file[*], and you can then use its results to
file it back where it needs to go.
[*] that is sufficiently small for Python to hold it in memory, but git
shares the same problems in that department.
--
Thomas Rast
trast@{inf,student}.ethz.ch
next prev parent reply other threads:[~2012-02-20 10:57 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-02-15 13:22 [PATCH/RFC] Document format of basic Git objects Nguyễn Thái Ngọc Duy
2012-02-15 17:31 ` Jonathan Nieder
2012-02-15 19:48 ` Junio C Hamano
2012-02-16 7:12 ` Junio C Hamano
2012-02-19 4:15 ` [PATCH/RFC v2] " Nguyễn Thái Ngọc Duy
2012-02-19 8:39 ` Junio C Hamano
2012-02-19 9:14 ` Junio C Hamano
2012-02-20 13:55 ` Nguyen Thai Ngoc Duy
2012-02-20 16:11 ` Jeff King
2012-02-19 18:07 ` Manually decoding a git object Philip Oakley
2012-02-20 4:45 ` 徐迪
2012-02-20 8:19 ` Philip Oakley
2012-02-20 8:29 ` Thomas Rast
2012-02-20 10:19 ` Philip Oakley
2012-02-20 10:56 ` Thomas Rast [this message]
2012-02-20 11:39 ` 徐迪
2012-02-20 18:27 ` Philip Oakley
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=871uppbwnu.fsf@thomas.inf.ethz.ch \
--to=trast@inf.ethz.ch \
--cc=git@vger.kernel.org \
--cc=philipoakley@iee.org \
--cc=xudifsd@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).