]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiMirror.pm
lei_mirror: break circular references
[public-inbox.git] / lib / PublicInbox / LeiMirror.pm
index 0508c9a8c4aad99ca3cf2b8adddd92cb73a1c1cb..92156cf1cca8b250675bbbebba67971059ac7459 100644 (file)
@@ -428,20 +428,9 @@ sub forkgroup_prep {
        my $key = $self->{-key} // die 'BUG: no -key';
        my $rn = substr(sha256_hex($key), 0, 16);
        if (!-d $self->{cur_dst} && !$self->{dry_run}) {
-               my $alt = File::Spec->rel2abs("$dir/objects");
                PublicInbox::Import::init_bare($self->{cur_dst});
-               my $o = "$self->{cur_dst}/objects";
-               my $f = "$o/info/alternates";
-               my $l = File::Spec->abs2rel($alt, File::Spec->rel2abs($o));
-               open my $fh, '+>>', $f or die "open($f): $!";
-               seek($fh, SEEK_SET, 0) or die "seek($f): $!";
-               chomp(my @cur = <$fh>);
-               if (!grep(/\A\Q$l\E\z/, @cur)) {
-                       say $fh $l or die "say($f): $!";
-               }
-               close $fh or die "close($f): $!";
-               $f = "$self->{cur_dst}/config";
-               open $fh, '+>>', $f or die "open:($f): $!";
+               my $f = "$self->{cur_dst}/config";
+               open my $fh, '+>>', $f or die "open:($f): $!";
                print $fh <<EOM or die "print($f): $!";
 ; rely on the "$rn" remote in the
 ; $fg fork group for fetches
@@ -453,6 +442,19 @@ sub forkgroup_prep {
 EOM
                close $fh or die "close($f): $!";
        }
+       if (!$self->{dry_run}) {
+               my $alt = File::Spec->rel2abs("$dir/objects");
+               my $o = "$self->{cur_dst}/objects";
+               my $f = "$o/info/alternates";
+               my $l = File::Spec->abs2rel($alt, File::Spec->rel2abs($o));
+               open my $fh, '+>>', $f or die "open($f): $!";
+               seek($fh, SEEK_SET, 0) or die "seek($f): $!";
+               chomp(my @cur = <$fh>);
+               if (!grep(/\A\Q$l\E\z/, @cur)) {
+                       say $fh $l or die "say($f): $!";
+               }
+               close $fh or die "close($f): $!";
+       }
        bless {
                %$self, -osdir => $dir, -remote => $rn, -uri => $uri
        }, __PACKAGE__;
@@ -705,6 +707,7 @@ sub update_ent {
        }
        if (my $symlinks = $self->{-ent}->{symlinks}) {
                my $top = File::Spec->rel2abs($self->{dst});
+               push @{$self->{-new_symlinks}}, @$symlinks;
                for my $p (@$symlinks) {
                        my $ln = "$top/$p";
                        $ln =~ tr!/!/!s;
@@ -957,9 +960,13 @@ sub clone_all {
                        # resolve multi-level references
                        while ($m && defined($nxt = $m->{$x}->{reference})) {
                                exists($todo->{$nxt}) or last;
-                               die <<EOM if ++$nr > 1000;
-E: dependency loop detected (`$x' => `$nxt')
+                               if (++$nr > 1000) {
+                                       $m->{$x}->{reference} = undef;
+                                       $m->{$nxt}->{reference} = undef;
+                                       warn <<EOM
+E: dependency loop detected (`$x' => `$nxt'), breaking
 EOM
+                               }
                                $x = $nxt;
                        }
                        my $y = delete $todo->{$x} // next; # already done
@@ -991,6 +998,36 @@ sub dump_manifest ($$) {
        utime($mtime, $mtime, "$ft") or die "utime(..., $ft): $!";
 }
 
+sub dump_project_list ($$) {
+       my ($self, $m) = @_;
+       my $f = $self->{'-project-list'} // return;
+       my $old = PublicInbox::Git::try_cat($f);
+       my %new;
+
+       open my $dh, '<', '.' or die "open(.): $!";
+       chdir($self->{dst}) or die "chdir($self->{dst}): $!";
+       my @local = grep { -e $_ ? ($new{$_} = undef) : 1 } split(/\n/s, $old);
+       chdir($dh) or die "chdir(restore): $!";
+
+       $new{substr($_, 1)} = 1 for keys %$m; # drop leading '/'
+       my @list = sort keys %new;
+       my @remote = grep { !defined($new{$_}) } @list;
+       my %lnk = map { substr($_, 1) => undef } @{$self->{-new_symlinks}};
+       @remote = grep { !exists($lnk{$_}) } @remote;
+
+       warn <<EOM if @remote;
+The following local repositories are ignored/gone from $self->{src}:
+EOM
+       warn "\t", $_, "\n" for @remote;
+       warn <<EOM if @local;
+The following repos in $f no longer exist on the filesystem:
+EOM
+       warn "\t", $_, "\n" for @local;
+
+       my (undef, $dn, $bn) = File::Spec->splitpath($f);
+       atomic_write($dn, $bn, join("\n", @list, ''));
+}
+
 # FIXME: this gets confused by single inbox instance w/ global manifest.js.gz
 sub try_manifest {
        my ($self) = @_;
@@ -1028,6 +1065,7 @@ sub try_manifest {
        }
        local $self->{chg} = {};
        local $self->{-local_manifest} = load_current_manifest($self);
+       local $self->{-new_symlinks} = [];
        my ($path_pfx, $n, $multi) = multi_inbox($self, \$path, $m);
        return $lei->child_error(1, $multi) if !ref($multi);
        my $v2 = delete $multi->{v2};
@@ -1104,6 +1142,7 @@ EOM
        warn(<<EOM, map { ("\t", $_, "\n") } @$bad) if $bad;
 W: The following exist and have not been converted to symlinks
 EOM
+       dump_project_list($self, $m);
        ft_rename($ft, $manifest, 0666);
 }
 
@@ -1124,14 +1163,18 @@ sub do_mirror { # via wq_io_do or public-inbox-clone
                $ic =~ /\A(?:v1|v2|always|never)\z/s or die <<"";
 --inbox-config must be one of `always', `v2', `v1', or `never'
 
-               # we support --objstore= and --manifest= with '' (empty string)
-               for my $default (qw(objstore manifest.js.gz)) {
-                       my ($k) = (split(/\./, $default))[0];
+               # we support these switches with '' (empty string).
+               # defaults match example conf distributed with grokmirror
+               my @pairs = qw(objstore objstore manifest manifest.js.gz
+                               project-list projects.list);
+               while (@pairs) {
+                       my ($k, $default) = splice(@pairs, 0, 2);
                        my $v = $lei->{opt}->{$k} // next;
                        $v = $default if $v eq '';
                        $v = "$self->{dst}/$v" if $v !~ m!\A\.{0,2}/!;
                        $self->{"-$k"} = $v;
                }
+
                local $LIVE = {};
                local $TODO = {};
                local $FGRP_TODO = {};