Quilt.pm 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. unless exists $self->{options}{single_debian_patch};
  37. $self->{options}{allow_version_of_quilt_db} = []
  38. unless exists $self->{options}{allow_version_of_quilt_db};
  39. $self->SUPER::init_options();
  40. }
  41. sub parse_cmdline_option {
  42. my ($self, $opt) = @_;
  43. return 1 if $self->SUPER::parse_cmdline_option($opt);
  44. if ($opt =~ /^--single-debian-patch$/) {
  45. $self->{options}{single_debian_patch} = 1;
  46. # For backwards compatibility.
  47. $self->{options}{auto_commit} = 1;
  48. return 1;
  49. } elsif ($opt =~ /^--allow-version-of-quilt-db=(.*)$/) {
  50. push @{$self->{options}{allow_version_of_quilt_db}}, $1;
  51. return 1;
  52. }
  53. return 0;
  54. }
  55. sub build_quilt_object {
  56. my ($self, $dir) = @_;
  57. return $self->{quilt}{$dir} if exists $self->{quilt}{$dir};
  58. $self->{quilt}{$dir} = Dpkg::Source::Quilt->new($dir);
  59. return $self->{quilt}{$dir};
  60. }
  61. sub can_build {
  62. my ($self, $dir) = @_;
  63. my ($code, $msg) = $self->SUPER::can_build($dir);
  64. return ($code, $msg) if $code == 0;
  65. my $v = Dpkg::Version->new($self->{fields}->{'Version'});
  66. return (0, _g('non-native package version does not contain a revision'))
  67. if $v->is_native();
  68. my $quilt = $self->build_quilt_object($dir);
  69. $msg = $quilt->find_problems();
  70. return (0, $msg) if $msg;
  71. return 1;
  72. }
  73. sub get_autopatch_name {
  74. my ($self) = @_;
  75. if ($self->{options}{single_debian_patch}) {
  76. return 'debian-changes';
  77. } else {
  78. return 'debian-changes-' . $self->{fields}{'Version'};
  79. }
  80. }
  81. sub apply_patches {
  82. my ($self, $dir, %opts) = @_;
  83. if ($opts{usage} eq 'unpack') {
  84. $opts{verbose} = 1;
  85. } elsif ($opts{usage} eq 'build') {
  86. $opts{warn_options} = 1;
  87. $opts{verbose} = 0;
  88. }
  89. my $quilt = $self->build_quilt_object($dir);
  90. $quilt->load_series(%opts) if $opts{warn_options}; # Trigger warnings
  91. # Always create the quilt db so that if the maintainer calls quilt to
  92. # create a patch, it's stored in the right directory
  93. $quilt->write_db();
  94. # Update debian/patches/series symlink if needed to allow quilt usage
  95. my $series = $quilt->get_series_file();
  96. my $basename = (File::Spec->splitpath($series))[2];
  97. if ($basename ne 'series') {
  98. my $dest = $quilt->get_patch_file('series');
  99. unlink($dest) if -l $dest;
  100. unless (-f _) { # Don't overwrite real files
  101. symlink($basename, $dest)
  102. or syserr(_g("can't create symlink %s"), $dest);
  103. }
  104. }
  105. return unless scalar($quilt->series());
  106. if ($opts{usage} eq 'preparation' and
  107. $self->{options}{unapply_patches} eq 'auto') {
  108. # We're applying the patches in --before-build, remember to unapply
  109. # them afterwards in --after-build
  110. my $pc_unapply = $quilt->get_db_file('.dpkg-source-unapply');
  111. open(my $unapply_fh, '>', $pc_unapply)
  112. or syserr(_g('cannot write %s'), $pc_unapply);
  113. close($unapply_fh);
  114. }
  115. # Apply patches
  116. my $pc_applied = $quilt->get_db_file('applied-patches');
  117. $opts{timestamp} = fs_time($pc_applied);
  118. if ($opts{skip_auto}) {
  119. my $auto_patch = $self->get_autopatch_name();
  120. $quilt->push(%opts) while ($quilt->next() and $quilt->next() ne $auto_patch);
  121. } else {
  122. $quilt->push(%opts) while $quilt->next();
  123. }
  124. }
  125. sub unapply_patches {
  126. my ($self, $dir, %opts) = @_;
  127. my $quilt = $self->build_quilt_object($dir);
  128. $opts{verbose} //= 1;
  129. my $pc_applied = $quilt->get_db_file('applied-patches');
  130. my @applied = $quilt->applied();
  131. $opts{timestamp} = fs_time($pc_applied) if @applied;
  132. $quilt->pop(%opts) while $quilt->top();
  133. erasedir($quilt->get_db_dir());
  134. }
  135. sub prepare_build {
  136. my ($self, $dir) = @_;
  137. $self->SUPER::prepare_build($dir);
  138. # Skip .pc directories of quilt by default and ignore difference
  139. # on debian/patches/series symlinks and d/p/.dpkg-source-applied
  140. # stamp file created by ourselves
  141. my $func = sub {
  142. return 1 if $_[0] =~ m{^debian/patches/series$} and -l $_[0];
  143. return 1 if $_[0] =~ /^\.pc(\/|$)/;
  144. return 1 if $_[0] =~ /$self->{options}{diff_ignore_regex}/;
  145. return 0;
  146. };
  147. $self->{diff_options}{diff_ignore_func} = $func;
  148. }
  149. sub do_build {
  150. my ($self, $dir) = @_;
  151. my $quilt = $self->build_quilt_object($dir);
  152. my $version = $quilt->get_db_version();
  153. if (defined($version) and $version != 2) {
  154. if (any { $version eq $_ }
  155. @{$self->{options}{allow_version_of_quilt_db}})
  156. {
  157. warning(_g('unsupported version of the quilt metadata: %s'), $version);
  158. } else {
  159. error(_g('unsupported version of the quilt metadata: %s'), $version);
  160. }
  161. }
  162. $self->SUPER::do_build($dir);
  163. }
  164. sub after_build {
  165. my ($self, $dir) = @_;
  166. my $quilt = $self->build_quilt_object($dir);
  167. my $pc_unapply = $quilt->get_db_file('.dpkg-source-unapply');
  168. my $opt_unapply = $self->{options}{unapply_patches};
  169. if (($opt_unapply eq 'auto' and -e $pc_unapply) or $opt_unapply eq 'yes') {
  170. unlink($pc_unapply);
  171. $self->unapply_patches($dir);
  172. }
  173. }
  174. sub check_patches_applied {
  175. my ($self, $dir) = @_;
  176. my $quilt = $self->build_quilt_object($dir);
  177. my $next = $quilt->next();
  178. return if not defined $next;
  179. my $first_patch = File::Spec->catfile($dir, 'debian', 'patches', $next);
  180. my $patch_obj = Dpkg::Source::Patch->new(filename => $first_patch);
  181. return unless $patch_obj->check_apply($dir);
  182. $self->apply_patches($dir, usage => 'preparation', verbose => 1);
  183. }
  184. sub _add_line {
  185. my ($file, $line) = @_;
  186. open(my $file_fh, '>>', $file) or syserr(_g('cannot write %s'), $file);
  187. print { $file_fh } "$line\n";
  188. close($file_fh);
  189. }
  190. sub _drop_line {
  191. my ($file, $re) = @_;
  192. open(my $file_fh, '<', $file) or syserr(_g('cannot read %s'), $file);
  193. my @lines = <$file_fh>;
  194. close($file_fh);
  195. open($file_fh, '>', $file) or syserr(_g('cannot write %s'), $file);
  196. print { $file_fh } $_ foreach grep { not /^\Q$re\E\s*$/ } @lines;
  197. close($file_fh);
  198. }
  199. sub register_patch {
  200. my ($self, $dir, $tmpdiff, $patch_name) = @_;
  201. my $quilt = $self->build_quilt_object($dir);
  202. my @patches = $quilt->series();
  203. my $has_patch = (grep { $_ eq $patch_name } @patches) ? 1 : 0;
  204. my $series = $quilt->get_series_file();
  205. my $applied = $quilt->get_db_file('applied-patches');
  206. my $patch = $quilt->get_patch_file($patch_name);
  207. if (-s $tmpdiff) {
  208. copy($tmpdiff, $patch)
  209. or syserr(_g('failed to copy %s to %s'), $tmpdiff, $patch);
  210. chmod(0666 & ~ umask(), $patch)
  211. or syserr(_g("unable to change permission of `%s'"), $patch);
  212. } elsif (-e $patch) {
  213. unlink($patch) or syserr(_g('cannot remove %s'), $patch);
  214. }
  215. if (-e $patch) {
  216. $quilt->setup_db();
  217. # Add patch to series file
  218. if (not $has_patch) {
  219. _add_line($series, $patch_name);
  220. _add_line($applied, $patch_name);
  221. $quilt->load_series();
  222. $quilt->load_db();
  223. }
  224. # Ensure quilt meta-data are created and in sync with some trickery:
  225. # reverse-apply the patch, drop .pc/$patch, re-apply it
  226. # with the correct options to recreate the backup files
  227. $quilt->pop(reverse_apply => 1);
  228. $quilt->push();
  229. } else {
  230. # Remove auto_patch from series
  231. if ($has_patch) {
  232. _drop_line($series, $patch_name);
  233. _drop_line($applied, $patch_name);
  234. erasedir($quilt->get_db_file($patch_name));
  235. $quilt->load_db();
  236. $quilt->load_series();
  237. }
  238. # Clean up empty series
  239. unlink($series) if -z $series;
  240. }
  241. return $patch;
  242. }
  243. 1;