V2.pm 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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(!getcwd);
  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 unless defined($opts{"skip_auto"});
  177. my @patches;
  178. my $pd = "$dir/debian/patches";
  179. my $auto_patch = $self->get_autopatch_name();
  180. if (-d $pd) {
  181. opendir(DIR, $pd) || syserr(_g("cannot opendir %s"), $pd);
  182. foreach my $patch (sort readdir(DIR)) {
  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);
  189. }
  190. return @patches;
  191. }
  192. sub apply_patches {
  193. my ($self, $dir, %opts) = @_;
  194. $opts{"skip_auto"} = 0 unless defined($opts{"skip_auto"});
  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(APPLIED, '>', $applied) || syserr(_g("cannot write %s"), $applied);
  199. print APPLIED "# During $opts{'usage'}\n";
  200. my $timestamp = fs_time($applied);
  201. foreach my $patch ($self->get_patches($dir, %opts)) {
  202. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  203. info(_g("applying %s"), $patch) unless $opts{"skip_auto"};
  204. my $patch_obj = Dpkg::Source::Patch->new(filename => $path);
  205. $patch_obj->apply($dir, force_timestamp => 1,
  206. timestamp => $timestamp,
  207. add_options => [ '-E' ]);
  208. print APPLIED "$patch\n";
  209. }
  210. close(APPLIED);
  211. }
  212. sub unapply_patches {
  213. my ($self, $dir, %opts) = @_;
  214. my @patches = reverse($self->get_patches($dir, %opts));
  215. return unless scalar(@patches);
  216. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  217. my $timestamp = fs_time($applied);
  218. foreach my $patch (@patches) {
  219. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  220. info(_g("unapplying %s"), $patch) unless $opts{"quiet"};
  221. my $patch_obj = Dpkg::Source::Patch->new(filename => $path);
  222. $patch_obj->apply($dir, force_timestamp => 1, verbose => 0,
  223. timestamp => $timestamp,
  224. add_options => [ '-E', '-R' ]);
  225. }
  226. unlink($applied);
  227. }
  228. sub upstream_tarball_template {
  229. my ($self) = @_;
  230. my $ext = "{" . join(",",
  231. sort map {
  232. compression_get_property($_, "file_ext")
  233. } compression_get_list()) . "}";
  234. return "../" . $self->get_basename() . ".orig.tar.$ext";
  235. }
  236. sub can_build {
  237. my ($self, $dir) = @_;
  238. return 1 if $self->find_original_tarballs(include_supplementary => 0);
  239. return 1 if $self->{'options'}{'create_empty_orig'} and
  240. $self->find_original_tarballs(include_main => 0);
  241. return (0, sprintf(_g("no upstream tarball found at %s"),
  242. $self->upstream_tarball_template()));
  243. }
  244. sub before_build {
  245. my ($self, $dir) = @_;
  246. $self->check_patches_applied($dir) if $self->{'options'}{'preparation'};
  247. }
  248. sub after_build {
  249. my ($self, $dir) = @_;
  250. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  251. my $reason = "";
  252. if (-e $applied) {
  253. open(APPLIED, "<", $applied) || syserr(_g("cannot read %s"), $applied);
  254. $reason = <APPLIED>;
  255. close(APPLIED);
  256. }
  257. my $opt_unapply = $self->{'options'}{'unapply_patches'};
  258. if (($opt_unapply eq "auto" and $reason =~ /^# During preparation/) or
  259. $opt_unapply eq "yes") {
  260. $self->unapply_patches($dir);
  261. }
  262. }
  263. sub prepare_build {
  264. my ($self, $dir) = @_;
  265. $self->{'diff_options'} = {
  266. diff_ignore_regexp => $self->{'options'}{'diff_ignore_regexp'} .
  267. '|(^|/)debian/patches/.dpkg-source-applied$',
  268. include_removal => $self->{'options'}{'include_removal'},
  269. include_timestamp => $self->{'options'}{'include_timestamp'},
  270. use_dev_null => 1,
  271. };
  272. push @{$self->{'options'}{'tar_ignore'}}, "debian/patches/.dpkg-source-applied";
  273. $self->check_patches_applied($dir) if $self->{'options'}{'preparation'};
  274. if ($self->{'options'}{'create_empty_orig'} and
  275. not $self->find_original_tarballs(include_supplementary => 0))
  276. {
  277. # No main orig.tar, create a dummy one
  278. my $filename = $self->get_basename() . ".orig.tar." .
  279. $self->{'options'}{'comp_ext'};
  280. my $tar = Dpkg::Source::Archive->new(filename => $filename);
  281. $tar->create();
  282. $tar->finish();
  283. }
  284. }
  285. sub check_patches_applied {
  286. my ($self, $dir) = @_;
  287. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  288. unless (-e $applied) {
  289. info(_g("patches are not applied, applying them now"));
  290. $self->apply_patches($dir, usage => 'preparation');
  291. }
  292. }
  293. sub generate_patch {
  294. my ($self, $dir, %opts) = @_;
  295. my ($dirname, $updir) = fileparse($dir);
  296. my $basedirname = $self->get_basename();
  297. $basedirname =~ s/_/-/;
  298. # Identify original tarballs
  299. my ($tarfile, %origtar);
  300. my @origtarballs;
  301. foreach (sort $self->find_original_tarballs()) {
  302. if (/\.orig\.tar\.$compression_re_file_ext$/) {
  303. if (defined($tarfile)) {
  304. error(_g("several orig.tar files found (%s and %s) but only " .
  305. "one is allowed"), $tarfile, $_);
  306. }
  307. $tarfile = $_;
  308. push @origtarballs, $_;
  309. $self->add_file($_);
  310. } elsif (/\.orig-([[:alnum:]-]+)\.tar\.$compression_re_file_ext$/) {
  311. $origtar{$1} = $_;
  312. push @origtarballs, $_;
  313. $self->add_file($_);
  314. }
  315. }
  316. error(_g("no upstream tarball found at %s"),
  317. $self->upstream_tarball_template()) unless $tarfile;
  318. if ($opts{'usage'} eq "build") {
  319. info(_g("building %s using existing %s"),
  320. $self->{'fields'}{'Source'}, "@origtarballs");
  321. }
  322. # Unpack a second copy for comparison
  323. my $tmp = tempdir("$dirname.orig.XXXXXX", DIR => $updir);
  324. push @Dpkg::Exit::handlers, sub { erasedir($tmp) };
  325. # Extract main tarball
  326. my $tar = Dpkg::Source::Archive->new(filename => $tarfile);
  327. $tar->extract($tmp);
  328. # Extract additional orig tarballs
  329. foreach my $subdir (keys %origtar) {
  330. my $file = $origtar{$subdir};
  331. $tar = Dpkg::Source::Archive->new(filename => $file);
  332. $tar->extract("$tmp/$subdir");
  333. }
  334. # Copy over the debian directory
  335. erasedir("$tmp/debian");
  336. system("cp", "-a", "--", "$dir/debian", "$tmp/");
  337. subprocerr(_g("copy of the debian directory")) if $?;
  338. # Apply all patches except the last automatic one
  339. $opts{'skip_auto'} //= 0;
  340. $self->apply_patches($tmp, skip_auto => $opts{'skip_auto'}, usage => 'build');
  341. # Create a patch
  342. my ($difffh, $tmpdiff) = tempfile($self->get_basename(1) . ".diff.XXXXXX",
  343. DIR => File::Spec->tmpdir(), UNLINK => 0);
  344. push @Dpkg::Exit::handlers, sub { unlink($tmpdiff) };
  345. my $diff = Dpkg::Source::Patch->new(filename => $tmpdiff,
  346. compression => "none");
  347. $diff->create();
  348. if ($opts{'header_from'} and -e $opts{'header_from'}) {
  349. my $header_from = Dpkg::Source::Patch->new(
  350. filename => $opts{'header_from'});
  351. my $analysis = $header_from->analyze($dir, verbose => 0);
  352. $diff->set_header($analysis->{'patchheader'});
  353. } else {
  354. $diff->set_header($self->get_patch_header($dir));
  355. }
  356. $diff->add_diff_directory($tmp, $dir, basedirname => $basedirname,
  357. %{$self->{'diff_options'}},
  358. handle_binary_func => $opts{'handle_binary'},
  359. order_from => $opts{'order_from'});
  360. error(_g("unrepresentable changes to source")) if not $diff->finish();
  361. if (-s $tmpdiff) {
  362. info(_g("local changes detected, the modified files are:"));
  363. my $analysis = $diff->analyze($dir, verbose => 0);
  364. foreach my $fn (sort keys %{$analysis->{'filepatched'}}) {
  365. print " $fn\n";
  366. }
  367. }
  368. # Remove the temporary directory
  369. erasedir($tmp);
  370. pop @Dpkg::Exit::handlers;
  371. pop @Dpkg::Exit::handlers;
  372. return $tmpdiff;
  373. }
  374. sub do_build {
  375. my ($self, $dir) = @_;
  376. my @argv = @{$self->{'options'}{'ARGV'}};
  377. if (scalar(@argv)) {
  378. usageerr(_g("-b takes only one parameter with format `%s'"),
  379. $self->{'fields'}{'Format'});
  380. }
  381. $self->prepare_build($dir);
  382. my $include_binaries = $self->{'options'}{'include_binaries'};
  383. my @tar_ignore = map { "--exclude=$_" } @{$self->{'options'}{'tar_ignore'}};
  384. my $sourcepackage = $self->{'fields'}{'Source'};
  385. my $basenamerev = $self->get_basename(1);
  386. # Check if the debian directory contains unwanted binary files
  387. my $binaryfiles = Dpkg::Source::Package::V2::BinaryFiles->new($dir);
  388. my $unwanted_binaries = 0;
  389. my $check_binary = sub {
  390. if (-f $_ and is_binary($_)) {
  391. my $fn = File::Spec->abs2rel($_, $dir);
  392. $binaryfiles->new_binary_found($fn);
  393. unless ($include_binaries or $binaryfiles->binary_is_allowed($fn)) {
  394. errormsg(_g("unwanted binary file: %s"), $fn);
  395. $unwanted_binaries++;
  396. }
  397. }
  398. };
  399. my $tar_ignore_glob = "{" . join(",",
  400. map {
  401. my $copy = $_;
  402. $copy =~ s/,/\\,/g;
  403. $copy;
  404. } @{$self->{'options'}{'tar_ignore'}}) . "}";
  405. my $filter_ignore = sub {
  406. # Filter out files that are not going to be included in the debian
  407. # tarball due to ignores.
  408. my %exclude;
  409. my $reldir = File::Spec->abs2rel($File::Find::dir, $dir);
  410. my $cwd = getcwd();
  411. # Apply the pattern both from the top dir and from the inspected dir
  412. chdir($dir) || syserr(_g("unable to chdir to `%s'"), $dir);
  413. $exclude{$_} = 1 foreach glob($tar_ignore_glob);
  414. chdir($cwd) || syserr(_g("unable to chdir to `%s'"), $cwd);
  415. chdir($File::Find::dir) ||
  416. syserr(_g("unable to chdir to `%s'"), $File::Find::dir);
  417. $exclude{$_} = 1 foreach glob($tar_ignore_glob);
  418. chdir($cwd) || syserr(_g("unable to chdir to `%s'"), $cwd);
  419. my @result;
  420. foreach my $fn (@_) {
  421. unless (exists $exclude{$fn} or exists $exclude{"$reldir/$fn"}) {
  422. push @result, $fn;
  423. }
  424. }
  425. return @result;
  426. };
  427. find({ wanted => $check_binary, preprocess => $filter_ignore,
  428. no_chdir => 1 }, File::Spec->catdir($dir, "debian"));
  429. error(P_("detected %d unwanted binary file (add it in " .
  430. "debian/source/include-binaries to allow its inclusion).",
  431. "detected %d unwanted binary files (add them in " .
  432. "debian/source/include-binaries to allow their inclusion).",
  433. $unwanted_binaries), $unwanted_binaries)
  434. if $unwanted_binaries;
  435. # Handle modified binary files detected by the auto-patch generation
  436. my $handle_binary = sub {
  437. my ($self, $old, $new) = @_;
  438. my $relfn = File::Spec->abs2rel($new, $dir);
  439. $binaryfiles->new_binary_found($relfn);
  440. unless ($include_binaries or $binaryfiles->binary_is_allowed($relfn)) {
  441. errormsg(_g("cannot represent change to %s: %s"), $relfn,
  442. _g("binary file contents changed"));
  443. errormsg(_g("add %s in debian/source/include-binaries if you want" .
  444. " to store the modified binary in the debian tarball"),
  445. $relfn);
  446. $self->register_error();
  447. }
  448. };
  449. # Create a patch
  450. my $autopatch = File::Spec->catfile($dir, "debian", "patches",
  451. $self->get_autopatch_name());
  452. my $tmpdiff = $self->generate_patch($dir, order_from => $autopatch,
  453. header_from => $autopatch,
  454. handle_binary => $handle_binary,
  455. skip_auto => $self->{'options'}{'auto_commit'},
  456. usage => 'build');
  457. unless (-z $tmpdiff or $self->{'options'}{'auto_commit'}) {
  458. info(_g("you can integrate the local changes with %s"),
  459. "dpkg-source --commit");
  460. error(_g("aborting due to unexpected upstream changes, see %s"),
  461. $tmpdiff);
  462. }
  463. push @Dpkg::Exit::handlers, sub { unlink($tmpdiff) };
  464. $binaryfiles->update_debian_source_include_binaries() if $include_binaries;
  465. # Install the diff as the new autopatch
  466. if ($self->{'options'}{'auto_commit'}) {
  467. mkpath(File::Spec->catdir($dir, "debian", "patches"));
  468. $autopatch = $self->register_patch($dir, $tmpdiff,
  469. $self->get_autopatch_name());
  470. info(_g("local changes have been recorded in a new patch: %s"),
  471. $autopatch) if -e $autopatch;
  472. rmdir(File::Spec->catdir($dir, "debian", "patches")); # No check on purpose
  473. }
  474. unlink($tmpdiff) || syserr(_g("cannot remove %s"), $tmpdiff);
  475. pop @Dpkg::Exit::handlers;
  476. # Create the debian.tar
  477. my $debianfile = "$basenamerev.debian.tar." . $self->{'options'}{'comp_ext'};
  478. info(_g("building %s in %s"), $sourcepackage, $debianfile);
  479. my $tar = Dpkg::Source::Archive->new(filename => $debianfile);
  480. $tar->create(options => \@tar_ignore, 'chdir' => $dir);
  481. $tar->add_directory("debian");
  482. foreach my $binary ($binaryfiles->get_seen_binaries()) {
  483. $tar->add_file($binary) unless $binary =~ m{^debian/};
  484. }
  485. $tar->finish();
  486. $self->add_file($debianfile);
  487. }
  488. sub get_patch_header {
  489. my ($self, $dir) = @_;
  490. my $ph = File::Spec->catfile($dir, "debian", "source", "local-patch-header");
  491. unless (-f $ph) {
  492. $ph = File::Spec->catfile($dir, "debian", "source", "patch-header");
  493. }
  494. my $text;
  495. if (-f $ph) {
  496. open(PH, "<", $ph) || syserr(_g("cannot read %s"), $ph);
  497. $text = join("", <PH>);
  498. close(PH);
  499. return $text;
  500. }
  501. my $ch_info = changelog_parse(offset => 0, count => 1,
  502. file => File::Spec->catfile($dir, "debian", "changelog"));
  503. return '' if not defined $ch_info;
  504. my $header = Dpkg::Control->new(type => CTRL_UNKNOWN);
  505. $header->{'Description'} = "<short summary of the patch>\n";
  506. $header->{'Description'} .=
  507. "TODO: Put a short summary on the line above and replace this paragraph
  508. with a longer explanation of this change. Complete the meta-information
  509. with other relevant fields (see below for details). To make it easier, the
  510. information below has been extracted from the changelog. Adjust it or drop
  511. it.\n";
  512. $header->{'Description'} .= $ch_info->{'Changes'} . "\n";
  513. $header->{'Author'} = $ch_info->{'Maintainer'};
  514. $text = "$header";
  515. run_vendor_hook("extend-patch-header", \$text, $ch_info);
  516. $text .= "\n---
  517. The information above should follow the Patch Tagging Guidelines, please
  518. checkout http://dep.debian.net/deps/dep3/ to learn about the format. Here
  519. are templates for supplementary fields that you might want to add:
  520. Origin: <vendor|upstream|other>, <url of original patch>
  521. Bug: <url in upstream bugtracker>
  522. Bug-Debian: http://bugs.debian.org/<bugnumber>
  523. Bug-Ubuntu: https://launchpad.net/bugs/<bugnumber>
  524. Forwarded: <no|not-needed|url proving that it has been forwarded>
  525. Reviewed-By: <name and email of someone who approved the patch>
  526. Last-Update: <YYYY-MM-DD>\n\n";
  527. return $text;
  528. }
  529. sub register_patch {
  530. my ($self, $dir, $patch_file, $patch_name) = @_;
  531. my $patch = File::Spec->catfile($dir, "debian", "patches", $patch_name);
  532. if (-s $patch_file) {
  533. copy($patch_file, $patch) ||
  534. syserr(_g("failed to copy %s to %s"), $patch_file, $patch);
  535. chmod(0666 & ~ umask(), $patch) ||
  536. syserr(_g("unable to change permission of `%s'"), $patch);
  537. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  538. open(APPLIED, '>>', $applied) || syserr(_g("cannot write %s"), $applied);
  539. print APPLIED "$patch\n";
  540. close(APPLIED) || syserr(_g("cannot close %s"), $applied);
  541. } elsif (-e $patch) {
  542. unlink($patch) || syserr(_g("cannot remove %s"), $patch);
  543. }
  544. return $patch;
  545. }
  546. sub do_commit {
  547. my ($self, $dir) = @_;
  548. my ($patch_name, $tmpdiff) = @{$self->{'options'}{'ARGV'}};
  549. sub 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"), $patch);
  556. return 1;
  557. }
  558. return 0;
  559. }
  560. $self->prepare_build($dir);
  561. # Try to fix up a broken relative filename for the patch
  562. if ($tmpdiff and not -e $tmpdiff) {
  563. $tmpdiff = File::Spec->catfile($dir, $tmpdiff)
  564. unless File::Spec->file_name_is_absolute($tmpdiff);
  565. error(_g("patch file '%s' doesn't exist"), $tmpdiff) if not -e $tmpdiff;
  566. }
  567. my $binaryfiles = Dpkg::Source::Package::V2::BinaryFiles->new($dir);
  568. my $handle_binary = sub {
  569. my ($self, $old, $new) = @_;
  570. my $fn = File::Spec->abs2rel($new, $dir);
  571. $binaryfiles->new_binary_found($fn);
  572. };
  573. unless ($tmpdiff) {
  574. $tmpdiff = $self->generate_patch($dir, handle_binary => $handle_binary,
  575. usage => "commit");
  576. $binaryfiles->update_debian_source_include_binaries();
  577. }
  578. push @Dpkg::Exit::handlers, sub { unlink($tmpdiff) };
  579. unless (-s $tmpdiff) {
  580. unlink($tmpdiff) || syserr(_g("cannot remove %s"), $tmpdiff);
  581. info(_g("there are no local changes to record"));
  582. return;
  583. }
  584. while (bad_patch_name($dir, $patch_name)) {
  585. # Ask the patch name interactively
  586. print STDOUT _g("Enter the desired patch name: ");
  587. chomp($patch_name = <STDIN>);
  588. $patch_name =~ s/\s+/-/g;
  589. $patch_name =~ s/\///g;
  590. }
  591. mkpath(File::Spec->catdir($dir, "debian", "patches"));
  592. my $patch = $self->register_patch($dir, $tmpdiff, $patch_name);
  593. system("sensible-editor", $patch);
  594. unlink($tmpdiff) || syserr(_g("cannot remove %s"), $tmpdiff);
  595. pop @Dpkg::Exit::handlers;
  596. info(_g("local changes have been recorded in a new patch: %s"), $patch);
  597. }
  598. package Dpkg::Source::Package::V2::BinaryFiles;
  599. use Dpkg::ErrorHandling;
  600. use Dpkg::Gettext;
  601. use File::Path;
  602. sub new {
  603. my ($this, $dir) = @_;
  604. my $class = ref($this) || $this;
  605. my $self = {
  606. dir => $dir,
  607. allowed_binaries => {},
  608. seen_binaries => {},
  609. include_binaries_path =>
  610. File::Spec->catfile($dir, "debian", "source", "include-binaries"),
  611. };
  612. bless $self, $class;
  613. $self->load_allowed_binaries();
  614. return $self;
  615. }
  616. sub new_binary_found {
  617. my ($self, $path) = @_;
  618. $self->{'seen_binaries'}{$path} = 1;
  619. }
  620. sub load_allowed_binaries {
  621. my ($self) = @_;
  622. my $incbin_file = $self->{'include_binaries_path'};
  623. if (-f $incbin_file) {
  624. open(INC, "<", $incbin_file) || syserr(_g("cannot read %s"), $incbin_file);
  625. while(defined($_ = <INC>)) {
  626. chomp; s/^\s*//; s/\s*$//;
  627. next if /^#/ or /^$/;
  628. $self->{'allowed_binaries'}{$_} = 1;
  629. }
  630. close(INC);
  631. }
  632. }
  633. sub binary_is_allowed {
  634. my ($self, $path) = @_;
  635. return 1 if exists $self->{'allowed_binaries'}{$path};
  636. return 0;
  637. }
  638. sub update_debian_source_include_binaries {
  639. my ($self) = @_;
  640. my @unknown_binaries = $self->get_unknown_binaries();
  641. return unless scalar(@unknown_binaries);
  642. my $incbin_file = $self->{'include_binaries_path'};
  643. mkpath(File::Spec->catdir($self->{'dir'}, "debian", "source"));
  644. open(INC, ">>", $incbin_file) || syserr(_g("cannot write %s"), $incbin_file);
  645. foreach my $binary (@unknown_binaries) {
  646. print INC "$binary\n";
  647. info(_g("adding %s to %s"), $binary, "debian/source/include-binaries");
  648. $self->{'allowed_binaries'}{$binary} = 1;
  649. }
  650. close(INC);
  651. }
  652. sub get_unknown_binaries {
  653. my ($self) = @_;
  654. return grep { not $self->binary_is_allowed($_) } $self->get_seen_binaries();
  655. }
  656. sub get_seen_binaries {
  657. my ($self) = @_;
  658. return sort keys %{$self->{'seen_binaries'}};
  659. }
  660. 1;