summaryrefslogtreecommitdiffstats
path: root/scripts/Dpkg/Source/Package/V3/Git.pm
blob: 721036a6be03097da97559166f3623d8080e55c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#
# git support for dpkg-source
#
# Copyright © 2007,2010 Joey Hess <joeyh@debian.org>.
# Copyright © 2008 Frank Lichtenheld <djpig@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

package Dpkg::Source::Package::V3::Git;

use strict;
use warnings;

our $VERSION = '0.02';

use Cwd qw(abs_path getcwd);
use File::Basename;
use File::Spec;
use File::Temp qw(tempdir);

use Dpkg::Gettext;
use Dpkg::ErrorHandling;
use Dpkg::Exit qw(push_exit_handler pop_exit_handler);
use Dpkg::Path qw(find_command);
use Dpkg::Source::Functions qw(erasedir);

use parent qw(Dpkg::Source::Package);

our $CURRENT_MINOR_VERSION = '0';

# Remove variables from the environment that might cause git to do
# something unexpected.
delete $ENV{GIT_DIR};
delete $ENV{GIT_INDEX_FILE};
delete $ENV{GIT_OBJECT_DIRECTORY};
delete $ENV{GIT_ALTERNATE_OBJECT_DIRECTORIES};
delete $ENV{GIT_WORK_TREE};

sub prerequisites {
    return 1 if find_command('git');
    error(g_('cannot unpack git-format source package because ' .
             'git is not in the PATH'));
}

sub _check_workdir {
    my $srcdir = shift;

    if (! -d "$srcdir/.git") {
        error(g_('source directory is not the top directory of a git ' .
                 'repository (%s/.git not present), but Format git was ' .
                 'specified'), $srcdir);
    }
    if (-s "$srcdir/.gitmodules") {
        error(g_('git repository %s uses submodules; this is not yet supported'),
              $srcdir);
    }

    return 1;
}

sub _parse_vcs_git {
    my $vcs_git = shift;
    my ($url, $opt, $branch) = split ' ', $vcs_git;

    if (defined $opt && $opt eq '-b' && defined $branch) {
        return ($url, $branch);
    } else {
        return ($url);
    }
}

my @module_cmdline = (
    {
        name => '--git-ref=<ref>',
        help => N_('specify a git <ref> to include in the git bundle'),
        when => 'build',
    }, {
        name => '--git-depth=<number>',
        help => N_('create a shallow clone with <number> depth'),
        when => 'build',
    }
);

sub describe_cmdline_options {
    my $self = shift;

    my @cmdline = ( $self->SUPER::describe_cmdline_options(), @module_cmdline );

    return @cmdline;
}

sub parse_cmdline_option {
    my ($self, $opt) = @_;
    return 1 if $self->SUPER::parse_cmdline_option($opt);
    if ($opt =~ /^--git-ref=(.*)$/) {
        push @{$self->{options}{git_ref}}, $1;
        return 1;
    } elsif ($opt =~ /^--git-depth=(\d+)$/) {
        $self->{options}{git_depth} = $1;
        return 1;
    }
    return 0;
}

sub can_build {
    my ($self, $dir) = @_;

    return (0, g_("doesn't contain a git repository")) unless -d "$dir/.git";
    return 1;
}

sub do_build {
    my ($self, $dir) = @_;
    my $diff_ignore_regex = $self->{options}{diff_ignore_regex};

    $dir =~ s{/+$}{}; # Strip trailing /
    my ($dirname, $updir) = fileparse($dir);
    my $basenamerev = $self->get_basename(1);

    _check_workdir($dir);

    my $old_cwd = getcwd();
    chdir $dir or syserr(g_("unable to chdir to '%s'"), $dir);

    # Check for uncommitted files.
    # To support dpkg-source -i, get a list of files
    # equivalent to the ones git status finds, and remove any
    # ignored files from it.
    my @ignores = '--exclude-per-directory=.gitignore';
    my $core_excludesfile = qx(git config --get core.excludesfile);
    chomp $core_excludesfile;
    if (length $core_excludesfile && -e $core_excludesfile) {
        push @ignores, "--exclude-from=$core_excludesfile";
    }
    if (-e '.git/info/exclude') {
        push @ignores, '--exclude-from=.git/info/exclude';
    }
    open(my $git_ls_files_fh, '-|', 'git', 'ls-files', '--modified', '--deleted',
         '-z', '--others', @ignores) or subprocerr('git ls-files');
    my @files;
    {
      local $_;
      local $/ = "\0";
      while (<$git_ls_files_fh>) {
          chomp;
          if (! length $diff_ignore_regex ||
              ! m/$diff_ignore_regex/o) {
              push @files, $_;
          }
      }
    }
    close($git_ls_files_fh) or syserr(g_('git ls-files exited nonzero'));
    if (@files) {
        error(g_('uncommitted, not-ignored changes in working directory: %s'),
              join(' ', @files));
    }

    # If a depth was specified, need to create a shallow clone and
    # bundle that.
    my $tmp;
    my $shallowfile;
    if ($self->{options}{git_depth}) {
        chdir $old_cwd or syserr(g_("unable to chdir to '%s'"), $old_cwd);
        $tmp = tempdir("$dirname.git.XXXXXX", DIR => $updir);
        push_exit_handler(sub { erasedir($tmp) });
        my $clone_dir = "$tmp/repo.git";
        # file:// is needed to avoid local cloning, which does not
        # create a shallow clone.
        info(g_('creating shallow clone with depth %s'),
                $self->{options}{git_depth});
        system('git', 'clone', '--depth=' . $self->{options}{git_depth},
               '--quiet', '--bare', 'file://' . abs_path($dir), $clone_dir);
        subprocerr('git clone') if $?;
        chdir($clone_dir)
            or syserr(g_("unable to chdir to '%s'"), $clone_dir);
        $shallowfile = "$basenamerev.gitshallow";
        system('cp', '-f', 'shallow', "$old_cwd/$shallowfile");
        subprocerr('cp shallow') if $?;
    }

    # Create the git bundle.
    my $bundlefile = "$basenamerev.git";
    my @bundle_arg = $self->{options}{git_ref} ?
        (@{$self->{options}{git_ref}}) : '--all';
    info(g_('bundling: %s'), join(' ', @bundle_arg));
    system('git', 'bundle', 'create', "$old_cwd/$bundlefile",
           @bundle_arg,
           'HEAD', # ensure HEAD is included no matter what
           '--', # avoids ambiguity error when referring to eg, a debian branch
    );
    subprocerr('git bundle') if $?;

    chdir $old_cwd or syserr(g_("unable to chdir to '%s'"), $old_cwd);

    if (defined $tmp) {
        erasedir($tmp);
        pop_exit_handler();
    }

    $self->add_file($bundlefile);
    if (defined $shallowfile) {
        $self->add_file($shallowfile);
    }
}

sub do_extract {
    my ($self, $newdirectory) = @_;
    my $fields = $self->{fields};

    my $basenamerev = $self->get_basename(1);

    my @files = $self->get_files();
    my ($bundle, $shallow);
    foreach my $file (@files) {
        if ($file =~ /^\Q$basenamerev\E\.git$/) {
            if (! defined $bundle) {
                $bundle = $file;
            } else {
                error(g_('format v3.0 (git) uses only one .git file'));
            }
        } elsif ($file =~ /^\Q$basenamerev\E\.gitshallow$/) {
            if (! defined $shallow) {
                $shallow = $file;
            } else {
                error(g_('format v3.0 (git) uses only one .gitshallow file'));
            }
        } else {
            error(g_('format v3.0 (git) unknown file: %s'), $file);
        }
    }
    if (! defined $bundle) {
        error(g_('format v3.0 (git) expected %s'), "$basenamerev.git");
    }

    if ($self->{options}{no_overwrite_dir} and -e $newdirectory) {
        error(g_('unpack target exists: %s'), $newdirectory);
    } else {
        erasedir($newdirectory);
    }

    # Extract git bundle.
    info(g_('cloning %s'), $bundle);
    my $bundle_path = File::Spec->catfile($self->{basedir}, $bundle);
    system('git', 'clone', '--quiet', '--origin=bundle', $bundle_path, $newdirectory);
    subprocerr('git bundle') if $?;

    if (defined $shallow) {
        # Move shallow info file into place, so git does not
        # try to follow parents of shallow refs.
        info(g_('setting up shallow clone'));
        my $shallow_orig = File::Spec->catfile($self->{basedir}, $shallow);
        my $shallow_dest = File::Spec->catfile($newdirectory, '.git', 'shallow');
        system('cp', '-f',  $shallow_orig, $shallow_dest);
        subprocerr('cp') if $?;
    }

    _check_workdir($newdirectory);

    if (defined $fields->{'Vcs-Git'}) {
        my $remote = 'origin';
        my ($url, $head) = _parse_vcs_git($fields->{'Vcs-Git'});

        my @git_remote_add = (qw(git -C), $newdirectory, qw(remote add));
        push @git_remote_add, '-m', $head if defined $head;

        info(g_('setting remote %s to %s'), $remote, $url);
        system(@git_remote_add, $remote, $url);
        subprocerr('git remote add') if $?;
    }
}

1;