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