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 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');
33 my $raw = $gcf->cat_file($f);
34 is($x[2], length($$raw), 'length matches');
38 my $rv = $gcf->cat_file($f, sub {
43 is($rv, 'nothing', 'returned from callback without reading');
44 is($size, $x[2], 'set size for callback correctly');
47 eval { $gcf->cat_file($f, sub { die 'OMG' }) };
48 like($@, qr/\bOMG\b/, 'died in callback propagated');
49 is(${$gcf->cat_file($f)}, $$raw, 'not broken after failures');
53 my $rv = $gcf->cat_file($f, sub {
55 $r = read($in, $buf, 2);
59 is($r, 2, 'only read 2 bytes');
60 is($buf, '--', 'partial read succeeded');
61 is($rv, 'blah', 'return value propagated');
63 is(${$gcf->cat_file($f)}, $$raw, 'not broken after partial read');
67 my $cmd = [ 'git', "--git-dir=$dir", qw(hash-object -w --stdin) ];
69 # need a big file, use the AGPL-3.0 :p
70 my $big_data = getcwd().'/COPYING';
71 ok(-r $big_data, 'COPYING readable');
72 my $size = -s $big_data;
73 ok($size > 8192, 'file is big enough');
76 IPC::Run::run($cmd, '<', $big_data, '>', \$buf);
77 is(0, $?, 'hashed object successfully');
80 my $gcf = PublicInbox::Git->new($dir);
82 is($gcf->cat_file($buf, sub {
85 }), 'x', 'checked input');
86 is($rsize, $size, 'got correct size on big file');
88 my $x = $gcf->cat_file($buf, \$rsize);
89 is($rsize, $size, 'got correct size ref on big file');
90 is(length($$x), $size, 'read correct number of bytes');
93 $gcf->cat_file($buf, sub {
96 $$left -= length($rline);
99 open my $fh, '<', $big_data or die "open failed: $!\n";
100 is($rline, <$fh>, 'first line matches');
104 $gcf->cat_file($buf, sub {
105 my ($in, $left) = @_;
106 my $x = read($in, $all, $$left);
110 open my $fh, '<', $big_data or die "open failed: $!\n";
112 is($all, <$fh>, 'entire read matches');
115 my $ref = $gcf->qx(qw(cat-file blob), $buf);
116 is($all, $ref, 'qx read giant single string');
118 my @ref = $gcf->qx(qw(cat-file blob), $buf);
119 is($all, join('', @ref), 'qx returned array when wanted');
120 my $nl = scalar @ref;
121 ok($nl > 1, "qx returned array length of $nl");
123 $gcf->qx(qw(repack -adq));
124 ok($gcf->packed_bytes > 0, 'packed size is positive');
127 if ('alternates reloaded') {
128 my $alt = tempdir('pi-git-XXXXXX', TMPDIR => 1, CLEANUP => 1);
129 my @cmd = ('git', "--git-dir=$alt", qw(hash-object -w --stdin));
130 is(system(qw(git init -q --bare), $alt), 0, 'create alt directory');
131 open my $fh, '<', "$alt/config" or die "open failed: $!\n";
132 my $rd = popen_rd(\@cmd, {}, { 0 => fileno($fh) } );
133 close $fh or die "close failed: $!";
134 chomp(my $remote = <$rd>);
135 my $gcf = PublicInbox::Git->new($dir);
136 is($gcf->cat_file($remote), undef, "remote file not found");
137 open $fh, '>>', "$dir/objects/info/alternates" or
138 die "open failed: $!\n";
139 print $fh "$alt/objects" or die "print failed: $!\n";
140 close $fh or die "close failed: $!";
141 my $found = $gcf->cat_file($remote);
142 open $fh, '<', "$alt/config" or die "open failed: $!\n";
143 my $config = eval { local $/; <$fh> };
144 is($$found, $config, 'alternates reloaded');
147 use_ok 'PublicInbox::Git', qw(git_unquote git_quote);
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');
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');