2020-2021 were bad times and I somehow got deluded into
believing git-config(1) would always succeed :x
---
 lib/PublicInbox/LEI.pm               | 9 ++++++---
 lib/PublicInbox/LeiAddWatch.pm       | 7 ++++---
 lib/PublicInbox/LeiForgetExternal.pm | 3 +--
 lib/PublicInbox/LeiInit.pm           | 4 ++--
 lib/PublicInbox/LeiRmWatch.pm        | 2 +-
 5 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index 368f9357..a6d92eec 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -776,9 +776,8 @@ EOM
                /\A([^=\.]+\.[^=]+)(?:=(.*))?\z/ or return fail($self, <<EOM);
 `-c $_' is not of the form -c <name>=<value>'
 EOM
-               my $name = $1;
-               my $value = $2 // 1;
-               _config($self, '--add', $name, $value);
+               my ($name, $value) = ($1, $2 // 1);
+               _config($self, '--add', $name, $value) or return;
                if (defined(my $v = $tmp->{$name})) {
                        if (ref($v) eq 'ARRAY') {
                                push @$v, $value;
@@ -894,13 +893,17 @@ sub _lei_store ($;$) {
        };
 }
 
+# returns true on success, undef
+# argv[0] eq `+e' means errors do not ->fail # (like `sh +e')
 sub _config {
        my ($self, @argv) = @_;
+       my $err_ok = ($argv[0] // '') eq '+e' ? shift(@argv) : undef;
        my %env = (%{$self->{env}}, GIT_CONFIG => undef);
        my $cfg = _lei_cfg($self, 1);
        my $cmd = [ qw(git config -f), $cfg->{'-f'}, @argv ];
        my %rdr = map { $_ => $self->{$_} } (0..2);
        waitpid(spawn($cmd, \%env, \%rdr), 0);
+       $? == 0 ? 1 : ($err_ok ? undef : fail($self, $?));
 }
 
 sub lei_daemon_pid { puts shift, $$ }
diff --git a/lib/PublicInbox/LeiAddWatch.pm b/lib/PublicInbox/LeiAddWatch.pm
index 97e7a342..f61e2de4 100644
--- a/lib/PublicInbox/LeiAddWatch.pm
+++ b/lib/PublicInbox/LeiAddWatch.pm
@@ -26,13 +26,14 @@ sub lei_add_watch {
        for my $w (@{$self->{inputs}}) {
                # clobber existing, allow multiple
                if (defined($vmd0)) {
-                       $lei->_config("watch.$w.vmd", '--replace-all', $vmd0);
+                       $lei->_config("watch.$w.vmd", '--replace-all', $vmd0)
+                               or return;
                        for my $v (@vmd) {
-                               $lei->_config("watch.$w.vmd", $v);
+                               $lei->_config("watch.$w.vmd", $v) or return;
                        }
                }
                next if defined $cfg->{"watch.$w.state"};
-               $lei->_config("watch.$w.state", $state);
+               $lei->_config("watch.$w.state", $state) or return;
        }
        $lei->_lei_store(1); # create
        $lei->lms(1)->lms_write_prepare->add_folders(@{$self->{inputs}});
diff --git a/lib/PublicInbox/LeiForgetExternal.pm 
b/lib/PublicInbox/LeiForgetExternal.pm
index 39bfc60b..c8d1df38 100644
--- a/lib/PublicInbox/LeiForgetExternal.pm
+++ b/lib/PublicInbox/LeiForgetExternal.pm
@@ -16,8 +16,7 @@ sub lei_forget_external {
                        next if $seen{$l}++;
                        my $key = "external.$l.boost";
                        delete($cfg->{$key});
-                       $lei->_config('--unset', $key);
-                       if ($? == 0) {
+                       if ($lei->_config('+e', '--unset', $key)) {
                                $lei->qerr("# $l forgotten ");
                        } elsif (($? >> 8) == 5) {
                                warn("# $l not found\n");
diff --git a/lib/PublicInbox/LeiInit.pm b/lib/PublicInbox/LeiInit.pm
index 27ce8169..94897e61 100644
--- a/lib/PublicInbox/LeiInit.pm
+++ b/lib/PublicInbox/LeiInit.pm
@@ -23,7 +23,7 @@ sub lei_init {
 
                # some folks like symlinks and bind mounts :P
                if (@dir && "@cur[1,0]" eq "@dir[1,0]") {
-                       $self->_config('leistore.dir', $dir);
+                       $self->_config('leistore.dir', $dir) or return;
                        $self->_lei_store(1)->done;
                        return $self->qerr("$exists (as $cur)");
                }
@@ -31,7 +31,7 @@ sub lei_init {
 E: leistore.dir=$cur already initialized and it is not $dir
 
        }
-       $self->_config('leistore.dir', $dir);
+       $self->_config('leistore.dir', $dir) or return;
        $self->_lei_store(1)->done;
        $exists //= "# leistore.dir=$dir newly initialized";
        $self->qerr($exists);
diff --git a/lib/PublicInbox/LeiRmWatch.pm b/lib/PublicInbox/LeiRmWatch.pm
index c0f336f0..19bee3ab 100644
--- a/lib/PublicInbox/LeiRmWatch.pm
+++ b/lib/PublicInbox/LeiRmWatch.pm
@@ -14,7 +14,7 @@ sub lei_rm_watch {
        my $self = bless { missing_ok => 1 }, __PACKAGE__;
        $self->prepare_inputs($lei, \@argv) or return;
        for my $w (@{$self->{inputs}}) {
-               $lei->_config('--remove-section', "watch.$w");
+               $lei->_config('--remove-section', "watch.$w") or return;
        }
        delete $lei->{cfg}; # force reload
        $lei->refresh_watches;

Reply via email to