Dpkg/Source/Package/V3/Git.pm untested

filter: Statementstmt 0.0% 0 / 171 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 319 CC 33 SCAR 44.3
CC
33
Cov
0%
do_extract
182.0
do_build
156.0
_check_workdir
20.0
CRAP 84.2
1 # Copyright © 2007,2010 Joey Hess <joeyh@debian.org>.
2 # Copyright © 2008 Frank Lichtenheld <djpig@debian.org>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16  
17 =encoding utf8
18  
19 =head1 NAME
20  
21 Dpkg::Source::Package::V3::Git - class for source format 3.0 (git)
22  
23 =head1 DESCRIPTION
24  
25 This module provides a class to handle the source package format 3.0 (git).
26  
27 B<Note>: This is a private module, its API can change at any time.
28  
29 =cut
30  
31 package Dpkg::Source::Package::V3::Git 0.02;
32  
33 use v5.36;
34  
35 use Cwd qw(abs_path getcwd);
36 use File::Basename;
37 use File::Spec;
38 use File::Temp;
39  
40 use Dpkg::Gettext;
41 use Dpkg::ErrorHandling;
42 use Dpkg::Exit qw(push_exit_handler pop_exit_handler);
43 use Dpkg::Path qw(find_command);
44 use Dpkg::IPC;
45 use Dpkg::Source::Functions qw(erasedir);
46  
47 use parent qw(Dpkg::Source::Package);
48  
49 sub CURRENT_MINOR_VERSION {
50 '0';
51 }
52  
53 # Remove variables from the environment that might cause git to do
54 # something unexpected.
55 delete $ENV{GIT_DIR};
56 delete $ENV{GIT_INDEX_FILE};
57 delete $ENV{GIT_OBJECT_DIRECTORY};
58 delete $ENV{GIT_ALTERNATE_OBJECT_DIRECTORIES};
59 delete $ENV{GIT_WORK_TREE};
60  
61 sub prerequisites {
62 return 1 if find_command('git');
63 error(g_('cannot unpack git-format source package because ' .
64 'git is not in the PATH'));
65 }
66  
67 sub _check_workdir {
68 my $srcdir = shift;
69  
70 if (! -d "$srcdir/.git") {
71 error(g_('source directory is not the top directory of a git ' .
72 'repository (%s/.git not present), but Format git was ' .
73 'specified'), $srcdir);
74 }
75 if (-s "$srcdir/.gitmodules") {
76 my $stdout;
77  
78 spawn(
79 exec => [ qw(git submodule status --recursive) ],
80 chdir => $srcdir,
81 wait_child => 1,
82 to_string => \$stdout,
83 );
84 if (length $stdout) {
85 error(g_('git repository %s uses submodules; ' .
86 'this is not yet supported'),
87 $srcdir);
88 }
89 }
90  
91 return 1;
92 }
93  
94 sub _parse_vcs_git {
95 my $vcs_git = shift;
96 my ($url, $opt, $branch) = split ' ', $vcs_git;
97  
98 if (defined $opt && $opt eq '-b' && defined $branch) {
99 return ($url, $branch);
100 } else {
101 return ($url);
102 }
103 }
104  
105 my @module_cmdline = (
106 {
107 name => '--git-ref=<ref>',
108 help => N_('Specify a git <ref> to include in the git bundle.'),
109 when => 'build',
110 }, {
111 name => '--git-depth=<number>',
112 help => N_('Create a shallow clone with <number> depth.'),
113 when => 'build',
114 }
115 );
116  
117 sub describe_cmdline_options {
118 my $self = shift;
119  
120 my @cmdline = (
121 $self->SUPER::describe_cmdline_options(),
122 @module_cmdline,
123 );
124  
125 return @cmdline;
126 }
127  
128 sub parse_cmdline_option {
129 my ($self, $opt) = @_;
130 return 1 if $self->SUPER::parse_cmdline_option($opt);
131 if ($opt =~ /^--git-ref=(.*)$/) {
132 push @{$self->{options}{git_ref}}, $1;
133 return 1;
134 } elsif ($opt =~ /^--git-depth=(\d+)$/) {
135 $self->{options}{git_depth} = $1;
136 return 1;
137 }
138 return 0;
139 }
140  
141 sub can_build {
142 my ($self, $dir) = @_;
143  
144 return (0, g_('does not contain a git repository')) unless -d "$dir/.git";
145 return 1;
146 }
147  
148 sub do_build {
149 my ($self, $dir) = @_;
150 my $diff_ignore_regex = $self->{options}{diff_ignore_regex};
151  
152 # Strip trailing "/".
153 $dir =~ s{/+$}{};
154 my ($dirname, $updir) = fileparse($dir);
155 my $basenamerev = $self->get_basename(1);
156  
157 _check_workdir($dir);
158  
159 my $old_cwd = getcwd();
160 chdir $dir or syserr(g_("cannot change directory to '%s'"), $dir);
161  
162 # Check for uncommitted files.
163 # To support «dpkg-source -i», get a list of files equivalent to the ones
164 # «git status» finds, and remove any ignored files from it.
165 my @ignores = '--exclude-per-directory=.gitignore';
166 my $core_excludesfile = qx(git config --get core.excludesfile);
167 chomp $core_excludesfile;
168 if (length $core_excludesfile && -e $core_excludesfile) {
169 push @ignores, "--exclude-from=$core_excludesfile";
170 }
171 if (-e '.git/info/exclude') {
172 push @ignores, '--exclude-from=.git/info/exclude';
173 }
174 open(my $git_ls_files_fh, '-|', 'git', 'ls-files', '--modified', '--deleted',
175 '-z', '--others', @ignores) or subprocerr('git ls-files');
176 my @files;
177 {
178 local $_;
179 local $/ = "\0";
180 while (<$git_ls_files_fh>) {
181 chomp;
182 if (! length $diff_ignore_regex || ! m/$diff_ignore_regex/o) {
183 push @files, $_;
184 }
185 }
186 }
187 close($git_ls_files_fh) or subprocerr('git ls-files');
188 if (@files) {
189 error(g_('uncommitted, not-ignored changes in working directory: %s'),
190 join(' ', @files));
191 }
192  
193 # If a depth was specified, need to create a shallow clone and bundle that.
194 my $tmpdir;
195 my $shallowfile;
196 if ($self->{options}{git_depth}) {
197 chdir $old_cwd
198 or syserr(g_("cannot change directory to '%s'"), $old_cwd);
199 $tmpdir = File::Temp->newdir(
200 TEMPLATE => "$dirname.git.XXXXXX",
201 DIR => $updir,
202 CLEANUP => 0,
203 );
204 push_exit_handler(sub { erasedir($tmpdir) });
205 my $clone_dir = "$tmpdir/repo.git";
206 # file:// is needed to avoid local cloning, which does not
207 # create a shallow clone.
208 info(g_('creating shallow clone with depth %s'),
209 $self->{options}{git_depth});
210 system(
211 'git', 'clone',
212 '--depth=' . $self->{options}{git_depth},
213 '--quiet',
214 '--bare',
215 '--end-of-options',
216 'file://' . abs_path($dir), $clone_dir
217 );
218 subprocerr('git clone') if $?;
219 chdir($clone_dir)
220 or syserr(g_("cannot change directory to '%s'"), $clone_dir);
221 $shallowfile = "$basenamerev.gitshallow";
222 system('cp', '-f', 'shallow', "$old_cwd/$shallowfile");
223 subprocerr("cp shallow $old_cwd/$shallowfile") if $?;
224 }
225  
226 # Create the git bundle.
227 my $bundlefile = "$basenamerev.git";
228 my @bundle_arg = $self->{options}{git_ref} ?
229 (@{$self->{options}{git_ref}}) : '--all';
230 info(g_('bundling: %s'), join(' ', @bundle_arg));
231 system('git', 'bundle', 'create', "$old_cwd/$bundlefile",
232 @bundle_arg,
233 # Ensure HEAD is included no matter what.
234 'HEAD',
235 # Avoids ambiguity error when referring to, for example, a debian
236 # branch.
237 '--',
238 );
239 subprocerr("git bundle create $old_cwd/$bundlefile") if $?;
240  
241 chdir $old_cwd or syserr(g_("cannot change directory to '%s'"), $old_cwd);
242  
243 if (defined $tmpdir) {
244 erasedir($tmpdir);
245 pop_exit_handler();
246 }
247  
248 $self->add_file($bundlefile);
249 if (defined $shallowfile) {
250 $self->add_file($shallowfile);
251 }
252 }
253  
254 sub do_extract {
255 my ($self, $newdirectory) = @_;
256 my $fields = $self->{fields};
257  
258 my $basenamerev = $self->get_basename(1);
259  
260 my @files = $self->get_files();
261 my ($bundle, $shallow);
262 foreach my $file (@files) {
263 if ($file =~ /^\Q$basenamerev\E\.git$/) {
264 if (! defined $bundle) {
265 $bundle = $file;
266 } else {
267 error(g_('format v3.0 (git) uses only one .git file'));
268 }
269 } elsif ($file =~ /^\Q$basenamerev\E\.gitshallow$/) {
270 if (! defined $shallow) {
271 $shallow = $file;
272 } else {
273 error(g_('format v3.0 (git) uses only one .gitshallow file'));
274 }
275 } else {
276 error(g_('format v3.0 (git) unknown file: %s'), $file);
277 }
278 }
279 if (! defined $bundle) {
280 error(g_('format v3.0 (git) expected %s'), "$basenamerev.git");
281 }
282  
283 if ($self->{options}{no_overwrite_dir} and -e $newdirectory) {
284 error(g_('unpack target exists: %s'), $newdirectory);
285 } else {
286 erasedir($newdirectory);
287 }
288  
289 # Extract git bundle.
290 info(g_('cloning %s'), $bundle);
291 my $bundle_path = File::Spec->catfile($self->{basedir}, $bundle);
292 system(
293 'git', 'clone',
294 '--quiet',
295 '--origin=bundle',
296 '--end-of-options',
297 $bundle_path, $newdirectory
298 );
299 subprocerr('git bundle') if $?;
300  
301 if (defined $shallow) {
302 # Move shallow info file into place, so git does not try to follow
303 # parents of shallow refs.
304 info(g_('setting up shallow clone'));
305 my $shallow_orig = File::Spec->catfile($self->{basedir}, $shallow);
306 my $shallow_dest = File::Spec->catfile($newdirectory, '.git', 'shallow');
307 system('cp', '-f', $shallow_orig, $shallow_dest);
308 subprocerr('cp') if $?;
309 }
310  
311 _check_workdir($newdirectory);
312  
313 if (defined $fields->{'Vcs-Git'}) {
314 my $remote = 'origin';
315 my ($url, $head) = _parse_vcs_git($fields->{'Vcs-Git'});
316  
317 my @git_remote_add = (qw(git -C), $newdirectory, qw(remote add));
318 push @git_remote_add, '-m', $head if defined $head;
319  
320 info(g_('setting remote %s to %s'), $remote, $url);
321 system(@git_remote_add, $remote, $url);
322 subprocerr('git remote add') if $?;
323 }
324 }
325  
326 =head1 CHANGES
327  
328 =head2 Version 0.xx
329  
330 This is a private module.
331  
332 =cut
333  
334 1;