]> Sergey Matveev's repositories - public-inbox.git/blob - t/solver_git.t
www_coderepo: an alternative to cgit
[public-inbox.git] / t / solver_git.t
1 #!perl -w
2 # Copyright (C)  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);
11 require_mods(qw(DBD::SQLite Search::Xapian Plack::Util));
12 my $rdr = { 2 => \(my $null) };
13 my $git_dir = xqx([qw(git rev-parse --git-common-dir)], undef, $rdr);
14 $git_dir = xqx([qw(git rev-parse --git-dir)], undef, $rdr) if $? != 0;
15 $? == 0 or plan skip_all => "$0 must be run from a git working tree";
16 chomp $git_dir;
17
18 # needed for alternates, and --absolute-git-dir is only in git 2.13+
19 $git_dir = abs_path($git_dir);
20
21 use_ok "PublicInbox::$_" for (qw(Inbox V2Writable Git SolverGit WWW));
22 my $patch2 = eml_load 't/solve/0002-rename-with-modifications.patch';
23 my $patch2_oid = git_sha(1, $patch2)->hexdigest;
24
25 my ($tmpdir, $for_destroy) = tmpdir();
26 my $ibx = create_inbox 'v2', version => 2,
27                         indexlevel => 'medium', sub {
28         my ($im) = @_;
29         $im->add(eml_load 't/solve/0001-simple-mod.patch') or BAIL_OUT;
30         $im->add($patch2) or BAIL_OUT;
31 };
32 my $md = "$tmpdir/md";
33 File::Path::mkpath([map { $md.$_ } (qw(/ /cur /new /tmp))]);
34 symlink(abs_path('t/solve/0001-simple-mod.patch'), "$md/cur/foo:2,") or
35         xbail "symlink: $!";
36
37 my $v1_0_0_tag = 'cb7c42b1e15577ed2215356a2bf925aef59cdd8d';
38 my $v1_0_0_tag_short = substr($v1_0_0_tag, 0, 16);
39 my $expect = '69df7d565d49fbaaeb0a067910f03dc22cd52bd0';
40 my $non_existent = 'ee5e32211bf62ab6531bdf39b84b6920d0b6775a';
41
42 test_lei({tmpdir => "$tmpdir/blob"}, sub {
43         lei_ok('blob', '--mail', $patch2_oid, '-I', $ibx->{inboxdir},
44                 \'--mail works for existing oid');
45         is($lei_out, $patch2->as_string, 'blob matches');
46         ok(!lei('blob', '--mail', '69df7d5', '-I', $ibx->{inboxdir}),
47                 "--mail won't run solver");
48         like($lei_err, qr/\b69df7d5\b/, 'OID in error by git(1)');
49
50         lei_ok('blob', '69df7d5', '-I', $ibx->{inboxdir});
51         is(git_sha(1, \$lei_out)->hexdigest, $expect, 'blob contents output');
52         my $prev = $lei_out;
53         lei_ok(qw(blob --no-mail 69df7d5 -I), $ibx->{inboxdir});
54         is($lei_out, $prev, '--no-mail works');
55         ok(!lei(qw(blob -I), $ibx->{inboxdir}, $non_existent),
56                         'non-existent blob fails');
57         my $abbrev = substr($non_existent, 0, 7);
58         like($lei_err, qr/could not find $abbrev/, 'failed abbreviation noted');
59         SKIP: {
60                 skip '/.git exists', 1 if -e '/.git';
61                 lei_ok(qw(-C / blob 69df7d5 -I), $ibx->{inboxdir},
62                         "--git-dir=$git_dir");
63                 is($lei_out, $prev, '--git-dir works');
64
65                 ok(!lei(qw(-C / blob --no-cwd 69df7d5 -I), $ibx->{inboxdir}),
66                         '--no-cwd works');
67                 like($lei_err, qr/no --git-dir to try/,
68                         'lack of --git-dir noted');
69
70                 ok(!lei(qw(-C / blob -I), $ibx->{inboxdir}, $non_existent),
71                         'non-existent blob fails');
72                 like($lei_err, qr/no --git-dir to try/,
73                         'lack of --git-dir noted');
74         }
75
76         # fallbacks
77         lei_ok('blob', $v1_0_0_tag, '-I', $ibx->{inboxdir});
78         lei_ok('blob', $v1_0_0_tag_short, '-I', $ibx->{inboxdir});
79 });
80
81 test_lei({tmpdir => "$tmpdir/rediff"}, sub {
82         lei_ok(qw(rediff -q -U9 t/solve/0001-simple-mod.patch));
83         like($lei_out, qr!^\Q+++\E b/TODO\n@@ -103,9 \+103,11 @@!sm,
84                 'got more context with -U9');
85
86         my (undef, $re) = split(/\n\n/, $lei_out, 2);
87         $re =~ s/^/> /sgm;
88         substr($re, 0, 0, <<EOM);
89 From: me\@example.com
90 Subject: Re: awesome advice
91
92 WEB DESIGN EXPERT wrote:
93 EOM
94         lei_ok([qw(rediff --full-index -U16 --drq)], undef,
95                 { 0 => \$re, %$lei_opt });
96         my $exp = <<'EOM';
97 From: me@example.com
98 Subject: Re: awesome advice
99
100 EOM
101         like($lei_out, qr/\Q$exp\E/, '--drq preserved header');
102
103         # n.b. --drq can requote the attribution line ("So-and-so wrote:"),
104         # but it's probably not worth preventing...
105
106         $exp = <<'EOM';
107 > ---
108 >  TODO | 2 ++
109 >  Ω    | 5 --
110 >  1 file changed, 2 insertions(+)
111 >
112 > diff --git a/TODO b/TODO
113 > index 605013e4904baabecd4a0a55997aebd8e8477a8f..69df7d565d49fbaaeb0a067910f03dc22cd52bd0 100644
114 > --- a/TODO
115 > +++ b/TODO
116 > @@ -96,16 +96,18 @@ all need to be considered for everything we introduce)
117 EOM
118         $exp =~ s/^>$/> /sgm; # re-add trailing white space
119         like($lei_out, qr/\Q$exp\E/, '--drq diffstat + context');
120
121         lei_ok(qw(rediff -q --full-index -U9 t/solve/bare.patch));
122         $exp = <<'EOM';
123 diff --git a/script/public-inbox-extindex b/script/public-inbox-extindex
124 old mode 100644
125 new mode 100755
126 index 15ac20eb871bf47697377e58a27db23102a38fca..771486c425b315bae70fd8a82d62ab0331e0a827
127 --- a/script/public-inbox-extindex
128 +++ b/script/public-inbox-extindex
129 @@ -1,13 +1,12 @@
130  #!perl -w
131 EOM
132         like($lei_out, qr/\Q$exp\E/,
133                 'preserve mode, regen header + context from -U0 patch');
134         is($lei_err, '', 'no warnings from bare patch');
135         my $e = { GIT_DIR => "$ENV{HOME}/.local/share/lei/store/ALL.git" };
136         my @x = xqx([qw(git cat-file --batch-all-objects --batch-check)], $e);
137         is_deeply(\@x, [], 'no objects stored') or diag explain(\@x);
138 });
139
140 test_lei({tmpdir => "$tmpdir/index-eml-only"}, sub {
141         lei_ok(qw(index), $md);
142         lei_ok(qw(blob 69df7d5)); # hits LeiSearch->smsg_eml -> lms->local_blob
143 });
144
145 my $git = PublicInbox::Git->new($git_dir);
146 $ibx->{-repo_objs} = [ $git ];
147 my $res;
148 my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
149 open my $log, '+>>', "$tmpdir/solve.log" or die "open: $!";
150 my $psgi_env = { 'psgi.errors' => \*STDERR, 'psgi.url_scheme' => 'http',
151                 'HTTP_HOST' => 'example.com' };
152 $solver->solve($psgi_env, $log, '69df7d5', {});
153 ok($res, 'solved a blob!');
154 my $wt_git = $res->[0];
155 is(ref($wt_git), 'PublicInbox::Git', 'got a git object for the blob');
156 is($res->[1], $expect, 'resolved blob to unabbreviated identifier');
157 is($res->[2], 'blob', 'type specified');
158 is($res->[3], 4405, 'size returned');
159
160 is(ref($wt_git->cat_file($res->[1])), 'SCALAR', 'wt cat-file works');
161 is_deeply([$expect, 'blob', 4405],
162           [$wt_git->check($res->[1])], 'wt check works');
163
164 my $oid = $expect;
165 for my $i (1..2) {
166         my $more;
167         my $s = PublicInbox::SolverGit->new($ibx, sub { $more = $_[0] });
168         $s->solve($psgi_env, $log, $oid, {});
169         is($more->[1], $expect, 'resolved blob to long OID '.$i);
170         chop($oid);
171 }
172
173 $solver = undef;
174 $res = undef;
175 my $wt_git_dir = $wt_git->{git_dir};
176 $wt_git = undef;
177 ok(!-d $wt_git_dir, 'no references to WT held');
178
179 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
180 $solver->solve($psgi_env, $log, '0'x40, {});
181 is($res, undef, 'no error on z40');
182
183 my $git_v2_20_1_tag = '7a95a1cd084cb665c5c2586a415e42df0213af74';
184 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
185 $solver->solve($psgi_env, $log, $git_v2_20_1_tag, {});
186 is($res, undef, 'no error on a tag not in our repo');
187
188 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
189 $solver->solve($psgi_env, $log, '0a92431', {});
190 ok($res, 'resolved without hints');
191
192 my $hints = {
193         oid_a => '3435775',
194         path_a => 'HACKING',
195         path_b => 'CONTRIBUTING'
196 };
197 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
198 $solver->solve($psgi_env, $log, '0a92431', $hints);
199 my $hinted = $res;
200 # don't compare ::Git objects:
201 shift @$res; shift @$hinted;
202 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
203
204 my @psgi = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder);
205 SKIP: {
206         require_mods(@psgi, 7 + scalar(@psgi));
207         use_ok($_) for @psgi;
208         my $binfoo = "$ibx->{inboxdir}/binfoo.git";
209         my $l = "$ibx->{inboxdir}/inbox.lock";
210         -f $l or BAIL_OUT "BUG: $l missing: $!";
211         require_ok 'PublicInbox::ViewVCS';
212         my $big_size = do {
213                 no warnings 'once';
214                 $PublicInbox::ViewVCS::MAX_SIZE + 1;
215         };
216         my %bin = (big => $big_size, small => 1);
217         my %oid; # (small|big) => OID
218         my $lk = bless { lock_path => $l }, 'PublicInbox::Lock';
219         my $acq = $lk->lock_for_scope;
220         my $stamp = "$binfoo/stamp";
221         if (open my $fh, '<', $stamp) {
222                 %oid = map { chomp; split(/=/, $_) } (<$fh>);
223         } else {
224                 PublicInbox::Import::init_bare($binfoo);
225                 my $cmd = [ qw(git hash-object -w --stdin) ];
226                 my $env = { GIT_DIR => $binfoo };
227                 open my $fh, '>', "$stamp.$$" or BAIL_OUT;
228                 while (my ($label, $size) = each %bin) {
229                         pipe(my ($rin, $win)) or BAIL_OUT;
230                         my $rout = popen_rd($cmd , $env, { 0 => $rin });
231                         $rin = undef;
232                         print { $win } ("\0" x $size) or BAIL_OUT;
233                         close $win or BAIL_OUT;
234                         chomp(my $x = <$rout>);
235                         close $rout or BAIL_OUT "$?";
236                         print $fh "$label=$x\n" or BAIL_OUT;
237                         $oid{$label} = $x;
238                 }
239                 close $fh or BAIL_OUT;
240                 rename("$stamp.$$", $stamp) or BAIL_OUT;
241         }
242         undef $acq;
243         # ensure the PSGI frontend (ViewVCS) works:
244         my $name = $ibx->{name};
245         my $cfgpfx = "publicinbox.$name";
246         my $cfgpath = "$tmpdir/httpd-config";
247         open my $cfgfh, '>', $cfgpath or die;
248         print $cfgfh <<EOF or die;
249 [publicinbox "$name"]
250         address = $ibx->{-primary_address}
251         inboxdir = $ibx->{inboxdir}
252         coderepo = public-inbox
253         coderepo = binfoo
254         url = http://example.com/$name
255 [coderepo "public-inbox"]
256         dir = $git_dir
257         cgiturl = http://example.com/public-inbox
258 [coderepo "binfoo"]
259         dir = $binfoo
260         cgiturl = http://example.com/binfoo
261 EOF
262         close $cfgfh or die;
263         my $cfg = PublicInbox::Config->new($cfgpath);
264         my $www = PublicInbox::WWW->new($cfg);
265         my $client = sub {
266                 my ($cb) = @_;
267                 my $mid = '20190401081523.16213-1-BOFH@YHBT.net';
268                 my @warn;
269                 my $res = do {
270                         local $SIG{__WARN__} = sub { push @warn, @_ };
271                         $cb->(GET("/$name/$mid/"));
272                 };
273                 is_deeply(\@warn, [], 'no warnings from rendering diff');
274                 like($res->content, qr!>&#937;</a>!, 'omega escaped');
275
276                 $res = $cb->(GET("/$name/3435775/s/"));
277                 is($res->code, 200, 'success with existing blob');
278
279                 $res = $cb->(GET("/$name/".('0'x40).'/s/'));
280                 is($res->code, 404, 'failure with null OID');
281
282                 $res = $cb->(GET("/$name/$non_existent/s/"));
283                 is($res->code, 404, 'failure with null OID');
284
285                 $res = $cb->(GET("/$name/$v1_0_0_tag/s/"));
286                 is($res->code, 200, 'shows commit (unabbreviated)');
287                 $res = $cb->(GET("/$name/$v1_0_0_tag_short/s/"));
288                 is($res->code, 200, 'shows commit (abbreviated)');
289                 while (my ($label, $size) = each %bin) {
290                         $res = $cb->(GET("/$name/$oid{$label}/s/"));
291                         is($res->code, 200, "$label binary file");
292                         ok(index($res->content,
293                                 "blob $oid{$label} $size bytes") >= 0,
294                                 "showed $label binary blob size");
295                         $res = $cb->(GET("/$name/$oid{$label}/s/raw"));
296                         is($res->code, 200, "$label raw binary download");
297                         is($res->content, "\0" x $size,
298                                 "$label content matches");
299                 }
300                 my $utf8 = 'e022d3377fd2c50fd9931bf96394728958a90bf3';
301                 $res = $cb->(GET("/$name/$utf8/s/"));
302                 is($res->code, 200, 'shows commit w/ utf8.eml');
303                 like($res->content, qr/El&#233;anor/,
304                                 'UTF-8 commit shown properly');
305
306                 # WwwCoderepo
307                 my $olderr;
308                 if (defined $ENV{PLACK_TEST_EXTERNALSERVER_URI}) {
309                         ok(!-s "$tmpdir/stderr.log",
310                                 'nothing in stderr.log, yet');
311                 } else {
312                         open $olderr, '>&', \*STDERR or xbail "open: $!";
313                         open STDERR, '+>>', "$tmpdir/stderr.log" or
314                                 xbail "open: $!";
315                 }
316                 $res = $cb->(GET('/binfoo/'));
317                 defined($ENV{PLACK_TEST_EXTERNALSERVER_URI}) or
318                         open STDERR, '>&', $olderr or xbail "open: $!";
319                 is($res->code, 200, 'coderepo summary (binfoo)');
320                 if (ok(-s "$tmpdir/stderr.log")) {
321                         open my $fh, '<', "$tmpdir/stderr.log" or xbail $!;
322                         my $s = do { local $/; <$fh> };
323                         open $fh, '>', "$tmpdir/stderr.log" or xbail $!;
324                         ok($s =~ s/^fatal: your current branch.*?\n//sm,
325                                 'got current branch warning');
326                         ok($s =~ s/^.*? exit status=[1-9]+ .*?\n//sm,
327                                 'got exit status warning');
328                         is($s, '', 'no unexpected warnings on empty coderepo');
329                 }
330                 $res = $cb->(GET('/public-inbox/'));
331                 is($res->code, 200, 'coderepo summary (public-inbox)');
332                 $res = $cb->(GET('/public-inbox'));
333                 is($res->code, 301, 'redirected');
334         };
335         test_psgi(sub { $www->call(@_) }, $client);
336         my $env = { PI_CONFIG => $cfgpath, TMPDIR => $tmpdir };
337         test_httpd($env, $client, 7, sub {
338         SKIP: {
339                 require_cmd('curl', 1) or skip 'no curl', 1;
340                 mkdir "$tmpdir/ext" // xbail "mkdir $!";
341                 my $rurl = "$ENV{PLACK_TEST_EXTERNALSERVER_URI}/$name";
342                 test_lei({tmpdir => "$tmpdir/ext"}, sub {
343                         lei_ok(qw(blob --no-mail 69df7d5 -I), $rurl);
344                         is(git_sha(1, \$lei_out)->hexdigest, $expect,
345                                 'blob contents output');
346                         ok(!lei(qw(blob -I), $rurl, $non_existent),
347                                         'non-existent blob fails');
348                 });
349         }});
350 }
351
352 done_testing();