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 Digest::SHA qw(sha1_hex);
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));
21 my ($tmpdir, $for_destroy) = tmpdir();
22 my $ibx = create_inbox 'v2', version => 2,
23 indexlevel => 'medium', sub {
25 $im->add(eml_load 't/solve/0001-simple-mod.patch') or BAIL_OUT;
26 $im->add(eml_load 't/solve/0002-rename-with-modifications.patch') or
29 my $v1_0_0_tag = 'cb7c42b1e15577ed2215356a2bf925aef59cdd8d';
30 my $v1_0_0_tag_short = substr($v1_0_0_tag, 0, 16);
31 my $expect = '69df7d565d49fbaaeb0a067910f03dc22cd52bd0';
32 my $non_existent = 'ee5e32211bf62ab6531bdf39b84b6920d0b6775a';
34 test_lei({tmpdir => $tmpdir}, sub {
35 lei_ok('blob', '69df7d5', '-I', $ibx->{inboxdir});
36 is(sha1_hex("blob ".length($lei_out)."\0".$lei_out),
37 $expect, 'blob contents output');
39 lei_ok(qw(blob --no-mail 69df7d5 -I), $ibx->{inboxdir});
40 is($lei_out, $prev, '--no-mail works');
41 ok(!lei(qw(blob -I), $ibx->{inboxdir}, $non_existent),
42 'non-existent blob fails');
44 skip '/.git exists', 1 if -e '/.git';
45 require PublicInbox::OnDestroy;
46 opendir my $dh, '.' or xbail "opendir: $!";
47 my $end = PublicInbox::OnDestroy->new($$, sub {
48 chdir $dh or xbail "chdir: $!";
50 lei_ok(qw(-C / blob 69df7d5 -I), $ibx->{inboxdir},
51 "--git-dir=$git_dir");
52 is($lei_out, $prev, '--git-dir works');
54 ok(!lei(qw(-C / blob --no-cwd 69df7d5 -I), $ibx->{inboxdir}),
57 ok(!lei(qw(-C / blob -I), $ibx->{inboxdir}, $non_existent),
58 'non-existent blob fails');
62 lei_ok('blob', $v1_0_0_tag, '-I', $ibx->{inboxdir});
63 lei_ok('blob', $v1_0_0_tag_short, '-I', $ibx->{inboxdir});
66 my $git = PublicInbox::Git->new($git_dir);
67 $ibx->{-repo_objs} = [ $git ];
69 my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
70 open my $log, '+>>', "$tmpdir/solve.log" or die "open: $!";
71 my $psgi_env = { 'psgi.errors' => \*STDERR, 'psgi.url_scheme' => 'http',
72 'HTTP_HOST' => 'example.com' };
73 $solver->solve($psgi_env, $log, '69df7d5', {});
74 ok($res, 'solved a blob!');
75 my $wt_git = $res->[0];
76 is(ref($wt_git), 'PublicInbox::Git', 'got a git object for the blob');
77 is($res->[1], $expect, 'resolved blob to unabbreviated identifier');
78 is($res->[2], 'blob', 'type specified');
79 is($res->[3], 4405, 'size returned');
81 is(ref($wt_git->cat_file($res->[1])), 'SCALAR', 'wt cat-file works');
82 is_deeply([$expect, 'blob', 4405],
83 [$wt_git->check($res->[1])], 'wt check works');
88 my $s = PublicInbox::SolverGit->new($ibx, sub { $more = $_[0] });
89 $s->solve($psgi_env, $log, $oid, {});
90 is($more->[1], $expect, 'resolved blob to long OID '.$i);
96 my $wt_git_dir = $wt_git->{git_dir};
98 ok(!-d $wt_git_dir, 'no references to WT held');
100 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
101 $solver->solve($psgi_env, $log, '0'x40, {});
102 is($res, undef, 'no error on z40');
104 my $git_v2_20_1_tag = '7a95a1cd084cb665c5c2586a415e42df0213af74';
105 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
106 $solver->solve($psgi_env, $log, $git_v2_20_1_tag, {});
107 is($res, undef, 'no error on a tag not in our repo');
109 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
110 $solver->solve($psgi_env, $log, '0a92431', {});
111 ok($res, 'resolved without hints');
116 path_b => 'CONTRIBUTING'
118 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
119 $solver->solve($psgi_env, $log, '0a92431', $hints);
121 # don't compare ::Git objects:
122 shift @$res; shift @$hinted;
123 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
125 my @psgi = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder);
127 require_mods(@psgi, 7 + scalar(@psgi));
128 use_ok($_) for @psgi;
129 my $binfoo = "$ibx->{inboxdir}/binfoo.git";
130 my $l = "$ibx->{inboxdir}/inbox.lock";
131 -f $l or BAIL_OUT "BUG: $l missing: $!";
132 require_ok 'PublicInbox::ViewVCS';
135 $PublicInbox::ViewVCS::MAX_SIZE + 1;
137 my %bin = (big => $big_size, small => 1);
138 my %oid; # (small|big) => OID
139 my $lk = bless { lock_path => $l }, 'PublicInbox::Lock';
140 my $acq = $lk->lock_for_scope;
141 my $stamp = "$binfoo/stamp";
142 if (open my $fh, '<', $stamp) {
143 %oid = map { chomp; split(/=/, $_) } (<$fh>);
145 PublicInbox::Import::init_bare($binfoo);
146 my $cmd = [ qw(git hash-object -w --stdin) ];
147 my $env = { GIT_DIR => $binfoo };
148 open my $fh, '>', "$stamp.$$" or BAIL_OUT;
149 while (my ($label, $size) = each %bin) {
150 pipe(my ($rin, $win)) or BAIL_OUT;
151 my $rout = popen_rd($cmd , $env, { 0 => $rin });
153 print { $win } ("\0" x $size) or BAIL_OUT;
154 close $win or BAIL_OUT;
155 chomp(my $x = <$rout>);
156 close $rout or BAIL_OUT "$?";
157 print $fh "$label=$x\n" or BAIL_OUT;
160 close $fh or BAIL_OUT;
161 rename("$stamp.$$", $stamp) or BAIL_OUT;
164 # ensure the PSGI frontend (ViewVCS) works:
165 my $name = $ibx->{name};
166 my $cfgpfx = "publicinbox.$name";
167 my $cfgpath = "$tmpdir/httpd-config";
168 open my $cfgfh, '>', $cfgpath or die;
169 print $cfgfh <<EOF or die;
170 [publicinbox "$name"]
171 address = $ibx->{-primary_address}
172 inboxdir = $ibx->{inboxdir}
173 coderepo = public-inbox
175 url = http://example.com/$name
176 [coderepo "public-inbox"]
178 cgiturl = http://example.com/public-inbox
181 cgiturl = http://example.com/binfoo
184 my $cfg = PublicInbox::Config->new($cfgpath);
185 my $www = PublicInbox::WWW->new($cfg);
188 my $mid = '20190401081523.16213-1-BOFH@YHBT.net';
191 local $SIG{__WARN__} = sub { push @warn, @_ };
192 $cb->(GET("/$name/$mid/"));
194 is_deeply(\@warn, [], 'no warnings from rendering diff');
195 like($res->content, qr!>Ω</a>!, 'omega escaped');
197 $res = $cb->(GET("/$name/3435775/s/"));
198 is($res->code, 200, 'success with existing blob');
200 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
201 is($res->code, 404, 'failure with null OID');
203 $res = $cb->(GET("/$name/$non_existent/s/"));
204 is($res->code, 404, 'failure with null OID');
206 $res = $cb->(GET("/$name/$v1_0_0_tag/s/"));
207 is($res->code, 200, 'shows commit (unabbreviated)');
208 $res = $cb->(GET("/$name/$v1_0_0_tag_short/s/"));
209 is($res->code, 200, 'shows commit (abbreviated)');
210 while (my ($label, $size) = each %bin) {
211 $res = $cb->(GET("/$name/$oid{$label}/s/"));
212 is($res->code, 200, "$label binary file");
213 ok(index($res->content, "blob $size bytes") >= 0,
214 "showed $label binary blob size");
215 $res = $cb->(GET("/$name/$oid{$label}/s/raw"));
216 is($res->code, 200, "$label raw binary download");
217 is($res->content, "\0" x $size,
218 "$label content matches");
221 test_psgi(sub { $www->call(@_) }, $client);
223 require_mods(qw(Plack::Test::ExternalServer), 7);
224 my $env = { PI_CONFIG => $cfgpath };
225 my $sock = tcp_server() or die;
226 my ($out, $err) = map { "$tmpdir/std$_.log" } qw(out err);
227 my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
228 my $td = start_script($cmd, $env, { 3 => $sock });
229 my ($h, $p) = tcp_host_port($sock);
230 local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = "http://$h:$p";
231 Plack::Test::ExternalServer::test_psgi(client => $client);