Git development
 help / color / mirror / Atom feed
From: Aniket Gupta <aniketgupta3001@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
	Derrick Stolee <stolee@gmail.com>, Patrick Steinhardt <ps@pks.im>
Subject: [PATCH v5 1/3] remote: add relative_url_gently()
Date: Thu, 26 Nov 2026 17:28:01 +0530	[thread overview]
Message-ID: <20261126115803.30015-2-aniketgupta3001@gmail.com> (raw)
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)


  reply	other threads:[~2026-11-26 11:58 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-05 15:28 [PATCH v3 0/3] bundle-uri: do not die on unresolvable relative URIs Aniket Gupta
2026-11-05 15:28 ` [PATCH v3 1/3] remote: add relative_url_gently() Aniket Gupta
2026-11-06 14:36   ` Derrick Stolee
2026-11-05 15:28 ` [PATCH v3 2/3] remote: do not show credentials when relative_url() fails Aniket Gupta
2026-11-06 14:43   ` Derrick Stolee
2026-11-06 19:15     ` Junio C Hamano
2026-11-09 10:39     ` Aniket Gupta
2026-11-05 15:28 ` [PATCH v3 3/3] bundle-uri: do not die on unresolvable relative URIs Aniket Gupta
2026-11-06 14:51   ` Derrick Stolee
2026-11-06 14:57 ` [PATCH v3 0/3] " Derrick Stolee
2026-11-06 17:25   ` Aniket Gupta
2026-11-09  9:26     ` Patrick Steinhardt
2026-11-09 10:56       ` Aniket Gupta
2026-11-23  6:44 ` [PATCH v4 " Aniket Gupta
2026-11-23  6:44   ` [PATCH v4 1/3] remote: add relative_url_gently() Aniket Gupta
2026-11-23  6:44   ` [PATCH v4 2/3] remote: name the original URL when relative_url() fails Aniket Gupta
2026-11-23  6:44   ` [PATCH v4 3/3] bundle-uri: do not die on unresolvable relative URIs Aniket Gupta
2026-11-23 21:04     ` Junio C Hamano
2026-11-23 22:25       ` Aniket Gupta
2026-11-23 14:54   ` [PATCH v4 0/3] " Derrick Stolee
2026-11-26 11:58 ` [PATCH v5 " Aniket Gupta
2026-11-26 11:58   ` Aniket Gupta [this message]
2026-11-26 11:58   ` [PATCH v5 2/3] remote: name the original URL when relative_url() fails Aniket Gupta
2026-11-26 11:58   ` [PATCH v5 3/3] bundle-uri: do not die on unresolvable relative URIs Aniket Gupta
2026-11-30 19:11     ` Derrick Stolee

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126115803.30015-2-aniketgupta3001@gmail.com \
    --to=aniketgupta3001@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=ps@pks.im \
    --cc=stolee@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox