dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH 1/2] import: hoist out _check_path function
@ 2016-08-18  2:07 Eric Wong
  2016-08-18  2:07 ` [PATCH 2/2] import: support setting blobs of arbitrary type Eric Wong
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Wong @ 2016-08-18  2:07 UTC (permalink / raw)
  To: spew

We will be using it again in a new function.
---
 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 related	[flat|nested] 2+ messages in thread

* [PATCH 2/2] import: support setting blobs of arbitrary type
  2016-08-18  2:07 [PATCH 1/2] import: hoist out _check_path function Eric Wong
@ 2016-08-18  2:07 ` Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2016-08-18  2:07 UTC (permalink / raw)
  To: spew

This allows adding arbitrary text files to the inbox
(perhaps help text or a README).
---
 lib/PublicInbox/Import.pm | 45 +++++++++++++++++++++++++++++++++++++++++++++
 t/import.t                | 24 ++++++++++++++++++++++++
 2 files changed, 69 insertions(+)

diff --git a/lib/PublicInbox/Import.pm b/lib/PublicInbox/Import.pm
index 09dd38d..e616af5 100644
--- a/lib/PublicInbox/Import.pm
+++ b/lib/PublicInbox/Import.pm
@@ -204,6 +204,51 @@ sub add {
 	$self->{tip} = ":$commit";
 }
 
+# mainly used for setting text/ or .mailmap files; coul
+sub set {
+	my ($self, $cmt_msg, $path, $valref) = @_;
+	my ($r, $w) = $self->gfi_start;
+	my $tip = $self->{tip};
+	my $mline;
+	if ($valref) {
+		my $blob = $self->{mark}++;
+		my $len = bytes::length($$valref);
+		print $w "blob\nmark :$blob\ndata $len\n" or wfail;
+		print $w $$valref or wfail;
+		$mline = "M 100644 :$blob $path";
+	} else { # remove existing blob
+		_check_path($r, $w, $tip, $path) or return;
+
+		$mline = "D $path";
+	}
+
+	$cmt_msg .= "\n" unless $cmt_msg =~ /\n\z/s;
+
+	my $ref = $self->{ref};
+	my $commit = $self->{mark}++;
+	my $parent = $tip =~ /\A:/ ? $tip : undef;
+	unless ($parent) {
+		print $w "reset $ref\n" or wfail;
+	}
+
+	# quiet down wide character warnings:
+	my $now = now2822();
+	my $ident = $self->{ident};
+	binmode $w, ':utf8' or die "binmode :utf8 failed: $!";
+	print $w "commit $ref\nmark :$commit\n",
+		"author $ident $now\n",
+		"committer $ident $now\n",
+		"data ", bytes::length($cmt_msg), "\n",
+		$cmt_msg, "\n" or wfail;
+	binmode $w, ':raw' or die "binmode :raw failed: $!";
+	if ($tip ne '') {
+		print $w 'from ', ($parent ? $parent : $tip), "\n" or wfail;
+	}
+	print $w $mline, "\n\n" or wfail;
+	$self->{nchg}++;
+	$self->{tip} = ":$commit";
+}
+
 sub done {
 	my ($self) = @_;
 	my $w = delete $self->{out} or return;
diff --git a/t/import.t b/t/import.t
index 73f92ad..5b6683f 100644
--- a/t/import.t
+++ b/t/import.t
@@ -66,4 +66,28 @@ is($im->add($mime, sub { undef }), undef, 'check callback fails');
 is($im->remove($mime), undef, 'message not added, so not removed');
 
 $im->done;
+
+# set tests
+{
+	my $ref;
+
+	ok($im->set('add', 'text/help', \('hello world')), 'add text');
+	$im->done;
+	$ref = $git->cat_file('HEAD:text/help');
+	is($$ref, 'hello world', 'content matches');
+
+	ok($im->set('mod', 'text/help', \('goodbye world')), 'modify');
+	$im->done;
+	$ref = $git->cat_file('HEAD:text/help');
+	is($$ref, 'goodbye world', 'content matches after modification');
+
+	ok($im->set('del', 'text/help'), 'remove text');
+	$im->done;
+	$ref = $git->cat_file('HEAD:text/help');
+	is($ref, undef, 'removed');
+
+	is($im->set('del again', 'text/help'), undef, 'remove idempotent');
+	$im->done;
+}
+
 done_testing();
-- 
EW


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-08-18  2:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-08-18  2:07 [PATCH 1/2] import: hoist out _check_path function Eric Wong
2016-08-18  2:07 ` [PATCH 2/2] import: support setting blobs of arbitrary type Eric Wong

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).