lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / solver_git.t
blobdb6729040c96eb19b8d274ca4d10c0949e6059d9
1 #!perl -w
2 # Copyright (C)  all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use v5.12;
5 use PublicInbox::TestCommon;
6 use Cwd qw(abs_path);
7 require_git v2.6;
8 use PublicInbox::ContentHash qw(git_sha);
9 use PublicInbox::Spawn qw(run_qx);
10 require_mods(qw(DBD::SQLite Xapian URI::Escape));
11 require PublicInbox::SolverGit;
12 my $rdr = { 2 => \(my $null) };
13 my $git_dir = xqx([qw(git rev-parse --git-common-dir)], undef, $rdr);
14 $git_dir = xqx([qw(git rev-parse --git-dir)], undef, $rdr) if $? != 0;
15 $? == 0 or plan skip_all => "$0 must be run from a git working tree";
16 chomp $git_dir;
18 # needed for alternates, and --absolute-git-dir is only in git 2.13+
19 $git_dir = abs_path($git_dir);
21 my $patch2 = eml_load 't/solve/0002-rename-with-modifications.patch';
22 my $patch2_oid = git_sha(1, $patch2)->hexdigest;
24 my ($tmpdir, $for_destroy) = tmpdir();
25 my $ibx = create_inbox 'v2', version => 2,
26                         indexlevel => 'medium', sub {
27         my ($im) = @_;
28         $im->add(eml_load 't/solve/0001-simple-mod.patch') or BAIL_OUT;
29         $im->add($patch2) or BAIL_OUT;
31 my $md = "$tmpdir/md";
32 File::Path::make_path(map { $md.$_ } (qw(/cur /new /tmp)));
33 symlink(abs_path('t/solve/0001-simple-mod.patch'), "$md/cur/foo:2,") or
34         xbail "symlink: $!";
36 my $v1_0_0_rev = '8a918a8523bc9904123460f85999d75f6d604916';
37 my $v1_0_0_tag = 'cb7c42b1e15577ed2215356a2bf925aef59cdd8d';
38 my $v1_0_0_tag_short = substr($v1_0_0_tag, 0, 16);
39 my $expect = '69df7d565d49fbaaeb0a067910f03dc22cd52bd0';
40 my $non_existent = 'ee5e32211bf62ab6531bdf39b84b6920d0b6775a';
41 my $stderr_empty = sub {
42         my ($msg) = @_;
43         open my $efh, '<', "$tmpdir/stderr.log" or xbail $!;
44         my @l = <$efh>;
45         @l = grep(!/reverse ?proxy/i, @l);
46         is_xdeeply(\@l, [], $msg // 'stderr.log is empty');
49 test_lei({tmpdir => "$tmpdir/blob"}, sub {
50         lei_ok('blob', '--mail', $patch2_oid, '-I', $ibx->{inboxdir},
51                 \'--mail works for existing oid');
52         is($lei_out, $patch2->as_string, 'blob matches');
53         ok(!lei('blob', '--mail', '69df7d5', '-I', $ibx->{inboxdir}),
54                 "--mail won't run solver");
55         like($lei_err, qr/\b69df7d5\b/, 'OID in error by git(1)');
57         lei_ok('blob', '69df7d5', '-I', $ibx->{inboxdir});
58         is(git_sha(1, \$lei_out)->hexdigest, $expect, 'blob contents output');
59         my $prev = $lei_out;
60         lei_ok(qw(blob --no-mail 69df7d5 -I), $ibx->{inboxdir});
61         is($lei_out, $prev, '--no-mail works');
62         ok(!lei(qw(blob -I), $ibx->{inboxdir}, $non_existent),
63                         'non-existent blob fails');
64         my $abbrev = substr($non_existent, 0, 7);
65         like($lei_err, qr/could not find $abbrev/, 'failed abbreviation noted');
66         SKIP: {
67                 skip '/.git exists', 1 if -e '/.git';
68                 lei_ok(qw(-C / blob 69df7d5 -I), $ibx->{inboxdir},
69                         "--git-dir=$git_dir");
70                 is($lei_out, $prev, '--git-dir works');
72                 ok(!lei(qw(-C / blob --no-cwd 69df7d5 -I), $ibx->{inboxdir}),
73                         '--no-cwd works');
74                 like($lei_err, qr/no --git-dir to try/,
75                         'lack of --git-dir noted');
77                 ok(!lei(qw(-C / blob -I), $ibx->{inboxdir}, $non_existent),
78                         'non-existent blob fails');
79                 like($lei_err, qr/no --git-dir to try/,
80                         'lack of --git-dir noted');
81         }
83         # fallbacks
84         lei_ok('blob', $v1_0_0_tag, '-I', $ibx->{inboxdir});
85         lei_ok('blob', $v1_0_0_tag_short, '-I', $ibx->{inboxdir});
86 });
88 test_lei({tmpdir => "$tmpdir/rediff"}, sub {
89         lei_ok(qw(rediff -q -U9 t/solve/0001-simple-mod.patch));
90         like($lei_out, qr!^\Q+++\E b/TODO\n@@ -103,9 \+103,11 @@!sm,
91                 'got more context with -U9');
93         my (undef, $re) = split(/\n\n/, $lei_out, 2);
94         $re =~ s/^/> /sgm;
95         substr($re, 0, 0, <<EOM);
96 From: me\@example.com
97 Subject: Re: awesome advice
99 WEB DESIGN EXPERT wrote:
101         lei_ok([qw(rediff --full-index -U16 --drq)], undef,
102                 { 0 => \$re, %$lei_opt });
103         my $exp = <<'EOM';
104 From: me@example.com
105 Subject: Re: awesome advice
108         like($lei_out, qr/\Q$exp\E/, '--drq preserved header');
110         # n.b. --drq can requote the attribution line ("So-and-so wrote:"),
111         # but it's probably not worth preventing...
113         $exp = <<'EOM';
114 > ---
115 >  TODO | 2 ++
116 >  Ω    | 5 --
117 >  1 file changed, 2 insertions(+)
119 > diff --git a/TODO b/TODO
120 > index 605013e4904baabecd4a0a55997aebd8e8477a8f..69df7d565d49fbaaeb0a067910f03dc22cd52bd0 100644
121 > --- a/TODO
122 > +++ b/TODO
123 > @@ -96,16 +96,18 @@ all need to be considered for everything we introduce)
125         $exp =~ s/^>$/> /sgm; # re-add trailing white space
126         like($lei_out, qr/\Q$exp\E/, '--drq diffstat + context');
128         lei_ok(qw(rediff -q --full-index -U9 t/solve/bare.patch));
129         $exp = <<'EOM';
130 diff --git a/script/public-inbox-extindex b/script/public-inbox-extindex
131 old mode 100644
132 new mode 100755
133 index 15ac20eb871bf47697377e58a27db23102a38fca..771486c425b315bae70fd8a82d62ab0331e0a827
134 --- a/script/public-inbox-extindex
135 +++ b/script/public-inbox-extindex
136 @@ -1,13 +1,12 @@
137  #!perl -w
139         like($lei_out, qr/\Q$exp\E/,
140                 'preserve mode, regen header + context from -U0 patch');
141         is($lei_err, '', 'no warnings from bare patch');
142         my $e = { GIT_DIR => "$ENV{HOME}/.local/share/lei/store/ALL.git" };
143         my @x = xqx([qw(git cat-file --batch-all-objects --batch-check)], $e);
144         is_deeply(\@x, [], 'no objects stored') or diag explain(\@x);
147 test_lei({tmpdir => "$tmpdir/index-eml-only"}, sub {
148         lei_ok(qw(index), $md);
149         lei_ok(qw(blob 69df7d5)); # hits LeiSearch->smsg_eml -> lms->local_blob
152 my $git = PublicInbox::Git->new($git_dir);
153 $ibx->{-repo_objs} = [ $git ];
154 my $res;
155 my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
156 open my $log, '+>>', "$tmpdir/solve.log" or die "open: $!";
157 my $psgi_env = { 'psgi.errors' => \*STDERR, 'psgi.url_scheme' => 'http',
158                 'HTTP_HOST' => 'example.com' };
159 $solver->solve($psgi_env, $log, '69df7d5', {});
160 ok($res, 'solved a blob!');
161 my $wt_git = $res->[0];
162 is(ref($wt_git), 'PublicInbox::Git', 'got a git object for the blob');
163 is($res->[1], $expect, 'resolved blob to unabbreviated identifier');
164 is($res->[2], 'blob', 'type specified');
165 is($res->[3], 4405, 'size returned');
167 is(ref($wt_git->cat_file($res->[1])), 'SCALAR', 'wt cat-file works');
168 is_deeply([$expect, 'blob', 4405],
169           [$wt_git->check($res->[1])], 'wt check works');
171 my $oid = $expect;
172 for my $i (1..2) {
173         my $more;
174         my $s = PublicInbox::SolverGit->new($ibx, sub { $more = $_[0] });
175         $s->solve($psgi_env, $log, $oid, {});
176         is($more->[1], $expect, 'resolved blob to long OID '.$i);
177         chop($oid);
180 $solver = undef;
181 $res = undef;
182 my $wt_git_dir = $wt_git->{git_dir};
183 $wt_git = undef;
184 ok(!-d $wt_git_dir, 'no references to WT held');
186 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
187 $solver->solve($psgi_env, $log, '0'x40, {});
188 is($res, undef, 'no error on z40');
190 my $git_v2_20_1_tag = '7a95a1cd084cb665c5c2586a415e42df0213af74';
191 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
192 $solver->solve($psgi_env, $log, $git_v2_20_1_tag, {});
193 is($res, undef, 'no error on a tag not in our repo');
195 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
196 $solver->solve($psgi_env, $log, '0a92431', {});
197 ok($res, 'resolved without hints');
199 my $hints = {
200         oid_a => '3435775',
201         path_a => 'HACKING',
202         path_b => 'CONTRIBUTING'
204 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
205 $solver->solve($psgi_env, $log, '0a92431', $hints);
206 my $hinted = $res;
207 # don't compare ::Git objects:
208 shift @$res; shift @$hinted;
209 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
211 my @psgi = qw(HTTP::Request::Common Plack::Test Plack::Builder);
212 SKIP: {
213         require_mods(@psgi, 7 + scalar(@psgi));
214         use_ok($_) for @psgi;
215         require PublicInbox::WWW;
216         my $binfoo = "$ibx->{inboxdir}/binfoo.git";
217         my $l = "$ibx->{inboxdir}/inbox.lock";
218         -f $l or BAIL_OUT "BUG: $l missing: $!";
219         require_ok 'PublicInbox::ViewVCS';
220         my $big_size = do {
221                 no warnings 'once';
222                 $PublicInbox::ViewVCS::MAX_SIZE + 1;
223         };
224         my %bin = (big => $big_size, small => 1);
225         my %oid; # (small|big) => OID
226         require PublicInbox::Lock;
227         my $lk = PublicInbox::Lock->new($l);
228         my $acq = $lk->lock_for_scope;
229         my $stamp = "$binfoo/stamp-";
230         if (open my $fh, '<', $stamp) {
231                 %oid = map { chomp; split(/=/, $_) } (<$fh>);
232         } else {
233                 PublicInbox::Import::init_bare($binfoo);
234                 my $cmd = [ qw(git hash-object -w --stdin) ];
235                 my $env = { GIT_DIR => $binfoo };
236                 while (my ($label, $size) = each %bin) {
237                         my $rdr = { 0 => \("\0" x $size) };
238                         chomp(my $x = run_qx($cmd , $env, $rdr));
239                         xbail "@$cmd: \$?=$?" if $?;
240                         $oid{$label} = $x;
241                 }
243                 open my $null, '<', '/dev/null' or xbail "open /dev/null: $!";
244                 my $t = xqx([qw(git mktree)], $env, { 0 => $null });
245                 xbail "mktree: $?" if $?;
246                 chomp($t);
247                 my $non_utf8 = "K\x{e5}g";
248                 $env->{GIT_AUTHOR_NAME} = $non_utf8;
249                 $env->{GIT_AUTHOR_EMAIL} = 'e@example.com';
250                 $env->{GIT_COMMITTER_NAME} = $env->{GIT_AUTHOR_NAME};
251                 $env->{GIT_COMMITTER_EMAIL} = $env->{GIT_AUTHOR_EMAIL};
252                 my $in = \"$non_utf8\n\nK\x{e5}g\n";
253                 my @ct = qw(git -c i18n.commitEncoding=iso-8859-1 commit-tree);
254                 my $c = xqx([@ct, $t], $env, { 0 => $in });
255                 xbail "commit-tree: $?" if $?;
256                 chomp($c);
257                 $oid{'iso-8859-1'} = $c;
259                 $c = xqx([@ct, '-p', $c, $t], $env, { 0 => $in });
260                 xbail "commit-tree: $?" if $?;
261                 chomp($c);
262                 $oid{'8859-parent'} = $c;
264                 open my $fh, '>', "$stamp.$$" or BAIL_OUT;
265                 while (my ($k, $v) = each %oid) {
266                         print $fh "$k=$v\n" or xbail "print: $!";
267                 }
268                 close $fh or BAIL_OUT;
269                 rename("$stamp.$$", $stamp) or BAIL_OUT;
270         }
271         undef $acq;
272         # ensure the PSGI frontend (ViewVCS) works:
273         my $name = $ibx->{name};
274         my $cfgpfx = "publicinbox.$name";
275         my $cfgpath = "$tmpdir/httpd-config";
276         open my $cfgfh, '>', $cfgpath or die;
277         print $cfgfh <<EOF or die;
278 [coderepo]
279         snapshots = tar.gz
280 [publicinbox "$name"]
281         address = $ibx->{-primary_address}
282         inboxdir = $ibx->{inboxdir}
283         coderepo = public-inbox
284         coderepo = binfoo
285         url = http://example.com/$name
286 [coderepo "public-inbox"]
287         dir = $git_dir
288         cgiturl = http://example.com/public-inbox
289 [coderepo "binfoo"]
290         dir = $binfoo
291         cgiturl = http://example.com/binfoo
293         close $cfgfh or die;
294         my $exp_digest;
295         {
296                 my $exp = xqx([qw(git archive --format=tar.gz
297                                 --prefix=public-inbox-1.0.0/ v1.0.0)],
298                                 { GIT_DIR => $git_dir });
299                 is($?, 0, 'no error from git archive');
300                 ok(length($exp) > 1024, 'expected archive generated');
301                 $exp_digest = git_sha(256, \$exp)->hexdigest;
302         };
304         my $cfg = PublicInbox::Config->new($cfgpath);
305         my $www = PublicInbox::WWW->new($cfg);
306         my $client = sub {
307                 my ($cb) = @_;
308                 my $mid = '20190401081523.16213-1-BOFH@YHBT.net';
309                 my @warn;
310                 my $res = do {
311                         local $SIG{__WARN__} = sub { push @warn, @_ };
312                         $cb->(GET("/$name/$mid/"));
313                 };
314                 is_deeply(\@warn, [], 'no warnings from rendering diff');
315                 like($res->content, qr!>&#937;</a>!, 'omega escaped');
317                 $res = $cb->(GET("/$name/3435775/s/"));
318                 is($res->code, 200, 'success with existing blob');
320                 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
321                 is($res->code, 404, 'failure with null OID');
323                 $res = $cb->(GET("/$name/$non_existent/s/"));
324                 is($res->code, 404, 'failure with non-existent OID');
326                 $res = $cb->(GET("/$name/$v1_0_0_tag/s/"));
327                 is($res->code, 200, 'shows commit (unabbreviated)');
328                 $res = $cb->(GET("/$name/$v1_0_0_tag_short/s/"));
329                 is($res->code, 200, 'shows commit (abbreviated)');
330                 while (my ($label, $size) = each %bin) {
331                         $res = $cb->(GET("/$name/$oid{$label}/s/"));
332                         is($res->code, 200, "$label binary file");
333                         ok(index($res->content,
334                                 "blob $oid{$label} $size bytes") >= 0,
335                                 "showed $label binary blob size");
336                         $res = $cb->(GET("/$name/$oid{$label}/s/raw"));
337                         is($res->code, 200, "$label raw binary download");
338                         is($res->content, "\0" x $size,
339                                 "$label content matches");
340                 }
341                 my $utf8 = 'e022d3377fd2c50fd9931bf96394728958a90bf3';
342                 $res = $cb->(GET("/$name/$utf8/s/"));
343                 is($res->code, 200, 'shows commit w/ utf8.eml');
344                 like($res->content, qr/El&#233;anor/,
345                                 'UTF-8 commit shown properly');
347                 # WwwCoderepo
348                 my $olderr;
349                 if (defined $ENV{PLACK_TEST_EXTERNALSERVER_URI}) {
350                         $stderr_empty->('nothing in stderr.log, yet');
351                 } else {
352                         open $olderr, '>&', \*STDERR or xbail "open: $!";
353                         open STDERR, '+>>', "$tmpdir/stderr.log" or
354                                 xbail "open: $!";
355                 }
356                 $res = $cb->(GET('/binfoo/'));
357                 defined($ENV{PLACK_TEST_EXTERNALSERVER_URI}) or
358                         open STDERR, '>&', $olderr or xbail "open: $!";
359                 is($res->code, 200, 'coderepo summary (binfoo)');
360                 $stderr_empty->();
362                 $res = $cb->(GET("/binfoo/$oid{'iso-8859-1'}/s/"));
363                 is($res->code, 200, 'ISO-8859-1 commit');
364                 like($res->content, qr/K&#229;g/, 'ISO-8859-1 commit message');
365                 $stderr_empty->();
367                 $res = $cb->(GET("/binfoo/$oid{'8859-parent'}/s/"));
368                 is($res->code, 200, 'commit w/ ISO-8859-parent');
369                 like($res->content, qr/K&#229;g/, 'ISO-8859-1 commit message');
370                 $stderr_empty->();
372                 $res = $cb->(GET('/public-inbox/'));
373                 is($res->code, 200, 'coderepo summary (public-inbox)');
375                 my $tip = 'invalid-'.int(rand(0xdeadbeef));
376                 $res = $cb->(GET('/public-inbox/?h='.$tip));
377                 is($res->code, 200, 'coderepo summary on dead branch');
378                 like($res->content, qr/no commits in `\Q$tip\E', yet/,
379                         'lack of commits noted');
381                 $res = $cb->(GET('/public-inbox'));
382                 is($res->code, 301, 'redirected');
384                 my $fn = 'public-inbox-1.0.0.tar.gz';
385                 $res = $cb->(GET("/public-inbox/snapshot/$fn"));
386                 is($res->code, 200, 'tar.gz snapshot');
387                 is($res->header('Content-Disposition'),
388                         qq'inline; filename="$fn"', 'c-d header');
389                 is($res->header('ETag'), qq'"$v1_0_0_rev"', 'etag header');
391                 my $got = $res->content;
392                 is(git_sha(256, \$got)->hexdigest, $exp_digest,
393                         "content matches installed `git archive' output");
394                 undef $got;
396                 $fn = 'public-inbox-1.0.2.tar.gz';
397                 $res = $cb->(GET("/public-inbox/snapshot/$fn"));
398                 is($res->code, 404, '404 on non-existent tag');
400                 $fn = 'public-inbox-1.0.0.tar.bz2';
401                 $res = $cb->(GET("/public-inbox/snapshot/$fn"));
402                 is($res->code, 404, '404 on unconfigured snapshot format');
404                 $res = $cb->(GET('/public-inbox/atom/'));
405                 is($res->code, 200, 'Atom feed');
406                 SKIP: {
407                         require_mods('XML::TreePP', 1);
408                         my $t = eval { XML::TreePP->new->parse($res->content) }
409                                 or diag explain($res);
410                         is(scalar @{$t->{feed}->{entry}}, 50,
411                                 'got 50 entries') or diag explain([$t, $res]);
413                         $res = $cb->(GET('/public-inbox/atom/COPYING'));
414                         is($res->code, 200, 'file Atom feed');
415                         $t = XML::TreePP->new->parse($res->content);
416                         ok($t->{feed}->{entry}, 'got entry') or
417                                 diag explain([ $t, $res ]);
419                         $res = $cb->(GET('/public-inbox/atom/README.md'));
420                         is($res->code, 404, '404 on missing file Atom feed');
422                         $res = $cb->(GET('/public-inbox/atom/?h=gone'));
423                         is($res->code, 404, '404 on missing Atom feed branch');
424                 }
426                 $res = $cb->(GET('/public-inbox/tree/'));
427                 is($res->code, 200, 'got 200 for root listing');
428                 $got = $res->content;
429                 like($got, qr/\bgit ls-tree\b/, 'ls-tree help shown');
431                 $res = $cb->(GET('/public-inbox/tree/README'));
432                 is($res->code, 200, 'got 200 for regular file');
433                 $got = $res->content;
434                 like($got, qr/\bgit show\b/, 'git show help shown');
436                 $res = $cb->(GET('/public-inbox/tree/Documentation'));
437                 is($res->code, 200, 'got 200 for a directory');
438                 $got = $res->content;
439                 like($got, qr/\bgit ls-tree\b/, 'ls-tree help shown');
441                 $res = $cb->(GET('/public-inbox/tree/?h=no-branch'));
442                 is($res->code, 404, 'got 404 for non-existent ref root');
443                 $res = $cb->(GET('/public-inbox/tree/README?h=no-file'));
444                 is($res->code, 404, 'got 404 for non-existent ref README');
445                 $res = $cb->(GET('/public-inbox/tree/Documentation?h=no-dir'));
446                 is($res->code, 404, 'got 404 for non-existent ref directory');
448                 $res = $cb->(GET('/public-inbox/tags.atom'));
449                 is($res->code, 200, 'Atom feed');
450                 SKIP: {
451                         require_mods('XML::TreePP', 1);
452                         my $t = XML::TreePP->new->parse($res->content);
453                         ok(scalar @{$t->{feed}->{entry}}, 'got tag entries');
454                 }
455         };
456         test_psgi(sub { $www->call(@_) }, $client);
457         my $env = { PI_CONFIG => $cfgpath, TMPDIR => $tmpdir };
458         test_httpd($env, $client, 7, sub {
459         SKIP: {
460                 require_cmd('curl', 1) or skip 'no curl', 1;
461                 mkdir "$tmpdir/ext" // xbail "mkdir $!";
462                 my $rurl = "$ENV{PLACK_TEST_EXTERNALSERVER_URI}/$name";
463                 test_lei({tmpdir => "$tmpdir/ext"}, sub {
464                         lei_ok(qw(blob --no-mail 69df7d5 -I), $rurl);
465                         is(git_sha(1, \$lei_out)->hexdigest, $expect,
466                                 'blob contents output');
467                         ok(!lei(qw(blob -I), $rurl, $non_existent),
468                                         'non-existent blob fails');
469                 });
470         }});
473 done_testing();