Skip to content

Commit 2c6a403

Browse files
bk2204gitster
authored andcommitted
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>
1 parent bf30dbf commit 2c6a403

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

connect.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
static char *server_capabilities_v1;
2020
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);
2223

2324
static int check_ref(const char *name, unsigned int flags)
2425
{
@@ -180,17 +181,16 @@ static void parse_one_symref_info(struct string_list *symref, const char *val, i
180181
static void annotate_refs_with_symref_info(struct ref *ref)
181182
{
182183
struct string_list symref = STRING_LIST_INIT_DUP;
183-
const char *feature_list = server_capabilities_v1;
184+
int offset = 0;
184185

185-
while (feature_list) {
186+
while (1) {
186187
int len;
187188
const char *val;
188189

189-
val = parse_feature_value(feature_list, "symref", &len);
190+
val = next_server_feature_value("symref", &len, &offset);
190191
if (!val)
191192
break;
192193
parse_one_symref_info(&symref, val, len);
193-
feature_list = val + 1;
194194
}
195195
string_list_sort(&symref);
196196

@@ -452,14 +452,16 @@ struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
452452
return list;
453453
}
454454

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)
456456
{
457457
int len;
458458

459459
if (!feature_list)
460460
return NULL;
461461

462462
len = strlen(feature);
463+
if (offset)
464+
feature_list += *offset;
463465
while (*feature_list) {
464466
const char *found = strstr(feature_list, feature);
465467
if (!found)
@@ -474,9 +476,14 @@ static const char *parse_feature_value(const char *feature_list, const char *fea
474476
}
475477
/* feature with a value (e.g., "agent=git/1.2.3") */
476478
else if (*value == '=') {
479+
int end;
480+
477481
value++;
482+
end = strcspn(value, " \t\n");
478483
if (lenp)
479-
*lenp = strcspn(value, " \t\n");
484+
*lenp = end;
485+
if (offset)
486+
*offset = value + end - feature_list;
480487
return value;
481488
}
482489
/*
@@ -491,12 +498,17 @@ static const char *parse_feature_value(const char *feature_list, const char *fea
491498

492499
int parse_feature_request(const char *feature_list, const char *feature)
493500
{
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);
495507
}
496508

497509
const char *server_feature_value(const char *feature, int *len)
498510
{
499-
return parse_feature_value(server_capabilities_v1, feature, len);
511+
return parse_feature_value(server_capabilities_v1, feature, len, NULL);
500512
}
501513

502514
int server_supports(const char *feature)

0 commit comments

Comments
 (0)