Read, hash, and store git objects, compressed with zlib, using Node JS.
Meant for educational purposes, and understanding git internals.
Currently storing git objects is not implemented.
Node.js equivalent to git cat-file -p <object hash>
.
$ git cat-file -p 9daeafb9864cf43055ae93beb0afd6c7d144bfa4 ↵
test
For viewing the header containing object type, size in bytes, and null character separating header from content.
$ node cat-file.js 9daeafb9864cf43055ae93beb0afd6c7d144bfa4 ↵
blob 5test
Uses console.dir
to output contents from stdin to stdout.
$ node cat-file.js 9daeafb9864cf43055ae93beb0afd6c7d144bfa4 | node console-dir.js ↵
'blob 5\x00test\n'
For viewing hidden characters such as null characters (\x00
) or new-line characters (\n
).
Node.js equivalent to git hash-object --stdin
.
$ echo 'test' | git hash-object --stdin ↵
9daeafb9864cf43055ae93beb0afd6c7d144bfa4
For illustrative purposes, demonstrating the hash is the SHA-1 of the object's full content, including header information.
$ node cat-file.js 9daeafb9864cf43055ae93beb0afd6c7d144bfa4 | node hash-object.js ↵
9daeafb9864cf43055ae93beb0afd6c7d144bfa4
Currently works with blobs, commits, but not trees.
- https://git-scm.com/book/en/v2/Git-Internals-Git-Objects
- https://matthew-brett.github.io/curious-git/reading_git_objects.html
- https://stackoverflow.com/questions/7625251/compression-and-decompression-of-data-using-zlib-in-nodejs
- https://developpaper.com/front-end-ramble-git-internal-principle-git-object/
- https://stackoverflow.com/questions/6984139/how-can-i-get-the-sha1-hash-of-a-string-in-node-js
- https://codewords.recurse.com/issues/three/unpacking-git-packfiles