]> Sergey Matveev's repositories - public-inbox.git/blob - t/index-git-times.t
index: use git commit times on missing Date/Received
[public-inbox.git] / t / index-git-times.t
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use Test::More;
4 use PublicInbox::TestCommon;
5 use PublicInbox::Import;
6 use PublicInbox::Config;
7 use File::Path qw(remove_tree);
8
9 require_mods(qw(DBD::SQLite Search::Xapian));
10 use_ok 'PublicInbox::Over';
11
12 my ($tmpdir, $for_destroy) = tmpdir();
13 local $ENV{PI_CONFIG} = "$tmpdir/cfg";
14 my $v1dir = "$tmpdir/v1";
15 my $addr = 'x@example.com';
16 run_script(['-init', '--indexlevel=medium', 'v1', $v1dir,
17                 'http://example.com/x', $addr])
18         or die "init failed";
19
20 {
21         my $data = <<'EOF';
22 blob
23 mark :1
24 data 133
25 From: timeless <t@example.com>
26 To: x <x@example.com>
27 Subject: can I haz the time?
28 Message-ID: <19700101000000-1234@example.com>
29
30 plz
31
32 reset refs/heads/master
33 commit refs/heads/master
34 mark :2
35 author timeless <t@example.com> 749520000 +0100
36 committer x <x@example.com> 1285977600 -0100
37 data 20
38 can I haz the time?
39 M 100644 :1 53/256f6177504c2878d3a302ef5090dacf5e752c
40
41 EOF
42         pipe(my($r, $w)) or die;
43         length($data) <= 512 or die "data too large to fit in POSIX pipe";
44         print $w $data or die;
45         close $w or die;
46         my $cmd = ['git', "--git-dir=$v1dir", 'fast-import', '--quiet'];
47         PublicInbox::Import::run_die($cmd, undef, { 0 => $r });
48 }
49
50 run_script(['-index', $v1dir]) or die 'v1 index failed';
51 my $smsg;
52 {
53         my $cfg = PublicInbox::Config->new;
54         my $ibx = $cfg->lookup($addr);
55         $smsg = $ibx->over->get_art(1);
56         is($smsg->{ds}, 749520000, 'datestamp from git author time');
57         is($smsg->{ts}, 1285977600, 'timestamp from git committer time');
58         my $res = $ibx->search->query("m:$smsg->{mid}");
59         is(scalar @$res, 1, 'got one result for m:');
60         is($res->[0]->{ds}, $smsg->{ds}, 'Xapian stored datestamp');
61         $res = $ibx->search->query('d:19931002..19931002');
62         is(scalar @$res, 1, 'got one result for d:');
63         is($res->[0]->{ds}, $smsg->{ds}, 'Xapian search on datestamp');
64 }
65 SKIP: {
66         require_git(2.6, 1) or skip('git 2.6+ required for v2', 10);
67         my $v2dir = "$tmpdir/v2";
68         run_script(['-convert', $v1dir, $v2dir]) or die 'v2 conversion failed';
69
70         my $check_v2 = sub {
71                 my $ibx = PublicInbox::Inbox->new({inboxdir => $v2dir,
72                                 address => $addr});
73                 my $v2smsg = $ibx->over->get_art(1);
74                 is($v2smsg->{ds}, $smsg->{ds},
75                         'v2 datestamp from git author time');
76                 is($v2smsg->{ts}, $smsg->{ts},
77                         'v2 timestamp from git committer time');
78                 my $res = $ibx->search->query("m:$smsg->{mid}");
79                 is($res->[0]->{ds}, $smsg->{ds}, 'Xapian stored datestamp');
80                 $res = $ibx->search->query('d:19931002..19931002');
81                 is(scalar @$res, 1, 'got one result for d:');
82                 is($res->[0]->{ds}, $smsg->{ds}, 'Xapian search on datestamp');
83         };
84         $check_v2->();
85         remove_tree($v2dir);
86
87         # test non-parallelized conversion
88         run_script(['-convert', '-j0', $v1dir, $v2dir]) or
89                 die 'v2 conversion failed';
90         $check_v2->();
91 }
92
93 done_testing;