| 1 | # Copyright © 2007 Colin Watson <cjwatson@debian.org>. | ||
| 2 | # Based on Dpkg::Source::Package::V3_0::git, which is: | ||
| 3 | # Copyright © 2007 Joey Hess <joeyh@debian.org>. | ||
| 4 | # Copyright © 2008 Frank Lichtenheld <djpig@debian.org> | ||
| 5 | # | ||
| 6 | # This program is free software; you can redistribute it and/or modify | ||
| 7 | # it under the terms of the GNU General Public License as published by | ||
| 8 | # the Free Software Foundation; either version 2 of the License, or | ||
| 9 | # (at your option) any later version. | ||
| 10 | # | ||
| 11 | # This program is distributed in the hope that it will be useful, | ||
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | # GNU General Public License for more details. | ||
| 15 | # | ||
| 16 | # You should have received a copy of the GNU General Public License | ||
| 17 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 18 | |||
| 19 | =encoding utf8 | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | Dpkg::Source::Package::V3::Bzr - class for source format 3.0 (bzr) | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | This module provides a class to handle the source package format 3.0 (bzr). | ||
| 28 | |||
| 29 | B<Note>: This is a private module, its API can change at any time. | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | package Dpkg::Source::Package::V3::Bzr 0.01; | ||
| 34 | |||
| 35 | use v5.36; | ||
| 36 | |||
| 37 | use Cwd; | ||
| 38 | use File::Basename; | ||
| 39 | use File::Spec; | ||
| 40 | use File::Find; | ||
| 41 | use File::Temp; | ||
| 42 | |||
| 43 | use Dpkg::Gettext; | ||
| 44 | use Dpkg::Compression; | ||
| 45 | use Dpkg::ErrorHandling; | ||
| 46 | use Dpkg::Source::Archive; | ||
| 47 | use Dpkg::Exit qw(push_exit_handler pop_exit_handler); | ||
| 48 | use Dpkg::Path qw(find_command); | ||
| 49 | use Dpkg::Source::Functions qw(erasedir); | ||
| 50 | |||
| 51 | use parent qw(Dpkg::Source::Package); | ||
| 52 | |||
| 53 | sub CURRENT_MINOR_VERSION { | ||
| 54 | '0'; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub prerequisites { | ||
| 58 | return 1 if find_command('bzr'); | ||
| 59 | error(g_('cannot unpack bzr-format source package because ' . | ||
| 60 | 'bzr is not in the PATH')); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub _check_workdir { | ||
| 64 | my $srcdir = shift; | ||
| 65 | |||
| 66 | if (! -d "$srcdir/.bzr") { | ||
| 67 | error(g_('source directory is not the top directory of a bzr repository (%s/.bzr not present), but Format bzr was specified'), | ||
| 68 | $srcdir); | ||
| 69 | } | ||
| 70 | |||
| 71 | # Symlinks from .bzr to outside could cause unpack failures, or point to | ||
| 72 | # files they should not, so check for and do not allow. | ||
| 73 | if (-l "$srcdir/.bzr") { | ||
| 74 | error(g_('%s is a symbolic link'), "$srcdir/.bzr"); | ||
| 75 | } | ||
| 76 | my $abs_srcdir = Cwd::abs_path($srcdir); | ||
| 77 | find(sub { | ||
| 78 | if (-l) { | ||
| 79 | if (Cwd::abs_path(readlink) !~ /^\Q$abs_srcdir\E(?:\/|$)/) { | ||
| 80 | error(g_('%s is a symbolic link to outside %s'), | ||
| 81 | $File::Find::name, $srcdir); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | }, "$srcdir/.bzr"); | ||
| 85 | |||
| 86 | return 1; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub can_build { | ||
| 90 | my ($self, $dir) = @_; | ||
| 91 | |||
| 92 | return (0, g_('does not contain a bzr repository')) unless -d "$dir/.bzr"; | ||
| 93 | return 1; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub do_build { | ||
| 97 | my ($self, $dir) = @_; | ||
| 98 | my @argv = @{$self->{options}{ARGV}}; | ||
| 99 | # TODO: warn here? | ||
| 100 | #my @tar_ignore = map { "--exclude=$_" } @{$self->{options}{tar_ignore}}; | ||
| 101 | my $diff_ignore_regex = $self->{options}{diff_ignore_regex}; | ||
| 102 | |||
| 103 | # Strip trailing "/". | ||
| 104 | $dir =~ s{/+$}{}; | ||
| 105 | my ($dirname, $updir) = fileparse($dir); | ||
| 106 | |||
| 107 | if (scalar(@argv)) { | ||
| 108 | usageerr(g_("-b takes only one parameter with format '%s'"), | ||
| 109 | $self->{fields}{'Format'}); | ||
| 110 | } | ||
| 111 | |||
| 112 | my $sourcepackage = $self->{fields}{'Source'}; | ||
| 113 | my $basenamerev = $self->get_basename(1); | ||
| 114 | |||
| 115 | _check_workdir($dir); | ||
| 116 | |||
| 117 | my $old_cwd = getcwd(); | ||
| 118 | chdir $dir or syserr(g_("cannot change directory to '%s'"), $dir); | ||
| 119 | |||
| 120 | local $_; | ||
| 121 | |||
| 122 | # Check for uncommitted files. | ||
| 123 | # To support «dpkg-source -i», remove any ignored files from the | ||
| 124 | # output of bzr status. | ||
| 125 | open(my $bzr_status_fh, '-|', 'bzr', 'status') | ||
| 126 | or subprocerr('bzr status'); | ||
| 127 | my @files; | ||
| 128 | while (<$bzr_status_fh>) { | ||
| 129 | chomp; | ||
| 130 | next unless s/^ +//; | ||
| 131 | if (! length $diff_ignore_regex || | ||
| 132 | ! m/$diff_ignore_regex/o) { | ||
| 133 | push @files, $_; | ||
| 134 | } | ||
| 135 | } | ||
| 136 | close($bzr_status_fh) or subprocerr('bzr status'); | ||
| 137 | if (@files) { | ||
| 138 | error(g_('uncommitted, not-ignored changes in working directory: %s'), | ||
| 139 | join(' ', @files)); | ||
| 140 | } | ||
| 141 | |||
| 142 | chdir $old_cwd or syserr(g_("cannot change directory to '%s'"), $old_cwd); | ||
| 143 | |||
| 144 | my $tmpdir = File::Temp->newdir( | ||
| 145 | TEMPLATE => "$dirname.bzr.XXXXXX", | ||
| 146 | DIR => $updir, | ||
| 147 | CLEANUP => 0, | ||
| 148 | ); | ||
| 149 | push_exit_handler(sub { erasedir($tmpdir) }); | ||
| 150 | my $tardir = "$tmpdir/$dirname"; | ||
| 151 | |||
| 152 | system('bzr', 'branch', $dir, $tardir); | ||
| 153 | subprocerr("bzr branch $dir $tardir") if $?; | ||
| 154 | |||
| 155 | # Remove the working tree. | ||
| 156 | system('bzr', 'remove-tree', $tardir); | ||
| 157 | subprocerr("bzr remove-tree $tardir") if $?; | ||
| 158 | |||
| 159 | # Some branch metadata files are unhelpful. | ||
| 160 | unlink("$tardir/.bzr/branch/branch-name", | ||
| 161 | "$tardir/.bzr/branch/parent"); | ||
| 162 | |||
| 163 | # Create the tar file. | ||
| 164 | my $debianfile = File::Spec->catfile($self->{basedir}, "$basenamerev.bzr.tar." . $self->{options}{comp_ext}); | ||
| 165 | info(g_('building %s in %s'), | ||
| 166 | $sourcepackage, $debianfile); | ||
| 167 | my $tar = Dpkg::Source::Archive->new( | ||
| 168 | filename => $debianfile, | ||
| 169 | compression => $self->{options}{compression}, | ||
| 170 | compression_level => $self->{options}{comp_level}, | ||
| 171 | ); | ||
| 172 | $tar->create(chdir => $tmpdir); | ||
| 173 | $tar->add_directory($dirname); | ||
| 174 | $tar->finish(); | ||
| 175 | |||
| 176 | erasedir($tmpdir); | ||
| 177 | pop_exit_handler(); | ||
| 178 | |||
| 179 | $self->add_file($debianfile); | ||
| 180 | } | ||
| 181 | |||
| 182 | # Called after a tarball is unpacked, to check out the working copy. | ||
| 183 | sub do_extract { | ||
| 184 | my ($self, $newdirectory) = @_; | ||
| 185 | my $fields = $self->{fields}; | ||
| 186 | |||
| 187 | my $basenamerev = $self->get_basename(1); | ||
| 188 | |||
| 189 | my @files = $self->get_files(); | ||
| 190 | if (@files > 1) { | ||
| 191 | error(g_('format v3.0 (bzr) uses only one source file')); | ||
| 192 | } | ||
| 193 | my $tarfile = $files[0]; | ||
| 194 | my $comp_ext_regex = compression_get_file_extension_regex(); | ||
| 195 | if ($tarfile !~ /^\Q$basenamerev\E\.bzr\.tar\.$comp_ext_regex$/) { | ||
| 196 | error(g_('expected %s, got %s'), | ||
| 197 | "$basenamerev.bzr.tar.$comp_ext_regex", $tarfile); | ||
| 198 | } | ||
| 199 | |||
| 200 | if ($self->{options}{no_overwrite_dir} and -e $newdirectory) { | ||
| 201 | error(g_('unpack target exists: %s'), $newdirectory); | ||
| 202 | } else { | ||
| 203 | erasedir($newdirectory); | ||
| 204 | } | ||
| 205 | |||
| 206 | # Extract main tarball. | ||
| 207 | info(g_('unpacking %s'), $tarfile); | ||
| 208 | my $tar = Dpkg::Source::Archive->new( | ||
| 209 | filename => File::Spec->catfile($self->{basedir}, $tarfile), | ||
| 210 | ); | ||
| 211 | $tar->extract($newdirectory); | ||
| 212 | |||
| 213 | _check_workdir($newdirectory); | ||
| 214 | |||
| 215 | my $old_cwd = getcwd(); | ||
| 216 | chdir($newdirectory) | ||
| 217 | or syserr(g_("cannot change directory to '%s'"), $newdirectory); | ||
| 218 | |||
| 219 | # Reconstitute the working tree. | ||
| 220 | system('bzr', 'checkout'); | ||
| 221 | subprocerr('bzr checkout') if $?; | ||
| 222 | |||
| 223 | chdir $old_cwd or syserr(g_("cannot change directory to '%s'"), $old_cwd); | ||
| 224 | } | ||
| 225 | |||
| 226 | =head1 CHANGES | ||
| 227 | |||
| 228 | =head2 Version 0.xx | ||
| 229 | |||
| 230 | This is a private module. | ||
| 231 | |||
| 232 | =cut | ||
| 233 | |||
| 234 | 1; |