bzr.pm 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #
  2. # bzr support for dpkg-source
  3. #
  4. # Copyright © 2007 Colin Watson <cjwatson@debian.org>.
  5. # Based on Dpkg::Source::Package::V3_0::git, which is:
  6. # Copyright © 2007 Joey Hess <joeyh@debian.org>.
  7. # Copyright © 2008 Frank Lichtenheld <djpig@debian.org>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. package Dpkg::Source::Package::V3::bzr;
  22. use strict;
  23. use warnings;
  24. use base 'Dpkg::Source::Package';
  25. use Cwd;
  26. use File::Basename;
  27. use File::Find;
  28. use File::Temp qw(tempdir);
  29. use Dpkg;
  30. use Dpkg::Gettext;
  31. use Dpkg::Compression;
  32. use Dpkg::ErrorHandling;
  33. use Dpkg::Source::Archive;
  34. use Dpkg::Exit;
  35. use Dpkg::Source::Functions qw(erasedir);
  36. our $CURRENT_MINOR_VERSION = "0";
  37. sub import {
  38. foreach my $dir (split(/:/, $ENV{PATH})) {
  39. if (-x "$dir/bzr") {
  40. return 1;
  41. }
  42. }
  43. error(_g("This source package can only be manipulated using bzr, which is not in the PATH."));
  44. }
  45. sub sanity_check {
  46. my $srcdir = shift;
  47. if (! -d "$srcdir/.bzr") {
  48. error(_g("source directory is not the top directory of a bzr repository (%s/.bzr not present), but Format bzr was specified"),
  49. $srcdir);
  50. }
  51. # Symlinks from .bzr to outside could cause unpack failures, or
  52. # point to files they shouldn't, so check for and don't allow.
  53. if (-l "$srcdir/.bzr") {
  54. error(_g("%s is a symlink"), "$srcdir/.bzr");
  55. }
  56. my $abs_srcdir = Cwd::abs_path($srcdir);
  57. find(sub {
  58. if (-l $_) {
  59. if (Cwd::abs_path(readlink($_)) !~ /^\Q$abs_srcdir\E(\/|$)/) {
  60. error(_g("%s is a symlink to outside %s"),
  61. $File::Find::name, $srcdir);
  62. }
  63. }
  64. }, "$srcdir/.bzr");
  65. return 1;
  66. }
  67. sub can_build {
  68. my ($self, $dir) = @_;
  69. return (-d "$dir/.bzr", _g("doesn't contain a bzr repository"));
  70. }
  71. sub do_build {
  72. my ($self, $dir) = @_;
  73. my @argv = @{$self->{'options'}{'ARGV'}};
  74. # TODO: warn here?
  75. #my @tar_ignore = map { "--exclude=$_" } @{$self->{'options'}{'tar_ignore'}};
  76. my $diff_ignore_regexp = $self->{'options'}{'diff_ignore_regexp'};
  77. $dir =~ s{/+$}{}; # Strip trailing /
  78. my ($dirname, $updir) = fileparse($dir);
  79. if (scalar(@argv)) {
  80. usageerr(_g("-b takes only one parameter with format `%s'"),
  81. $self->{'fields'}{'Format'});
  82. }
  83. my $sourcepackage = $self->{'fields'}{'Source'};
  84. my $basenamerev = $self->get_basename(1);
  85. my $basename = $self->get_basename();
  86. my $basedirname = $basename;
  87. $basedirname =~ s/_/-/;
  88. sanity_check($dir);
  89. my $old_cwd = getcwd();
  90. chdir($dir) ||
  91. syserr(_g("unable to chdir to `%s'"), $dir);
  92. # Check for uncommitted files.
  93. # To support dpkg-source -i, remove any ignored files from the
  94. # output of bzr status.
  95. open(BZR_STATUS, '-|', "bzr", "status") ||
  96. subprocerr("bzr status");
  97. my @files;
  98. while (<BZR_STATUS>) {
  99. chomp;
  100. next unless s/^ +//;
  101. if (! length $diff_ignore_regexp ||
  102. ! m/$diff_ignore_regexp/o) {
  103. push @files, $_;
  104. }
  105. }
  106. close(BZR_STATUS) || syserr(_g("bzr status exited nonzero"));
  107. if (@files) {
  108. error(_g("uncommitted, not-ignored changes in working directory: %s"),
  109. join(" ", @files));
  110. }
  111. chdir($old_cwd) ||
  112. syserr(_g("unable to chdir to `%s'"), $old_cwd);
  113. my $tmp = tempdir("$dirname.bzr.XXXXXX", DIR => $updir);
  114. push @Dpkg::Exit::handlers, sub { erasedir($tmp) };
  115. my $tardir = "$tmp/$dirname";
  116. system("bzr", "branch", $dir, $tardir);
  117. $? && subprocerr("bzr branch $dir $tardir");
  118. # Remove the working tree.
  119. system("bzr", "remove-tree", $tardir);
  120. # Some branch metadata files are unhelpful.
  121. unlink("$tardir/.bzr/branch/branch-name",
  122. "$tardir/.bzr/branch/parent");
  123. # Create the tar file
  124. my $debianfile = "$basenamerev.bzr.tar." . $self->{'options'}{'comp_ext'};
  125. info(_g("building %s in %s"),
  126. $sourcepackage, $debianfile);
  127. my $tar = Dpkg::Source::Archive->new(filename => $debianfile,
  128. compression => $self->{'options'}{'compression'},
  129. compression_level => $self->{'options'}{'comp_level'});
  130. $tar->create('chdir' => $tmp);
  131. $tar->add_directory($dirname);
  132. $tar->finish();
  133. erasedir($tmp);
  134. pop @Dpkg::Exit::handlers;
  135. $self->add_file($debianfile);
  136. }
  137. # Called after a tarball is unpacked, to check out the working copy.
  138. sub do_extract {
  139. my ($self, $newdirectory) = @_;
  140. my $fields = $self->{'fields'};
  141. my $dscdir = $self->{'basedir'};
  142. my $basename = $self->get_basename();
  143. my $basenamerev = $self->get_basename(1);
  144. my @files = $self->get_files();
  145. if (@files > 1) {
  146. error(_g("format v3.0 uses only one source file"));
  147. }
  148. my $tarfile = $files[0];
  149. if ($tarfile !~ /^\Q$basenamerev\E\.bzr\.tar\.$comp_regex$/) {
  150. error(_g("expected %s, got %s"),
  151. "$basenamerev.bzr.tar.$comp_regex", $tarfile);
  152. }
  153. erasedir($newdirectory);
  154. # Extract main tarball
  155. info(_g("unpacking %s"), $tarfile);
  156. my $tar = Dpkg::Source::Archive->new(filename => "$dscdir$tarfile");
  157. $tar->extract($newdirectory);
  158. sanity_check($newdirectory);
  159. my $old_cwd = getcwd();
  160. chdir($newdirectory) ||
  161. syserr(_g("unable to chdir to `%s'"), $newdirectory);
  162. # Reconstitute the working tree.
  163. system("bzr", "checkout");
  164. chdir($old_cwd) ||
  165. syserr(_g("unable to chdir to `%s'"), $old_cwd);
  166. }
  167. 1;