This is an automated email from the git hooks/post-receive script.

guillem pushed a commit to branch main
in repository dpkg.

View the commit online:
https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=15a70e502505b3001670a7fe9ff6073289b9d416

commit 15a70e502505b3001670a7fe9ff6073289b9d416 (HEAD -> main)
Author: Guillem Jover <guil...@debian.org>
AuthorDate: Mon Jan 25 05:33:10 2021 +0100

    Dpkg::Control::FieldsCore: Add new field_parse_binary_source()
    
    This function parses the Source field from binary package control
    stanzas.
    
    Closes: #980527
---
 scripts/Dpkg/Control/FieldsCore.pm | 49 +++++++++++++++++++++++++++++++++++++-
 scripts/t/Dpkg_Control_Fields.t    | 26 +++++++++++++++++++-
 2 files changed, 73 insertions(+), 2 deletions(-)

diff --git a/scripts/Dpkg/Control/FieldsCore.pm 
b/scripts/Dpkg/Control/FieldsCore.pm
index 255786fce..1d21a03c2 100644
--- a/scripts/Dpkg/Control/FieldsCore.pm
+++ b/scripts/Dpkg/Control/FieldsCore.pm
@@ -18,13 +18,14 @@ package Dpkg::Control::FieldsCore;
 use strict;
 use warnings;
 
-our $VERSION = '1.00';
+our $VERSION = '1.01';
 our @EXPORT = qw(
     field_capitalize
     field_is_official
     field_is_allowed_in
     field_transfer_single
     field_transfer_all
+    field_parse_binary_source
     field_list_src_dep
     field_list_pkg_dep
     field_get_dep_type
@@ -844,6 +845,48 @@ sub field_ordered_list($) {
     return ();
 }
 
+=item ($source, $version) = field_parse_binary_source($ctrl)
+
+Parse the B<Source> field in a binary package control stanza. The field
+contains the source package name where it was built from, and optionally
+a space and the source version enclosed in parenthesis if it is different
+from the binary version.
+
+Returns a list with the $source name, and the source $version, or undef
+or an empty list when $ctrl does not contain a binary package control stanza.
+Neither $source nor $version are validated, but that can be done with
+Dpkg::Package::pkg_name_is_illegal() and Dpkg::Version::version_check().
+
+=cut
+
+sub field_parse_binary_source($) {
+    my $ctrl = shift;
+    my $ctrl_type = $ctrl->get_type();
+
+    if ($ctrl_type != CTRL_INDEX_PKG and
+        $ctrl_type != CTRL_PKG_DEB and
+        $ctrl_type != CTRL_FILE_STATUS) {
+        return;
+    }
+
+    my ($source, $version);
+
+    if (exists $ctrl->{'Source'}) {
+        $source = $ctrl->{'Source'};
+        if ($source =~ m/^([^ ]+) +\(([^)]*)\)$/) {
+            $source = $1;
+            $version = $2;
+        } else {
+            $version = $ctrl->{'Version'};
+        }
+    } else {
+        $source = $ctrl->{'Package'};
+        $version = $ctrl->{'Version'};
+    }
+
+    return ($source, $version);
+}
+
 =item field_list_src_dep()
 
 List of fields that contains dependencies-like information in a source
@@ -967,6 +1010,10 @@ sub field_insert_before($$@) {
 
 =head1 CHANGES
 
+=head2 Version 1.01 (dpkg 1.21.0)
+
+New function: field_parse_binary_source().
+
 =head2 Version 1.00 (dpkg 1.17.0)
 
 Mark the module as public.
diff --git a/scripts/t/Dpkg_Control_Fields.t b/scripts/t/Dpkg_Control_Fields.t
index 84d548a87..8076ef0ba 100644
--- a/scripts/t/Dpkg_Control_Fields.t
+++ b/scripts/t/Dpkg_Control_Fields.t
@@ -20,10 +20,11 @@ use Test::More;
 use Test::Dpkg qw(:paths);
 
 BEGIN {
-    plan tests => 2460;
+    plan tests => 2467;
 
     use_ok('Dpkg::Control::Types');
     use_ok('Dpkg::Control::FieldsCore');
+    use_ok('Dpkg::Control');
 }
 
 #my $datadir = test_get_data_path();
@@ -251,3 +252,26 @@ foreach my $type (sort keys %fields) {
         }
     }
 }
+
+# Check deb822 field parsers
+
+my $ctrl = Dpkg::Control->new(type => CTRL_PKG_DEB);
+
+my ($source, $version);
+
+$ctrl->{Package} = 'test-binary';
+$ctrl->{Version} = '2.0-1';
+$ctrl->{Source} = 'test-source (1.0)';
+($source, $version) = field_parse_binary_source($ctrl);
+is($source, 'test-source', 'Source package from binary w/ Source field');
+is($version, '1.0', 'Source version from binary w/ Source field');
+
+$ctrl->{Source} = 'test-source';
+($source, $version) = field_parse_binary_source($ctrl);
+is($source, 'test-source', 'Source package from binary w/ Source field w/o 
version');
+is($version, '2.0-1', 'Source version from binary w/ Source field w/o 
version');
+
+delete $ctrl->{Source};
+($source, $version) = field_parse_binary_source($ctrl);
+is($source, 'test-binary', 'Source package from binary w/o Source field');
+is($version, '2.0-1', 'Source version from binary w/o Source field');

-- 
Dpkg.Org's dpkg

Reply via email to