From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS200651 185.100.86.0/24 X-Spam-Status: No, score=-0.9 required=3.0 tests=AWL,BAYES_00, RCVD_IN_MSPIKE_BL,RCVD_IN_MSPIKE_ZBI,RCVD_IN_XBL,RDNS_NONE,SPF_FAIL, SPF_HELO_FAIL shortcircuit=no autolearn=no autolearn_force=no version=3.4.0 Received: from 80x24.org (unknown [185.100.86.86]) by dcvr.yhbt.net (Postfix) with ESMTP id CE2F82018E for ; Thu, 18 Aug 2016 02:07:05 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH 2/2] import: support setting blobs of arbitrary type Date: Thu, 18 Aug 2016 02:07:01 +0000 Message-Id: <20160818020701.16665-2-e@80x24.org> In-Reply-To: <20160818020701.16665-1-e@80x24.org> References: <20160818020701.16665-1-e@80x24.org> List-Id: 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