git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 1/1] Documentation/user-manual.txt: example for generating object hashes

From
Dirk Gouders <dirk@gouders.net>
Date
Mar 12, 2024, 10:41 UTC
Message-ID
<20240312104238.4920-2-dirk@gouders.net>
In-Reply-To
<20240312104238.4920-1-dirk@gouders.net>
Add a simple example on how object hashes can be generated manually.

Further, because the document suggests to have a look at the initial commit, clarify that some details changed since that time.

Signed-off-by: Dirk Gouders <dirk@gouders.net>
---
 Documentation/user-manual.txt | 36 +++++++++++++++++++++++++++++++++--
 1 file changed, 34 insertions(+), 2 deletions(-)
diff --git a/Documentation/user-manual.txt b/Documentation/user-manual.txt
index 6433903491..90a4189358 100644
--- a/Documentation/user-manual.txt
+++ b/Documentation/user-manual.txt
@@ -4093,7 +4093,38 @@ that not only specifies their type, but also provides size information
 about the data in the object.  It's worth noting that the SHA-1 hash
 that is used to name the object is the hash of the original data
 plus this header, so `sha1sum` 'file' does not match the object name
-for 'file'.
+for 'file' (the earliest versions of Git hashed slightly differently
+but the conclusion is still the same).
+
+The following is a short example that demonstrates how these hashes
+can be generated manually:
+
+Let's assume a small text file with some simple content:
+
+-------------------------------------------------
+$ echo "Hello world" >hello.txt
+-------------------------------------------------
+
+We can now manually generate the hash Git would use for this file:
+
+- The object we want the hash for is of type "blob" and its size is
+  12 bytes.
+
+- Prepend the object header to the file content and feed this to
+  `sha1sum`:
+
+-------------------------------------------------
+$ { printf "blob 12\0"; cat hello.txt; } | sha1sum
+802992c4220de19a90767f3000a79a31b98d0df7  -
+-------------------------------------------------
+
+This manually constructed hash can be verified using `git hash-object`
+which of course hides the addition of the header:
+
+-------------------------------------------------
+$ git hash-object hello.txt
+802992c4220de19a90767f3000a79a31b98d0df7
+-------------------------------------------------
 
 As a result, the general consistency of an object can always be tested
 independently of the contents or the type of the object: all objects can
@@ -4123,7 +4154,8 @@ $ git switch --detach e83c5163
 ----------------------------------------------------
 
 The initial revision lays the foundation for almost everything Git has
-today, but is small enough to read in one sitting.
+today (even though details may differ in a few places), but is small
+enough to read in one sitting.
 
 Note that terminology has changed since that revision.  For example, the
 README in that revision uses the word "changeset" to describe what we
-- 
2.43.0
Previous: Dirk Gouders
Message 10 of 10 in “Documentation/user-manual.txt: try to clarify on object hashes”
  1. 0/1 Documentation/user-manual.txt: try to clarify on object hashesDirk Gouders, Feb 29, 2024
  2. 1/1 Documentation/user-manual.txt: example for generating object hashesDirk Gouders, Feb 29, 2024
  3. Junio C HamanoFeb 29, 2024
  4. Dirk GoudersFeb 29, 2024
  5. Junio C HamanoFeb 29, 2024
  6. Dirk GoudersMar 8, 2024
  7. Junio C HamanoMar 8, 2024
  8. Dirk GoudersMar 8, 2024
  9. 0/1 Documentation/user-manual.txt: try to clarify on object hashesDirk Gouders, Mar 12, 2024
  10. 1/1 Documentation/user-manual.txt: example for generating object hashesDirk Gouders, Mar 12, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.