Procházet zdrojové kódy

Dpkg::Source::Quilt: Refactor register() and unregister() methods

Refactor the code out from Dpkg::Source::Package::V3::Quilt into two new
methods that take care of registering and unregistering a patch from the
series and applied_patches in-core and on-disk lists.
Guillem Jover před 12 roky
rodič
revize
432295ab9d

+ 2 - 58
scripts/Dpkg/Source/Package/V3/Quilt.pm

@@ -221,47 +221,10 @@ sub check_patches_applied {
     $self->apply_patches($dir, usage => 'preparation', verbose => 1);
 }
 
-sub _load_file {
-    my ($file) = @_;
-
-    open my $file_fh, '<', $file or syserr(_g('cannot read %s'), $file);
-    my @lines = <$file_fh>;
-    close $file_fh;
-
-    return @lines;
-}
-
-sub _add_line {
-    my ($file, $line) = @_;
-
-    my @lines;
-    @lines = _load_file($file) if -f $file;
-    push @lines, $line;
-    chomp @lines;
-
-    open my $file_fh, '>', $file or syserr(_g('cannot write %s'), $file);
-    print { $file_fh } "$_\n" foreach @lines;
-    close($file_fh);
-}
-
-sub _drop_line {
-    my ($file, $re) = @_;
-
-    my @lines = _load_file($file);
-    open my $file_fh, '>', $file or syserr(_g('cannot write %s'), $file);
-    print { $file_fh } $_ foreach grep { not /^\Q$re\E\s*$/ } @lines;
-    close($file_fh);
-}
-
 sub register_patch {
     my ($self, $dir, $tmpdiff, $patch_name) = @_;
 
     my $quilt = $self->build_quilt_object($dir);
-
-    my @patches = $quilt->series();
-    my $has_patch = (grep { $_ eq $patch_name } @patches) ? 1 : 0;
-    my $series = $quilt->get_series_file();
-    my $applied = $quilt->get_db_file('applied-patches');
     my $patch = $quilt->get_patch_file($patch_name);
 
     if (-s $tmpdiff) {
@@ -274,30 +237,11 @@ sub register_patch {
     }
 
     if (-e $patch) {
-        $quilt->setup_db();
         # Add patch to series file
-        if (not $has_patch) {
-            _add_line($series, $patch_name);
-            _add_line($applied, $patch_name);
-            $quilt->load_series();
-            $quilt->load_db();
-        }
-        # Ensure quilt meta-data are created and in sync with some trickery:
-        # reverse-apply the patch, drop .pc/$patch, re-apply it
-        # with the correct options to recreate the backup files
-        $quilt->pop(reverse_apply => 1);
-        $quilt->push();
+        $quilt->register($patch_name);
     } else {
         # Remove auto_patch from series
-        if ($has_patch) {
-            _drop_line($series, $patch_name);
-            _drop_line($applied, $patch_name);
-            erasedir($quilt->get_db_file($patch_name));
-            $quilt->load_db();
-            $quilt->load_series();
-        }
-        # Clean up empty series
-        unlink($series) if -z $series;
+        $quilt->unregister($patch_name);
     }
     return $patch;
 }

+ 69 - 0
scripts/Dpkg/Source/Quilt.pm

@@ -22,6 +22,7 @@ our $VERSION = '0.02';
 
 use Dpkg::Gettext;
 use Dpkg::ErrorHandling;
+use Dpkg::Util qw(:list);
 use Dpkg::Source::Patch;
 use Dpkg::Source::Functions qw(erasedir fs_time);
 use Dpkg::Vendor qw(get_current_vendor);
@@ -117,6 +118,42 @@ sub top {
     return;
 }
 
+sub register {
+    my ($self, $patch_name) = @_;
+
+    return if any { $_ eq $patch_name } @{$self->{series}};
+
+    # Add patch to series files.
+    $self->setup_db();
+    $self->_file_add_line($self->get_series_file(), $patch_name);
+    $self->_file_add_line($self->get_db_file('applied-patches'), $patch_name);
+    $self->load_db();
+    $self->load_series();
+
+    # Ensure quilt meta-data is created and in sync with some trickery:
+    # Reverse-apply the patch, drop .pc/$patch, and re-apply it with the
+    # correct options to recreate the backup files.
+    $self->pop(reverse_apply => 1);
+    $self->push();
+}
+
+sub unregister {
+    my ($self, $patch_name) = @_;
+
+    return if none { $_ eq $patch_name } @{$self->{series}};
+
+    my $series = $self->get_series_file();
+
+    $self->_file_drop_line($series, $patch_name);
+    $self->_file_drop_line($self->get_db_file('applied-patches'), $patch_name);
+    erasedir($self->get_db_file($patch_name));
+    $self->load_db();
+    $self->load_series();
+
+    # Clean up empty series.
+    unlink $series if -z $series;
+}
+
 sub next {
     my ($self) = @_;
     my $count_applied = scalar @{$self->{applied_patches}};
@@ -246,6 +283,38 @@ sub get_patch_dir {
 
 ## METHODS BELOW ARE INTERNAL ##
 
+sub _file_load {
+    my ($self, $file) = @_;
+
+    open my $file_fh, '<', $file or syserr(_g('cannot read %s'), $file);
+    my @lines = <$file_fh>;
+    close $file_fh;
+
+    return @lines;
+}
+
+sub _file_add_line {
+    my ($self, $file, $line) = @_;
+
+    my @lines;
+    @lines = $self->_file_load($file) if -f $file;
+    CORE::push @lines, $line;
+    chomp @lines;
+
+    open my $file_fh, '>', $file or syserr(_g('cannot write %s'), $file);
+    print { $file_fh } "$_\n" foreach @lines;
+    close $file_fh;
+}
+
+sub _file_drop_line {
+    my ($self, $file, $re) = @_;
+
+    my @lines = $self->_file_load($file);
+    open my $file_fh, '>', $file or syserr(_g('cannot write %s'), $file);
+    print { $file_fh } $_ foreach grep { not /^\Q$re\E\s*$/ } @lines;
+    close $file_fh;
+}
+
 sub read_patch_list {
     my ($self, $file, %opts) = @_;
     return () if not defined $file or not -f $file;