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;
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};
34 my ($lei, $dir) = @_; # TODO: support non-"origin"?
35 my $cmd = [ qw(git config remote.origin.url) ];
36 my $fh = popen_rd($cmd, undef, { -C => $dir, 2 => $lei->{2} });
43 sub do_manifest ($$$) {
44 my ($lei, $dir, $ibx_uri) = @_;
45 my $muri = URI->new("$ibx_uri/manifest.js.gz");
46 my $ft = File::Temp->new(TEMPLATE => 'm-XXXX',
47 UNLINK => 1, DIR => $dir, SUFFIX => '.tmp');
48 my $fn = $ft->filename;
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)
57 my ($bn) = ($fn =~ m!/([^/]+)\z!);
58 my $curl_cmd = $lei->{curl}->for_uri($lei, $muri, qw(-R -o), $bn);
59 my $opt = { -C => $dir };
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 my $m1 = PublicInbox::LeiMirror::decode_manifest($ft, $fn, $muri);
70 # filter out unchanged entries. We check modified, too, since
71 # fingerprints are SHA-1, so there's a teeny chance they'll collide
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 unless (keys %$mdiff) {
81 $lei->child_error(127 << 8) if $lei->{opt}->{'exit-code'};
84 my (undef, $v1_path, @v2_epochs) =
85 PublicInbox::LeiMirror::deduce_epochs($mdiff, $ibx_uri->path);
86 [ 200, $v1_path, \@v2_epochs, $muri, $ft, $mf, $m1 ];
89 sub get_fingerprint2 {
92 my $rd = popen_rd([qw(git show-ref)], undef, { -C => $git_dir });
93 Digest::SHA::sha256(do { local $/; <$rd> });
96 sub do_fetch { # main entry point
97 my ($cls, $lei, $cd) = @_;
99 $lei->{curl} //= PublicInbox::LeiCurl->new($lei) or return;
100 my $dir = PublicInbox::Admin::resolve_inboxdir($cd, \$ibx_ver);
101 my ($ibx_uri, @git_dir, @epochs, $mg, @new_epoch, $skip);
103 my $url = remote_url($lei, $dir) //
104 die "E: $dir missing remote.origin.url\n";
105 $ibx_uri = URI->new($url);
107 require PublicInbox::MultiGit;
108 $mg = PublicInbox::MultiGit->new($dir, 'all.git', 'git');
109 my @epochs = $mg->git_epochs;
110 my ($git_url, $epoch);
111 for my $nr (@epochs) { # try newest epoch, first
112 my $edir = "$dir/git/$nr.git";
113 unless (-d $edir && -w _) { # must be writable dir
117 next if defined $git_url;
118 if (defined(my $url = remote_url($lei, $edir))) {
122 warn "W: $edir missing remote.origin.url\n";
125 @epochs = grep { !$skip->{$_} } @epochs if $skip;
126 $skip //= {}; # makes code below easier
127 $git_url or die "Unable to determine git URL\n";
128 my $inbox_url = $git_url;
129 $inbox_url =~ s!/git/$epoch(?:\.git)?/?\z!! or
130 $inbox_url =~ s!/$epoch(?:\.git)?/?\z!! or die <<EOM;
131 Unable to infer inbox URL from <$git_url>
133 $ibx_uri = URI->new($inbox_url);
135 PublicInbox::LeiMirror::write_makefile($dir, $ibx_ver);
136 $lei->qerr("# inbox URL: $ibx_uri/");
137 my $res = do_manifest($lei, $dir, $ibx_uri) or return;
138 my ($code, $v1_path, $v2_epochs, $muri, $ft, $mf, $m1) = @$res;
140 # any pre-manifest.js.gz instances running? Just fetch all
141 # existing ones and unconditionally try cloning the next
142 $v2_epochs = [ map { "$dir/git/$_.git" } @epochs ];
144 my $n = $epochs[-1] + 1;
145 push @$v2_epochs, "$dir/git/$n.git" if !$skip->{$n};
148 $code == 200 or die "BUG unexpected code $code\n";
152 defined($v1_path) and warn <<EOM;
153 E: got v1 `$v1_path' when expecting v2 epoch(s) in <$muri>, WTF?
155 @git_dir = map { "$dir/git/$_.git" } sort { $a <=> $b } map {
156 my ($nr) = (m!/([0-9]+)\.git\z!g);
157 $skip->{$nr} ? () : $nr;
159 if ($m1 && scalar keys %$skip) {
160 my $re = join('|', keys %$skip);
161 my @del = grep(m!/git/$re\.git\z!, keys %$m1);
168 # n.b. this expects all epochs are from the same host
169 my $torsocks = $lei->{curl}->torsocks($lei, $muri);
170 my $fp2 = $lei->{opt}->{'exit-code'} ? [] : undef;
172 for my $d (@git_dir) {
174 my $opt = {}; # for spawn
176 $fp2->[0] = get_fingerprint2($d) if $fp2;
177 $cmd = [ @$torsocks, 'git', "--git-dir=$d",
178 fetch_args($lei, $opt) ];
180 my $e_uri = $ibx_uri->clone;
181 my ($epath) = ($d =~ m!(/git/[0-9]+\.git)\z!);
183 die "BUG: $d is not an epoch to clone\n";
184 $e_uri->path($ibx_uri->path.$epath);
186 PublicInbox::LeiMirror::clone_cmd($lei, $opt),
188 push @new_epoch, substr($epath, 5, -4) + 0;
191 my $cerr = PublicInbox::LeiMirror::run_reap($lei, $cmd, $opt);
192 # do not bail on clone failure if we didn't have a manifest
193 if ($cerr && ($code == 200 || -d $d)) {
194 $lei->child_error($cerr, "@$cmd failed");
198 $fp2->[1] = get_fingerprint2($d);
199 $xit = 0 if $fp2->[0] ne $fp2->[1];
202 for my $i (@new_epoch) { $mg->epoch_cfg_set($i) }
204 my $fn = $ft->filename;
206 my $json = PublicInbox::Config->json->encode($m1);
207 gzip(\$json => $fn) or die "gzip: $GzipError";
209 rename($fn, $mf) or die "E: rename($fn, $mf): $!\n";
210 $ft->unlink_on_destroy(0);
212 $lei->child_error($xit << 8) if $fp2 && $xit;