git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 02/13] urlmatch: define url_parse function

From
Matheus Afonso Martins Moreira via GitGitGadget <gitgitgadget@gmail.com>
Date
Apr 28, 2024, 22:30 UTC
Message-ID
<13b81b8aa06cfd63a5fd9d1acbaf21a8b388ff47.1714343461.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1715.git.git.1714343461.gitgitgadget@gmail.com>
From: Matheus Afonso Martins Moreira <matheus@matheusmoreira.com>

Define general parsing function that supports all Git URLs including scp style URLs such as hostname:~user/repo. Has the same interface as the URL normalization function and uses the same data structures, facilitating its use. It's adapted from the algorithm used to process URLs in connect.c, so it should support the same inputs.

Signed-off-by: Matheus Afonso Martins Moreira <matheus@matheusmoreira.com>
---
 urlmatch.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 urlmatch.h |  1 +
 2 files changed, 91 insertions(+)
diff --git a/urlmatch.c b/urlmatch.c
index 1d0254abacb..5a442e31fa2 100644
--- a/urlmatch.c
+++ b/urlmatch.c
@@ -3,6 +3,7 @@
 #include "hex-ll.h"
 #include "strbuf.h"
 #include "urlmatch.h"
+#include "url.h"
 
 #define URL_ALPHA "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
 #define URL_DIGIT "0123456789"
@@ -438,6 +439,95 @@ char *url_normalize(const char *url, struct url_info *out_info)
 	return url_normalize_1(url, out_info, 0);
 }
 
+enum protocol {
+	PROTO_UNKNOWN = 0,
+	PROTO_LOCAL,
+	PROTO_FILE,
+	PROTO_SSH,
+	PROTO_GIT,
+};
+
+static enum protocol url_get_protocol(const char *name, size_t n)
+{
+	if (!strncmp(name, "ssh", n))
+		return PROTO_SSH;
+	if (!strncmp(name, "git", n))
+		return PROTO_GIT;
+	if (!strncmp(name, "git+ssh", n)) /* deprecated - do not use */
+		return PROTO_SSH;
+	if (!strncmp(name, "ssh+git", n)) /* deprecated - do not use */
+		return PROTO_SSH;
+	if (!strncmp(name, "file", n))
+		return PROTO_FILE;
+	return PROTO_UNKNOWN;
+}
+
+char *url_parse(const char *url_orig, struct url_info *out_info)
+{
+	struct strbuf url;
+	char *host, *separator;
+	char *detached, *normalized;
+	enum protocol protocol = PROTO_LOCAL;
+	struct url_info local_info;
+	struct url_info *info = out_info? out_info : &local_info;
+	bool scp_syntax = false;
+
+	if (is_url(url_orig)) {
+		url_orig = url_decode(url_orig);
+	} else {
+		url_orig = xstrdup(url_orig);
+	}
+
+	strbuf_init(&url, strlen(url_orig) + sizeof("ssh://"));
+	strbuf_addstr(&url, url_orig);
+
+	host = strstr(url.buf, "://");
+	if (host) {
+		protocol = url_get_protocol(url.buf, host - url.buf);
+		host += 3;
+	} else {
+		if (!url_is_local_not_ssh(url.buf)) {
+			scp_syntax = true;
+			protocol = PROTO_SSH;
+			strbuf_insertstr(&url, 0, "ssh://");
+			host = url.buf + 6;
+		}
+	}
+
+	/* path starts after ':' in scp style SSH URLs */
+	if (scp_syntax) {
+		separator = strchr(host, ':');
+		if (separator) {
+			if (separator[1] == '/')
+				strbuf_remove(&url, separator - url.buf, 1);
+			else
+				*separator = '/';
+		}
+	}
+
+	detached = strbuf_detach(&url, NULL);
+	normalized = url_normalize(detached, info);
+	free(detached);
+
+	if (!normalized) {
+		return NULL;
+	}
+
+	/* point path to ~ for URL's like this:
+	 *
+	 *     ssh://host.xz/~user/repo
+	 *     git://host.xz/~user/repo
+	 *     host.xz:~user/repo
+	 *
+	 */
+	if (protocol == PROTO_GIT || protocol == PROTO_SSH) {
+		if (normalized[info->path_off + 1] == '~')
+			info->path_off++;
+	}
+
+	return normalized;
+}
+
 static size_t url_match_prefix(const char *url,
 			       const char *url_prefix,
 			       size_t url_prefix_len)
diff --git a/urlmatch.h b/urlmatch.h
index 5ba85cea139..6b3ce428582 100644
--- a/urlmatch.h
+++ b/urlmatch.h
@@ -35,6 +35,7 @@ struct url_info {
 };
 
 char *url_normalize(const char *, struct url_info *);
+char *url_parse(const char *, struct url_info *);
 
 struct urlmatch_item {
 	size_t hostmatch_len;
-- 
gitgitgadget
Previous: Matheus Afonso Martins Moreira via GitGitGadgetNext: Ghanshyam Thakkar
Message 3 of 44 in “builtin: implement, document and test url-parse”
  1. 00/13 builtin: implement, document and test url-parseMatheus Moreira via GitGitGadget, Apr 28, 2024
  2. 01/13 url: move helper function to URL header and sourceMatheus Afonso Martins Moreira via GitGitGadget, Apr 28, 2024
  3. 02/13 urlmatch: define url_parse functionMatheus Afonso Martins Moreira via GitGitGadget, Apr 28, 2024
  4. Ghanshyam ThakkarMay 1, 2024
  5. Torsten BögershausenMay 2, 2024
  6. 03/13 builtin: create url-parse commandMatheus Afonso Martins Moreira via GitGitGadget, Apr 28, 2024
  7. 04/13 url-parse: add URL parsing helper functionMatheus Afonso Martins Moreira via GitGitGadget, Apr 28, 2024
  8. 05/13 url-parse: enumerate possible URL componentsMatheus Afonso Martins Moreira via GitGitGadget, Apr 28, 2024
  9. 06/13 url-parse: define component extraction helper fnMatheus Afonso Martins Moreira via GitGitGadget, Apr 28, 2024
  10. 07/13 url-parse: define string to component converter fnMatheus Afonso Martins Moreira via GitGitGadget, Apr 28, 2024
  11. 08/13 url-parse: define usage and optionsMatheus Afonso Martins Moreira via GitGitGadget, Apr 28, 2024
  12. 09/13 url-parse: parse options given on the command lineMatheus Afonso Martins Moreira via GitGitGadget, Apr 28, 2024
  13. 10/13 url-parse: validate all given git URLsMatheus Afonso Martins Moreira via GitGitGadget, Apr 28, 2024
  14. 11/13 url-parse: output URL components selected by userMatheus Afonso Martins Moreira via GitGitGadget, Apr 28, 2024
  15. 12/13 Documentation: describe the url-parse builtinMatheus Afonso Martins Moreira via GitGitGadget, Apr 28, 2024
  16. Ghanshyam ThakkarApr 30, 2024
  17. 13/13 tests: add tests for the new url-parse builtinMatheus Afonso Martins Moreira via GitGitGadget, Apr 28, 2024
  18. Torsten BögershausenApr 29, 2024
  19. Reply to community feedbackMatheus Afonso Martins Moreira, Apr 29, 2024
  20. Torsten BögershausenApr 30, 2024
  21. 0/8 builtin: implement, document and test url-parseMatheus Moreira via GitGitGadget, May 1, 2026
  22. 1/8 connect: rename enum protocol to url_schemeMatheus Afonso Martins Moreira via GitGitGadget, May 1, 2026
  23. 2/8 url: move url_is_local_not_ssh to url.hMatheus Afonso Martins Moreira via GitGitGadget, May 1, 2026
  24. 3/8 url: move scheme detection to URL header/sourceMatheus Afonso Martins Moreira via GitGitGadget, May 1, 2026
  25. 4/8 url: return URL_SCHEME_UNKNOWN instead of dyingMatheus Afonso Martins Moreira via GitGitGadget, May 1, 2026
  26. 5/8 urlmatch: define url_parse functionMatheus Afonso Martins Moreira via GitGitGadget, May 1, 2026
  27. 6/8 builtin: create url-parse commandMatheus Afonso Martins Moreira via GitGitGadget, May 1, 2026
  28. 7/8 doc: describe the url-parse builtinMatheus Afonso Martins Moreira via GitGitGadget, May 1, 2026
  29. 8/8 t9904: add tests for the new url-parse builtinMatheus Afonso Martins Moreira via GitGitGadget, May 1, 2026
  30. 0/8 builtin: implement, document and test url-parseMatheus Moreira via GitGitGadget, May 2, 2026
  31. 1/8 connect: rename enum protocol to url_schemeMatheus Afonso Martins Moreira via GitGitGadget, May 2, 2026
  32. 2/8 url: move url_is_local_not_ssh to url.hMatheus Afonso Martins Moreira via GitGitGadget, May 2, 2026
  33. 3/8 url: move scheme detection to URL header/sourceMatheus Afonso Martins Moreira via GitGitGadget, May 2, 2026
  34. 4/8 url: return URL_SCHEME_UNKNOWN instead of dyingMatheus Afonso Martins Moreira via GitGitGadget, May 2, 2026
  35. 5/8 urlmatch: define url_parse functionMatheus Afonso Martins Moreira via GitGitGadget, May 2, 2026
  36. 6/8 builtin: create url-parse commandMatheus Afonso Martins Moreira via GitGitGadget, May 2, 2026
  37. 7/8 doc: describe the url-parse builtinMatheus Afonso Martins Moreira via GitGitGadget, May 2, 2026
  38. 8/8 t9904: add tests for the new url-parse builtinMatheus Afonso Martins Moreira via GitGitGadget, May 2, 2026
  39. Junio C HamanoMay 3, 2026
  40. Matheus Afonso Martins MoreiraMay 3, 2026
  41. Torsten BögershausenMay 3, 2026
  42. Matheus Afonso Martins MoreiraMay 3, 2026
  43. Junio C HamanoMay 12, 2026
  44. Torsten BögershausenMay 12, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.