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);
10 use PublicInbox::Admin;
12 use PublicInbox::LeiCurl;
13 use PublicInbox::LeiMirror;
16 sub new { bless {}, __PACKAGE__ }
21 $opt->{$_} = $lei->{$_} for (0..2);
22 # we support "-c $key=$val" for arbitrary git config options
23 # e.g.: git -c http.proxy=socks5h://127.0.0.1:9050
24 push(@cmd, '-c', $_) for @{$lei->{opt}->{c} // []};
26 push @cmd, '-q' if $lei->{opt}->{quiet};
27 push @cmd, '-v' if $lei->{opt}->{verbose};
32 my ($lei, $dir) = @_; # TODO: support non-"origin"?
33 my $cmd = [ qw(git config remote.origin.url) ];
34 my $fh = popen_rd($cmd, undef, { -C => $dir, 2 => $lei->{2} });
41 sub do_manifest ($$$) {
42 my ($lei, $dir, $ibx_uri) = @_;
43 my $muri = URI->new("$ibx_uri/manifest.js.gz");
44 my $ft = File::Temp->new(TEMPLATE => 'manifest-XXXX',
45 UNLINK => 1, DIR => $dir);
46 my $fn = $ft->filename;
47 my @opt = (qw(-R -o), $fn);
48 my $mf = "$dir/manifest.js.gz";
49 my $m0; # current manifest.js.gz contents
50 if (open my $fh, '<', $mf) {
52 PublicInbox::LeiMirror::decode_manifest($fh, $mf, $mf)
56 my $curl_cmd = $lei->{curl}->for_uri($lei, $muri, @opt);
58 $opt->{$_} = $lei->{$_} for (0..2);
59 my $cerr = PublicInbox::LeiMirror::run_reap($lei, $curl_cmd, $opt);
61 return [ 404 ] if ($cerr >> 8) == 22; # 404 Missing
62 $lei->child_error($cerr, "@$curl_cmd failed");
65 my $m1 = PublicInbox::LeiMirror::decode_manifest($ft, $fn, $muri);
68 # filter out unchanged entries. We check modified, too, since
69 # fingerprints are SHA-1, so there's a teeny chance they'll collide
70 while (my ($k, $v0) = each %{$m0 // {}}) {
71 my $cur = $m1->{$k} // next;
72 my $f0 = $v0->{fingerprint} // next;
73 my $f1 = $cur->{fingerprint} // next;
74 my $t0 = $v0->{modified} // next;
75 my $t1 = $cur->{modified} // next;
76 delete($mdiff->{$k}) if $f0 eq $f1 && $t0 == $t1;
78 unless (keys %$mdiff) {
79 $lei->child_error(127 << 8) if $lei->{opt}->{'exit-code'};
82 my (undef, $v1_path, @v2_epochs) =
83 PublicInbox::LeiMirror::deduce_epochs($mdiff, $ibx_uri->path);
84 [ 200, $v1_path, \@v2_epochs, $muri, $ft, $mf ];
87 sub get_fingerprint2 {
90 my $rd = popen_rd([qw(git show-ref)], undef, { -C => $git_dir });
91 Digest::SHA::sha256(do { local $/; <$rd> });
95 my ($cls, $lei, $cd) = @_;
97 $lei->{curl} //= PublicInbox::LeiCurl->new($lei) or return;
98 my $dir = PublicInbox::Admin::resolve_inboxdir($cd, \$ibx_ver);
99 my ($ibx_uri, @git_dir, @epochs, $mg, @new_epoch);
101 my $url = remote_url($lei, $dir) //
102 die "E: $dir missing remote.origin.url\n";
103 $ibx_uri = URI->new($url);
105 require PublicInbox::MultiGit;
106 $mg = PublicInbox::MultiGit->new($dir, 'all.git', 'git');
107 my @epochs = $mg->git_epochs;
108 my ($git_url, $epoch);
109 for my $nr (@epochs) { # try newest epoch, first
110 my $edir = "$dir/git/$nr.git";
111 if (defined(my $url = remote_url($lei, $edir))) {
116 warn "W: $edir missing remote.origin.url\n";
119 $git_url or die "Unable to determine git URL\n";
120 my $inbox_url = $git_url;
121 $inbox_url =~ s!/git/$epoch(?:\.git)?/?\z!! or
122 $inbox_url =~ s!/$epoch(?:\.git)?/?\z!! or die <<EOM;
123 Unable to infer inbox URL from <$git_url>
125 $ibx_uri = URI->new($inbox_url);
127 $lei->qerr("# inbox URL: $ibx_uri/");
128 my $res = do_manifest($lei, $dir, $ibx_uri) or return;
129 my ($code, $v1_path, $v2_epochs, $muri, $ft, $mf) = @$res;
131 # any pre-manifest.js.gz instances running? Just fetch all
132 # existing ones and unconditionally try cloning the next
133 $v2_epochs = [ map { "$dir/git/$_.git" } @epochs ];
134 push @$v2_epochs, "$dir/git/".($epochs[-1] + 1) if @epochs;
136 $code == 200 or die "BUG unexpected code $code\n";
139 defined($v1_path) and warn <<EOM;
140 E: got v1 `$v1_path' when expecting v2 epoch(s) in <$muri>, WTF?
142 @git_dir = map { "$dir/git/$_.git" } sort { $a <=> $b }
143 map { my ($nr) = (m!/([0-9]+)\.git\z!g) } @$v2_epochs;
147 # n.b. this expects all epochs are from the same host
148 my $torsocks = $lei->{curl}->torsocks($lei, $muri);
149 my $fp2 = $lei->{opt}->{'exit-code'} ? [] : undef;
151 for my $d (@git_dir) {
153 my $opt = {}; # for spawn
156 $fp2->[0] = get_fingerprint2($d) if $fp2;
157 $cmd = [ @$torsocks, fetch_cmd($lei, $opt) ];
159 my $e_uri = $ibx_uri->clone;
160 my ($epath) = ($d =~ m!(/git/[0-9]+\.git)\z!);
162 die "BUG: $d is not an epoch to clone\n";
163 $e_uri->path($ibx_uri->path.$epath);
165 PublicInbox::LeiMirror::clone_cmd($lei, $opt),
167 push @new_epoch, substr($epath, 5, -4) + 0;
170 my $cerr = PublicInbox::LeiMirror::run_reap($lei, $cmd, $opt);
171 # do not bail on clone failure if we didn't have a manifest
172 if ($cerr && ($code == 200 || -d $d)) {
173 $lei->child_error($cerr, "@$cmd failed");
177 $fp2->[1] = get_fingerprint2($d);
178 $xit = 0 if $fp2->[0] ne $fp2->[1];
181 for my $i (@new_epoch) { $mg->epoch_cfg_set($i) }
183 my $fn = $ft->filename;
184 rename($fn, $mf) or die "E: rename($fn, $mf): $!\n";
185 $ft->unlink_on_destroy(0);
187 $lei->child_error($xit << 8) if $fp2 && $xit;