V2.pm 27 KB

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