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 3/6] config: handle key-only entries as booleans
Date: Sun, 24 Sep 2023 05:42:11 +0000	[thread overview]
Message-ID: <20230924054214.3539091-4-e@80x24.org> (raw)
In-Reply-To: <20230924054214.3539091-1-e@80x24.org>

It's how git-config works, so our `git config --list' parser
must be able to handle it.  Fortunately this doesn't seem to
incur a measurable overhead when parsing a config with 50k
inboxes.
---
 lib/PublicInbox/Config.pm |  5 ++++-
 t/config.t                | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/lib/PublicInbox/Config.pm b/lib/PublicInbox/Config.pm
index 0a6b210f..f6236d84 100644
--- a/lib/PublicInbox/Config.pm
+++ b/lib/PublicInbox/Config.pm
@@ -151,8 +151,11 @@ sub config_fh_parse ($$$) {
 	local $/ = $rs;
 	while (defined($line = <$fh>)) { # perf critical with giant configs
 		$i = index($line, $fs);
+		# $i may be -1 if $fs not found and it's a key-only entry
+		# (meaning boolean true).  Either way the -1 will drop the
+		# $rs either from $k or $v.
 		$k = substr($line, 0, $i);
-		$v = substr($line, $i + 1, -1); # chop off $fs
+		$v = $i >= 0 ? substr($line, $i + 1, -1) : 1;
 		$section = substr($k, 0, rindex($k, '.'));
 		$seen{$section} //= push(@section_order, $section);
 
diff --git a/t/config.t b/t/config.t
index 80f214cd..8a27a920 100644
--- a/t/config.t
+++ b/t/config.t
@@ -7,6 +7,25 @@ use_ok 'PublicInbox';
 ok(defined(eval('$PublicInbox::VERSION')), 'VERSION defined');
 use_ok 'PublicInbox::Config';
 my ($tmpdir, $for_destroy) = tmpdir();
+use autodie qw(open close);
+my $validate_git_behavior = $ENV{TEST_VALIDATE_GIT_BEHAVIOR};
+
+{
+	my $f = "$tmpdir/bool_config";
+	open my $fh, '>', $f;
+	print $fh <<EOM;
+[imap]
+	debug
+	port = 2
+EOM
+	close $fh;
+	my $cfg = PublicInbox::Config->git_config_dump($f);
+	$validate_git_behavior and
+		is(xqx([qw(git config -f), $f, qw(--bool imap.debug)]),
+			"true\n", 'git handles key-only as truth');
+	ok($cfg->git_bool($cfg->{'imap.debug'}), 'key-only value handled');
+	is($cfg->{'imap.port'}, 2, 'normal k=v read after key-only');
+}
 
 {
 	PublicInbox::Import::init_bare($tmpdir);

  parent 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 ` [PATCH 1/6] lei: check git-config(1) failures Eric Wong
2023-09-24  5:42 ` [PATCH 2/6] lei view_text: used tied ProcessPipe for `git config' Eric Wong
2023-09-24  5:42 ` Eric Wong [this message]
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-4-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).