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

Re: insteadOf and git-request-pull output

From
Jeff King <peff@peff.net>
Date
Nov 22, 2018, 17:31 UTC
Message-ID
<20181122173109.GI28192@sigill.intra.peff.net>
In-Reply-To
<xmqq7ehbhk63.fsf@gitster-ct.c.googlers.com>
On Sat, Nov 17, 2018 at 11:07:32PM +0900, Junio C Hamano wrote:
Show 28 quoted lines
> Jeff King <peff@peff.net> writes:
> 
> > I suspect it would be less confusing if the rewrite were inverted, like:
> >
> >   [url "gh:"]
> >   rewriteTo = https://github.com
> >   rewritePrivate
> >
> >   [url "git://github.com"]
> >   rewriteTo = https://github.com
> >
> > where the mapping of sections to rewrite rules must be one-to-one, not
> > one-to-many (and you can see that the flip side is that we have to
> > repeat ourselves).
> >
> > I hate to introduce two ways of doing the same thing, but maybe it is
> > simple enough to explain that url.X.insteadOf=Y is identical to
> > url.Y.rewriteTo=X. I do think people have gotten confused by the
> > ordering of insteadOf over the years, so this would let them specify it
> > in whichever way makes the most sense to them.
> 
> I do admit that the current "insteadOf" was too cute a way to
> configure this feature and I often have to read it aloud twice
> before convincing myself I got X and Y right.  It would have been
> cleaner if the definition were in the opposite direction, exactly
> because you can rewrite a single thing into just one way, but we do
> want to support that many things mapping to the same, and the
> approach to use "url.Y.rewriteTo=X" does express it better.

In case anybody is interested in picking this up, the code is actually quite simple (the underlying data structure remains the same; we're just inverting the order in the config). It would need documentation and tests, and probably a matching pushRewriteTo.

    diff --git a/remote.c b/remote.c
    index 28c7260ed1..26b1a7b119 100644
    --- a/remote.c
    +++ b/remote.c
    @@ -345,6 +345,11 @@ static int handle_config(const char *key, const char *value, void *cb)
     				return config_error_nonbool(key);
     			rewrite = make_rewrite(&rewrites_push, name, namelen);
     			add_instead_of(rewrite, xstrdup(value));
    +		} else if (!strcmp(subkey, "rewriteto")) {
    +			if (!value)
    +				return config_error_nonbool(key);
    +			rewrite = make_rewrite(&rewrites, value, strlen(value));
    +			add_instead_of(rewrite, xmemdupz(name, namelen));
     		}
     	}
     

However, I did notice the cleanup below as part of writing that. It may be worth applying independently.

-- >8 --
Subject: [PATCH] remote: check config validity before creating rewrite struct

When parsing url.foo.insteadOf, we call make_rewrite() and only then check to make sure the config value is a string (and return an error if it isn't). This isn't quite a leak, because the struct we allocate is part of a global array, but it does leave a funny half-finished struct.

In practice, it doesn't make much difference because we exit soon after due to the config error anyway. But let's flip the order here to avoid leaving a trap for somebody in the future.

Signed-off-by: Jeff King <peff@peff.net>
---
 remote.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/remote.c b/remote.c
index b850f2feb3..28c7260ed1 100644
--- a/remote.c
+++ b/remote.c
@@ -336,14 +336,14 @@ static int handle_config(const char *key, const char *value, void *cb)
 		if (!name)
 			return 0;
 		if (!strcmp(subkey, "insteadof")) {
-			rewrite = make_rewrite(&rewrites, name, namelen);
 			if (!value)
 				return config_error_nonbool(key);
+			rewrite = make_rewrite(&rewrites, name, namelen);
 			add_instead_of(rewrite, xstrdup(value));
 		} else if (!strcmp(subkey, "pushinsteadof")) {
-			rewrite = make_rewrite(&rewrites_push, name, namelen);
 			if (!value)
 				return config_error_nonbool(key);
+			rewrite = make_rewrite(&rewrites_push, name, namelen);
 			add_instead_of(rewrite, xstrdup(value));
 		}
 	}
-- 
2.20.0.rc1.703.g93fba25b62
Previous: Junio C Hamano
Message 9 of 9 in “insteadOf and git-request-pull output”
  1. Konstantin RyabitsevNov 15, 2018
  2. Ævar Arnfjörð BjarmasonNov 15, 2018
  3. Konstantin RyabitsevNov 15, 2018
  4. Junio C HamanoNov 16, 2018
  5. brian m. carlsonNov 16, 2018
  6. Junio C HamanoNov 17, 2018
  7. Jeff KingNov 17, 2018
  8. Junio C HamanoNov 17, 2018
  9. Jeff KingNov 22, 2018

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.