-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-mclone
More file actions
executable file
·327 lines (270 loc) · 8.07 KB
/
Copy pathgit-mclone
File metadata and controls
executable file
·327 lines (270 loc) · 8.07 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env perl
# git-mclone --- wrapper for git clone
# Author: Noah Friedman <friedman@splode.com>
# Created: 2017-05-19
# Public domain
# Commentary:
# Code:
use strict;
use warnings qw(all);
use POSIX qw(:sys_wait_h strftime);
use Fcntl qw(:mode);
use File::Find;
use Getopt::Long;
#use Pod::Usage;
(my $progname = $0) =~ s=^.*/==;
my %opt = ( mode => 'recursive',
passthru => undef,
template => undef,
verbose => 0,
);
my @git_cmd = ($ENV{GITPROG} // "git");
my $git_lfs = undef;
# `our' declares global so they can be dynamically scoped.
our $git_dir = undef;
our $ignore_exit = 0;
my $final_exitcode = 0;
sub getprog
{
my @path = map { if ($_ eq '') { '.' }
else { $_ }
} split( /:/, $ENV{PATH} );
for my $prog (@_)
{
map { return $prog if -f "$_/$prog" && -x _ } @path;
}
return;
}
sub mktmpdir
{
my $topdir = $ENV{XDG_RUNTIME_DIR} || $ENV{TMPDIR} || '/tmp';
my $tmpdir = "$topdir/emptydir.$$";
my $saved_umask = umask( 077 );
mkdir( $tmpdir ) or die "mkdir: $tmpdir: $!\n";
umask( $saved_umask );
return $tmpdir;
}
sub cmd
{
if ($opt{verbose})
{
my @xq = xquote( @_ );
print STDERR "+ @xq\n";
}
system( { $_[0] } @_ );
return cmd_check_exit( $_[0] ) unless $ignore_exit;
return $? == 0;
}
# Like `` but avoids shell exec/reparsing.
sub cmd_out
{
if ($opt{verbose} > 1)
{
my @xq = xquote( @_ );
print STDERR "+ @xq\n";
}
open( my $fh, "-|", @_ ) or die "exec: $_[0]: $!\n";
local $/ = wantarray ? $/ : undef;
my @data = map { s/\n+$//s; $_ } <$fh>;
close( $fh ); # child will exit; perl calls waitpid implicitly
cmd_check_exit( $_[0] ) unless $ignore_exit && !@data;
return wantarray? @data : $data[0];
}
# POSIX :sys_wait_h does not provide WCOREDUMP.
# Linux and FreeBSD both use WCOREFLAG=128
sub WCOREDUMP { $_[0] & 0x80 }
sub cmd_check_exit
{
return 1 unless $?; # 1=success (boolean true)
$final_exitcode = 1;
my $msg;
if ($? == -1)
{ $msg = "$!" }
elsif (WIFSIGNALED( $? ))
{
my $core = WCOREDUMP( $? ) ? ' (core dumped)' : '';
$msg = sprintf( "signal %d%s", WTERMSIG( $? ), $core );
}
else
{ $msg = sprintf( "exit %d", WEXITSTATUS( $? )) }
print STDERR sprintf( "%s: warning: %s: %s\n", $progname, $_[0], $msg );
return 0; # 0=failure (boolean false)
}
sub xquote
{
my @xq = @_; # don't modify original
map { s/'/'\\''/sg;
s/^(.*)$/'$1'/s if /[][{}\(\)<>\'\"\!\?\|\$&\*;\s\\]/s;
$_ } @xq;
}
sub git
{
my @C = ('-C', $git_dir) if defined $git_dir;
return cmd( @git_cmd, @C, @_ );
}
sub git_out
{
my @C = ('-C', $git_dir) if defined $git_dir;
return cmd_out( @git_cmd, @C, @_ );
}
sub git_config_get
{
my $name = shift @_;
return git_out( 'config', @_, '--get', $name );
}
sub get_config_template_dir
{
return $ENV{GIT_TEMPLATE_DIR} if defined $ENV{GIT_TEMPLATE_DIR};
my $tdir = git_config_get( 'init.templateDir' );
return $tdir if $tdir;
return;
}
sub git_clone
{
my @clone_cmd = ('clone', '--'.$opt{mode} );
push( @clone_cmd, @{$opt{passthru}} ) if $opt{passthru};
push( @clone_cmd, '--template', $opt{template}) if $opt{template};
return git( @clone_cmd, @_ );
}
sub ST_MODE { 2 } # file type and perms
sub ST_ATIME { 8 } # last access time in seconds since the epoch
sub ST_MTIME { 9 } # last modify time in seconds since the epoch
sub touch
{
my ($file, $new_mtime, $st) = @_;
my $mode = $st->[ST_MODE];
my $old_atime = $st->[ST_ATIME];
my $old_mtime = $st->[ST_MTIME];
my $info;
if ($opt{verbose} > 1)
{
my $strfmt = '%Y-%m-%d %H:%M:%S%z';
my $ostamp = strftime( $strfmt, localtime( $old_mtime ));
my $nstamp;
if (S_ISLNK( $mode ))
{ $nstamp = '(ignoring symlink)' }
elsif ($new_mtime > $old_mtime)
{ $nstamp = '(no change)' }
else
{ $nstamp = strftime( $strfmt, localtime( $new_mtime )) }
printf( "%-42s %s\n", $ostamp.' => '.$nstamp, $file );
}
return if S_ISLNK( $mode );
return if $new_mtime > $old_mtime;
utime( $old_atime, $new_mtime, $file );
}
sub fixup_repo
{
my $repo_dir = shift;
local $git_dir = $repo_dir;
if (-e "$repo_dir/.git")
{
my @verbose = ('--verbose') x $opt{verbose};
return git( 'restore-commit-mtime', @verbose );
}
else # bare repo
{
# Git 2.49 added the strange behavior of force-disabling --tags
# with --mirror (but not with --bare), and adding a local
# config option disabling it when fetching as well.
# Commit 0dab2468ee5bbfaa log provides a rational for this,
# but the --mirror case doesn't make sense to me.
if ($opt{mode} eq 'mirror')
{
local $ignore_exit = 1;
my $val = git_config_get( 'remote.origin.tagOpt', '--local' );
git( qw(config unset --local remote.origin.tagOpt) )
if $? == 0 && !$val;
}
$git_lfs //= getprog( 'git-lfs' );
if ($git_lfs)
{
git( qw(lfs fetch --all) );
# don't check for errors from rmdir
rmdir( "$repo_dir/lfs/tmp" ) && rmdir( "$repo_dir/lfs" );
}
# n.b. could just use restore-commit-mtime here too,
# but bare repos shouldn't have many files anyway.
my $stamp = git_out( qw(log -n1 --all --format=tformat:%ct) );
my $pstamp = strftime( '%Y-%m-%d %H:%M:%S%Z', localtime( $stamp ));
local $File::Find::dont_use_nlink = 0;
File::Find::find
( { wanted => sub { my @st = lstat _;
touch( $File::Find::name, $stamp, \@st );
},
bydepth => 1,
no_chdir => 1, },
$repo_dir );
}
}
sub parse_options
{
my $help = -1;
local *ARGV = $_[0]; # modify our local arglist, not real ARGV.
# If there is no `--' to signal the end of options, insert one.
# This is to separate passthrough options from URLs and directories.
unless (grep( /^--$/, @ARGV))
{
for (my $i = @ARGV; $i > 0; $i--)
{
next unless $ARGV[$i-1] =~ /^-/;
splice( @ARGV, $i, 0, '--' );
last;
}
# This can happen if there were no options supplied
unshift( @ARGV, '--' ) unless grep( /^--$/, @ARGV );
}
# Important: stringify "$_[0]", because in Getopt::Long 2.37 it's a
# callback object rather than just the option name. The string
# representation is just the name for (semi-)backward compatibility.
# But $_[0]->{name} also works.
my $modefn = sub { $opt{mode} = "$_[0]" },
my $parser = Getopt::Long::Parser->new;
$parser->configure( qw(pass_through no_ignore_case no_auto_abbrev bundling) );
my $succ = $parser->getoptions
( 'h|?|help+' => \$help,
'v|verbose+' => \$opt{verbose},
'template=s' => \$opt{template},
'bare' => $modefn,
'mirror' => $modefn,
'shared' => $modefn,
'no-checkout' => $modefn,
'<>' => sub { push @{$opt{passthru}}, "$_[0]" },
);
#pod2usage (-exitstatus => 1, -verbose => 0) unless $succ;
#pod2usage (-exitstatus => 0, -verbose => $help) if $help >= 0;
# The option terminator `--' is normally swallowed by getopt,
# but not when using passthrough.
shift @ARGV if @ARGV && $ARGV[0] eq '--';
$opt{template} = $opt{tmpdir} = mktmpdir()
unless ($opt{template} || get_config_template_dir());
return $succ;
}
sub main
{
parse_options( \@_ );
# Special two-arg case: url destdir-not-url
if (@_ == 2 && $_[1] !~ m|^[^:/]+://|)
{
git_clone( '--', @_ ) and fixup_repo( $_[1] );
}
else
{
for my $url (@_)
{
print "** $url\n" unless $opt{verbose} || @_ == 1;
(my $localdir = $url) =~ s=/+$==;
$localdir =~ s=^.*/==;
$localdir .= '.git' unless $localdir =~ /\.git$/;
git_clone( '--', $url, $localdir ) and fixup_repo( $localdir );
print "\n" unless @_ == 1;
}
}
return $final_exitcode;
}
END
{
rmdir( $opt{tmpdir} ) if $opt{tmpdir};
}
exit( main( @ARGV ));
# eof