| 1 | # Copyright © 2008-2011 Raphaël Hertzog <hertzog@debian.org> | ||
| 2 | # Copyright © 2008-2015 Guillem Jover <guillem@debian.org> | ||
| 3 | # | ||
| 4 | # This program is free software; you can redistribute it and/or modify | ||
| 5 | # it under the terms of the GNU General Public License as published by | ||
| 6 | # the Free Software Foundation; either version 2 of the License, or | ||
| 7 | # (at your option) any later version. | ||
| 8 | # | ||
| 9 | # This program is distributed in the hope that it will be useful, | ||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | # GNU General Public License for more details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License | ||
| 15 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | =encoding utf8 | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | Dpkg::Source::Package::V2 - class for source format 2.0 | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | This module provides a class to handle the source package format 2.0. | ||
| 26 | |||
| 27 | B<Note>: This is a private module, its API can change at any time. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | package Dpkg::Source::Package::V2 0.01; | ||
| 32 | |||
| 33 | use v5.36; | ||
| 34 | |||
| 35 | use List::Util qw(first); | ||
| 36 | use Cwd; | ||
| 37 | use File::Basename; | ||
| 38 | use File::Temp; | ||
| 39 | use File::Path qw(make_path); | ||
| 40 | use File::Spec; | ||
| 41 | use File::Find; | ||
| 42 | use File::Copy; | ||
| 43 | |||
| 44 | use Dpkg::Gettext; | ||
| 45 | use Dpkg::ErrorHandling; | ||
| 46 | use Dpkg::File; | ||
| 47 | use Dpkg::Path qw(find_command); | ||
| 48 | use Dpkg::Version; | ||
| 49 | use Dpkg::Compression; | ||
| 50 | use Dpkg::Source::Archive; | ||
| 51 | use Dpkg::Source::Patch; | ||
| 52 | use Dpkg::Source::BinaryFiles; | ||
| 53 | use Dpkg::Exit qw(push_exit_handler pop_exit_handler); | ||
| 54 | use Dpkg::Source::Functions qw(erasedir chmod_if_needed fs_time); | ||
| 55 | use Dpkg::Vendor qw(run_vendor_hook); | ||
| 56 | use Dpkg::Control; | ||
| 57 | use Dpkg::Changelog::Parse; | ||
| 58 | |||
| 59 | use parent qw(Dpkg::Source::Package); | ||
| 60 | |||
| 61 | sub CURRENT_MINOR_VERSION { | ||
| 62 | '0'; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub init_options { | ||
| 66 | my $self = shift; | ||
| 67 | $self->SUPER::init_options(); | ||
| 68 | $self->{options}{include_removal} //= 0; | ||
| 69 | $self->{options}{include_timestamp} //= 0; | ||
| 70 | $self->{options}{include_binaries} //= 0; | ||
| 71 | $self->{options}{preparation} //= 1; | ||
| 72 | $self->{options}{skip_patches} //= 0; | ||
| 73 | $self->{options}{unapply_patches} //= 'auto'; | ||
| 74 | $self->{options}{skip_debianization} //= 0; | ||
| 75 | $self->{options}{create_empty_orig} //= 0; | ||
| 76 | $self->{options}{auto_commit} //= 0; | ||
| 77 | $self->{options}{ignore_bad_version} //= 0; | ||
| 78 | } | ||
| 79 | |||
| 80 | my @module_cmdline = ( | ||
| 81 | { | ||
| 82 | name => '--include-removal', | ||
| 83 | help => N_('Include removed files in the patch.'), | ||
| 84 | when => 'build', | ||
| 85 | }, { | ||
| 86 | name => '--include-timestamp', | ||
| 87 | help => N_('Include timestamp in the patch.'), | ||
| 88 | when => 'build', | ||
| 89 | }, { | ||
| 90 | name => '--include-binaries', | ||
| 91 | help => N_('Include binary files in the tarball.'), | ||
| 92 | when => 'build', | ||
| 93 | }, { | ||
| 94 | name => '--no-preparation', | ||
| 95 | help => N_('Do not prepare build tree by applying patches.'), | ||
| 96 | when => 'build', | ||
| 97 | }, { | ||
| 98 | name => '--no-unapply-patches', | ||
| 99 | help => N_('Do not unapply patches if previously applied.'), | ||
| 100 | when => 'build', | ||
| 101 | }, { | ||
| 102 | name => '--unapply-patches', | ||
| 103 | help => N_('Unapply patches if previously applied (default).'), | ||
| 104 | when => 'build', | ||
| 105 | }, { | ||
| 106 | name => '--create-empty-orig', | ||
| 107 | help => N_('Create an empty original tarball if missing.'), | ||
| 108 | when => 'build', | ||
| 109 | }, { | ||
| 110 | name => '--abort-on-upstream-changes', | ||
| 111 | help => N_('Abort if generated diff has upstream files changes.'), | ||
| 112 | when => 'build', | ||
| 113 | }, { | ||
| 114 | name => '--auto-commit', | ||
| 115 | help => N_('Record generated patches, instead of aborting.'), | ||
| 116 | when => 'build', | ||
| 117 | }, { | ||
| 118 | name => '--skip-debianization', | ||
| 119 | help => N_('Do not extract debian tarball into upstream sources.'), | ||
| 120 | when => 'extract', | ||
| 121 | }, { | ||
| 122 | name => '--skip-patches', | ||
| 123 | help => N_('Do not apply patches at the end of the extraction.'), | ||
| 124 | when => 'extract', | ||
| 125 | } | ||
| 126 | ); | ||
| 127 | |||
| 128 | sub describe_cmdline_options { | ||
| 129 | return @module_cmdline; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub parse_cmdline_option { | ||
| 133 | my ($self, $opt) = @_; | ||
| 134 | if ($opt eq '--include-removal') { | ||
| 135 | $self->{options}{include_removal} = 1; | ||
| 136 | return 1; | ||
| 137 | } elsif ($opt eq '--include-timestamp') { | ||
| 138 | $self->{options}{include_timestamp} = 1; | ||
| 139 | return 1; | ||
| 140 | } elsif ($opt eq '--include-binaries') { | ||
| 141 | $self->{options}{include_binaries} = 1; | ||
| 142 | return 1; | ||
| 143 | } elsif ($opt eq '--no-preparation') { | ||
| 144 | $self->{options}{preparation} = 0; | ||
| 145 | return 1; | ||
| 146 | } elsif ($opt eq '--skip-patches') { | ||
| 147 | $self->{options}{skip_patches} = 1; | ||
| 148 | return 1; | ||
| 149 | } elsif ($opt eq '--unapply-patches') { | ||
| 150 | $self->{options}{unapply_patches} = 'yes'; | ||
| 151 | return 1; | ||
| 152 | } elsif ($opt eq '--no-unapply-patches') { | ||
| 153 | $self->{options}{unapply_patches} = 'no'; | ||
| 154 | return 1; | ||
| 155 | } elsif ($opt eq '--skip-debianization') { | ||
| 156 | $self->{options}{skip_debianization} = 1; | ||
| 157 | return 1; | ||
| 158 | } elsif ($opt eq '--create-empty-orig') { | ||
| 159 | $self->{options}{create_empty_orig} = 1; | ||
| 160 | return 1; | ||
| 161 | } elsif ($opt eq '--abort-on-upstream-changes') { | ||
| 162 | $self->{options}{auto_commit} = 0; | ||
| 163 | return 1; | ||
| 164 | } elsif ($opt eq '--auto-commit') { | ||
| 165 | $self->{options}{auto_commit} = 1; | ||
| 166 | return 1; | ||
| 167 | } elsif ($opt eq '--ignore-bad-version') { | ||
| 168 | $self->{options}{ignore_bad_version} = 1; | ||
| 169 | return 1; | ||
| 170 | } | ||
| 171 | return 0; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub do_extract { | ||
| 175 | my ($self, $newdirectory) = @_; | ||
| 176 | my $fields = $self->{fields}; | ||
| 177 | |||
| 178 | my $basename = $self->get_basename(); | ||
| 179 | my $basenamerev = $self->get_basename(1); | ||
| 180 | |||
| 181 | my ($tarfile, $debianfile, %addonfile, %seen); | ||
| 182 | my ($tarsign, %addonsign); | ||
| 183 | my $re_ext = compression_get_file_extension_regex(); | ||
| 184 | foreach my $file ($self->get_files()) { | ||
| 185 | my $uncompressed = $file; | ||
| 186 | $uncompressed =~ s/\.$re_ext$/.*/; | ||
| 187 | $uncompressed =~ s/\.$re_ext\.asc$/.*.asc/; | ||
| 188 | error(g_('duplicate files in %s source package: %s'), 'v2.0', | ||
| 189 | $uncompressed) if $seen{$uncompressed}; | ||
| 190 | $seen{$uncompressed} = 1; | ||
| 191 | if ($file =~ /^\Q$basename\E\.orig\.tar\.$re_ext$/) { | ||
| 192 | $tarfile = $file; | ||
| 193 | } elsif ($file =~ /^\Q$basename\E\.orig\.tar\.$re_ext\.asc$/) { | ||
| 194 | $tarsign = $file; | ||
| 195 | } elsif ($file =~ /^\Q$basename\E\.orig-([[:alnum:]-]+)\.tar\.$re_ext$/) { | ||
| 196 | $addonfile{$1} = $file; | ||
| 197 | } elsif ($file =~ /^\Q$basename\E\.orig-([[:alnum:]-]+)\.tar\.$re_ext\.asc$/) { | ||
| 198 | $addonsign{$1} = $file; | ||
| 199 | } elsif ($file =~ /^\Q$basenamerev\E\.debian\.tar\.$re_ext$/) { | ||
| 200 | $debianfile = $file; | ||
| 201 | } else { | ||
| 202 | error(g_('unrecognized file for a %s source package: %s'), | ||
| 203 | 'v2.0', $file); | ||
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | unless ($tarfile and $debianfile) { | ||
| 208 | error(g_('missing orig.tar or debian.tar file in v2.0 source package')); | ||
| 209 | } | ||
| 210 | if ($tarsign and $tarfile ne substr $tarsign, 0, -4) { | ||
| 211 | error(g_('mismatched orig.tar %s for signature %s in source package'), | ||
| 212 | $tarfile, $tarsign); | ||
| 213 | } | ||
| 214 | foreach my $name (keys %addonsign) { | ||
| 215 | error(g_('missing addon orig.tar for signature %s in source package'), | ||
| 216 | $addonsign{$name}) | ||
| 217 | if not exists $addonfile{$name}; | ||
| 218 | error(g_('mismatched addon orig.tar %s for signature %s in source package'), | ||
| 219 | $addonfile{$name}, $addonsign{$name}) | ||
| 220 | if $addonfile{$name} ne substr $addonsign{$name}, 0, -4; | ||
| 221 | } | ||
| 222 | |||
| 223 | my $v = Dpkg::Version->new($fields->{'Version'}); | ||
| 224 | if (! $v->has_revision()) { | ||
| 225 | warning(g_('non-native package version does not contain a revision')) | ||
| 226 | } | ||
| 227 | |||
| 228 | if ($self->{options}{no_overwrite_dir} and -e $newdirectory) { | ||
| 229 | error(g_('unpack target exists: %s'), $newdirectory); | ||
| 230 | } else { | ||
| 231 | erasedir($newdirectory); | ||
| 232 | } | ||
| 233 | |||
| 234 | # Extract main tarball. | ||
| 235 | info(g_('unpacking %s'), $tarfile); | ||
| 236 | my $tar = Dpkg::Source::Archive->new( | ||
| 237 | filename => File::Spec->catfile($self->{basedir}, $tarfile), | ||
| 238 | ); | ||
| 239 | $tar->extract($newdirectory, | ||
| 240 | options => [ | ||
| 241 | '--anchored', | ||
| 242 | '--no-wildcards-match-slash', | ||
| 243 | '--exclude', | ||
| 244 | '*/.pc', | ||
| 245 | '--exclude', | ||
| 246 | '.pc', | ||
| 247 | ], | ||
| 248 | ); | ||
| 249 | # The ".pc" exclusion is only needed for 3.0 (quilt) and to avoid having | ||
| 250 | # an upstream tarball provide a directory with symlinks that would be | ||
| 251 | # blindly followed when applying the patches. | ||
| 252 | |||
| 253 | # Extract additional orig tarballs. | ||
| 254 | foreach my $subdir (sort keys %addonfile) { | ||
| 255 | my $file = $addonfile{$subdir}; | ||
| 256 | info(g_('unpacking %s'), $file); | ||
| 257 | |||
| 258 | # If the pathname is an empty directory, just silently remove it, as | ||
| 259 | # it might be part of a git repository, as a submodule for example. | ||
| 260 | rmdir "$newdirectory/$subdir"; | ||
| 261 | if (-e "$newdirectory/$subdir") { | ||
| 262 | warning(g_("required removal of '%s' installed by original tarball"), | ||
| 263 | $subdir); | ||
| 264 | erasedir("$newdirectory/$subdir"); | ||
| 265 | } | ||
| 266 | $tar = Dpkg::Source::Archive->new( | ||
| 267 | filename => File::Spec->catfile($self->{basedir}, $file), | ||
| 268 | ); | ||
| 269 | $tar->extract("$newdirectory/$subdir"); | ||
| 270 | } | ||
| 271 | |||
| 272 | # Stop here if debianization is not wanted. | ||
| 273 | return if $self->{options}{skip_debianization}; | ||
| 274 | |||
| 275 | # Extract debian tarball after removing the debian directory. | ||
| 276 | info(g_('unpacking %s'), $debianfile); | ||
| 277 | erasedir("$newdirectory/debian"); | ||
| 278 | $tar = Dpkg::Source::Archive->new( | ||
| 279 | filename => File::Spec->catfile($self->{basedir}, $debianfile), | ||
| 280 | ); | ||
| 281 | $tar->extract($newdirectory, | ||
| 282 | in_place => 1, | ||
| 283 | ); | ||
| 284 | |||
| 285 | # Apply patches (in a separate method as it might be overridden). | ||
| 286 | $self->apply_patches($newdirectory, usage => 'unpack') | ||
| 287 | unless $self->{options}{skip_patches}; | ||
| 288 | } | ||
| 289 | |||
| 290 | sub get_autopatch_name { | ||
| 291 | return 'zz_debian-diff-auto'; | ||
| 292 | } | ||
| 293 | |||
| 294 | sub _get_patches { | ||
| 295 | my ($self, $dir, %opts) = @_; | ||
| 296 | $opts{skip_auto} //= 0; | ||
| 297 | my @patches; | ||
| 298 | my $pd = "$dir/debian/patches"; | ||
| 299 | my $auto_patch = $self->get_autopatch_name(); | ||
| 300 | if (-d $pd) { | ||
| 301 | opendir(my $dir_dh, $pd) | ||
| 302 | or syserr(g_('cannot open directory %s'), $pd); | ||
| 303 | foreach my $patch (sort readdir($dir_dh)) { | ||
| 304 | # The «patches» directory matches the same rules as run-parts. | ||
| 305 | next unless $patch =~ /^[\w-]+$/ and -f "$pd/$patch"; | ||
| 306 | next if $opts{skip_auto} and $patch eq $auto_patch; | ||
| 307 | push @patches, $patch; | ||
| 308 | } | ||
| 309 | closedir($dir_dh); | ||
| 310 | } | ||
| 311 | return @patches; | ||
| 312 | } | ||
| 313 | |||
| 314 | sub apply_patches { | ||
| 315 | my ($self, $dir, %opts) = @_; | ||
| 316 | $opts{skip_auto} //= 0; | ||
| 317 | my @patches = $self->_get_patches($dir, %opts); | ||
| 318 | return unless scalar(@patches); | ||
| 319 | my $applied = File::Spec->catfile($dir, 'debian', 'patches', '.dpkg-source-applied'); | ||
| 320 | open(my $applied_fh, '>', $applied) | ||
| 321 | or syserr(g_('cannot write %s'), $applied); | ||
| 322 | print { $applied_fh } "# During $opts{usage}\n"; | ||
| 323 | my $timestamp = fs_time($applied); | ||
| 324 | foreach my $patch ($self->_get_patches($dir, %opts)) { | ||
| 325 | my $path = File::Spec->catfile($dir, 'debian', 'patches', $patch); | ||
| 326 | info(g_('applying %s'), $patch) unless $opts{skip_auto}; | ||
| 327 | my $patch_obj = Dpkg::Source::Patch->new(filename => $path); | ||
| 328 | $patch_obj->apply($dir, | ||
| 329 | force_timestamp => 1, | ||
| 330 | timestamp => $timestamp, | ||
| 331 | add_options => [ | ||
| 332 | '-E', | ||
| 333 | ], | ||
| 334 | ); | ||
| 335 | print { $applied_fh } "$patch\n"; | ||
| 336 | } | ||
| 337 | close($applied_fh); | ||
| 338 | } | ||
| 339 | |||
| 340 | sub unapply_patches { | ||
| 341 | my ($self, $dir, %opts) = @_; | ||
| 342 | my @patches = reverse($self->_get_patches($dir, %opts)); | ||
| 343 | return unless scalar(@patches); | ||
| 344 | my $applied = File::Spec->catfile($dir, 'debian', 'patches', '.dpkg-source-applied'); | ||
| 345 | my $timestamp = fs_time($applied); | ||
| 346 | foreach my $patch (@patches) { | ||
| 347 | my $path = File::Spec->catfile($dir, 'debian', 'patches', $patch); | ||
| 348 | info(g_('unapplying %s'), $patch) unless $opts{quiet}; | ||
| 349 | my $patch_obj = Dpkg::Source::Patch->new(filename => $path); | ||
| 350 | $patch_obj->apply($dir, | ||
| 351 | verbose => 0, | ||
| 352 | force_timestamp => 1, | ||
| 353 | timestamp => $timestamp, | ||
| 354 | add_options => [ | ||
| 355 | '-E', | ||
| 356 | '-R', | ||
| 357 | ], | ||
| 358 | ); | ||
| 359 | } | ||
| 360 | unlink($applied); | ||
| 361 | } | ||
| 362 | |||
| 363 | sub _upstream_tarball_template { | ||
| 364 | my $self = shift; | ||
| 365 | my $ext = '{' . join(',', | ||
| 366 | sort map { | ||
| 367 | compression_get_file_extension($_) | ||
| 368 | } compression_get_list()) . '}'; | ||
| 369 | return File::Spec->catfile($self->{basedir}, $self->get_basename() . ".orig.tar.$ext"); | ||
| 370 | } | ||
| 371 | |||
| 372 | sub can_build { | ||
| 373 | my ($self, $dir) = @_; | ||
| 374 | |||
| 375 | my $v = Dpkg::Version->new($self->{fields}->{'Version'}); | ||
| 376 | return (0, g_('non-native package version does not contain a revision')) | ||
| 377 | if ! $v->has_revision(); | ||
| 378 | |||
| 379 | return 1 if $self->find_original_tarballs(include_supplementary => 0); | ||
| 380 | return 1 if $self->{options}{create_empty_orig} and | ||
| 381 | $self->find_original_tarballs(include_main => 0); | ||
| 382 | return (0, sprintf(g_('no upstream tarball found at %s'), | ||
| 383 | $self->_upstream_tarball_template())); | ||
| 384 | } | ||
| 385 | |||
| 386 | sub before_build { | ||
| 387 | my ($self, $dir) = @_; | ||
| 388 | $self->check_patches_applied($dir) if $self->{options}{preparation}; | ||
| 389 | } | ||
| 390 | |||
| 391 | sub after_build { | ||
| 392 | my ($self, $dir) = @_; | ||
| 393 | my $applied = File::Spec->catfile($dir, 'debian', 'patches', '.dpkg-source-applied'); | ||
| 394 | my $reason = ''; | ||
| 395 | if (-e $applied) { | ||
| 396 | $reason = file_slurp($applied); | ||
| 397 | } | ||
| 398 | my $opt_unapply = $self->{options}{unapply_patches}; | ||
| 399 | if (($opt_unapply eq 'auto' and $reason =~ /^# During preparation/) or | ||
| 400 | $opt_unapply eq 'yes') { | ||
| 401 | $self->unapply_patches($dir); | ||
| 402 | } | ||
| 403 | } | ||
| 404 | |||
| 405 | sub prepare_build { | ||
| 406 | my ($self, $dir) = @_; | ||
| 407 | $self->{diff_options} = { | ||
| 408 | diff_ignore_regex => $self->{options}{diff_ignore_regex} . | ||
| 409 | '|(^|/)debian/patches/.dpkg-source-applied$', | ||
| 410 | include_removal => $self->{options}{include_removal}, | ||
| 411 | include_timestamp => $self->{options}{include_timestamp}, | ||
| 412 | use_dev_null => 1, | ||
| 413 | }; | ||
| 414 | push @{$self->{options}{tar_ignore}}, 'debian/patches/.dpkg-source-applied'; | ||
| 415 | $self->check_patches_applied($dir) if $self->{options}{preparation}; | ||
| 416 | if ($self->{options}{create_empty_orig} and | ||
| 417 | not $self->find_original_tarballs(include_supplementary => 0)) | ||
| 418 | { | ||
| 419 | # No main orig.tar, create a dummy one. | ||
| 420 | my $filename = $self->get_basename() . '.orig.tar.' . | ||
| 421 | $self->{options}{comp_ext}; | ||
| 422 | my $tar = Dpkg::Source::Archive->new( | ||
| 423 | filename => $filename, | ||
| 424 | compression_level => $self->{options}{comp_level}, | ||
| 425 | ); | ||
| 426 | $tar->create(); | ||
| 427 | $tar->finish(); | ||
| 428 | } | ||
| 429 | } | ||
| 430 | |||
| 431 | sub check_patches_applied { | ||
| 432 | my ($self, $dir) = @_; | ||
| 433 | my $applied = File::Spec->catfile($dir, 'debian', 'patches', '.dpkg-source-applied'); | ||
| 434 | unless (-e $applied) { | ||
| 435 | info(g_('patches are not applied, applying them now')); | ||
| 436 | $self->apply_patches($dir, | ||
| 437 | usage => 'preparation', | ||
| 438 | ); | ||
| 439 | } | ||
| 440 | } | ||
| 441 | |||
| 442 | sub _generate_patch { | ||
| 443 | my ($self, $dir, %opts) = @_; | ||
| 444 | my ($dirname, $updir) = fileparse($dir); | ||
| 445 | my $basedirname = $self->get_basedirname(); | ||
| 446 | |||
| 447 | # Identify original tarballs. | ||
| 448 | my ($tarfile, %addonfile); | ||
| 449 | my $comp_ext_regex = compression_get_file_extension_regex(); | ||
| 450 | my @origtarfiles; | ||
| 451 | my @origtarsigns; | ||
| 452 | foreach my $file (sort $self->find_original_tarballs()) { | ||
| 453 | if ($file =~ /\.orig\.tar\.$comp_ext_regex$/) { | ||
| 454 | if (defined($tarfile)) { | ||
| 455 | error(g_('several orig.tar files found (%s and %s) but only ' . | ||
| 456 | 'one is allowed'), $tarfile, $file); | ||
| 457 | } | ||
| 458 | $tarfile = $file; | ||
| 459 | } elsif ($file =~ /\.orig-([[:alnum:]-]+)\.tar\.$comp_ext_regex$/) { | ||
| 460 | $addonfile{$1} = $file; | ||
| 461 | } else { | ||
| 462 | next; | ||
| 463 | } | ||
| 464 | |||
| 465 | push @origtarfiles, $file; | ||
| 466 | $self->add_file($file); | ||
| 467 | |||
| 468 | # Check for an upstream signature. | ||
| 469 | if (-e "$file.sig" and not -e "$file.asc") { | ||
| 470 | $self->armor_original_tarball_signature("$file.sig", "$file.asc"); | ||
| 471 | } | ||
| 472 | if (-e "$file.asc") { | ||
| 473 | push @origtarfiles, "$file.asc"; | ||
| 474 | push @origtarsigns, "$file.asc"; | ||
| 475 | $self->add_file("$file.asc") | ||
| 476 | } | ||
| 477 | } | ||
| 478 | |||
| 479 | error(g_('no upstream tarball found at %s'), | ||
| 480 | $self->_upstream_tarball_template()) unless $tarfile; | ||
| 481 | |||
| 482 | if ($opts{usage} eq 'build') { | ||
| 483 | if (@origtarsigns) { | ||
| 484 | $self->check_original_tarball_signature($dir, @origtarsigns); | ||
| 485 | } else { | ||
| 486 | my $key = $self->get_upstream_signing_key($dir); | ||
| 487 | if (-e $key) { | ||
| 488 | warning(g_('upstream signing key but no upstream tarball signature')); | ||
| 489 | } | ||
| 490 | } | ||
| 491 | |||
| 492 | foreach my $origtarfile (@origtarfiles) { | ||
| 493 | info(g_('building %s using existing %s'), | ||
| 494 | $self->{fields}{'Source'}, $origtarfile); | ||
| 495 | } | ||
| 496 | } | ||
| 497 | |||
| 498 | # Unpack a second copy for comparison. | ||
| 499 | my $tmpdir = File::Temp->newdir( | ||
| 500 | TEMPLATE => "$dirname.orig.XXXXXX", | ||
| 501 | DIR => $updir, | ||
| 502 | CLEANUP => 0, | ||
| 503 | ); | ||
| 504 | push_exit_handler(sub { erasedir($tmpdir) }); | ||
| 505 | |||
| 506 | # Extract main tarball. | ||
| 507 | my $tar = Dpkg::Source::Archive->new(filename => $tarfile); | ||
| 508 | $tar->extract($tmpdir); | ||
| 509 | |||
| 510 | # Extract additional orig tarballs. | ||
| 511 | foreach my $subdir (keys %addonfile) { | ||
| 512 | my $file = $addonfile{$subdir}; | ||
| 513 | $tar = Dpkg::Source::Archive->new(filename => $file); | ||
| 514 | $tar->extract("$tmpdir/$subdir"); | ||
| 515 | } | ||
| 516 | |||
| 517 | # Copy over the debian directory. | ||
| 518 | erasedir("$tmpdir/debian"); | ||
| 519 | system('cp', '-RPp', '--', "$dir/debian", "$tmpdir/"); | ||
| 520 | subprocerr("cp $dir/debian $tmpdir/") if $?; | ||
| 521 | |||
| 522 | # Apply all patches except the last automatic one. | ||
| 523 | $opts{skip_auto} //= 0; | ||
| 524 | $self->apply_patches($tmpdir, | ||
| 525 | skip_auto => $opts{skip_auto}, | ||
| 526 | usage => 'build', | ||
| 527 | ); | ||
| 528 | |||
| 529 | # Create a patch. | ||
| 530 | my $tmpdiff = File::Temp->new( | ||
| 531 | TEMPLATE => $self->get_basename(1) . '.diff.XXXXXX', | ||
| 532 | TMPDIR => 1, | ||
| 533 | UNLINK => 0, | ||
| 534 | ); | ||
| 535 | push_exit_handler(sub { unlink($tmpdiff) }); | ||
| 536 | my $diff = Dpkg::Source::Patch->new( | ||
| 537 | filename => $tmpdiff, | ||
| 538 | compression => 'none', | ||
| 539 | ); | ||
| 540 | $diff->create(); | ||
| 541 | $diff->set_header(sub { | ||
| 542 | if ($opts{header_from} and -e $opts{header_from}) { | ||
| 543 | my $header_from = Dpkg::Source::Patch->new( | ||
| 544 | filename => $opts{header_from}, | ||
| 545 | ); | ||
| 546 | my $analysis = $header_from->analyze($dir, | ||
| 547 | verbose => 0, | ||
| 548 | ); | ||
| 549 | return $analysis->{patchheader}; | ||
| 550 | } else { | ||
| 551 | return $self->_get_patch_header($dir); | ||
| 552 | } | ||
| 553 | }); | ||
| 554 | $diff->add_diff_directory($tmpdir, $dir, | ||
| 555 | basedirname => $basedirname, | ||
| 556 | %{$self->{diff_options}}, | ||
| 557 | handle_binary_func => $opts{handle_binary}, | ||
| 558 | order_from => $opts{order_from}, | ||
| 559 | ); | ||
| 560 | $diff->finish(); | ||
| 561 | |||
| 562 | if (-s $tmpdiff) { | ||
| 563 | info(g_('local changes detected, the modified files are:')); | ||
| 564 | my $analysis = $diff->analyze($dir, | ||
| 565 | verbose => 0, | ||
| 566 | ); | ||
| 567 | foreach my $fullfn (sort keys %{$analysis->{filepatched}}) { | ||
| 568 | my $fn = ($fullfn =~ s{^\Q$dir\E/+}{}r); | ||
| 569 | print " $fn\n"; | ||
| 570 | } | ||
| 571 | } | ||
| 572 | |||
| 573 | error(g_('unrepresentable changes to source')) if $diff->has_errors(); | ||
| 574 | |||
| 575 | # Remove the temporary directory. | ||
| 576 | erasedir($tmpdir); | ||
| 577 | pop_exit_handler(); | ||
| 578 | pop_exit_handler(); | ||
| 579 | |||
| 580 | return $tmpdiff; | ||
| 581 | } | ||
| 582 | |||
| 583 | sub do_build { | ||
| 584 | my ($self, $dir) = @_; | ||
| 585 | my @argv = @{$self->{options}{ARGV}}; | ||
| 586 | if (scalar(@argv)) { | ||
| 587 | usageerr(g_("-b takes only one parameter with format '%s'"), | ||
| 588 | $self->{fields}{'Format'}); | ||
| 589 | } | ||
| 590 | $self->prepare_build($dir); | ||
| 591 | |||
| 592 | my $include_binaries = $self->{options}{include_binaries}; | ||
| 593 | my @tar_ignore = map { "--exclude=$_" } @{$self->{options}{tar_ignore}}; | ||
| 594 | |||
| 595 | my $sourcepackage = $self->{fields}{'Source'}; | ||
| 596 | my $basenamerev = $self->get_basename(1); | ||
| 597 | |||
| 598 | # Check if the debian directory contains unwanted binary files. | ||
| 599 | my $binaryfiles = Dpkg::Source::BinaryFiles->new($dir); | ||
| 600 | |||
| 601 | $binaryfiles->detect_binary_files( | ||
| 602 | exclude_globs => $self->{options}{tar_ignore}, | ||
| 603 | include_binaries => $include_binaries, | ||
| 604 | ); | ||
| 605 | |||
| 606 | # Handle modified binary files detected by the auto-patch generation. | ||
| 607 | my $handle_binary = sub { | ||
| 608 | my ($self, $old, $new, %opts) = @_; | ||
| 609 | |||
| 610 | my $file = $opts{filename}; | ||
| 611 | $binaryfiles->new_binary_found($file); | ||
| 612 | unless ($include_binaries or $binaryfiles->binary_is_allowed($file)) { | ||
| 613 | errormsg(g_('cannot represent changes to %s: %s'), $file, | ||
| 614 | g_('binary file contents changed')); | ||
| 615 | errormsg(g_('add %s in debian/source/include-binaries if you want ' . | ||
| 616 | 'to store the modified binary in the debian tarball'), | ||
| 617 | $file); | ||
| 618 | $self->register_error(); | ||
| 619 | } | ||
| 620 | }; | ||
| 621 | |||
| 622 | # Create a patch. | ||
| 623 | my $autopatch = File::Spec->catfile($dir, 'debian', 'patches', | ||
| 624 | $self->get_autopatch_name()); | ||
| 625 | my $tmpdiff = $self->_generate_patch($dir, | ||
| 626 | order_from => $autopatch, | ||
| 627 | header_from => $autopatch, | ||
| 628 | handle_binary => $handle_binary, | ||
| 629 | skip_auto => $self->{options}{auto_commit}, | ||
| 630 | usage => 'build', | ||
| 631 | ); | ||
| 632 | unless (-z $tmpdiff or $self->{options}{auto_commit}) { | ||
| 633 | hint(g_('make sure the version in debian/changelog matches ' . | ||
| 634 | 'the unpacked source tree')); | ||
| 635 | hint(g_('you can integrate the local changes with %s'), | ||
| 636 | 'dpkg-source --commit'); | ||
| 637 | error(g_('aborting due to unexpected upstream changes, see %s'), | ||
| 638 | $tmpdiff); | ||
| 639 | } | ||
| 640 | push_exit_handler(sub { unlink($tmpdiff) }); | ||
| 641 | $binaryfiles->update_debian_source_include_binaries() if $include_binaries; | ||
| 642 | |||
| 643 | # Install the diff as the new autopatch. | ||
| 644 | if ($self->{options}{auto_commit}) { | ||
| 645 | make_path(File::Spec->catdir($dir, 'debian', 'patches')); | ||
| 646 | $autopatch = $self->register_patch($dir, $tmpdiff, | ||
| 647 | $self->get_autopatch_name()); | ||
| 648 | info(g_('local changes have been recorded in a new patch: %s'), | ||
| 649 | $autopatch) if -e $autopatch; | ||
| 650 | # No check when removing directory on purpose. | ||
| 651 | rmdir(File::Spec->catdir($dir, 'debian', 'patches')); | ||
| 652 | } | ||
| 653 | unlink($tmpdiff) or syserr(g_('cannot remove %s'), $tmpdiff); | ||
| 654 | pop_exit_handler(); | ||
| 655 | |||
| 656 | # Create the "debian.tar". | ||
| 657 | my $debianfile = File::Spec->catfile($self->{basedir}, "$basenamerev.debian.tar." . $self->{options}{comp_ext}); | ||
| 658 | info(g_('building %s in %s'), $sourcepackage, $debianfile); | ||
| 659 | my $tar = Dpkg::Source::Archive->new( | ||
| 660 | filename => $debianfile, | ||
| 661 | compression_level => $self->{options}{comp_level}, | ||
| 662 | ); | ||
| 663 | $tar->create( | ||
| 664 | options => \@tar_ignore, | ||
| 665 | chdir => $dir, | ||
| 666 | ); | ||
| 667 | $tar->add_directory('debian'); | ||
| 668 | foreach my $binary ($binaryfiles->get_seen_binaries()) { | ||
| 669 | $tar->add_file($binary) unless $binary =~ m{^debian/}; | ||
| 670 | } | ||
| 671 | $tar->finish(); | ||
| 672 | |||
| 673 | $self->add_file($debianfile); | ||
| 674 | } | ||
| 675 | |||
| 676 | sub _get_patch_header { | ||
| 677 | my ($self, $dir) = @_; | ||
| 678 | |||
| 679 | my $ph = File::Spec->catfile($dir, 'debian', 'source', 'local-patch-header'); | ||
| 680 | unless (-f $ph) { | ||
| 681 | $ph = File::Spec->catfile($dir, 'debian', 'source', 'patch-header'); | ||
| 682 | } | ||
| 683 | if (-f $ph) { | ||
| 684 | return file_slurp($ph); | ||
| 685 | } | ||
| 686 | |||
| 687 | if ($self->{options}->{single_debian_patch}) { | ||
| 688 | return <<'AUTOGEN_HEADER'; | ||
| 689 | Description: Autogenerated patch header for a single-debian-patch file. | ||
| 690 | The delta against upstream is either kept as a single patch, or maintained | ||
| 691 | in some VCS, and exported as a single patch instead of more manageable | ||
| 692 | atomic patches. | ||
| 693 | Forwarded: not-needed | ||
| 694 | |||
| 695 | --- | ||
| 696 | AUTOGEN_HEADER | ||
| 697 | } | ||
| 698 | |||
| 699 | my $ch_info = changelog_parse( | ||
| 700 | filename => $self->{options}{changelog_file}, | ||
| 701 | offset => 0, | ||
| 702 | count => 1, | ||
| 703 | ); | ||
| 704 | return '' if not defined $ch_info; | ||
| 705 | |||
| 706 | my $patch_date = POSIX::strftime('%b, %d %Y %T %z', gmtime); | ||
| 707 | my $text = <<"HEADER"; | ||
| 708 | From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 | ||
| 709 | From: $ch_info->{'Maintainer'} | ||
| 710 | Date: $patch_date | ||
| 711 | Subject: [PATCH] <short summary of the patch> | ||
| 712 | |||
| 713 | TODO: Put a short summary on the line above and replace this paragraph | ||
| 714 | with a longer explanation of this change. Complete the meta-information | ||
| 715 | with other relevant fields (see below for details). To make it easier, the | ||
| 716 | information below has been extracted from the changelog. Adjust it or drop | ||
| 717 | it. | ||
| 718 | |||
| 719 | HEADER | ||
| 720 | |||
| 721 | run_vendor_hook('extend-patch-header', \$text, $ch_info); | ||
| 722 | |||
| 723 | $text .= <<'TRAILER'; | ||
| 724 | --- | ||
| 725 | The information above should follow the Patch Tagging Guidelines, | ||
| 726 | see https://dep.debian.net/deps/dep3/ to learn about the format. Here | ||
| 727 | are templates for supplementary fields that you might want to add: | ||
| 728 | |||
| 729 | Origin: (upstream|backport|vendor|other), (<patch-url>|commit:<commit-id>) | ||
| 730 | Bug: <upstream-bugtracker-url> | ||
| 731 | Bug-<Vendor>: <vendor-bugtracker-url> | ||
| 732 | Forwarded: (no|not-needed|<patch-forwarded-url>) | ||
| 733 | Applied-Upstream: <version>, (<commit-url>|commit:<commid-id>) | ||
| 734 | Reviewed-By: <name and email of someone who approved/reviewed the patch> | ||
| 735 | |||
| 736 | TRAILER | ||
| 737 | |||
| 738 | return $text; | ||
| 739 | } | ||
| 740 | |||
| 741 | sub register_patch { | ||
| 742 | my ($self, $dir, $patch_file, $patch_name) = @_; | ||
| 743 | my $patch = File::Spec->catfile($dir, 'debian', 'patches', $patch_name); | ||
| 744 | if (-s $patch_file) { | ||
| 745 | copy($patch_file, $patch) | ||
| 746 | or syserr(g_('cannot copy %s to %s'), $patch_file, $patch); | ||
| 747 | chmod_if_needed(0o666 & ~ umask(), $patch) | ||
| 748 | or syserr(g_("cannot change permission of '%s'"), $patch); | ||
| 749 | my $applied = File::Spec->catfile($dir, 'debian', 'patches', '.dpkg-source-applied'); | ||
| 750 | open(my $applied_fh, '>>', $applied) | ||
| 751 | or syserr(g_('cannot write %s'), $applied); | ||
| 752 | print { $applied_fh } "$patch\n"; | ||
| 753 | close($applied_fh) or syserr(g_('cannot close %s'), $applied); | ||
| 754 | } elsif (-e $patch) { | ||
| 755 | unlink($patch) or syserr(g_('cannot remove %s'), $patch); | ||
| 756 | } | ||
| 757 | return $patch; | ||
| 758 | } | ||
| 759 | |||
| 760 | sub _is_bad_patch_name { | ||
| 761 | my ($dir, $patch_name) = @_; | ||
| 762 | |||
| 763 | return 1 if not defined($patch_name); | ||
| 764 | return 1 if not length($patch_name); | ||
| 765 | |||
| 766 | my $patch = File::Spec->catfile($dir, 'debian', 'patches', $patch_name); | ||
| 767 | if (-e $patch) { | ||
| 768 | warning(g_('cannot register changes in %s, this patch already exists'), | ||
| 769 | $patch); | ||
| 770 | return 1; | ||
| 771 | } | ||
| 772 | return 0; | ||
| 773 | } | ||
| 774 | |||
| 775 | sub do_commit { | ||
| 776 | my ($self, $dir) = @_; | ||
| 777 | my ($patch_name, $tmpdiff) = @{$self->{options}{ARGV}}; | ||
| 778 | |||
| 779 | $self->prepare_build($dir); | ||
| 780 | |||
| 781 | # Try to fix up a broken relative filename for the patch. | ||
| 782 | if ($tmpdiff and not -e $tmpdiff) { | ||
| 783 | $tmpdiff = File::Spec->catfile($dir, $tmpdiff) | ||
| 784 | unless File::Spec->file_name_is_absolute($tmpdiff); | ||
| 785 | error(g_("patch file '%s' does not exist"), $tmpdiff) | ||
| 786 | if not -e $tmpdiff; | ||
| 787 | } | ||
| 788 | |||
| 789 | my $binaryfiles = Dpkg::Source::BinaryFiles->new($dir); | ||
| 790 | my $handle_binary = sub { | ||
| 791 | my ($self, $old, $new, %opts) = @_; | ||
| 792 | my $fn = File::Spec->abs2rel($new, $dir); | ||
| 793 | $binaryfiles->new_binary_found($fn); | ||
| 794 | }; | ||
| 795 | |||
| 796 | unless ($tmpdiff) { | ||
| 797 | $tmpdiff = $self->_generate_patch($dir, | ||
| 798 | handle_binary => $handle_binary, | ||
| 799 | usage => 'commit', | ||
| 800 | ); | ||
| 801 | $binaryfiles->update_debian_source_include_binaries(); | ||
| 802 | } | ||
| 803 | push_exit_handler(sub { unlink($tmpdiff) }); | ||
| 804 | unless (-s $tmpdiff) { | ||
| 805 | unlink($tmpdiff) or syserr(g_('cannot remove %s'), $tmpdiff); | ||
| 806 | info(g_('there are no local changes to record')); | ||
| 807 | return; | ||
| 808 | } | ||
| 809 | while (_is_bad_patch_name($dir, $patch_name)) { | ||
| 810 | # Ask the patch name interactively. | ||
| 811 | print g_('Enter the desired patch name: '); | ||
| 812 | $patch_name = <STDIN>; | ||
| 813 | if (not defined $patch_name) { | ||
| 814 | error(g_('no patch name given; cannot proceed')); | ||
| 815 | } | ||
| 816 | chomp $patch_name; | ||
| 817 | $patch_name =~ s/\s+/-/g; | ||
| 818 | $patch_name =~ s/\///g; | ||
| 819 | } | ||
| 820 | make_path(File::Spec->catdir($dir, 'debian', 'patches')); | ||
| 821 | my $patch = $self->register_patch($dir, $tmpdiff, $patch_name); | ||
| 822 | my @editors = ( | ||
| 823 | 'sensible-editor', | ||
| 824 | $ENV{VISUAL}, | ||
| 825 | $ENV{EDITOR}, | ||
| 826 | 'vi', | ||
| 827 | ); | ||
| 828 | my $editor = first { find_command($_) } @editors; | ||
| 829 | if (not $editor) { | ||
| 830 | error(g_('cannot find an editor')); | ||
| 831 | } | ||
| 832 | system($editor, $patch); | ||
| 833 | subprocerr($editor) if $?; | ||
| 834 | unlink($tmpdiff) or syserr(g_('cannot remove %s'), $tmpdiff); | ||
| 835 | pop_exit_handler(); | ||
| 836 | info(g_('local changes have been recorded in a new patch: %s'), $patch); | ||
| 837 | } | ||
| 838 | |||
| 839 | =head1 CHANGES | ||
| 840 | |||
| 841 | =head2 Version 0.xx | ||
| 842 | |||
| 843 | This is a private module. | ||
| 844 | |||
| 845 | =cut | ||
| 846 | |||
| 847 | 1; |