]> Sergey Matveev's repositories - public-inbox.git/blob - t/git.t
7b950d88f4cc5c83f26393183b8918ce452ea34d
[public-inbox.git] / t / git.t
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>
3 use strict;
4 use Test::More;
5 use PublicInbox::TestCommon;
6 my ($dir, $for_destroy) = tmpdir();
7 use PublicInbox::Import;
8 use POSIX qw(strftime);
9
10 use_ok 'PublicInbox::Git';
11
12 {
13         PublicInbox::Import::init_bare($dir);
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');
20 }
21 {
22         my $git = PublicInbox::Git->new($dir);
23         my $s = $git->date_parse('1970-01-01T00:00:00Z');
24         is($s, 0, 'parsed epoch');
25         local $ENV{TZ} = 'UTC';
26         $s = $git->date_parse('1993-10-02 01:02:09');
27         is(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($s)), '1993-10-02T01:02:09Z',
28                 'round trips');
29         $s = $git->date_parse('1993-10-02');
30         is(strftime('%Y-%m-%d', gmtime($s)), '1993-10-02',
31                 'round trips date-only');
32 }
33
34 {
35         my $gcf = PublicInbox::Git->new($dir);
36         is($gcf->modified, 749520000, 'modified time detected from commit');
37         my $f = 'HEAD:foo.txt';
38         my @x = $gcf->check($f);
39         is(scalar @x, 3, 'returned 3 element array for existing file');
40         like($x[0], qr/\A[a-f0-9]{40}\z/, 'returns obj ID in 1st element');
41         is('blob', $x[1], 'returns obj type in 2nd element');
42         like($x[2], qr/\A\d+\z/, 'returns obj size in 3rd element');
43
44         my $raw = $gcf->cat_file($f);
45         is($x[2], length($$raw), 'length matches');
46
47         is(${$gcf->cat_file($f)}, $$raw, 'not broken after failures');
48         is(${$gcf->cat_file($f)}, $$raw, 'not broken after partial read');
49
50         my $oid = $x[0];
51         my $arg = { 'foo' => 'bar' };
52         my $res = [];
53         my $missing = [];
54         $gcf->cat_async($oid, sub {
55                 my ($bref, $oid_hex, $type, $size, $arg) = @_;
56                 $res = [ @_ ];
57         }, $arg);
58         $gcf->cat_async('non-existent', sub {
59                 my ($bref, $oid_hex, $type, $size, $arg) = @_;
60                 $missing = [ @_ ];
61         }, $arg);
62         $gcf->cat_async_wait;
63         my ($bref, $oid_hex, $type, $size, $arg_res) = @$res;
64         is_deeply([$oid_hex, $type, $size], \@x, 'got expected header');
65         is($arg_res, $arg, 'arg passed to cat_async');
66         is_deeply($raw, $bref, 'blob result matches');
67         is_deeply($missing, [ undef, 'non-existent', 'missing', undef, $arg],
68                 'non-existent blob gives expected result');
69 }
70
71 if (1) {
72         # need a big file, use the AGPL-3.0 :p
73         my $big_data = './COPYING';
74         ok(-r $big_data, 'COPYING readable');
75         my $size = -s $big_data;
76         ok($size > 8192, 'file is big enough');
77         open my $fh, '<', $big_data or die;
78         my $cmd = [ 'git', "--git-dir=$dir", qw(hash-object -w --stdin) ];
79         my $buf = xqx($cmd, { GIT_DIR => $dir }, { 0 => $fh });
80         is(0, $?, 'hashed object successfully');
81         chomp $buf;
82
83         my $gcf = PublicInbox::Git->new($dir);
84         my @x = $gcf->cat_file($buf);
85         is($x[2], 'blob', 'got blob on wantarray');
86         is($x[3], $size, 'got correct size ref on big file');
87         is(length(${$x[0]}), $size, 'read correct number of bytes');
88
89         my $ref = $gcf->qx(qw(cat-file blob), $buf);
90         is($?, 0, 'no error on scalar success');
91         my @ref = $gcf->qx(qw(cat-file blob), $buf);
92         is($?, 0, 'no error on wantarray success');
93         my $nl = scalar @ref;
94         ok($nl > 1, "qx returned array length of $nl");
95         is(join('', @ref), $ref, 'qx array and scalar context both work');
96
97         $gcf->qx(qw(repack -adq));
98         ok($gcf->packed_bytes > 0, 'packed size is positive');
99         $gcf->qx(qw(rev-parse --verify bogus));
100         isnt($?, 0, '$? set on failure'.$?);
101 }
102
103 SKIP: {
104         require_git(2.6, 7) or skip('need git 2.6+ for --batch-all-objects', 7);
105         my ($alt, $alt_obj) = tmpdir();
106         my $hash_obj = [ 'git', "--git-dir=$alt", qw(hash-object -w --stdin) ];
107         PublicInbox::Import::init_bare($alt);
108         open my $fh, '<', "$alt/config" or die "open failed: $!\n";
109         chomp(my $remote = xqx($hash_obj, undef, { 0 => $fh }));
110         my $gcf = PublicInbox::Git->new($dir);
111         is($gcf->cat_file($remote), undef, "remote file not found");
112         open $fh, '>>', "$dir/objects/info/alternates" or
113                         die "open failed: $!\n";
114         print $fh "$alt/objects\n" or die "print failed: $!\n";
115         close $fh or die "close failed: $!";
116         my $found = $gcf->cat_file($remote);
117         open $fh, '<', "$alt/config" or die "open failed: $!\n";
118         my $config = eval { local $/; <$fh> };
119         is($$found, $config, 'alternates reloaded');
120
121         # with the async interface
122         my ($async_alt, $async_dir_obj) = tmpdir();
123         PublicInbox::Import::init_bare($async_alt);
124         my @exist = map { chomp; [ split / / ] } (xqx(['git', "--git-dir=$dir",
125                         qw(cat-file --batch-all-objects --batch-check)]));
126         my $results = [];
127         my $cb = sub {
128                 my ($bref, $oid, $type, $size) = @_;
129                 push @$results, [ $oid, $type, $size ];
130         };
131         for my $i (0..5) {
132                 $gcf->cat_async($exist[$i]->[0], $cb, $results);
133                 next if $i != 3;
134
135                 # stick a new alternate into a running async pipeline
136                 $hash_obj->[1] = "--git-dir=$async_alt";
137                 $remote = xqx($hash_obj, undef, { 0 => \'async' });
138                 chomp $remote;
139                 open $fh, '>>', "$dir/objects/info/alternates" or
140                                 die "open failed: $!\n";
141                 print $fh "$async_alt/objects\n" or die "print failed: $!\n";
142                 close $fh or die "close failed: $!";
143                 # trigger cat_async_retry:
144                 $gcf->cat_async($remote, $cb, $results);
145         }
146         $gcf->cat_async_wait;
147         my $expect = [ @exist[0..3], [ $remote, 'blob', 5 ], @exist[4..5] ];
148         is_deeply($results, $expect, 'got expected results');
149
150         ok(!$gcf->cleanup, 'cleanup can expire');
151         ok(!$gcf->cleanup, 'cleanup idempotent');
152
153         my $t = $gcf->modified;
154         ok($t <= time, 'repo not modified in the future');
155         isnt($t, 0, 'repo not modified in 1970')
156 }
157
158 use_ok 'PublicInbox::Git', qw(git_unquote git_quote);
159 my $s;
160 is("foo\nbar", git_unquote($s = '"foo\\nbar"'), 'unquoted newline');
161 is("Eléanor", git_unquote($s = '"El\\303\\251anor"'), 'unquoted octal');
162 is(git_unquote($s = '"I\"m"'), 'I"m', 'unquoted dq');
163 is(git_unquote($s = '"I\\m"'), 'I\\m', 'unquoted backslash');
164
165 is(git_quote($s = "Eléanor"), '"El\\303\\251anor"', 'quoted octal');
166 is(git_quote($s = "hello\"world"), '"hello\"world"', 'quoted dq');
167 is(git_quote($s = "hello\\world"), '"hello\\\\world"', 'quoted backslash');
168 is(git_quote($s = "hello\nworld"), '"hello\\nworld"', 'quoted LF');
169
170 done_testing();