git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "René Scharfe" <l.s.r@web.de>
To: Jeff King <peff@peff.net>
Cc: Git List <git@vger.kernel.org>,
	Junio C Hamano <gitster@pobox.com>,
	"brian m. carlson" <sandals@crustytoothpaste.net>,
	Michael Haggerty <mhagger@alum.mit.edu>,
	Thomas Gummerer <t.gummerer@gmail.com>
Subject: Re: [PATCH 2/3] http-push: use hex_to_bytes()
Date: Sat, 4 Nov 2017 10:05:37 +0100	[thread overview]
Message-ID: <abf7c219-880b-790f-78ef-18c7cdd54ad6@web.de> (raw)
In-Reply-To: <20171101221541.6dwezd4t4hweuis2@sigill.intra.peff.net>

Am 01.11.2017 um 23:15 schrieb Jeff King:
> On Wed, Nov 01, 2017 at 10:59:49PM +0100, René Scharfe wrote:
> 
>>> The hex_to_bytes() function requires that the caller make sure they have
>>> the right number of bytes. But for many callers, I think they'd want to
>>> say "parse this oid, which might be truncated; I can't tell what the
>>> length is supposed to be".
>>
>> I'm confused by the word "many".  After this series there are three
>> callers of hex_to_bytes() and I don't expect that number to grow.
> 
> I meant only that most callers that parse oids, both in-file and not,
> would want to stop knowing about the length ahead of time.

That's a good idea.

> I'm not sure we know 100%
> yet what "new"-style hashes will look like, nor how their loose-object
> filenames would look.

So it's too early to implement a solution, but here's how a patch for
reducing dependency on hash lengths could look like today:

---
 cache.h     |  2 ++
 hex.c       | 13 +++++++++++++
 http-push.c |  7 +++----
 notes.c     |  6 +-----
 sha1_file.c |  4 +---
 5 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/cache.h b/cache.h
index f06bfbaf32..acd3804c21 100644
--- a/cache.h
+++ b/cache.h
@@ -1317,6 +1317,8 @@ extern int set_disambiguate_hint_config(const char *var, const char *value);
 extern int get_sha1_hex(const char *hex, unsigned char *sha1);
 extern int get_oid_hex(const char *hex, struct object_id *sha1);
 
+extern int get_oid_hex_tail(const char *hex, struct object_id *oid, size_t offset);
+
 /*
  * Read `len` pairs of hexadecimal digits from `hex` and write the
  * values to `binary` as `len` bytes. Return 0 on success, or -1 if
diff --git a/hex.c b/hex.c
index 8df2d63728..3e6abe4d5e 100644
--- a/hex.c
+++ b/hex.c
@@ -47,6 +47,19 @@ int hex_to_bytes(unsigned char *binary, const char *hex, size_t len)
 	return 0;
 }
 
+int get_oid_hex_tail(const char *hex, struct object_id *oid, size_t offset)
+{
+	for (; offset < GIT_SHA1_RAWSZ; offset++, hex += 2) {
+		int val = hex2chr(hex);
+		if (val < 0)
+			return -1;
+		oid->hash[offset] = val;
+	}
+	if (*hex)
+		return -1;
+	return 0;
+}
+
 int get_sha1_hex(const char *hex, unsigned char *sha1)
 {
 	int i;
diff --git a/http-push.c b/http-push.c
index 14435ab65d..a5512616b9 100644
--- a/http-push.c
+++ b/http-push.c
@@ -1007,18 +1007,17 @@ static void remote_ls(const char *path, int flags,
 		      void (*userFunc)(struct remote_ls_ctx *ls),
 		      void *userData);
 
-/* extract hex from sharded "xx/x{38}" filename */
+/* extract hex from sharded "xx/x{N}" filename */
 static int get_oid_hex_from_objpath(const char *path, struct object_id *oid)
 {
-	if (strlen(path) != GIT_SHA1_HEXSZ + 1)
+	if (strnlen(path, 3) < 3)
 		return -1;
-
 	if (hex_to_bytes(oid->hash, path, 1))
 		return -1;
 	path += 2;
 	path++; /* skip '/' */
 
-	return hex_to_bytes(oid->hash + 1, path, GIT_SHA1_RAWSZ - 1);
+	return get_oid_hex_tail(path, oid, 1);
 }
 
 static void process_ls_object(struct remote_ls_ctx *ls)
diff --git a/notes.c b/notes.c
index 04f8c8613c..ff6ce57022 100644
--- a/notes.c
+++ b/notes.c
@@ -410,17 +410,13 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
 		struct leaf_node *l;
 		size_t path_len = strlen(entry.path);
 
-		if (path_len == 2 * (GIT_SHA1_RAWSZ - prefix_len)) {
+		if (!get_oid_hex_tail(entry.path, &object_oid, prefix_len)) {
 			/* This is potentially the remainder of the SHA-1 */
 
 			if (!S_ISREG(entry.mode))
 				/* notes must be blobs */
 				goto handle_non_note;
 
-			if (hex_to_bytes(object_oid.hash + prefix_len, entry.path,
-					 GIT_SHA1_RAWSZ - prefix_len))
-				goto handle_non_note; /* entry.path is not a SHA1 */
-
 			type = PTR_TYPE_NOTE;
 		} else if (path_len == 2) {
 			/* This is potentially an internal node */
diff --git a/sha1_file.c b/sha1_file.c
index a3c32d91d1..0486696b0b 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -1911,9 +1911,7 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
 		strbuf_setlen(path, baselen);
 		strbuf_addf(path, "/%s", de->d_name);
 
-		if (strlen(de->d_name) == GIT_SHA1_HEXSZ - 2 &&
-		    !hex_to_bytes(oid.hash + 1, de->d_name,
-				  GIT_SHA1_RAWSZ - 1)) {
+		if (!get_oid_hex_tail(de->d_name, &oid, 1)) {
 			if (obj_cb) {
 				r = obj_cb(&oid, path->buf, data);
 				if (r)
-- 
2.15.0

  reply	other threads:[~2017-11-04  9:06 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-31 13:46 [PATCH 1/3] notes: move hex_to_bytes() to hex.c and export it René Scharfe
2017-10-31 13:49 ` [PATCH 2/3] http-push: use hex_to_bytes() René Scharfe
2017-11-01 19:55   ` Jeff King
2017-11-01 21:59     ` René Scharfe
2017-11-01 22:15       ` Jeff King
2017-11-04  9:05         ` René Scharfe [this message]
2017-10-31 13:50 ` [PATCH 3/3] sha1_file: " René Scharfe
2017-11-01 19:58   ` Jeff King
2017-11-05  2:56 ` [PATCH 1/3] notes: move hex_to_bytes() to hex.c and export it Kevin Daudt
2017-11-05 16:47   ` René Scharfe
2017-11-05 19:57     ` Kevin Daudt

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=abf7c219-880b-790f-78ef-18c7cdd54ad6@web.de \
    --to=l.s.r@web.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=mhagger@alum.mit.edu \
    --cc=peff@peff.net \
    --cc=sandals@crustytoothpaste.net \
    --cc=t.gummerer@gmail.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).