]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Fetch.pm
464ffe12f3af7b66367ab98626c4233795732355
[public-inbox.git] / lib / PublicInbox / Fetch.pm
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;
5 use strict;
6 use v5.10.1;
7 use parent qw(PublicInbox::IPC);
8 use URI ();
9 use PublicInbox::Spawn qw(popen_rd run_die);
10 use PublicInbox::Admin;
11 use PublicInbox::LEI;
12 use PublicInbox::LeiCurl;
13 use PublicInbox::LeiMirror;
14 use File::Temp ();
15
16 sub new { bless {}, __PACKAGE__ }
17
18 sub fetch_args ($$) {
19         my ($lei, $opt) = @_;
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} // []};
25         push @cmd, 'fetch';
26         push @cmd, '-q' if $lei->{opt}->{quiet};
27         push @cmd, '-v' if $lei->{opt}->{verbose};
28         @cmd;
29 }
30
31 sub remote_url ($$) {
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} });
35         my $url = <$fh>;
36         close $fh or return;
37         $url =~ s!/*\n!!s;
38         $url;
39 }
40
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) {
50                 $m0 = eval {
51                         PublicInbox::LeiMirror::decode_manifest($fh, $mf, $mf)
52                 };
53                 $lei->err($@) if $@;
54         }
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);
60         if ($cerr) {
61                 return [ 404 ] if ($cerr >> 8) == 22; # 404 Missing
62                 $lei->child_error($cerr, "@$curl_cmd failed");
63                 return;
64         }
65         my $m1 = PublicInbox::LeiMirror::decode_manifest($ft, $fn, $muri);
66         my $mdiff = { %$m1 };
67
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;
77         }
78         unless (keys %$mdiff) {
79                 $lei->child_error(127 << 8) if $lei->{opt}->{'exit-code'};
80                 return;
81         }
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 ];
85 }
86
87 sub get_fingerprint2 {
88         my ($git_dir) = @_;
89         require Digest::SHA;
90         my $rd = popen_rd([qw(git show-ref)], undef, { -C => $git_dir });
91         Digest::SHA::sha256(do { local $/; <$rd> });
92 }
93
94 sub do_fetch { # main entry point
95         my ($cls, $lei, $cd) = @_;
96         my $ibx_ver;
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);
100         if ($ibx_ver == 1) {
101                 my $url = remote_url($lei, $dir) //
102                         die "E: $dir missing remote.origin.url\n";
103                 $ibx_uri = URI->new($url);
104         } else { # v2:
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
112                                 $skip->{$nr} = 1;
113                                 next;
114                         }
115                         next if defined $git_url;
116                         if (defined(my $url = remote_url($lei, $edir))) {
117                                 $git_url = $url;
118                                 $epoch = $nr;
119                         } else {
120                                 warn "W: $edir missing remote.origin.url\n";
121                         }
122                 }
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>
130 EOM
131                 $ibx_uri = URI->new($inbox_url);
132         }
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;
137         if ($code == 404) {
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 ];
141                 if (@epochs) {
142                         my $n = $epochs[-1] + 1;
143                         push @$v2_epochs, "$dir/git/$n.git" if !$skip->{$n};
144                 }
145         } else {
146                 $code == 200 or die "BUG unexpected code $code\n";
147         }
148         if ($ibx_ver == 2) {
149                 defined($v1_path) and warn <<EOM;
150 E: got v1 `$v1_path' when expecting v2 epoch(s) in <$muri>, WTF?
151 EOM
152                 @git_dir = map { "$dir/git/$_.git" } sort { $a <=> $b } map {
153                                 my ($nr) = (m!/([0-9]+)\.git\z!g);
154                                 $skip->{$nr} ? () : $nr;
155                         } @$v2_epochs;
156         } else {
157                 $git_dir[0] = $dir;
158         }
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;
162         my $xit = 127;
163         for my $d (@git_dir) {
164                 my $cmd;
165                 my $opt = {}; # for spawn
166                 if (-d $d) {
167                         $fp2->[0] = get_fingerprint2($d) if $fp2;
168                         $cmd = [ @$torsocks, 'git', "--git-dir=$d",
169                                 fetch_args($lei, $opt) ];
170                 } else {
171                         my $e_uri = $ibx_uri->clone;
172                         my ($epath) = ($d =~ m!(/git/[0-9]+\.git)\z!);
173                         defined($epath) or
174                                 die "BUG: $d is not an epoch to clone\n";
175                         $e_uri->path($ibx_uri->path.$epath);
176                         $cmd = [ @$torsocks,
177                                 PublicInbox::LeiMirror::clone_cmd($lei, $opt),
178                                 $$e_uri, $d];
179                         push @new_epoch, substr($epath, 5, -4) + 0;
180                         $xit = 0;
181                 }
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");
186                         return;
187                 }
188                 if ($fp2 && $xit) {
189                         $fp2->[1] = get_fingerprint2($d);
190                         $xit = 0 if $fp2->[0] ne $fp2->[1];
191                 }
192         }
193         for my $i (@new_epoch) { $mg->epoch_cfg_set($i) }
194         if ($ft) {
195                 my $fn = $ft->filename;
196                 rename($fn, $mf) or die "E: rename($fn, $mf): $!\n";
197                 $ft->unlink_on_destroy(0);
198         }
199         $lei->child_error($xit << 8) if $fp2 && $xit;
200 }
201
202 1;