quilt.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. # Copyright © 2008 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::V3::quilt;
  16. use strict;
  17. use warnings;
  18. # Based on wig&pen implementation
  19. use base 'Dpkg::Source::Package::V2';
  20. use Dpkg;
  21. use Dpkg::Gettext;
  22. use Dpkg::ErrorHandling;
  23. use Dpkg::Source::Patch;
  24. use Dpkg::IPC;
  25. use Dpkg::Vendor qw(get_current_vendor run_vendor_hook);
  26. use Dpkg::Control;
  27. use Dpkg::Changelog::Parse;
  28. use POSIX;
  29. use File::Basename;
  30. use File::Spec;
  31. use File::Path;
  32. our $CURRENT_MINOR_VERSION = "0";
  33. sub init_options {
  34. my ($self) = @_;
  35. $self->SUPER::init_options();
  36. # By default use quilt, unless it's not available
  37. $self->{'options'}{'without_quilt'} = (-x "/usr/bin/quilt") ? 0 : 1
  38. unless exists $self->{'options'}{'without_quilt'};
  39. }
  40. sub parse_cmdline_option {
  41. my ($self, $opt) = @_;
  42. return 1 if $self->SUPER::parse_cmdline_option($opt);
  43. if ($opt =~ /^--without-quilt$/) {
  44. $self->{'options'}{'without_quilt'} = 1;
  45. return 1;
  46. }
  47. return 0;
  48. }
  49. sub get_autopatch_name {
  50. my ($self) = @_;
  51. return "debian-changes-" . $self->{'fields'}{'Version'};
  52. }
  53. sub get_series_file {
  54. my ($self, $dir) = @_;
  55. my $pd = File::Spec->catdir($dir, "debian", "patches");
  56. my $vendor = lc(get_current_vendor() || "debian");
  57. foreach (File::Spec->catfile($pd, "$vendor.series"),
  58. File::Spec->catfile($pd, "series")) {
  59. return $_ if -e $_;
  60. }
  61. return undef;
  62. }
  63. sub get_patches {
  64. my ($self, $dir, $skip_auto) = @_;
  65. my @patches;
  66. my $auto_patch = $self->get_autopatch_name();
  67. my $series = $self->get_series_file($dir);
  68. if (defined($series)) {
  69. open(SERIES, "<" , $series) || syserr(_g("cannot read %s"), $series);
  70. while(defined($_ = <SERIES>)) {
  71. chomp; s/^\s+//; s/\s+$//; # Strip leading/trailing spaces
  72. s/(^|\s+)#.*$//; # Strip comment
  73. next unless $_;
  74. if (/^(\S+)\s+(.*)$/) {
  75. $_ = $1;
  76. if ($2 ne '-p1') {
  77. warning(_g("the series file (%s) contains unsupported " .
  78. "options ('%s', line %s), dpkg-source might " .
  79. "fail when applying patches."),
  80. $series, $2, $.) unless $skip_auto;
  81. }
  82. }
  83. next if $skip_auto and $_ eq $auto_patch;
  84. push @patches, $_;
  85. }
  86. close(SERIES);
  87. }
  88. return @patches;
  89. }
  90. sub run_quilt {
  91. my ($self, $dir, $params, %more_opts) = @_;
  92. $params = [ $params ] unless ref($params) eq "ARRAY";
  93. my $absdir = $dir;
  94. unless (File::Spec->file_name_is_absolute($absdir)) {
  95. $absdir = File::Spec->rel2abs($dir);
  96. }
  97. my $series = $self->get_series_file($dir);
  98. # Use default name if no series files exist yet
  99. $series = "$absdir/debian/patches/series" unless defined $series;
  100. unless (File::Spec->file_name_is_absolute($series)) {
  101. $series = File::Spec->rel2abs($series);
  102. }
  103. my %opts = (
  104. env => { QUILT_PATCHES => "$absdir/debian/patches",
  105. QUILT_SERIES => $series },
  106. 'chdir' => $dir,
  107. 'exec' => [ 'quilt', '--quiltrc', '/dev/null', @$params ],
  108. %more_opts
  109. );
  110. my $pid = fork_and_exec(%opts);
  111. return $pid;
  112. }
  113. sub apply_patches {
  114. my ($self, $dir, $skip_auto) = @_;
  115. # Update debian/patches/series symlink if needed to allow quilt usage
  116. my $series = $self->get_series_file($dir);
  117. return unless $series; # No series, no patches
  118. my $basename = basename($series);
  119. if ($basename ne "series") {
  120. my $dest = File::Spec->catfile($dir, "debian", "patches", "series");
  121. unlink($dest) if -l $dest;
  122. unless (-f _) { # Don't overwrite real files
  123. symlink($basename, $dest) ||
  124. syserr(_g("can't create symlink %s"), $dest);
  125. }
  126. }
  127. my @patches = $self->get_patches($dir, $skip_auto);
  128. return unless scalar(@patches);
  129. # Apply patches
  130. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  131. open(APPLIED, '>', $applied) || syserr(_g("cannot write %s"), $applied);
  132. my $now = time();
  133. my $pobj = {};
  134. my $panalysis = {};
  135. foreach my $patch (@patches) {
  136. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  137. $pobj->{$patch} = Dpkg::Source::Patch->new(filename => $path);
  138. if ($self->{'options'}{'without_quilt'}) {
  139. info(_g("applying %s"), $patch) unless $skip_auto;
  140. $pobj->{$patch}->apply($dir, timestamp => $now,
  141. force_timestamp => 1, create_dirs => 1,
  142. add_options => [ '-E' ]);
  143. print APPLIED "$patch\n";
  144. } else {
  145. $panalysis->{$patch} = $pobj->{$patch}->analyze($dir);
  146. foreach my $dir (keys %{$panalysis->{$patch}->{'dirtocreate'}}) {
  147. eval { mkpath($dir); };
  148. syserr(_g("cannot create directory %s"), $dir) if $@;
  149. }
  150. }
  151. }
  152. if (not $self->{'options'}{'without_quilt'}) {
  153. my %opts;
  154. $opts{"to_file"} = "/dev/null" if $skip_auto;
  155. info(_g("applying all patches with %s"), "quilt push -q " . $patches[-1]) unless $skip_auto;
  156. $self->run_quilt($dir, ['push', '-q', $patches[-1]],
  157. delete_env => ['QUILT_PATCH_OPTS'],
  158. wait_child => 1, %opts);
  159. foreach my $patch (@patches) {
  160. foreach my $fn (keys %{$panalysis->{$patch}->{'filepatched'}}) {
  161. utime($now, $now, $fn) || $! == ENOENT ||
  162. syserr(_g("cannot change timestamp for %s"), $fn);
  163. }
  164. print APPLIED "$patch\n";
  165. }
  166. }
  167. close(APPLIED);
  168. }
  169. sub prepare_build {
  170. my ($self, $dir) = @_;
  171. $self->SUPER::prepare_build($dir);
  172. # Skip .pc directories of quilt by default and ignore difference
  173. # on debian/patches/series symlinks and d/p/.dpkg-source-applied
  174. # stamp file created by ourselves
  175. my $func = sub {
  176. return 1 if $_[0] =~ m{^debian/patches/series$} and -l $_[0];
  177. return 1 if $_[0] =~ m{^debian/patches/.dpkg-source-applied$};
  178. return 1 if $_[0] =~ /^.pc(\/|$)/;
  179. return 1 if $_[0] =~ /$self->{'options'}{'diff_ignore_regexp'}/;
  180. return 0;
  181. };
  182. $self->{'diff_options'}{'diff_ignore_func'} = $func;
  183. }
  184. sub check_patches_applied {
  185. my ($self, $dir) = @_;
  186. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  187. my $auto_patch = $self->get_autopatch_name();
  188. my @patches ;
  189. # First we try to get a list of patches that are probably not napplied
  190. if (not $self->{'options'}{'without_quilt'}) {
  191. my $pipe;
  192. my $pid = $self->run_quilt($dir, ['unapplied'], error_to_file => '/dev/null',
  193. to_pipe => \$pipe);
  194. @patches = map { chomp; $_ } (<$pipe>);
  195. close ($pipe) || syserr("close on 'quilt unapplied' pipe");
  196. wait_child($pid, cmdline => "quilt unapplied", nocheck => 1);
  197. subprocerr("quilt unapplied") unless WIFEXITED($?);
  198. } else {
  199. @patches = $self->get_patches($dir);
  200. }
  201. # Then we check if it's applicable, and if yes, we make the
  202. # assumption that patches are not applied and need to be applied
  203. if (scalar(@patches)) {
  204. my $first_patch = File::Spec->catfile($dir, "debian", "patches", $patches[0]);
  205. my $patch_obj = Dpkg::Source::Patch->new(filename => $first_patch);
  206. if ($patch_obj->check_apply($dir)) {
  207. warning(_g("patches have not been applied, applying them now (use --no-preparation to override)"));
  208. $self->apply_patches($dir);
  209. }
  210. }
  211. }
  212. sub register_autopatch {
  213. my ($self, $dir) = @_;
  214. my $auto_patch = $self->get_autopatch_name();
  215. my @patches = $self->get_patches($dir);
  216. my $has_patch = (grep { $_ eq $auto_patch } @patches) ? 1 : 0;
  217. my $series = $self->get_series_file($dir);
  218. $series ||= File::Spec->catfile($dir, "debian", "patches", "series");
  219. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  220. my $patch = File::Spec->catfile($dir, "debian", "patches", $auto_patch);
  221. my $absdir = $dir;
  222. unless (File::Spec->file_name_is_absolute($absdir)) {
  223. $absdir = File::Spec->rel2abs($dir);
  224. }
  225. if (-e $patch) {
  226. # Add auto_patch to series file
  227. if (not $has_patch) {
  228. # Use quilt to register only if it's wanted/available AND :
  229. # - either we have patches and quilt has been used (.pc dir exists)
  230. # - or we don't have patches, hence quilt couldn't be used
  231. if ((-d "$dir/.pc" or not scalar(@patches)) and
  232. not $self->{'options'}{'without_quilt'})
  233. {
  234. # Registering the new patch with quilt requires some
  235. # trickery: reverse-apply the patch, create a new quilt patch,
  236. # fold the patch into the quilt-managed one
  237. my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
  238. $patch_obj->apply($dir, add_options => ['-R', '-E']);
  239. $self->run_quilt($dir, ['new', "$auto_patch"],
  240. wait_child => 1, to_file => '/dev/null');
  241. $self->run_quilt($dir, ['fold'],
  242. from_file => "$absdir/debian/patches/$auto_patch",
  243. wait_child => 1, to_file => '/dev/null');
  244. } else {
  245. open(SERIES, ">>", $series) || syserr(_g("cannot write %s"), $series);
  246. print SERIES "$auto_patch\n";
  247. close(SERIES);
  248. }
  249. } else {
  250. # If quilt was used, ensure its meta-information are
  251. # synchronized with the updated patch
  252. if (-d "$dir/.pc" and not $self->{'options'}{'without_quilt'}) {
  253. # Some trickery needed: reverse-apply the patch, fold the
  254. # new patch into the quilt-managed one
  255. my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
  256. $patch_obj->apply($dir, add_options => ['-R', '-E']);
  257. $self->run_quilt($dir, ['fold'],
  258. from_file => "$absdir/debian/patches/$auto_patch",
  259. wait_child => 1, to_file => '/dev/null');
  260. }
  261. }
  262. } else {
  263. # Remove auto_patch from series
  264. if ($has_patch) {
  265. if ($self->{'options'}{'without_quilt'}) {
  266. open(SERIES, "<", $series) || syserr(_g("cannot read %s"), $series);
  267. my @lines = <SERIES>;
  268. close(SERIES);
  269. open(SERIES, ">", $series) || syserr(_g("cannot write %s"), $series);
  270. print(SERIES $_) foreach grep { not /^\Q$auto_patch\E\s*$/ } @lines;
  271. close(SERIES);
  272. } else {
  273. $self->run_quilt($dir, ['delete', $auto_patch],
  274. wait_child => 1, to_file => '/dev/null');
  275. }
  276. }
  277. # Clean up empty series
  278. unlink($series) if not -s $series;
  279. }
  280. }
  281. sub get_patch_header {
  282. my ($self, $dir, $previous) = @_;
  283. my $ch_info = changelog_parse(offset => 0, count => 1,
  284. file => File::Spec->catfile($dir, "debian", "changelog"));
  285. return '' if not defined $ch_info;
  286. my $header = Dpkg::Control->new(type => CTRL_UNKNOWN);
  287. $header->{'Description'} = "Upstream changes introduced in version " .
  288. $ch_info->{'Version'} . "\n";
  289. $header->{'Description'} .=
  290. "This patch has been created by dpkg-source during the package build.
  291. Here's the last changelog entry, hopefully it gives details on why
  292. those changes were made:\n";
  293. $header->{'Description'} .= $ch_info->{'Changes'} . "\n";
  294. $header->{'Description'} .=
  295. "\nThe person named in the Author field signed this changelog entry.\n";
  296. $header->{'Author'} = $ch_info->{'Maintainer'};
  297. my $text = "$header";
  298. run_vendor_hook("extend-patch-header", \$text, $ch_info);
  299. $text .= "\n---
  300. The information above should follow the Patch Tagging Guidelines, please
  301. checkout http://dep.debian.net/deps/dep3/ to learn about the format. Here
  302. are templates for supplementary fields that you might want to add:
  303. Origin: <vendor|upstream|other>, <url of original patch>
  304. Bug: <url in upstream bugtracker>
  305. Bug-Debian: http://bugs.debian.org/<bugnumber>
  306. Forwarded: <no|not-needed|url proving that it has been forwarded>
  307. Reviewed-By: <name and email of someone who approved the patch>
  308. Last-Update: <YYYY-MM-DD>\n\n";
  309. return $text;
  310. }
  311. # vim:et:sw=4:ts=8
  312. 1;