2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 use PublicInbox::TestCommon;
8 use PublicInbox::Import;
10 require_mods('PublicInbox::Gcf2');
11 use_ok 'PublicInbox::Gcf2Client';
12 my ($tmpdir, $for_destroy) = tmpdir();
13 my $git_a = "$tmpdir/a.git";
14 my $git_b = "$tmpdir/b.git";
15 PublicInbox::Import::init_bare($git_a);
16 PublicInbox::Import::init_bare($git_b);
17 my $fi_data = './t/git.fast-import-data';
19 open $rdr->{0}, '<', $fi_data or BAIL_OUT $!;
20 xsys([qw(git fast-import --quiet)], { GIT_DIR => $git_a }, $rdr);
21 is($?, 0, 'fast-import succeeded');
23 my $tree = 'fdbc43725f21f485051c17463b50185f4c3cf88c';
25 my $err_f = "$tmpdir/err";
27 local $ENV{PATH} = getcwd()."/blib/script:$ENV{PATH}";
28 open my $err, '>', $err_f or BAIL_OUT $!;
29 my $gcf2c = PublicInbox::Gcf2Client::new({ 2 => $err });
30 $gcf2c->cat_async("$tree $git_a", sub {
31 my ($bref, $oid, $type, $size, $arg) = @_;
32 is($oid, $tree, 'got expected OID');
33 is($size, 30, 'got expected length');
34 is($type, 'tree', 'got tree type');
35 is(length($$bref), 30, 'got a tree');
36 is($arg, 'hi', 'arg passed');
39 $gcf2c->cat_async_wait;
41 open $err, '<', $err_f or BAIL_OUT $!;
42 my $estr = do { local $/; <$err> };
43 is($estr, '', 'nothing in stderr');
45 my $trunc = substr($tree, 0, 39);
46 $gcf2c->cat_async("$trunc $git_a", sub {
47 my ($bref, $oid, $type, $size, $arg) = @_;
48 is(undef, $bref, 'missing bref is undef');
49 is($oid, $trunc, 'truncated OID printed');
50 is($type, 'missing', 'type is "missing"');
51 is($size, undef, 'size is undef');
52 is($arg, 'bye', 'arg passed when missing');
55 $gcf2c->cat_async_wait;
57 open $err, '<', $err_f or BAIL_OUT $!;
58 $estr = do { local $/; <$err> };
59 like($estr, qr/retrying/, 'warned about retry');
61 # try failed alternates lookup
62 open $err, '>', $err_f or BAIL_OUT $!;
63 $gcf2c = PublicInbox::Gcf2Client::new({ 2 => $err });
64 $gcf2c->cat_async("$tree $git_b", sub {
65 my ($bref, $oid, $type, $size, $arg) = @_;
66 is(undef, $bref, 'missing bref from alt is undef');
69 $gcf2c->cat_async_wait;
70 open $err, '<', $err_f or BAIL_OUT $!;
71 $estr = do { local $/; <$err> };
72 like($estr, qr/retrying/, 'warned about retry before alt update');
74 # now try successful alternates lookup
75 open my $alt, '>>', "$git_b/objects/info/alternates" or BAIL_OUT $!;
76 print $alt "$git_a/objects\n" or BAIL_OUT $!;
77 close $alt or BAIL_OUT;
78 my $expect = xqx(['git', "--git-dir=$git_a", qw(cat-file tree), $tree]);
79 $gcf2c->cat_async("$tree $git_a", sub {
80 my ($bref, $oid, $type, $size, $arg) = @_;
81 is($oid, $tree, 'oid match on alternates retry');
82 is($$bref, $expect, 'tree content matched');
85 $gcf2c->cat_async_wait;
87 is($called, 4, 'cat_async callbacks hit');