bundle-uri: create "key=value" line parsing
When advertising a bundle list over Git's protocol v2, we will use packet lines. Each line will be of the form "key=value" representing a bundle list. Connect the API necessary for Git's transport to the key-value pair parsing created in the previous change. We are not currently implementing this protocol v2 functionality, but instead preparing to expose this parsing to be unit-testable. Co-authored-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ævar Arnfjörð Bjarmason committed
Oct 12, 2022 at 12:52 UTC
9424e373fd2136aa7f5cec23c8cafc272996ecd6
2 files changed
+38
-1
bundle-uri.c
+26
-1
@@ -71,7 +71,6 @@ int for_all_bundles_in_list(struct bundle_list *list,
71
* Returns 0 if the key-value pair is understood. Returns -1 if the key
72
* is not understood or the value is malformed.
73
*/
74
-MAYBE_UNUSED
74
static int bundle_list_update(const char *key, const char *value,
75
struct bundle_list *list)
76
{
@@ -306,3 +305,29 @@ cleanup:
305
free(filename);
306
return result;
307
}
308
+
309
+/**
310
+ * General API for {transport,connect}.c etc.
311
+ */
312
+int bundle_uri_parse_line(struct bundle_list *list, const char *line)
313
+{
314
+ int result;
315
+ const char *equals;
316
+ struct strbuf key = STRBUF_INIT;
317
+
318
+ if (!strlen(line))
319
+ return error(_("bundle-uri: got an empty line"));
320
+
321
+ equals = strchr(line, '=');
322
+
323
+ if (!equals)
324
+ return error(_("bundle-uri: line is not of the form 'key=value'"));
325
+ if (line == equals || !*(equals + 1))
326
+ return error(_("bundle-uri: line has empty key or value"));
327
+
328
+ strbuf_add(&key, line, equals - line);
329
+ result = bundle_list_update(key.buf, equals + 1, list);
330
+ strbuf_release(&key);
331
+
332
+ return result;
333
+}
bundle-uri.h
+12
@@ -67,4 +67,16 @@ int for_all_bundles_in_list(struct bundle_list *list,
67
*/
68
int fetch_bundle_uri(struct repository *r, const char *uri);
69
70
+/**
71
+ * General API for {transport,connect}.c etc.
72
+ */
73
+
74
+/**
75
+ * Parse a "key=value" packet line from the bundle-uri verb.
76
+ *
77
+ * Returns 0 on success and non-zero on error.
78
+ */
79
+int bundle_uri_parse_line(struct bundle_list *list,
80
+ const char *line);
81
+
82
#endif