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>
6 use File::Temp qw/tempdir/;
7 my $dir = tempdir('pi-git-XXXXXX', TMPDIR => 1, CLEANUP => 1);
9 use PublicInbox::Spawn qw(popen_rd);
11 eval { require IPC::Run } or plan skip_all => 'IPC::Run missing';
12 use_ok 'PublicInbox::Git';
15 is(system(qw(git init -q --bare), $dir), 0, 'created git directory');
16 my $cmd = [ 'git', "--git-dir=$dir", 'fast-import', '--quiet' ];
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');
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');
34 my $raw = $gcf->cat_file($f);
35 is($x[2], length($$raw), 'length matches');
39 my $rv = $gcf->cat_file($f, sub {
44 is($rv, 'nothing', 'returned from callback without reading');
45 is($size, $x[2], 'set size for callback correctly');
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');
54 my $rv = $gcf->cat_file($f, sub {
56 $r = read($in, $buf, 2);
60 is($r, 2, 'only read 2 bytes');
61 is($buf, '--', 'partial read succeeded');
62 is($rv, 'blah', 'return value propagated');
64 is(${$gcf->cat_file($f)}, $$raw, 'not broken after partial read');
68 my $cmd = [ 'git', "--git-dir=$dir", qw(hash-object -w --stdin) ];
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');
77 IPC::Run::run($cmd, '<', $big_data, '>', \$buf);
78 is(0, $?, 'hashed object successfully');
81 my $gcf = PublicInbox::Git->new($dir);
83 is($gcf->cat_file($buf, sub {
86 }), 'x', 'checked input');
87 is($rsize, $size, 'got correct size on big file');
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');
94 $gcf->cat_file($buf, sub {
97 $$left -= length($rline);
100 open my $fh, '<', $big_data or die "open failed: $!\n";
101 is($rline, <$fh>, 'first line matches');
105 $gcf->cat_file($buf, sub {
106 my ($in, $left) = @_;
107 my $x = read($in, $all, $$left);
111 open my $fh, '<', $big_data or die "open failed: $!\n";
113 is($all, <$fh>, 'entire read matches');
116 my $ref = $gcf->qx(qw(cat-file blob), $buf);
117 is($all, $ref, 'qx read giant single string');
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");
124 $gcf->qx(qw(repack -adq));
125 ok($gcf->packed_bytes > 0, 'packed size is positive');
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');
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');
151 my $t = $gcf->modified;
152 ok($t <= time, 'repo not modified in the future');
153 isnt($t, 0, 'repo not modified in 1970')
156 use_ok 'PublicInbox::Git', qw(git_unquote git_quote);
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');
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');