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