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, 'master');
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/foo.git");
22 my $nick = $git->local_nick; # internal sub
23 unlike($nick, qr/\.git\.git\z/, "no doubled `.git.git' suffix");
24 like($nick, qr/\.git\z/, "one `.git' suffix");
25 $git = PublicInbox::Git->new($dir);
26 $nick = $git->local_nick; # internal sub
27 like($nick, qr/\.git\z/, "local nick always adds `.git' suffix");
28 my @s = $git->date_parse('1970-01-01T00:00:00Z');
29 is($s[0], 0, 'parsed epoch');
30 local $ENV{TZ} = 'UTC';
31 @s = $git->date_parse('1993-10-02 01:02:09', '2010-10-02 01:03:04');
32 is(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($s[0])),
33 '1993-10-02T01:02:09Z', 'round trips');
34 is(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($s[1])),
35 '2010-10-02T01:03:04Z', '2nd arg round trips');
36 @s = $git->date_parse('1993-10-02');
37 is(strftime('%Y-%m-%d', gmtime($s[0])), '1993-10-02',
38 'round trips date-only');
42 my $gcf = PublicInbox::Git->new($dir);
43 is($gcf->modified, 749520000, 'modified time detected from commit');
44 my $f = 'HEAD:foo.txt';
45 my @x = $gcf->check($f);
46 is(scalar @x, 3, 'returned 3 element array for existing file');
47 like($x[0], qr/\A[a-f0-9]{40}\z/, 'returns obj ID in 1st element');
48 is('blob', $x[1], 'returns obj type in 2nd element');
49 like($x[2], qr/\A\d+\z/, 'returns obj size in 3rd element');
51 my $raw = $gcf->cat_file($f);
52 is($x[2], length($$raw), 'length matches');
54 is(${$gcf->cat_file($f)}, $$raw, 'not broken after failures');
55 is(${$gcf->cat_file($f)}, $$raw, 'not broken after partial read');
58 my $arg = { 'foo' => 'bar' };
61 $gcf->cat_async($oid, sub {
62 my ($bref, $oid_hex, $type, $size, $arg) = @_;
65 $gcf->cat_async('non-existent', sub {
66 my ($bref, $oid_hex, $type, $size, $arg) = @_;
70 my ($bref, $oid_hex, $type, $size, $arg_res) = @$res;
71 is_deeply([$oid_hex, $type, $size], \@x, 'got expected header');
72 is($arg_res, $arg, 'arg passed to cat_async');
73 is_deeply($raw, $bref, 'blob result matches');
74 is_deeply($missing, [ undef, 'non-existent', 'missing', undef, $arg],
75 'non-existent blob gives expected result');
78 $gcf->cat_async($oid, sub { push @$res, \@_ });
79 $gcf->cat_async($oid, sub { die 'HI' });
80 $gcf->cat_async($oid, sub { push @$res, \@_ });
81 eval { $gcf->async_wait_all };
82 like($@, qr/\bHI\b/, 'die in callback propagates');
83 is(scalar(@$res), 2, 'two results');
84 is_deeply($res->[0], [ $raw, @x, undef ], '1st cb result');
85 is_deeply($res->[1], [ undef, $oid, undef, undef, undef ],
89 local $PublicInbox::Git::async_warn = 1;
90 local $SIG{__WARN__} = sub { push @w, @_ };
92 $gcf->cat_async($oid, sub { push @$res, \@_ });
93 $gcf->cat_async($oid, sub { die 'HI' });
94 $gcf->cat_async($oid, sub { push @$res, \@_ });
95 eval { $gcf->async_wait_all };
96 is(scalar(@$res), 2, 'two results');
97 is_deeply($res->[0], [ $raw, @x, undef ], '1st cb result');
98 is_deeply($res->[1], [ $raw, @x, undef ], '2st cb result');
99 like("@w", qr/\bHI\b/, 'die turned to warning');
103 # need a big file, use the AGPL-3.0 :p
104 my $big_data = './COPYING';
105 ok(-r $big_data, 'COPYING readable');
106 my $size = -s $big_data;
107 ok($size > 8192, 'file is big enough');
108 open my $fh, '<', $big_data or die;
109 my $cmd = [ 'git', "--git-dir=$dir", qw(hash-object -w --stdin) ];
110 my $buf = xqx($cmd, { GIT_DIR => $dir }, { 0 => $fh });
111 is(0, $?, 'hashed object successfully');
114 my $gcf = PublicInbox::Git->new($dir);
115 my @x = $gcf->cat_file($buf);
116 is($x[2], 'blob', 'got blob on wantarray');
117 is($x[3], $size, 'got correct size ref on big file');
118 is(length(${$x[0]}), $size, 'read correct number of bytes');
120 my $ref = $gcf->qx(qw(cat-file blob), $buf);
121 is($?, 0, 'no error on scalar success');
122 my @ref = $gcf->qx(qw(cat-file blob), $buf);
123 is($?, 0, 'no error on wantarray success');
124 my $nl = scalar @ref;
125 ok($nl > 1, "qx returned array length of $nl");
126 is(join('', @ref), $ref, 'qx array and scalar context both work');
128 $gcf->qx(qw(repack -adq));
129 ok($gcf->packed_bytes > 0, 'packed size is positive');
131 open $rdr->{2}, '+>', '/dev/null' or xbail "open $!";
132 $gcf->qx([qw(rev-parse --verify bogus)], undef, $rdr);
133 isnt($?, 0, '$? set on failure: '.$?);
137 require_git(2.6, 7) or skip('need git 2.6+ for --batch-all-objects', 7);
138 my ($alt, $alt_obj) = tmpdir();
139 my $hash_obj = [ 'git', "--git-dir=$alt", qw(hash-object -w --stdin) ];
140 PublicInbox::Import::init_bare($alt);
141 open my $fh, '<', "$alt/config" or die "open failed: $!\n";
142 chomp(my $remote = xqx($hash_obj, undef, { 0 => $fh }));
143 my $gcf = PublicInbox::Git->new($dir);
144 is($gcf->cat_file($remote), undef, "remote file not found");
145 open $fh, '>>', "$dir/objects/info/alternates" or
146 die "open failed: $!\n";
147 print $fh "$alt/objects\n" or die "print failed: $!\n";
148 close $fh or die "close failed: $!";
149 my $found = $gcf->cat_file($remote);
150 open $fh, '<', "$alt/config" or die "open failed: $!\n";
151 my $config = eval { local $/; <$fh> };
152 is($$found, $config, 'alternates reloaded');
154 # with the async interface
155 my ($async_alt, $async_dir_obj) = tmpdir();
156 PublicInbox::Import::init_bare($async_alt);
157 my @exist = map { chomp; [ split / / ] } (xqx(['git', "--git-dir=$dir",
158 qw(cat-file --batch-all-objects --batch-check)]));
161 my ($bref, $oid, $type, $size) = @_;
162 push @$results, [ $oid, $type, $size ];
165 $gcf->cat_async($exist[$i]->[0], $cb, $results);
168 # stick a new alternate into a running async pipeline
169 $hash_obj->[1] = "--git-dir=$async_alt";
170 $remote = xqx($hash_obj, undef, { 0 => \'async' });
172 open $fh, '>>', "$dir/objects/info/alternates" or
173 die "open failed: $!\n";
174 print $fh "$async_alt/objects\n" or die "print failed: $!\n";
175 close $fh or die "close failed: $!";
176 # trigger cat_async_retry:
177 $gcf->cat_async($remote, $cb, $results);
179 $gcf->async_wait_all;
180 my $expect = [ @exist[0..3], [ $remote, 'blob', 5 ], @exist[4..5] ];
181 is_deeply($results, $expect, 'got expected results');
183 ok(!$gcf->cleanup, 'cleanup can expire');
184 ok(!$gcf->cleanup, 'cleanup idempotent');
186 my $t = $gcf->modified;
187 ok($t <= time, 'repo not modified in the future');
188 isnt($t, 0, 'repo not modified in 1970')
191 use_ok 'PublicInbox::Git', qw(git_unquote git_quote);
193 is("foo\nbar", git_unquote($s = '"foo\\nbar"'), 'unquoted newline');
194 is("Eléanor", git_unquote($s = '"El\\303\\251anor"'), 'unquoted octal');
195 is(git_unquote($s = '"I\"m"'), 'I"m', 'unquoted dq');
196 is(git_unquote($s = '"I\\m"'), 'I\\m', 'unquoted backslash');
198 is(git_quote($s = "Eléanor"), '"El\\303\\251anor"', 'quoted octal');
199 is(git_quote($s = "hello\"world"), '"hello\"world"', 'quoted dq');
200 is(git_quote($s = "hello\\world"), '"hello\\\\world"', 'quoted backslash');
201 is(git_quote($s = "hello\nworld"), '"hello\\nworld"', 'quoted LF');
202 is(git_quote($s = "hello\x06world"), '"hello\\006world"', 'quoted \\x06');
203 is(git_unquote($s = '"hello\\006world"'), "hello\x06world", 'unquoted \\x06');