Quilt.pm 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 <http://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('version does not contain a revision')) if $v->is_native();
  67. my $quilt = $self->build_quilt_object($dir);
  68. $msg = $quilt->find_problems();
  69. return (0, $msg) if $msg;
  70. return 1;
  71. }
  72. sub get_autopatch_name {
  73. my ($self) = @_;
  74. if ($self->{options}{single_debian_patch}) {
  75. return 'debian-changes';
  76. } else {
  77. return 'debian-changes-' . $self->{fields}{'Version'};
  78. }
  79. }
  80. sub apply_patches {
  81. my ($self, $dir, %opts) = @_;
  82. if ($opts{usage} eq 'unpack') {
  83. $opts{verbose} = 1;
  84. } elsif ($opts{usage} eq 'build') {
  85. $opts{warn_options} = 1;
  86. $opts{verbose} = 0;
  87. }
  88. my $quilt = $self->build_quilt_object($dir);
  89. $quilt->load_series(%opts) if $opts{warn_options}; # Trigger warnings
  90. # Always create the quilt db so that if the maintainer calls quilt to
  91. # create a patch, it's stored in the right directory
  92. $quilt->write_db();
  93. # Update debian/patches/series symlink if needed to allow quilt usage
  94. my $series = $quilt->get_series_file();
  95. my $basename = (File::Spec->splitpath($series))[2];
  96. if ($basename ne 'series') {
  97. my $dest = $quilt->get_patch_file('series');
  98. unlink($dest) if -l $dest;
  99. unless (-f _) { # Don't overwrite real files
  100. symlink($basename, $dest) ||
  101. syserr(_g("can't create symlink %s"), $dest);
  102. }
  103. }
  104. return unless scalar($quilt->series());
  105. if ($opts{usage} eq 'preparation' and
  106. $self->{options}{unapply_patches} eq 'auto') {
  107. # We're applying the patches in --before-build, remember to unapply
  108. # them afterwards in --after-build
  109. my $pc_unapply = $quilt->get_db_file('.dpkg-source-unapply');
  110. open(my $unapply_fh, '>', $pc_unapply) ||
  111. syserr(_g('cannot write %s'), $pc_unapply);
  112. close($unapply_fh);
  113. }
  114. # Apply patches
  115. my $pc_applied = $quilt->get_db_file('applied-patches');
  116. $opts{timestamp} = fs_time($pc_applied);
  117. if ($opts{skip_auto}) {
  118. my $auto_patch = $self->get_autopatch_name();
  119. $quilt->push(%opts) while ($quilt->next() and $quilt->next() ne $auto_patch);
  120. } else {
  121. $quilt->push(%opts) while $quilt->next();
  122. }
  123. }
  124. sub unapply_patches {
  125. my ($self, $dir, %opts) = @_;
  126. my $quilt = $self->build_quilt_object($dir);
  127. $opts{verbose} //= 1;
  128. my $pc_applied = $quilt->get_db_file('applied-patches');
  129. my @applied = $quilt->applied();
  130. $opts{timestamp} = fs_time($pc_applied) if @applied;
  131. $quilt->pop(%opts) while $quilt->top();
  132. erasedir($quilt->get_db_dir());
  133. }
  134. sub prepare_build {
  135. my ($self, $dir) = @_;
  136. $self->SUPER::prepare_build($dir);
  137. # Skip .pc directories of quilt by default and ignore difference
  138. # on debian/patches/series symlinks and d/p/.dpkg-source-applied
  139. # stamp file created by ourselves
  140. my $func = sub {
  141. return 1 if $_[0] =~ m{^debian/patches/series$} and -l $_[0];
  142. return 1 if $_[0] =~ /^\.pc(\/|$)/;
  143. return 1 if $_[0] =~ /$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 _add_line {
  184. my ($file, $line) = @_;
  185. open(my $file_fh, '>>', $file) || syserr(_g('cannot write %s'), $file);
  186. print $file_fh "$line\n";
  187. close($file_fh);
  188. }
  189. sub _drop_line {
  190. my ($file, $re) = @_;
  191. open(my $file_fh, '<', $file) || syserr(_g('cannot read %s'), $file);
  192. my @lines = <$file_fh>;
  193. close($file_fh);
  194. open($file_fh, '>', $file) || syserr(_g('cannot write %s'), $file);
  195. print($file_fh $_) foreach grep { not /^\Q$re\E\s*$/ } @lines;
  196. close($file_fh);
  197. }
  198. sub register_patch {
  199. my ($self, $dir, $tmpdiff, $patch_name) = @_;
  200. my $quilt = $self->build_quilt_object($dir);
  201. my @patches = $quilt->series();
  202. my $has_patch = (grep { $_ eq $patch_name } @patches) ? 1 : 0;
  203. my $series = $quilt->get_series_file();
  204. my $applied = $quilt->get_db_file('applied-patches');
  205. my $patch = $quilt->get_patch_file($patch_name);
  206. if (-s $tmpdiff) {
  207. copy($tmpdiff, $patch) ||
  208. syserr(_g('failed to copy %s to %s'), $tmpdiff, $patch);
  209. chmod(0666 & ~ umask(), $patch) ||
  210. syserr(_g("unable to change permission of `%s'"), $patch);
  211. } elsif (-e $patch) {
  212. unlink($patch) || syserr(_g('cannot remove %s'), $patch);
  213. }
  214. if (-e $patch) {
  215. $quilt->setup_db();
  216. # Add patch to series file
  217. if (not $has_patch) {
  218. _add_line($series, $patch_name);
  219. _add_line($applied, $patch_name);
  220. $quilt->load_series();
  221. $quilt->load_db();
  222. }
  223. # Ensure quilt meta-data are created and in sync with some trickery:
  224. # reverse-apply the patch, drop .pc/$patch, re-apply it
  225. # with the correct options to recreate the backup files
  226. $quilt->pop(reverse_apply => 1);
  227. $quilt->push();
  228. } else {
  229. # Remove auto_patch from series
  230. if ($has_patch) {
  231. _drop_line($series, $patch_name);
  232. _drop_line($applied, $patch_name);
  233. erasedir($quilt->get_db_file($patch_name));
  234. $quilt->load_db();
  235. $quilt->load_series();
  236. }
  237. # Clean up empty series
  238. unlink($series) if -z $series;
  239. }
  240. return $patch;
  241. }
  242. 1;