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