]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MultiGit.pm
multi_git: hoist out common epoch/alternates handling
[public-inbox.git] / lib / PublicInbox / MultiGit.pm
1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # common git alternates + all.git||ALL.git management code
5 package PublicInbox::MultiGit;
6 use strict;
7 use v5.10.1;
8 use PublicInbox::Spawn qw(run_die);
9 use PublicInbox::Import;
10 use File::Temp 0.19;
11 use List::Util qw(max);
12
13 sub new {
14         my ($cls, $topdir, $all, $epfx) = @_;
15         bless {
16                 topdir => $topdir, # inboxdir || extindex.*.topdir
17                 all => $all, # all.git or ALL.git
18                 epfx => $epfx, # "git" (inbox) or "local" (lei/store)
19         }, $cls;
20 }
21
22 sub read_alternates {
23         my ($self, $moderef, $prune) = @_;
24         my $objpfx = "$self->{topdir}/$self->{all}/objects/";
25         my $f = "${objpfx}info/alternates";
26         my %alt; # line => score
27         my %seen; # $st_dev\0$st_ino => count
28         my $other = 0;
29         if (open(my $fh, '<', $f)) {
30                 my $is_edir = defined($self->{epfx}) ?
31                         qr!\A\Q../../$self->{epfx}\E/([0-9]+)\.git/objects\z! :
32                         undef;
33                 $$moderef = (stat($fh))[2] & 07777;
34                 for my $rel (split(/^/m, do { local $/; <$fh> })) {
35                         chomp(my $dir = $rel);
36                         my $score;
37                         if (defined($is_edir) && $dir =~ $is_edir) {
38                                 $score = $1 + 0;
39                                 substr($dir, 0, 0) = $objpfx;
40                         } else { # absolute paths, if any (extindex)
41                                 $score = --$other;
42                         }
43                         if (my @st = stat($dir)) {
44                                 next if $seen{"$st[0]\0$st[1]"}++;
45                                 $alt{$rel} = $score;
46                         } else {
47                                 warn "W: stat($dir) failed: $! ($f)";
48                                 $alt{$rel} = $score unless $prune;
49                         }
50                 }
51         } elsif (!$!{ENOENT}) {
52                 die "E: open($f): $!";
53         }
54         (\%alt, \%seen);
55 }
56
57 sub epoch_dir { "$_[0]->{topdir}/$_[0]->{epfx}" }
58
59 sub write_alternates {
60         my ($self, $mode, $alt, @new) = @_;
61         my $all_dir = "$self->{topdir}/$self->{all}";
62         PublicInbox::Import::init_bare($all_dir);
63         my $out = join('', sort { $alt->{$b} <=> $alt->{$a} } keys %$alt);
64         my $info_dir = "$all_dir/objects/info";
65         my $fh = File::Temp->new(TEMPLATE => 'alt-XXXX', DIR => $info_dir);
66         my $f = $fh->filename;
67         print $fh $out, @new or die "print($f): $!";
68         chmod($mode, $fh) or die "fchmod($f): $!";
69         close $fh or die "close($f): $!";
70         my $fn = "$info_dir/alternates";
71         rename($f, $fn) or die "rename($f, $fn): $!";
72         $fh->unlink_on_destroy(0);
73 }
74
75 # returns true if new epochs exist
76 sub merge_epochs {
77         my ($self, $alt, $seen) = @_;
78         my $epoch_dir = epoch_dir($self);
79         if (opendir my $dh, $epoch_dir) {
80                 my $has_new;
81                 for my $bn (grep(/\A[0-9]+\.git\z/, readdir($dh))) {
82                         my $rel = "../../$self->{epfx}/$bn/objects\n";
83                         next if exists($alt->{$rel});
84                         if (my @st = stat("$epoch_dir/$bn/objects")) {
85                                 next if $seen->{"$st[0]\0$st[1]"}++;
86                                 $alt->{$rel} = substr($bn, 0, -4) + 0;
87                                 $has_new = 1;
88                         } else {
89                                 warn "E: stat($epoch_dir/$bn/objects): $!";
90                         }
91                 }
92                 $has_new;
93         } else {
94                 $!{ENOENT} ? undef : die "opendir($epoch_dir): $!";
95         }
96 }
97
98 sub fill_alternates {
99         my ($self) = @_;
100         my ($alt, $seen) = read_alternates($self, \(my $mode = 0644));
101         merge_epochs($self, $alt, $seen) and
102                 write_alternates($self, $mode, $alt);
103 }
104
105 sub epoch_cfg_set {
106         my ($self, $epoch_nr) = @_;
107         run_die([qw(git config -f), epoch_dir($self)."/$epoch_nr.git/config",
108                 'include.path', "../../$self->{all}/config" ]);
109 }
110
111 sub add_epoch {
112         my ($self, $epoch_nr) = @_;
113         my $git_dir = epoch_dir($self)."/$epoch_nr.git";
114         my $f = "$git_dir/config";
115         my $existing = -f $f;
116         PublicInbox::Import::init_bare($git_dir);
117         epoch_cfg_set($self, $epoch_nr) unless $existing;
118         fill_alternates($self);
119         $git_dir;
120 }
121
122 sub git_epochs  {
123         my ($self) = @_;
124         if (opendir(my $dh, epoch_dir($self))) {
125                 my @epochs = map {
126                         substr($_, 0, -4) + 0; # drop ".git" suffix
127                 } grep(/\A[0-9]+\.git\z/, readdir($dh));
128                 wantarray ? sort { $b <=> $a } @epochs : (max(@epochs) // 0);
129         } elsif ($!{ENOENT}) {
130                 wantarray ? () : 0;
131         } else {
132                 die(epoch_dir($self).": $!");
133         }
134 }
135
136 1;