]> Sergey Matveev's repositories - public-inbox.git/blob - t/solver_git.t
fetch: support v2 w/o manifest on old WWW
[public-inbox.git] / t / solver_git.t
1 #!perl -w
2 # Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use PublicInbox::TestCommon;
7 use Cwd qw(abs_path);
8 require_git(2.6);
9 use PublicInbox::ContentHash qw(git_sha);
10 use PublicInbox::Spawn qw(popen_rd);
11 require_mods(qw(DBD::SQLite Search::Xapian Plack::Util));
12 my $git_dir = xqx([qw(git rev-parse --git-dir)], undef, {2 => \(my $null)});
13 $? == 0 or plan skip_all => "$0 must be run from a git working tree";
14 chomp $git_dir;
15
16 # needed for alternates, and --absolute-git-dir is only in git 2.13+
17 $git_dir = abs_path($git_dir);
18
19 use_ok "PublicInbox::$_" for (qw(Inbox V2Writable Git SolverGit WWW));
20 my $patch2 = eml_load 't/solve/0002-rename-with-modifications.patch';
21 my $patch2_oid = git_sha(1, $patch2)->hexdigest;
22
23 my ($tmpdir, $for_destroy) = tmpdir();
24 my $ibx = create_inbox 'v2', version => 2,
25                         indexlevel => 'medium', sub {
26         my ($im) = @_;
27         $im->add(eml_load 't/solve/0001-simple-mod.patch') or BAIL_OUT;
28         $im->add($patch2) or BAIL_OUT;
29 };
30 my $md = "$tmpdir/md";
31 File::Path::mkpath([map { $md.$_ } (qw(/ /cur /new /tmp))]);
32 symlink(abs_path('t/solve/0001-simple-mod.patch'), "$md/cur/foo:2,") or
33         xbail "symlink: $!";
34
35 my $v1_0_0_tag = 'cb7c42b1e15577ed2215356a2bf925aef59cdd8d';
36 my $v1_0_0_tag_short = substr($v1_0_0_tag, 0, 16);
37 my $expect = '69df7d565d49fbaaeb0a067910f03dc22cd52bd0';
38 my $non_existent = 'ee5e32211bf62ab6531bdf39b84b6920d0b6775a';
39
40 test_lei({tmpdir => "$tmpdir/blob"}, sub {
41         lei_ok('blob', '--mail', $patch2_oid, '-I', $ibx->{inboxdir},
42                 \'--mail works for existing oid');
43         is($lei_out, $patch2->as_string, 'blob matches');
44         ok(!lei('blob', '--mail', '69df7d5', '-I', $ibx->{inboxdir}),
45                 "--mail won't run solver");
46         like($lei_err, qr/\b69df7d5\b/, 'OID in error by git(1)');
47
48         lei_ok('blob', '69df7d5', '-I', $ibx->{inboxdir});
49         is(git_sha(1, \$lei_out)->hexdigest, $expect, 'blob contents output');
50         my $prev = $lei_out;
51         lei_ok(qw(blob --no-mail 69df7d5 -I), $ibx->{inboxdir});
52         is($lei_out, $prev, '--no-mail works');
53         ok(!lei(qw(blob -I), $ibx->{inboxdir}, $non_existent),
54                         'non-existent blob fails');
55         my $abbrev = substr($non_existent, 0, 7);
56         like($lei_err, qr/could not find $abbrev/, 'failed abbreviation noted');
57         SKIP: {
58                 skip '/.git exists', 1 if -e '/.git';
59                 lei_ok(qw(-C / blob 69df7d5 -I), $ibx->{inboxdir},
60                         "--git-dir=$git_dir");
61                 is($lei_out, $prev, '--git-dir works');
62
63                 ok(!lei(qw(-C / blob --no-cwd 69df7d5 -I), $ibx->{inboxdir}),
64                         '--no-cwd works');
65                 like($lei_err, qr/no --git-dir to try/,
66                         'lack of --git-dir noted');
67
68                 ok(!lei(qw(-C / blob -I), $ibx->{inboxdir}, $non_existent),
69                         'non-existent blob fails');
70                 like($lei_err, qr/no --git-dir to try/,
71                         'lack of --git-dir noted');
72         }
73
74         # fallbacks
75         lei_ok('blob', $v1_0_0_tag, '-I', $ibx->{inboxdir});
76         lei_ok('blob', $v1_0_0_tag_short, '-I', $ibx->{inboxdir});
77 });
78
79 test_lei({tmpdir => "$tmpdir/rediff"}, sub {
80         lei_ok(qw(rediff -q -U9 t/solve/0001-simple-mod.patch));
81         like($lei_out, qr!^\Q+++\E b/TODO\n@@ -103,9 \+103,11 @@!sm,
82                 'got more context with -U9');
83         lei_ok(qw(rediff -q -U9 t/solve/bare.patch));
84         my $exp = <<'EOM';
85 diff --git a/script/public-inbox-extindex b/script/public-inbox-extindex
86 old mode 100644
87 new mode 100755
88 index 15ac20eb..771486c4
89 --- a/script/public-inbox-extindex
90 +++ b/script/public-inbox-extindex
91 @@ -1,13 +1,12 @@
92  #!perl -w
93 EOM
94         like($lei_out, qr/\Q$exp\E/,
95                 'preserve mode, regen header + context from -U0 patch');
96         my $e = { GIT_DIR => "$ENV{HOME}/.local/share/lei/store/ALL.git" };
97         my @x = xqx([qw(git cat-file --batch-all-objects --batch-check)], $e);
98         is_deeply(\@x, [], 'no objects stored') or diag explain(\@x);
99 });
100
101 test_lei({tmpdir => "$tmpdir/index-eml-only"}, sub {
102         lei_ok(qw(index), $md);
103         lei_ok(qw(blob 69df7d5)); # hits LeiSearch->smsg_eml -> lms->local_blob
104 });
105
106 my $git = PublicInbox::Git->new($git_dir);
107 $ibx->{-repo_objs} = [ $git ];
108 my $res;
109 my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
110 open my $log, '+>>', "$tmpdir/solve.log" or die "open: $!";
111 my $psgi_env = { 'psgi.errors' => \*STDERR, 'psgi.url_scheme' => 'http',
112                 'HTTP_HOST' => 'example.com' };
113 $solver->solve($psgi_env, $log, '69df7d5', {});
114 ok($res, 'solved a blob!');
115 my $wt_git = $res->[0];
116 is(ref($wt_git), 'PublicInbox::Git', 'got a git object for the blob');
117 is($res->[1], $expect, 'resolved blob to unabbreviated identifier');
118 is($res->[2], 'blob', 'type specified');
119 is($res->[3], 4405, 'size returned');
120
121 is(ref($wt_git->cat_file($res->[1])), 'SCALAR', 'wt cat-file works');
122 is_deeply([$expect, 'blob', 4405],
123           [$wt_git->check($res->[1])], 'wt check works');
124
125 my $oid = $expect;
126 for my $i (1..2) {
127         my $more;
128         my $s = PublicInbox::SolverGit->new($ibx, sub { $more = $_[0] });
129         $s->solve($psgi_env, $log, $oid, {});
130         is($more->[1], $expect, 'resolved blob to long OID '.$i);
131         chop($oid);
132 }
133
134 $solver = undef;
135 $res = undef;
136 my $wt_git_dir = $wt_git->{git_dir};
137 $wt_git = undef;
138 ok(!-d $wt_git_dir, 'no references to WT held');
139
140 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
141 $solver->solve($psgi_env, $log, '0'x40, {});
142 is($res, undef, 'no error on z40');
143
144 my $git_v2_20_1_tag = '7a95a1cd084cb665c5c2586a415e42df0213af74';
145 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
146 $solver->solve($psgi_env, $log, $git_v2_20_1_tag, {});
147 is($res, undef, 'no error on a tag not in our repo');
148
149 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
150 $solver->solve($psgi_env, $log, '0a92431', {});
151 ok($res, 'resolved without hints');
152
153 my $hints = {
154         oid_a => '3435775',
155         path_a => 'HACKING',
156         path_b => 'CONTRIBUTING'
157 };
158 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
159 $solver->solve($psgi_env, $log, '0a92431', $hints);
160 my $hinted = $res;
161 # don't compare ::Git objects:
162 shift @$res; shift @$hinted;
163 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
164
165 my @psgi = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder);
166 SKIP: {
167         require_mods(@psgi, 7 + scalar(@psgi));
168         use_ok($_) for @psgi;
169         my $binfoo = "$ibx->{inboxdir}/binfoo.git";
170         my $l = "$ibx->{inboxdir}/inbox.lock";
171         -f $l or BAIL_OUT "BUG: $l missing: $!";
172         require_ok 'PublicInbox::ViewVCS';
173         my $big_size = do {
174                 no warnings 'once';
175                 $PublicInbox::ViewVCS::MAX_SIZE + 1;
176         };
177         my %bin = (big => $big_size, small => 1);
178         my %oid; # (small|big) => OID
179         my $lk = bless { lock_path => $l }, 'PublicInbox::Lock';
180         my $acq = $lk->lock_for_scope;
181         my $stamp = "$binfoo/stamp";
182         if (open my $fh, '<', $stamp) {
183                 %oid = map { chomp; split(/=/, $_) } (<$fh>);
184         } else {
185                 PublicInbox::Import::init_bare($binfoo);
186                 my $cmd = [ qw(git hash-object -w --stdin) ];
187                 my $env = { GIT_DIR => $binfoo };
188                 open my $fh, '>', "$stamp.$$" or BAIL_OUT;
189                 while (my ($label, $size) = each %bin) {
190                         pipe(my ($rin, $win)) or BAIL_OUT;
191                         my $rout = popen_rd($cmd , $env, { 0 => $rin });
192                         $rin = undef;
193                         print { $win } ("\0" x $size) or BAIL_OUT;
194                         close $win or BAIL_OUT;
195                         chomp(my $x = <$rout>);
196                         close $rout or BAIL_OUT "$?";
197                         print $fh "$label=$x\n" or BAIL_OUT;
198                         $oid{$label} = $x;
199                 }
200                 close $fh or BAIL_OUT;
201                 rename("$stamp.$$", $stamp) or BAIL_OUT;
202         }
203         undef $acq;
204         # ensure the PSGI frontend (ViewVCS) works:
205         my $name = $ibx->{name};
206         my $cfgpfx = "publicinbox.$name";
207         my $cfgpath = "$tmpdir/httpd-config";
208         open my $cfgfh, '>', $cfgpath or die;
209         print $cfgfh <<EOF or die;
210 [publicinbox "$name"]
211         address = $ibx->{-primary_address}
212         inboxdir = $ibx->{inboxdir}
213         coderepo = public-inbox
214         coderepo = binfoo
215         url = http://example.com/$name
216 [coderepo "public-inbox"]
217         dir = $git_dir
218         cgiturl = http://example.com/public-inbox
219 [coderepo "binfoo"]
220         dir = $binfoo
221         cgiturl = http://example.com/binfoo
222 EOF
223         close $cfgfh or die;
224         my $cfg = PublicInbox::Config->new($cfgpath);
225         my $www = PublicInbox::WWW->new($cfg);
226         my $client = sub {
227                 my ($cb) = @_;
228                 my $mid = '20190401081523.16213-1-BOFH@YHBT.net';
229                 my @warn;
230                 my $res = do {
231                         local $SIG{__WARN__} = sub { push @warn, @_ };
232                         $cb->(GET("/$name/$mid/"));
233                 };
234                 is_deeply(\@warn, [], 'no warnings from rendering diff');
235                 like($res->content, qr!>&#937;</a>!, 'omega escaped');
236
237                 $res = $cb->(GET("/$name/3435775/s/"));
238                 is($res->code, 200, 'success with existing blob');
239
240                 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
241                 is($res->code, 404, 'failure with null OID');
242
243                 $res = $cb->(GET("/$name/$non_existent/s/"));
244                 is($res->code, 404, 'failure with null OID');
245
246                 $res = $cb->(GET("/$name/$v1_0_0_tag/s/"));
247                 is($res->code, 200, 'shows commit (unabbreviated)');
248                 $res = $cb->(GET("/$name/$v1_0_0_tag_short/s/"));
249                 is($res->code, 200, 'shows commit (abbreviated)');
250                 while (my ($label, $size) = each %bin) {
251                         $res = $cb->(GET("/$name/$oid{$label}/s/"));
252                         is($res->code, 200, "$label binary file");
253                         ok(index($res->content, "blob $size bytes") >= 0,
254                                 "showed $label binary blob size");
255                         $res = $cb->(GET("/$name/$oid{$label}/s/raw"));
256                         is($res->code, 200, "$label raw binary download");
257                         is($res->content, "\0" x $size,
258                                 "$label content matches");
259                 }
260         };
261         test_psgi(sub { $www->call(@_) }, $client);
262         SKIP: {
263                 require_mods(qw(Plack::Test::ExternalServer), 7);
264                 my $env = { PI_CONFIG => $cfgpath };
265                 my $sock = tcp_server() or die;
266                 my ($out, $err) = map { "$tmpdir/std$_.log" } qw(out err);
267                 my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
268                 my $td = start_script($cmd, $env, { 3 => $sock });
269                 my ($h, $p) = tcp_host_port($sock);
270                 my $url = "http://$h:$p";
271                 local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = $url;
272                 Plack::Test::ExternalServer::test_psgi(client => $client);
273                 require_cmd('curl', 1) or skip 'no curl', 1;
274
275                 mkdir "$tmpdir/ext" // xbail "mkdir $!";
276                 test_lei({tmpdir => "$tmpdir/ext"}, sub {
277                         my $rurl = "$url/$name";
278                         lei_ok(qw(blob --no-mail 69df7d5 -I), $rurl);
279                         is(git_sha(1, \$lei_out)->hexdigest, $expect,
280                                 'blob contents output');
281                         ok(!lei(qw(blob -I), $rurl, $non_existent),
282                                         'non-existent blob fails');
283                 });
284         }
285 }
286
287 done_testing();