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>
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 $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";
16 # needed for alternates, and --absolute-git-dir is only in git 2.13+
17 $git_dir = abs_path($git_dir);
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;
23 my ($tmpdir, $for_destroy) = tmpdir();
24 my $ibx = create_inbox 'v2', version => 2,
25 indexlevel => 'medium', sub {
27 $im->add(eml_load 't/solve/0001-simple-mod.patch') or BAIL_OUT;
28 $im->add($patch2) or BAIL_OUT;
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
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';
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)');
48 lei_ok('blob', '69df7d5', '-I', $ibx->{inboxdir});
49 is(git_sha(1, \$lei_out)->hexdigest, $expect, 'blob contents output');
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');
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');
63 ok(!lei(qw(-C / blob --no-cwd 69df7d5 -I), $ibx->{inboxdir}),
65 like($lei_err, qr/no --git-dir to try/,
66 'lack of --git-dir noted');
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');
75 lei_ok('blob', $v1_0_0_tag, '-I', $ibx->{inboxdir});
76 lei_ok('blob', $v1_0_0_tag_short, '-I', $ibx->{inboxdir});
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));
85 diff --git a/script/public-inbox-extindex b/script/public-inbox-extindex
88 index 15ac20eb..771486c4
89 --- a/script/public-inbox-extindex
90 +++ b/script/public-inbox-extindex
94 like($lei_out, qr/\Q$exp\E/,
95 'preserve mode, regen header + context from -U0 patch');
96 is($lei_err, '', 'no warnings from bare patch');
97 my $e = { GIT_DIR => "$ENV{HOME}/.local/share/lei/store/ALL.git" };
98 my @x = xqx([qw(git cat-file --batch-all-objects --batch-check)], $e);
99 is_deeply(\@x, [], 'no objects stored') or diag explain(\@x);
102 test_lei({tmpdir => "$tmpdir/index-eml-only"}, sub {
103 lei_ok(qw(index), $md);
104 lei_ok(qw(blob 69df7d5)); # hits LeiSearch->smsg_eml -> lms->local_blob
107 my $git = PublicInbox::Git->new($git_dir);
108 $ibx->{-repo_objs} = [ $git ];
110 my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
111 open my $log, '+>>', "$tmpdir/solve.log" or die "open: $!";
112 my $psgi_env = { 'psgi.errors' => \*STDERR, 'psgi.url_scheme' => 'http',
113 'HTTP_HOST' => 'example.com' };
114 $solver->solve($psgi_env, $log, '69df7d5', {});
115 ok($res, 'solved a blob!');
116 my $wt_git = $res->[0];
117 is(ref($wt_git), 'PublicInbox::Git', 'got a git object for the blob');
118 is($res->[1], $expect, 'resolved blob to unabbreviated identifier');
119 is($res->[2], 'blob', 'type specified');
120 is($res->[3], 4405, 'size returned');
122 is(ref($wt_git->cat_file($res->[1])), 'SCALAR', 'wt cat-file works');
123 is_deeply([$expect, 'blob', 4405],
124 [$wt_git->check($res->[1])], 'wt check works');
129 my $s = PublicInbox::SolverGit->new($ibx, sub { $more = $_[0] });
130 $s->solve($psgi_env, $log, $oid, {});
131 is($more->[1], $expect, 'resolved blob to long OID '.$i);
137 my $wt_git_dir = $wt_git->{git_dir};
139 ok(!-d $wt_git_dir, 'no references to WT held');
141 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
142 $solver->solve($psgi_env, $log, '0'x40, {});
143 is($res, undef, 'no error on z40');
145 my $git_v2_20_1_tag = '7a95a1cd084cb665c5c2586a415e42df0213af74';
146 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
147 $solver->solve($psgi_env, $log, $git_v2_20_1_tag, {});
148 is($res, undef, 'no error on a tag not in our repo');
150 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
151 $solver->solve($psgi_env, $log, '0a92431', {});
152 ok($res, 'resolved without hints');
157 path_b => 'CONTRIBUTING'
159 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
160 $solver->solve($psgi_env, $log, '0a92431', $hints);
162 # don't compare ::Git objects:
163 shift @$res; shift @$hinted;
164 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
166 my @psgi = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder);
168 require_mods(@psgi, 7 + scalar(@psgi));
169 use_ok($_) for @psgi;
170 my $binfoo = "$ibx->{inboxdir}/binfoo.git";
171 my $l = "$ibx->{inboxdir}/inbox.lock";
172 -f $l or BAIL_OUT "BUG: $l missing: $!";
173 require_ok 'PublicInbox::ViewVCS';
176 $PublicInbox::ViewVCS::MAX_SIZE + 1;
178 my %bin = (big => $big_size, small => 1);
179 my %oid; # (small|big) => OID
180 my $lk = bless { lock_path => $l }, 'PublicInbox::Lock';
181 my $acq = $lk->lock_for_scope;
182 my $stamp = "$binfoo/stamp";
183 if (open my $fh, '<', $stamp) {
184 %oid = map { chomp; split(/=/, $_) } (<$fh>);
186 PublicInbox::Import::init_bare($binfoo);
187 my $cmd = [ qw(git hash-object -w --stdin) ];
188 my $env = { GIT_DIR => $binfoo };
189 open my $fh, '>', "$stamp.$$" or BAIL_OUT;
190 while (my ($label, $size) = each %bin) {
191 pipe(my ($rin, $win)) or BAIL_OUT;
192 my $rout = popen_rd($cmd , $env, { 0 => $rin });
194 print { $win } ("\0" x $size) or BAIL_OUT;
195 close $win or BAIL_OUT;
196 chomp(my $x = <$rout>);
197 close $rout or BAIL_OUT "$?";
198 print $fh "$label=$x\n" or BAIL_OUT;
201 close $fh or BAIL_OUT;
202 rename("$stamp.$$", $stamp) or BAIL_OUT;
205 # ensure the PSGI frontend (ViewVCS) works:
206 my $name = $ibx->{name};
207 my $cfgpfx = "publicinbox.$name";
208 my $cfgpath = "$tmpdir/httpd-config";
209 open my $cfgfh, '>', $cfgpath or die;
210 print $cfgfh <<EOF or die;
211 [publicinbox "$name"]
212 address = $ibx->{-primary_address}
213 inboxdir = $ibx->{inboxdir}
214 coderepo = public-inbox
216 url = http://example.com/$name
217 [coderepo "public-inbox"]
219 cgiturl = http://example.com/public-inbox
222 cgiturl = http://example.com/binfoo
225 my $cfg = PublicInbox::Config->new($cfgpath);
226 my $www = PublicInbox::WWW->new($cfg);
229 my $mid = '20190401081523.16213-1-BOFH@YHBT.net';
232 local $SIG{__WARN__} = sub { push @warn, @_ };
233 $cb->(GET("/$name/$mid/"));
235 is_deeply(\@warn, [], 'no warnings from rendering diff');
236 like($res->content, qr!>Ω</a>!, 'omega escaped');
238 $res = $cb->(GET("/$name/3435775/s/"));
239 is($res->code, 200, 'success with existing blob');
241 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
242 is($res->code, 404, 'failure with null OID');
244 $res = $cb->(GET("/$name/$non_existent/s/"));
245 is($res->code, 404, 'failure with null OID');
247 $res = $cb->(GET("/$name/$v1_0_0_tag/s/"));
248 is($res->code, 200, 'shows commit (unabbreviated)');
249 $res = $cb->(GET("/$name/$v1_0_0_tag_short/s/"));
250 is($res->code, 200, 'shows commit (abbreviated)');
251 while (my ($label, $size) = each %bin) {
252 $res = $cb->(GET("/$name/$oid{$label}/s/"));
253 is($res->code, 200, "$label binary file");
254 ok(index($res->content, "blob $size bytes") >= 0,
255 "showed $label binary blob size");
256 $res = $cb->(GET("/$name/$oid{$label}/s/raw"));
257 is($res->code, 200, "$label raw binary download");
258 is($res->content, "\0" x $size,
259 "$label content matches");
262 test_psgi(sub { $www->call(@_) }, $client);
264 require_mods(qw(Plack::Test::ExternalServer), 7);
265 my $env = { PI_CONFIG => $cfgpath };
266 my $sock = tcp_server() or die;
267 my ($out, $err) = map { "$tmpdir/std$_.log" } qw(out err);
268 my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
269 my $td = start_script($cmd, $env, { 3 => $sock });
270 my ($h, $p) = tcp_host_port($sock);
271 my $url = "http://$h:$p";
272 local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = $url;
273 Plack::Test::ExternalServer::test_psgi(client => $client);
274 require_cmd('curl', 1) or skip 'no curl', 1;
276 mkdir "$tmpdir/ext" // xbail "mkdir $!";
277 test_lei({tmpdir => "$tmpdir/ext"}, sub {
278 my $rurl = "$url/$name";
279 lei_ok(qw(blob --no-mail 69df7d5 -I), $rurl);
280 is(git_sha(1, \$lei_out)->hexdigest, $expect,
281 'blob contents output');
282 ok(!lei(qw(blob -I), $rurl, $non_existent),
283 'non-existent blob fails');