From 194a6f1987b7b2161c3d2cfae8b33a64b58db4c3 Mon Sep 17 00:00:00 2001 From: Oswald Buddenhagen Date: Thu, 25 Jun 2020 17:57:00 +0200 Subject: gpush/gpick: add support for url.*.{push,}insteadOf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Started-by: Tor Arne Vestbø Change-Id: I00e15103c0341c0a197681161605c24f70e080be Reviewed-by: Tor Arne Vestbø --- bin/git_gpush.pm | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'bin') diff --git a/bin/git_gpush.pm b/bin/git_gpush.pm index 65515bb..a94465e 100644 --- a/bin/git_gpush.pm +++ b/bin/git_gpush.pm @@ -313,6 +313,9 @@ sub goto_gitdir() # `git config --list` output, plus contents of .git-gpush-aliases' [config] our %gitconfig; # { key => [ value, ... ] } +# Pre-processed url.*.insteadOf mappings. +my @url_rewrites; +my @url_rewrites_push; sub _load_git_config() { @@ -321,9 +324,20 @@ sub _load_git_config() my $cfg = open_cmd_pipe(0, 'git', 'config', '-l', '-z'); while (read_fields($cfg, my $entry)) { $entry =~ /^([^\n]+)\n(.*)$/; - push @{$gitconfig{$1}}, $2; + my ($key, $value) = ($1, $2); + push @{$gitconfig{$key}}, $value; + + if ($key =~ /^url\.(.*)\.insteadof$/) { + push @url_rewrites, [ $value, $1 ]; + } elsif ($key =~ /^url\.(.*)\.pushinsteadof$/) { + push @url_rewrites_push, [ $value, $1 ]; + } } close_process($cfg); + + # Sort backwards, so longest match is hit first. + @url_rewrites = sort { $$b[0] cmp $$a[0] } @url_rewrites; + @url_rewrites_push = sort { $$b[0] cmp $$a[0] } @url_rewrites_push; } sub git_configs($) @@ -340,6 +354,17 @@ sub git_config($;$) return scalar(@cfg) ? $cfg[-1] : $dflt; } +sub _rewrite_git_url($$) +{ + my ($url, $push) = @_; + + foreach my $ent ($push ? @url_rewrites_push : @url_rewrites) { + my ($pfx, $sub) = @$ent; + return $url if ($url =~ s/^\Q$pfx\E/$sub/); + } + return $url; +} + our $_indexfile; END { unlink($_indexfile) if ($_indexfile); } @@ -2104,9 +2129,10 @@ sub set_gerrit_config($) { my ($rmt) = @_; - my $url = git_config('remote.'.$rmt.'.pushurl'); - $url = git_config('remote.'.$rmt.'.url') if (!$url); + my ($url, $push) = (git_config('remote.'.$rmt.'.pushurl'), 1); + ($url, $push) = (git_config('remote.'.$rmt.'.url'), 0) if (!$url); fail("Remote '$rmt' does not exist.\n") if (!$url); + $url = _rewrite_git_url($url, $push); if ($url =~ m,^ssh://([^/:]+)(?::(\d+))?/(.*?)(?:\.git)?/?$,) { push @gerrit_ssh, '-p', $2 if (defined($2)); push @gerrit_ssh, $1; -- cgit v1.2.1