2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 use PublicInbox::TestCommon;
8 use PublicInbox::ContentHash qw(git_sha);
9 use PublicInbox::Spawn qw(run_qx which);
10 use File::Path qw(remove_tree);
11 use PublicInbox::IO qw(write_file);
12 use autodie qw(close mkdir open rename symlink unlink);
13 require_mods qw(DBD::SQLite Xapian);
14 require PublicInbox::SolverGit;
15 my $rdr = { 2 => \(my $null) };
16 my $git_dir = xqx([qw(git rev-parse --git-common-dir)], undef, $rdr);
17 $git_dir = xqx([qw(git rev-parse --git-dir)], undef, $rdr) if $? != 0;
18 $? == 0 or plan skip_all => "$0 must be run from a git working tree";
21 # needed for alternates, and --absolute-git-dir is only in git 2.13+
22 $git_dir = abs_path($git_dir);
24 my $patch2 = eml_load 't/solve/0002-rename-with-modifications.patch';
25 my $patch2_oid = git_sha(1, $patch2)->hexdigest;
27 my ($tmpdir, $for_destroy) = tmpdir();
28 my $gone_repo = "$tmpdir/to-be-deleted.git";
30 my $ibx = create_inbox 'v2', version => 2,
31 indexlevel => 'medium', sub {
33 $im->add(eml_load 't/solve/0001-simple-mod.patch') or BAIL_OUT;
34 $im->add($patch2) or BAIL_OUT;
36 my $md = "$tmpdir/md";
37 File::Path::make_path(map { $md.$_ } (qw(/cur /new /tmp)));
38 symlink abs_path('t/solve/0001-simple-mod.patch'), "$md/cur/foo:2,";
40 my $v1_0_0_rev = '8a918a8523bc9904123460f85999d75f6d604916';
41 my $v1_0_0_tag = 'cb7c42b1e15577ed2215356a2bf925aef59cdd8d';
42 my $v1_0_0_tag_short = substr($v1_0_0_tag, 0, 16);
43 my $expect = '69df7d565d49fbaaeb0a067910f03dc22cd52bd0';
44 my $non_existent = 'ee5e32211bf62ab6531bdf39b84b6920d0b6775a';
45 my $stderr_empty = sub {
46 no_httpd_errors "$tmpdir/stderr.log",
47 $_[0] // 'stderr.log is empty';
50 test_lei({tmpdir => "$tmpdir/blob"}, sub {
51 lei_ok('blob', '--mail', $patch2_oid, '-I', $ibx->{inboxdir},
52 \'--mail works for existing oid');
53 is($lei_out, $patch2->as_string, 'blob matches');
54 ok(!lei('blob', '--mail', '69df7d5', '-I', $ibx->{inboxdir}),
55 "--mail won't run solver");
56 like($lei_err, qr/\b69df7d5\b/, 'OID in error by git(1)');
58 lei_ok('blob', '69df7d5', '-I', $ibx->{inboxdir});
59 is(git_sha(1, \$lei_out)->hexdigest, $expect, 'blob contents output');
61 lei_ok(qw(blob --no-mail 69df7d5 -I), $ibx->{inboxdir});
62 is($lei_out, $prev, '--no-mail works');
63 ok(!lei(qw(blob -I), $ibx->{inboxdir}, $non_existent),
64 'non-existent blob fails');
65 my $abbrev = substr($non_existent, 0, 7);
66 like($lei_err, qr/could not find $abbrev/, 'failed abbreviation noted');
68 skip '/.git exists', 1 if -e '/.git';
69 lei_ok(qw(-C / blob 69df7d5 -I), $ibx->{inboxdir},
70 "--git-dir=$git_dir");
71 is($lei_out, $prev, '--git-dir works');
73 ok(!lei(qw(-C / blob --no-cwd 69df7d5 -I), $ibx->{inboxdir}),
75 like($lei_err, qr/no --git-dir to try/,
76 'lack of --git-dir noted');
78 ok(!lei(qw(-C / blob -I), $ibx->{inboxdir}, $non_existent),
79 'non-existent blob fails');
80 like($lei_err, qr/no --git-dir to try/,
81 'lack of --git-dir noted');
85 lei_ok('blob', $v1_0_0_tag, '-I', $ibx->{inboxdir});
86 lei_ok('blob', $v1_0_0_tag_short, '-I', $ibx->{inboxdir});
89 test_lei({tmpdir => "$tmpdir/rediff"}, sub {
90 lei_ok(qw(rediff -q -U9 t/solve/0001-simple-mod.patch));
91 like($lei_out, qr!^\Q+++\E b/TODO\n@@ -103,9 \+103,11 @@!sm,
92 'got more context with -U9');
94 my (undef, $re) = split(/\n\n/, $lei_out, 2);
96 substr($re, 0, 0, <<EOM);
98 Subject: Re: awesome advice
100 WEB DESIGN EXPERT wrote:
102 lei_ok([qw(rediff --full-index -U16 --drq)], undef,
103 { 0 => \$re, %$lei_opt });
106 Subject: Re: awesome advice
109 like($lei_out, qr/\Q$exp\E/, '--drq preserved header');
111 # n.b. --drq can requote the attribution line ("So-and-so wrote:"),
112 # but it's probably not worth preventing...
118 > 1 file changed, 2 insertions(+)
120 > diff --git a/TODO b/TODO
121 > index 605013e4904baabecd4a0a55997aebd8e8477a8f..69df7d565d49fbaaeb0a067910f03dc22cd52bd0 100644
124 > @@ -96,16 +96,18 @@ all need to be considered for everything we introduce)
126 $exp =~ s/^>$/> /sgm; # re-add trailing white space
127 like($lei_out, qr/\Q$exp\E/, '--drq diffstat + context');
129 lei_ok(qw(rediff -q --full-index -U9 t/solve/bare.patch));
131 diff --git a/script/public-inbox-extindex b/script/public-inbox-extindex
134 index 15ac20eb871bf47697377e58a27db23102a38fca..771486c425b315bae70fd8a82d62ab0331e0a827
135 --- a/script/public-inbox-extindex
136 +++ b/script/public-inbox-extindex
140 like($lei_out, qr/\Q$exp\E/,
141 'preserve mode, regen header + context from -U0 patch');
142 is($lei_err, '', 'no warnings from bare patch');
143 my $e = { GIT_DIR => "$ENV{HOME}/.local/share/lei/store/ALL.git" };
144 my @x = xqx([qw(git cat-file --batch-all-objects --batch-check)], $e);
145 is_deeply(\@x, [], 'no objects stored') or diag explain(\@x);
148 test_lei({tmpdir => "$tmpdir/index-eml-only"}, sub {
149 lei_ok(qw(index), $md);
150 lei_ok(qw(blob 69df7d5)); # hits LeiSearch->smsg_eml -> lms->local_blob
153 my $git = PublicInbox::Git->new($git_dir);
154 $ibx->{-repo_objs} = [ $git ];
156 my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
157 open my $log, '+>>', "$tmpdir/solve.log";
158 my $psgi_env = { 'psgi.errors' => \*STDERR, 'psgi.url_scheme' => 'http',
159 'HTTP_HOST' => 'example.com' };
160 $solver->solve($psgi_env, $log, '69df7d5', {});
161 ok($res, 'solved a blob!');
162 my $wt_git = $res->[0];
163 is(ref($wt_git), 'PublicInbox::Git', 'got a git object for the blob');
164 is($res->[1], $expect, 'resolved blob to unabbreviated identifier');
165 is($res->[2], 'blob', 'type specified');
166 is($res->[3], 4405, 'size returned');
168 is(ref($wt_git->cat_file($res->[1])), 'SCALAR', 'wt cat-file works');
169 is_deeply([$expect, 'blob', 4405],
170 [$wt_git->check($res->[1])], 'wt check works');
175 my $s = PublicInbox::SolverGit->new($ibx, sub { $more = $_[0] });
176 $s->solve($psgi_env, $log, $oid, {});
177 is($more->[1], $expect, 'resolved blob to long OID '.$i);
183 my $wt_git_dir = $wt_git->{git_dir};
185 ok(!-d $wt_git_dir, 'no references to WT held');
187 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
188 $solver->solve($psgi_env, $log, '0'x40, {});
189 is($res, undef, 'no error on z40');
191 my $git_v2_20_1_tag = '7a95a1cd084cb665c5c2586a415e42df0213af74';
192 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
193 $solver->solve($psgi_env, $log, $git_v2_20_1_tag, {});
194 is($res, undef, 'no error on a tag not in our repo');
196 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
197 $solver->solve($psgi_env, $log, '0a92431', {});
198 ok($res, 'resolved without hints');
203 path_b => 'CONTRIBUTING'
205 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
206 $solver->solve($psgi_env, $log, '0a92431', $hints);
208 # don't compare ::Git objects:
209 shift @$res; shift @$hinted;
210 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
213 require_mods qw(psgi 1);
214 require PublicInbox::WWW;
215 my $binfoo = "$ibx->{inboxdir}/binfoo.git";
216 my $l = "$ibx->{inboxdir}/inbox.lock";
217 -f $l or BAIL_OUT "BUG: $l missing: $!";
218 require_ok 'PublicInbox::ViewVCS';
221 $PublicInbox::ViewVCS::MAX_SIZE + 1;
223 my %bin = (big => $big_size, small => 1);
224 my %oid; # (small|big) => OID
225 require PublicInbox::Lock;
226 my $lk = PublicInbox::Lock->new($l);
227 my $acq = $lk->lock_for_scope;
228 my $stamp = "$binfoo/stamp-";
229 if (CORE::open my $fh, '<', $stamp) {
230 %oid = map { chomp; split(/=/, $_) } (<$fh>);
232 PublicInbox::Import::init_bare($binfoo);
233 my $cmd = [ qw(git hash-object -w --stdin) ];
234 my $env = { GIT_DIR => $binfoo };
235 while (my ($label, $size) = each %bin) {
236 my $rdr = { 0 => \("\0" x $size) };
237 chomp(my $x = run_qx($cmd , $env, $rdr));
238 xbail "@$cmd: \$?=$?" if $?;
242 open my $null, '<', '/dev/null';
243 my $t = xqx([qw(git mktree)], $env, { 0 => $null });
244 xbail "mktree: $?" if $?;
246 my $non_utf8 = "K\x{e5}g";
247 $env->{GIT_AUTHOR_NAME} = $non_utf8;
248 $env->{GIT_AUTHOR_EMAIL} = 'e@example.com';
249 $env->{GIT_COMMITTER_NAME} = $env->{GIT_AUTHOR_NAME};
250 $env->{GIT_COMMITTER_EMAIL} = $env->{GIT_AUTHOR_EMAIL};
251 my $in = \"$non_utf8\n\nK\x{e5}g\n";
252 my @ct = qw(git -c i18n.commitEncoding=iso-8859-1 commit-tree);
253 my $c = xqx([@ct, $t], $env, { 0 => $in });
254 xbail "commit-tree: $?" if $?;
256 $oid{'iso-8859-1'} = $c;
258 $c = xqx([@ct, '-p', $c, $t], $env, { 0 => $in });
259 xbail "commit-tree: $?" if $?;
261 $oid{'8859-parent'} = $c;
263 open my $fh, '>', "$stamp.$$";
264 while (my ($k, $v) = each %oid) {
268 rename "$stamp.$$", $stamp;
271 # ensure the PSGI frontend (ViewVCS) works:
272 my $name = $ibx->{name};
273 my $cfgpfx = "publicinbox.$name";
274 my $cfgpath = "$tmpdir/httpd-config";
275 write_file '>', $cfgpath, <<EOF;
278 [publicinbox "$name"]
279 address = $ibx->{-primary_address}
280 inboxdir = $ibx->{inboxdir}
281 coderepo = public-inbox
283 url = http://example.com/$name
284 [coderepo "public-inbox"]
286 cgiturl = http://example.com/public-inbox
289 cgiturl = http://example.com/binfoo
295 my $exp = xqx([qw(git archive --format=tar.gz
296 --prefix=public-inbox-1.0.0/ v1.0.0)],
297 { GIT_DIR => $git_dir });
298 is($?, 0, 'no error from git archive');
299 ok(length($exp) > 1024, 'expected archive generated');
300 $exp_digest = git_sha(256, \$exp)->hexdigest;
303 my $cfg = PublicInbox::Config->new($cfgpath);
304 my $www = PublicInbox::WWW->new($cfg);
307 my $mid = '20190401081523.16213-1-BOFH@YHBT.net';
310 local $SIG{__WARN__} = sub { push @warn, @_ };
311 $cb->(GET("/$name/$mid/"));
313 is_deeply(\@warn, [], 'no warnings from rendering diff');
314 like($res->content, qr!>Ω</a>!, 'omega escaped');
316 $res = $cb->(GET("/$name/3435775/s/"));
317 is($res->code, 200, 'success with existing blob');
319 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
320 is($res->code, 404, 'failure with null OID');
322 $res = $cb->(GET("/$name/$non_existent/s/"));
323 is($res->code, 404, 'failure with non-existent OID');
325 $res = $cb->(GET("/$name/$v1_0_0_tag/s/"));
326 is($res->code, 200, 'shows commit (unabbreviated)');
327 $res = $cb->(GET("/$name/$v1_0_0_tag_short/s/"));
328 is($res->code, 200, 'shows commit (abbreviated)');
329 while (my ($label, $size) = each %bin) {
330 $res = $cb->(GET("/$name/$oid{$label}/s/"));
331 is($res->code, 200, "$label binary file");
332 ok(index($res->content,
333 "blob $oid{$label} $size bytes") >= 0,
334 "showed $label binary blob size");
335 $res = $cb->(GET("/$name/$oid{$label}/s/raw"));
336 is($res->code, 200, "$label raw binary download");
337 is($res->content, "\0" x $size,
338 "$label content matches");
340 my $utf8 = 'e022d3377fd2c50fd9931bf96394728958a90bf3';
341 $res = $cb->(GET("/$name/$utf8/s/"));
342 is($res->code, 200, 'shows commit w/ utf8.eml');
343 like($res->content, qr/Eléanor/,
344 'UTF-8 commit shown properly');
348 if (defined $ENV{PLACK_TEST_EXTERNALSERVER_URI}) {
349 $stderr_empty->('nothing in stderr.log, yet');
351 open $olderr, '>&', \*STDERR;
352 open STDERR, '+>>', "$tmpdir/stderr.log";
354 $res = $cb->(GET('/binfoo/'));
355 defined($ENV{PLACK_TEST_EXTERNALSERVER_URI}) or
356 open STDERR, '>&', $olderr;
357 is($res->code, 200, 'coderepo summary (binfoo)');
360 $res = $cb->(GET("/binfoo/$oid{'iso-8859-1'}/s/"));
361 is($res->code, 200, 'ISO-8859-1 commit');
362 like($res->content, qr/Kåg/, 'ISO-8859-1 commit message');
365 $res = $cb->(GET("/binfoo/$oid{'8859-parent'}/s/"));
366 is($res->code, 200, 'commit w/ ISO-8859-parent');
367 like($res->content, qr/Kåg/, 'ISO-8859-1 commit message');
370 $res = $cb->(GET('/public-inbox/'));
371 is($res->code, 200, 'coderepo summary (public-inbox)');
373 my $tip = 'invalid-'.int(rand(0xdeadbeef));
374 $res = $cb->(GET('/public-inbox/?h='.$tip));
375 is($res->code, 200, 'coderepo summary on dead branch');
376 like($res->content, qr/no commits in `\Q$tip\E', yet/,
377 'lack of commits noted');
379 $res = $cb->(GET('/public-inbox'));
380 is($res->code, 301, 'redirected');
382 my $fn = 'public-inbox-1.0.0.tar.gz';
383 $res = $cb->(GET("/public-inbox/snapshot/$fn"));
384 is($res->code, 200, 'tar.gz snapshot');
385 is($res->header('Content-Disposition'),
386 qq'inline; filename="$fn"', 'c-d header');
387 is($res->header('ETag'), qq'"$v1_0_0_rev"', 'etag header');
389 my $got = $res->content;
390 is(git_sha(256, \$got)->hexdigest, $exp_digest,
391 "content matches installed `git archive' output");
394 $fn = 'public-inbox-1.0.2.tar.gz';
395 $res = $cb->(GET("/public-inbox/snapshot/$fn"));
396 is($res->code, 404, '404 on non-existent tag');
398 $fn = 'public-inbox-1.0.0.tar.bz2';
399 $res = $cb->(GET("/public-inbox/snapshot/$fn"));
400 is($res->code, 404, '404 on unconfigured snapshot format');
402 $res = $cb->(GET('/public-inbox/atom/'));
403 is($res->code, 200, 'Atom feed');
405 require_mods('XML::TreePP', 1);
406 my $t = eval { XML::TreePP->new->parse($res->content) }
407 or diag explain($res);
408 is(scalar @{$t->{feed}->{entry}}, 50,
409 'got 50 entries') or diag explain([$t, $res]);
411 $res = $cb->(GET('/public-inbox/atom/COPYING'));
412 is($res->code, 200, 'file Atom feed');
413 $t = XML::TreePP->new->parse($res->content);
414 ok($t->{feed}->{entry}, 'got entry') or
415 diag explain([ $t, $res ]);
417 $res = $cb->(GET('/public-inbox/atom/README.md'));
418 is($res->code, 404, '404 on missing file Atom feed');
420 $res = $cb->(GET('/public-inbox/atom/?h=gone'));
421 is($res->code, 404, '404 on missing Atom feed branch');
424 $res = $cb->(GET('/public-inbox/tree/'));
425 is($res->code, 200, 'got 200 for root listing');
426 $got = $res->content;
427 like($got, qr/\bgit ls-tree\b/, 'ls-tree help shown');
429 $res = $cb->(GET('/public-inbox/tree/README'));
430 is($res->code, 200, 'got 200 for regular file');
431 $got = $res->content;
432 like($got, qr/\bgit show\b/, 'git show help shown');
434 $res = $cb->(GET('/public-inbox/tree/Documentation'));
435 is($res->code, 200, 'got 200 for a directory');
436 $got = $res->content;
437 like($got, qr/\bgit ls-tree\b/, 'ls-tree help shown');
439 $res = $cb->(GET('/public-inbox/tree/?h=no-branch'));
440 is($res->code, 404, 'got 404 for non-existent ref root');
441 $res = $cb->(GET('/public-inbox/tree/README?h=no-file'));
442 is($res->code, 404, 'got 404 for non-existent ref README');
443 $res = $cb->(GET('/public-inbox/tree/Documentation?h=no-dir'));
444 is($res->code, 404, 'got 404 for non-existent ref directory');
446 $res = $cb->(GET('/public-inbox/tags.atom'));
447 is($res->code, 200, 'Atom feed');
449 require_mods('XML::TreePP', 1);
450 my $t = XML::TreePP->new->parse($res->content);
451 ok(scalar @{$t->{feed}->{entry}}, 'got tag entries');
454 # test disappearing repos
455 $res = $cb->(GET('/goner/'));
456 is $res->code, 200, 'coderepo summary for goner repo';
457 $res = $cb->(GET("/goner/$oid{'iso-8859-1'}/s/"));
458 is $res->code, 200, 'goner repo OID /s/ still available';
460 remove_tree $gone_repo;
462 $res = $cb->(GET('/goner/'));
463 is $res->code, 404, '/goner/ repo summary';
466 $res = $cb->(GET("/goner/$oid{'iso-8859-1'}/s/"));
467 is $res->code, 404, 'gone repo OID /s/';
471 state $cp = which('cp');
472 $cp or xbail "`cp' not available (WTF!?)";
473 xsys_e $cp, qw(-Rp), $binfoo, $gone_repo;
475 test_psgi(sub { $www->call(@_) }, $client);
476 my $env = { PI_CONFIG => $cfgpath, TMPDIR => $tmpdir };
478 xsys_e $cp, qw(-Rp), $binfoo, $gone_repo; # for test_httpd $client
479 test_httpd($env, $client, 7, sub {
481 require_cmd('curl', 1) or skip 'no curl', 1;
483 my $rurl = "$ENV{PLACK_TEST_EXTERNALSERVER_URI}/$name";
484 test_lei({tmpdir => "$tmpdir/ext"}, sub {
485 lei_ok(qw(blob --no-mail 69df7d5 -I), $rurl);
486 is(git_sha(1, \$lei_out)->hexdigest, $expect,
487 'blob contents output');
488 ok(!lei(qw(blob -I), $rurl, $non_existent),
489 'non-existent blob fails');