]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Fetch.pm
multi_git: hoist out common epoch/alternates handling
[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_cmd ($$) {
19         my ($lei, $opt) = @_;
20         my @cmd = qw(git);
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 => '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) {
51                 $m0 = eval {
52                         PublicInbox::LeiMirror::decode_manifest($fh, $mf, $mf)
53                 };
54                 $lei->err($@) if $@;
55         }
56         my $curl_cmd = $lei->{curl}->for_uri($lei, $muri, @opt);
57         my $opt = {};
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         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 ];
82 }
83
84 sub do_fetch {
85         my ($cls, $lei, $cd) = @_;
86         my $ibx_ver;
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);
90         if ($ibx_ver == 1) {
91                 my $url = remote_url($lei, $dir) //
92                         die "E: $dir missing remote.origin.url\n";
93                 $ibx_uri = URI->new($url);
94         } else { # v2:
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))) {
102                                 $git_url = $url;
103                                 $epoch = $nr;
104                                 last;
105                         } else {
106                                 warn "W: $edir missing remote.origin.url\n";
107                         }
108                 }
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>
114 EOM
115                 $ibx_uri = URI->new($inbox_url);
116         }
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;
120         if ($code == 404) {
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;
125         } else {
126                 $code == 200 or die "BUG unexpected code $code\n";
127         }
128         if ($ibx_ver == 2) {
129                 defined($v1_path) and warn <<EOM;
130 E: got v1 `$v1_path' when expecting v2 epoch(s) in <$muri>, WTF?
131 EOM
132                 @git_dir = map { "$dir/git/$_.git" } sort { $a <=> $b }
133                         map { my ($nr) = (m!/([0-9]+)\.git\z!g) } @$v2_epochs;
134         } else {
135                 $git_dir[0] = $dir;
136         }
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) {
140                 my $cmd;
141                 my $opt = {}; # for spawn
142                 if (-d $d) {
143                         $opt->{-C} = $d;
144                         $cmd = [ @$torsocks, fetch_cmd($lei, $opt) ];
145                 } else {
146                         my $e_uri = $ibx_uri->clone;
147                         my ($epath) = ($d =~ m!(/git/[0-9]+\.git)\z!);
148                         defined($epath) or
149                                 die "BUG: $d is not an epoch to clone\n";
150                         $e_uri->path($ibx_uri->path.$epath);
151                         $cmd = [ @$torsocks,
152                                 PublicInbox::LeiMirror::clone_cmd($lei, $opt),
153                                 $$e_uri, $d];
154                         push @new_epoch, substr($epath, 5, -4) + 0;
155                 }
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");
160                         return;
161                 }
162         }
163         for my $i (@new_epoch) { $mg->epoch_cfg_set($i) }
164         if ($ft) {
165                 my $fn = $ft->filename;
166                 rename($fn, $mf) or die "E: rename($fn, $mf): $!\n";
167                 $ft->unlink_on_destroy(0);
168         }
169 }
170
171 1;