urlmatch: define url_parse function

Define url_parse, a general parsing function that supports all Git URLs including scp style URLs such as hostname:~user/repo. It is adapted from the algorithm in connect.c's parse_connect_url and reuses the shared enum url_scheme and url_get_scheme function that previous commits made available in url.h. The new parser and the connect path agree on scheme classification. url_parse has the same interface as url_normalize and uses the same data structures. Both functions accept the same URL forms with one deliberate exception. Bare local paths such as "/abs/path", "./rel" or "repo" are accepted by parse_connect_url as URL_SCHEME_LOCAL, but rejected by url_parse because url_normalize requires a URL with a scheme://host form. A consumer that wants to handle both URLs and local paths needs to dispatch on url_is_local_not_ssh before calling url_parse, just as the connect path does internally. The duplication with parse_connect_url is intentional. The two functions have different contracts: - parse_connect_url Calls die() on an unknown scheme and returns NUL-terminated host/path strings for the connect path - url_parse Returns NULL on failure while populating out_info->err, and exposes components as offset/length pairs into the normalized URL buffer, matching url_normalize. Reconciling both is possible, but not in the scope of the current patch set. Signed-off-by: Matheus Afonso Martins Moreira <matheus@matheusmoreira.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matheus Afonso Martins Moreira committed May 2, 2026 at 05:28 UTC 18a828171243b630bc7585c7bc8d85bb37125c01
3 files changed +173
t/unit-tests/u-urlmatch-normalization.c
+45
@@ -245,3 +245,48 @@ void test_urlmatch_normalization__equivalents(void)
245 compare_normalized_urls("https://@x.y/^/../abc", "httpS://@x.y:0443/abc", 1);
246 compare_normalized_urls("https://@x.y/^/..", "httpS://@x.y:0443/", 1);
247 }
248 +
249 +static void check_parsed_path(const char *url, const char *expected_path)
250 +{
251 + struct url_info info;
252 + char *parsed = url_parse(url, &info);
253 + char *path;
254 +
255 + cl_assert(parsed != NULL);
256 + path = xstrndup(parsed + info.path_off, info.path_len);
257 + cl_assert_equal_s(path, expected_path);
258 + free(path);
259 + free(parsed);
260 +}
261 +
262 +void test_urlmatch_normalization__parse_scp(void)
263 +{
264 + check_parsed_path("host:path", "/path");
265 + check_parsed_path("user@host:path", "/path");
266 + check_parsed_path("host:~user/repo", "~user/repo");
267 + check_parsed_path("user@host:~user/repo", "~user/repo");
268 + check_parsed_path("[host]:src", "/src");
269 + check_parsed_path("[host:123]:src", "/src");
270 + check_parsed_path("[::1]:repo", "/repo");
271 + check_parsed_path("user@[::1]:repo", "/repo");
272 +}
273 +
274 +void test_urlmatch_normalization__parse_url_form(void)
275 +{
276 + check_parsed_path("ssh://host/repo", "/repo");
277 + check_parsed_path("ssh://host/~user/repo", "~user/repo");
278 + check_parsed_path("git://host:9418/repo", "/repo");
279 + check_parsed_path("git://host/~user/repo", "~user/repo");
280 + check_parsed_path("ssh://[::1]:1234/repo", "/repo");
281 + check_parsed_path("http://[2001:db8::1]/repo", "/repo");
282 +}
283 +
284 +void test_urlmatch_normalization__parse_strips_query_and_fragment(void)
285 +{
286 + check_parsed_path("ssh://host/~user/repo?q", "~user/repo");
287 + check_parsed_path("ssh://host/~user/repo#frag", "~user/repo");
288 + check_parsed_path("git://host/~user/repo?q", "~user/repo");
289 + check_parsed_path("user@host:~user/repo?q", "~user/repo");
290 + check_parsed_path("https://host/repo?q", "/repo");
291 + check_parsed_path("https://host/repo#frag", "/repo");
292 +}
urlmatch.c
+127
@@ -5,6 +5,7 @@
5 #include "hex-ll.h"
6 #include "strbuf.h"
7 #include "urlmatch.h"
8 +#include "url.h"
9
10 #define URL_ALPHA "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
11 #define URL_DIGIT "0123456789"
@@ -440,6 +441,132 @@ char *url_normalize(const char *url, struct url_info *out_info)
441 return url_normalize_1(url, out_info, 0);
442 }
443
444 +char *url_parse(const char *url_orig, struct url_info *out_info)
445 +{
446 + struct strbuf url;
447 + char *host, *separator;
448 + char *detached, *normalized;
449 + char *url_decoded;
450 + enum url_scheme scheme = URL_SCHEME_LOCAL;
451 + struct url_info local_info;
452 + struct url_info *info = out_info ? out_info : &local_info;
453 + bool scp_syntax = false;
454 +
455 + if (is_url(url_orig))
456 + url_decoded = url_decode(url_orig);
457 + else
458 + url_decoded = xstrdup(url_orig);
459 +
460 + strbuf_init(&url, strlen(url_decoded) + sizeof("ssh://"));
461 + strbuf_addstr(&url, url_decoded);
462 + free(url_decoded);
463 +
464 + host = strstr(url.buf, "://");
465 + if (host) {
466 + /*
467 + * Temporarily NUL-terminate the scheme name
468 + * so we can pass it to url_get_scheme(),
469 + * then restore the ':' so the buffer
470 + * is intact for url_normalize() below.
471 + */
472 + char saved = *host;
473 + *host = '\0';
474 + scheme = url_get_scheme(url.buf);
475 + *host = saved;
476 + host += 3;
477 + } else {
478 + if (!url_is_local_not_ssh(url.buf)) {
479 + scp_syntax = true;
480 + scheme = URL_SCHEME_SSH;
481 + strbuf_insertstr(&url, 0, "ssh://");
482 + host = url.buf + strlen("ssh://");
483 + }
484 + }
485 +
486 + /*
487 + * Path starts after ':' in scp style SSH URLs.
488 + *
489 + * The host portion can begin with an optional "user@",
490 + * and the host itself can be wrapped in '[' ']' brackets.
491 + * The bracket form is git's legacy way of supporting:
492 + *
493 + * - IPv6 literals: [::1]:repo
494 + * - host:port pairs in the short form: [myhost:123]:src
495 + * - Plain hostnames that happen to need bracketing: [host]:path
496 + *
497 + * Treat '[' followed by 0 or 1 inner colons as the host:port
498 + * or plain hostname form and strip the brackets so url_normalize
499 + * sees host[:port] natively. Two or more inner colons mark an
500 + * IPv6 literal: keep the brackets for url_normalize to recognize.
501 + *
502 + * The scp path separator is the ':' that follows the host part,
503 + * and we must skip over user@ and any '[...]' before searching.
504 + */
505 + if (scp_syntax) {
506 + char *user_at;
507 + char *host_start;
508 + char *bracket_end;
509 +
510 + user_at = strchr(host, '@');
511 + host_start = user_at ? user_at + 1 : host;
512 +
513 + if (*host_start == '[') {
514 + char *p;
515 + int inner_colons;
516 +
517 + bracket_end = strchr(host_start, ']');
518 + inner_colons = 0;
519 + for (p = host_start + 1; bracket_end && p < bracket_end; p++)
520 + if (*p == ':')
521 + inner_colons++;
522 +
523 + if (bracket_end && inner_colons <= 1) {
524 + size_t close_off = bracket_end - url.buf;
525 + size_t open_off = host_start - url.buf;
526 + strbuf_remove(&url, close_off, 1);
527 + strbuf_remove(&url, open_off, 1);
528 + separator = url.buf + close_off - 1;
529 + } else if (bracket_end) {
530 + separator = strchr(bracket_end + 1, ':');
531 + } else {
532 + separator = strchr(host_start, ':');
533 + }
534 + } else {
535 + separator = strchr(host_start, ':');
536 + }
537 +
538 + if (separator) {
539 + if (separator[1] == '/')
540 + strbuf_remove(&url, separator - url.buf, 1);
541 + else
542 + *separator = '/';
543 + }
544 + }
545 +
546 + detached = strbuf_detach(&url, NULL);
547 + normalized = url_normalize(detached, info);
548 + free(detached);
549 +
550 + if (!normalized)
551 + return NULL;
552 +
553 + /*
554 + * Point path to ~ for URLs like this:
555 + *
556 + * ssh://host.xz/~user/repo
557 + * git://host.xz/~user/repo
558 + * host.xz:~user/repo
559 + */
560 + if (scheme == URL_SCHEME_GIT || scheme == URL_SCHEME_SSH) {
561 + if (normalized[info->path_off + 1] == '~') {
562 + info->path_off++;
563 + info->path_len--;
564 + }
565 + }
566 +
567 + return normalized;
568 +}
569 +
570 static size_t url_match_prefix(const char *url,
571 const char *url_prefix,
572 size_t url_prefix_len)
urlmatch.h
+1
@@ -35,6 +35,7 @@ struct url_info {
35 };
36
37 char *url_normalize(const char *, struct url_info *);
38 +char *url_parse(const char *, struct url_info *);
39
40 struct urlmatch_item {
41 size_t hostmatch_len;