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

[PATCH 2/3] http-push: use hex_to_bytes()

From
René Scharfe <l.s.r@web.de>
Date
Oct 31, 2017, 13:49 UTC
Message-ID
<31ce5c1c-cab3-80aa-623b-43f91ddd2162@web.de>
In-Reply-To
<508b1b3f-6b55-eee8-110a-c17d572ec27a@web.de>

The path of a loose object contains its hash value encoded into two substrings of hexadecimal digits, separated by a slash. The current code copies the pieces into a temporary buffer to get rid of the slash and then uses get_oid_hex() to decode the hash value.

Avoid the copy by using hex_to_bytes() directly on the substrings. That's shorter and easier.

While at it correct the length of the second substring in a comment.
Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 http-push.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
diff --git a/http-push.c b/http-push.c
index 493ee7d719..14435ab65d 100644
--- a/http-push.c
+++ b/http-push.c
@@ -1007,20 +1007,18 @@ static void remote_ls(const char *path, int flags,
 		      void (*userFunc)(struct remote_ls_ctx *ls),
 		      void *userData);
 
-/* extract hex from sharded "xx/x{40}" filename */
+/* extract hex from sharded "xx/x{38}" filename */
 static int get_oid_hex_from_objpath(const char *path, struct object_id *oid)
 {
-	char hex[GIT_MAX_HEXSZ];
-
 	if (strlen(path) != GIT_SHA1_HEXSZ + 1)
 		return -1;
 
-	memcpy(hex, path, 2);
+	if (hex_to_bytes(oid->hash, path, 1))
+		return -1;
 	path += 2;
 	path++; /* skip '/' */
-	memcpy(hex + 2, path, GIT_SHA1_HEXSZ - 2);
 
-	return get_oid_hex(hex, oid);
+	return hex_to_bytes(oid->hash + 1, path, GIT_SHA1_RAWSZ - 1);
 }
 
 static void process_ls_object(struct remote_ls_ctx *ls)
-- 
2.15.0
Previous: René ScharfeNext: Jeff King
Message 2 of 11 in “notes: move hex_to_bytes() to hex.c and export it”
  1. 1/3 notes: move hex_to_bytes() to hex.c and export itRené Scharfe, Oct 31, 2017
  2. 2/3 http-push: use hex_to_bytes()René Scharfe, Oct 31, 2017
  3. Jeff KingNov 1, 2017
  4. René ScharfeNov 1, 2017
  5. Jeff KingNov 1, 2017
  6. René ScharfeNov 4, 2017
  7. 3/3 sha1_file: use hex_to_bytes()René Scharfe, Oct 31, 2017
  8. Jeff KingNov 1, 2017
  9. Kevin DaudtNov 5, 2017
  10. René ScharfeNov 5, 2017
  11. Kevin DaudtNov 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.