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

[PATCH 2/5] Teach 'git config' to rewrite URLs according to current url.*.insteadOf rules

From
Johan Herland <johan@herland.net>
Date
Aug 3, 2008, 23:01 UTC
Message-ID
<200808040101.01082.johan@herland.net>
In-Reply-To
<200808040057.00221.johan@herland.net>

This patch adds the --rewrite-url option to 'git config'. The option takes exactly one argument; a URL that is to be rewritten according to the longest matching url.*.insteadOf rule in the current config. The resulting URL is printed on stdout, and 0 is returned.

The rationale for this patch is to enable access to Git's URL rewriting functionality from shell scripts.

The URL rewriting functionality is implemented by piggybacking on the existing URL rewriting code in remote.c.

Signed-off-by: Johan Herland <johan@herland.net>
---
 builtin-config.c |   23 ++++++++++++++++++++++-
 1 files changed, 22 insertions(+), 1 deletions(-)
diff --git a/builtin-config.c b/builtin-config.c
index 91fdc49..977e6cb 100644
--- a/builtin-config.c
+++ b/builtin-config.c
@@ -1,9 +1,10 @@
 #include "builtin.h"
 #include "cache.h"
 #include "color.h"
+#include "remote.h"
 
 static const char git_config_set_usage[] =
-"git config [ --global | --system | [ -f | --file ] config-file ] [ --bool | --int | --bool-or-int ] [ -z | --null ] 
[--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_name | --remove-section name | --list | --get-color 
var [default] | --get-colorbool name [stdout-is-tty]";
+"git config [ --global | --system | [ -f | --file ] config-file ] [ --bool | --int | --bool-or-int ] [ -z | --null ] 
[--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_name | --remove-section name | --list | --get-color 
var [default] | --get-colorbool name [stdout-is-tty] | --rewrite-url url";
 
 static char *key;
 static regex_t *key_regexp;
@@ -281,6 +282,24 @@ static int get_colorbool(int argc, const char **argv)
 	}
 }
 
+static int rewrite_url(int argc, const char **argv)
+{
+	/*
+	 * git config --rewrite_url <url>
+	 *
+	 * returns <url> after rewriting it according to url.*.insteadOf rules.
+	 */
+
+	if (argc > 1)
+		usage(git_config_set_usage);
+
+	struct remote *remote = remote_get(argv[0]);
+	if (remote->url_nr != 1)
+		die("Expected exactly one URL from remote_get()!");
+	printf("%s\n", remote->url[0]);
+	return 0;
+}
+
 int cmd_config(int argc, const char **argv, const char *prefix)
 {
 	int nongit;
@@ -362,6 +381,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 			return get_color(argc-2, argv+2);
 		} else if (!strcmp(argv[1], "--get-colorbool")) {
 			return get_colorbool(argc-2, argv+2);
+		} else if (!strcmp(argv[1], "--rewrite-url")) {
+			return rewrite_url(argc-2, argv+2);
 		} else
 			break;
 		argc--;
-- 
1.6.0.rc1.34.g0fe8c
Previous: Johan HerlandNext: Johan Herland
Message 4 of 12 in “Fix 'url.*.insteadOf' for submodule URLs”
  1. 0/5 Fix 'url.*.insteadOf' for submodule URLsJohan Herland, Aug 3, 2008
  2. 1/5 Add testcase for 'git submodule' with url.*.insteadOf set in the super-repoJohan Herland, Aug 3, 2008
  3. 1/5 Add testcase for 'git submodule' with url.*.insteadOf set in the super-repoJohan Herland, Aug 3, 2008
  4. 2/5 Teach 'git config' to rewrite URLs according to current url.*.insteadOf rulesJohan Herland, Aug 3, 2008
  5. 3/5 Add selftest for new option '--rewrite-url' to 'git config'Johan Herland, Aug 3, 2008
  6. 4/5 Add documentation on the new --rewrite-url option to 'git config'Johan Herland, Aug 3, 2008
  7. 5/5 Teach 'git submodule' to rewrite submodule URLs according to super-repo's rulesJohan Herland, Aug 3, 2008
  8. Johannes SchindelinAug 3, 2008
  9. Johan HerlandAug 3, 2008
  10. Junio C HamanoAug 4, 2008
  11. Johan HerlandAug 4, 2008
  12. Junio C HamanoAug 3, 2008

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.