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_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';
42 test_lei({tmpdir => "$tmpdir/blob"}, sub {
43 lei_ok('blob', '--mail', $patch2_oid, '-I', $ibx->{inboxdir},
44 \'--mail works for existing oid');
45 is($lei_out, $patch2->as_string, 'blob matches');
46 ok(!lei('blob', '--mail', '69df7d5', '-I', $ibx->{inboxdir}),
47 "--mail won't run solver");
48 like($lei_err, qr/\b69df7d5\b/, 'OID in error by git(1)');
50 lei_ok('blob', '69df7d5', '-I', $ibx->{inboxdir});
51 is(git_sha(1, \$lei_out)->hexdigest, $expect, 'blob contents output');
53 lei_ok(qw(blob --no-mail 69df7d5 -I), $ibx->{inboxdir});
54 is($lei_out, $prev, '--no-mail works');
55 ok(!lei(qw(blob -I), $ibx->{inboxdir}, $non_existent),
56 'non-existent blob fails');
57 my $abbrev = substr($non_existent, 0, 7);
58 like($lei_err, qr/could not find $abbrev/, 'failed abbreviation noted');
60 skip '/.git exists', 1 if -e '/.git';
61 lei_ok(qw(-C / blob 69df7d5 -I), $ibx->{inboxdir},
62 "--git-dir=$git_dir");
63 is($lei_out, $prev, '--git-dir works');
65 ok(!lei(qw(-C / blob --no-cwd 69df7d5 -I), $ibx->{inboxdir}),
67 like($lei_err, qr/no --git-dir to try/,
68 'lack of --git-dir noted');
70 ok(!lei(qw(-C / blob -I), $ibx->{inboxdir}, $non_existent),
71 'non-existent blob fails');
72 like($lei_err, qr/no --git-dir to try/,
73 'lack of --git-dir noted');
77 lei_ok('blob', $v1_0_0_tag, '-I', $ibx->{inboxdir});
78 lei_ok('blob', $v1_0_0_tag_short, '-I', $ibx->{inboxdir});
81 test_lei({tmpdir => "$tmpdir/rediff"}, sub {
82 lei_ok(qw(rediff -q -U9 t/solve/0001-simple-mod.patch));
83 like($lei_out, qr!^\Q+++\E b/TODO\n@@ -103,9 \+103,11 @@!sm,
84 'got more context with -U9');
86 my (undef, $re) = split(/\n\n/, $lei_out, 2);
88 substr($re, 0, 0, <<EOM);
90 Subject: Re: awesome advice
92 WEB DESIGN EXPERT wrote:
94 lei_ok([qw(rediff --full-index -U16 --drq)], undef,
95 { 0 => \$re, %$lei_opt });
98 Subject: Re: awesome advice
101 like($lei_out, qr/\Q$exp\E/, '--drq preserved header');
103 # n.b. --drq can requote the attribution line ("So-and-so wrote:"),
104 # but it's probably not worth preventing...
110 > 1 file changed, 2 insertions(+)
112 > diff --git a/TODO b/TODO
113 > index 605013e4904baabecd4a0a55997aebd8e8477a8f..69df7d565d49fbaaeb0a067910f03dc22cd52bd0 100644
116 > @@ -96,16 +96,18 @@ all need to be considered for everything we introduce)
118 $exp =~ s/^>$/> /sgm; # re-add trailing white space
119 like($lei_out, qr/\Q$exp\E/, '--drq diffstat + context');
121 lei_ok(qw(rediff -q --full-index -U9 t/solve/bare.patch));
123 diff --git a/script/public-inbox-extindex b/script/public-inbox-extindex
126 index 15ac20eb871bf47697377e58a27db23102a38fca..771486c425b315bae70fd8a82d62ab0331e0a827
127 --- a/script/public-inbox-extindex
128 +++ b/script/public-inbox-extindex
132 like($lei_out, qr/\Q$exp\E/,
133 'preserve mode, regen header + context from -U0 patch');
134 is($lei_err, '', 'no warnings from bare patch');
135 my $e = { GIT_DIR => "$ENV{HOME}/.local/share/lei/store/ALL.git" };
136 my @x = xqx([qw(git cat-file --batch-all-objects --batch-check)], $e);
137 is_deeply(\@x, [], 'no objects stored') or diag explain(\@x);
140 test_lei({tmpdir => "$tmpdir/index-eml-only"}, sub {
141 lei_ok(qw(index), $md);
142 lei_ok(qw(blob 69df7d5)); # hits LeiSearch->smsg_eml -> lms->local_blob
145 my $git = PublicInbox::Git->new($git_dir);
146 $ibx->{-repo_objs} = [ $git ];
148 my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
149 open my $log, '+>>', "$tmpdir/solve.log" or die "open: $!";
150 my $psgi_env = { 'psgi.errors' => \*STDERR, 'psgi.url_scheme' => 'http',
151 'HTTP_HOST' => 'example.com' };
152 $solver->solve($psgi_env, $log, '69df7d5', {});
153 ok($res, 'solved a blob!');
154 my $wt_git = $res->[0];
155 is(ref($wt_git), 'PublicInbox::Git', 'got a git object for the blob');
156 is($res->[1], $expect, 'resolved blob to unabbreviated identifier');
157 is($res->[2], 'blob', 'type specified');
158 is($res->[3], 4405, 'size returned');
160 is(ref($wt_git->cat_file($res->[1])), 'SCALAR', 'wt cat-file works');
161 is_deeply([$expect, 'blob', 4405],
162 [$wt_git->check($res->[1])], 'wt check works');
167 my $s = PublicInbox::SolverGit->new($ibx, sub { $more = $_[0] });
168 $s->solve($psgi_env, $log, $oid, {});
169 is($more->[1], $expect, 'resolved blob to long OID '.$i);
175 my $wt_git_dir = $wt_git->{git_dir};
177 ok(!-d $wt_git_dir, 'no references to WT held');
179 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
180 $solver->solve($psgi_env, $log, '0'x40, {});
181 is($res, undef, 'no error on z40');
183 my $git_v2_20_1_tag = '7a95a1cd084cb665c5c2586a415e42df0213af74';
184 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
185 $solver->solve($psgi_env, $log, $git_v2_20_1_tag, {});
186 is($res, undef, 'no error on a tag not in our repo');
188 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
189 $solver->solve($psgi_env, $log, '0a92431', {});
190 ok($res, 'resolved without hints');
195 path_b => 'CONTRIBUTING'
197 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
198 $solver->solve($psgi_env, $log, '0a92431', $hints);
200 # don't compare ::Git objects:
201 shift @$res; shift @$hinted;
202 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
204 my @psgi = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder);
206 require_mods(@psgi, 7 + scalar(@psgi));
207 use_ok($_) for @psgi;
208 my $binfoo = "$ibx->{inboxdir}/binfoo.git";
209 my $l = "$ibx->{inboxdir}/inbox.lock";
210 -f $l or BAIL_OUT "BUG: $l missing: $!";
211 require_ok 'PublicInbox::ViewVCS';
214 $PublicInbox::ViewVCS::MAX_SIZE + 1;
216 my %bin = (big => $big_size, small => 1);
217 my %oid; # (small|big) => OID
218 my $lk = bless { lock_path => $l }, 'PublicInbox::Lock';
219 my $acq = $lk->lock_for_scope;
220 my $stamp = "$binfoo/stamp";
221 if (open my $fh, '<', $stamp) {
222 %oid = map { chomp; split(/=/, $_) } (<$fh>);
224 PublicInbox::Import::init_bare($binfoo);
225 my $cmd = [ qw(git hash-object -w --stdin) ];
226 my $env = { GIT_DIR => $binfoo };
227 open my $fh, '>', "$stamp.$$" or BAIL_OUT;
228 while (my ($label, $size) = each %bin) {
229 pipe(my ($rin, $win)) or BAIL_OUT;
230 my $rout = popen_rd($cmd , $env, { 0 => $rin });
232 print { $win } ("\0" x $size) or BAIL_OUT;
233 close $win or BAIL_OUT;
234 chomp(my $x = <$rout>);
235 close $rout or BAIL_OUT "$?";
236 print $fh "$label=$x\n" or BAIL_OUT;
239 close $fh or BAIL_OUT;
240 rename("$stamp.$$", $stamp) or BAIL_OUT;
243 # ensure the PSGI frontend (ViewVCS) works:
244 my $name = $ibx->{name};
245 my $cfgpfx = "publicinbox.$name";
246 my $cfgpath = "$tmpdir/httpd-config";
247 open my $cfgfh, '>', $cfgpath or die;
248 print $cfgfh <<EOF or die;
249 [publicinbox "$name"]
250 address = $ibx->{-primary_address}
251 inboxdir = $ibx->{inboxdir}
252 coderepo = public-inbox
254 url = http://example.com/$name
255 [coderepo "public-inbox"]
257 cgiturl = http://example.com/public-inbox
260 cgiturl = http://example.com/binfoo
263 my $cfg = PublicInbox::Config->new($cfgpath);
264 my $www = PublicInbox::WWW->new($cfg);
267 my $mid = '20190401081523.16213-1-BOFH@YHBT.net';
270 local $SIG{__WARN__} = sub { push @warn, @_ };
271 $cb->(GET("/$name/$mid/"));
273 is_deeply(\@warn, [], 'no warnings from rendering diff');
274 like($res->content, qr!>Ω</a>!, 'omega escaped');
276 $res = $cb->(GET("/$name/3435775/s/"));
277 is($res->code, 200, 'success with existing blob');
279 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
280 is($res->code, 404, 'failure with null OID');
282 $res = $cb->(GET("/$name/$non_existent/s/"));
283 is($res->code, 404, 'failure with null OID');
285 $res = $cb->(GET("/$name/$v1_0_0_tag/s/"));
286 is($res->code, 200, 'shows commit (unabbreviated)');
287 $res = $cb->(GET("/$name/$v1_0_0_tag_short/s/"));
288 is($res->code, 200, 'shows commit (abbreviated)');
289 while (my ($label, $size) = each %bin) {
290 $res = $cb->(GET("/$name/$oid{$label}/s/"));
291 is($res->code, 200, "$label binary file");
292 ok(index($res->content,
293 "blob $oid{$label} $size bytes") >= 0,
294 "showed $label binary blob size");
295 $res = $cb->(GET("/$name/$oid{$label}/s/raw"));
296 is($res->code, 200, "$label raw binary download");
297 is($res->content, "\0" x $size,
298 "$label content matches");
300 my $utf8 = 'e022d3377fd2c50fd9931bf96394728958a90bf3';
301 $res = $cb->(GET("/$name/$utf8/s/"));
302 is($res->code, 200, 'shows commit w/ utf8.eml');
303 like($res->content, qr/Eléanor/,
304 'UTF-8 commit shown properly');
308 if (defined $ENV{PLACK_TEST_EXTERNALSERVER_URI}) {
309 ok(!-s "$tmpdir/stderr.log",
310 'nothing in stderr.log, yet');
312 open $olderr, '>&', \*STDERR or xbail "open: $!";
313 open STDERR, '+>>', "$tmpdir/stderr.log" or
316 $res = $cb->(GET('/binfoo/'));
317 defined($ENV{PLACK_TEST_EXTERNALSERVER_URI}) or
318 open STDERR, '>&', $olderr or xbail "open: $!";
319 is($res->code, 200, 'coderepo summary (binfoo)');
320 if (ok(-s "$tmpdir/stderr.log")) {
321 open my $fh, '<', "$tmpdir/stderr.log" or xbail $!;
322 my $s = do { local $/; <$fh> };
323 open $fh, '>', "$tmpdir/stderr.log" or xbail $!;
324 ok($s =~ s/^fatal: your current branch.*?\n//sm,
325 'got current branch warning');
326 ok($s =~ s/^.*? exit status=[1-9]+ .*?\n//sm,
327 'got exit status warning');
328 is($s, '', 'no unexpected warnings on empty coderepo');
330 $res = $cb->(GET('/public-inbox/'));
331 is($res->code, 200, 'coderepo summary (public-inbox)');
332 $res = $cb->(GET('/public-inbox'));
333 is($res->code, 301, 'redirected');
335 test_psgi(sub { $www->call(@_) }, $client);
336 my $env = { PI_CONFIG => $cfgpath, TMPDIR => $tmpdir };
337 test_httpd($env, $client, 7, sub {
339 require_cmd('curl', 1) or skip 'no curl', 1;
340 mkdir "$tmpdir/ext" // xbail "mkdir $!";
341 my $rurl = "$ENV{PLACK_TEST_EXTERNALSERVER_URI}/$name";
342 test_lei({tmpdir => "$tmpdir/ext"}, sub {
343 lei_ok(qw(blob --no-mail 69df7d5 -I), $rurl);
344 is(git_sha(1, \$lei_out)->hexdigest, $expect,
345 'blob contents output');
346 ok(!lei(qw(blob -I), $rurl, $non_existent),
347 'non-existent blob fails');