Dpkg/Source/Package/V3/Quilt.pm untested

filter: Statementstmt 0.0% 0 / 151 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 306 CC 29 SCAR 33.4
CC
29
Cov
0%
apply_patches
110.0
parse_cmdline_option
20.0
prepare_build
20.0
CRAP 28.3
1 # Copyright © 2008-2012 Raphaël Hertzog <hertzog@debian.org>
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <https://www.gnu.org/licenses/>.
15  
16 =encoding utf8
17  
18 =head1 NAME
19  
20 Dpkg::Source::Package::V3::Quilt - class for source format 3.0 (quilt)
21  
22 =head1 DESCRIPTION
23  
24 This module provides a class to handle the source package format 3.0 (quilt).
25  
26 B<Note>: This is a private module, its API can change at any time.
27  
28 =cut
29  
30 package Dpkg::Source::Package::V3::Quilt 0.01;
31  
32 use v5.36;
33  
34 use List::Util qw(any);
35 use File::Spec;
36 use File::Copy;
37  
38 use Dpkg::Gettext;
39 use Dpkg::ErrorHandling;
40 use Dpkg::File;
41 use Dpkg::Source::Patch;
42 use Dpkg::Source::Functions qw(erasedir chmod_if_needed fs_time);
43 use Dpkg::Source::Quilt;
44 use Dpkg::Exit;
45  
46 # Based on wig&pen implementation.
47 use parent qw(Dpkg::Source::Package::V2);
48  
49 sub CURRENT_MINOR_VERSION {
50 '0';
51 }
52  
53 sub init_options {
54 my $self = shift;
55 $self->{options}{single_debian_patch} //= 0;
56 $self->{options}{allow_version_of_quilt_db} //= [];
57  
58 $self->SUPER::init_options();
59 }
60  
61 my @module_cmdline = (
62 {
63 name => '--single-debian-patch',
64 help => N_('Use a single debianization patch.'),
65 when => 'build',
66 }, {
67 name => '--allow-version-of-quilt-db=<version>',
68 help => N_('Accept quilt metadata <version> even if unknown.'),
69 when => 'build',
70 }
71 );
72  
73 sub describe_cmdline_options {
74 my $self = shift;
75  
76 my @cmdline = (
77 $self->SUPER::describe_cmdline_options(),
78 @module_cmdline,
79 );
80  
81 return @cmdline;
82 }
83  
84 sub parse_cmdline_option {
85 my ($self, $opt) = @_;
86 return 1 if $self->SUPER::parse_cmdline_option($opt);
87 if ($opt eq '--single-debian-patch') {
88 $self->{options}{single_debian_patch} = 1;
89 # For backwards compatibility.
90 $self->{options}{auto_commit} = 1;
91 return 1;
92 } elsif ($opt =~ /^--allow-version-of-quilt-db=(.*)$/) {
93 push @{$self->{options}{allow_version_of_quilt_db}}, $1;
94 return 1;
95 }
96 return 0;
97 }
98  
99 sub _build_quilt_object {
100 my ($self, $dir) = @_;
101 return $self->{quilt}{$dir} if exists $self->{quilt}{$dir};
102 $self->{quilt}{$dir} = Dpkg::Source::Quilt->new($dir);
103 return $self->{quilt}{$dir};
104 }
105  
106 sub can_build {
107 my ($self, $dir) = @_;
108 my ($code, $msg) = $self->SUPER::can_build($dir);
109 return ($code, $msg) if $code == 0;
110  
111 my $quilt = $self->_build_quilt_object($dir);
112 $msg = $quilt->find_problems();
113 return (0, $msg) if $msg;
114 return 1;
115 }
116  
117 sub get_autopatch_name {
118 my $self = shift;
119 if ($self->{options}{single_debian_patch}) {
120 return 'debian-changes';
121 } else {
122 return 'debian-changes-' . $self->{fields}{'Version'};
123 }
124 }
125  
126 sub apply_patches {
127 my ($self, $dir, %opts) = @_;
128  
129 if ($opts{usage} eq 'unpack') {
130 $opts{verbose} = 1;
131 } elsif ($opts{usage} eq 'build') {
132 $opts{warn_options} = 1;
133 $opts{verbose} = 0;
134 }
135  
136 my $quilt = $self->_build_quilt_object($dir);
137 # Trigger warnings.
138 $quilt->load_series(%opts) if $opts{warn_options};
139  
140 # Always create the quilt db so that if the maintainer calls quilt to
141 # create a patch, it is stored in the right directory.
142 $quilt->save_db();
143  
144 # Update debian/patches/series symlink if needed to allow quilt usage.
145 my $series = $quilt->get_series_file();
146 my $basename = (File::Spec->splitpath($series))[2];
147 if ($basename ne 'series') {
148 my $dest = $quilt->get_patch_file('series');
149 unlink($dest) if -l $dest;
150 # Do not overwrite real files.
151 unless (-f _) {
152 symlink($basename, $dest)
153 or syserr(g_('cannot create symbolic link %s'), $dest);
154 }
155 }
156  
157 return unless scalar($quilt->series());
158  
159 info(g_('using patch list from %s'), "debian/patches/$basename");
160  
161 if ($opts{usage} eq 'preparation' and
162 $self->{options}{unapply_patches} eq 'auto') {
163 # We are applying the patches in --before-build, remember to unapply
164 # them afterwards in --after-build.
165 my $pc_unapply = $quilt->get_db_file('.dpkg-source-unapply');
166 file_touch($pc_unapply);
167 }
168  
169 # Apply patches.
170 my $pc_applied = $quilt->get_db_file('applied-patches');
171 $opts{timestamp} = fs_time($pc_applied);
172 if ($opts{skip_auto}) {
173 my $auto_patch = $self->get_autopatch_name();
174 $quilt->push(%opts) while ($quilt->next() and $quilt->next() ne $auto_patch);
175 } else {
176 $quilt->push(%opts) while $quilt->next();
177 }
178 }
179  
180 sub unapply_patches {
181 my ($self, $dir, %opts) = @_;
182  
183 my $quilt = $self->_build_quilt_object($dir);
184  
185 $opts{verbose} //= 1;
186  
187 my $pc_applied = $quilt->get_db_file('applied-patches');
188 my @applied = $quilt->applied();
189 $opts{timestamp} = fs_time($pc_applied) if @applied;
190  
191 $quilt->pop(%opts) while $quilt->top();
192  
193 erasedir($quilt->get_db_dir());
194 }
195  
196 sub prepare_build {
197 my ($self, $dir) = @_;
198 $self->SUPER::prepare_build($dir);
199 # Skip .pc directories of quilt by default and ignore difference
200 # on debian/patches/series symlinks and d/p/.dpkg-source-applied
201 # stamp file created by ourselves.
202 my $func = sub {
203 my $pathname = shift;
204  
205 return 1 if $pathname eq 'debian/patches/series' and -l $pathname;
206 return 1 if $pathname =~ /^\.pc(\/|$)/;
207 return 1 if $pathname =~ /$self->{options}{diff_ignore_regex}/;
208 return 0;
209 };
210 $self->{diff_options}{diff_ignore_func} = $func;
211 }
212  
213 sub do_build {
214 my ($self, $dir) = @_;
215  
216 my $quilt = $self->_build_quilt_object($dir);
217 my $version = $quilt->get_db_version();
218  
219 if (defined($version) and $version != 2) {
220 if (any { $version eq $_ }
221 @{$self->{options}{allow_version_of_quilt_db}})
222 {
223 warning(g_('unsupported version of the quilt metadata: %s'), $version);
224 } else {
225 error(g_('unsupported version of the quilt metadata: %s'), $version);
226 }
227 }
228  
229 $self->SUPER::do_build($dir);
230 }
231  
232 sub after_build {
233 my ($self, $dir) = @_;
234 my $quilt = $self->_build_quilt_object($dir);
235 my $pc_unapply = $quilt->get_db_file('.dpkg-source-unapply');
236 my $opt_unapply = $self->{options}{unapply_patches};
237 if (($opt_unapply eq 'auto' and -e $pc_unapply) or $opt_unapply eq 'yes') {
238 unlink($pc_unapply);
239 $self->unapply_patches($dir);
240 }
241 }
242  
243 sub check_patches_applied {
244 my ($self, $dir) = @_;
245  
246 my $quilt = $self->_build_quilt_object($dir);
247 my $next = $quilt->next();
248 return if not defined $next;
249  
250 my $first_patch = File::Spec->catfile($dir, 'debian', 'patches', $next);
251 my $patch_obj = Dpkg::Source::Patch->new(filename => $first_patch);
252 return unless $patch_obj->check_apply($dir, fatal_dupes => 1);
253  
254 $self->apply_patches($dir,
255 usage => 'preparation',
256 verbose => 1,
257 );
258 }
259  
260 sub register_patch {
261 my ($self, $dir, $tmpdiff, $patch_name) = @_;
262  
263 my $quilt = $self->_build_quilt_object($dir);
264 my $patch = $quilt->get_patch_file($patch_name);
265  
266 if (-s $tmpdiff) {
267 copy($tmpdiff, $patch)
268 or syserr(g_('cannot copy %s to %s'), $tmpdiff, $patch);
269 chmod_if_needed(0o666 & ~ umask(), $patch)
270 or syserr(g_("cannot change permission of '%s'"), $patch);
271 } elsif (-e $patch) {
272 unlink($patch) or syserr(g_('cannot remove %s'), $patch);
273 }
274  
275 if (-e $patch) {
276 # Add patch to series file.
277 $quilt->register($patch_name);
278 } else {
279 # Remove auto_patch from series.
280 $quilt->unregister($patch_name);
281 }
282 return $patch;
283 }
284  
285 =head1 CHANGES
286  
287 =head2 Version 0.xx
288  
289 This is a private module.
290  
291 =cut
292  
293 1;