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__ }
20 my @cmd; # (git --git-dir=...) to be added by caller
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 => 'm-XXXX',
45 UNLINK => 1, DIR => $dir, SUFFIX => '.tmp');
46 my $fn = $ft->filename;
47 my $mf = "$dir/manifest.js.gz";
48 my $m0; # current manifest.js.gz contents
49 if (open my $fh, '<', $mf) {
51 PublicInbox::LeiMirror::decode_manifest($fh, $mf, $mf)
55 my ($bn) = ($fn =~ m!/([^/]+)\z!);
56 my $curl_cmd = $lei->{curl}->for_uri($lei, $muri, qw(-R -o), $bn);
57 my $opt = { -C => $dir };
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> });
94 sub do_fetch { # main entry point
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, $skip);
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 unless (-d $edir && -w _) { # must be writable dir
115 if (defined(my $url = remote_url($lei, $edir))) {
120 warn "W: $edir missing remote.origin.url\n";
123 @epochs = grep { !$skip->{$_} } @epochs if $skip;
124 $skip //= {}; # makes code below easier
125 $git_url or die "Unable to determine git URL\n";
126 my $inbox_url = $git_url;
127 $inbox_url =~ s!/git/$epoch(?:\.git)?/?\z!! or
128 $inbox_url =~ s!/$epoch(?:\.git)?/?\z!! or die <<EOM;
129 Unable to infer inbox URL from <$git_url>
131 $ibx_uri = URI->new($inbox_url);
133 PublicInbox::LeiMirror::write_makefile($dir, $ibx_ver);
134 $lei->qerr("# inbox URL: $ibx_uri/");
135 my $res = do_manifest($lei, $dir, $ibx_uri) or return;
136 my ($code, $v1_path, $v2_epochs, $muri, $ft, $mf) = @$res;
138 # any pre-manifest.js.gz instances running? Just fetch all
139 # existing ones and unconditionally try cloning the next
140 $v2_epochs = [ map { "$dir/git/$_.git" } @epochs ];
142 my $n = $epochs[-1] + 1;
143 push @$v2_epochs, "$dir/git/$n.git" if !$skip->{$n};
146 $code == 200 or die "BUG unexpected code $code\n";
149 defined($v1_path) and warn <<EOM;
150 E: got v1 `$v1_path' when expecting v2 epoch(s) in <$muri>, WTF?
152 @git_dir = map { "$dir/git/$_.git" } sort { $a <=> $b } map {
153 my ($nr) = (m!/([0-9]+)\.git\z!g);
154 $skip->{$nr} ? () : $nr;
159 # n.b. this expects all epochs are from the same host
160 my $torsocks = $lei->{curl}->torsocks($lei, $muri);
161 my $fp2 = $lei->{opt}->{'exit-code'} ? [] : undef;
163 for my $d (@git_dir) {
165 my $opt = {}; # for spawn
167 $fp2->[0] = get_fingerprint2($d) if $fp2;
168 $cmd = [ @$torsocks, 'git', "--git-dir=$d",
169 fetch_args($lei, $opt) ];
171 my $e_uri = $ibx_uri->clone;
172 my ($epath) = ($d =~ m!(/git/[0-9]+\.git)\z!);
174 die "BUG: $d is not an epoch to clone\n";
175 $e_uri->path($ibx_uri->path.$epath);
177 PublicInbox::LeiMirror::clone_cmd($lei, $opt),
179 push @new_epoch, substr($epath, 5, -4) + 0;
182 my $cerr = PublicInbox::LeiMirror::run_reap($lei, $cmd, $opt);
183 # do not bail on clone failure if we didn't have a manifest
184 if ($cerr && ($code == 200 || -d $d)) {
185 $lei->child_error($cerr, "@$cmd failed");
189 $fp2->[1] = get_fingerprint2($d);
190 $xit = 0 if $fp2->[0] ne $fp2->[1];
193 for my $i (@new_epoch) { $mg->epoch_cfg_set($i) }
195 my $fn = $ft->filename;
196 rename($fn, $mf) or die "E: rename($fn, $mf): $!\n";
197 $ft->unlink_on_destroy(0);
199 $lei->child_error($xit << 8) if $fp2 && $xit;