quilt.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. # Kept as close as possible to default patch options in
  107. # Dpkg::Source::Patch (used in without_quilt mode)
  108. QUILT_PATCH_OPTS => "-t -F 0 -N -u -V never -g0" },
  109. 'chdir' => $dir,
  110. 'exec' => [ 'quilt', '--quiltrc', '/dev/null', @$params ],
  111. %more_opts
  112. );
  113. my $pid = fork_and_exec(%opts);
  114. return $pid;
  115. }
  116. sub apply_patches {
  117. my ($self, $dir, $skip_auto) = @_;
  118. # Update debian/patches/series symlink if needed to allow quilt usage
  119. my $series = $self->get_series_file($dir);
  120. return unless $series; # No series, no patches
  121. my $basename = basename($series);
  122. if ($basename ne "series") {
  123. my $dest = File::Spec->catfile($dir, "debian", "patches", "series");
  124. unlink($dest) if -l $dest;
  125. unless (-f _) { # Don't overwrite real files
  126. symlink($basename, $dest) ||
  127. syserr(_g("can't create symlink %s"), $dest);
  128. }
  129. }
  130. my @patches = $self->get_patches($dir, $skip_auto);
  131. return unless scalar(@patches);
  132. # Apply patches
  133. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  134. open(APPLIED, '>', $applied) || syserr(_g("cannot write %s"), $applied);
  135. my $now = time();
  136. my $pobj = {};
  137. my $panalysis = {};
  138. foreach my $patch (@patches) {
  139. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  140. $pobj->{$patch} = Dpkg::Source::Patch->new(filename => $path);
  141. if ($self->{'options'}{'without_quilt'}) {
  142. info(_g("applying %s"), $patch) unless $skip_auto;
  143. $pobj->{$patch}->apply($dir, timestamp => $now,
  144. force_timestamp => 1, create_dirs => 1,
  145. add_options => [ '-E' ]);
  146. print APPLIED "$patch\n";
  147. } else {
  148. $panalysis->{$patch} = $pobj->{$patch}->analyze($dir);
  149. foreach my $dir (keys %{$panalysis->{$patch}->{'dirtocreate'}}) {
  150. eval { mkpath($dir); };
  151. syserr(_g("cannot create directory %s"), $dir) if $@;
  152. }
  153. }
  154. }
  155. if (not $self->{'options'}{'without_quilt'}) {
  156. my %opts;
  157. $opts{"to_file"} = "/dev/null" if $skip_auto;
  158. info(_g("applying all patches with %s"), "quilt push -q " . $patches[-1]) unless $skip_auto;
  159. $self->run_quilt($dir, ['push', '-q', $patches[-1]],
  160. wait_child => 1, %opts);
  161. foreach my $patch (@patches) {
  162. foreach my $fn (keys %{$panalysis->{$patch}->{'filepatched'}}) {
  163. utime($now, $now, $fn) || $! == ENOENT ||
  164. syserr(_g("cannot change timestamp for %s"), $fn);
  165. }
  166. print APPLIED "$patch\n";
  167. }
  168. }
  169. close(APPLIED);
  170. }
  171. sub prepare_build {
  172. my ($self, $dir) = @_;
  173. $self->SUPER::prepare_build($dir);
  174. # Skip .pc directories of quilt by default and ignore difference
  175. # on debian/patches/series symlinks and d/p/.dpkg-source-applied
  176. # stamp file created by ourselves
  177. my $func = sub {
  178. return 1 if $_[0] =~ m{^debian/patches/series$} and -l $_[0];
  179. return 1 if $_[0] =~ m{^debian/patches/.dpkg-source-applied$};
  180. return 1 if $_[0] =~ /^.pc(\/|$)/;
  181. return 1 if $_[0] =~ /$self->{'options'}{'diff_ignore_regexp'}/;
  182. return 0;
  183. };
  184. $self->{'diff_options'}{'diff_ignore_func'} = $func;
  185. }
  186. sub check_patches_applied {
  187. my ($self, $dir) = @_;
  188. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  189. my $auto_patch = $self->get_autopatch_name();
  190. my @patches ;
  191. # First we try to get a list of patches that are probably not napplied
  192. if (not $self->{'options'}{'without_quilt'}) {
  193. my $pipe;
  194. my $pid = $self->run_quilt($dir, ['unapplied'], error_to_file => '/dev/null',
  195. to_pipe => \$pipe);
  196. @patches = map { chomp; $_ } (<$pipe>);
  197. close ($pipe) || syserr("close on 'quilt unapplied' pipe");
  198. wait_child($pid, cmdline => "quilt unapplied", nocheck => 1);
  199. subprocerr("quilt unapplied") unless WIFEXITED($?);
  200. } else {
  201. @patches = $self->get_patches($dir);
  202. }
  203. # Then we check if it's applicable, and if yes, we make the
  204. # assumption that patches are not applied and need to be applied
  205. if (scalar(@patches)) {
  206. my $first_patch = File::Spec->catfile($dir, "debian", "patches", $patches[0]);
  207. my $patch_obj = Dpkg::Source::Patch->new(filename => $first_patch);
  208. if ($patch_obj->check_apply($dir)) {
  209. warning(_g("patches have not been applied, applying them now (use --no-preparation to override)"));
  210. $self->apply_patches($dir);
  211. }
  212. }
  213. }
  214. sub register_autopatch {
  215. my ($self, $dir) = @_;
  216. my $auto_patch = $self->get_autopatch_name();
  217. my @patches = $self->get_patches($dir);
  218. my $has_patch = (grep { $_ eq $auto_patch } @patches) ? 1 : 0;
  219. my $series = $self->get_series_file($dir);
  220. $series ||= File::Spec->catfile($dir, "debian", "patches", "series");
  221. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  222. my $patch = File::Spec->catfile($dir, "debian", "patches", $auto_patch);
  223. my $absdir = $dir;
  224. unless (File::Spec->file_name_is_absolute($absdir)) {
  225. $absdir = File::Spec->rel2abs($dir);
  226. }
  227. if (-e $patch) {
  228. # Add auto_patch to series file
  229. if (not $has_patch) {
  230. # Use quilt to register only if it's wanted/available AND :
  231. # - either we have patches and quilt has been used (.pc dir exists)
  232. # - or we don't have patches, hence quilt couldn't be used
  233. if ((-d "$dir/.pc" or not scalar(@patches)) and
  234. not $self->{'options'}{'without_quilt'})
  235. {
  236. # Registering the new patch with quilt requires some
  237. # trickery: reverse-apply the patch, create a new quilt patch,
  238. # fold the patch into the quilt-managed one
  239. my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
  240. $patch_obj->apply($dir, add_options => ['-R', '-E']);
  241. $self->run_quilt($dir, ['new', "$auto_patch"],
  242. wait_child => 1, to_file => '/dev/null');
  243. $self->run_quilt($dir, ['fold'],
  244. from_file => "$absdir/debian/patches/$auto_patch",
  245. wait_child => 1, to_file => '/dev/null');
  246. } else {
  247. open(SERIES, ">>", $series) || syserr(_g("cannot write %s"), $series);
  248. print SERIES "$auto_patch\n";
  249. close(SERIES);
  250. }
  251. } else {
  252. # If quilt was used, ensure its meta-information are
  253. # synchronized with the updated patch
  254. if (-d "$dir/.pc" and not $self->{'options'}{'without_quilt'}) {
  255. # Some trickery needed: reverse-apply the patch, fold the
  256. # new patch into the quilt-managed one
  257. my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
  258. $patch_obj->apply($dir, add_options => ['-R', '-E']);
  259. $self->run_quilt($dir, ['fold'],
  260. from_file => "$absdir/debian/patches/$auto_patch",
  261. wait_child => 1, to_file => '/dev/null');
  262. }
  263. }
  264. } else {
  265. # Remove auto_patch from series
  266. if ($has_patch) {
  267. if ($self->{'options'}{'without_quilt'}) {
  268. open(SERIES, "<", $series) || syserr(_g("cannot read %s"), $series);
  269. my @lines = <SERIES>;
  270. close(SERIES);
  271. open(SERIES, ">", $series) || syserr(_g("cannot write %s"), $series);
  272. print(SERIES $_) foreach grep { not /^\Q$auto_patch\E\s*$/ } @lines;
  273. close(SERIES);
  274. } else {
  275. $self->run_quilt($dir, ['delete', $auto_patch],
  276. wait_child => 1, to_file => '/dev/null');
  277. }
  278. }
  279. # Clean up empty series
  280. unlink($series) if not -s $series;
  281. }
  282. }
  283. sub get_patch_header {
  284. my ($self, $dir, $previous) = @_;
  285. my $ch_info = changelog_parse(offset => 0, count => 1,
  286. file => File::Spec->catfile($dir, "debian", "changelog"));
  287. return '' if not defined $ch_info;
  288. my $header = Dpkg::Control->new(type => CTRL_UNKNOWN);
  289. $header->{'Description'} = "Upstream changes introduced in version " .
  290. $ch_info->{'Version'} . "\n";
  291. $header->{'Description'} .=
  292. "This patch has been created by dpkg-source during the package build.
  293. Here's the last changelog entry, hopefully it gives details on why
  294. those changes were made:\n";
  295. $header->{'Description'} .= $ch_info->{'Changes'} . "\n";
  296. $header->{'Description'} .=
  297. "\nThe person named in the Author field signed this changelog entry.\n";
  298. $header->{'Author'} = $ch_info->{'Maintainer'};
  299. my $text = "$header";
  300. run_vendor_hook("extend-patch-header", \$text, $ch_info);
  301. $text .= "\n---
  302. The information above should follow the Patch Tagging Guidelines, please
  303. checkout http://dep.debian.net/deps/dep3/ to learn about the format. Here
  304. are templates for supplementary fields that you might want to add:
  305. Origin: <vendor|upstream|other>, <url of original patch>
  306. Bug: <url in upstream bugtracker>
  307. Bug-Debian: http://bugs.debian.org/<bugnumber>
  308. Forwarded: <no|not-needed|url proving that it has been forwarded>
  309. Reviewed-By: <name and email of someone who approved the patch>
  310. Last-Update: <YYYY-MM-DD>\n\n";
  311. return $text;
  312. }
  313. # vim:et:sw=4:ts=8
  314. 1;