]> Sergey Matveev's repositories - public-inbox.git/blob - t/git.t
0936ac5ea8d1420641afd00c4586cd95ceaddc0a
[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         ok(-r $fi_data, "fast-import data readable (or run test at top level)");
17         local $ENV{GIT_DIR} = $dir;
18         system("git fast-import --quiet <$fi_data");
19         is($?, 0, 'fast-import succeeded');
20 }
21
22 {
23         my $gcf = PublicInbox::Git->new($dir);
24         is($gcf->modified, 749520000, 'modified time detected from commit');
25         my $f = 'HEAD:foo.txt';
26         my @x = $gcf->check($f);
27         is(scalar @x, 3, 'returned 3 element array for existing file');
28         like($x[0], qr/\A[a-f0-9]{40}\z/, 'returns obj ID in 1st element');
29         is('blob', $x[1], 'returns obj type in 2nd element');
30         like($x[2], qr/\A\d+\z/, 'returns obj size in 3rd element');
31
32         my $raw = $gcf->cat_file($f);
33         is($x[2], length($$raw), 'length matches');
34
35         is(${$gcf->cat_file($f)}, $$raw, 'not broken after failures');
36         is(${$gcf->cat_file($f)}, $$raw, 'not broken after partial read');
37
38         my $oid = $x[0];
39         my $arg = { 'foo' => 'bar' };
40         my $res = [];
41         my $missing = [];
42         $gcf->cat_async_begin;
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, undef, undef, undef, $arg],
57                 'non-existent blob gives expected result');
58 }
59
60 if (1) {
61         my $cmd = [ 'git', "--git-dir=$dir", qw(hash-object -w --stdin) ];
62
63         # need a big file, use the AGPL-3.0 :p
64         my $big_data = './COPYING';
65         ok(-r $big_data, 'COPYING readable');
66         my $size = -s $big_data;
67         ok($size > 8192, 'file is big enough');
68
69         my $buf = do {
70                 local $ENV{GIT_DIR} = $dir;
71                 `git hash-object -w --stdin <$big_data`;
72         };
73         is(0, $?, 'hashed object successfully');
74         chomp $buf;
75
76         my $gcf = PublicInbox::Git->new($dir);
77         my $rsize;
78         my $x = $gcf->cat_file($buf, \$rsize);
79         is($rsize, $size, 'got correct size ref on big file');
80         is(length($$x), $size, 'read correct number of bytes');
81
82         my $ref = $gcf->qx(qw(cat-file blob), $buf);
83         my @ref = $gcf->qx(qw(cat-file blob), $buf);
84         my $nl = scalar @ref;
85         ok($nl > 1, "qx returned array length of $nl");
86
87         $gcf->qx(qw(repack -adq));
88         ok($gcf->packed_bytes > 0, 'packed size is positive');
89 }
90
91 if ('alternates reloaded') {
92         my ($alt, $alt_obj) = tmpdir();
93         my @cmd = ('git', "--git-dir=$alt", qw(hash-object -w --stdin));
94         PublicInbox::Import::init_bare($alt);
95         open my $fh, '<', "$alt/config" or die "open failed: $!\n";
96         my $rd = popen_rd(\@cmd, {}, { 0 => $fh } );
97         close $fh or die "close failed: $!";
98         chomp(my $remote = <$rd>);
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" 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         ok(!$gcf->cleanup, 'cleanup can expire');
111         ok(!$gcf->cleanup, 'cleanup idempotent');
112
113         my $t = $gcf->modified;
114         ok($t <= time, 'repo not modified in the future');
115         isnt($t, 0, 'repo not modified in 1970')
116 }
117
118 use_ok 'PublicInbox::Git', qw(git_unquote git_quote);
119 my $s;
120 is("foo\nbar", git_unquote($s = '"foo\\nbar"'), 'unquoted newline');
121 is("Eléanor", git_unquote($s = '"El\\303\\251anor"'), 'unquoted octal');
122 is(git_unquote($s = '"I\"m"'), 'I"m', 'unquoted dq');
123 is(git_unquote($s = '"I\\m"'), 'I\\m', 'unquoted backslash');
124
125 is(git_quote($s = "Eléanor"), '"El\\303\\251anor"', 'quoted octal');
126 is(git_quote($s = "hello\"world"), '"hello\"world"', 'quoted dq');
127 is(git_quote($s = "hello\\world"), '"hello\\\\world"', 'quoted backslash');
128 is(git_quote($s = "hello\nworld"), '"hello\\nworld"', 'quoted LF');
129
130 done_testing();