connect: add function to parse multiple v1 capability values
In a capability response, we can have multiple symref entries. In the future, we will also allow for multiple hash algorithms to be specified. To avoid duplication, expand the parse_feature_value function to take an optional offset where the parsing should begin next time. Add a wrapper function that allows us to query the next server feature value, and use it in the existing symref parsing code. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
May 25, 2020 at 19:58 UTC
2c6a403d96cd5d31f1638679502f06e2b953647f
1 file changed
+21
-9
connect.c
+21
-9
@@ -18,7 +18,8 @@
18
19
static char *server_capabilities_v1;
20
static struct argv_array server_capabilities_v2 = ARGV_ARRAY_INIT;
21
-static const char *parse_feature_value(const char *, const char *, int *);
21
+static const char *parse_feature_value(const char *, const char *, int *, int *);
22
+static const char *next_server_feature_value(const char *feature, int *len, int *offset);
23
24
static int check_ref(const char *name, unsigned int flags)
25
{
@@ -180,17 +181,16 @@ reject:
181
static void annotate_refs_with_symref_info(struct ref *ref)
182
{
183
struct string_list symref = STRING_LIST_INIT_DUP;
183
- const char *feature_list = server_capabilities_v1;
184
+ int offset = 0;
185
185
- while (feature_list) {
186
+ while (1) {
187
int len;
188
const char *val;
189
189
- val = parse_feature_value(feature_list, "symref", &len);
190
+ val = next_server_feature_value("symref", &len, &offset);
191
if (!val)
192
break;
193
parse_one_symref_info(&symref, val, len);
193
- feature_list = val + 1;
194
}
195
string_list_sort(&symref);
196
@@ -452,7 +452,7 @@ struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
452
return list;
453
}
454
455
-static const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp)
455
+static const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp, int *offset)
456
{
457
int len;
458
@@ -460,6 +460,8 @@ static const char *parse_feature_value(const char *feature_list, const char *fea
460
return NULL;
461
462
len = strlen(feature);
463
+ if (offset)
464
+ feature_list += *offset;
465
while (*feature_list) {
466
const char *found = strstr(feature_list, feature);
467
if (!found)
@@ -474,9 +476,14 @@ static const char *parse_feature_value(const char *feature_list, const char *fea
476
}
477
/* feature with a value (e.g., "agent=git/1.2.3") */
478
else if (*value == '=') {
479
+ int end;
480
+
481
value++;
482
+ end = strcspn(value, " \t\n");
483
if (lenp)
479
- *lenp = strcspn(value, " \t\n");
484
+ *lenp = end;
485
+ if (offset)
486
+ *offset = value + end - feature_list;
487
return value;
488
}
489
/*
@@ -491,12 +498,17 @@ static const char *parse_feature_value(const char *feature_list, const char *fea
498
499
int parse_feature_request(const char *feature_list, const char *feature)
500
{
494
- return !!parse_feature_value(feature_list, feature, NULL);
501
+ return !!parse_feature_value(feature_list, feature, NULL, NULL);
502
+}
503
+
504
+static const char *next_server_feature_value(const char *feature, int *len, int *offset)
505
+{
506
+ return parse_feature_value(server_capabilities_v1, feature, len, offset);
507
}
508
509
const char *server_feature_value(const char *feature, int *len)
510
{
499
- return parse_feature_value(server_capabilities_v1, feature, len);
511
+ return parse_feature_value(server_capabilities_v1, feature, len, NULL);
512
}
513
514
int server_supports(const char *feature)