V2.pm 28 KB

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