From ab42b22acfd9670df0b61f514b4fb89fe7754846 Mon Sep 17 00:00:00 2001
From: Eric Wong <e@80x24.org>
Date: Mon, 19 Apr 2021 08:52:13 +0000
Subject: [PATCH] config: git_config_dump blesses

I don't know if it's worth it to sub (or super)class
PublicInbox::Config into something more generic for
lei, but this change simplifies a good chunk of lei
code that reuses the public-inbox config parsing.
---
 lib/PublicInbox/Config.pm         | 10 +++++-----
 lib/PublicInbox/LEI.pm            |  3 +--
 lib/PublicInbox/LeiMirror.pm      |  2 +-
 lib/PublicInbox/LeiSavedSearch.pm |  7 ++-----
 4 files changed, 9 insertions(+), 13 deletions(-)

diff --git a/lib/PublicInbox/Config.pm b/lib/PublicInbox/Config.pm
index 603dad98..016f50ec 100644
--- a/lib/PublicInbox/Config.pm
+++ b/lib/PublicInbox/Config.pm
@@ -24,11 +24,11 @@ sub new {
 	if (ref($file) eq 'SCALAR') { # used by some tests
 		open my $fh, '<', $file or die;  # PerlIO::scalar
 		$self = config_fh_parse($fh, "\n", '=');
+		bless $self, $class;
 	} else {
-		$self = git_config_dump($file);
+		$self = git_config_dump($class, $file);
 		$self->{'-f'} = $file;
 	}
-	bless $self, $class;
 	# caches
 	$self->{-by_addr} = {};
 	$self->{-by_list_id} = {};
@@ -158,13 +158,13 @@ sub config_fh_parse ($$$) {
 }
 
 sub git_config_dump {
-	my ($file) = @_;
-	return {} unless -e $file;
+	my ($class, $file) = @_;
+	return bless {}, $class unless -e $file;
 	my $cmd = [ qw(git config -z -l --includes), "--file=$file" ];
 	my $fh = popen_rd($cmd);
 	my $rv = config_fh_parse($fh, "\0", "\n");
 	close $fh or die "failed to close (@$cmd) pipe: $?";
-	$rv;
+	bless $rv, $class;
 }
 
 sub valid_foo_name ($;$) {
diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index 56640be1..f641f0d9 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -700,8 +700,7 @@ sub _lei_cfg ($;$) {
 		$cur_st = pack('dd', $st[10], $st[7]);
 		qerr($self, "# $f created") if $self->{cmd} ne 'config';
 	}
-	my $cfg = PublicInbox::Config::git_config_dump($f);
-	bless $cfg, 'PublicInbox::Config';
+	my $cfg = PublicInbox::Config->git_config_dump($f);
 	$cfg->{-st} = $cur_st;
 	$cfg->{'-f'} = $f;
 	if ($sto && File::Spec->canonpath($sto_dir // store_path($self))
diff --git a/lib/PublicInbox/LeiMirror.pm b/lib/PublicInbox/LeiMirror.pm
index 89574d28..15adb71b 100644
--- a/lib/PublicInbox/LeiMirror.pm
+++ b/lib/PublicInbox/LeiMirror.pm
@@ -110,7 +110,7 @@ sub _try_config {
 	}
 	return $lei->err("# @$cmd failed (non-fatal)") if $cerr;
 	rename($f, $ce) or return $lei->err("link($f, $ce): $! (non-fatal)");
-	my $cfg = PublicInbox::Config::git_config_dump($f);
+	my $cfg = PublicInbox::Config->git_config_dump($f);
 	my $ibx = $self->{ibx} = {};
 	for my $sec (grep(/\Apublicinbox\./, @{$cfg->{-section_order}})) {
 		for (qw(address newsgroup nntpmirror)) {
diff --git a/lib/PublicInbox/LeiSavedSearch.pm b/lib/PublicInbox/LeiSavedSearch.pm
index 19f4aa5f..d3a32d36 100644
--- a/lib/PublicInbox/LeiSavedSearch.pm
+++ b/lib/PublicInbox/LeiSavedSearch.pm
@@ -56,9 +56,8 @@ sub list {
 		print $fh "\tpath = ", cquote_val($p), "\n";
 	}
 	close $fh or die "close $f: $!";
-	my $cfg = PublicInbox::Config::git_config_dump($f);
+	my $cfg = PublicInbox::Config->git_config_dump($f);
 	unlink($f);
-	bless $cfg, 'PublicInbox::Config';
 	my $out = $cfg->get_all('lei.q.output') or return ();
 	map {;
 		s!\A(?:maildir|mh|mbox.+|mmdf):!!i;
@@ -76,7 +75,6 @@ sub new {
 		output2lssdir($self, $lei, \$dir, \$f) or
 			return $lei->fail("--save was not used with $dst cwd=".
 						$lei->rel2abs('.'));
-		$self->{-cfg} //= PublicInbox::Config::git_config_dump($f);
 		$self->{'-f'} = $f;
 	} else { # new saved search "lei q --save"
 		$dst = $lei->{ovv}->{dst};
@@ -113,7 +111,6 @@ EOM
 		}
 		close($fh) or return $lei->fail("close $f: $!");
 	}
-	bless $self->{-cfg}, 'PublicInbox::Config';
 	$self->{lock_path} = "$self->{-f}.flock";
 	$self->{-ovf} = "$dir/over.sqlite3";
 	$self;
@@ -198,7 +195,7 @@ sub output2lssdir {
 	my $dir = lss_dir_for($lei, \$dst);
 	my $f = "$dir/lei.saved-search";
 	if (-f $f && -r _) {
-		$self->{-cfg} = PublicInbox::Config::git_config_dump($f);
+		$self->{-cfg} = PublicInbox::Config->git_config_dump($f);
 		$$dir_ref = $dir;
 		$$fn_ref = $f;
 		return 1;
-- 
2.50.0