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