2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
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 $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";
18 # needed for alternates, and --absolute-git-dir is only in git 2.13+
19 $git_dir = abs_path($git_dir);
21 use_ok "PublicInbox::$_" for (qw(Inbox V2Writable Git SolverGit WWW));
22 my $patch2 = eml_load 't/solve/0002-rename-with-modifications.patch';
23 my $patch2_oid = git_sha(1, $patch2)->hexdigest;
25 my ($tmpdir, $for_destroy) = tmpdir();
26 my $ibx = create_inbox 'v2', version => 2,
27 indexlevel => 'medium', sub {
29 $im->add(eml_load 't/solve/0001-simple-mod.patch') or BAIL_OUT;
30 $im->add($patch2) or BAIL_OUT;
32 my $md = "$tmpdir/md";
33 File::Path::mkpath([map { $md.$_ } (qw(/ /cur /new /tmp))]);
34 symlink(abs_path('t/solve/0001-simple-mod.patch'), "$md/cur/foo:2,") or
37 my $v1_0_0_rev = '8a918a8523bc9904123460f85999d75f6d604916';
38 my $v1_0_0_tag = 'cb7c42b1e15577ed2215356a2bf925aef59cdd8d';
39 my $v1_0_0_tag_short = substr($v1_0_0_tag, 0, 16);
40 my $expect = '69df7d565d49fbaaeb0a067910f03dc22cd52bd0';
41 my $non_existent = 'ee5e32211bf62ab6531bdf39b84b6920d0b6775a';
43 test_lei({tmpdir => "$tmpdir/blob"}, sub {
44 lei_ok('blob', '--mail', $patch2_oid, '-I', $ibx->{inboxdir},
45 \'--mail works for existing oid');
46 is($lei_out, $patch2->as_string, 'blob matches');
47 ok(!lei('blob', '--mail', '69df7d5', '-I', $ibx->{inboxdir}),
48 "--mail won't run solver");
49 like($lei_err, qr/\b69df7d5\b/, 'OID in error by git(1)');
51 lei_ok('blob', '69df7d5', '-I', $ibx->{inboxdir});
52 is(git_sha(1, \$lei_out)->hexdigest, $expect, 'blob contents output');
54 lei_ok(qw(blob --no-mail 69df7d5 -I), $ibx->{inboxdir});
55 is($lei_out, $prev, '--no-mail works');
56 ok(!lei(qw(blob -I), $ibx->{inboxdir}, $non_existent),
57 'non-existent blob fails');
58 my $abbrev = substr($non_existent, 0, 7);
59 like($lei_err, qr/could not find $abbrev/, 'failed abbreviation noted');
61 skip '/.git exists', 1 if -e '/.git';
62 lei_ok(qw(-C / blob 69df7d5 -I), $ibx->{inboxdir},
63 "--git-dir=$git_dir");
64 is($lei_out, $prev, '--git-dir works');
66 ok(!lei(qw(-C / blob --no-cwd 69df7d5 -I), $ibx->{inboxdir}),
68 like($lei_err, qr/no --git-dir to try/,
69 'lack of --git-dir noted');
71 ok(!lei(qw(-C / blob -I), $ibx->{inboxdir}, $non_existent),
72 'non-existent blob fails');
73 like($lei_err, qr/no --git-dir to try/,
74 'lack of --git-dir noted');
78 lei_ok('blob', $v1_0_0_tag, '-I', $ibx->{inboxdir});
79 lei_ok('blob', $v1_0_0_tag_short, '-I', $ibx->{inboxdir});
82 test_lei({tmpdir => "$tmpdir/rediff"}, sub {
83 lei_ok(qw(rediff -q -U9 t/solve/0001-simple-mod.patch));
84 like($lei_out, qr!^\Q+++\E b/TODO\n@@ -103,9 \+103,11 @@!sm,
85 'got more context with -U9');
87 my (undef, $re) = split(/\n\n/, $lei_out, 2);
89 substr($re, 0, 0, <<EOM);
91 Subject: Re: awesome advice
93 WEB DESIGN EXPERT wrote:
95 lei_ok([qw(rediff --full-index -U16 --drq)], undef,
96 { 0 => \$re, %$lei_opt });
99 Subject: Re: awesome advice
102 like($lei_out, qr/\Q$exp\E/, '--drq preserved header');
104 # n.b. --drq can requote the attribution line ("So-and-so wrote:"),
105 # but it's probably not worth preventing...
111 > 1 file changed, 2 insertions(+)
113 > diff --git a/TODO b/TODO
114 > index 605013e4904baabecd4a0a55997aebd8e8477a8f..69df7d565d49fbaaeb0a067910f03dc22cd52bd0 100644
117 > @@ -96,16 +96,18 @@ all need to be considered for everything we introduce)
119 $exp =~ s/^>$/> /sgm; # re-add trailing white space
120 like($lei_out, qr/\Q$exp\E/, '--drq diffstat + context');
122 lei_ok(qw(rediff -q --full-index -U9 t/solve/bare.patch));
124 diff --git a/script/public-inbox-extindex b/script/public-inbox-extindex
127 index 15ac20eb871bf47697377e58a27db23102a38fca..771486c425b315bae70fd8a82d62ab0331e0a827
128 --- a/script/public-inbox-extindex
129 +++ b/script/public-inbox-extindex
133 like($lei_out, qr/\Q$exp\E/,
134 'preserve mode, regen header + context from -U0 patch');
135 is($lei_err, '', 'no warnings from bare patch');
136 my $e = { GIT_DIR => "$ENV{HOME}/.local/share/lei/store/ALL.git" };
137 my @x = xqx([qw(git cat-file --batch-all-objects --batch-check)], $e);
138 is_deeply(\@x, [], 'no objects stored') or diag explain(\@x);
141 test_lei({tmpdir => "$tmpdir/index-eml-only"}, sub {
142 lei_ok(qw(index), $md);
143 lei_ok(qw(blob 69df7d5)); # hits LeiSearch->smsg_eml -> lms->local_blob
146 my $git = PublicInbox::Git->new($git_dir);
147 $ibx->{-repo_objs} = [ $git ];
149 my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
150 open my $log, '+>>', "$tmpdir/solve.log" or die "open: $!";
151 my $psgi_env = { 'psgi.errors' => \*STDERR, 'psgi.url_scheme' => 'http',
152 'HTTP_HOST' => 'example.com' };
153 $solver->solve($psgi_env, $log, '69df7d5', {});
154 ok($res, 'solved a blob!');
155 my $wt_git = $res->[0];
156 is(ref($wt_git), 'PublicInbox::Git', 'got a git object for the blob');
157 is($res->[1], $expect, 'resolved blob to unabbreviated identifier');
158 is($res->[2], 'blob', 'type specified');
159 is($res->[3], 4405, 'size returned');
161 is(ref($wt_git->cat_file($res->[1])), 'SCALAR', 'wt cat-file works');
162 is_deeply([$expect, 'blob', 4405],
163 [$wt_git->check($res->[1])], 'wt check works');
168 my $s = PublicInbox::SolverGit->new($ibx, sub { $more = $_[0] });
169 $s->solve($psgi_env, $log, $oid, {});
170 is($more->[1], $expect, 'resolved blob to long OID '.$i);
176 my $wt_git_dir = $wt_git->{git_dir};
178 ok(!-d $wt_git_dir, 'no references to WT held');
180 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
181 $solver->solve($psgi_env, $log, '0'x40, {});
182 is($res, undef, 'no error on z40');
184 my $git_v2_20_1_tag = '7a95a1cd084cb665c5c2586a415e42df0213af74';
185 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
186 $solver->solve($psgi_env, $log, $git_v2_20_1_tag, {});
187 is($res, undef, 'no error on a tag not in our repo');
189 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
190 $solver->solve($psgi_env, $log, '0a92431', {});
191 ok($res, 'resolved without hints');
196 path_b => 'CONTRIBUTING'
198 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
199 $solver->solve($psgi_env, $log, '0a92431', $hints);
201 # don't compare ::Git objects:
202 shift @$res; shift @$hinted;
203 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
205 my @psgi = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder);
207 require_mods(@psgi, 7 + scalar(@psgi));
208 use_ok($_) for @psgi;
209 my $binfoo = "$ibx->{inboxdir}/binfoo.git";
210 my $l = "$ibx->{inboxdir}/inbox.lock";
211 -f $l or BAIL_OUT "BUG: $l missing: $!";
212 require_ok 'PublicInbox::ViewVCS';
215 $PublicInbox::ViewVCS::MAX_SIZE + 1;
217 my %bin = (big => $big_size, small => 1);
218 my %oid; # (small|big) => OID
219 my $lk = bless { lock_path => $l }, 'PublicInbox::Lock';
220 my $acq = $lk->lock_for_scope;
221 my $stamp = "$binfoo/stamp";
222 if (open my $fh, '<', $stamp) {
223 %oid = map { chomp; split(/=/, $_) } (<$fh>);
225 PublicInbox::Import::init_bare($binfoo);
226 my $cmd = [ qw(git hash-object -w --stdin) ];
227 my $env = { GIT_DIR => $binfoo };
228 open my $fh, '>', "$stamp.$$" or BAIL_OUT;
229 while (my ($label, $size) = each %bin) {
230 pipe(my ($rin, $win)) or BAIL_OUT;
231 my $rout = popen_rd($cmd , $env, { 0 => $rin });
233 print { $win } ("\0" x $size) or BAIL_OUT;
234 close $win or BAIL_OUT;
235 chomp(my $x = <$rout>);
236 close $rout or BAIL_OUT "$?";
237 print $fh "$label=$x\n" or BAIL_OUT;
240 close $fh or BAIL_OUT;
241 rename("$stamp.$$", $stamp) or BAIL_OUT;
244 # ensure the PSGI frontend (ViewVCS) works:
245 my $name = $ibx->{name};
246 my $cfgpfx = "publicinbox.$name";
247 my $cfgpath = "$tmpdir/httpd-config";
248 open my $cfgfh, '>', $cfgpath or die;
249 print $cfgfh <<EOF or die;
252 [publicinbox "$name"]
253 address = $ibx->{-primary_address}
254 inboxdir = $ibx->{inboxdir}
255 coderepo = public-inbox
257 url = http://example.com/$name
258 [coderepo "public-inbox"]
260 cgiturl = http://example.com/public-inbox
263 cgiturl = http://example.com/binfoo
268 my $exp = xqx([qw(git archive --format=tar.gz
269 --prefix=public-inbox-1.0.0/ v1.0.0)],
270 { GIT_DIR => $git_dir });
271 is($?, 0, 'no error from git archive');
272 ok(length($exp) > 1024, 'expected archive generated');
273 $exp_digest = git_sha(256, \$exp)->hexdigest;
276 my $cfg = PublicInbox::Config->new($cfgpath);
277 my $www = PublicInbox::WWW->new($cfg);
280 my $mid = '20190401081523.16213-1-BOFH@YHBT.net';
283 local $SIG{__WARN__} = sub { push @warn, @_ };
284 $cb->(GET("/$name/$mid/"));
286 is_deeply(\@warn, [], 'no warnings from rendering diff');
287 like($res->content, qr!>Ω</a>!, 'omega escaped');
289 $res = $cb->(GET("/$name/3435775/s/"));
290 is($res->code, 200, 'success with existing blob');
292 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
293 is($res->code, 404, 'failure with null OID');
295 $res = $cb->(GET("/$name/$non_existent/s/"));
296 is($res->code, 404, 'failure with non-existent OID');
298 $res = $cb->(GET("/$name/$v1_0_0_tag/s/"));
299 is($res->code, 200, 'shows commit (unabbreviated)');
300 $res = $cb->(GET("/$name/$v1_0_0_tag_short/s/"));
301 is($res->code, 200, 'shows commit (abbreviated)');
302 while (my ($label, $size) = each %bin) {
303 $res = $cb->(GET("/$name/$oid{$label}/s/"));
304 is($res->code, 200, "$label binary file");
305 ok(index($res->content,
306 "blob $oid{$label} $size bytes") >= 0,
307 "showed $label binary blob size");
308 $res = $cb->(GET("/$name/$oid{$label}/s/raw"));
309 is($res->code, 200, "$label raw binary download");
310 is($res->content, "\0" x $size,
311 "$label content matches");
313 my $utf8 = 'e022d3377fd2c50fd9931bf96394728958a90bf3';
314 $res = $cb->(GET("/$name/$utf8/s/"));
315 is($res->code, 200, 'shows commit w/ utf8.eml');
316 like($res->content, qr/Eléanor/,
317 'UTF-8 commit shown properly');
321 if (defined $ENV{PLACK_TEST_EXTERNALSERVER_URI}) {
322 ok(!-s "$tmpdir/stderr.log",
323 'nothing in stderr.log, yet');
325 open $olderr, '>&', \*STDERR or xbail "open: $!";
326 open STDERR, '+>>', "$tmpdir/stderr.log" or
329 $res = $cb->(GET('/binfoo/'));
330 defined($ENV{PLACK_TEST_EXTERNALSERVER_URI}) or
331 open STDERR, '>&', $olderr or xbail "open: $!";
332 is($res->code, 200, 'coderepo summary (binfoo)');
333 if (ok(-s "$tmpdir/stderr.log")) {
334 open my $fh, '<', "$tmpdir/stderr.log" or xbail $!;
335 my $s = do { local $/; <$fh> };
336 open $fh, '>', "$tmpdir/stderr.log" or xbail $!;
337 ok($s =~ s/^fatal: your current branch.*?\n//sm,
338 'got current branch warning');
339 ok($s =~ s/^.*? exit status=[1-9]+\n\z//sm,
340 'got exit status warning');
341 is($s, '', 'no unexpected warnings on empty coderepo');
343 $res = $cb->(GET('/public-inbox/'));
344 is($res->code, 200, 'coderepo summary (public-inbox)');
345 $res = $cb->(GET('/public-inbox'));
346 is($res->code, 301, 'redirected');
348 my $fn = 'public-inbox-1.0.0.tar.gz';
349 $res = $cb->(GET("/public-inbox/snapshot/$fn"));
350 is($res->code, 200, 'tar.gz snapshot');
351 is($res->header('Content-Disposition'),
352 qq'inline; filename="$fn"', 'c-d header');
353 is($res->header('ETag'), qq'"$v1_0_0_rev"', 'etag header');
355 my $got = $res->content;
356 is(git_sha(256, \$got)->hexdigest, $exp_digest,
357 "content matches installed `git archive' output");
360 $fn = 'public-inbox-1.0.2.tar.gz';
361 $res = $cb->(GET("/public-inbox/snapshot/$fn"));
362 is($res->code, 404, '404 on non-existent tag');
364 $fn = 'public-inbox-1.0.0.tar.bz2';
365 $res = $cb->(GET("/public-inbox/snapshot/$fn"));
366 is($res->code, 404, '404 on unconfigured snapshot format');
368 $res = $cb->(GET('/public-inbox/atom/'));
369 is($res->code, 200, 'Atom feed');
371 require_mods('XML::TreePP', 1);
372 my $t = XML::TreePP->new->parse($res->content);
373 is(scalar @{$t->{feed}->{entry}}, 50,
376 $res = $cb->(GET('/public-inbox/atom/COPYING'));
377 is($res->code, 200, 'file Atom feed');
378 $t = XML::TreePP->new->parse($res->content);
379 ok($t->{feed}->{entry}, 'got entry');
381 $res = $cb->(GET('/public-inbox/atom/README.md'));
382 is($res->code, 404, '404 on non-existent file Atom feed');
385 $res = $cb->(GET('/public-inbox/tree/'));
386 is($res->code, 200, 'got 200 for root listing');
387 $got = $res->content;
388 like($got, qr/\bgit ls-tree\b/, 'ls-tree help shown');
390 $res = $cb->(GET('/public-inbox/tree/README'));
391 is($res->code, 200, 'got 200 for regular file');
392 $got = $res->content;
393 like($got, qr/\bgit show\b/, 'git show help shown');
395 $res = $cb->(GET('/public-inbox/tree/Documentation'));
396 is($res->code, 200, 'got 200 for a directory');
397 $got = $res->content;
398 like($got, qr/\bgit ls-tree\b/, 'ls-tree help shown');
400 $res = $cb->(GET('/public-inbox/tree/?h=no-branch'));
401 is($res->code, 404, 'got 404 for non-existent ref root');
402 $res = $cb->(GET('/public-inbox/tree/README?h=no-file'));
403 is($res->code, 404, 'got 404 for non-existent ref README');
404 $res = $cb->(GET('/public-inbox/tree/Documentation?h=no-dir'));
405 is($res->code, 404, 'got 404 for non-existent ref directory');
407 $res = $cb->(GET('/public-inbox/tags.atom'));
408 is($res->code, 200, 'Atom feed');
410 require_mods('XML::TreePP', 1);
411 my $t = XML::TreePP->new->parse($res->content);
412 ok(scalar @{$t->{feed}->{entry}}, 'got tag entries');
415 test_psgi(sub { $www->call(@_) }, $client);
416 my $env = { PI_CONFIG => $cfgpath, TMPDIR => $tmpdir };
417 test_httpd($env, $client, 7, sub {
419 require_cmd('curl', 1) or skip 'no curl', 1;
420 mkdir "$tmpdir/ext" // xbail "mkdir $!";
421 my $rurl = "$ENV{PLACK_TEST_EXTERNALSERVER_URI}/$name";
422 test_lei({tmpdir => "$tmpdir/ext"}, sub {
423 lei_ok(qw(blob --no-mail 69df7d5 -I), $rurl);
424 is(git_sha(1, \$lei_out)->hexdigest, $expect,
425 'blob contents output');
426 ok(!lei(qw(blob -I), $rurl, $non_existent),
427 'non-existent blob fails');