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