user/dev discussion of public-inbox itself
 help / Atom feed
* [PATCH] import: hoist out _check_path function
@ 2016-09-08 20:05 Eric Wong
  0 siblings, 0 replies; 1+ messages in thread
From: Eric Wong @ 2016-09-08 20:05 UTC (permalink / raw)
  To: meta

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


^ permalink raw reply	[flat|threaded] 1+ messages in thread

only message in thread, back to index

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-08 20:05 [PATCH] import: hoist out _check_path function Eric Wong

user/dev discussion of public-inbox itself

Archives are clonable:
	git clone --mirror http://public-inbox.org/meta
	git clone --mirror http://czquwvybam4bgbro.onion/meta
	git clone --mirror http://hjrcffqmbrq6wope.onion/meta
	git clone --mirror http://ou63pmih66umazou.onion/meta

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.mail.public-inbox.meta
	nntp://ou63pmih66umazou.onion/inbox.comp.mail.public-inbox.meta
	nntp://czquwvybam4bgbro.onion/inbox.comp.mail.public-inbox.meta
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.mail.public-inbox.meta
	nntp://news.gmane.org/gmane.mail.public-inbox.general

 note: .onion URLs require Tor: https://www.torproject.org/
       or Tor2web: https://www.tor2web.org/

AGPL code for this site: git clone https://public-inbox.org/ public-inbox