]> Sergey Matveev's repositories - public-inbox.git/blob - t/git.t
git: calculate modified time of repository
[public-inbox.git] / t / git.t
1 # Copyright (C) 2015-2018 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 File::Temp qw/tempdir/;
7 my $dir = tempdir('pi-git-XXXXXX', TMPDIR => 1, CLEANUP => 1);
8 use Cwd qw/getcwd/;
9 use PublicInbox::Spawn qw(popen_rd);
10
11 eval { require IPC::Run } or plan skip_all => 'IPC::Run missing';
12 use_ok 'PublicInbox::Git';
13
14 {
15         is(system(qw(git init -q --bare), $dir), 0, 'created git directory');
16         my $cmd = [ 'git', "--git-dir=$dir", 'fast-import', '--quiet' ];
17
18         my $fi_data = getcwd().'/t/git.fast-import-data';
19         ok(-r $fi_data, "fast-import data readable (or run test at top level)");
20         IPC::Run::run($cmd, '<', $fi_data);
21         is($?, 0, 'fast-import succeeded');
22 }
23
24 {
25         my $gcf = PublicInbox::Git->new($dir);
26         is($gcf->modified, 749520000, 'modified time detected from commit');
27         my $f = 'HEAD:foo.txt';
28         my @x = $gcf->check($f);
29         is(scalar @x, 3, 'returned 3 element array for existing file');
30         like($x[0], qr/\A[a-f0-9]{40}\z/, 'returns obj ID in 1st element');
31         is('blob', $x[1], 'returns obj type in 2nd element');
32         like($x[2], qr/\A\d+\z/, 'returns obj size in 3rd element');
33
34         my $raw = $gcf->cat_file($f);
35         is($x[2], length($$raw), 'length matches');
36
37         {
38                 my $size;
39                 my $rv = $gcf->cat_file($f, sub {
40                         my ($in, $left) = @_;
41                         $size = $$left;
42                         'nothing'
43                 });
44                 is($rv, 'nothing', 'returned from callback without reading');
45                 is($size, $x[2], 'set size for callback correctly');
46         }
47
48         eval { $gcf->cat_file($f, sub { die 'OMG' }) };
49         like($@, qr/\bOMG\b/, 'died in callback propagated');
50         is(${$gcf->cat_file($f)}, $$raw, 'not broken after failures');
51
52         {
53                 my ($buf, $r);
54                 my $rv = $gcf->cat_file($f, sub {
55                         my ($in, $left) = @_;
56                         $r = read($in, $buf, 2);
57                         $$left -= $r;
58                         'blah'
59                 });
60                 is($r, 2, 'only read 2 bytes');
61                 is($buf, '--', 'partial read succeeded');
62                 is($rv, 'blah', 'return value propagated');
63         }
64         is(${$gcf->cat_file($f)}, $$raw, 'not broken after partial read');
65 }
66
67 if (1) {
68         my $cmd = [ 'git', "--git-dir=$dir", qw(hash-object -w --stdin) ];
69
70         # need a big file, use the AGPL-3.0 :p
71         my $big_data = getcwd().'/COPYING';
72         ok(-r $big_data, 'COPYING readable');
73         my $size = -s $big_data;
74         ok($size > 8192, 'file is big enough');
75
76         my $buf = '';
77         IPC::Run::run($cmd, '<', $big_data, '>', \$buf);
78         is(0, $?, 'hashed object successfully');
79         chomp $buf;
80
81         my $gcf = PublicInbox::Git->new($dir);
82         my $rsize;
83         is($gcf->cat_file($buf, sub {
84                 $rsize = ${$_[1]};
85                 'x';
86         }), 'x', 'checked input');
87         is($rsize, $size, 'got correct size on big file');
88
89         my $x = $gcf->cat_file($buf, \$rsize);
90         is($rsize, $size, 'got correct size ref on big file');
91         is(length($$x), $size, 'read correct number of bytes');
92
93         my $rline;
94         $gcf->cat_file($buf, sub {
95                 my ($in, $left) = @_;
96                 $rline = <$in>;
97                 $$left -= length($rline);
98         });
99         {
100                 open my $fh, '<', $big_data or die "open failed: $!\n";
101                 is($rline, <$fh>, 'first line matches');
102         };
103
104         my $all;
105         $gcf->cat_file($buf, sub {
106                 my ($in, $left) = @_;
107                 my $x = read($in, $all, $$left);
108                 $$left -= $x;
109         });
110         {
111                 open my $fh, '<', $big_data or die "open failed: $!\n";
112                 local $/;
113                 is($all, <$fh>, 'entire read matches');
114         };
115
116         my $ref = $gcf->qx(qw(cat-file blob), $buf);
117         is($all, $ref, 'qx read giant single string');
118
119         my @ref = $gcf->qx(qw(cat-file blob), $buf);
120         is($all, join('', @ref), 'qx returned array when wanted');
121         my $nl = scalar @ref;
122         ok($nl > 1, "qx returned array length of $nl");
123
124         $gcf->qx(qw(repack -adq));
125         ok($gcf->packed_bytes > 0, 'packed size is positive');
126 }
127
128 if ('alternates reloaded') {
129         my $alt = tempdir('pi-git-XXXXXX', TMPDIR => 1, CLEANUP => 1);
130         my @cmd = ('git', "--git-dir=$alt", qw(hash-object -w --stdin));
131         is(system(qw(git init -q --bare), $alt), 0, 'create alt directory');
132         open my $fh, '<', "$alt/config" or die "open failed: $!\n";
133         my $rd = popen_rd(\@cmd, {}, { 0 => fileno($fh) } );
134         close $fh or die "close failed: $!";
135         chomp(my $remote = <$rd>);
136         my $gcf = PublicInbox::Git->new($dir);
137         is($gcf->cat_file($remote), undef, "remote file not found");
138         open $fh, '>>', "$dir/objects/info/alternates" or
139                         die "open failed: $!\n";
140         print $fh "$alt/objects" or die "print failed: $!\n";
141         close $fh or die "close failed: $!";
142         my $found = $gcf->cat_file($remote);
143         open $fh, '<', "$alt/config" or die "open failed: $!\n";
144         my $config = eval { local $/; <$fh> };
145         is($$found, $config, 'alternates reloaded');
146
147         ok($gcf->cleanup(time - 30), 'cleanup did not expire');
148         ok(!$gcf->cleanup(time + 30), 'cleanup can expire');
149         ok(!$gcf->cleanup, 'cleanup idempotent');
150
151         my $t = $gcf->modified;
152         ok($t <= time, 'repo not modified in the future');
153         isnt($t, 0, 'repo not modified in 1970')
154 }
155
156 use_ok 'PublicInbox::Git', qw(git_unquote git_quote);
157 my $s;
158 is("foo\nbar", git_unquote($s = '"foo\\nbar"'), 'unquoted newline');
159 is("Eléanor", git_unquote($s = '"El\\303\\251anor"'), 'unquoted octal');
160 is(git_unquote($s = '"I\"m"'), 'I"m', 'unquoted dq');
161 is(git_unquote($s = '"I\\m"'), 'I\\m', 'unquoted backslash');
162
163 is(git_quote($s = "Eléanor"), '"El\\303\\251anor"', 'quoted octal');
164 is(git_quote($s = "hello\"world"), '"hello\"world"', 'quoted dq');
165 is(git_quote($s = "hello\\world"), '"hello\\\\world"', 'quoted backslash');
166 is(git_quote($s = "hello\nworld"), '"hello\\nworld"', 'quoted LF');
167
168 done_testing();