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

[PATCH v5 1/3] remote: add relative_url_gently()

From
AGAniket Gupta <aniketgupta3001@gmail.com>
Date
Nov 26, 2026, 11:58 UTC
Message-ID
<20261126115803.30015-2-aniketgupta3001@gmail.com>
In-Reply-To
<20261126115803.30015-1-aniketgupta3001@gmail.com>

relative_url() calls die() when there are more "../" components in the URL than the base URL can absorb. A caller that would rather carry on has no way to do so.

Add relative_url_gently(), which returns NULL and adds the reason to a strbuf instead of dying. relative_url() is now a thin wrapper around it that continues to die, so existing callers behave the same.

Signed-off-by: Aniket Gupta <aniketgupta3001@gmail.com>
---
 remote.c | 34 ++++++++++++++++++++++++++++------
 remote.h |  8 ++++++++
 2 files changed, 36 insertions(+), 6 deletions(-)
diff --git a/remote.c b/remote.c
index 2b7d26b844..3bca10364e 100644
--- a/remote.c
+++ b/remote.c
@@ -3108,7 +3108,8 @@ void remote_state_clear(struct remote_state *remote_state)
 }
 
 /*
- * Returns 1 if it was the last chop before ':'.
+ * Returns 1 if it was the last chop before ':', or -1 if there is
+ * nothing left to chop.
  */
 static int chop_last_dir(char **remoteurl, int is_relative)
 {
@@ -3125,16 +3126,15 @@ static int chop_last_dir(char **remoteurl, int is_relative)
 	}
 
 	if (is_relative || !strcmp(".", *remoteurl))
-		die(_("cannot strip one component off url '%s'"),
-			*remoteurl);
+		return -1;
 
 	free(*remoteurl);
 	*remoteurl = xstrdup(".");
 	return 0;
 }
 
-char *relative_url(const char *remote_url, const char *url,
-		   const char *up_path)
+char *relative_url_gently(const char *remote_url, const char *url,
+			  const char *up_path, struct strbuf *err)
 {
 	int is_relative = 0;
 	int colonsep = 0;
@@ -3176,8 +3176,19 @@ char *relative_url(const char *remote_url, const char *url,
 	 */
 	while (*url) {
 		if (starts_with_dot_dot_slash_native(url)) {
+			int chopped;
+
 			url += 3;
-			colonsep |= chop_last_dir(&remoteurl, is_relative);
+			chopped = chop_last_dir(&remoteurl, is_relative);
+			if (chopped < 0) {
+				strbuf_addf(err,
+					    _("cannot strip one component off url '%s'"),
+					    remoteurl);
+				free(remoteurl);
+				strbuf_release(&sb);
+				return NULL;
+			}
+			colonsep |= chopped;
 		} else if (starts_with_dot_slash_native(url))
 			url += 2;
 		else
@@ -3205,6 +3216,17 @@ char *relative_url(const char *remote_url, const char *url,
 	return strbuf_detach(&sb, NULL);
 }
 
+char *relative_url(const char *remote_url, const char *url,
+		   const char *up_path)
+{
+	struct strbuf err = STRBUF_INIT;
+	char *out = relative_url_gently(remote_url, url, up_path, &err);
+
+	if (!out)
+		die("%s", err.buf);
+	return out;
+}
+
 int valid_remote_name(const char *name)
 {
 	int result;
diff --git a/remote.h b/remote.h
index cca02033b9..940474fa71 100644
--- a/remote.h
+++ b/remote.h
@@ -478,6 +478,14 @@ void apply_push_cas(struct push_cas_option *, struct remote *, struct ref *);
 char *relative_url(const char *remote_url, const char *url,
 		   const char *up_path);
 
+/*
+ * Like relative_url(), but if `url` cannot be resolved against
+ * `remote_url`, return NULL and add the reason to `err` instead of
+ * dying. `err` must not be NULL.
+ */
+char *relative_url_gently(const char *remote_url, const char *url,
+			  const char *up_path, struct strbuf *err);
+
 int valid_remote_name(const char *name);
 
 #endif
-- 
2.54.0 (Apple Git-157)
Previous: Aniket GuptaNext: Aniket Gupta
Message 22 of 37 in “bundle-uri: do not die on unresolvable relative URIs”
  1. 0/3 bundle-uri: do not die on unresolvable relative URIsAniket Gupta, Nov 5, 2026
  2. 1/3 remote: add relative_url_gently()Aniket Gupta, Nov 5, 2026
  3. Derrick StoleeNov 6, 2026
  4. 2/3 remote: do not show credentials when relative_url() failsAniket Gupta, Nov 5, 2026
  5. Derrick StoleeNov 6, 2026
  6. Junio C HamanoNov 6, 2026
  7. Aniket GuptaNov 9, 2026
  8. 3/3 bundle-uri: do not die on unresolvable relative URIsAniket Gupta, Nov 5, 2026
  9. Derrick StoleeNov 6, 2026
  10. Derrick StoleeNov 6, 2026
  11. Aniket GuptaNov 6, 2026
  12. Patrick SteinhardtNov 9, 2026
  13. Aniket GuptaNov 9, 2026
  14. 0/3 bundle-uri: do not die on unresolvable relative URIsAniket Gupta, Nov 23, 2026
  15. 1/3 remote: add relative_url_gently()Aniket Gupta, Nov 23, 2026
  16. 2/3 remote: name the original URL when relative_url() failsAniket Gupta, Nov 23, 2026
  17. 3/3 bundle-uri: do not die on unresolvable relative URIsAniket Gupta, Nov 23, 2026
  18. Junio C HamanoNov 23, 2026
  19. Aniket GuptaNov 23, 2026
  20. Derrick StoleeNov 23, 2026
  21. 0/3 bundle-uri: do not die on unresolvable relative URIsAniket Gupta, Nov 26, 2026
  22. 1/3 remote: add relative_url_gently()Aniket Gupta, Nov 26, 2026
  23. 2/3 remote: name the original URL when relative_url() failsAniket Gupta, Nov 26, 2026
  24. Patrick SteinhardtDec 1, 2026
  25. Aniket GuptaDec 1, 2026
  26. 3/3 bundle-uri: do not die on unresolvable relative URIsAniket Gupta, Nov 26, 2026
  27. Derrick StoleeNov 30, 2026
  28. Patrick SteinhardtDec 1, 2026
  29. Aniket GuptaDec 1, 2026
  30. Patrick SteinhardtDec 2, 2026
  31. Patrick SteinhardtDec 1, 2026
  32. Aniket GuptaDec 1, 2026
  33. 0/3 bundle-uri: do not die on unresolvable relative URIsAniket Gupta, Dec 2, 2026
  34. 1/3 remote: add relative_url_gently()Aniket Gupta, Dec 2, 2026
  35. 2/3 remote: name the original URL when relative_url() failsAniket Gupta, Dec 2, 2026
  36. 3/3 bundle-uri: do not die on unresolvable relative URIsAniket Gupta, Dec 2, 2026
  37. Patrick SteinhardtDec 3, 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.