quilt.pm 8.6 KB

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