lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / git.t
blobb7df6186aee296799470e3cb15a64134b244c08a
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 v5.12;
5 use PublicInbox::TestCommon;
6 my ($dir, $for_destroy) = tmpdir();
7 use PublicInbox::Import;
8 use POSIX qw(strftime);
9 use PublicInbox::Git;
10 is(PublicInbox::Git::MAX_INFLIGHT,
11         int(PublicInbox::Git::MAX_INFLIGHT), 'MAX_INFLIGHT is an integer');
14         PublicInbox::Import::init_bare($dir, 'master');
15         my $fi_data = './t/git.fast-import-data';
16         open my $fh, '<', $fi_data or die
17                 "fast-import data readable (or run test at top level: $!";
18         my $rdr = { 0 => $fh };
19         xsys([qw(git fast-import --quiet)], { GIT_DIR => $dir }, $rdr);
20         is($?, 0, 'fast-import succeeded');
23         my $git = PublicInbox::Git->new("$dir/foo.git");
24         my $nick = $git->local_nick; # internal sub
25         unlike($nick, qr/\.git\.git\z/, "no doubled `.git.git' suffix");
26         like($nick, qr/\.git\z/, "one `.git' suffix");
27         $git = PublicInbox::Git->new($dir);
28         $nick = $git->local_nick; # internal sub
29         like($nick, qr/\.git\z/, "local nick always adds `.git' suffix");
30         my @s = $git->date_parse('1970-01-01T00:00:00Z');
31         is($s[0], 0, 'parsed epoch');
32         local $ENV{TZ} = 'UTC';
33         @s = $git->date_parse('1993-10-02 01:02:09', '2010-10-02 01:03:04');
34         is(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($s[0])),
35                 '1993-10-02T01:02:09Z', 'round trips');
36         is(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($s[1])),
37                 '2010-10-02T01:03:04Z', '2nd arg round trips');
38         @s = $git->date_parse('1993-10-02');
39         is(strftime('%Y-%m-%d', gmtime($s[0])), '1993-10-02',
40                 'round trips date-only');
44         my $gcf = PublicInbox::Git->new($dir);
45         is($gcf->modified, 749520000, 'modified time detected from commit');
46         my $f = 'HEAD:foo.txt';
47         my @x = $gcf->check($f);
48         is(scalar @x, 3, 'returned 3 element array for existing file');
49         like($x[0], qr/\A[a-f0-9]{40,64}\z/, 'returns obj ID in 1st element');
50         is('blob', $x[1], 'returns obj type in 2nd element');
51         like($x[2], qr/\A\d+\z/, 'returns obj size in 3rd element');
53         my $raw = $gcf->cat_file($f);
54         is($x[2], length($$raw), 'length matches');
56         is(${$gcf->cat_file($f)}, $$raw, 'not broken after failures');
57         is(${$gcf->cat_file($f)}, $$raw, 'not broken after partial read');
59         my $oid = $x[0];
60         my $arg = { 'foo' => 'bar' };
61         my $res = [];
62         my $missing = [];
63         $gcf->cat_async($oid, sub {
64                 my ($bref, $oid_hex, $type, $size, $arg) = @_;
65                 $res = [ @_ ];
66         }, $arg);
67         $gcf->cat_async('non-existent', sub {
68                 my ($bref, $oid_hex, $type, $size, $arg) = @_;
69                 $missing = [ @_ ];
70         }, $arg);
71         $gcf->async_wait_all;
72         my ($bref, $oid_hex, $type, $size, $arg_res) = @$res;
73         is_deeply([$oid_hex, $type, $size], \@x, 'got expected header');
74         is($arg_res, $arg, 'arg passed to cat_async');
75         is_deeply($raw, $bref, 'blob result matches');
76         is_deeply($missing, [ undef, 'non-existent', 'missing', undef, $arg],
77                 'non-existent blob gives expected result');
79         $res = [];
80         $gcf->cat_async($oid, sub { push @$res, \@_ });
81         $gcf->cat_async($oid, sub { die 'HI' });
82         $gcf->cat_async($oid, sub { push @$res, \@_ });
83         eval { $gcf->async_wait_all };
84         like($@, qr/\bHI\b/, 'die in callback propagates');
85         is(scalar(@$res), 2, 'two results');
86         is_deeply($res->[0], [ $raw, @x, undef ], '1st cb result');
87         is_deeply($res->[1], [ undef, $oid, undef, undef, undef ],
88                 '2nd cb aborted ');
90         my @w;
91         local $PublicInbox::Git::async_warn = 1;
92         local $SIG{__WARN__} = sub { push @w, @_ };
93         $res = [];
94         $gcf->cat_async($oid, sub { push @$res, \@_ });
95         $gcf->cat_async($oid, sub { die 'HI' });
96         $gcf->cat_async($oid, sub { push @$res, \@_ });
97         eval { $gcf->async_wait_all };
98         is(scalar(@$res), 2, 'two results');
99         is_deeply($res->[0], [ $raw, @x, undef ], '1st cb result');
100         is_deeply($res->[1], [ $raw, @x, undef ], '2st cb result');
101         like("@w", qr/\bHI\b/, 'die turned to warning');
104 if (1) {
105         # need a big file, use the AGPL-3.0 :p
106         my $big_data = './COPYING';
107         ok(-r $big_data, 'COPYING readable');
108         my $size = -s $big_data;
109         ok($size > 8192, 'file is big enough');
110         open my $fh, '<', $big_data or die;
111         my $cmd = [ 'git', "--git-dir=$dir", qw(hash-object -w --stdin) ];
112         my $buf = xqx($cmd, { GIT_DIR => $dir }, { 0 => $fh });
113         is(0, $?, 'hashed object successfully');
114         chomp $buf;
116         my $gcf = PublicInbox::Git->new($dir);
117         my @x = $gcf->cat_file($buf);
118         is($x[2], 'blob', 'got blob on wantarray');
119         is($x[3], $size, 'got correct size ref on big file');
120         is(length(${$x[0]}), $size, 'read correct number of bytes');
122         my $ref = $gcf->qx(qw(cat-file blob), $buf);
123         is($?, 0, 'no error on scalar success');
124         my @ref = $gcf->qx(qw(cat-file blob), $buf);
125         is($?, 0, 'no error on wantarray success');
126         my $nl = scalar @ref;
127         ok($nl > 1, "qx returned array length of $nl");
128         is(join('', @ref), $ref, 'qx array and scalar context both work');
130         $gcf->qx(qw(repack -adq));
131         ok($gcf->packed_bytes > 0, 'packed size is positive');
132         my $rdr;
133         open $rdr->{2}, '+>', '/dev/null' or xbail "open $!";
134         $gcf->qx([qw(rev-parse --verify bogus)], undef, $rdr);
135         isnt($?, 0, '$? set on failure: '.$?);
138 SKIP: {
139         require_git(2.6, 7);
140         my ($alt, $alt_obj) = tmpdir();
141         my $hash_obj = [ 'git', "--git-dir=$alt", qw(hash-object -w --stdin) ];
142         PublicInbox::Import::init_bare($alt);
143         open my $fh, '<', "$alt/config" or die "open failed: $!\n";
144         chomp(my $remote = xqx($hash_obj, undef, { 0 => $fh }));
145         my $gcf = PublicInbox::Git->new($dir);
146         is($gcf->cat_file($remote), undef, "remote file not found");
147         open $fh, '>>', "$dir/objects/info/alternates" or
148                         die "open failed: $!\n";
149         print $fh "$alt/objects\n" or die "print failed: $!\n";
150         close $fh or die "close failed: $!";
151         my $found = $gcf->cat_file($remote);
152         open $fh, '<', "$alt/config" or die "open failed: $!\n";
153         my $config = eval { local $/; <$fh> };
154         is($$found, $config, 'alternates reloaded');
156         # with the async interface
157         my ($async_alt, $async_dir_obj) = tmpdir();
158         PublicInbox::Import::init_bare($async_alt);
159         my @exist = map { chomp; [ split / / ] } (xqx(['git', "--git-dir=$dir",
160                         qw(cat-file --batch-all-objects --batch-check)]));
161         my $results = [];
162         my $cb = sub {
163                 my ($bref, $oid, $type, $size) = @_;
164                 push @$results, [ $oid, $type, $size ];
165         };
166         for my $i (0..5) {
167                 $gcf->cat_async($exist[$i]->[0], $cb, $results);
168                 next if $i != 3;
170                 # stick a new alternate into a running async pipeline
171                 $hash_obj->[1] = "--git-dir=$async_alt";
172                 $remote = xqx($hash_obj, undef, { 0 => \'async' });
173                 chomp $remote;
174                 open $fh, '>>', "$dir/objects/info/alternates" or
175                                 die "open failed: $!\n";
176                 print $fh "$async_alt/objects\n" or die "print failed: $!\n";
177                 close $fh or die "close failed: $!";
178                 # trigger cat_async_retry:
179                 $gcf->cat_async($remote, $cb, $results);
180         }
181         $gcf->async_wait_all;
182         my $expect = [ @exist[0..3], [ $remote, 'blob', 5 ], @exist[4..5] ];
183         is_deeply($results, $expect, 'got expected results');
185         ok(!$gcf->cleanup, 'cleanup can expire');
186         ok(!$gcf->cleanup, 'cleanup idempotent');
188         my $t = $gcf->modified;
189         ok($t <= time, 'repo not modified in the future');
190         isnt($t, 0, 'repo not modified in 1970')
193 use_ok 'PublicInbox::Git', qw(git_unquote git_quote);
194 my $s;
195 is("foo\nbar", git_unquote($s = '"foo\\nbar"'), 'unquoted newline');
196 is("Eléanor", git_unquote($s = '"El\\303\\251anor"'), 'unquoted octal');
197 is(git_unquote($s = '"I\"m"'), 'I"m', 'unquoted dq');
198 is(git_unquote($s = '"I\\m"'), 'I\\m', 'unquoted backslash');
200 is(git_quote($s = "Eléanor"), '"El\\303\\251anor"', 'quoted octal');
201 is(git_quote($s = "hello\"world"), '"hello\"world"', 'quoted dq');
202 is(git_quote($s = "hello\\world"), '"hello\\\\world"', 'quoted backslash');
203 is(git_quote($s = "hello\nworld"), '"hello\\nworld"', 'quoted LF');
204 is(git_quote($s = "hello\x06world"), '"hello\\006world"', 'quoted \\x06');
205 is(git_unquote($s = '"hello\\006world"'), "hello\x06world", 'unquoted \\x06');
207 diag 'git_version='.sprintf('%vd', PublicInbox::Git::git_version());
208 done_testing;