]> Sergey Matveev's repositories - public-inbox.git/blobdiff - script/public-inbox-edit
edit: pass global variables into subs
[public-inbox.git] / script / public-inbox-edit
index 16d785294f49bcb79a62620af2a5548437ba508c..c98840531fd674c1716aeb0a1aed0b77746d725f 100755 (executable)
@@ -46,9 +46,9 @@ PublicInbox::AdminEdit::check_editable(\@ibxs);
 
 my $found = {}; # cid => [ [ibx, smsg] [, [ibx, smsg] ] ]
 
-sub find_mid ($) {
-       my ($mid) = @_;
-       foreach my $ibx (@ibxs) {
+sub find_mid ($$$) {
+       my ($found, $mid, $ibxs) = @_;
+       foreach my $ibx (@$ibxs) {
                my $over = $ibx->over;
                my ($id, $prev);
                while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
@@ -65,10 +65,11 @@ sub find_mid ($) {
 
 sub show_cmd ($$) {
        my ($ibx, $smsg) = @_;
-       " GIT_DIR=$ibx->{mainrepo}/all.git \\\n    git show $smsg->{blob}\n";
+       " GIT_DIR=$ibx->{inboxdir}/all.git \\\n    git show $smsg->{blob}\n";
 }
 
-sub show_found () {
+sub show_found ($) {
+       my ($found) = @_;
        foreach my $to_edit (values %$found) {
                foreach my $tuple (@$to_edit) {
                        my ($ibx, $smsg) = @$tuple;
@@ -79,7 +80,7 @@ sub show_found () {
 
 if (defined($mid)) {
        $mid = mid_clean($mid);
-       $found = find_mid($mid);
+       find_mid($found, $mid, \@ibxs);
        my $nr = scalar(keys %$found);
        die "No message found for <$mid>\n" unless $nr;
        if ($nr > 1) {
@@ -87,7 +88,7 @@ if (defined($mid)) {
 Multiple messages with different content found matching
 <$mid>:
 
-               show_found();
+               show_found($found);
                die "Use --force to edit all of them\n" if !$opt->{force};
                warn "Will edit all of them\n";
        }
@@ -96,7 +97,7 @@ Multiple messages with different content found matching
        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($found, $_, \@ibxs) for (@$mids); # populates $found
        my $cid = content_id($mime);
        my $to_edit = $found->{$cid};
        unless ($to_edit) {
@@ -106,7 +107,7 @@ Multiple messages with different content found matching
 $nr matches to Message-ID(s) in $file, but none matched content
 Partial matches below:
 
-                       show_found();
+                       show_found($found);
                } elsif ($nr == 0) {
                        $mids = join('', map { "  <$_>\n" } @$mids);
                        warn <<"";
@@ -121,7 +122,7 @@ $mids
 
 my $tmpl = 'public-inbox-edit-XXXXXX';
 foreach my $to_edit (values %$found) {
-       my ($edit_fh, $edit_fn) = tempfile($tmpl, TMPDIR => 1);
+       my ($edit_fh, $edit_fn) = tempfile($tmpl, TMPDIR => 1, UNLINK => 1);
        $edit_fh->autoflush(1);
        my ($ibx, $smsg) = @{$to_edit->[0]};
        my $old_raw = $ibx->msg_by_smsg($smsg);
@@ -130,7 +131,8 @@ foreach my $to_edit (values %$found) {
        my $tmp = $$old_raw;
        if (!$opt->{raw}) {
                my $oid = $smsg->{blob};
-               print $edit_fh "From mboxrd\@$oid Thu Jan  1 00:00:00 1970\n";
+               print $edit_fh "From mboxrd\@$oid Thu Jan  1 00:00:00 1970\n"
+                       or die "failed to write From_ line: $!";
                $tmp =~ s/^(>*From )/>$1/gm;
        }
        print $edit_fh $tmp or
@@ -138,7 +140,7 @@ foreach my $to_edit (values %$found) {
 
        # run the editor, respecting spaces/quote
 retry_edit:
-       if (system(qw(sh -c), qq(eval "$editor" '"\$@"'), '--', $edit_fn)) {
+       if (system(qw(sh -c), $editor.' "$@"', $editor, $edit_fn)) {
                if (!(-t STDIN) && !$opt->{force}) {
                        die "E: $editor failed: $?\n";
                }
@@ -149,7 +151,11 @@ retry_edit:
                        chomp(my $op = <STDIN> || '');
                        $op = lc($op);
                        goto retry_edit if $op eq 'r';
-                       exit $? if $op eq 'q';
+                       if ($op eq 'q') {
+                               # n.b. we'll lose the exit signal, here,
+                               # oh well; "q" is user-specified anyways.
+                               exit($? >> 8);
+                       }
                        last if $op eq 'c'; # continuing
                        print STDERR "\`$op' not recognized\n";
                }