diff mbox series

[10/44] connect: add function to detect supported v1 hash functions

Message ID 20200513005424.81369-11-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:53 a.m. UTC
Add a function, server_supports_hash, to see if the remote server
supports a particular hash algorithm when speaking protocol v1.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
 connect.c | 24 ++++++++++++++++++++++++
 connect.h |  1 +
 2 files changed, 25 insertions(+)

Comments

Martin Ågren May 13, 2020, 7:39 p.m. UTC | #1
On Wed, 13 May 2020 at 02:56, brian m. carlson
<sandals@crustytoothpaste.net> wrote:
> Add a function, server_supports_hash, to see if the remote server
> supports a particular hash algorithm when speaking protocol v1.

> +int server_supports_hash(const char *desired, int *feature_supported)
> +{
> +       int offset = 0;
> +       int len, found = 0;
> +       const char *hash;
> +
> +       hash = next_server_feature_value("object-format", &len, &offset);
> +       if (feature_supported)
> +               *feature_supported = !!hash;

If we got something, anything, the server supports this feature. It just
remains to see if it supports the exact algorithm we're after.

> +       if (!hash) {
> +               hash = hash_algos[GIT_HASH_SHA1].name;
> +               len = strlen(hash);
> +       }

OK, if the server doesn't say anything, we fall back to SHA-1. If it's
the desired one, we'll return 1 accordingly below.

> +       while (hash) {
> +               if (!xstrncmpz(desired, hash, len))
> +                       found = 1;
> +
> +               if (found)
> +                       return 1;

I first thought this structure was because this loop body would learn to
do something else later in the series. But this is it. This looks like
it could just be "if (!xstrncmpz(...)) return 1;" and drop "found".



> +               hash = next_server_feature_value("object-format", &len, &offset);
> +       }
> +       return 0;
> +}

Martin
brian m. carlson May 13, 2020, 10:49 p.m. UTC | #2
On 2020-05-13 at 19:39:41, Martin Ågren wrote:
> On Wed, 13 May 2020 at 02:56, brian m. carlson
> <sandals@crustytoothpaste.net> wrote:
> > +       while (hash) {
> > +               if (!xstrncmpz(desired, hash, len))
> > +                       found = 1;
> > +
> > +               if (found)
> > +                       return 1;
> 
> I first thought this structure was because this loop body would learn to
> do something else later in the series. But this is it. This looks like
> it could just be "if (!xstrncmpz(...)) return 1;" and drop "found".

Yeah, I think it could.  I originally didn't have the helper and the
code was pretty hideous, so I probably forgot to simplify when I used
the helper again.
diff mbox series

Patch

diff --git a/connect.c b/connect.c
index 4df9e77206..cb69aafe2c 100644
--- a/connect.c
+++ b/connect.c
@@ -511,6 +511,30 @@  static const char *parse_feature_value(const char *feature_list, const char *fea
 	return NULL;
 }
 
+int server_supports_hash(const char *desired, int *feature_supported)
+{
+	int offset = 0;
+	int len, found = 0;
+	const char *hash;
+
+	hash = next_server_feature_value("object-format", &len, &offset);
+	if (feature_supported)
+		*feature_supported = !!hash;
+	if (!hash) {
+		hash = hash_algos[GIT_HASH_SHA1].name;
+		len = strlen(hash);
+	}
+	while (hash) {
+		if (!xstrncmpz(desired, hash, len))
+			found = 1;
+
+		if (found)
+			return 1;
+		hash = next_server_feature_value("object-format", &len, &offset);
+	}
+	return 0;
+}
+
 int parse_feature_request(const char *feature_list, const char *feature)
 {
 	return !!parse_feature_value(feature_list, feature, NULL, NULL);
diff --git a/connect.h b/connect.h
index 4d76a6017d..fc75d6a457 100644
--- a/connect.h
+++ b/connect.h
@@ -18,6 +18,7 @@  int url_is_local_not_ssh(const char *url);
 struct packet_reader;
 enum protocol_version discover_version(struct packet_reader *reader);
 
+int server_supports_hash(const char *desired, int *feature_supported);
 int server_supports_v2(const char *c, int die_on_error);
 int server_feature_v2(const char *c, const char **v);
 int server_supports_feature(const char *c, const char *feature,