-sub fill_alternates ($$) {
- my ($self, $epoch) = @_;
-
- my $pfx = "$self->{ibx}->{inboxdir}/git";
- my $all = "$self->{ibx}->{inboxdir}/all.git";
- PublicInbox::Import::init_bare($all) unless -d $all;
- my $info_dir = "$all/objects/info";
- my $alt = "$info_dir/alternates";
- my (%alt, $new);
- my $mode = 0644;
- if (-e $alt) {
- open(my $fh, '<', $alt) or die "open < $alt: $!\n";
- $mode = (stat($fh))[2] & 07777;
-
- # we assign a sort score to every alternate and favor
- # the newest (highest numbered) one because loose objects
- # require scanning epochs and only the latest epoch is
- # expected to see loose objects
- my $score;
- my $other = 0; # in case admin adds non-epoch repos
- %alt = map {;
- if (m!\A\Q../../\E([0-9]+)\.git/objects\z!) {
- $score = $1 + 0;
- } else {
- $score = --$other;
- }
- $_ => $score;
- } split(/\n+/, do { local $/; <$fh> });
- }
-
- foreach my $i (0..$epoch) {
- my $dir = "../../git/$i.git/objects";
- if (!exists($alt{$dir}) && -d "$pfx/$i.git") {
- $alt{$dir} = $i;
- $new = 1;
- }
- }
- return unless $new;
-
- my ($fh, $tmp) = tempfile('alt-XXXXXXXX', DIR => $info_dir);
- print $fh join("\n", sort { $alt{$b} <=> $alt{$a} } keys %alt), "\n"
- or die "print $tmp: $!\n";
- chmod($mode, $fh) or die "fchmod $tmp: $!\n";
- close $fh or die "close $tmp $!\n";
- rename($tmp, $alt) or die "rename $tmp => $alt: $!\n";
-}
-
-sub git_init {
- my ($self, $epoch) = @_;
- my $git_dir = "$self->{ibx}->{inboxdir}/git/$epoch.git";
- PublicInbox::Import::init_bare($git_dir);
- my @cmd = (qw/git config/, "--file=$git_dir/config",
- 'include.path', '../../all.git/config');
- PublicInbox::Import::run_die(\@cmd);
- fill_alternates($self, $epoch);
- $git_dir
-}
-
-sub git_dir_latest {
- my ($self, $max) = @_;
- $$max = -1;
- my $pfx = "$self->{ibx}->{inboxdir}/git";
- return unless -d $pfx;
- my $latest;
- opendir my $dh, $pfx or die "opendir $pfx: $!\n";
- while (defined(my $git_dir = readdir($dh))) {
- $git_dir =~ m!\A([0-9]+)\.git\z! or next;
- if ($1 > $$max) {
- $$max = $1;
- $latest = "$pfx/$git_dir";
- }
- }
- $latest;
-}
-