From e07f113726f46b8ab921ceecd4e282d93b39c2a2 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 9 Jul 2017 14:12:47 +0100 Subject: [PATCH] Dgit.pm: git_cat_file: Introduce $etype parameter No functional change for existing callers. Signed-off-by: Ian Jackson --- Debian/Dgit.pm | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Debian/Dgit.pm b/Debian/Dgit.pm index f299450b..abcf1236 100644 --- a/Debian/Dgit.pm +++ b/Debian/Dgit.pm @@ -348,11 +348,21 @@ sub git_rev_parse ($) { return cmdoutput qw(git rev-parse), "$_[0]~0"; } -sub git_cat_file ($) { - my ($objname) = @_; +sub git_cat_file ($;$) { + my ($objname, $etype) = @_; # => ($type, $data) or ('missing', undef) # in scalar context, just the data + # if $etype defined, dies unless type is $etype or in @$etype our ($gcf_pid, $gcf_i, $gcf_o); + my $chk = sub { + my ($gtype, $data) = @_; + if ($etype) { + $etype = [$etype] unless ref $etype; + confess "$objname expected @$etype but is $gtype" + unless grep { $gtype eq $_ } @$etype; + } + return ($gtype, $data); + }; if (!$gcf_pid) { my @cmd = qw(git cat-file --batch); debugcmd "GCF|", @cmd; @@ -362,13 +372,13 @@ sub git_cat_file ($) { print $gcf_i $objname, "\n" or die $!; my $x = <$gcf_o>; printdebug "GCF<| ", $x; - if ($x =~ m/ (missing)$/) { return ($1, undef); } + if ($x =~ m/ (missing)$/) { return $chk->($1, undef); } my ($type, $size) = $x =~ m/^.* (\w+) (\d+)\n/ or die "$objname ?"; my $data; (read $gcf_o, $data, $size) == $size or die "$objname $!"; $x = <$gcf_o>; $x eq "\n" or die "$objname ($_) $!"; - return ($type, $data); + return $chk->($type, $data); } sub git_for_each_ref ($$;$) { -- 2.30.2