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 run_die spawn);
10 use PublicInbox::Admin;
12 use PublicInbox::LeiCurl;
13 use PublicInbox::LeiMirror;
15 use PublicInbox::Config;
16 use IO::Compress::Gzip qw(gzip $GzipError);
18 sub new { bless {}, __PACKAGE__ }
22 my @cmd; # (git --git-dir=...) to be added by caller
23 $opt->{$_} = $lei->{$_} for (0..2);
24 # we support "-c $key=$val" for arbitrary git config options
25 # e.g.: git -c http.proxy=socks5h://127.0.0.1:9050
26 push(@cmd, '-c', $_) for @{$lei->{opt}->{c} // []};
28 push @cmd, '-q' if $lei->{opt}->{quiet};
29 push @cmd, '-v' if $lei->{opt}->{verbose};
35 my $rn = $lei->{opt}->{'try-remote'} // [ 'origin', '_grokmirror' ];
37 my $cmd = [ qw(git config), "remote.$r.url" ];
38 my $fh = popen_rd($cmd, undef, { -C => $dir, 2 => $lei->{2} });
47 # PSGI mount prefixes and manifest.js.gz prefixes don't always align...
48 # TODO: remove, handle multi-inbox fetch
49 sub deduce_epochs ($$) {
51 my ($v1_ent, @v2_epochs);
55 $v1_ent = $m->{$path};
56 @v2_epochs = grep(m!\A\Q$path\E/git/[0-9]+\.git\z!, keys %$m);
57 } while (!defined($v1_ent) && !@v2_epochs &&
58 $path =~ s!\A(/[^/]+)/!/! and $path_pfx .= $1);
59 ($path_pfx, $v1_ent ? $path : undef, @v2_epochs);
62 sub do_manifest ($$$) {
63 my ($lei, $dir, $ibx_uri) = @_;
64 my $muri = URI->new("$ibx_uri/manifest.js.gz");
65 my $ft = File::Temp->new(TEMPLATE => 'm-XXXX',
66 UNLINK => 1, DIR => $dir, SUFFIX => '.tmp');
67 my $fn = $ft->filename;
68 my $mf = "$dir/manifest.js.gz";
69 my $m0; # current manifest.js.gz contents
70 if (open my $fh, '<', $mf) {
72 PublicInbox::LeiMirror::decode_manifest($fh, $mf, $mf)
76 my ($bn) = ($fn =~ m!/([^/]+)\z!);
77 my $curl_cmd = $lei->{curl}->for_uri($lei, $muri, qw(-R -o), $bn);
78 my $opt = { -C => $dir };
79 $opt->{$_} = $lei->{$_} for (0..2);
80 my $cerr = PublicInbox::LeiMirror::run_reap($lei, $curl_cmd, $opt);
82 return [ 404, $muri ] if ($cerr >> 8) == 22; # 404 Missing
83 $lei->child_error($cerr, "@$curl_cmd failed");
87 PublicInbox::LeiMirror::decode_manifest($ft, $fn, $muri);
88 } or return [ 404, $muri ];
91 # filter out unchanged entries. We check modified, too, since
92 # fingerprints are SHA-1, so there's a teeny chance they'll collide
93 while (my ($k, $v0) = each %{$m0 // {}}) {
94 my $cur = $m1->{$k} // next;
95 my $f0 = $v0->{fingerprint} // next;
96 my $f1 = $cur->{fingerprint} // next;
97 my $t0 = $v0->{modified} // next;
98 my $t1 = $cur->{modified} // next;
99 delete($mdiff->{$k}) if $f0 eq $f1 && $t0 == $t1;
101 unless (keys %$mdiff) {
102 $lei->child_error(127 << 8) if $lei->{opt}->{'exit-code'};
105 my (undef, $v1_path, @v2_epochs) =
106 deduce_epochs($mdiff, $ibx_uri->path);
107 [ 200, $muri, $v1_path, \@v2_epochs, $ft, $mf, $m1 ];
110 sub get_fingerprint2 {
113 my $rd = popen_rd([qw(git show-ref)], undef, { -C => $git_dir });
114 Digest::SHA::sha256(do { local $/; <$rd> });
117 sub writable_dir ($) {
119 return unless -d $dir && -w _;
121 $st[2] & 0222; # any writable bits set? (in case of root)
124 sub do_fetch { # main entry point
125 my ($cls, $lei, $cd) = @_;
127 $lei->{curl} //= PublicInbox::LeiCurl->new($lei) or return;
128 my $dir = PublicInbox::Admin::resolve_inboxdir($cd, \$ibx_ver);
129 my ($ibx_uri, @git_dir, @epochs, $mg, @new_epoch, $skip);
131 my $url = remote_url($lei, $dir) //
132 die "E: $dir missing remote.*.url\n";
133 $ibx_uri = URI->new($url);
135 require PublicInbox::MultiGit;
136 $mg = PublicInbox::MultiGit->new($dir, 'all.git', 'git');
137 @epochs = $mg->git_epochs;
138 my ($git_url, $epoch);
139 for my $nr (@epochs) { # try newest epoch, first
140 my $edir = "$dir/git/$nr.git";
141 if (!writable_dir($edir)) {
145 next if defined $git_url;
146 if (defined(my $url = remote_url($lei, $edir))) {
150 warn "W: $edir missing remote.*.url\n";
151 my $pid = spawn([qw(git config -l)], undef,
152 { 1 => $lei->{2}, 2 => $lei->{2} });
154 $lei->child_error($?) if $?;
157 @epochs = grep { !$skip->{$_} } @epochs if $skip;
158 $skip //= {}; # makes code below easier
159 $git_url or die "Unable to determine git URL\n";
160 my $inbox_url = $git_url;
161 $inbox_url =~ s!/git/$epoch(?:\.git)?/?\z!! or
162 $inbox_url =~ s!/$epoch(?:\.git)?/?\z!! or die <<EOM;
163 Unable to infer inbox URL from <$git_url>
165 $ibx_uri = URI->new($inbox_url);
167 PublicInbox::LeiMirror::write_makefile($dir, $ibx_ver);
168 $lei->qerr("# inbox URL: $ibx_uri/");
169 my $res = do_manifest($lei, $dir, $ibx_uri) or return;
170 my ($code, $muri, $v1_path, $v2_epochs, $ft, $mf, $m1) = @$res;
172 # any pre-manifest.js.gz instances running? Just fetch all
173 # existing ones and unconditionally try cloning the next
174 $v2_epochs = [ map { "$dir/git/$_.git" } @epochs ];
176 my $n = $epochs[-1] + 1;
177 push @$v2_epochs, "$dir/git/$n.git" if !$skip->{$n};
180 $code == 200 or die "BUG unexpected code $code\n";
184 defined($v1_path) and warn <<EOM;
185 E: got v1 `$v1_path' when expecting v2 epoch(s) in <$muri>, WTF?
187 @git_dir = map { "$dir/git/$_.git" } sort { $a <=> $b } map {
188 my ($nr) = (m!/([0-9]+)\.git\z!g);
189 $skip->{$nr} ? () : $nr;
191 if ($m1 && scalar keys %$skip) {
192 my $re = join('|', keys %$skip);
193 my @del = grep(m!/git/$re\.git\z!, keys %$m1);
200 # n.b. this expects all epochs are from the same host
201 my $torsocks = $lei->{curl}->torsocks($lei, $muri);
202 my $fp2 = $lei->{opt}->{'exit-code'} ? [] : undef;
204 for my $d (@git_dir) {
206 my $opt = {}; # for spawn
208 $fp2->[0] = get_fingerprint2($d) if $fp2;
209 $cmd = [ @$torsocks, 'git', "--git-dir=$d",
210 fetch_args($lei, $opt) ];
212 my $e_uri = $ibx_uri->clone;
213 my ($epath) = ($d =~ m!(/git/[0-9]+\.git)\z!);
215 die "BUG: $d is not an epoch to clone\n";
216 $e_uri->path($ibx_uri->path.$epath);
218 PublicInbox::LeiMirror::clone_cmd($lei, $opt),
220 push @new_epoch, substr($epath, 5, -4) + 0;
223 my $cerr = PublicInbox::LeiMirror::run_reap($lei, $cmd, $opt);
224 # do not bail on clone failure if we didn't have a manifest
225 if ($cerr && ($code == 200 || -d $d)) {
226 $lei->child_error($cerr, "@$cmd failed");
230 $fp2->[1] = get_fingerprint2($d);
231 $xit = 0 if $fp2->[0] ne $fp2->[1];
234 for my $i (@new_epoch) { $mg->epoch_cfg_set($i) }
237 my $json = PublicInbox::Config->json->encode($m1);
238 my $fn = $ft->filename;
239 my $mtime = (stat($fn))[9];
240 gzip(\$json => $fn) or die "gzip: $GzipError";
241 utime($mtime, $mtime, $fn) or die "utime(..., $fn): $!";
243 PublicInbox::LeiMirror::ft_rename($ft, $mf, 0666);
245 $lei->child_error($xit << 8) if $fp2 && $xit;