dumping ground for random patches and texts
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH 2/2] wip
Date: Sun,  3 Nov 2019 03:17:19 +0000	[thread overview]
Message-ID: <20191103031719.8845-2-e@80x24.org> (raw)
In-Reply-To: <20191103031719.8845-1-e@80x24.org>

---
 script/public-inbox-edit | 53 ++++++++++++++++++++--------------------
 1 file changed, 27 insertions(+), 26 deletions(-)

diff --git a/script/public-inbox-edit b/script/public-inbox-edit
index 2f47a170..13e74d9b 100755
--- a/script/public-inbox-edit
+++ b/script/public-inbox-edit
@@ -35,20 +35,20 @@ unless (defined $editor) {
 	}
 }
 
-my $mid = $opt->{mid};
-my $file = $opt->{file};
-if (defined $mid && defined $file) {
+if (defined($opt->{mid}) && defined($opt->{file})) {
 	die "the --mid and --file options are mutually exclusive\n";
 }
-
-my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt);
-PublicInbox::AdminEdit::check_editable(\@ibxs);
-
-my $found = {}; # cid => [ [ibx, smsg] [, [ibx, smsg] ] ]
-
-sub find_mid ($) {
-	my ($mid) = @_;
-	foreach my $ibx (@ibxs) {
+my $cmd_edit = {
+	found => {}, # cid => [ [ibx, smsg] [, [ibx, smsg] ] ]
+	ibxs => [ PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt) ],
+	mid => $opt->{mid},
+	file => $opt->{file},
+};
+PublicInbox::AdminEdit::check_editable($cmd_edit->{ibxs});
+
+sub find_mid ($$) {
+	my ($self, $mid) = @_;
+	foreach my $ibx (@{$self->{ibxs}}) {
 		my $over = $ibx->over;
 		my ($id, $prev);
 		while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
@@ -56,11 +56,10 @@ sub find_mid ($) {
 			my $mime = PublicInbox::MIME->new($ref);
 			my $cid = content_id($mime);
 			my $tuple = [ $ibx, $smsg ];
-			push @{$found->{$cid} ||= []}, $tuple
+			push @{$self->{found}->{$cid} //= []}, $tuple
 		}
 		delete @$ibx{qw(over mm git search)}; # cleanup
 	}
-	$found;
 }
 
 sub show_cmd ($$) {
@@ -68,8 +67,9 @@ sub show_cmd ($$) {
 	" GIT_DIR=$ibx->{inboxdir}/all.git \\\n    git show $smsg->{blob}\n";
 }
 
-sub show_found () {
-	foreach my $to_edit (values %$found) {
+sub show_found ($) {
+	my ($self) = @_;
+	foreach my $to_edit (values %{$self->{found}}) {
 		foreach my $tuple (@$to_edit) {
 			my ($ibx, $smsg) = @$tuple;
 			warn show_cmd($ibx, $smsg);
@@ -77,36 +77,37 @@ sub show_found () {
 	}
 }
 
-if (defined($mid)) {
+if (defined(my $mid = $cmd_edit->{mid})) {
 	$mid = mid_clean($mid);
-	$found = find_mid($mid);
-	my $nr = scalar(keys %$found);
+	find_mid($cmd_edit, $mid);
+	my $nr = scalar(keys %{$cmd_edit->{found}});
 	die "No message found for <$mid>\n" unless $nr;
 	if ($nr > 1) {
 		warn <<"";
 Multiple messages with different content found matching
 <$mid>:
 
-		show_found();
+		show_found($cmd_edit);
 		die "Use --force to edit all of them\n" if !$opt->{force};
 		warn "Will edit all of them\n";
 	}
 } else {
+	my $file = $cmd_edit->{file};
 	open my $fh, '<', $file or die "open($file) failed: $!";
 	my $orig = do { local $/; <$fh> };
 	my $mime = PublicInbox::MIME->new(\$orig);
 	my $mids = mids($mime->header_obj);
-	find_mid($_) for (@$mids); # populates $found
+	find_mid($cmd_edit, $_) for (@$mids); # populates {found}
 	my $cid = content_id($mime);
-	my $to_edit = $found->{$cid};
+	my $to_edit = $cmd_edit->{found}->{$cid};
 	unless ($to_edit) {
-		my $nr = scalar(keys %$found);
+		my $nr = scalar(keys %{$cmd_edit->{found}});
 		if ($nr > 0) {
 			warn <<"";
 $nr matches to Message-ID(s) in $file, but none matched content
 Partial matches below:
 
-			show_found();
+			show_found($cmd_edit);
 		} elsif ($nr == 0) {
 			$mids = join('', map { "  <$_>\n" } @$mids);
 			warn <<"";
@@ -116,7 +117,7 @@ $mids
 		}
 		exit 1;
 	}
-	$found = { $cid => $to_edit };
+	$cmd_edit->{found} = { $cid => $to_edit };
 }
 
 sub prompt_retry_continue_quit () {
@@ -147,7 +148,7 @@ Did you forget to purge the original message from the mbox after editing?
 }
 
 my $tmpl = 'public-inbox-edit-XXXXXX';
-foreach my $to_edit (values %$found) {
+foreach my $to_edit (values %{$cmd_edit->{found}}) {
 	my ($edit_fh, $edit_fn) = tempfile($tmpl, TMPDIR => 1, UNLINK => 1);
 	$edit_fh->autoflush(1);
 	my ($ibx, $smsg) = @{$to_edit->[0]};
-- 
EW


  reply	other threads:[~2019-11-03  3:17 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-03  3:17 [PATCH 1/2] edit: hoist out prompt functions into sub functions Eric Wong
2019-11-03  3:17 ` Eric Wong [this message]
  -- strict thread matches above, loose matches on Subject: below --
2023-05-13 22:16 [PATCH 1/2] search: add comments wrt codesearch, reduce ops Eric Wong
2023-05-13 22:16 ` [PATCH 2/2] WIP Eric Wong
2023-05-04 11:07 [PATCH 1/2] reuse open(2) from rb_file_load_ok on POSIX-like system Eric Wong
2023-05-04 11:07 ` [PATCH 2/2] wip Eric Wong
2023-04-17 23:06 [PATCH 1/2] reuse open(2) from rb_file_load_ok on POSIX-like system Eric Wong
2023-04-17 23:06 ` [PATCH 2/2] wip Eric Wong
2021-01-15 10:24 [PATCH 1/2] lei_to_mail: prepare Eric Wong
2021-01-15 10:24 ` [PATCH 2/2] wip Eric Wong
2020-03-31  6:42 [PATCH 1/2] v2writable: index Message-IDs w/ spaces properly Eric Wong
2020-03-31  6:42 ` [PATCH 2/2] WIP Eric Wong
2019-09-23  3:16 [PATCH 1/2] edit: hoist out prompt functions into sub functions ew
2019-09-23  3:16 ` [PATCH 2/2] wip ew
2019-01-07  8:02 [PATCH 1/2] hoist out resolve_repo_dir from -index Eric Wong
2019-01-07  8:02 ` [PATCH 2/2] wip Eric Wong
2018-09-09  8:35 [PATCH 1/2] cont.c (fiber_memsize): do not rely on ROOT_FIBER_CONTEXT Eric Wong
2018-09-09  8:35 ` [PATCH 2/2] wip Eric Wong
2018-09-09  8:00 [PATCH 1/2] test_fiber: show crash on GC after forking Eric Wong
2018-09-09  8:00 ` [PATCH 2/2] wip 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

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

  git send-email \
    --in-reply-to=20191103031719.8845-2-e@80x24.org \
    --to=e@80x24.org \
    --cc=spew@80x24.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).