Skip to content

Commit 7836cfe

Browse files
authored
Merge pull request #1081 from jingsam/patch-1
Add hash-object to validate ruby hash function
2 parents f356841 + 894d4e0 commit 7836cfe

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

book/10-git-internals/sections/objects.asc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,15 @@ You can calculate the SHA-1 value of a string in Ruby by including the SHA1 dige
381381
=> "bd9dbf5aae1a3862dd1526723246b20206e5fc37"
382382
----
383383

384+
Let's compare that to the output of `git hash-object`.
385+
Here we use `echo -n` to prevent adding a newline to the input.
386+
387+
[source,console]
388+
----
389+
$ echo -n "what is up, doc?" | git hash-object --stdin
390+
bd9dbf5aae1a3862dd1526723246b20206e5fc37
391+
----
392+
384393
Git compresses the new content with zlib, which you can do in Ruby with the zlib library.
385394
First, you need to require the library and then run `Zlib::Deflate.deflate()` on the content:
386395

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

421+
Let's check the content of the object using `git cat-file`:
422+
423+
[source,console]
424+
---
425+
$ git cat-file -p bd9dbf5aae1a3862dd1526723246b20206e5fc37
426+
what is up, doc?
427+
---
428+
412429
That's it – you've created a valid Git blob object.
430+
413431
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.
414432
Also, although the blob content can be nearly anything, the commit and tree content are very specifically formatted.

0 commit comments

Comments
 (0)