1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # Wrapper to "git fetch" remote public-inboxes
4 package PublicInbox::Fetch;
7 use parent qw(PublicInbox::IPC);
9 use PublicInbox::Spawn qw(popen_rd);
10 use PublicInbox::Admin;
12 use PublicInbox::LeiCurl;
13 use PublicInbox::LeiMirror;
14 use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
17 sub new { bless {}, __PACKAGE__ }
22 $opt->{$_} = $lei->{$_} for (0..2);
23 # we support "-c $key=$val" for arbitrary git config options
24 # e.g.: git -c http.proxy=socks5h://127.0.0.1:9050
25 push(@cmd, '-c', $_) for @{$lei->{opt}->{c} // []};
27 push @cmd, '-q' if $lei->{opt}->{quiet};
28 push @cmd, '-v' if $lei->{opt}->{verbose};
33 my ($lei, $dir) = @_; # TODO: support non-"origin"?
34 my $cmd = [ qw(git config remote.origin.url) ];
35 my $fh = popen_rd($cmd, undef, { -C => $dir, 2 => $lei->{2} });
42 sub do_manifest ($$$) {
43 my ($lei, $dir, $ibx_uri) = @_;
44 my $muri = URI->new("$ibx_uri/manifest.js.gz");
45 my $ft = File::Temp->new(TEMPLATE => 'manifest-XXXX',
46 UNLINK => 1, DIR => $dir);
47 my $fn = $ft->filename;
48 my @opt = (qw(-R -o), $fn);
49 my $mf = "$dir/manifest.js.gz";
50 my $m0; # current manifest.js.gz contents
51 if (open my $fh, '<', $mf) {
53 PublicInbox::LeiMirror::decode_manifest($fh, $mf, $mf)
56 push @opt, '-z', $mf if defined($m0);
58 my $curl_cmd = $lei->{curl}->for_uri($lei, $muri, @opt);
60 $opt->{$_} = $lei->{$_} for (0..2);
61 my $cerr = PublicInbox::LeiMirror::run_reap($lei, $curl_cmd, $opt);
63 return [ 404 ] if ($cerr >> 8) == 22; # 404 Missing
64 $lei->child_error($cerr, "@$curl_cmd failed");
67 return [ 304 ] if !-s $ft; # 304 Not Modified via curl -z
68 my $m1 = PublicInbox::LeiMirror::decode_manifest($ft, $fn, $muri);
71 # filter out unchanged entries
72 while (my ($k, $v0) = each %{$m0 // {}}) {
73 my $cur = $m1->{$k} // next;
74 my $f0 = $v0->{fingerprint} // next;
75 my $f1 = $cur->{fingerprint} // next;
76 my $t0 = $v0->{modified} // next;
77 my $t1 = $cur->{modified} // next;
78 delete($mdiff->{$k}) if $f0 eq $f1 && $t0 == $t1;
80 my (undef, $v1_path, @v2_epochs) =
81 PublicInbox::LeiMirror::deduce_epochs($mdiff, $ibx_uri->path);
82 [ 200, $v1_path, \@v2_epochs, $muri, $ft, $mf ];
86 my ($cls, $lei, $cd) = @_;
88 $lei->{curl} //= PublicInbox::LeiCurl->new($lei) or return;
89 my $dir = PublicInbox::Admin::resolve_inboxdir($cd, \$ibx_ver);
90 my ($ibx_uri, @git_dir, @epochs);
92 my $url = remote_url($lei, $dir) //
93 die "E: $dir missing remote.origin.url\n";
94 $ibx_uri = URI->new($url);
96 opendir my $dh, "$dir/git" or die "opendir $dir/git: $!";
97 @epochs = sort { $b <=> $a } map { substr($_, 0, -4) + 0 }
98 grep(/\A[0-9]+\.git\z/, readdir($dh));
99 my ($git_url, $epoch);
100 for my $nr (@epochs) { # try newest epoch, first
101 my $edir = "$dir/git/$nr.git";
102 if (defined(my $url = remote_url($lei, $edir))) {
107 warn "W: $edir missing remote.origin.url\n";
110 $git_url or die "Unable to determine git URL\n";
111 my $inbox_url = $git_url;
112 $inbox_url =~ s!/git/$epoch(?:\.git)?/?\z!! or
113 $inbox_url =~ s!/$epoch(?:\.git)?/?\z!! or die <<EOM;
114 Unable to infer inbox URL from <$git_url>
116 $ibx_uri = URI->new($inbox_url);
118 $lei->qerr("# inbox URL: $ibx_uri/");
119 my $res = do_manifest($lei, $dir, $ibx_uri) or return;
120 my ($code, $v1_path, $v2_epochs, $muri, $ft, $mf) = @$res;
121 return if $code == 304;
123 # any pre-manifest.js.gz instances running? Just fetch all
124 # existing ones and unconditionally try cloning the next
125 $v2_epochs = [ map {;
128 push @$v2_epochs, "$dir/git/".($epochs[-1] + 1) if @epochs;
130 $code == 200 or die "BUG unexpected code $code\n";
133 defined($v1_path) and warn <<EOM;
134 E: got v1 `$v1_path' when expecting v2 epoch(s) in <$muri>, WTF?
136 @git_dir = map { "$dir/git/$_.git" } sort { $a <=> $b }
137 map { my ($nr) = (m!/([0-9]+)\.git\z!g) } @$v2_epochs;
141 # n.b. this expects all epochs are from the same host
142 my $torsocks = $lei->{curl}->torsocks($lei, $muri);
143 for my $d (@git_dir) {
145 my $opt = {}; # for spawn
148 $cmd = [ @$torsocks, fetch_cmd($lei, $opt) ];
150 my $e_uri = $ibx_uri->clone;
151 my ($epath) = ($d =~ m!(/git/[0-9]+\.git)\z!);
153 die "BUG: $d is not an epoch to clone\n";
154 $e_uri->path($ibx_uri->path.$epath);
156 PublicInbox::LeiMirror::clone_cmd($lei, $opt),
159 my $cerr = PublicInbox::LeiMirror::run_reap($lei, $cmd, $opt);
160 # do not bail on clone failure if we didn't have a manifest
161 if ($cerr && ($code == 200 || -d $d)) {
162 $lei->child_error($cerr, "@$cmd failed");
167 my $fn = $ft->filename;
168 rename($fn, $mf) or die "E: rename($fn, $mf): $!\n";
169 $ft->unlink_on_destroy(0);