]> Sergey Matveev's repositories - public-inbox.git/commitdiff
fetch: support --try-remote/-T for alternate remote names
authorEric Wong <e@80x24.org>
Wed, 13 Oct 2021 11:06:21 +0000 (11:06 +0000)
committerEric Wong <e@80x24.org>
Wed, 13 Oct 2021 19:52:26 +0000 (19:52 +0000)
This allows -fetch to work out-of-the-box on using the
grokmirror 2.x default of "_grokmirror".

Documentation/public-inbox-fetch.pod
lib/PublicInbox/Fetch.pm
script/public-inbox-fetch

index 833df862b556348dbe944e996d05df87033e2ccf..1890ae75928817d6a3ad9c0eda17147e479e512e 100644 (file)
@@ -43,6 +43,15 @@ If you wish to re-enable fetches to the epoch:
 
 Quiets down progress messages, also passed to L<git-fetch(1)>.
 
+=item -T REMOTE
+
+=item --try-remote REMOTE
+
+Try a given remote name instead of C<origin> or C<_grokmirror>.
+May be specified more than once.
+
+Default: C<origin>, C<_grokmirror>
+
 =item --exit-code
 
 Exit with C<127> if no updates are done.  This can be used in
index e5756fb664e44cd774a6daa20214cd6c0291a943..0d4badbf216f87ba50a5e1d7a8986ca2a3117468 100644 (file)
@@ -31,13 +31,17 @@ sub fetch_args ($$) {
 }
 
 sub remote_url ($$) {
-       my ($lei, $dir) = @_; # TODO: support non-"origin"?
-       my $cmd = [ qw(git config remote.origin.url) ];
-       my $fh = popen_rd($cmd, undef, { -C => $dir, 2 => $lei->{2} });
-       my $url = <$fh>;
-       close $fh or return;
-       $url =~ s!/*\n!!s;
-       $url;
+       my ($lei, $dir) = @_;
+       my $rn = $lei->{opt}->{'try-remote'} // [ 'origin', '_grokmirror' ];
+       for my $r (@$rn) {
+               my $cmd = [ qw(git config), "remote.$r.url" ];
+               my $fh = popen_rd($cmd, undef, { -C => $dir, 2 => $lei->{2} });
+               my $url = <$fh>;
+               close $fh or next;
+               $url =~ s!/*\n!!s;
+               return $url;
+       }
+       undef
 }
 
 sub do_manifest ($$$) {
@@ -110,7 +114,7 @@ sub do_fetch { # main entry point
        my ($ibx_uri, @git_dir, @epochs, $mg, @new_epoch, $skip);
        if ($ibx_ver == 1) {
                my $url = remote_url($lei, $dir) //
-                       die "E: $dir missing remote.origin.url\n";
+                       die "E: $dir missing remote.*.url\n";
                $ibx_uri = URI->new($url);
        } else { # v2:
                require PublicInbox::MultiGit;
@@ -128,7 +132,7 @@ sub do_fetch { # main entry point
                                $git_url = $url;
                                $epoch = $nr;
                        } else {
-                               warn "W: $edir missing remote.origin.url\n";
+                               warn "W: $edir missing remote.*.url\n";
                                my $pid = spawn([qw(git config -l)], undef,
                                        { 1 => $lei->{2}, 2 => $lei->{2} });
                                waitpid($pid, 0);
index d7d4ba47ab613254d43e28ec3449bc26a5271452..f9bac4e3f582013824b98d98bde60808cbf61d73 100755 (executable)
@@ -16,12 +16,14 @@ options:
   --torsocks VAL      whether or not to wrap git and curl commands with
                       torsocks (default: `auto')
                       Must be one of: `auto', `no' or `yes'
+  -T NAME             Name of remote(s) to try (may be repeated)
+                      default: `origin' and `_grokmirror'
   --exit-code         exit with 127 if no updates
   --verbose | -v      increase verbosity (may be repeated)
     --quiet | -q      increase verbosity (may be repeated)
     -C DIR            chdir to specified directory
 EOF
-GetOptions($opt, qw(help|h quiet|q verbose|v+ C=s@ c=s@
+GetOptions($opt, qw(help|h quiet|q verbose|v+ C=s@ c=s@ try-remote|T=s@
        no-torsocks torsocks=s exit-code)) or die $help;
 if ($opt->{help}) { print $help; exit };
 require PublicInbox::Fetch; # loads Admin