]> Sergey Matveev's repositories - public-inbox.git/blob - t/solver_git.t
7bf3ba21d182c2d78cd54d17b28acc6d19504d47
[public-inbox.git] / t / solver_git.t
1 #!perl -w
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>
4 use strict;
5 use v5.10.1;
6 use PublicInbox::TestCommon;
7 use Cwd qw(abs_path);
8 require_git(2.6);
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";
14 chomp $git_dir;
15
16 # needed for alternates, and --absolute-git-dir is only in git 2.13+
17 $git_dir = abs_path($git_dir);
18
19 use_ok "PublicInbox::$_" for (qw(Inbox V2Writable Git SolverGit WWW));
20
21 my ($tmpdir, $for_destroy) = tmpdir();
22 my $ibx = create_inbox 'v2', version => 2,
23                         indexlevel => 'medium', sub {
24         my ($im) = @_;
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
27                 BAIL_OUT;
28 };
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
33 test_lei({tmpdir => $tmpdir}, sub {
34         lei_ok('blob', '69df7d5', '-I', $ibx->{inboxdir});
35         is(sha1_hex("blob ".length($lei_out)."\0".$lei_out),
36                 $expect, 'blob contents output');
37         my $prev = $lei_out;
38         lei_ok(qw(blob --no-mail 69df7d5 -I), $ibx->{inboxdir});
39         is($lei_out, $prev, '--no-mail works');
40
41         # fallbacks
42         lei_ok('blob', $v1_0_0_tag, '-I', $ibx->{inboxdir});
43         lei_ok('blob', $v1_0_0_tag_short, '-I', $ibx->{inboxdir});
44 });
45
46 my $git = PublicInbox::Git->new($git_dir);
47 $ibx->{-repo_objs} = [ $git ];
48 my $res;
49 my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
50 open my $log, '+>>', "$tmpdir/solve.log" or die "open: $!";
51 my $psgi_env = { 'psgi.errors' => \*STDERR, 'psgi.url_scheme' => 'http',
52                 'HTTP_HOST' => 'example.com' };
53 $solver->solve($psgi_env, $log, '69df7d5', {});
54 ok($res, 'solved a blob!');
55 my $wt_git = $res->[0];
56 is(ref($wt_git), 'PublicInbox::Git', 'got a git object for the blob');
57 is($res->[1], $expect, 'resolved blob to unabbreviated identifier');
58 is($res->[2], 'blob', 'type specified');
59 is($res->[3], 4405, 'size returned');
60
61 is(ref($wt_git->cat_file($res->[1])), 'SCALAR', 'wt cat-file works');
62 is_deeply([$expect, 'blob', 4405],
63           [$wt_git->check($res->[1])], 'wt check works');
64
65 my $oid = $expect;
66 for my $i (1..2) {
67         my $more;
68         my $s = PublicInbox::SolverGit->new($ibx, sub { $more = $_[0] });
69         $s->solve($psgi_env, $log, $oid, {});
70         is($more->[1], $expect, 'resolved blob to long OID '.$i);
71         chop($oid);
72 }
73
74 $solver = undef;
75 $res = undef;
76 my $wt_git_dir = $wt_git->{git_dir};
77 $wt_git = undef;
78 ok(!-d $wt_git_dir, 'no references to WT held');
79
80 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
81 $solver->solve($psgi_env, $log, '0'x40, {});
82 is($res, undef, 'no error on z40');
83
84 my $git_v2_20_1_tag = '7a95a1cd084cb665c5c2586a415e42df0213af74';
85 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
86 $solver->solve($psgi_env, $log, $git_v2_20_1_tag, {});
87 is($res, undef, 'no error on a tag not in our repo');
88
89 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
90 $solver->solve($psgi_env, $log, '0a92431', {});
91 ok($res, 'resolved without hints');
92
93 my $hints = {
94         oid_a => '3435775',
95         path_a => 'HACKING',
96         path_b => 'CONTRIBUTING'
97 };
98 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
99 $solver->solve($psgi_env, $log, '0a92431', $hints);
100 my $hinted = $res;
101 # don't compare ::Git objects:
102 shift @$res; shift @$hinted;
103 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
104
105 my @psgi = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder);
106 SKIP: {
107         require_mods(@psgi, 7 + scalar(@psgi));
108         use_ok($_) for @psgi;
109         my $binfoo = "$ibx->{inboxdir}/binfoo.git";
110         my $l = "$ibx->{inboxdir}/inbox.lock";
111         -f $l or BAIL_OUT "BUG: $l missing: $!";
112         require_ok 'PublicInbox::ViewVCS';
113         my $big_size = do {
114                 no warnings 'once';
115                 $PublicInbox::ViewVCS::MAX_SIZE + 1;
116         };
117         my %bin = (big => $big_size, small => 1);
118         my %oid; # (small|big) => OID
119         my $lk = bless { lock_path => $l }, 'PublicInbox::Lock';
120         my $acq = $lk->lock_for_scope;
121         my $stamp = "$binfoo/stamp";
122         if (open my $fh, '<', $stamp) {
123                 %oid = map { chomp; split(/=/, $_) } (<$fh>);
124         } else {
125                 PublicInbox::Import::init_bare($binfoo);
126                 my $cmd = [ qw(git hash-object -w --stdin) ];
127                 my $env = { GIT_DIR => $binfoo };
128                 open my $fh, '>', "$stamp.$$" or BAIL_OUT;
129                 while (my ($label, $size) = each %bin) {
130                         pipe(my ($rin, $win)) or BAIL_OUT;
131                         my $rout = popen_rd($cmd , $env, { 0 => $rin });
132                         $rin = undef;
133                         print { $win } ("\0" x $size) or BAIL_OUT;
134                         close $win or BAIL_OUT;
135                         chomp(my $x = <$rout>);
136                         close $rout or BAIL_OUT "$?";
137                         print $fh "$label=$x\n" or BAIL_OUT;
138                         $oid{$label} = $x;
139                 }
140                 close $fh or BAIL_OUT;
141                 rename("$stamp.$$", $stamp) or BAIL_OUT;
142         }
143         undef $acq;
144         # ensure the PSGI frontend (ViewVCS) works:
145         my $name = $ibx->{name};
146         my $cfgpfx = "publicinbox.$name";
147         my $cfgpath = "$tmpdir/httpd-config";
148         open my $cfgfh, '>', $cfgpath or die;
149         print $cfgfh <<EOF or die;
150 [publicinbox "$name"]
151         address = $ibx->{-primary_address}
152         inboxdir = $ibx->{inboxdir}
153         coderepo = public-inbox
154         coderepo = binfoo
155         url = http://example.com/$name
156 [coderepo "public-inbox"]
157         dir = $git_dir
158         cgiturl = http://example.com/public-inbox
159 [coderepo "binfoo"]
160         dir = $binfoo
161         cgiturl = http://example.com/binfoo
162 EOF
163         close $cfgfh or die;
164         my $cfg = PublicInbox::Config->new($cfgpath);
165         my $www = PublicInbox::WWW->new($cfg);
166         my $non_existent = 'ee5e32211bf62ab6531bdf39b84b6920d0b6775a';
167         my $client = sub {
168                 my ($cb) = @_;
169                 my $mid = '20190401081523.16213-1-BOFH@YHBT.net';
170                 my @warn;
171                 my $res = do {
172                         local $SIG{__WARN__} = sub { push @warn, @_ };
173                         $cb->(GET("/$name/$mid/"));
174                 };
175                 is_deeply(\@warn, [], 'no warnings from rendering diff');
176                 like($res->content, qr!>&#937;</a>!, 'omega escaped');
177
178                 $res = $cb->(GET("/$name/3435775/s/"));
179                 is($res->code, 200, 'success with existing blob');
180
181                 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
182                 is($res->code, 404, 'failure with null OID');
183
184                 $res = $cb->(GET("/$name/$non_existent/s/"));
185                 is($res->code, 404, 'failure with null OID');
186
187                 $res = $cb->(GET("/$name/$v1_0_0_tag/s/"));
188                 is($res->code, 200, 'shows commit (unabbreviated)');
189                 $res = $cb->(GET("/$name/$v1_0_0_tag_short/s/"));
190                 is($res->code, 200, 'shows commit (abbreviated)');
191                 while (my ($label, $size) = each %bin) {
192                         $res = $cb->(GET("/$name/$oid{$label}/s/"));
193                         is($res->code, 200, "$label binary file");
194                         ok(index($res->content, "blob $size bytes") >= 0,
195                                 "showed $label binary blob size");
196                         $res = $cb->(GET("/$name/$oid{$label}/s/raw"));
197                         is($res->code, 200, "$label raw binary download");
198                         is($res->content, "\0" x $size,
199                                 "$label content matches");
200                 }
201         };
202         test_psgi(sub { $www->call(@_) }, $client);
203         SKIP: {
204                 require_mods(qw(Plack::Test::ExternalServer), 7);
205                 my $env = { PI_CONFIG => $cfgpath };
206                 my $sock = tcp_server() or die;
207                 my ($out, $err) = map { "$tmpdir/std$_.log" } qw(out err);
208                 my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
209                 my $td = start_script($cmd, $env, { 3 => $sock });
210                 my ($h, $p) = tcp_host_port($sock);
211                 local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = "http://$h:$p";
212                 Plack::Test::ExternalServer::test_psgi(client => $client);
213         }
214 }
215
216 done_testing();