1 # Copyright (C) 2015 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(CLEANUP => 1);
10 use_ok 'PublicInbox::Git';
12 is(system(qw(git init -q --bare), $dir), 0, 'created git directory');
13 my @cmd = ('git', "--git-dir=$dir", 'fast-import', '--quiet');
15 my $fi_data = getcwd().'/t/git.fast-import-data';
16 ok(-r $fi_data, "fast-import data readable (or run test at top level)");
18 defined $pid or die "fork failed: $!\n";
20 open STDIN, '<', $fi_data or die "open $fi_data: $!\n";
22 die "failed exec: ",join(' ', @cmd),": $!\n";
25 is($?, 0, 'fast-import succeeded');
29 my $gcf = PublicInbox::Git->new($dir);
30 my $f = 'HEAD:foo.txt';
31 my @x = $gcf->check($f);
32 is(scalar @x, 3, 'returned 3 element array for existing file');
33 like($x[0], qr/\A[a-f0-9]{40}\z/, 'returns obj ID in 1st element');
34 is('blob', $x[1], 'returns obj type in 2nd element');
35 like($x[2], qr/\A\d+\z/, 'returns obj size in 3rd element');
37 my $raw = $gcf->cat_file($f);
38 is($x[2], length($$raw), 'length matches');
42 my $rv = $gcf->cat_file($f, sub {
47 is($rv, 'nothing', 'returned from callback without reading');
48 is($size, $x[2], 'set size for callback correctly');
51 eval { $gcf->cat_file($f, sub { die 'OMG' }) };
52 like($@, qr/\bOMG\b/, 'died in callback propagated');
53 is(${$gcf->cat_file($f)}, $$raw, 'not broken after failures');
57 my $rv = $gcf->cat_file($f, sub {
59 $r = read($in, $buf, 2);
63 is($r, 2, 'only read 2 bytes');
64 is($buf, '--', 'partial read succeeded');
65 is($rv, 'blah', 'return value propagated');
67 is(${$gcf->cat_file($f)}, $$raw, 'not broken after partial read');
72 my @cmd = ('git', "--git-dir=$dir", qw(hash-object -w --stdin));
74 # need a big file, use the AGPL-3.0 :p
75 my $big_data = getcwd().'/COPYING';
76 ok(-r $big_data, 'COPYING readable');
77 my $size = -s $big_data;
78 ok($size > 8192, 'file is big enough');
81 ok(pipe($r, $w), 'created pipe');
84 defined $pid or die "fork failed: $!\n";
87 open STDIN, '<', $big_data or die "open $big_data: $!\n";
90 die "failed exec: ",join(' ', @cmd),": $!\n";
93 my $n = read $r, my $buf, 41;
95 is(0, $?, 'hashed object successfully');
98 my $gcf = PublicInbox::Git->new($dir);
100 is($gcf->cat_file($buf, sub {
103 }), 'x', 'checked input');
104 is($rsize, $size, 'got correct size on big file');
106 my $x = $gcf->cat_file($buf, \$rsize);
107 is($rsize, $size, 'got correct size ref on big file');
108 is(length($$x), $size, 'read correct number of bytes');
111 $gcf->cat_file($buf, sub {
112 my ($in, $left) = @_;
114 $$left -= length($rline);
117 open my $fh, '<', $big_data or die "open failed: $!\n";
118 is($rline, <$fh>, 'first line matches');
122 $gcf->cat_file($buf, sub {
123 my ($in, $left) = @_;
124 my $x = read($in, $all, $$left);
128 open my $fh, '<', $big_data or die "open failed: $!\n";
130 is($all, <$fh>, 'entire read matches');