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] import: hoist out _check_path function
Date: Thu,  8 Sep 2016 20:05:03 +0000	[thread overview]
Message-ID: <20160908200503.4935-1-e@80x24.org> (raw)

This reduces duplication, slightly.  We may be using it
yet again in a to-be-introduced function (or we may not
introduce it).
---
 lib/PublicInbox/Import.pm | 37 ++++++++++++++++++-------------------
 1 file changed, 18 insertions(+), 19 deletions(-)

diff --git a/lib/PublicInbox/Import.pm b/lib/PublicInbox/Import.pm
index c2beb19..09dd38d 100644
--- a/lib/PublicInbox/Import.pm
+++ b/lib/PublicInbox/Import.pm
@@ -75,6 +75,15 @@ sub norm_body ($) {
 	$b
 }
 
+sub _check_path ($$$$) {
+	my ($r, $w, $tip, $path) = @_;
+	return if $tip eq '';
+	print $w "ls $tip $path\n" or wfail;
+	local $/ = "\n";
+	defined(my $info = <$r>) or die "EOF from fast-import: $!";
+	$info =~ /\Amissing / ? undef : $info;
+}
+
 # returns undef on non-existent
 # ('MISMATCH', msg) on mismatch
 # (:MARK, msg) on success
@@ -86,20 +95,16 @@ sub remove {
 
 	my ($r, $w) = $self->gfi_start;
 	my $tip = $self->{tip};
-	return ('MISSING', undef) if $tip eq '';
-
-	print $w "ls $tip $path\n" or wfail;
-	local $/ = "\n";
-	my $check = <$r>;
-	defined $check or die "EOF from fast-import / ls: $!";
-	return ('MISSING', undef) if $check =~ /\Amissing /;
-	$check =~ m!\A100644 blob ([a-f0-9]{40})\t!s or die "not blob: $check";
+	my $info = _check_path($r, $w, $tip, $path) or return ('MISSING',undef);
+	$info =~ m!\A100644 blob ([a-f0-9]{40})\t!s or die "not blob: $info";
 	my $blob = $1;
+
 	print $w "cat-blob $blob\n" or wfail;
-	$check = <$r>;
-	defined $check or die "EOF from fast-import / cat-blob: $!";
-	$check =~ /\A[a-f0-9]{40} blob (\d+)\n\z/ or
-				die "unexpected cat-blob response: $check";
+	local $/ = "\n";
+	$info = <$r>;
+	defined $info or die "EOF from fast-import / cat-blob: $!";
+	$info =~ /\A[a-f0-9]{40} blob (\d+)\n\z/ or
+				die "unexpected cat-blob response: $info";
 	my $left = $1;
 	my $offset = 0;
 	my $buf = '';
@@ -162,13 +167,7 @@ sub add {
 
 	my ($r, $w) = $self->gfi_start;
 	my $tip = $self->{tip};
-	if ($tip ne '') {
-		print $w "ls $tip $path\n" or wfail;
-		local $/ = "\n";
-		my $check = <$r>;
-		defined $check or die "EOF from fast-import: $!";
-		return unless $check =~ /\Amissing /;
-	}
+	_check_path($r, $w, $tip, $path) and return;
 
 	# kill potentially confusing/misleading headers
 	$mime->header_set($_) for qw(bytes lines content-length status);
-- 
EW


                 reply	other threads:[~2016-09-08 20:05 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20160908200503.4935-1-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).