2 # bzr support for dpkg-source
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>
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.
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.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <https://www.gnu.org/licenses/>.
22 package Dpkg::Source::Package::V3::Bzr;
27 our $VERSION = '0.01';
32 use File::Temp qw(tempdir);
35 use Dpkg::Compression;
36 use Dpkg::ErrorHandling;
37 use Dpkg::Source::Archive;
38 use Dpkg::Exit qw(push_exit_handler pop_exit_handler);
39 use Dpkg::Source::Functions qw(erasedir);
41 use parent qw(Dpkg::Source::Package);
43 our $CURRENT_MINOR_VERSION = '0';
46 foreach my $dir (split(/:/, $ENV{PATH})) {
51 error(g_('cannot unpack bzr-format source package because ' .
52 'bzr is not in the PATH'));
58 if (! -d "$srcdir/.bzr") {
59 error(g_('source directory is not the top directory of a bzr repository (%s/.bzr not present), but Format bzr was specified'),
63 # Symlinks from .bzr to outside could cause unpack failures, or
64 # point to files they shouldn't, so check for and don't allow.
65 if (-l "$srcdir/.bzr") {
66 error(g_('%s is a symlink'), "$srcdir/.bzr");
68 my $abs_srcdir = Cwd::abs_path($srcdir);
71 if (Cwd::abs_path(readlink) !~ /^\Q$abs_srcdir\E(?:\/|$)/) {
72 error(g_('%s is a symlink to outside %s'),
73 $File::Find::name, $srcdir);
82 my ($self, $dir) = @_;
84 return (0, g_("doesn't contain a bzr repository")) unless -d "$dir/.bzr";
89 my ($self, $dir) = @_;
90 my @argv = @{$self->{options}{ARGV}};
92 #my @tar_ignore = map { "--exclude=$_" } @{$self->{options}{tar_ignore}};
93 my $diff_ignore_regex = $self->{options}{diff_ignore_regex};
95 $dir =~ s{/+$}{}; # Strip trailing /
96 my ($dirname, $updir) = fileparse($dir);
99 usageerr(g_("-b takes only one parameter with format '%s'"),
100 $self->{fields}{'Format'});
103 my $sourcepackage = $self->{fields}{'Source'};
104 my $basenamerev = $self->get_basename(1);
105 my $basename = $self->get_basename();
106 my $basedirname = $basename;
107 $basedirname =~ s/_/-/;
111 my $old_cwd = getcwd();
112 chdir $dir or syserr(g_("unable to chdir to '%s'"), $dir);
116 # Check for uncommitted files.
117 # To support dpkg-source -i, remove any ignored files from the
118 # output of bzr status.
119 open(my $bzr_status_fh, '-|', 'bzr', 'status')
120 or subprocerr('bzr status');
122 while (<$bzr_status_fh>) {
125 if (! length $diff_ignore_regex ||
126 ! m/$diff_ignore_regex/o) {
130 close($bzr_status_fh) or syserr(g_('bzr status exited nonzero'));
132 error(g_('uncommitted, not-ignored changes in working directory: %s'),
136 chdir $old_cwd or syserr(g_("unable to chdir to '%s'"), $old_cwd);
138 my $tmp = tempdir("$dirname.bzr.XXXXXX", DIR => $updir);
139 push_exit_handler(sub { erasedir($tmp) });
140 my $tardir = "$tmp/$dirname";
142 system('bzr', 'branch', $dir, $tardir);
143 subprocerr("bzr branch $dir $tardir") if $?;
145 # Remove the working tree.
146 system('bzr', 'remove-tree', $tardir);
147 subprocerr("bzr remove-tree $tardir") if $?;
149 # Some branch metadata files are unhelpful.
150 unlink("$tardir/.bzr/branch/branch-name",
151 "$tardir/.bzr/branch/parent");
153 # Create the tar file
154 my $debianfile = "$basenamerev.bzr.tar." . $self->{options}{comp_ext};
155 info(g_('building %s in %s'),
156 $sourcepackage, $debianfile);
157 my $tar = Dpkg::Source::Archive->new(filename => $debianfile,
158 compression => $self->{options}{compression},
159 compression_level => $self->{options}{comp_level});
160 $tar->create(chdir => $tmp);
161 $tar->add_directory($dirname);
167 $self->add_file($debianfile);
170 # Called after a tarball is unpacked, to check out the working copy.
172 my ($self, $newdirectory) = @_;
173 my $fields = $self->{fields};
175 my $dscdir = $self->{basedir};
177 my $basename = $self->get_basename();
178 my $basenamerev = $self->get_basename(1);
180 my @files = $self->get_files();
182 error(g_('format v3.0 uses only one source file'));
184 my $tarfile = $files[0];
185 my $comp_ext_regex = compression_get_file_extension_regex();
186 if ($tarfile !~ /^\Q$basenamerev\E\.bzr\.tar\.$comp_ext_regex$/) {
187 error(g_('expected %s, got %s'),
188 "$basenamerev.bzr.tar.$comp_ext_regex", $tarfile);
191 if ($self->{options}{no_overwrite_dir} and -e $newdirectory) {
192 error(g_('unpack target exists: %s'), $newdirectory);
194 erasedir($newdirectory);
197 # Extract main tarball
198 info(g_('unpacking %s'), $tarfile);
199 my $tar = Dpkg::Source::Archive->new(filename => "$dscdir$tarfile");
200 $tar->extract($newdirectory);
202 _sanity_check($newdirectory);
204 my $old_cwd = getcwd();
206 or syserr(g_("unable to chdir to '%s'"), $newdirectory);
208 # Reconstitute the working tree.
209 system('bzr', 'checkout');
210 subprocerr('bzr checkout') if $?;
212 chdir $old_cwd or syserr(g_("unable to chdir to '%s'"), $old_cwd);