1 # Copyright (C) 2015-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
7 my ($dir, $for_destroy) = tmpdir();
8 use PublicInbox::Spawn qw(popen_rd);
9 use PublicInbox::Import;
11 use_ok 'PublicInbox::Git';
14 PublicInbox::Import::init_bare($dir);
15 my $fi_data = './t/git.fast-import-data';
16 open my $fh, '<', $fi_data or die
17 "fast-import data readable (or run test at top level: $!";
18 my $rdr = { 0 => $fh };
19 xsys([qw(git fast-import --quiet)], { GIT_DIR => $dir }, $rdr);
20 is($?, 0, 'fast-import succeeded');
24 my $gcf = PublicInbox::Git->new($dir);
25 is($gcf->modified, 749520000, 'modified time detected from commit');
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');
36 is(${$gcf->cat_file($f)}, $$raw, 'not broken after failures');
37 is(${$gcf->cat_file($f)}, $$raw, 'not broken after partial read');
40 my $arg = { 'foo' => 'bar' };
43 $gcf->cat_async($oid, sub {
44 my ($bref, $oid_hex, $type, $size, $arg) = @_;
47 $gcf->cat_async('non-existent', sub {
48 my ($bref, $oid_hex, $type, $size, $arg) = @_;
52 my ($bref, $oid_hex, $type, $size, $arg_res) = @$res;
53 is_deeply([$oid_hex, $type, $size], \@x, 'got expected header');
54 is($arg_res, $arg, 'arg passed to cat_async');
55 is_deeply($raw, $bref, 'blob result matches');
56 is_deeply($missing, [ undef, 'non-existent', 'missing', undef, $arg],
57 'non-existent blob gives expected result');
61 # need a big file, use the AGPL-3.0 :p
62 my $big_data = './COPYING';
63 ok(-r $big_data, 'COPYING readable');
64 my $size = -s $big_data;
65 ok($size > 8192, 'file is big enough');
66 open my $fh, '<', $big_data or die;
67 my $cmd = [ 'git', "--git-dir=$dir", qw(hash-object -w --stdin) ];
68 my $buf = xqx($cmd, { GIT_DIR => $dir }, { 0 => $fh });
69 is(0, $?, 'hashed object successfully');
72 my $gcf = PublicInbox::Git->new($dir);
74 my $x = $gcf->cat_file($buf, \$rsize);
75 is($rsize, $size, 'got correct size ref on big file');
76 is(length($$x), $size, 'read correct number of bytes');
78 my $ref = $gcf->qx(qw(cat-file blob), $buf);
79 my @ref = $gcf->qx(qw(cat-file blob), $buf);
81 ok($nl > 1, "qx returned array length of $nl");
83 $gcf->qx(qw(repack -adq));
84 ok($gcf->packed_bytes > 0, 'packed size is positive');
88 require_git(2.6, 7) or skip('need git 2.6+ for --batch-all-objects', 7);
89 my ($alt, $alt_obj) = tmpdir();
90 my $hash_obj = [ 'git', "--git-dir=$alt", qw(hash-object -w --stdin) ];
91 PublicInbox::Import::init_bare($alt);
92 open my $fh, '<', "$alt/config" or die "open failed: $!\n";
93 chomp(my $remote = xqx($hash_obj, undef, { 0 => $fh }));
94 my $gcf = PublicInbox::Git->new($dir);
95 is($gcf->cat_file($remote), undef, "remote file not found");
96 open $fh, '>>', "$dir/objects/info/alternates" or
97 die "open failed: $!\n";
98 print $fh "$alt/objects\n" or die "print failed: $!\n";
99 close $fh or die "close failed: $!";
100 my $found = $gcf->cat_file($remote);
101 open $fh, '<', "$alt/config" or die "open failed: $!\n";
102 my $config = eval { local $/; <$fh> };
103 is($$found, $config, 'alternates reloaded');
105 # with the async interface
106 my ($async_alt, $async_dir_obj) = tmpdir();
107 PublicInbox::Import::init_bare($async_alt);
108 my @exist = map { chomp; [ split / / ] } (xqx(['git', "--git-dir=$dir",
109 qw(cat-file --batch-all-objects --batch-check)]));
112 my ($bref, $oid, $type, $size) = @_;
113 push @$results, [ $oid, $type, $size ];
116 $gcf->cat_async($exist[$i]->[0], $cb, $results);
119 # stick a new alternate into a running async pipeline
120 $hash_obj->[1] = "--git-dir=$async_alt";
121 $remote = xqx($hash_obj, undef, { 0 => \'async' });
123 open $fh, '>>', "$dir/objects/info/alternates" or
124 die "open failed: $!\n";
125 print $fh "$async_alt/objects\n" or die "print failed: $!\n";
126 close $fh or die "close failed: $!";
127 # trigger cat_async_retry:
128 $gcf->cat_async($remote, $cb, $results);
130 $gcf->cat_async_wait;
131 my $expect = [ @exist[0..3], [ $remote, 'blob', 5 ], @exist[4..5] ];
132 is_deeply($results, $expect, 'got expected results');
134 ok(!$gcf->cleanup, 'cleanup can expire');
135 ok(!$gcf->cleanup, 'cleanup idempotent');
137 my $t = $gcf->modified;
138 ok($t <= time, 'repo not modified in the future');
139 isnt($t, 0, 'repo not modified in 1970')
142 use_ok 'PublicInbox::Git', qw(git_unquote git_quote);
144 is("foo\nbar", git_unquote($s = '"foo\\nbar"'), 'unquoted newline');
145 is("Eléanor", git_unquote($s = '"El\\303\\251anor"'), 'unquoted octal');
146 is(git_unquote($s = '"I\"m"'), 'I"m', 'unquoted dq');
147 is(git_unquote($s = '"I\\m"'), 'I\\m', 'unquoted backslash');
149 is(git_quote($s = "Eléanor"), '"El\\303\\251anor"', 'quoted octal');
150 is(git_quote($s = "hello\"world"), '"hello\"world"', 'quoted dq');
151 is(git_quote($s = "hello\\world"), '"hello\\\\world"', 'quoted backslash');
152 is(git_quote($s = "hello\nworld"), '"hello\\nworld"', 'quoted LF');