2 # Copyright (C) 2020-2021 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;
11 require_mods('PublicInbox::Gcf2');
12 use_ok 'PublicInbox::Gcf2Client';
13 my ($tmpdir, $for_destroy) = tmpdir();
14 my $git_a = "$tmpdir/a.git";
15 my $git_b = "$tmpdir/b.git";
16 PublicInbox::Import::init_bare($git_a);
17 PublicInbox::Import::init_bare($git_b);
18 my $fi_data = './t/git.fast-import-data';
20 open $rdr->{0}, '<', $fi_data or BAIL_OUT $!;
21 xsys([qw(git fast-import --quiet)], { GIT_DIR => $git_a }, $rdr);
22 is($?, 0, 'fast-import succeeded');
24 my $tree = 'fdbc43725f21f485051c17463b50185f4c3cf88c';
26 my $err_f = "$tmpdir/err";
28 PublicInbox::DS->Reset;
29 open my $err, '>>', $err_f or BAIL_OUT $!;
30 my $gcf2c = PublicInbox::Gcf2Client::new({ 2 => $err });
31 $gcf2c->gcf2_async(\"$tree $git_a\n", sub {
32 my ($bref, $oid, $type, $size, $arg) = @_;
33 is($oid, $tree, 'got expected OID');
34 is($size, 30, 'got expected length');
35 is($type, 'tree', 'got tree type');
36 is(length($$bref), 30, 'got a tree');
37 is($arg, 'hi', 'arg passed');
40 $gcf2c->cat_async_step($gcf2c->{inflight});
42 open $err, '<', $err_f or BAIL_OUT $!;
43 my $estr = do { local $/; <$err> };
44 is($estr, '', 'nothing in stderr');
46 my $trunc = substr($tree, 0, 39);
47 $gcf2c->gcf2_async(\"$trunc $git_a\n", sub {
48 my ($bref, $oid, $type, $size, $arg) = @_;
49 is(undef, $bref, 'missing bref is undef');
50 is($oid, $trunc, 'truncated OID printed');
51 is($type, 'missing', 'type is "missing"');
52 is($size, undef, 'size is undef');
53 is($arg, 'bye', 'arg passed when missing');
56 $gcf2c->cat_async_step($gcf2c->{inflight});
58 open $err, '<', $err_f or BAIL_OUT $!;
59 $estr = do { local $/; <$err> };
60 like($estr, qr/retrying/, 'warned about retry');
62 # try failed alternates lookup
63 PublicInbox::DS->Reset;
64 open $err, '>', $err_f or BAIL_OUT $!;
65 $gcf2c = PublicInbox::Gcf2Client::new({ 2 => $err });
66 $gcf2c->gcf2_async(\"$tree $git_b\n", sub {
67 my ($bref, $oid, $type, $size, $arg) = @_;
68 is(undef, $bref, 'missing bref from alt is undef');
71 $gcf2c->cat_async_step($gcf2c->{inflight});
72 open $err, '<', $err_f or BAIL_OUT $!;
73 $estr = do { local $/; <$err> };
74 like($estr, qr/retrying/, 'warned about retry before alt update');
76 # now try successful alternates lookup
77 open my $alt, '>>', "$git_b/objects/info/alternates" or BAIL_OUT $!;
78 print $alt "$git_a/objects\n" or BAIL_OUT $!;
79 close $alt or BAIL_OUT;
80 my $expect = xqx(['git', "--git-dir=$git_a", qw(cat-file tree), $tree]);
81 $gcf2c->gcf2_async(\"$tree $git_a\n", sub {
82 my ($bref, $oid, $type, $size, $arg) = @_;
83 is($oid, $tree, 'oid match on alternates retry');
84 is($$bref, $expect, 'tree content matched');
87 $gcf2c->cat_async_step($gcf2c->{inflight});
89 is($called, 4, 'gcf2_async callbacks hit');