]> git.ipfire.org Git - thirdparty/git.git/commitdiff
promisor-remote: use string constants for 'name' and 'url' too
authorChristian Couder <christian.couder@gmail.com>
Mon, 8 Sep 2025 05:30:49 +0000 (07:30 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 8 Sep 2025 17:30:54 +0000 (10:30 -0700)
A previous commit started to define `promisor_field_filter` and
`promisor_field_token`, and used them instead of the
"partialCloneFilter" and "token" string literals.

Let's do the same for "name" and "url" to avoid repeating them
several times and for consistency with the other fields.

For skipping "name=" or "url=" in advertisements, let's introduce
a skip_field_name_prefix() helper function to keep parsing clean
and easy to understand.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
promisor-remote.c

index 98ba59e9529332100f9ccdfee4b7c75cfcd2a47e..3913e32c1166cc5f33a0ff3d87d8757ad816a9df 100644 (file)
@@ -314,6 +314,12 @@ static int allow_unsanitized(char ch)
        return ch > 32 && ch < 127;
 }
 
+/*
+ * All the fields used in "promisor-remote" protocol capability,
+ * including the mandatory "name" and "url" ones.
+ */
+static const char promisor_field_name[] = "name";
+static const char promisor_field_url[] = "url";
 static const char promisor_field_filter[] = "partialCloneFilter";
 static const char promisor_field_token[] = "token";
 
@@ -497,9 +503,9 @@ char *promisor_remote_info(struct repository *repo)
                if (item != config_info.items)
                        strbuf_addch(&sb, ';');
 
-               strbuf_addstr(&sb, "name=");
+               strbuf_addf(&sb, "%s=", promisor_field_name);
                strbuf_addstr_urlencode(&sb, p->name, allow_unsanitized);
-               strbuf_addstr(&sb, ",url=");
+               strbuf_addf(&sb, ",%s=", promisor_field_url);
                strbuf_addstr_urlencode(&sb, p->url, allow_unsanitized);
 
                if (p->filter) {
@@ -563,6 +569,15 @@ static int should_accept_remote(enum accept_promisor accept,
        return 0;
 }
 
+static int skip_field_name_prefix(const char *elem, const char *field_name, const char **value)
+{
+       const char *p;
+       if (!skip_prefix(elem, field_name, &p) || *p != '=')
+               return 0;
+       *value = p + 1;
+       return 1;
+}
+
 static void filter_promisor_remote(struct repository *repo,
                                   struct strvec *accepted,
                                   const char *info)
@@ -610,8 +625,8 @@ static void filter_promisor_remote(struct repository *repo,
 
                for (size_t j = 0; elems[j]; j++) {
                        strbuf_strip_suffix(elems[j], ",");
-                       if (!skip_prefix(elems[j]->buf, "name=", &remote_name))
-                               skip_prefix(elems[j]->buf, "url=", &remote_url);
+                       if (!skip_field_name_prefix(elems[j]->buf, promisor_field_name, &remote_name))
+                               skip_field_name_prefix(elems[j]->buf, promisor_field_url, &remote_url);
                }
 
                if (remote_name)