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