1 # Copyright (C) 2015-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 use PublicInbox::TestCommon;
6 my ($dir, $for_destroy) = tmpdir();
7 use PublicInbox::Import;
8 use POSIX qw(strftime);
12 PublicInbox::Import::init_bare($dir);
13 my $fi_data = './t/git.fast-import-data';
14 open my $fh, '<', $fi_data or die
15 "fast-import data readable (or run test at top level: $!";
16 my $rdr = { 0 => $fh };
17 xsys([qw(git fast-import --quiet)], { GIT_DIR => $dir }, $rdr);
18 is($?, 0, 'fast-import succeeded');
21 my $git = PublicInbox::Git->new($dir);
22 my @s = $git->date_parse('1970-01-01T00:00:00Z');
23 is($s[0], 0, 'parsed epoch');
24 local $ENV{TZ} = 'UTC';
25 @s = $git->date_parse('1993-10-02 01:02:09', '2010-10-02 01:03:04');
26 is(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($s[0])),
27 '1993-10-02T01:02:09Z', 'round trips');
28 is(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($s[1])),
29 '2010-10-02T01:03:04Z', '2nd arg round trips');
30 @s = $git->date_parse('1993-10-02');
31 is(strftime('%Y-%m-%d', gmtime($s[0])), '1993-10-02',
32 'round trips date-only');
36 my $gcf = PublicInbox::Git->new($dir);
37 is($gcf->modified, 749520000, 'modified time detected from commit');
38 my $f = 'HEAD:foo.txt';
39 my @x = $gcf->check($f);
40 is(scalar @x, 3, 'returned 3 element array for existing file');
41 like($x[0], qr/\A[a-f0-9]{40}\z/, 'returns obj ID in 1st element');
42 is('blob', $x[1], 'returns obj type in 2nd element');
43 like($x[2], qr/\A\d+\z/, 'returns obj size in 3rd element');
45 my $raw = $gcf->cat_file($f);
46 is($x[2], length($$raw), 'length matches');
48 is(${$gcf->cat_file($f)}, $$raw, 'not broken after failures');
49 is(${$gcf->cat_file($f)}, $$raw, 'not broken after partial read');
52 my $arg = { 'foo' => 'bar' };
55 $gcf->cat_async($oid, sub {
56 my ($bref, $oid_hex, $type, $size, $arg) = @_;
59 $gcf->cat_async('non-existent', sub {
60 my ($bref, $oid_hex, $type, $size, $arg) = @_;
64 my ($bref, $oid_hex, $type, $size, $arg_res) = @$res;
65 is_deeply([$oid_hex, $type, $size], \@x, 'got expected header');
66 is($arg_res, $arg, 'arg passed to cat_async');
67 is_deeply($raw, $bref, 'blob result matches');
68 is_deeply($missing, [ undef, 'non-existent', 'missing', undef, $arg],
69 'non-existent blob gives expected result');
72 $gcf->cat_async($oid, sub { push @$res, \@_ });
73 $gcf->cat_async($oid, sub { die 'HI' });
74 $gcf->cat_async($oid, sub { push @$res, \@_ });
75 eval { $gcf->async_wait_all };
76 like($@, qr/\bHI\b/, 'die in callback propagates');
77 is(scalar(@$res), 2, 'two results');
78 is_deeply($res->[0], [ $raw, @x, undef ], '1st cb result');
79 is_deeply($res->[1], [ undef, $oid, undef, undef, undef ],
83 local $PublicInbox::Git::async_warn = 1;
84 local $SIG{__WARN__} = sub { push @w, @_ };
86 $gcf->cat_async($oid, sub { push @$res, \@_ });
87 $gcf->cat_async($oid, sub { die 'HI' });
88 $gcf->cat_async($oid, sub { push @$res, \@_ });
89 eval { $gcf->async_wait_all };
90 is(scalar(@$res), 2, 'two results');
91 is_deeply($res->[0], [ $raw, @x, undef ], '1st cb result');
92 is_deeply($res->[1], [ $raw, @x, undef ], '2st cb result');
93 like("@w", qr/\bHI\b/, 'die turned to warning');
97 # need a big file, use the AGPL-3.0 :p
98 my $big_data = './COPYING';
99 ok(-r $big_data, 'COPYING readable');
100 my $size = -s $big_data;
101 ok($size > 8192, 'file is big enough');
102 open my $fh, '<', $big_data or die;
103 my $cmd = [ 'git', "--git-dir=$dir", qw(hash-object -w --stdin) ];
104 my $buf = xqx($cmd, { GIT_DIR => $dir }, { 0 => $fh });
105 is(0, $?, 'hashed object successfully');
108 my $gcf = PublicInbox::Git->new($dir);
109 my @x = $gcf->cat_file($buf);
110 is($x[2], 'blob', 'got blob on wantarray');
111 is($x[3], $size, 'got correct size ref on big file');
112 is(length(${$x[0]}), $size, 'read correct number of bytes');
114 my $ref = $gcf->qx(qw(cat-file blob), $buf);
115 is($?, 0, 'no error on scalar success');
116 my @ref = $gcf->qx(qw(cat-file blob), $buf);
117 is($?, 0, 'no error on wantarray success');
118 my $nl = scalar @ref;
119 ok($nl > 1, "qx returned array length of $nl");
120 is(join('', @ref), $ref, 'qx array and scalar context both work');
122 $gcf->qx(qw(repack -adq));
123 ok($gcf->packed_bytes > 0, 'packed size is positive');
125 open $rdr->{2}, '+>', '/dev/null' or xbail "open $!";
126 $gcf->qx([qw(rev-parse --verify bogus)], undef, $rdr);
127 isnt($?, 0, '$? set on failure: '.$?);
131 require_git(2.6, 7) or skip('need git 2.6+ for --batch-all-objects', 7);
132 my ($alt, $alt_obj) = tmpdir();
133 my $hash_obj = [ 'git', "--git-dir=$alt", qw(hash-object -w --stdin) ];
134 PublicInbox::Import::init_bare($alt);
135 open my $fh, '<', "$alt/config" or die "open failed: $!\n";
136 chomp(my $remote = xqx($hash_obj, undef, { 0 => $fh }));
137 my $gcf = PublicInbox::Git->new($dir);
138 is($gcf->cat_file($remote), undef, "remote file not found");
139 open $fh, '>>', "$dir/objects/info/alternates" or
140 die "open failed: $!\n";
141 print $fh "$alt/objects\n" or die "print failed: $!\n";
142 close $fh or die "close failed: $!";
143 my $found = $gcf->cat_file($remote);
144 open $fh, '<', "$alt/config" or die "open failed: $!\n";
145 my $config = eval { local $/; <$fh> };
146 is($$found, $config, 'alternates reloaded');
148 # with the async interface
149 my ($async_alt, $async_dir_obj) = tmpdir();
150 PublicInbox::Import::init_bare($async_alt);
151 my @exist = map { chomp; [ split / / ] } (xqx(['git', "--git-dir=$dir",
152 qw(cat-file --batch-all-objects --batch-check)]));
155 my ($bref, $oid, $type, $size) = @_;
156 push @$results, [ $oid, $type, $size ];
159 $gcf->cat_async($exist[$i]->[0], $cb, $results);
162 # stick a new alternate into a running async pipeline
163 $hash_obj->[1] = "--git-dir=$async_alt";
164 $remote = xqx($hash_obj, undef, { 0 => \'async' });
166 open $fh, '>>', "$dir/objects/info/alternates" or
167 die "open failed: $!\n";
168 print $fh "$async_alt/objects\n" or die "print failed: $!\n";
169 close $fh or die "close failed: $!";
170 # trigger cat_async_retry:
171 $gcf->cat_async($remote, $cb, $results);
173 $gcf->async_wait_all;
174 my $expect = [ @exist[0..3], [ $remote, 'blob', 5 ], @exist[4..5] ];
175 is_deeply($results, $expect, 'got expected results');
177 ok(!$gcf->cleanup, 'cleanup can expire');
178 ok(!$gcf->cleanup, 'cleanup idempotent');
180 my $t = $gcf->modified;
181 ok($t <= time, 'repo not modified in the future');
182 isnt($t, 0, 'repo not modified in 1970')
185 use_ok 'PublicInbox::Git', qw(git_unquote git_quote);
187 is("foo\nbar", git_unquote($s = '"foo\\nbar"'), 'unquoted newline');
188 is("Eléanor", git_unquote($s = '"El\\303\\251anor"'), 'unquoted octal');
189 is(git_unquote($s = '"I\"m"'), 'I"m', 'unquoted dq');
190 is(git_unquote($s = '"I\\m"'), 'I\\m', 'unquoted backslash');
192 is(git_quote($s = "Eléanor"), '"El\\303\\251anor"', 'quoted octal');
193 is(git_quote($s = "hello\"world"), '"hello\"world"', 'quoted dq');
194 is(git_quote($s = "hello\\world"), '"hello\\\\world"', 'quoted backslash');
195 is(git_quote($s = "hello\nworld"), '"hello\\nworld"', 'quoted LF');
196 is(git_quote($s = "hello\x06world"), '"hello\\006world"', 'quoted \\x06');
197 is(git_unquote($s = '"hello\\006world"'), "hello\x06world", 'unquoted \\x06');