V2.pm 27 KB

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