0

For educational purpose, I would like to manually create a Git Blob without Git.

I've read that Git uses zlib and sha256, so I wrote this:

>>> import hashlib
>>> import zlib

>>> open('hello.txt', 'w').write('Hello World!\n')
>>> hashlib.sha256(zlib.compress(open('hello.txt', 'r').read().encode())).hexdigest()
'df46eac3bcbac69a75fbc5f87fe82c5f12c4991a9610805fc3fade2bb33fbb25'

However I don't get the same as Git:

$ git cat-file -p d712
100644 blob 980a0d5f19a64b4b30a87d4206aade58726b60e3    hello.txt

What am I missing?

nowox
  • 25,978
  • 39
  • 143
  • 293
  • 1
    https://stackoverflow.com/a/28416702/717372 – Philippe Mar 05 '23 at 11:38
  • 1
    This one also could help https://git-scm.com/book/en/v2/Git-Internals-Git-Objects – Philippe Mar 05 '23 at 11:41
  • https://stackoverflow.com/search?q=%5Bgit%5D+how+objects+hash+calculated – phd Mar 05 '23 at 12:01
  • 2
    `printf 'Hello, World!\n' | git hash-object --stdin` -> 8ab686eafeb1f44702738c8b0f24f2567c36da6d ; `printf 'blob 14\0Hello, World!\n' | sha1sum -` -> 8ab686eafeb1f44702738c8b0f24f2567c36da6d – phd Mar 05 '23 at 12:07

0 Answers0