about summary refs log tree commit homepage
path: root/lib/PublicInbox/LeiMirror.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/PublicInbox/LeiMirror.pm')
-rw-r--r--lib/PublicInbox/LeiMirror.pm510
1 files changed, 317 insertions, 193 deletions
diff --git a/lib/PublicInbox/LeiMirror.pm b/lib/PublicInbox/LeiMirror.pm
index 33cf55ab..e7c265bd 100644
--- a/lib/PublicInbox/LeiMirror.pm
+++ b/lib/PublicInbox/LeiMirror.pm
@@ -7,32 +7,37 @@ use v5.12;
 use parent qw(PublicInbox::IPC);
 use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
 use IO::Compress::Gzip qw(gzip $GzipError);
-use PublicInbox::Spawn qw(popen_rd spawn run_die);
+use PublicInbox::Spawn qw(spawn run_wait run_die run_qx);
+use PublicInbox::IO qw(write_file);
 use File::Path ();
 use File::Temp ();
 use File::Spec ();
 use Fcntl qw(SEEK_SET O_CREAT O_EXCL O_WRONLY);
 use Carp qw(croak);
 use URI;
-use PublicInbox::Config;
+use PublicInbox::Config qw(glob2re);
 use PublicInbox::Inbox;
 use PublicInbox::LeiCurl;
 use PublicInbox::OnDestroy;
-use Digest::SHA qw(sha256_hex sha1_hex);
+use PublicInbox::SHA qw(sha256_hex sha_all);
 use POSIX qw(strftime);
+use PublicInbox::Admin qw(fmt_localtime);
+use autodie qw(chdir chmod close open pipe readlink
+                seek symlink sysopen sysseek truncate unlink);
+use PublicInbox::Git qw(git_exe);
 
 our $LIVE; # pid => callback
-our $FGRP_TODO; # objstore -> [ fgrp mirror objects ]
+our $FGRP_TODO; # objstore -> [[ to resume ], [ to clone ]]
 our $TODO; # reference => [ non-fgrp mirror objects ]
+our @PUH; # post-update hooks
 
 sub keep_going ($) {
         $LIVE && (!$_[0]->{lei}->{child_error} ||
                 $_[0]->{lei}->{opt}->{'keep-going'});
 }
 
-sub _wq_done_wait { # dwaitpid callback (via wq_eof)
-        my ($arg, $pid) = @_;
-        my ($mrr, $lei) = @$arg;
+sub _wq_done_wait { # awaitpid cb (via wq_eof)
+        my ($pid, $mrr, $lei) = @_;
         if ($?) {
                 $lei->child_error($?);
         } elsif (!$lei->{child_error}) {
@@ -54,9 +59,8 @@ sub try_scrape {
         my $curl = $self->{curl} //= PublicInbox::LeiCurl->new($lei) or return;
         my $cmd = $curl->for_uri($lei, $uri, '--compressed');
         my $opt = { 0 => $lei->{0}, 2 => $lei->{2} };
-        my $fh = popen_rd($cmd, undef, $opt);
-        my $html = do { local $/; <$fh> } // die "read(curl $uri): $!";
-        close($fh) or return $lei->child_error($?, "@$cmd failed");
+        my $html = run_qx($cmd, undef, $opt);
+        return $lei->child_error($?, "@$cmd failed") if $?;
 
         # we grep with URL below, we don't want Subject/From headers
         # making us clone random URLs.  This assumes remote instances
@@ -102,7 +106,7 @@ E: confused by scraping <$uri>, got ambiguous results:
 
 sub clone_cmd {
         my ($lei, $opt) = @_;
-        my @cmd = qw(git);
+        my @cmd = (git_exe);
         $opt->{$_} = $lei->{$_} for (0..2);
         # we support "-c $key=$val" for arbitrary git config options
         # e.g.: git -c http.proxy=socks5h://127.0.0.1:9050
@@ -121,7 +125,7 @@ sub ft_rename ($$$;$) {
         my ($ft, $dst, $open_mode, $fh) = @_;
         my @st = stat($fh // $dst);
         my $mode = @st ? ($st[2] & 07777) : ($open_mode & ~umask);
-        chmod($mode, $ft) or croak "E: chmod($ft): $!";
+        chmod($mode, $ft);
         require File::Copy;
         File::Copy::mv($ft->filename, $dst) or croak "E: mv($ft => $dst): $!";
         $ft->unlink_on_destroy(0);
@@ -169,10 +173,9 @@ sub _get_txt_done { # returns true on error (non-fatal), undef on success
         $? = 0; # don't influence normal lei exit
         return warn("$uri missing\n") if ($cerr >> 8) == 22;
         return warn("# @$cmd failed (non-fatal)\n") if $cerr;
-        seek($fh, SEEK_SET, 0) or die "seek: $!";
+        seek($fh, 0, SEEK_SET);
         $self->{"mtime.$endpoint"} = (stat($fh))[9];
-        local $/;
-        $self->{"txt.$endpoint"} = <$fh>;
+        $self->{"txt.$endpoint"} = PublicInbox::IO::read_all $fh, -s _;
         undef; # success
 }
 
@@ -182,9 +185,9 @@ sub _write_inbox_config {
         my $dst = $self->{cur_dst} // $self->{dst};
         my $f = "$dst/inbox.config.example";
         my $mtime = delete $self->{'mtime._/text/config/raw'};
-        if (sysopen(my $fh, $f, O_CREAT|O_EXCL|O_WRONLY)) {
-                print $fh $buf or die "print: $!";
-                chmod(0444 & ~umask, $fh) or die "chmod($f): $!";
+        if (CORE::sysopen(my $fh, $f, O_CREAT|O_EXCL|O_WRONLY)) {
+                print $fh $buf;
+                chmod(0444 & ~umask, $fh);
                 $fh->flush or die "flush($f): $!";
                 if (defined $mtime) {
                         utime($mtime, $mtime, $fh) or die "utime($f): $!";
@@ -192,8 +195,9 @@ sub _write_inbox_config {
         } elsif (!$!{EEXIST}) {
                 die "open($f): $!";
         }
-        my $cfg = PublicInbox::Config->git_config_dump($f, $self->{lei}->{2});
-        my $ibx = $self->{ibx} = {};
+        my $cfg = PublicInbox::Config->git_config_dump($f,
+                                                { 2 => $self->{lei}->{2} });
+        my $ibx = $self->{ibx} = {}; # for indexing
         for my $sec (grep(/\Apublicinbox\./, @{$cfg->{-section_order}})) {
                 for (qw(address newsgroup nntpmirror)) {
                         $ibx->{$_} = $cfg->{"$sec.$_"};
@@ -204,7 +208,7 @@ sub _write_inbox_config {
 sub set_description ($) {
         my ($self) = @_;
         my $dst = $self->{cur_dst} // $self->{dst};
-        chomp(my $orig = PublicInbox::Git::try_cat("$dst/description"));
+        chomp(my $orig = PublicInbox::IO::try_cat("$dst/description"));
         my $d = $orig;
         while (defined($d) && ($d =~ m!^\(\$INBOX_DIR/description missing\)! ||
                         $d =~ /^Unnamed repository/ || $d !~ /\S/)) {
@@ -235,9 +239,9 @@ sub index_cloned_inbox {
                         my ($k) = ($sw =~ /\A([\w-]+)/);
                         $opt->{$k} = $lei->{opt}->{$k};
                 }
-                # force synchronous dwaitpid for v2:
+                # force synchronous awaitpid for v2:
                 local $PublicInbox::DS::in_loop = 0;
-                my $cfg = PublicInbox::Config->new(undef, $lei->{2});
+                my $cfg = PublicInbox::Config->new(undef, { 2 => $lei->{2} });
                 my $env = PublicInbox::Admin::index_prepare($opt, $cfg);
                 local %ENV = (%ENV, %$env) if $env;
                 PublicInbox::Admin::progress_prepare($opt, $lei->{2});
@@ -249,8 +253,7 @@ sub index_cloned_inbox {
 sub run_reap {
         my ($lei, $cmd, $opt) = @_;
         $lei->qerr("# @$cmd");
-        waitpid(spawn($cmd, undef, $opt), 0) // die "waitpid: $!";
-        my $ret = $?;
+        my $ret = run_wait($cmd, undef, $opt);
         $? = 0; # don't let it influence normal exit
         $ret;
 }
@@ -275,6 +278,8 @@ sub fetch_args ($$) {
                         ($lei->{opt}->{jobs} // 1) > 1;
         push @cmd, '-v' if $lei->{opt}->{verbose};
         push(@cmd, '-p') if $lei->{opt}->{prune};
+        PublicInbox::Git::git_version() ge v2.29.0 and
+                push(@cmd, '--no-write-fetch-head');
         @cmd;
 }
 
@@ -284,25 +289,32 @@ sub upr { # feed `git update-ref --stdin -z' verbosely
         print $w "$op ", join("\0", @rest, '') or die "print(w): $!";
 }
 
+sub start_update_ref {
+        my ($fgrp) = @_;
+        pipe(my $r, my $w);
+        my $cmd = [ git_exe, "--git-dir=$fgrp->{cur_dst}",
+                qw(update-ref --stdin -z) ];
+        my $pack = on_destroy \&satellite_done, $fgrp;
+        start_cmd($fgrp, $cmd, { 0 => $r, 2 => $fgrp->{lei}->{2} }, $pack);
+        close $r;
+        $fgrp->{dry_run} ? undef : $w;
+}
+
+sub upref_warn { warn "E: close(update-ref --stdin): $! (need git 1.8.5+)\n" }
+
 sub fgrp_update {
         my ($fgrp) = @_;
         return if !keep_going($fgrp);
         my $srcfh = delete $fgrp->{srcfh} or return;
         my $dstfh = delete $fgrp->{dstfh} or return;
-        seek($srcfh, SEEK_SET, 0) or die "seek(src): $!";
-        seek($dstfh, SEEK_SET, 0) or die "seek(dst): $!";
-        my %src = map { chomp; split(/\0/) } (<$srcfh>);
-        close $srcfh;
-        my %dst = map { chomp; split(/\0/) } (<$dstfh>);
-        close $dstfh;
-        pipe(my ($r, $w)) or die "pipe: $!";
-        my $cmd = [ 'git', "--git-dir=$fgrp->{cur_dst}",
-                qw(update-ref --stdin -z) ];
+        seek($srcfh, 0, SEEK_SET);
+        seek($dstfh, 0, SEEK_SET);
+        my %src = map { chomp; split /\0/ } PublicInbox::IO::read_all $srcfh;
+        my %dst = map { chomp; split /\0/ } PublicInbox::IO::read_all $dstfh;
+        $srcfh = $dstfh = undef;
+        my $w = start_update_ref($fgrp) or return;
         my $lei = $fgrp->{lei};
-        my $pack = PublicInbox::OnDestroy->new($$, \&pack_dst, $fgrp);
-        start_cmd($fgrp, $cmd, { 0 => $r, 2 => $lei->{2} }, $pack);
-        close $r or die "close(r): $!";
-        return if $fgrp->{dry_run};
+        my $ndel;
         for my $ref (keys %dst) {
                 my $new = delete $src{$ref};
                 my $old = $dst{$ref};
@@ -311,47 +323,68 @@ sub fgrp_update {
                                 upr($lei, $w, 'update', $ref, $new, $old);
                 } else {
                         upr($lei, $w, 'delete', $ref, $old);
+                        ++$ndel;
                 }
         }
-        while (my ($ref, $oid) = each %src) {
-                upr($lei, $w, 'create', $ref, $oid);
+        # git's ref files backend doesn't allow directory/file conflicts
+        # between `delete' and `create' ops:
+        if ($ndel && scalar(keys %src)) {
+                $fgrp->{-create_refs} = \%src;
+        } else {
+                while (my ($ref, $oid) = each %src) {
+                        upr($lei, $w, 'create', $ref, $oid);
+                }
         }
-        close($w) or warn "E: close(update-ref --stdin): $! (need git 1.8.5+)\n";
+        $w->close or upref_warn();
 }
 
-sub pack_dst { # packs lightweight satellite repos
+sub satellite_done {
         my ($fgrp) = @_;
-        pack_refs($fgrp, $fgrp->{cur_dst});
+        if (my $create = delete $fgrp->{-create_refs}) {
+                my $w = start_update_ref($fgrp) or return;
+                while (my ($ref, $oid) = each %$create) {
+                        upr($fgrp->{lei}, $w, 'create', $ref, $oid);
+                }
+                $w->close or upref_warn();
+        } else {
+                pack_refs($fgrp, $fgrp->{cur_dst});
+                run_puh($fgrp);
+        }
 }
 
 sub pack_refs {
         my ($self, $git_dir) = @_;
-        my $cmd = [ 'git', "--git-dir=$git_dir", qw(pack-refs --all --prune) ];
+        my $cmd = [git_exe, "--git-dir=$git_dir", qw(pack-refs --all --prune)];
         start_cmd($self, $cmd, { 2 => $self->{lei}->{2} });
 }
 
+sub unlink_fetch_head ($) {
+        my ($git_dir) = @_;
+        return if CORE::unlink("$git_dir/FETCH_HEAD") || $!{ENOENT};
+        warn "W: unlink($git_dir/FETCH_HEAD): $!";
+}
+
 sub fgrpv_done {
         my ($fgrpv) = @_;
         return if !$LIVE;
         my $first = $fgrpv->[0] // die 'BUG: no fgrpv->[0]';
         return if !keep_going($first);
+        unlink_fetch_head($first->{-osdir}) if !$first->{dry_run};
         pack_refs($first, $first->{-osdir}); # objstore refs always packed
         for my $fgrp (@$fgrpv) {
                 my $rn = $fgrp->{-remote};
                 my %opt = ( 2 => $fgrp->{lei}->{2} );
-
-                my $update_ref = PublicInbox::OnDestroy->new($$,
-                                                        \&fgrp_update, $fgrp);
-
-                my $src = [ 'git', "--git-dir=$fgrp->{-osdir}", 'for-each-ref',
+                my $update_ref = on_destroy \&fgrp_update, $fgrp;
+                my $src = [ git_exe, "--git-dir=$fgrp->{-osdir}",
+                        'for-each-ref',
                         "--format=refs/%(refname:lstrip=3)%00%(objectname)",
                         "refs/remotes/$rn/" ];
-                open(my $sfh, '+>', undef) or die "open(src): $!";
+                open(my $sfh, '+>', undef);
                 $fgrp->{srcfh} = $sfh;
                 start_cmd($fgrp, $src, { %opt, 1 => $sfh }, $update_ref);
-                my $dst = [ 'git', "--git-dir=$fgrp->{cur_dst}", 'for-each-ref',
-                        '--format=%(refname)%00%(objectname)' ];
-                open(my $dfh, '+>', undef) or die "open(dst): $!";
+                my $dst = [ git_exe, "--git-dir=$fgrp->{cur_dst}",
+                        'for-each-ref', '--format=%(refname)%00%(objectname)' ];
+                open(my $dfh, '+>', undef);
                 $fgrp->{dstfh} = $dfh;
                 start_cmd($fgrp, $dst, { %opt, 1 => $dfh }, $update_ref);
         }
@@ -368,43 +401,72 @@ sub fgrp_fetch_all {
         # system argv limits:
         my $grp = 'fgrptmp';
 
-        my @git = (@{$self->{-torsocks}}, 'git');
+        my @git = (@{$self->{-torsocks}}, git_exe);
         my $j = $self->{lei}->{opt}->{jobs};
         my $opt = {};
         my @fetch = do {
                 local $self->{lei}->{opt}->{jobs} = 1;
-                (fetch_args($self->{lei}, $opt),
-                        qw(--no-tags --multiple));
+                (fetch_args($self->{lei}, $opt), qw(--no-tags --multiple));
         };
         push(@fetch, "-j$j") if $j;
-        while (my ($osdir, $fgrpv) = each %$todo) {
+        while (my ($osdir, $fgrp_old_new) = each %$todo) {
                 my $f = "$osdir/config";
                 return if !keep_going($self);
+                my ($old, $new) = @$fgrp_old_new;
+                @$old = sort { $b->{-sort} <=> $a->{-sort} } @$old;
+                # $new is ordered by {references}
+                my $cmd = [ git_exe, "--git-dir=$osdir", qw(config -f), $f ];
+
+                # clobber settings from previous run atomically
+                for ("remotes.$grp", 'fetch.hideRefs') {
+                        my $c = [ @$cmd, '--unset-all', $_ ];
+                        $self->{lei}->qerr("# @$c");
+                        next if $self->{dry_run};
+                        run_wait($c, undef, $opt);
+                        die "E: @$c \$?=$?" if ($? && ($? >> 8) != 5);
+                }
 
-                # clobber group from previous run atomically
-                my $cmd = ['git', "--git-dir=$osdir", qw(config -f),
-                                $f, '--unset-all', "remotes.$grp"];
-                $self->{lei}->qerr("# @$cmd");
-                if (!$self->{dry_run}) {
-                        my $pid = spawn($cmd, undef, { 2 => $self->{lei}->{2} });
-                        waitpid($pid, 0) // die "waitpid: $!";
-                        die "E: @$cmd: \$?=$?" if ($? && ($? >> 8) != 5);
+                # permanent configs:
+                my $cfg = PublicInbox::Config->git_config_dump($f);
+                for my $fgrp (@$old, @$new) {
+                        my $u = $fgrp->{-uri} // die 'BUG: no {-uri}';
+                        my $rn = $fgrp->{-remote} // die 'BUG: no {-remote}';
+                        for ("url=$u", "fetch=+refs/*:refs/remotes/$rn/*",
+                                        'tagopt=--no-tags') {
+                                my ($k, $v) = split(/=/, $_, 2);
+                                $k = "remote.$rn.$k";
+                                next if ($cfg->{$k} // '') eq $v;
+                                my $c = [@$cmd, $k, $v];
+                                $fgrp->{lei}->qerr("# @$c");
+                                next if $fgrp->{dry_run};
+                                run_die($c, undef, $opt);
+                        }
+                }
 
+                if (!$self->{dry_run}) {
                         # update the config atomically via O_APPEND while
                         # respecting git-config locking
-                        sysopen(my $lk, "$f.lock", O_CREAT|O_EXCL|O_WRONLY)
-                                or die "open($f.lock): $!";
-                        open my $fh, '>>', $f or die "open(>>$f): $!";
+                        sysopen(my $lk, "$f.lock", O_CREAT|O_EXCL|O_WRONLY);
+                        open my $fh, '>>', $f;
                         $fh->autoflush(1);
-                        my $buf = join('', "[remotes]\n",
-                                map { "\t$grp = $_->{-remote}\n" } @$fgrpv);
+                        my $buf = '';
+                        if (@$old) {
+                                $buf = "[fetch]\n\thideRefs = refs\n";
+                                $buf .= join('', map {
+                                        "\thideRefs = !refs/remotes/" .
+                                                "$_->{-remote}/\n";
+                                } @$old);
+                        }
+                        $buf .= join('', "[remotes]\n",
+                                (map { "\t$grp = $_->{-remote}\n" } @$old),
+                                (map { "\t$grp = $_->{-remote}\n" } @$new));
                         print $fh $buf or die "print($f): $!";
-                        close $fh or die "close($f): $!";
-                        unlink("$f.lock") or die "unlink($f.lock): $!";
+                        close $fh;
+                        unlink("$f.lock");
                 }
-
-                $cmd = [ @git, "--git-dir=$osdir", @fetch, $grp ];
-                my $end = PublicInbox::OnDestroy->new($$, \&fgrpv_done, $fgrpv);
+                $cmd  = [ @git, "--git-dir=$osdir", @fetch, $grp ];
+                push @$old, @$new;
+                my $end = on_destroy \&fgrpv_done, $old;
                 start_cmd($self, $cmd, $opt, $end);
         }
 }
@@ -418,20 +480,18 @@ sub forkgroup_prep {
         my $dir = "$os/$fg.git";
         if (!-d $dir && !$self->{dry_run}) {
                 PublicInbox::Import::init_bare($dir);
-                my @cmd = ('git', "--git-dir=$dir", 'config');
-                my $opt = { 2 => $self->{lei}->{2} };
-                for ('repack.useDeltaIslands=true',
-                                'pack.island=refs/remotes/([^/]+)/') {
-                        run_die([@cmd, split(/=/, $_, 2)], undef, $opt);
-                }
+                write_file '+>>', "$dir/config", <<EOM;
+[repack]
+        useDeltaIslands = true
+[pack]
+        island = refs/remotes/([^/]+)/
+EOM
         }
         my $key = $self->{-key} // die 'BUG: no -key';
         my $rn = substr(sha256_hex($key), 0, 16);
         if (!-d $self->{cur_dst} && !$self->{dry_run}) {
                 PublicInbox::Import::init_bare($self->{cur_dst});
-                my $f = "$self->{cur_dst}/config";
-                open my $fh, '+>>', $f or die "open:($f): $!";
-                print $fh <<EOM or die "print($f): $!";
+                write_file '+>>', "$self->{cur_dst}/config", <<EOM;
 ; rely on the "$rn" remote in the
 ; $fg fork group for fetches
 ; only uncomment the following iff you detach from fork groups
@@ -440,20 +500,16 @@ sub forkgroup_prep {
 ;        fetch = +refs/*:refs/*
 ;        mirror = true
 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): $!";
+                open my $fh, '+>>', my $f = "$o/info/alternates";
+                seek($fh, 0, SEEK_SET); # Perl did SEEK_END when it saw '>>'
+                my $seen = grep /\A\Q$l\E\n/, PublicInbox::IO::read_all $fh;
+                print $fh "$l\n" if !$seen;
+                close $fh;
         }
         bless {
                 %$self, -osdir => $dir, -remote => $rn, -uri => $uri
@@ -461,13 +517,17 @@ EOM
 }
 
 sub fp_done {
-        my ($self, $cb, @arg) = @_;
+        my ($self, $cmd, $cb, @arg) = @_;
+        if ($?) {
+                $self->{lei}->err("@$cmd failed (\$?=$?) (non-fatal)");
+                $? = 0; # don't let it influence normal exit
+        }
         return if !keep_going($self);
         my $fh = delete $self->{-show_ref} // die 'BUG: no show-ref output';
-        seek($fh, SEEK_SET, 0) or die "seek(show_ref): $!";
+        sysseek($fh, 0, SEEK_SET);
         $self->{-ent} // die 'BUG: no -ent';
         my $A = $self->{-ent}->{fingerprint} // die 'BUG: no fingerprint';
-        my $B = sha1_hex(do { local $/; <$fh> } // die("read(show_ref): $!"));
+        my $B = sha_all(1, $fh)->hexdigest;
         return $cb->($self, @arg) if $A ne $B;
         $self->{lei}->qerr("# $self->{-key} up-to-date");
 }
@@ -483,48 +543,40 @@ sub cmp_fp_do {
                 return if $cur_ent->{fingerprint} eq $new;
         }
         my $dst = $self->{cur_dst} // $self->{dst};
-        my $cmd = ['git', "--git-dir=$dst", 'show-ref'];
+        my $cmd = [git_exe, "--git-dir=$dst", 'show-ref'];
         my $opt = { 2 => $self->{lei}->{2} };
-        open($opt->{1}, '+>', undef) or die "open(tmp): $!";
+        open($opt->{1}, '+>', undef);
         $self->{-show_ref} = $opt->{1};
-        my $done = PublicInbox::OnDestroy->new($$, \&fp_done, $self, $cb, @arg);
-        start_cmd($self, $cmd, $opt, $done);
+        do_reap($self);
+        $self->{lei}->qerr("# @$cmd");
+        $LIVE->{spawn($cmd, undef, $opt)} = [ \&fp_done, $self, $cmd,
+                                                $cb, @arg ];
 }
 
 sub resume_fetch {
         my ($self, $uri, $fini) = @_;
         return if !keep_going($self);
         my $dst = $self->{cur_dst} // $self->{dst};
-        my @git = ('git', "--git-dir=$dst");
+        my @git = (git_exe, "--git-dir=$dst");
         my $opt = { 2 => $self->{lei}->{2} };
-        my $rn = 'origin'; # configurable?
+        my $rn = 'random'.int(rand(1 << 30));
         for ("url=$uri", "fetch=+refs/*:refs/*", 'mirror=true') {
-                my @kv = split(/=/, $_, 2);
-                $kv[0] = "remote.$rn.$kv[0]";
-                next if $self->{dry_run};
-                run_die([@git, 'config', @kv], undef, $opt);
+                push @git, '-c', "remote.$rn.$_";
         }
         my $cmd = [ @{$self->{-torsocks}}, @git,
                         fetch_args($self->{lei}, $opt), $rn ];
         push @$cmd, '-P' if $self->{lei}->{prune}; # --prune-tags implied
-        start_cmd($self, $cmd, $opt, $fini);
+        my $run_puh = on_destroy \&run_puh, $self, $fini;
+        ++$self->{chg}->{nr_chg};
+        start_cmd($self, $cmd, $opt, $run_puh);
 }
 
 sub fgrp_enqueue {
         my ($fgrp, $end) = @_; # $end calls fgrp_fetch_all
         return if !keep_going($fgrp);
-        my $opt = { 2 => $fgrp->{lei}->{2} };
-        # --no-tags is required to avoid conflicts
-        my $u = $fgrp->{-uri} // die 'BUG: no {-uri}';
-        my $rn = $fgrp->{-remote} // die 'BUG: no {-remote}';
-        my @cmd = ('git', "--git-dir=$fgrp->{-osdir}", 'config');
-        for ("url=$u", "fetch=+refs/*:refs/remotes/$rn/*", 'tagopt=--no-tags') {
-                my @kv = split(/=/, $_, 2);
-                $kv[0] = "remote.$rn.$kv[0]";
-                $fgrp->{dry_run} ? $fgrp->{lei}->qerr("# @cmd @kv") :
-                                run_die([@cmd, @kv], undef, $opt);
-        }
-        push @{$FGRP_TODO->{$fgrp->{-osdir}}}, $fgrp;
+        ++$fgrp->{chg}->{nr_chg};
+        my $dst = $FGRP_TODO->{$fgrp->{-osdir}} //= [ [], [] ]; # [ old, new ]
+        push @{$dst->[defined($fgrp->{-sort} ? 0 : 1)]}, $fgrp;
 }
 
 sub clone_v1 {
@@ -538,12 +590,23 @@ sub clone_v1 {
                 die "$uri is a v1 inbox, --epoch is not supported\n";
         $self->{-torsocks} //= $curl->torsocks($lei, $uri) or return;
         my $dst = $self->{cur_dst} // $self->{dst};
-        my $fini = PublicInbox::OnDestroy->new($$, \&v1_done, $self);
         my $resume = -d $dst;
+        if ($resume) { # respect read-only cloned w/ --epoch=
+                my @st = stat(_); # for root
+                if (!-w _ || !($st[2] & 0222)) {
+                        warn "# skipping $dst, not writable\n";
+                        return;
+                }
+        }
+        my $fini = on_destroy \&v1_done, $self;
         if (my $fgrp = forkgroup_prep($self, $uri)) {
                 $fgrp->{-fini} = $fini;
-                $resume ? cmp_fp_do($fgrp, \&fgrp_enqueue, $end)
-                        : fgrp_enqueue($fgrp, $end);
+                if ($resume) {
+                        $fgrp->{-sort} = $fgrp->{-ent}->{modified};
+                        cmp_fp_do($fgrp, \&fgrp_enqueue, $end);
+                } else { # new repo, save for last
+                        fgrp_enqueue($fgrp, $end);
+                }
         } elsif ($resume) {
                 cmp_fp_do($self, \&resume_fetch, $uri, $fini);
         } else { # normal clone
@@ -556,15 +619,18 @@ sub clone_v1 {
                                                 "$self->{dst}$ref";
                         }
                 }
-                start_cmd($self, $cmd, $opt, $fini);
+                ++$self->{chg}->{nr_chg};
+                start_cmd($self, $cmd, $opt,
+                        on_destroy(\&run_puh, $self, $fini));
         }
         if (!$self->{-is_epoch} && $lei->{opt}->{'inbox-config'} =~
-                                /\A(?:always|v1)\z/s) {
+                                /\A(?:always|v1)\z/s &&
+                        !-f "$dst/inbox.config.example") {
                 _get_txt_start($self, '_/text/config/raw', $fini);
         }
 
         my $d = $self->{-ent} ? $self->{-ent}->{description} : undef;
-        $self->{'txt.description'} = $d if defined $d;
+        utf8::encode($self->{'txt.description'} = $d) if defined $d;
         (!defined($d) && !$end) and
                 _get_txt_start($self, 'description', $fini);
 
@@ -617,9 +683,11 @@ EOM
 sub init_placeholder ($$$) {
         my ($src, $edst, $ent) = @_;
         PublicInbox::Import::init_bare($edst);
-        my $f = "$edst/config";
-        open my $fh, '>>', $f or die "open($f): $!";
-        print $fh <<EOM or die "print($f): $!";
+        my @owner = defined($ent->{owner}) ? (<<EOM) : ();
+[gitweb]
+        owner = $ent->{owner}
+EOM
+        write_file '>>', "$edst/config", <<EOM, @owner;
 [remote "origin"]
         url = $src
         fetch = +refs/*:refs/*
@@ -629,21 +697,11 @@ sub init_placeholder ($$$) {
 ; will not fetch updates for it unless write permission is added.
 ; Hint: chmod +w $edst
 EOM
-        if (defined($ent->{owner})) {
-                print $fh <<EOM or die "print($f): $!";
-[gitweb]
-        owner = $ent->{owner}
-EOM
-        }
-        close $fh or die "close($f): $!";
         my %map = (head => 'HEAD', description => undef);
         while (my ($key, $fn) = each %map) {
                 my $val = $ent->{$key} // next;
                 $fn //= $key;
-                $fn = "$edst/$fn";
-                open $fh, '>', $fn or die "open($fn): $!";
-                print $fh $val, "\n" or die "print($fn): $!";
-                close $fh or die "close($fn): $!";
+                write_file '>', "$edst/$fn", $val;
         }
 }
 
@@ -658,10 +716,10 @@ sub up_fp_done {
         my ($self) = @_;
         return if !keep_going($self);
         my $fh = delete $self->{-show_ref_up} // die 'BUG: no show-ref output';
-        seek($fh, SEEK_SET, 0) or die "seek(show_ref): $!";
+        sysseek($fh, 0, SEEK_SET);
         $self->{-ent} // die 'BUG: no -ent';
         my $A = $self->{-ent}->{fingerprint} // die 'BUG: no fingerprint';
-        my $B = sha1_hex(do { local $/; <$fh> } // die("read(show_ref): $!"));
+        my $B = sha_all(1, $fh)->hexdigest;
         return if $A eq $B;
         $self->{-ent}->{fingerprint} = $B;
         push @{$self->{chg}->{fp_mismatch}}, $self->{-key};
@@ -670,11 +728,27 @@ sub up_fp_done {
 sub atomic_write ($$$) {
         my ($dn, $bn, $raw) = @_;
         my $ft = File::Temp->new(DIR => $dn, TEMPLATE => "$bn-XXXX");
-        print $ft $raw or die "print($ft): $!";
+        print $ft $raw;
         $ft->flush or die "flush($ft): $!";
         ft_rename($ft, "$dn/$bn", 0666);
 }
 
+sub run_next_puh {
+        my ($self) = @_;
+        my $puh = shift @{$self->{-puh_todo}} // return delete($self->{-fini});
+        my $fini = on_destroy \&run_next_puh, $self;
+        my $cmd = [ @$puh, ($self->{cur_dst} // $self->{dst}) ];
+        my $opt = +{ map { $_ => $self->{lei}->{$_} } (0..2) };
+        start_cmd($self, $cmd, undef, $opt, $fini);
+}
+
+sub run_puh {
+        my ($self, $fini) = @_;
+        $self->{-fini} = $fini;
+        @{$self->{-puh_todo}} = @PUH;
+        run_next_puh($self);
+}
+
 # modifies the to-be-written manifest entry, and sets values from it, too
 sub update_ent {
         my ($self) = @_;
@@ -683,18 +757,18 @@ sub update_ent {
         my $cur = $self->{-local_manifest}->{$key}->{fingerprint} // "\0";
         my $dst = $self->{cur_dst} // $self->{dst};
         if (defined($new) && $new ne $cur) {
-                my $cmd = ['git', "--git-dir=$dst", 'show-ref'];
+                my $cmd = [git_exe, "--git-dir=$dst", 'show-ref'];
                 my $opt = { 2 => $self->{lei}->{2} };
-                open($opt->{1}, '+>', undef) or die "open(tmp): $!";
+                open($opt->{1}, '+>', undef);
                 $self->{-show_ref_up} = $opt->{1};
-                my $done = PublicInbox::OnDestroy->new($$, \&up_fp_done, $self);
+                my $done = on_destroy \&up_fp_done, $self;
                 start_cmd($self, $cmd, $opt, $done);
         }
         $new = $self->{-ent}->{head};
         $cur = $self->{-local_manifest}->{$key}->{head} // "\0";
         if (defined($new) && $new ne $cur) {
                 # n.b. grokmirror writes raw contents to $dst/HEAD w/o locking
-                my $cmd = [ 'git', "--git-dir=$dst" ];
+                my $cmd = [ git_exe, "--git-dir=$dst" ];
                 if ($new =~ s/\Aref: //) {
                         push @$cmd, qw(symbolic-ref HEAD), $new;
                 } elsif ($new =~ /\A[a-f0-9]{40,}\z/) {
@@ -707,6 +781,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;
@@ -717,17 +792,18 @@ sub update_ent {
                         if (lstat($ln)) {
                                 if (-l _) {
                                         next if readlink($ln) eq $tgt;
-                                        unlink($ln) or die "unlink($ln): $!";
+                                        unlink($ln);
                                 } else {
                                         push @{$self->{chg}->{badlink}}, $p;
                                 }
                         }
-                        symlink($tgt, $ln) or die "symlink($tgt, $ln): $!";
+                        symlink($tgt, $ln);
+                        ++$self->{chg}->{nr_chg};
                 }
         }
         if (defined(my $t = $self->{-ent}->{modified})) {
                 my ($dn, $bn) = ("$dst/info/web", 'last-modified');
-                my $orig = PublicInbox::Git::try_cat("$dn/$bn");
+                my $orig = PublicInbox::IO::try_cat("$dn/$bn");
                 $t = strftime('%F %T', gmtime($t))." +0000\n";
                 File::Path::mkpath($dn);
                 atomic_write($dn, $bn, $t) if $orig ne $t;
@@ -736,7 +812,9 @@ sub update_ent {
         $new = $self->{-ent}->{owner} // return;
         $cur = $self->{-local_manifest}->{$key}->{owner} // "\0";
         return if $cur eq $new;
-        my $cmd = [ qw(git config -f), "$dst/config", 'gitweb.owner', $new ];
+        utf8::encode($new); # to octets
+        my $cmd = [ git_exe, qw(config -f), "$dst/config",
+                        'gitweb.owner', $new ];
         start_cmd($self, $cmd, { 2 => $self->{lei}->{2} });
 }
 
@@ -745,9 +823,10 @@ sub v1_done { # called via OnDestroy
         return if $self->{dry_run} || !keep_going($self);
         _write_inbox_config($self);
         my $dst = $self->{cur_dst} // $self->{dst};
+        unlink_fetch_head($dst);
         update_ent($self) if $self->{-ent};
         my $o = "$dst/objects";
-        if (open(my $fh, '<', my $fn = "$o/info/alternates")) {;
+        if (CORE::open(my $fh, '<', my $fn = "$o/info/alternates")) {;
                 my $base = File::Spec->rel2abs($o);
                 my @l = <$fh>;
                 my $ft;
@@ -758,7 +837,7 @@ sub v1_done { # called via OnDestroy
                                                 DIR => "$o/info");
                 }
                 if ($ft) {
-                        print $ft @l or die "print($ft): $!";
+                        print $ft @l;
                         $ft->flush or die "flush($ft): $!";
                         ft_rename($ft, $fn, 0666, $fh);
                 }
@@ -776,8 +855,8 @@ sub v2_done { # called via OnDestroy
         return if $self->{dry_run} || !keep_going($self);
         my $dst = $self->{cur_dst} // $self->{dst};
         require PublicInbox::Lock;
-        my $lk = bless { lock_path => "$dst/inbox.lock" }, 'PublicInbox::Lock';
-        my $lck = $lk->lock_for_scope($$);
+        my $lk = PublicInbox::Lock->new("$dst/inbox.lock");
+        my $lck = $lk->lock_for_scope;
         _write_inbox_config($self);
         require PublicInbox::MultiGit;
         my $mg = PublicInbox::MultiGit->new($dst, 'all.git', 'git');
@@ -785,7 +864,7 @@ sub v2_done { # called via OnDestroy
         for my $i ($mg->git_epochs) { $mg->epoch_cfg_set($i) }
         for my $edst (@{delete($self->{-read_only}) // []}) {
                 my @st = stat($edst) or die "stat($edst): $!";
-                chmod($st[2] & 0555, $edst) or die "chmod(a-w, $edst): $!";
+                chmod($st[2] & 0555, $edst);
         }
         write_makefile($dst, 2);
         undef $lck; # unlock
@@ -804,7 +883,7 @@ sub clone_v2_prep ($$;$) {
         my $want = parse_epochs($lei->{opt}->{epoch}, $v2_epochs);
         my $task = $m ? bless { %$self }, __PACKAGE__ : $self;
         my (@skip, $desc);
-        my $fini = PublicInbox::OnDestroy->new($$, \&v2_done, $task);
+        my $fini = on_destroy \&v2_done, $task;
         for my $nr (sort { $a <=> $b } keys %$v2_epochs) {
                 my ($uri, $key) = @{$v2_epochs->{$nr}};
                 my $src = $uri->as_string;
@@ -841,10 +920,12 @@ failed to extract epoch number from $src
         # filter out the epochs we skipped
         $self->{chg}->{manifest} = 1 if $m && delete(@$m{@skip});
 
-        (!$self->{dry_run} && !-d $dst) and File::Path::mkpath($dst);
+        $self->{dry_run} or File::Path::mkpath($dst);
 
-        $lei->{opt}->{'inbox-config'} =~ /\A(?:always|v2)\z/s and
+        if ($lei->{opt}->{'inbox-config'} =~ /\A(?:always|v2)\z/s &&
+                        !-f "$dst/inbox.config.example") {
                 _get_txt_start($task, '_/text/config/raw', $fini);
+        }
 
         defined($desc) ? ($task->{'txt.description'} = $desc) :
                 _get_txt_start($task, 'description', $fini);
@@ -853,7 +934,7 @@ failed to extract epoch number from $src
 sub decode_manifest ($$$) {
         my ($fh, $fn, $uri) = @_;
         my $js;
-        my $gz = do { local $/; <$fh> } // die "slurp($fn): $!";
+        my $gz = PublicInbox::IO::read_all $fh;
         gunzip(\$gz => \$js, MultiStream => 1) or
                 die "gunzip($uri): $GunzipError\n";
         my $m = eval { PublicInbox::Config->json->decode($js) };
@@ -865,13 +946,14 @@ sub decode_manifest ($$$) {
 sub load_current_manifest ($) {
         my ($self) = @_;
         my $fn = $self->{-manifest} // return;
-        if (open(my $fh, '<', $fn)) {
+        if (CORE::open(my $fh, '<', $fn)) {
                 decode_manifest($fh, $fn, $fn);
         } elsif ($!{ENOENT}) { # non-fatal, we can just do it slowly
-                warn "open($fn): $!\n" if !$self->{-initial_clone};
+                $self->{-initial_clone} or
+                        warn "W: open($fn): $! (non-fatal)\n";
                 undef;
         } else {
-                die "open($fn): $!\n";
+                die "E: open($fn): $!\n";
         }
 }
 
@@ -893,7 +975,7 @@ sub multi_inbox ($$$) {
         my @orig = defined($incl // $excl) ? (keys %$v2, @v1) : ();
         if (defined $incl) {
                 my $re = '(?:'.join('\\z|', map {
-                                $self->{lei}->glob2re($_) // qr/\A\Q$_\E/
+                                glob2re($_) // qr/\A\Q$_\E/
                         } @$incl).'\\z)';
                 my @gone = delete @$v2{grep(!/$re/, keys %$v2)};
                 delete @$m{map { @$_ } @gone} and $self->{chg}->{manifest} = 1;
@@ -902,7 +984,7 @@ sub multi_inbox ($$$) {
         }
         if (defined $excl) {
                 my $re = '(?:'.join('\\z|', map {
-                                $self->{lei}->glob2re($_) // qr/\A\Q$_\E/
+                                glob2re($_) // qr/\A\Q$_\E/
                         } @$excl).'\\z)';
                 my @gone = delete @$v2{grep(/$re/, keys %$v2)};
                 delete @$m{map { @$_ } @gone} and $self->{chg}->{manifest} = 1;
@@ -936,7 +1018,7 @@ sub clone_all {
         my ($self, $m) = @_;
         my $todo = $TODO;
         $TODO = \'BUG on further use';
-        my $end = PublicInbox::OnDestroy->new($$, \&fgrp_fetch_all, $self);
+        my $end = on_destroy \&fgrp_fetch_all, $self;
         {
                 my $nodep = delete $todo->{''};
 
@@ -959,9 +1041,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
@@ -986,8 +1072,8 @@ sub dump_manifest ($$) {
         # epoch they no longer want to skip
         my $json = PublicInbox::Config->json->encode($m);
         my $mtime = (stat($ft))[9];
-        seek($ft, SEEK_SET, 0) or die "seek($ft): $!";
-        truncate($ft, 0) or die "truncate($ft): $!";
+        seek($ft, 0, SEEK_SET);
+        truncate($ft, 0);
         gzip(\$json => $ft) or die "gzip($ft): $GzipError";
         $ft->flush or die "flush($ft): $!";
         utime($mtime, $mtime, "$ft") or die "utime(..., $ft): $!";
@@ -995,30 +1081,50 @@ sub dump_manifest ($$) {
 
 sub dump_project_list ($$) {
         my ($self, $m) = @_;
-        my $f = $self->{'-project-list'} // return;
-        my $old = PublicInbox::Git::try_cat($f);
+        my $f = $self->{'-project-list'};
+        my $old = defined($f) ? PublicInbox::IO::try_cat($f) : '';
         my %new;
 
-        open my $dh, '<', '.' or die "open(.): $!";
-        chdir($self->{dst}) or die "chdir($self->{dst}): $!";
+        open my $dh, '<', '.';
+        if (!$self->{dry_run} || -d $self->{dst}) {
+                chdir($self->{dst});
+        }
         my @local = grep { -e $_ ? ($new{$_} = undef) : 1 } split(/\n/s, $old);
-        chdir($dh) or die "chdir(restore): $!";
+        chdir($dh);
 
         $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;
+        if (@remote) {
+                warn <<EOM;
 The following local repositories are ignored/gone from $self->{src}:
 EOM
-        warn "\t", $_, "\n" for @remote;
-        warn <<EOM if @local;
+                warn "\t", $_, "\n" for @remote;
+
+                if ($self->{lei}->{opt}->{purge} && !$self->{dry_run}) {
+                        my $o = {};
+                        $o->{verbose} = 1 if $self->{lei}->{opt}->{verbose};
+                        my $dst = $self->{dst};
+                        File::Path::remove_tree(map { "$dst/$_" } @remote, $o);
+                        my %rm = map { $_ => undef } @remote;
+                        @list = grep { !exists($rm{$_}) } @list;
+                        $self->{lei}->qerr('# purged ');
+                }
+        }
+        if (defined($f) && @local) {
+                warn <<EOM;
 The following repos in $f no longer exist on the filesystem:
 EOM
-        warn "\t", $_, "\n" for @local;
-
+                warn "\t", $_, "\n" for @local;
+        }
+        $self->{chg}->{nr_chg} += scalar(@remote) + scalar(@local);
+        return if !defined($f) || $self->{dry_run};
         my (undef, $dn, $bn) = File::Spec->splitpath($f);
-        atomic_write($dn, $bn, join("\n", @list, ''));
+        my $new = join("\n", @list, '');
+        atomic_write($dn, $bn, $new) if $new ne $old;
 }
 
 # FIXME: this gets confused by single inbox instance w/ global manifest.js.gz
@@ -1030,7 +1136,13 @@ sub try_manifest {
         $self->{-torsocks} //= $curl->torsocks($lei, $uri) or return;
         my $path = $uri->path;
         chop($path) eq '/' or die "BUG: $uri not canonicalized";
-        $uri->path($path . '/manifest.js.gz');
+        my $rmf = $lei->{opt}->{'remote-manifest'} // '/manifest.js.gz';
+        if ($rmf =~ m!\A[^/:]+://!) {
+                $uri = URI->new($rmf);
+        } else {
+                $rmf = "/$rmf" if index($rmf, '/') != 0;
+                $uri->path($path.$rmf);
+        }
         my $manifest = $self->{-manifest} // "$self->{dst}/manifest.js.gz";
         my %opt = (UNLINK => 1, SUFFIX => '.tmp', TMPDIR => 1);
         if (!$self->{dry_run} && $manifest =~ m!\A(.+?)/[^/]+\z! and -d $1) {
@@ -1049,7 +1161,10 @@ sub try_manifest {
         }
 
         # bail out if curl -z/--timecond hit 304 Not Modified, $ft will be empty
-        return $lei->qerr("# $manifest unchanged") if -f $manifest && !-s $ft;
+        if (-f $manifest && !-s $ft) {
+                $lei->child_error(127 << 8) if $lei->{opt}->{'exit-code'};
+                return $lei->qerr("# $manifest unchanged");
+        }
 
         my $m = eval { decode_manifest($ft, $ft, $uri) };
         if ($@) {
@@ -1058,8 +1173,9 @@ 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);
+        return $lei->child_error(0, $multi) if !ref($multi);
         my $v2 = delete $multi->{v2};
         if ($v2) {
                 for my $name (sort keys %$v2) {
@@ -1112,13 +1228,12 @@ EOM
         }
         delete local $lei->{opt}->{epoch} if defined($v2);
         clone_all($self, $m);
-        return if $self->{dry_run} || !keep_going($self);
+        return if !keep_going($self);
 
         # set by clone_v2_prep/-I/--exclude
         my $mis = delete $self->{chg}->{fp_mismatch};
         if ($mis) {
-                my $t = (stat($ft))[9];
-                $t = strftime('%F %k:%M:%S %z', localtime($t));
+                my $t = fmt_localtime((stat($ft))[9]);
                 warn <<EOM;
 W: Fingerprints for the following repositories do not match
 W: $mf_url @ $t:
@@ -1129,13 +1244,17 @@ EOM
 W: The above fingerprints may never match without --prune
 EOM
         }
-        dump_manifest($m => $ft) if delete($self->{chg}->{manifest}) || $mis;
+        if ((delete($self->{chg}->{manifest}) || $mis) && !$self->{dry_run}) {
+                dump_manifest($m => $ft);
+        }
         my $bad = delete $self->{chg}->{badlink};
         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);
+        ft_rename($ft, $manifest, 0666) if !$self->{dry_run};
+        !$self->{chg}->{nr_chg} && $lei->{opt}->{'exit-code'} and
+                $lei->child_error(127 << 8);
 }
 
 sub start_clone_url {
@@ -1150,6 +1269,11 @@ sub do_mirror { # via wq_io_do or public-inbox-clone
         $self->{dry_run} = 1 if $lei->{opt}->{'dry-run'};
         umask($lei->{client_umask}) if defined $lei->{client_umask};
         $self->{-initial_clone} = 1 if !-d $self->{dst};
+        local @PUH;
+        if (defined(my $puh = $lei->{opt}->{'post-update-hook'})) {
+                require Text::ParseWords;
+                @PUH = map { [ Text::ParseWords::shellwords($_) ] } @$puh;
+        }
         eval {
                 my $ic = $lei->{opt}->{'inbox-config'} //= 'always';
                 $ic =~ /\A(?:v1|v2|always|never)\z/s or die <<"";
@@ -1202,8 +1326,8 @@ sub ipc_atfork_child {
 sub write_makefile {
         my ($dir, $ibx_ver) = @_;
         my $f = "$dir/Makefile";
-        if (sysopen my $fh, $f, O_CREAT|O_EXCL|O_WRONLY) {
-                print $fh <<EOM or die "print($f) $!";
+        if (CORE::sysopen my $fh, $f, O_CREAT|O_EXCL|O_WRONLY) {
+                print $fh <<EOM;
 # This is a v$ibx_ver public-inbox, see the public-inbox-v$ibx_ver-format(5)
 # manpage for more information on the format.  This Makefile is
 # intended as a familiar wrapper for users unfamiliar with
@@ -1217,7 +1341,7 @@ sub write_makefile {
 # so you may edit it freely with your own convenience targets
 # and notes.  public-inbox-fetch will recreate it if removed.
 EOM
-                print $fh <<'EOM' or die "print($f): $!";
+                print $fh <<'EOM';
 # the default target:
 help :
         @echo Common targets:
@@ -1227,7 +1351,7 @@ help :
         @echo Rarely needed targets:
         @echo '    make reindex      - may be needed for new features/bugfixes'
         @echo '    make compact      - rewrite Xapian storage to save space'
-        @echo '    make index        - initial index after clone
+        @echo '    make index        - initial index after clone'
 
 fetch :
         public-inbox-fetch
@@ -1253,7 +1377,7 @@ compact :
 
 .PHONY : help fetch update index reindex compact
 EOM
-                close $fh or die "close($f): $!";
+                close $fh;
         } else {
                 die "open($f): $!" unless $!{EEXIST};
         }