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=1befbd547c793da857949cd13775a75838293cec

commit 1befbd547c793da857949cd13775a75838293cec
Author: Guillem Jover <[email protected]>
AuthorDate: Thu Jan 27 01:42:48 2022 +0100

    Dpkg::Source::Package: Only generate the patch header if needed
    
    When we are generating a patch we need to generate a header too, but
    only if the patch is going to contain actual differences. Otherwise
    we end up parsing the changelog unnecessarily.
    
    Prompted-by: Umut <[email protected]> (on IRC)
---
 scripts/Dpkg/Source/Package/V2.pm | 18 ++++++++++--------
 scripts/Dpkg/Source/Patch.pm      | 12 +++++++++++-
 2 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/scripts/Dpkg/Source/Package/V2.pm 
b/scripts/Dpkg/Source/Package/V2.pm
index a45837b40..96e917001 100644
--- a/scripts/Dpkg/Source/Package/V2.pm
+++ b/scripts/Dpkg/Source/Package/V2.pm
@@ -479,14 +479,16 @@ sub _generate_patch {
     my $diff = Dpkg::Source::Patch->new(filename => $tmpdiff,
                                         compression => 'none');
     $diff->create();
-    if ($opts{header_from} and -e $opts{header_from}) {
-        my $header_from = Dpkg::Source::Patch->new(
-            filename => $opts{header_from});
-        my $analysis = $header_from->analyze($dir, verbose => 0);
-        $diff->set_header($analysis->{patchheader});
-    } else {
-        $diff->set_header($self->_get_patch_header($dir));
-    }
+    $diff->set_header(sub {
+        if ($opts{header_from} and -e $opts{header_from}) {
+            my $header_from = Dpkg::Source::Patch->new(
+                filename => $opts{header_from});
+            my $analysis = $header_from->analyze($dir, verbose => 0);
+            return $analysis->{patchheader};
+        } else {
+            return $self->_get_patch_header($dir);
+        }
+    });
     $diff->add_diff_directory($tmp, $dir, basedirname => $basedirname,
             %{$self->{diff_options}},
             handle_binary_func => $opts{handle_binary},
diff --git a/scripts/Dpkg/Source/Patch.pm b/scripts/Dpkg/Source/Patch.pm
index 25d56335d..58255c42e 100644
--- a/scripts/Dpkg/Source/Patch.pm
+++ b/scripts/Dpkg/Source/Patch.pm
@@ -62,6 +62,16 @@ sub set_header {
     *$self->{header} = $header;
 }
 
+sub get_header {
+    my $self = shift;
+
+    if (ref *$self->{header} eq 'CODE') {
+        return *$self->{header}->();
+    } else {
+        return *$self->{header};
+    }
+}
+
 sub add_diff_file {
     my ($self, $old, $new, %opts) = @_;
     $opts{include_timestamp} //= 0;
@@ -125,7 +135,7 @@ sub add_diff_file {
             error(g_("unknown line from diff -u on %s: '%s'"), $new, $_);
         }
        if (*$self->{empty} and defined(*$self->{header})) {
-           $self->print(*$self->{header}) or syserr(g_('failed to write'));
+           $self->print($self->get_header()) or syserr(g_('failed to write'));
            *$self->{empty} = 0;
        }
         print { $self } $_ or syserr(g_('failed to write'));

-- 
Dpkg.Org's dpkg

Reply via email to