unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 1/6] lei: check git-config(1) failures
Date: Sun, 24 Sep 2023 05:42:09 +0000	[thread overview]
Message-ID: <20230924054214.3539091-2-e@80x24.org> (raw)
In-Reply-To: <20230924054214.3539091-1-e@80x24.org>

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	other threads:[~2023-09-24  5:42 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-24  5:42 [PATCH 0/6] lei config fixes and improvements Eric Wong
2023-09-24  5:42 ` Eric Wong [this message]
2023-09-24  5:42 ` [PATCH 2/6] lei view_text: used tied ProcessPipe for `git config' Eric Wong
2023-09-24  5:42 ` [PATCH 3/6] config: handle key-only entries as booleans Eric Wong
2023-09-24  5:42 ` [PATCH 4/6] lei config: send `git config' errors to pager Eric Wong
2023-09-24  5:42 ` [PATCH 5/6] lei: fix `-c NAME=VALUE' config support Eric Wong
2023-09-24  5:42 ` [PATCH 6/6] config: drop scalar ref support from internal API Eric Wong
2023-09-24  9:50   ` [SQUASH 7/6] t/config: fix missing coderepo.<PROJECT>.dir entry Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230924054214.3539091-2-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).