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