V2.pm 25 KB

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