Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions book/10-git-internals/sections/objects.asc
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,15 @@ You can calculate the SHA-1 value of a string in Ruby by including the SHA1 dige
=> "bd9dbf5aae1a3862dd1526723246b20206e5fc37"
----

Let's compare that to the output of `git hash-object`.
Here we use `echo -n` to prevent adding a newline to the input.

[source,console]
----
$ echo -n "what is up, doc?" | git hash-object --stdin
bd9dbf5aae1a3862dd1526723246b20206e5fc37
----

Git compresses the new content with zlib, which you can do in Ruby with the zlib library.
First, you need to require the library and then run `Zlib::Deflate.deflate()` on the content:

Expand Down Expand Up @@ -409,6 +418,15 @@ Then, open the file with `File.open()` and write out the previously zlib-compres
=> 32
----

Let's check the content of the object using `git cat-file`:

[source,console]
---
$ git cat-file -p bd9dbf5aae1a3862dd1526723246b20206e5fc37
what is up, doc?
---

That's it – you've created a valid Git blob object.

All Git objects are stored the same way, just with different types – instead of the string blob, the header will begin with commit or tree.
Also, although the blob content can be nearly anything, the commit and tree content are very specifically formatted.