Quilt.pm 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. # Copyright © 2008-2012 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 <https://www.gnu.org/licenses/>.
  15. package Dpkg::Source::Package::V3::Quilt;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '0.01';
  19. use File::Spec;
  20. use File::Copy;
  21. use Dpkg::Gettext;
  22. use Dpkg::ErrorHandling;
  23. use Dpkg::Util qw(:list);
  24. use Dpkg::Version;
  25. use Dpkg::Source::Patch;
  26. use Dpkg::Source::Functions qw(erasedir fs_time);
  27. use Dpkg::Source::Quilt;
  28. use Dpkg::Exit;
  29. # Based on wig&pen implementation
  30. use parent qw(Dpkg::Source::Package::V2);
  31. our $CURRENT_MINOR_VERSION = '0';
  32. sub init_options {
  33. my $self = shift;
  34. $self->{options}{single_debian_patch} //= 0;
  35. $self->{options}{allow_version_of_quilt_db} //= [];
  36. $self->SUPER::init_options();
  37. }
  38. my @module_cmdline = (
  39. {
  40. name => '--single-debian-patch',
  41. help => N_('use a single debianization patch'),
  42. when => 'build',
  43. }, {
  44. name => '--allow-version-of-quilt-db=<version>',
  45. help => N_('accept quilt metadata <version> even if unknown'),
  46. when => 'build',
  47. }
  48. );
  49. sub describe_cmdline_options {
  50. my $self = shift;
  51. my @cmdline = ( $self->SUPER::describe_cmdline_options(), @module_cmdline );
  52. return @cmdline;
  53. }
  54. sub parse_cmdline_option {
  55. my ($self, $opt) = @_;
  56. return 1 if $self->SUPER::parse_cmdline_option($opt);
  57. if ($opt eq '--single-debian-patch') {
  58. $self->{options}{single_debian_patch} = 1;
  59. # For backwards compatibility.
  60. $self->{options}{auto_commit} = 1;
  61. return 1;
  62. } elsif ($opt =~ /^--allow-version-of-quilt-db=(.*)$/) {
  63. push @{$self->{options}{allow_version_of_quilt_db}}, $1;
  64. return 1;
  65. }
  66. return 0;
  67. }
  68. sub _build_quilt_object {
  69. my ($self, $dir) = @_;
  70. return $self->{quilt}{$dir} if exists $self->{quilt}{$dir};
  71. $self->{quilt}{$dir} = Dpkg::Source::Quilt->new($dir);
  72. return $self->{quilt}{$dir};
  73. }
  74. sub can_build {
  75. my ($self, $dir) = @_;
  76. my ($code, $msg) = $self->SUPER::can_build($dir);
  77. return ($code, $msg) if $code == 0;
  78. my $v = Dpkg::Version->new($self->{fields}->{'Version'});
  79. return (0, g_('non-native package version does not contain a revision'))
  80. if $v->is_native();
  81. my $quilt = $self->_build_quilt_object($dir);
  82. $msg = $quilt->find_problems();
  83. return (0, $msg) if $msg;
  84. return 1;
  85. }
  86. sub get_autopatch_name {
  87. my $self = shift;
  88. if ($self->{options}{single_debian_patch}) {
  89. return 'debian-changes';
  90. } else {
  91. return 'debian-changes-' . $self->{fields}{'Version'};
  92. }
  93. }
  94. sub apply_patches {
  95. my ($self, $dir, %opts) = @_;
  96. if ($opts{usage} eq 'unpack') {
  97. $opts{verbose} = 1;
  98. } elsif ($opts{usage} eq 'build') {
  99. $opts{warn_options} = 1;
  100. $opts{verbose} = 0;
  101. }
  102. my $quilt = $self->_build_quilt_object($dir);
  103. $quilt->load_series(%opts) if $opts{warn_options}; # Trigger warnings
  104. # Always create the quilt db so that if the maintainer calls quilt to
  105. # create a patch, it's stored in the right directory
  106. $quilt->save_db();
  107. # Update debian/patches/series symlink if needed to allow quilt usage
  108. my $series = $quilt->get_series_file();
  109. my $basename = (File::Spec->splitpath($series))[2];
  110. if ($basename ne 'series') {
  111. my $dest = $quilt->get_patch_file('series');
  112. unlink($dest) if -l $dest;
  113. unless (-f _) { # Don't overwrite real files
  114. symlink($basename, $dest)
  115. or syserr(g_("can't create symlink %s"), $dest);
  116. }
  117. }
  118. return unless scalar($quilt->series());
  119. if ($opts{usage} eq 'preparation' and
  120. $self->{options}{unapply_patches} eq 'auto') {
  121. # We're applying the patches in --before-build, remember to unapply
  122. # them afterwards in --after-build
  123. my $pc_unapply = $quilt->get_db_file('.dpkg-source-unapply');
  124. open(my $unapply_fh, '>', $pc_unapply)
  125. or syserr(g_('cannot write %s'), $pc_unapply);
  126. close($unapply_fh);
  127. }
  128. # Apply patches
  129. my $pc_applied = $quilt->get_db_file('applied-patches');
  130. $opts{timestamp} = fs_time($pc_applied);
  131. if ($opts{skip_auto}) {
  132. my $auto_patch = $self->get_autopatch_name();
  133. $quilt->push(%opts) while ($quilt->next() and $quilt->next() ne $auto_patch);
  134. } else {
  135. $quilt->push(%opts) while $quilt->next();
  136. }
  137. }
  138. sub unapply_patches {
  139. my ($self, $dir, %opts) = @_;
  140. my $quilt = $self->_build_quilt_object($dir);
  141. $opts{verbose} //= 1;
  142. my $pc_applied = $quilt->get_db_file('applied-patches');
  143. my @applied = $quilt->applied();
  144. $opts{timestamp} = fs_time($pc_applied) if @applied;
  145. $quilt->pop(%opts) while $quilt->top();
  146. erasedir($quilt->get_db_dir());
  147. }
  148. sub prepare_build {
  149. my ($self, $dir) = @_;
  150. $self->SUPER::prepare_build($dir);
  151. # Skip .pc directories of quilt by default and ignore difference
  152. # on debian/patches/series symlinks and d/p/.dpkg-source-applied
  153. # stamp file created by ourselves
  154. my $func = sub {
  155. my $pathname = shift;
  156. return 1 if $pathname eq 'debian/patches/series' and -l $pathname;
  157. return 1 if $pathname =~ /^\.pc(\/|$)/;
  158. return 1 if $pathname =~ /$self->{options}{diff_ignore_regex}/;
  159. return 0;
  160. };
  161. $self->{diff_options}{diff_ignore_func} = $func;
  162. }
  163. sub do_build {
  164. my ($self, $dir) = @_;
  165. my $quilt = $self->_build_quilt_object($dir);
  166. my $version = $quilt->get_db_version();
  167. if (defined($version) and $version != 2) {
  168. if (any { $version eq $_ }
  169. @{$self->{options}{allow_version_of_quilt_db}})
  170. {
  171. warning(g_('unsupported version of the quilt metadata: %s'), $version);
  172. } else {
  173. error(g_('unsupported version of the quilt metadata: %s'), $version);
  174. }
  175. }
  176. $self->SUPER::do_build($dir);
  177. }
  178. sub after_build {
  179. my ($self, $dir) = @_;
  180. my $quilt = $self->_build_quilt_object($dir);
  181. my $pc_unapply = $quilt->get_db_file('.dpkg-source-unapply');
  182. my $opt_unapply = $self->{options}{unapply_patches};
  183. if (($opt_unapply eq 'auto' and -e $pc_unapply) or $opt_unapply eq 'yes') {
  184. unlink($pc_unapply);
  185. $self->unapply_patches($dir);
  186. }
  187. }
  188. sub check_patches_applied {
  189. my ($self, $dir) = @_;
  190. my $quilt = $self->_build_quilt_object($dir);
  191. my $next = $quilt->next();
  192. return if not defined $next;
  193. my $first_patch = File::Spec->catfile($dir, 'debian', 'patches', $next);
  194. my $patch_obj = Dpkg::Source::Patch->new(filename => $first_patch);
  195. return unless $patch_obj->check_apply($dir, fatal_dupes => 1);
  196. $self->apply_patches($dir, usage => 'preparation', verbose => 1);
  197. }
  198. sub register_patch {
  199. my ($self, $dir, $tmpdiff, $patch_name) = @_;
  200. my $quilt = $self->_build_quilt_object($dir);
  201. my $patch = $quilt->get_patch_file($patch_name);
  202. if (-s $tmpdiff) {
  203. copy($tmpdiff, $patch)
  204. or syserr(g_('failed to copy %s to %s'), $tmpdiff, $patch);
  205. chmod(0666 & ~ umask(), $patch)
  206. or syserr(g_("unable to change permission of '%s'"), $patch);
  207. } elsif (-e $patch) {
  208. unlink($patch) or syserr(g_('cannot remove %s'), $patch);
  209. }
  210. if (-e $patch) {
  211. # Add patch to series file
  212. $quilt->register($patch_name);
  213. } else {
  214. # Remove auto_patch from series
  215. $quilt->unregister($patch_name);
  216. }
  217. return $patch;
  218. }
  219. 1;