V2.pm 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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. if ($opts{'header_from'} and -e $opts{'header_from'}) {
  344. my $header_from = Dpkg::Source::Patch->new(
  345. filename => $opts{'header_from'});
  346. my $analysis = $header_from->analyze($dir, verbose => 0);
  347. $diff->set_header($analysis->{'patchheader'});
  348. } else {
  349. $diff->set_header($self->get_patch_header($dir));
  350. }
  351. $diff->add_diff_directory($tmp, $dir, basedirname => $basedirname,
  352. %{$self->{'diff_options'}},
  353. handle_binary_func => $opts{'handle_binary'},
  354. order_from => $opts{'order_from'});
  355. error(_g("unrepresentable changes to source")) if not $diff->finish();
  356. if (-s $tmpdiff) {
  357. info(_g("local changes detected, the modified files are:"));
  358. my $analysis = $diff->analyze($dir, verbose => 0);
  359. foreach my $fn (sort keys %{$analysis->{'filepatched'}}) {
  360. print " $fn\n";
  361. }
  362. }
  363. # Remove the temporary directory
  364. erasedir($tmp);
  365. pop @Dpkg::Exit::handlers;
  366. pop @Dpkg::Exit::handlers;
  367. return $tmpdiff;
  368. }
  369. sub do_build {
  370. my ($self, $dir) = @_;
  371. my @argv = @{$self->{'options'}{'ARGV'}};
  372. if (scalar(@argv)) {
  373. usageerr(_g("-b takes only one parameter with format `%s'"),
  374. $self->{'fields'}{'Format'});
  375. }
  376. $self->prepare_build($dir);
  377. my $include_binaries = $self->{'options'}{'include_binaries'};
  378. my @tar_ignore = map { "--exclude=$_" } @{$self->{'options'}{'tar_ignore'}};
  379. my $sourcepackage = $self->{'fields'}{'Source'};
  380. my $basenamerev = $self->get_basename(1);
  381. # Prepare handling of binary files
  382. my %auth_bin_files;
  383. my $incbin_file = File::Spec->catfile($dir, "debian", "source", "include-binaries");
  384. if (-f $incbin_file) {
  385. open(INC, "<", $incbin_file) || syserr(_g("cannot read %s"), $incbin_file);
  386. while(defined($_ = <INC>)) {
  387. chomp; s/^\s*//; s/\s*$//;
  388. next if /^#/ or /^$/;
  389. $auth_bin_files{$_} = 1;
  390. }
  391. close(INC);
  392. }
  393. my @binary_files;
  394. my $handle_binary = sub {
  395. my ($self, $old, $new) = @_;
  396. my $relfn = File::Spec->abs2rel($new, $dir);
  397. # Include binaries if they are whitelisted or if
  398. # --include-binaries has been given
  399. if ($include_binaries or $auth_bin_files{$relfn}) {
  400. push @binary_files, $relfn;
  401. } else {
  402. errormsg(_g("cannot represent change to %s: %s"), $new,
  403. _g("binary file contents changed"));
  404. errormsg(_g("add %s in debian/source/include-binaries if you want" .
  405. " to store the modified binary in the debian tarball"),
  406. $relfn);
  407. $self->register_error();
  408. }
  409. };
  410. # Check if the debian directory contains unwanted binary files
  411. my $unwanted_binaries = 0;
  412. my $check_binary = sub {
  413. my $fn = File::Spec->abs2rel($_, $dir);
  414. if (-f $_ and is_binary($_)) {
  415. if ($include_binaries or $auth_bin_files{$fn}) {
  416. push @binary_files, $fn;
  417. } else {
  418. errormsg(_g("unwanted binary file: %s"), $fn);
  419. $unwanted_binaries++;
  420. }
  421. }
  422. };
  423. my $tar_ignore_glob = "{" . join(",",
  424. map {
  425. my $copy = $_;
  426. $copy =~ s/,/\\,/g;
  427. $copy;
  428. } @{$self->{'options'}{'tar_ignore'}}) . "}";
  429. my $filter_ignore = sub {
  430. # Filter out files that are not going to be included in the debian
  431. # tarball due to ignores.
  432. my %exclude;
  433. my $reldir = File::Spec->abs2rel($File::Find::dir, $dir);
  434. my $cwd = getcwd();
  435. # Apply the pattern both from the top dir and from the inspected dir
  436. chdir($dir) || syserr(_g("unable to chdir to `%s'"), $dir);
  437. $exclude{$_} = 1 foreach glob($tar_ignore_glob);
  438. chdir($cwd) || syserr(_g("unable to chdir to `%s'"), $cwd);
  439. chdir($File::Find::dir) ||
  440. syserr(_g("unable to chdir to `%s'"), $File::Find::dir);
  441. $exclude{$_} = 1 foreach glob($tar_ignore_glob);
  442. chdir($cwd) || syserr(_g("unable to chdir to `%s'"), $cwd);
  443. my @result;
  444. foreach my $fn (@_) {
  445. unless (exists $exclude{$fn} or exists $exclude{"$reldir/$fn"}) {
  446. push @result, $fn;
  447. }
  448. }
  449. return @result;
  450. };
  451. find({ wanted => $check_binary, preprocess => $filter_ignore,
  452. no_chdir => 1 }, File::Spec->catdir($dir, "debian"));
  453. error(P_("detected %d unwanted binary file (add it in " .
  454. "debian/source/include-binaries to allow its inclusion).",
  455. "detected %d unwanted binary files (add them in " .
  456. "debian/source/include-binaries to allow their inclusion).",
  457. $unwanted_binaries), $unwanted_binaries)
  458. if $unwanted_binaries;
  459. # Create a patch
  460. my $autopatch = File::Spec->catfile($dir, "debian", "patches",
  461. $self->get_autopatch_name());
  462. my $tmpdiff = $self->generate_patch($dir, order_from => $autopatch,
  463. header_from => $autopatch,
  464. handle_binary => $handle_binary,
  465. skip_auto => $self->{'options'}{'auto_commit'},
  466. usage => 'build');
  467. unless (-z $tmpdiff or $self->{'options'}{'auto_commit'}) {
  468. info(_g("you can integrate the local changes with %s"),
  469. "dpkg-source --commit");
  470. error(_g("aborting due to unexpected upstream changes, see %s"),
  471. $tmpdiff);
  472. }
  473. push @Dpkg::Exit::handlers, sub { unlink($tmpdiff) };
  474. # Install the diff as the new autopatch
  475. if ($self->{'options'}{'auto_commit'}) {
  476. mkpath(File::Spec->catdir($dir, "debian", "patches"));
  477. $autopatch = $self->register_patch($dir, $tmpdiff,
  478. $self->get_autopatch_name());
  479. info(_g("local changes have been recorded in a new patch: %s"),
  480. $autopatch) if -e $autopatch;
  481. rmdir(File::Spec->catdir($dir, "debian", "patches")); # No check on purpose
  482. }
  483. unlink($tmpdiff) || syserr(_g("cannot remove %s"), $tmpdiff);
  484. pop @Dpkg::Exit::handlers;
  485. # Update debian/source/include-binaries if needed
  486. if (scalar(@binary_files) and $include_binaries) {
  487. mkpath(File::Spec->catdir($dir, "debian", "source"));
  488. open(INC, ">>", $incbin_file) || syserr(_g("cannot write %s"), $incbin_file);
  489. foreach my $binary (@binary_files) {
  490. unless ($auth_bin_files{$binary}) {
  491. print INC "$binary\n";
  492. info(_g("adding %s to %s"), $binary, "debian/source/include-binaries");
  493. }
  494. }
  495. close(INC);
  496. }
  497. # Create the debian.tar
  498. my $debianfile = "$basenamerev.debian.tar." . $self->{'options'}{'comp_ext'};
  499. info(_g("building %s in %s"), $sourcepackage, $debianfile);
  500. my $tar = Dpkg::Source::Archive->new(filename => $debianfile);
  501. $tar->create(options => \@tar_ignore, 'chdir' => $dir);
  502. $tar->add_directory("debian");
  503. foreach my $binary (@binary_files) {
  504. $tar->add_file($binary) unless $binary =~ m{^debian/};
  505. }
  506. $tar->finish();
  507. $self->add_file($debianfile);
  508. }
  509. sub get_patch_header {
  510. my ($self, $dir) = @_;
  511. my $ph = File::Spec->catfile($dir, "debian", "source", "local-patch-header");
  512. unless (-f $ph) {
  513. $ph = File::Spec->catfile($dir, "debian", "source", "patch-header");
  514. }
  515. my $text;
  516. if (-f $ph) {
  517. open(PH, "<", $ph) || syserr(_g("cannot read %s"), $ph);
  518. $text = join("", <PH>);
  519. close(PH);
  520. return $text;
  521. }
  522. my $ch_info = changelog_parse(offset => 0, count => 1,
  523. file => File::Spec->catfile($dir, "debian", "changelog"));
  524. return '' if not defined $ch_info;
  525. my $header = Dpkg::Control->new(type => CTRL_UNKNOWN);
  526. $header->{'Description'} = "<short summary of the patch>\n";
  527. $header->{'Description'} .=
  528. "TODO: Put a short summary on the line above and replace this paragraph
  529. with a longer explanation of this change. Complete the meta-information
  530. with other relevant fields (see below for details). To make it easier, the
  531. information below has been extracted from the changelog. Adjust it or drop
  532. it.\n";
  533. $header->{'Description'} .= $ch_info->{'Changes'} . "\n";
  534. $header->{'Author'} = $ch_info->{'Maintainer'};
  535. $text = "$header";
  536. run_vendor_hook("extend-patch-header", \$text, $ch_info);
  537. $text .= "\n---
  538. The information above should follow the Patch Tagging Guidelines, please
  539. checkout http://dep.debian.net/deps/dep3/ to learn about the format. Here
  540. are templates for supplementary fields that you might want to add:
  541. Origin: <vendor|upstream|other>, <url of original patch>
  542. Bug: <url in upstream bugtracker>
  543. Bug-Debian: http://bugs.debian.org/<bugnumber>
  544. Bug-Ubuntu: https://launchpad.net/bugs/<bugnumber>
  545. Forwarded: <no|not-needed|url proving that it has been forwarded>
  546. Reviewed-By: <name and email of someone who approved the patch>
  547. Last-Update: <YYYY-MM-DD>\n\n";
  548. return $text;
  549. }
  550. sub register_patch {
  551. my ($self, $dir, $patch_file, $patch_name) = @_;
  552. my $patch = File::Spec->catfile($dir, "debian", "patches", $patch_name);
  553. if (-s $patch_file) {
  554. copy($patch_file, $patch) ||
  555. syserr(_g("failed to copy %s to %s"), $patch_file, $patch);
  556. chmod(0666 & ~ umask(), $patch) ||
  557. syserr(_g("unable to change permission of `%s'"), $patch);
  558. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  559. open(APPLIED, '>>', $applied) || syserr(_g("cannot write %s"), $applied);
  560. print APPLIED "$patch\n";
  561. close(APPLIED) || syserr(_g("cannot close %s"), $applied);
  562. } elsif (-e $patch) {
  563. unlink($patch) || syserr(_g("cannot remove %s"), $patch);
  564. }
  565. return $patch;
  566. }
  567. sub do_commit {
  568. my ($self, $dir) = @_;
  569. my ($patch_name, $tmpdiff) = @{$self->{'options'}{'ARGV'}};
  570. sub bad_patch_name {
  571. my ($dir, $patch_name) = @_;
  572. return 1 if not defined($patch_name);
  573. return 1 if not length($patch_name);
  574. my $patch = File::Spec->catfile($dir, "debian", "patches", $patch_name);
  575. if (-e $patch) {
  576. warning(_g("cannot register changes in %s, this patch already exists"), $patch);
  577. return 1;
  578. }
  579. return 0;
  580. }
  581. $self->prepare_build($dir);
  582. # Try to fix up a broken relative filename for the patch
  583. if ($tmpdiff and not -e $tmpdiff) {
  584. $tmpdiff = File::Spec->catfile($dir, $tmpdiff)
  585. unless File::Spec->file_name_is_absolute($tmpdiff);
  586. error(_g("patch file '%s' doesn't exist"), $tmpdiff) if not -e $tmpdiff;
  587. }
  588. unless ($tmpdiff) {
  589. $tmpdiff = $self->generate_patch($dir, usage => "commit");
  590. }
  591. push @Dpkg::Exit::handlers, sub { unlink($tmpdiff) };
  592. unless (-s $tmpdiff) {
  593. unlink($tmpdiff) || syserr(_g("cannot remove %s"), $tmpdiff);
  594. info(_g("there are no local changes to record"));
  595. return;
  596. }
  597. while (bad_patch_name($dir, $patch_name)) {
  598. # Ask the patch name interactively
  599. print STDOUT _g("Enter the desired patch name: ");
  600. chomp($patch_name = <STDIN>);
  601. $patch_name =~ s/\s+/-/g;
  602. $patch_name =~ s/\///g;
  603. }
  604. mkpath(File::Spec->catdir($dir, "debian", "patches"));
  605. my $patch = $self->register_patch($dir, $tmpdiff, $patch_name);
  606. system("sensible-editor", $patch);
  607. unlink($tmpdiff) || syserr(_g("cannot remove %s"), $tmpdiff);
  608. pop @Dpkg::Exit::handlers;
  609. info(_g("local changes have been recorded in a new patch: %s"), $patch);
  610. }
  611. # vim:et:sw=4:ts=8
  612. 1;