All of lore.kernel.org
 help / color / mirror / Atom feed
From: Brandon Williams <bmwill@google.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com, peff@peff.net, sandals@crustytoothpaste.net,
	Brandon Williams <bmwill@google.com>
Subject: [PATCH 02/33] notes: convert internal parts to struct object_id
Date: Tue, 30 May 2017 10:30:38 -0700	[thread overview]
Message-ID: <20170530173109.54904-3-bmwill@google.com> (raw)
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


  parent reply	other threads:[~2017-05-30 17:31 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-30 17:30 [PATCH 00/33] object id conversion (grep and diff) Brandon Williams
2017-05-30 17:30 ` [PATCH 01/33] notes: convert internal structures to struct object_id Brandon Williams
2017-05-30 17:30 ` Brandon Williams [this message]
2017-05-30 17:30 ` [PATCH 03/33] notes: convert for_each_note " Brandon Williams
2017-05-30 17:30 ` [PATCH 04/33] notes: make get_note return pointer " Brandon Williams
2017-07-15 18:15   ` René Scharfe
2017-07-17 17:49     ` Brandon Williams
2017-05-30 17:30 ` [PATCH 05/33] notes: convert format_display_notes " Brandon Williams
2017-05-30 17:30 ` [PATCH 06/33] builtin/notes: convert " Brandon Williams
2017-05-30 17:30 ` [PATCH 07/33] notes: convert some accessor functions " Brandon Williams
2017-05-30 17:30 ` [PATCH 08/33] grep: convert " Brandon Williams
2017-06-02  1:00   ` Junio C Hamano
2017-05-30 17:30 ` [PATCH 09/33] diff: convert get_stat_data " Brandon Williams
2017-05-30 17:30 ` [PATCH 10/33] diff: convert diff_index_show_file " Brandon Williams
2017-05-30 17:30 ` [PATCH 11/33] diff: convert diff_addremove " Brandon Williams
2017-05-30 17:30 ` [PATCH 12/33] diff: convert run_diff_files " Brandon Williams
2017-05-30 17:30 ` [PATCH 13/33] diff: convert diff_change " Brandon Williams
2017-05-30 17:30 ` [PATCH 14/33] diff: convert fill_filespec " Brandon Williams
2017-05-30 17:30 ` [PATCH 15/33] diff: convert reuse_worktree_file " Brandon Williams
2017-05-30 17:30 ` [PATCH 16/33] diff: finish conversion for prepare_temp_file " Brandon Williams
2017-05-31  0:41   ` Stefan Beller
2017-05-30 17:30 ` [PATCH 17/33] patch-ids: convert " Brandon Williams
2017-05-30 17:30 ` [PATCH 18/33] diff: convert diff_flush_patch_id " Brandon Williams
2017-05-30 17:30 ` [PATCH 19/33] combine-diff: convert diff_tree_combined " Brandon Williams
2017-05-30 17:30 ` [PATCH 20/33] combine-diff: convert find_paths_* " Brandon Williams
2017-05-31 17:49   ` Stefan Beller
2017-06-02  1:37     ` Junio C Hamano
2017-05-30 17:30 ` [PATCH 21/33] tree-diff: convert diff_root_tree_sha1 " Brandon Williams
2017-05-30 17:30 ` [PATCH 22/33] notes-merge: convert notes_merge* " Brandon Williams
2017-05-31 17:54   ` Stefan Beller
2017-05-31 22:00   ` brian m. carlson
2017-06-02  1:13     ` Junio C Hamano
2017-06-02 18:32     ` Brandon Williams
2017-05-30 17:30 ` [PATCH 23/33] notes-merge: convert merge_from_diffs " Brandon Williams
2017-05-31 18:04   ` Stefan Beller
2017-06-02  0:42     ` Junio C Hamano
2017-05-30 17:31 ` [PATCH 24/33] notes-merge: convert find_notes_merge_pair_ps " Brandon Williams
2017-05-30 17:31 ` [PATCH 25/33] notes-merge: convert verify_notes_filepair " Brandon Williams
2017-05-31 18:09   ` Stefan Beller
2017-06-02  0:47   ` Junio C Hamano
2017-06-02 18:55     ` Brandon Williams
2017-06-02 23:49       ` Junio C Hamano
2017-05-30 17:31 ` [PATCH 26/33] notes-merge: convert write_note_to_worktree " Brandon Williams
2017-05-30 17:31 ` [PATCH 27/33] diff-tree: convert diff_tree_sha1 " Brandon Williams
2017-05-30 17:31 ` [PATCH 28/33] builtin/diff-tree: cleanup references to sha1 Brandon Williams
2017-06-02  1:26   ` Junio C Hamano
2017-05-30 17:31 ` [PATCH 29/33] tree-diff: convert try_to_follow_renames to struct object_id Brandon Williams
2017-05-30 17:31 ` [PATCH 30/33] tree-diff: convert diff_tree_paths " Brandon Williams
2017-05-31 18:24   ` Stefan Beller
2017-05-31 21:29     ` Jeff King
2017-05-31 21:39       ` Jeff King
2017-06-02  1:31   ` Junio C Hamano
2017-07-15 17:18   ` René Scharfe
2017-07-15 17:22     ` brian m. carlson
2017-05-30 17:31 ` [PATCH 31/33] tree-diff: convert path_appendnew to object_id Brandon Williams
2017-06-02  1:32   ` Junio C Hamano
2017-05-30 17:31 ` [PATCH 32/33] diffcore-rename: use is_empty_blob_oid Brandon Williams
2017-05-30 17:31 ` [PATCH 33/33] diff: rename diff_fill_sha1_info to diff_fill_oid_info Brandon Williams
2017-05-31 22:06 ` [PATCH 00/33] object id conversion (grep and diff) brian m. carlson
2017-06-02 19:24   ` Brandon Williams
2017-06-02  1:34 ` Junio C Hamano
2017-06-02  5:08   ` Junio C Hamano
2017-06-02  7:19     ` Junio C Hamano
2017-06-02 18:22       ` Brandon Williams
2017-06-02 23:35         ` Junio C Hamano
2017-06-05 19:42           ` Brandon Williams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170530173109.54904-3-bmwill@google.com \
    --to=bmwill@google.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=peff@peff.net \
    --cc=sandals@crustytoothpaste.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.