1 # Copyright (C) 2015-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 require './t/common.perl';
7 my ($dir, $for_destroy) = tmpdir();
8 use PublicInbox::Spawn qw(popen_rd);
10 use_ok 'PublicInbox::Git';
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');
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');
31 my $raw = $gcf->cat_file($f);
32 is($x[2], length($$raw), 'length matches');
34 is(${$gcf->cat_file($f)}, $$raw, 'not broken after failures');
35 is(${$gcf->cat_file($f)}, $$raw, 'not broken after partial read');
39 my $cmd = [ 'git', "--git-dir=$dir", qw(hash-object -w --stdin) ];
41 # need a big file, use the AGPL-3.0 :p
42 my $big_data = './COPYING';
43 ok(-r $big_data, 'COPYING readable');
44 my $size = -s $big_data;
45 ok($size > 8192, 'file is big enough');
48 local $ENV{GIT_DIR} = $dir;
49 `git hash-object -w --stdin <$big_data`;
51 is(0, $?, 'hashed object successfully');
54 my $gcf = PublicInbox::Git->new($dir);
56 my $x = $gcf->cat_file($buf, \$rsize);
57 is($rsize, $size, 'got correct size ref on big file');
58 is(length($$x), $size, 'read correct number of bytes');
60 my $ref = $gcf->qx(qw(cat-file blob), $buf);
61 my @ref = $gcf->qx(qw(cat-file blob), $buf);
63 ok($nl > 1, "qx returned array length of $nl");
65 $gcf->qx(qw(repack -adq));
66 ok($gcf->packed_bytes > 0, 'packed size is positive');
69 if ('alternates reloaded') {
70 my ($alt, $alt_obj) = tmpdir();
71 my @cmd = ('git', "--git-dir=$alt", qw(hash-object -w --stdin));
72 is(system(qw(git init -q --bare), $alt), 0, 'create alt directory');
73 open my $fh, '<', "$alt/config" or die "open failed: $!\n";
74 my $rd = popen_rd(\@cmd, {}, { 0 => fileno($fh) } );
75 close $fh or die "close failed: $!";
76 chomp(my $remote = <$rd>);
77 my $gcf = PublicInbox::Git->new($dir);
78 is($gcf->cat_file($remote), undef, "remote file not found");
79 open $fh, '>>', "$dir/objects/info/alternates" or
80 die "open failed: $!\n";
81 print $fh "$alt/objects" or die "print failed: $!\n";
82 close $fh or die "close failed: $!";
83 my $found = $gcf->cat_file($remote);
84 open $fh, '<', "$alt/config" or die "open failed: $!\n";
85 my $config = eval { local $/; <$fh> };
86 is($$found, $config, 'alternates reloaded');
88 ok(!$gcf->cleanup, 'cleanup can expire');
89 ok(!$gcf->cleanup, 'cleanup idempotent');
91 my $t = $gcf->modified;
92 ok($t <= time, 'repo not modified in the future');
93 isnt($t, 0, 'repo not modified in 1970')
96 use_ok 'PublicInbox::Git', qw(git_unquote git_quote);
98 is("foo\nbar", git_unquote($s = '"foo\\nbar"'), 'unquoted newline');
99 is("Eléanor", git_unquote($s = '"El\\303\\251anor"'), 'unquoted octal');
100 is(git_unquote($s = '"I\"m"'), 'I"m', 'unquoted dq');
101 is(git_unquote($s = '"I\\m"'), 'I\\m', 'unquoted backslash');
103 is(git_quote($s = "Eléanor"), '"El\\303\\251anor"', 'quoted octal');
104 is(git_quote($s = "hello\"world"), '"hello\"world"', 'quoted dq');
105 is(git_quote($s = "hello\\world"), '"hello\\\\world"', 'quoted backslash');
106 is(git_quote($s = "hello\nworld"), '"hello\\nworld"', 'quoted LF');