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 return unless keys %$mdiff;
79 my (undef, $v1_path, @v2_epochs) =
80 PublicInbox::LeiMirror::deduce_epochs($mdiff, $ibx_uri->path);
81 [ 200, $v1_path, \@v2_epochs, $muri, $ft, $mf ];
85 my ($cls, $lei, $cd) = @_;
87 $lei->{curl} //= PublicInbox::LeiCurl->new($lei) or return;
88 my $dir = PublicInbox::Admin::resolve_inboxdir($cd, \$ibx_ver);
89 my ($ibx_uri, @git_dir, @epochs, $mg, @new_epoch);
91 my $url = remote_url($lei, $dir) //
92 die "E: $dir missing remote.origin.url\n";
93 $ibx_uri = URI->new($url);
95 require PublicInbox::MultiGit;
96 $mg = PublicInbox::MultiGit->new($dir, 'all.git', 'git');
97 my @epochs = $mg->git_epochs;
98 my ($git_url, $epoch);
99 for my $nr (@epochs) { # try newest epoch, first
100 my $edir = "$dir/git/$nr.git";
101 if (defined(my $url = remote_url($lei, $edir))) {
106 warn "W: $edir missing remote.origin.url\n";
109 $git_url or die "Unable to determine git URL\n";
110 my $inbox_url = $git_url;
111 $inbox_url =~ s!/git/$epoch(?:\.git)?/?\z!! or
112 $inbox_url =~ s!/$epoch(?:\.git)?/?\z!! or die <<EOM;
113 Unable to infer inbox URL from <$git_url>
115 $ibx_uri = URI->new($inbox_url);
117 $lei->qerr("# inbox URL: $ibx_uri/");
118 my $res = do_manifest($lei, $dir, $ibx_uri) or return;
119 my ($code, $v1_path, $v2_epochs, $muri, $ft, $mf) = @$res;
121 # any pre-manifest.js.gz instances running? Just fetch all
122 # existing ones and unconditionally try cloning the next
123 $v2_epochs = [ map { "$dir/git/$_.git" } @epochs ];
124 push @$v2_epochs, "$dir/git/".($epochs[-1] + 1) if @epochs;
126 $code == 200 or die "BUG unexpected code $code\n";
129 defined($v1_path) and warn <<EOM;
130 E: got v1 `$v1_path' when expecting v2 epoch(s) in <$muri>, WTF?
132 @git_dir = map { "$dir/git/$_.git" } sort { $a <=> $b }
133 map { my ($nr) = (m!/([0-9]+)\.git\z!g) } @$v2_epochs;
137 # n.b. this expects all epochs are from the same host
138 my $torsocks = $lei->{curl}->torsocks($lei, $muri);
139 for my $d (@git_dir) {
141 my $opt = {}; # for spawn
144 $cmd = [ @$torsocks, fetch_cmd($lei, $opt) ];
146 my $e_uri = $ibx_uri->clone;
147 my ($epath) = ($d =~ m!(/git/[0-9]+\.git)\z!);
149 die "BUG: $d is not an epoch to clone\n";
150 $e_uri->path($ibx_uri->path.$epath);
152 PublicInbox::LeiMirror::clone_cmd($lei, $opt),
154 push @new_epoch, substr($epath, 5, -4) + 0;
156 my $cerr = PublicInbox::LeiMirror::run_reap($lei, $cmd, $opt);
157 # do not bail on clone failure if we didn't have a manifest
158 if ($cerr && ($code == 200 || -d $d)) {
159 $lei->child_error($cerr, "@$cmd failed");
163 for my $i (@new_epoch) { $mg->epoch_cfg_set($i) }
165 my $fn = $ft->filename;
166 rename($fn, $mf) or die "E: rename($fn, $mf): $!\n";
167 $ft->unlink_on_destroy(0);