diff mbox series

[35/44] remote-curl: detect algorithm for dumb HTTP by size

Message ID 20200513005424.81369-36-sandals@crustytoothpaste.net (mailing list archive)
State New, archived
Headers show
Series SHA-256 part 2/3: protocol functionality | expand

Commit Message

brian m. carlson May 13, 2020, 12:54 a.m. UTC
When reading the info/refs file for a repository, we have no explicit
way to detect which hash algorithm is in use because the file doesn't
provide one. Detect the hash algorithm in use by the size of the first
object ID.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
 remote-curl.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

Comments

Martin Ă…gren May 16, 2020, 11:17 a.m. UTC | #1
On Wed, 13 May 2020 at 02:57, brian m. carlson
<sandals@crustytoothpaste.net> wrote:

> +       options.hash_algo = detect_hash_algo(heads);
> +       if (!options.hash_algo)
> +               die("%sinfo/refs not valid: could not determine hash algorithm; "
> +                   "is this a git repository?",
> +                   url.buf);

Should this use `transport_anonymize_url()`?

>                 if (data[i] == '\n') {
> -                       if (mid - start != the_hash_algo->hexsz)
> +                       if (mid - start != options.hash_algo->hexsz)
>                                 die(_("%sinfo/refs not valid: is this a git repository?"),
>                                     transport_anonymize_url(url.buf));

Like here and elsewhere.

>                         data[i] = 0;
>                         ref_name = mid + 1;
>                         ref = alloc_ref(ref_name);
> -                       get_oid_hex(start, &ref->old_oid);
> +                       get_oid_hex_algop(start, &ref->old_oid, options.hash_algo);

Other than that, looks ok.



Martin
diff mbox series

Patch

diff --git a/remote-curl.c b/remote-curl.c
index 3ed0dfec1b..35275b42e9 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -252,6 +252,19 @@  static struct ref *parse_git_refs(struct discovery *heads, int for_push)
 	return list;
 }
 
+static const struct git_hash_algo *detect_hash_algo(struct discovery *heads)
+{
+	const char *p = memchr(heads->buf, '\t', heads->len);
+	int algo;
+	if (!p)
+		return NULL;
+
+	algo = hash_algo_by_length((p - heads->buf) / 2);
+	if (algo == GIT_HASH_UNKNOWN)
+		return NULL;
+	return &hash_algos[algo];
+}
+
 static struct ref *parse_info_refs(struct discovery *heads)
 {
 	char *data, *start, *mid;
@@ -262,6 +275,12 @@  static struct ref *parse_info_refs(struct discovery *heads)
 	struct ref *ref = NULL;
 	struct ref *last_ref = NULL;
 
+	options.hash_algo = detect_hash_algo(heads);
+	if (!options.hash_algo)
+		die("%sinfo/refs not valid: could not determine hash algorithm; "
+		    "is this a git repository?",
+		    url.buf);
+
 	data = heads->buf;
 	start = NULL;
 	mid = data;
@@ -272,13 +291,13 @@  static struct ref *parse_info_refs(struct discovery *heads)
 		if (data[i] == '\t')
 			mid = &data[i];
 		if (data[i] == '\n') {
-			if (mid - start != the_hash_algo->hexsz)
+			if (mid - start != options.hash_algo->hexsz)
 				die(_("%sinfo/refs not valid: is this a git repository?"),
 				    transport_anonymize_url(url.buf));
 			data[i] = 0;
 			ref_name = mid + 1;
 			ref = alloc_ref(ref_name);
-			get_oid_hex(start, &ref->old_oid);
+			get_oid_hex_algop(start, &ref->old_oid, options.hash_algo);
 			if (!refs)
 				refs = ref;
 			if (last_ref)