user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
* [PATCH] fetch: support --try-remote/-T for alternate remote names
@ 2021-10-13 11:06 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2021-10-13 11:06 UTC (permalink / raw)
  To: meta

This allows -fetch to work out-of-the-box on using the
grokmirror 2.x default of "_grokmirror".
---
 Documentation/public-inbox-fetch.pod |  9 +++++++++
 lib/PublicInbox/Fetch.pm             | 22 +++++++++++++---------
 script/public-inbox-fetch            |  4 +++-
 3 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/Documentation/public-inbox-fetch.pod b/Documentation/public-inbox-fetch.pod
index 833df862..1890ae75 100644
--- a/Documentation/public-inbox-fetch.pod
+++ b/Documentation/public-inbox-fetch.pod
@@ -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
diff --git a/lib/PublicInbox/Fetch.pm b/lib/PublicInbox/Fetch.pm
index e5756fb6..0d4badbf 100644
--- a/lib/PublicInbox/Fetch.pm
+++ b/lib/PublicInbox/Fetch.pm
@@ -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);
diff --git a/script/public-inbox-fetch b/script/public-inbox-fetch
index d7d4ba47..f9bac4e3 100755
--- a/script/public-inbox-fetch
+++ b/script/public-inbox-fetch
@@ -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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-10-13 11:06 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-13 11:06 [PATCH] fetch: support --try-remote/-T for alternate remote names Eric Wong

Code repositories for project(s) associated with this inbox:

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).