V2.pm 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. # Copyright © 2008-2011 Raphaël Hertzog <hertzog@debian.org>
  2. # Copyright © 2008-2014 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. package Dpkg::Source::Package::V2;
  17. use strict;
  18. use warnings;
  19. our $VERSION = '0.01';
  20. use parent qw(Dpkg::Source::Package);
  21. use Dpkg;
  22. use Dpkg::Gettext;
  23. use Dpkg::ErrorHandling;
  24. use Dpkg::File;
  25. use Dpkg::Path qw(find_command);
  26. use Dpkg::Compression;
  27. use Dpkg::Source::Archive;
  28. use Dpkg::Source::Patch;
  29. use Dpkg::Exit qw(push_exit_handler pop_exit_handler);
  30. use Dpkg::Source::Functions qw(erasedir is_binary fs_time);
  31. use Dpkg::Vendor qw(run_vendor_hook);
  32. use Dpkg::Control;
  33. use Dpkg::Changelog::Parse;
  34. use List::Util qw(first);
  35. use POSIX qw(:errno_h);
  36. use Cwd;
  37. use File::Basename;
  38. use File::Temp qw(tempfile tempdir);
  39. use File::Path;
  40. use File::Spec;
  41. use File::Find;
  42. use File::Copy;
  43. our $CURRENT_MINOR_VERSION = '0';
  44. sub init_options {
  45. my ($self) = @_;
  46. $self->SUPER::init_options();
  47. $self->{options}{include_removal} = 0
  48. unless exists $self->{options}{include_removal};
  49. $self->{options}{include_timestamp} = 0
  50. unless exists $self->{options}{include_timestamp};
  51. $self->{options}{include_binaries} = 0
  52. unless exists $self->{options}{include_binaries};
  53. $self->{options}{preparation} = 1
  54. unless exists $self->{options}{preparation};
  55. $self->{options}{skip_patches} = 0
  56. unless exists $self->{options}{skip_patches};
  57. $self->{options}{unapply_patches} = 'auto'
  58. unless exists $self->{options}{unapply_patches};
  59. $self->{options}{skip_debianization} = 0
  60. unless exists $self->{options}{skip_debianization};
  61. $self->{options}{create_empty_orig} = 0
  62. unless exists $self->{options}{create_empty_orig};
  63. $self->{options}{auto_commit} = 0
  64. unless exists $self->{options}{auto_commit};
  65. $self->{options}{ignore_bad_version} = 0
  66. unless exists $self->{options}{ignore_bad_version};
  67. }
  68. sub parse_cmdline_option {
  69. my ($self, $opt) = @_;
  70. if ($opt =~ /^--include-removal$/) {
  71. $self->{options}{include_removal} = 1;
  72. return 1;
  73. } elsif ($opt =~ /^--include-timestamp$/) {
  74. $self->{options}{include_timestamp} = 1;
  75. return 1;
  76. } elsif ($opt =~ /^--include-binaries$/) {
  77. $self->{options}{include_binaries} = 1;
  78. return 1;
  79. } elsif ($opt =~ /^--no-preparation$/) {
  80. $self->{options}{preparation} = 0;
  81. return 1;
  82. } elsif ($opt =~ /^--skip-patches$/) {
  83. $self->{options}{skip_patches} = 1;
  84. return 1;
  85. } elsif ($opt =~ /^--unapply-patches$/) {
  86. $self->{options}{unapply_patches} = 'yes';
  87. return 1;
  88. } elsif ($opt =~ /^--no-unapply-patches$/) {
  89. $self->{options}{unapply_patches} = 'no';
  90. return 1;
  91. } elsif ($opt =~ /^--skip-debianization$/) {
  92. $self->{options}{skip_debianization} = 1;
  93. return 1;
  94. } elsif ($opt =~ /^--create-empty-orig$/) {
  95. $self->{options}{create_empty_orig} = 1;
  96. return 1;
  97. } elsif ($opt =~ /^--abort-on-upstream-changes$/) {
  98. $self->{options}{auto_commit} = 0;
  99. return 1;
  100. } elsif ($opt =~ /^--auto-commit$/) {
  101. $self->{options}{auto_commit} = 1;
  102. return 1;
  103. } elsif ($opt =~ /^--ignore-bad-version$/) {
  104. $self->{options}{ignore_bad_version} = 1;
  105. return 1;
  106. }
  107. return 0;
  108. }
  109. sub do_extract {
  110. my ($self, $newdirectory) = @_;
  111. my $fields = $self->{fields};
  112. my $dscdir = $self->{basedir};
  113. my $basename = $self->get_basename();
  114. my $basenamerev = $self->get_basename(1);
  115. my ($tarfile, $debianfile, %origtar, %seen);
  116. my $re_ext = compression_get_file_extension_regex();
  117. foreach my $file ($self->get_files()) {
  118. (my $uncompressed = $file) =~ s/\.$re_ext$//;
  119. error(_g('duplicate files in %s source package: %s.*'), 'v2.0',
  120. $uncompressed) if $seen{$uncompressed};
  121. $seen{$uncompressed} = 1;
  122. if ($file =~ /^\Q$basename\E\.orig\.tar\.$re_ext$/) {
  123. $tarfile = $file;
  124. } elsif ($file =~ /^\Q$basename\E\.orig-([[:alnum:]-]+)\.tar\.$re_ext$/) {
  125. $origtar{$1} = $file;
  126. } elsif ($file =~ /^\Q$basenamerev\E\.debian\.tar\.$re_ext$/) {
  127. $debianfile = $file;
  128. } else {
  129. error(_g('unrecognized file for a %s source package: %s'),
  130. 'v2.0', $file);
  131. }
  132. }
  133. unless ($tarfile and $debianfile) {
  134. error(_g('missing orig.tar or debian.tar file in v2.0 source package'));
  135. }
  136. erasedir($newdirectory);
  137. # Extract main tarball
  138. info(_g('unpacking %s'), $tarfile);
  139. my $tar = Dpkg::Source::Archive->new(filename => "$dscdir$tarfile");
  140. $tar->extract($newdirectory, no_fixperms => 1,
  141. options => [ '--anchored', '--no-wildcards-match-slash',
  142. '--exclude', '*/.pc', '--exclude', '.pc' ]);
  143. # The .pc exclusion is only needed for 3.0 (quilt) and to avoid
  144. # having an upstream tarball provide a directory with symlinks
  145. # that would be blindly followed when applying the patches
  146. # Extract additional orig tarballs
  147. foreach my $subdir (sort keys %origtar) {
  148. my $file = $origtar{$subdir};
  149. info(_g('unpacking %s'), $file);
  150. if (-e "$newdirectory/$subdir") {
  151. warning(_g("required removal of `%s' installed by original tarball"), $subdir);
  152. erasedir("$newdirectory/$subdir");
  153. }
  154. $tar = Dpkg::Source::Archive->new(filename => "$dscdir$file");
  155. $tar->extract("$newdirectory/$subdir", no_fixperms => 1);
  156. }
  157. # Stop here if debianization is not wanted
  158. return if $self->{options}{skip_debianization};
  159. # Extract debian tarball after removing the debian directory
  160. info(_g('unpacking %s'), $debianfile);
  161. erasedir("$newdirectory/debian");
  162. # Exclude existing symlinks from extraction of debian.tar.gz as we
  163. # don't want to overwrite something outside of $newdirectory due to a
  164. # symlink
  165. my @exclude_symlinks;
  166. my $wanted = sub {
  167. return if not -l $_;
  168. my $fn = File::Spec->abs2rel($_, $newdirectory);
  169. push @exclude_symlinks, '--exclude', $fn;
  170. };
  171. find({ wanted => $wanted, no_chdir => 1 }, $newdirectory);
  172. $tar = Dpkg::Source::Archive->new(filename => "$dscdir$debianfile");
  173. $tar->extract($newdirectory, in_place => 1,
  174. options => [ '--anchored', '--no-wildcards',
  175. @exclude_symlinks ]);
  176. # Apply patches (in a separate method as it might be overriden)
  177. $self->apply_patches($newdirectory, usage => 'unpack')
  178. unless $self->{options}{skip_patches};
  179. }
  180. sub get_autopatch_name {
  181. return 'zz_debian-diff-auto';
  182. }
  183. sub get_patches {
  184. my ($self, $dir, %opts) = @_;
  185. $opts{skip_auto} //= 0;
  186. my @patches;
  187. my $pd = "$dir/debian/patches";
  188. my $auto_patch = $self->get_autopatch_name();
  189. if (-d $pd) {
  190. opendir(my $dir_dh, $pd) or syserr(_g('cannot opendir %s'), $pd);
  191. foreach my $patch (sort readdir($dir_dh)) {
  192. # patches match same rules as run-parts
  193. next unless $patch =~ /^[\w-]+$/ and -f "$pd/$patch";
  194. next if $opts{skip_auto} and $patch eq $auto_patch;
  195. push @patches, $patch;
  196. }
  197. closedir($dir_dh);
  198. }
  199. return @patches;
  200. }
  201. sub apply_patches {
  202. my ($self, $dir, %opts) = @_;
  203. $opts{skip_auto} //= 0;
  204. my @patches = $self->get_patches($dir, %opts);
  205. return unless scalar(@patches);
  206. my $applied = File::Spec->catfile($dir, 'debian', 'patches', '.dpkg-source-applied');
  207. open(my $applied_fh, '>', $applied)
  208. or syserr(_g('cannot write %s'), $applied);
  209. print { $applied_fh } "# During $opts{usage}\n";
  210. my $timestamp = fs_time($applied);
  211. foreach my $patch ($self->get_patches($dir, %opts)) {
  212. my $path = File::Spec->catfile($dir, 'debian', 'patches', $patch);
  213. info(_g('applying %s'), $patch) unless $opts{skip_auto};
  214. my $patch_obj = Dpkg::Source::Patch->new(filename => $path);
  215. $patch_obj->apply($dir, force_timestamp => 1,
  216. timestamp => $timestamp,
  217. add_options => [ '-E' ]);
  218. print { $applied_fh } "$patch\n";
  219. }
  220. close($applied_fh);
  221. }
  222. sub unapply_patches {
  223. my ($self, $dir, %opts) = @_;
  224. my @patches = reverse($self->get_patches($dir, %opts));
  225. return unless scalar(@patches);
  226. my $applied = File::Spec->catfile($dir, 'debian', 'patches', '.dpkg-source-applied');
  227. my $timestamp = fs_time($applied);
  228. foreach my $patch (@patches) {
  229. my $path = File::Spec->catfile($dir, 'debian', 'patches', $patch);
  230. info(_g('unapplying %s'), $patch) unless $opts{quiet};
  231. my $patch_obj = Dpkg::Source::Patch->new(filename => $path);
  232. $patch_obj->apply($dir, force_timestamp => 1, verbose => 0,
  233. timestamp => $timestamp,
  234. add_options => [ '-E', '-R' ]);
  235. }
  236. unlink($applied);
  237. }
  238. sub upstream_tarball_template {
  239. my ($self) = @_;
  240. my $ext = '{' . join(',',
  241. sort map {
  242. compression_get_property($_, 'file_ext')
  243. } compression_get_list()) . '}';
  244. return '../' . $self->get_basename() . ".orig.tar.$ext";
  245. }
  246. sub can_build {
  247. my ($self, $dir) = @_;
  248. return 1 if $self->find_original_tarballs(include_supplementary => 0);
  249. return 1 if $self->{options}{create_empty_orig} and
  250. $self->find_original_tarballs(include_main => 0);
  251. return (0, sprintf(_g('no upstream tarball found at %s'),
  252. $self->upstream_tarball_template()));
  253. }
  254. sub before_build {
  255. my ($self, $dir) = @_;
  256. $self->check_patches_applied($dir) if $self->{options}{preparation};
  257. }
  258. sub after_build {
  259. my ($self, $dir) = @_;
  260. my $applied = File::Spec->catfile($dir, 'debian', 'patches', '.dpkg-source-applied');
  261. my $reason = '';
  262. if (-e $applied) {
  263. open(my $applied_fh, '<', $applied)
  264. or syserr(_g('cannot read %s'), $applied);
  265. $reason = <$applied_fh>;
  266. close($applied_fh);
  267. }
  268. my $opt_unapply = $self->{options}{unapply_patches};
  269. if (($opt_unapply eq 'auto' and $reason =~ /^# During preparation/) or
  270. $opt_unapply eq 'yes') {
  271. $self->unapply_patches($dir);
  272. }
  273. }
  274. sub prepare_build {
  275. my ($self, $dir) = @_;
  276. $self->{diff_options} = {
  277. diff_ignore_regex => $self->{options}{diff_ignore_regex} .
  278. '|(^|/)debian/patches/.dpkg-source-applied$',
  279. include_removal => $self->{options}{include_removal},
  280. include_timestamp => $self->{options}{include_timestamp},
  281. use_dev_null => 1,
  282. };
  283. push @{$self->{options}{tar_ignore}}, 'debian/patches/.dpkg-source-applied';
  284. $self->check_patches_applied($dir) if $self->{options}{preparation};
  285. if ($self->{options}{create_empty_orig} and
  286. not $self->find_original_tarballs(include_supplementary => 0))
  287. {
  288. # No main orig.tar, create a dummy one
  289. my $filename = $self->get_basename() . '.orig.tar.' .
  290. $self->{options}{comp_ext};
  291. my $tar = Dpkg::Source::Archive->new(filename => $filename,
  292. compression_level => $self->{options}{comp_level});
  293. $tar->create();
  294. $tar->finish();
  295. }
  296. }
  297. sub check_patches_applied {
  298. my ($self, $dir) = @_;
  299. my $applied = File::Spec->catfile($dir, 'debian', 'patches', '.dpkg-source-applied');
  300. unless (-e $applied) {
  301. info(_g('patches are not applied, applying them now'));
  302. $self->apply_patches($dir, usage => 'preparation');
  303. }
  304. }
  305. sub generate_patch {
  306. my ($self, $dir, %opts) = @_;
  307. my ($dirname, $updir) = fileparse($dir);
  308. my $basedirname = $self->get_basename();
  309. $basedirname =~ s/_/-/;
  310. # Identify original tarballs
  311. my ($tarfile, %origtar);
  312. my $comp_ext_regex = compression_get_file_extension_regex();
  313. my @origtarballs;
  314. foreach (sort $self->find_original_tarballs()) {
  315. if (/\.orig\.tar\.$comp_ext_regex$/) {
  316. if (defined($tarfile)) {
  317. error(_g('several orig.tar files found (%s and %s) but only ' .
  318. 'one is allowed'), $tarfile, $_);
  319. }
  320. $tarfile = $_;
  321. push @origtarballs, $_;
  322. $self->add_file($_);
  323. } elsif (/\.orig-([[:alnum:]-]+)\.tar\.$comp_ext_regex$/) {
  324. $origtar{$1} = $_;
  325. push @origtarballs, $_;
  326. $self->add_file($_);
  327. }
  328. }
  329. error(_g('no upstream tarball found at %s'),
  330. $self->upstream_tarball_template()) unless $tarfile;
  331. if ($opts{usage} eq 'build') {
  332. info(_g('building %s using existing %s'),
  333. $self->{fields}{'Source'}, "@origtarballs");
  334. }
  335. # Unpack a second copy for comparison
  336. my $tmp = tempdir("$dirname.orig.XXXXXX", DIR => $updir);
  337. push_exit_handler(sub { erasedir($tmp) });
  338. # Extract main tarball
  339. my $tar = Dpkg::Source::Archive->new(filename => $tarfile);
  340. $tar->extract($tmp);
  341. # Extract additional orig tarballs
  342. foreach my $subdir (keys %origtar) {
  343. my $file = $origtar{$subdir};
  344. $tar = Dpkg::Source::Archive->new(filename => $file);
  345. $tar->extract("$tmp/$subdir");
  346. }
  347. # Copy over the debian directory
  348. erasedir("$tmp/debian");
  349. system('cp', '-a', '--', "$dir/debian", "$tmp/");
  350. subprocerr(_g('copy of the debian directory')) if $?;
  351. # Apply all patches except the last automatic one
  352. $opts{skip_auto} //= 0;
  353. $self->apply_patches($tmp, skip_auto => $opts{skip_auto}, usage => 'build');
  354. # Create a patch
  355. my ($difffh, $tmpdiff) = tempfile($self->get_basename(1) . '.diff.XXXXXX',
  356. DIR => File::Spec->tmpdir(), UNLINK => 0);
  357. push_exit_handler(sub { unlink($tmpdiff) });
  358. my $diff = Dpkg::Source::Patch->new(filename => $tmpdiff,
  359. compression => 'none');
  360. $diff->create();
  361. if ($opts{header_from} and -e $opts{header_from}) {
  362. my $header_from = Dpkg::Source::Patch->new(
  363. filename => $opts{header_from});
  364. my $analysis = $header_from->analyze($dir, verbose => 0);
  365. $diff->set_header($analysis->{patchheader});
  366. } else {
  367. $diff->set_header($self->get_patch_header($dir));
  368. }
  369. $diff->add_diff_directory($tmp, $dir, basedirname => $basedirname,
  370. %{$self->{diff_options}},
  371. handle_binary_func => $opts{handle_binary},
  372. order_from => $opts{order_from});
  373. error(_g('unrepresentable changes to source')) if not $diff->finish();
  374. if (-s $tmpdiff) {
  375. info(_g('local changes detected, the modified files are:'));
  376. my $analysis = $diff->analyze($dir, verbose => 0);
  377. foreach my $fn (sort keys %{$analysis->{filepatched}}) {
  378. print " $fn\n";
  379. }
  380. }
  381. # Remove the temporary directory
  382. erasedir($tmp);
  383. pop_exit_handler();
  384. pop_exit_handler();
  385. return $tmpdiff;
  386. }
  387. sub do_build {
  388. my ($self, $dir) = @_;
  389. my @argv = @{$self->{options}{ARGV}};
  390. if (scalar(@argv)) {
  391. usageerr(_g("-b takes only one parameter with format `%s'"),
  392. $self->{fields}{'Format'});
  393. }
  394. $self->prepare_build($dir);
  395. my $include_binaries = $self->{options}{include_binaries};
  396. my @tar_ignore = map { "--exclude=$_" } @{$self->{options}{tar_ignore}};
  397. my $sourcepackage = $self->{fields}{'Source'};
  398. my $basenamerev = $self->get_basename(1);
  399. # Check if the debian directory contains unwanted binary files
  400. my $binaryfiles = Dpkg::Source::Package::V2::BinaryFiles->new($dir);
  401. my $unwanted_binaries = 0;
  402. my $check_binary = sub {
  403. if (-f $_ and is_binary($_)) {
  404. my $fn = File::Spec->abs2rel($_, $dir);
  405. $binaryfiles->new_binary_found($fn);
  406. unless ($include_binaries or $binaryfiles->binary_is_allowed($fn)) {
  407. errormsg(_g('unwanted binary file: %s'), $fn);
  408. $unwanted_binaries++;
  409. }
  410. }
  411. };
  412. my $tar_ignore_glob = '{' . join(',',
  413. map {
  414. my $copy = $_;
  415. $copy =~ s/,/\\,/g;
  416. $copy;
  417. } @{$self->{options}{tar_ignore}}) . '}';
  418. my $filter_ignore = sub {
  419. # Filter out files that are not going to be included in the debian
  420. # tarball due to ignores.
  421. my %exclude;
  422. my $reldir = File::Spec->abs2rel($File::Find::dir, $dir);
  423. my $cwd = getcwd();
  424. # Apply the pattern both from the top dir and from the inspected dir
  425. chdir($dir) or syserr(_g("unable to chdir to `%s'"), $dir);
  426. $exclude{$_} = 1 foreach glob($tar_ignore_glob);
  427. chdir($cwd) or syserr(_g("unable to chdir to `%s'"), $cwd);
  428. chdir($File::Find::dir)
  429. or syserr(_g("unable to chdir to `%s'"), $File::Find::dir);
  430. $exclude{$_} = 1 foreach glob($tar_ignore_glob);
  431. chdir($cwd) or syserr(_g("unable to chdir to `%s'"), $cwd);
  432. my @result;
  433. foreach my $fn (@_) {
  434. unless (exists $exclude{$fn} or exists $exclude{"$reldir/$fn"}) {
  435. push @result, $fn;
  436. }
  437. }
  438. return @result;
  439. };
  440. find({ wanted => $check_binary, preprocess => $filter_ignore,
  441. no_chdir => 1 }, File::Spec->catdir($dir, 'debian'));
  442. error(P_('detected %d unwanted binary file (add it in ' .
  443. 'debian/source/include-binaries to allow its inclusion).',
  444. 'detected %d unwanted binary files (add them in ' .
  445. 'debian/source/include-binaries to allow their inclusion).',
  446. $unwanted_binaries), $unwanted_binaries)
  447. if $unwanted_binaries;
  448. # Handle modified binary files detected by the auto-patch generation
  449. my $handle_binary = sub {
  450. my ($self, $old, $new) = @_;
  451. my $relfn = File::Spec->abs2rel($new, $dir);
  452. $binaryfiles->new_binary_found($relfn);
  453. unless ($include_binaries or $binaryfiles->binary_is_allowed($relfn)) {
  454. errormsg(_g('cannot represent change to %s: %s'), $relfn,
  455. _g('binary file contents changed'));
  456. errormsg(_g('add %s in debian/source/include-binaries if you want ' .
  457. 'to store the modified binary in the debian tarball'),
  458. $relfn);
  459. $self->register_error();
  460. }
  461. };
  462. # Create a patch
  463. my $autopatch = File::Spec->catfile($dir, 'debian', 'patches',
  464. $self->get_autopatch_name());
  465. my $tmpdiff = $self->generate_patch($dir, order_from => $autopatch,
  466. header_from => $autopatch,
  467. handle_binary => $handle_binary,
  468. skip_auto => $self->{options}{auto_commit},
  469. usage => 'build');
  470. unless (-z $tmpdiff or $self->{options}{auto_commit}) {
  471. info(_g('you can integrate the local changes with %s'),
  472. 'dpkg-source --commit');
  473. error(_g('aborting due to unexpected upstream changes, see %s'),
  474. $tmpdiff);
  475. }
  476. push_exit_handler(sub { unlink($tmpdiff) });
  477. $binaryfiles->update_debian_source_include_binaries() if $include_binaries;
  478. # Install the diff as the new autopatch
  479. if ($self->{options}{auto_commit}) {
  480. mkpath(File::Spec->catdir($dir, 'debian', 'patches'));
  481. $autopatch = $self->register_patch($dir, $tmpdiff,
  482. $self->get_autopatch_name());
  483. info(_g('local changes have been recorded in a new patch: %s'),
  484. $autopatch) if -e $autopatch;
  485. rmdir(File::Spec->catdir($dir, 'debian', 'patches')); # No check on purpose
  486. }
  487. unlink($tmpdiff) or syserr(_g('cannot remove %s'), $tmpdiff);
  488. pop_exit_handler();
  489. # Create the debian.tar
  490. my $debianfile = "$basenamerev.debian.tar." . $self->{options}{comp_ext};
  491. info(_g('building %s in %s'), $sourcepackage, $debianfile);
  492. my $tar = Dpkg::Source::Archive->new(filename => $debianfile,
  493. compression_level => $self->{options}{comp_level});
  494. $tar->create(options => \@tar_ignore, chdir => $dir);
  495. $tar->add_directory('debian');
  496. foreach my $binary ($binaryfiles->get_seen_binaries()) {
  497. $tar->add_file($binary) unless $binary =~ m{^debian/};
  498. }
  499. $tar->finish();
  500. $self->add_file($debianfile);
  501. }
  502. sub get_patch_header {
  503. my ($self, $dir) = @_;
  504. my $ph = File::Spec->catfile($dir, 'debian', 'source', 'local-patch-header');
  505. unless (-f $ph) {
  506. $ph = File::Spec->catfile($dir, 'debian', 'source', 'patch-header');
  507. }
  508. my $text;
  509. if (-f $ph) {
  510. open(my $ph_fh, '<', $ph) or syserr(_g('cannot read %s'), $ph);
  511. $text = file_slurp($ph_fh);
  512. close($ph_fh);
  513. return $text;
  514. }
  515. my $ch_info = changelog_parse(offset => 0, count => 1,
  516. file => File::Spec->catfile($dir, 'debian', 'changelog'));
  517. return '' if not defined $ch_info;
  518. my $header = Dpkg::Control->new(type => CTRL_UNKNOWN);
  519. $header->{'Description'} = "<short summary of the patch>\n";
  520. $header->{'Description'} .=
  521. "TODO: Put a short summary on the line above and replace this paragraph
  522. with a longer explanation of this change. Complete the meta-information
  523. with other relevant fields (see below for details). To make it easier, the
  524. information below has been extracted from the changelog. Adjust it or drop
  525. it.\n";
  526. $header->{'Description'} .= $ch_info->{'Changes'} . "\n";
  527. $header->{'Author'} = $ch_info->{'Maintainer'};
  528. $text = "$header";
  529. run_vendor_hook('extend-patch-header', \$text, $ch_info);
  530. $text .= "\n---
  531. The information above should follow the Patch Tagging Guidelines, please
  532. checkout http://dep.debian.net/deps/dep3/ to learn about the format. Here
  533. are templates for supplementary fields that you might want to add:
  534. Origin: <vendor|upstream|other>, <url of original patch>
  535. Bug: <url in upstream bugtracker>
  536. Bug-Debian: http://bugs.debian.org/<bugnumber>
  537. Bug-Ubuntu: https://launchpad.net/bugs/<bugnumber>
  538. Forwarded: <no|not-needed|url proving that it has been forwarded>
  539. Reviewed-By: <name and email of someone who approved the patch>
  540. Last-Update: <YYYY-MM-DD>\n\n";
  541. return $text;
  542. }
  543. sub register_patch {
  544. my ($self, $dir, $patch_file, $patch_name) = @_;
  545. my $patch = File::Spec->catfile($dir, 'debian', 'patches', $patch_name);
  546. if (-s $patch_file) {
  547. copy($patch_file, $patch)
  548. or syserr(_g('failed to copy %s to %s'), $patch_file, $patch);
  549. chmod(0666 & ~ umask(), $patch)
  550. or syserr(_g("unable to change permission of `%s'"), $patch);
  551. my $applied = File::Spec->catfile($dir, 'debian', 'patches', '.dpkg-source-applied');
  552. open(my $applied_fh, '>>', $applied)
  553. or syserr(_g('cannot write %s'), $applied);
  554. print { $applied_fh } "$patch\n";
  555. close($applied_fh) or syserr(_g('cannot close %s'), $applied);
  556. } elsif (-e $patch) {
  557. unlink($patch) or syserr(_g('cannot remove %s'), $patch);
  558. }
  559. return $patch;
  560. }
  561. sub _is_bad_patch_name {
  562. my ($dir, $patch_name) = @_;
  563. return 1 if not defined($patch_name);
  564. return 1 if not length($patch_name);
  565. my $patch = File::Spec->catfile($dir, 'debian', 'patches', $patch_name);
  566. if (-e $patch) {
  567. warning(_g('cannot register changes in %s, this patch already exists'),
  568. $patch);
  569. return 1;
  570. }
  571. return 0;
  572. }
  573. sub do_commit {
  574. my ($self, $dir) = @_;
  575. my ($patch_name, $tmpdiff) = @{$self->{options}{ARGV}};
  576. $self->prepare_build($dir);
  577. # Try to fix up a broken relative filename for the patch
  578. if ($tmpdiff and not -e $tmpdiff) {
  579. $tmpdiff = File::Spec->catfile($dir, $tmpdiff)
  580. unless File::Spec->file_name_is_absolute($tmpdiff);
  581. error(_g("patch file '%s' doesn't exist"), $tmpdiff) if not -e $tmpdiff;
  582. }
  583. my $binaryfiles = Dpkg::Source::Package::V2::BinaryFiles->new($dir);
  584. my $handle_binary = sub {
  585. my ($self, $old, $new) = @_;
  586. my $fn = File::Spec->abs2rel($new, $dir);
  587. $binaryfiles->new_binary_found($fn);
  588. };
  589. unless ($tmpdiff) {
  590. $tmpdiff = $self->generate_patch($dir, handle_binary => $handle_binary,
  591. usage => 'commit');
  592. $binaryfiles->update_debian_source_include_binaries();
  593. }
  594. push_exit_handler(sub { unlink($tmpdiff) });
  595. unless (-s $tmpdiff) {
  596. unlink($tmpdiff) or syserr(_g('cannot remove %s'), $tmpdiff);
  597. info(_g('there are no local changes to record'));
  598. return;
  599. }
  600. while (_is_bad_patch_name($dir, $patch_name)) {
  601. # Ask the patch name interactively
  602. print _g('Enter the desired patch name: ');
  603. chomp($patch_name = <STDIN>);
  604. $patch_name =~ s/\s+/-/g;
  605. $patch_name =~ s/\///g;
  606. }
  607. mkpath(File::Spec->catdir($dir, 'debian', 'patches'));
  608. my $patch = $self->register_patch($dir, $tmpdiff, $patch_name);
  609. my @editors = ('sensible-editor', $ENV{VISUAL}, $ENV{EDITOR}, 'vi');
  610. my $editor = first { defined $_ and find_command($_) } @editors;
  611. if (not $editor) {
  612. error(_g('cannot find an editor'));
  613. }
  614. system($editor, $patch);
  615. subprocerr($editor) if $?;
  616. unlink($tmpdiff) or syserr(_g('cannot remove %s'), $tmpdiff);
  617. pop_exit_handler();
  618. info(_g('local changes have been recorded in a new patch: %s'), $patch);
  619. }
  620. package Dpkg::Source::Package::V2::BinaryFiles;
  621. use Dpkg::ErrorHandling;
  622. use Dpkg::Gettext;
  623. use File::Path;
  624. sub new {
  625. my ($this, $dir) = @_;
  626. my $class = ref($this) || $this;
  627. my $self = {
  628. dir => $dir,
  629. allowed_binaries => {},
  630. seen_binaries => {},
  631. include_binaries_path =>
  632. File::Spec->catfile($dir, 'debian', 'source', 'include-binaries'),
  633. };
  634. bless $self, $class;
  635. $self->load_allowed_binaries();
  636. return $self;
  637. }
  638. sub new_binary_found {
  639. my ($self, $path) = @_;
  640. $self->{seen_binaries}{$path} = 1;
  641. }
  642. sub load_allowed_binaries {
  643. my ($self) = @_;
  644. my $incbin_file = $self->{include_binaries_path};
  645. if (-f $incbin_file) {
  646. open(my $incbin_fh, '<', $incbin_file)
  647. or syserr(_g('cannot read %s'), $incbin_file);
  648. while (defined($_ = <$incbin_fh>)) {
  649. chomp; s/^\s*//; s/\s*$//;
  650. next if /^#/ or /^$/;
  651. $self->{allowed_binaries}{$_} = 1;
  652. }
  653. close($incbin_fh);
  654. }
  655. }
  656. sub binary_is_allowed {
  657. my ($self, $path) = @_;
  658. return 1 if exists $self->{allowed_binaries}{$path};
  659. return 0;
  660. }
  661. sub update_debian_source_include_binaries {
  662. my ($self) = @_;
  663. my @unknown_binaries = $self->get_unknown_binaries();
  664. return unless scalar(@unknown_binaries);
  665. my $incbin_file = $self->{include_binaries_path};
  666. mkpath(File::Spec->catdir($self->{dir}, 'debian', 'source'));
  667. open(my $incbin_fh, '>>', $incbin_file)
  668. or syserr(_g('cannot write %s'), $incbin_file);
  669. foreach my $binary (@unknown_binaries) {
  670. print { $incbin_fh } "$binary\n";
  671. info(_g('adding %s to %s'), $binary, 'debian/source/include-binaries');
  672. $self->{allowed_binaries}{$binary} = 1;
  673. }
  674. close($incbin_fh);
  675. }
  676. sub get_unknown_binaries {
  677. my ($self) = @_;
  678. return grep { not $self->binary_is_allowed($_) } $self->get_seen_binaries();
  679. }
  680. sub get_seen_binaries {
  681. my ($self) = @_;
  682. my @seen = sort keys %{$self->{seen_binaries}};
  683. return @seen;
  684. }
  685. 1;