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

[PATCH 02/33] notes: convert internal parts to struct object_id

From
BWBrandon Williams <bmwill@google.com>
Date
May 30, 2017, 17:30 UTC
Message-ID
<20170530173109.54904-3-bmwill@google.com>
In-Reply-To
<20170530173109.54904-1-bmwill@google.com>
From: "brian m. carlson" <sandals@crustytoothpaste.net>

Convert several portions of the internals of the code to struct object_id. Introduce two macros to denote the different constants in the code: KEY_INDEX for the last byte of the object ID, and FANOUT_PATH_SEPARATORS for the number of possible path separators (on Unix, "/"). While these constants are both 19 (one less than the number of bytes in the hash), distinguish them to make the code more understandable, and define them logically based on their intended purpose.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Brandon Williams <bmwill@google.com>
---
 notes.c | 64 +++++++++++++++++++++++++++++++++-------------------------------
 1 file changed, 33 insertions(+), 31 deletions(-)
diff --git a/notes.c b/notes.c
index 251cf11c9..babe0c0eb 100644
--- a/notes.c
+++ b/notes.c
@@ -65,8 +65,10 @@ struct non_note {
 
 #define GET_NIBBLE(n, sha1) (((sha1[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)
 
+#define KEY_INDEX (GIT_SHA1_RAWSZ - 1)
+#define FANOUT_PATH_SEPARATORS ((GIT_SHA1_HEXSZ / 2) - 1)
 #define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \
-	(memcmp(key_sha1, subtree_sha1, subtree_sha1[19]))
+	(memcmp(key_sha1, subtree_sha1, subtree_sha1[KEY_INDEX]))
 
 struct notes_tree default_notes_tree;
 
@@ -194,7 +196,7 @@ static void note_tree_remove(struct notes_tree *t,
 		struct leaf_node *entry)
 {
 	struct leaf_node *l;
-	struct int_node *parent_stack[20];
+	struct int_node *parent_stack[GIT_SHA1_RAWSZ];
 	unsigned char i, j;
 	void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
 
@@ -341,21 +343,21 @@ static void note_tree_free(struct int_node *tree)
  * Otherwise, returns number of bytes written to sha1 (i.e. hex_len / 2).
  * Pads sha1 with NULs up to sha1_len (not included in returned length).
  */
-static int get_sha1_hex_segment(const char *hex, unsigned int hex_len,
-		unsigned char *sha1, unsigned int sha1_len)
+static int get_oid_hex_segment(const char *hex, unsigned int hex_len,
+		unsigned char *oid, unsigned int oid_len)
 {
 	unsigned int i, len = hex_len >> 1;
-	if (hex_len % 2 != 0 || len > sha1_len)
+	if (hex_len % 2 != 0 || len > oid_len)
 		return -1;
 	for (i = 0; i < len; i++) {
 		unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
 		if (val & ~0xff)
 			return -1;
-		*sha1++ = val;
+		*oid++ = val;
 		hex += 2;
 	}
-	for (; i < sha1_len; i++)
-		*sha1++ = 0;
+	for (; i < oid_len; i++)
+		*oid++ = 0;
 	return len;
 }
 
@@ -413,7 +415,7 @@ static void add_non_note(struct notes_tree *t, char *path,
 static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
 		struct int_node *node, unsigned int n)
 {
-	unsigned char object_sha1[20];
+	struct object_id object_oid;
 	unsigned int prefix_len;
 	void *buf;
 	struct tree_desc desc;
@@ -427,13 +429,13 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
 		die("Could not read %s for notes-index",
 		     oid_to_hex(&subtree->val_oid));
 
-	prefix_len = subtree->key_oid.hash[19];
+	prefix_len = subtree->key_oid.hash[KEY_INDEX];
 	assert(prefix_len * 2 >= n);
-	memcpy(object_sha1, subtree->key_oid.hash, prefix_len);
+	memcpy(object_oid.hash, subtree->key_oid.hash, prefix_len);
 	while (tree_entry(&desc, &entry)) {
 		path_len = strlen(entry.path);
-		len = get_sha1_hex_segment(entry.path, path_len,
-				object_sha1 + prefix_len, 20 - prefix_len);
+		len = get_oid_hex_segment(entry.path, path_len,
+				object_oid.hash + prefix_len, GIT_SHA1_RAWSZ - prefix_len);
 		if (len < 0)
 			goto handle_non_note; /* entry.path is not a SHA1 */
 		len += prefix_len;
@@ -443,16 +445,16 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
 		 * If object SHA1 is incomplete (len < 20), and current
 		 * component consists of 2 hex chars, assume note subtree
 		 */
-		if (len <= 20) {
+		if (len <= GIT_SHA1_RAWSZ) {
 			type = PTR_TYPE_NOTE;
 			l = (struct leaf_node *)
 				xcalloc(1, sizeof(struct leaf_node));
-			hashcpy(l->key_oid.hash, object_sha1);
+			oidcpy(&l->key_oid, &object_oid);
 			oidcpy(&l->val_oid, entry.oid);
-			if (len < 20) {
+			if (len < GIT_SHA1_RAWSZ) {
 				if (!S_ISDIR(entry.mode) || path_len != 2)
 					goto handle_non_note; /* not subtree */
-				l->key_oid.hash[19] = (unsigned char) len;
+				l->key_oid.hash[KEY_INDEX] = (unsigned char) len;
 				type = PTR_TYPE_SUBTREE;
 			}
 			if (note_tree_insert(t, node, n, l, type,
@@ -542,14 +544,14 @@ static unsigned char determine_fanout(struct int_node *tree, unsigned char n,
 }
 
 /* hex SHA1 + 19 * '/' + NUL */
-#define FANOUT_PATH_MAX 40 + 19 + 1
+#define FANOUT_PATH_MAX GIT_SHA1_HEXSZ + FANOUT_PATH_SEPARATORS + 1
 
 static void construct_path_with_fanout(const unsigned char *sha1,
 		unsigned char fanout, char *path)
 {
 	unsigned int i = 0, j = 0;
 	const char *hex_sha1 = sha1_to_hex(sha1);
-	assert(fanout < 20);
+	assert(fanout < GIT_SHA1_RAWSZ);
 	while (fanout) {
 		path[i++] = hex_sha1[j++];
 		path[i++] = hex_sha1[j++];
@@ -599,7 +601,7 @@ static int for_each_note_helper(struct notes_tree *t, struct int_node *tree,
 			    flags & FOR_EACH_NOTE_YIELD_SUBTREES) {
 				/* invoke callback with subtree */
 				unsigned int path_len =
-					l->key_oid.hash[19] * 2 + fanout;
+					l->key_oid.hash[KEY_INDEX] * 2 + fanout;
 				assert(path_len < FANOUT_PATH_MAX - 1);
 				construct_path_with_fanout(l->key_oid.hash,
 							   fanout,
@@ -654,7 +656,7 @@ static void write_tree_entry(struct strbuf *buf, unsigned int mode,
 		unsigned char *sha1)
 {
 	strbuf_addf(buf, "%o %.*s%c", mode, path_len, path, '\0');
-	strbuf_add(buf, sha1, 20);
+	strbuf_add(buf, sha1, GIT_SHA1_RAWSZ);
 }
 
 static void tree_write_stack_init_subtree(struct tree_write_stack *tws,
@@ -666,7 +668,7 @@ static void tree_write_stack_init_subtree(struct tree_write_stack *tws,
 	n = (struct tree_write_stack *)
 		xmalloc(sizeof(struct tree_write_stack));
 	n->next = NULL;
-	strbuf_init(&n->buf, 256 * (32 + 40)); /* assume 256 entries per tree */
+	strbuf_init(&n->buf, 256 * (32 + GIT_SHA1_HEXSZ)); /* assume 256 entries per tree */
 	n->path[0] = n->path[1] = '\0';
 	tws->next = n;
 	tws->path[0] = path[0];
@@ -677,18 +679,18 @@ static int tree_write_stack_finish_subtree(struct tree_write_stack *tws)
 {
 	int ret;
 	struct tree_write_stack *n = tws->next;
-	unsigned char s[20];
+	struct object_id s;
 	if (n) {
 		ret = tree_write_stack_finish_subtree(n);
 		if (ret)
 			return ret;
-		ret = write_sha1_file(n->buf.buf, n->buf.len, tree_type, s);
+		ret = write_sha1_file(n->buf.buf, n->buf.len, tree_type, s.hash);
 		if (ret)
 			return ret;
 		strbuf_release(&n->buf);
 		free(n);
 		tws->next = NULL;
-		write_tree_entry(&tws->buf, 040000, tws->path, 2, s);
+		write_tree_entry(&tws->buf, 040000, tws->path, 2, s.hash);
 		tws->path[0] = tws->path[1] = '\0';
 	}
 	return 0;
@@ -771,7 +773,7 @@ static int write_each_note(const unsigned char *object_sha1,
 		note_path[note_path_len] = '\0';
 		mode = 040000;
 	}
-	assert(note_path_len <= 40 + 19);
+	assert(note_path_len <= GIT_SHA1_HEXSZ + FANOUT_PATH_SEPARATORS);
 
 	/* Weave non-note entries into note entries */
 	return  write_each_non_note_until(note_path, d) ||
@@ -946,8 +948,8 @@ void string_list_add_refs_by_glob(struct string_list *list, const char *glob)
 	if (has_glob_specials(glob)) {
 		for_each_glob_ref(string_list_add_one_ref, glob, list);
 	} else {
-		unsigned char sha1[20];
-		if (get_sha1(glob, sha1))
+		struct object_id oid;
+		if (get_oid(glob, &oid))
 			warning("notes ref %s is invalid", glob);
 		if (!unsorted_string_list_has_string(list, glob))
 			string_list_append(list, glob);
@@ -1150,7 +1152,7 @@ int write_notes_tree(struct notes_tree *t, unsigned char *result)
 
 	/* Prepare for traversal of current notes tree */
 	root.next = NULL; /* last forward entry in list is grounded */
-	strbuf_init(&root.buf, 256 * (32 + 40)); /* assume 256 entries */
+	strbuf_init(&root.buf, 256 * (32 + GIT_SHA1_HEXSZ)); /* assume 256 entries */
 	root.path[0] = root.path[1] = '\0';
 	cb_data.root = &root;
 	cb_data.next_non_note = t->first_non_note;
@@ -1315,9 +1317,9 @@ void expand_notes_ref(struct strbuf *sb)
 
 void expand_loose_notes_ref(struct strbuf *sb)
 {
-	unsigned char object[20];
+	struct object_id object;
 
-	if (get_sha1(sb->buf, object)) {
+	if (get_oid(sb->buf, &object)) {
 		/* fallback to expand_notes_ref */
 		expand_notes_ref(sb);
 	}
-- 
2.13.0.219.gdb65acc882-goog
Previous: Brandon WilliamsNext: Brandon Williams
Message 6 of 66 in “object id conversion (grep and diff)”
  1. 00/33 object id conversion (grep and diff)Brandon Williams, May 30, 2017
  2. 01/33 notes: convert internal structures to struct object_idBrandon Williams, May 30, 2017
  3. 03/33 notes: convert for_each_note to struct object_idBrandon Williams, May 30, 2017
  4. 05/33 notes: convert format_display_notes to struct object_idBrandon Williams, May 30, 2017
  5. 06/33 builtin/notes: convert to struct object_idBrandon Williams, May 30, 2017
  6. 02/33 notes: convert internal parts to struct object_idBrandon Williams, May 30, 2017
  7. 04/33 notes: make get_note return pointer to struct object_idBrandon Williams, May 30, 2017
  8. René ScharfeJul 15, 2017
  9. Brandon WilliamsJul 17, 2017
  10. 09/33 diff: convert get_stat_data to struct object_idBrandon Williams, May 30, 2017
  11. 07/33 notes: convert some accessor functions to struct object_idBrandon Williams, May 30, 2017
  12. 08/33 grep: convert to struct object_idBrandon Williams, May 30, 2017
  13. Junio C HamanoJun 2, 2017
  14. 15/33 diff: convert reuse_worktree_file to struct object_idBrandon Williams, May 30, 2017
  15. 10/33 diff: convert diff_index_show_file to struct object_idBrandon Williams, May 30, 2017
  16. 19/33 combine-diff: convert diff_tree_combined to struct object_idBrandon Williams, May 30, 2017
  17. 14/33 diff: convert fill_filespec to struct object_idBrandon Williams, May 30, 2017
  18. 21/33 tree-diff: convert diff_root_tree_sha1 to struct object_idBrandon Williams, May 30, 2017
  19. 24/33 notes-merge: convert find_notes_merge_pair_ps to struct object_idBrandon Williams, May 30, 2017
  20. 18/33 diff: convert diff_flush_patch_id to struct object_idBrandon Williams, May 30, 2017
  21. 25/33 notes-merge: convert verify_notes_filepair to struct object_idBrandon Williams, May 30, 2017
  22. Stefan BellerMay 31, 2017
  23. Junio C HamanoJun 2, 2017
  24. Brandon WilliamsJun 2, 2017
  25. Junio C HamanoJun 2, 2017
  26. 30/33 tree-diff: convert diff_tree_paths to struct object_idBrandon Williams, May 30, 2017
  27. Stefan BellerMay 31, 2017
  28. Jeff KingMay 31, 2017
  29. Jeff KingMay 31, 2017
  30. Junio C HamanoJun 2, 2017
  31. René ScharfeJul 15, 2017
  32. brian m. carlsonJul 15, 2017
  33. 26/33 notes-merge: convert write_note_to_worktree to struct object_idBrandon Williams, May 30, 2017
  34. 29/33 tree-diff: convert try_to_follow_renames to struct object_idBrandon Williams, May 30, 2017
  35. 20/33 combine-diff: convert find_paths_* to struct object_idBrandon Williams, May 30, 2017
  36. Stefan BellerMay 31, 2017
  37. Junio C HamanoJun 2, 2017
  38. 23/33 notes-merge: convert merge_from_diffs to struct object_idBrandon Williams, May 30, 2017
  39. Stefan BellerMay 31, 2017
  40. Junio C HamanoJun 2, 2017
  41. 22/33 notes-merge: convert notes_merge* to struct object_idBrandon Williams, May 30, 2017
  42. Stefan BellerMay 31, 2017
  43. brian m. carlsonMay 31, 2017
  44. Junio C HamanoJun 2, 2017
  45. Brandon WilliamsJun 2, 2017
  46. 33/33 diff: rename diff_fill_sha1_info to diff_fill_oid_infoBrandon Williams, May 30, 2017
  47. 31/33 tree-diff: convert path_appendnew to object_idBrandon Williams, May 30, 2017
  48. Junio C HamanoJun 2, 2017
  49. 27/33 diff-tree: convert diff_tree_sha1 to struct object_idBrandon Williams, May 30, 2017
  50. 32/33 diffcore-rename: use is_empty_blob_oidBrandon Williams, May 30, 2017
  51. 28/33 builtin/diff-tree: cleanup references to sha1Brandon Williams, May 30, 2017
  52. Junio C HamanoJun 2, 2017
  53. 17/33 patch-ids: convert to struct object_idBrandon Williams, May 30, 2017
  54. 16/33 diff: finish conversion for prepare_temp_file to struct object_idBrandon Williams, May 30, 2017
  55. Stefan BellerMay 31, 2017
  56. 11/33 diff: convert diff_addremove to struct object_idBrandon Williams, May 30, 2017
  57. 12/33 diff: convert run_diff_files to struct object_idBrandon Williams, May 30, 2017
  58. 13/33 diff: convert diff_change to struct object_idBrandon Williams, May 30, 2017
  59. brian m. carlsonMay 31, 2017
  60. Brandon WilliamsJun 2, 2017
  61. Junio C HamanoJun 2, 2017
  62. Junio C HamanoJun 2, 2017
  63. Junio C HamanoJun 2, 2017
  64. Brandon WilliamsJun 2, 2017
  65. Junio C HamanoJun 2, 2017
  66. Brandon WilliamsJun 5, 2017

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.