From a69a944614c40efca7544045f2f6a0f8cc45bb4a Mon Sep 17 00:00:00 2001 From: jingsam Date: Mon, 11 Jun 2018 18:00:09 +0800 Subject: [PATCH 1/2] Add hash-object to validate ruby hash function close #1080 --- book/10-git-internals/sections/objects.asc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/book/10-git-internals/sections/objects.asc b/book/10-git-internals/sections/objects.asc index f49842fa9..b12eca456 100644 --- a/book/10-git-internals/sections/objects.asc +++ b/book/10-git-internals/sections/objects.asc @@ -381,6 +381,14 @@ You can calculate the SHA-1 value of a string in Ruby by including the SHA1 dige => "bd9dbf5aae1a3862dd1526723246b20206e5fc37" ---- +Let's validate the hash with `git hash-object`. It shows that they are identical. Note that `echo` will add a trailing newline character in string, which is not we want in this case. `echo -n` can prevent from adding a trailing newline character. + +[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: From 894d4e02f3cfc2fddeb7c9c2466671806b23bf60 Mon Sep 17 00:00:00 2001 From: jingsam Date: Tue, 12 Jun 2018 09:23:56 +0800 Subject: [PATCH 2/2] Update objects.asc --- book/10-git-internals/sections/objects.asc | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/book/10-git-internals/sections/objects.asc b/book/10-git-internals/sections/objects.asc index b12eca456..ec076c7e3 100644 --- a/book/10-git-internals/sections/objects.asc +++ b/book/10-git-internals/sections/objects.asc @@ -381,12 +381,13 @@ You can calculate the SHA-1 value of a string in Ruby by including the SHA1 dige => "bd9dbf5aae1a3862dd1526723246b20206e5fc37" ---- -Let's validate the hash with `git hash-object`. It shows that they are identical. Note that `echo` will add a trailing newline character in string, which is not we want in this case. `echo -n` can prevent from adding a trailing newline character. +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 +$ 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. @@ -417,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.