Quilt.pm 7.3 KB

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