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