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

[PATCH v2 01/10] Define a structure for object IDs.

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Mar 13, 2015, 23:39 UTC
Message-ID
<1426289976-568060-2-git-send-email-sandals@crustytoothpaste.net>
In-Reply-To
<1426289976-568060-1-git-send-email-sandals@crustytoothpaste.net>

Many places throughout the code use "unsigned char [20]" to store object IDs (SHA-1 values). This leads to lots of hardcoded numbers throughout the codebase. It also leads to confusion about the purposes of a buffer.

Introduce a structure for object IDs. This allows us to obtain the benefits of compile-time checking for misuse. The structure is expected to remain the same size and have the same alignment requirements on all known platforms, compared to the array of unsigned char, although this is not required for correctness.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
 cache.h | 8 ++++++++
 1 file changed, 8 insertions(+)
diff --git a/cache.h b/cache.h
index 761c570..6582c35 100644
--- a/cache.h
+++ b/cache.h
@@ -43,6 +43,14 @@ int git_deflate_end_gently(git_zstream *);
 int git_deflate(git_zstream *, int flush);
 unsigned long git_deflate_bound(git_zstream *, unsigned long);
 
+/* The length in bytes and in hex digits of an object name (SHA-1 value). */
+#define GIT_SHA1_RAWSZ 20
+#define GIT_SHA1_HEXSZ (2 * GIT_SHA1_RAWSZ)
+
+struct object_id {
+	unsigned char hash[GIT_SHA1_RAWSZ];
+};
+
 #if defined(DT_UNKNOWN) && !defined(NO_D_TYPE_IN_DIRENT)
 #define DTYPE(de)	((de)->d_type)
 #else
-- 
2.2.1.209.g41e5f3a
Previous: brian m. carlsonNext: brian m. carlson
Message 2 of 12 in “Use a structure for object IDs.”
  1. 00/10 Use a structure for object IDs.brian m. carlson, Mar 13, 2015
  2. 01/10 Define a structure for object IDs.brian m. carlson, Mar 13, 2015
  3. 02/10 Define utility functions for object IDs.brian m. carlson, Mar 13, 2015
  4. 03/10 bisect.c: convert leaf functions to use struct object_idbrian m. carlson, Mar 13, 2015
  5. 04/10 archive.c: convert to use struct object_idbrian m. carlson, Mar 13, 2015
  6. 05/10 zip: use GIT_SHA1_HEXSZ for trailersbrian m. carlson, Mar 13, 2015
  7. 06/10 bulk-checkin.c: convert to use struct object_idbrian m. carlson, Mar 13, 2015
  8. 07/10 diff: convert struct combine_diff_path to object_idbrian m. carlson, Mar 13, 2015
  9. 08/10 commit: convert parts to struct object_idbrian m. carlson, Mar 13, 2015
  10. 09/10 patch-id: convert to use struct object_idbrian m. carlson, Mar 13, 2015
  11. 10/10 apply: convert threeway_stage to object_idbrian m. carlson, Mar 13, 2015
  12. brian m. carlsonMar 14, 2015

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.