-
Notifications
You must be signed in to change notification settings - Fork 1
/
init-repository
executable file
·657 lines (538 loc) · 21.4 KB
/
init-repository
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#!/usr/bin/env perl
#############################################################################
##
## Copyright (C) 2015 The Qt Company Ltd.
## Contact: http://www.qt.io/licensing/
##
## This file is part of the utilities of the Qt Toolkit.
##
## $QT_BEGIN_LICENSE:LGPL21$
## Commercial License Usage
## Licensees holding valid commercial Qt licenses may use this file in
## accordance with the commercial license agreement provided with the
## Software or, alternatively, in accordance with the terms contained in
## a written agreement between you and The Qt Company. For licensing terms
## and conditions see http://www.qt.io/terms-conditions. For further
## information use the contact form at http://www.qt.io/contact-us.
##
## GNU Lesser General Public License Usage
## Alternatively, this file may be used under the terms of the GNU Lesser
## General Public License version 2.1 or version 3 as published by the Free
## Software Foundation and appearing in the file LICENSE.LGPLv21 and
## LICENSE.LGPLv3 included in the packaging of this file. Please review the
## following information to ensure the GNU Lesser General Public License
## requirements will be met: https://www.gnu.org/licenses/lgpl.html and
## http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
##
## As a special exception, The Qt Company gives you certain additional
## rights. These rights are described in The Qt Company LGPL Exception
## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
##
## $QT_END_LICENSE$
##
#############################################################################
use v5.8;
use strict;
use warnings;
package Qt::InitRepository;
sub printUsage($)
{
my ($ex) = @_;
print <<EOF ;
Usage:
./init-repository [options]
This script may be run after an initial `git clone' of Qt5 in order to
check out all submodules. It fetches them from canonical URLs inferred
from the clone's origin.
Options:
Global options:
--force, -f
Force initialization (even if the submodules are already checked
out).
--force-hooks
Force initialization of hooks (even if there are already hooks in
checked out submodules).
--quiet, -q
Be quiet. Will exit cleanly if the repository is already
initialized.
Module options:
--module-subset=<module1>,<module2>...
Only initialize the specified subset of modules given as the
argument. Specified modules must already exist in .gitmodules. The
string "all" results in cloning all known modules. The strings
"essential", "addon", "preview", "deprecated", "obsolete", and
"ignore" refer to classes of modules; "default" maps to
"essential,addon,preview,deprecated", which corresponds with the
set of maintained modules and is also the default set. Module
names may be prefixed with a dash to exclude them from a bigger
set, e.g. "all,-ignore".
--no-update
Skip the `git submodule update' command.
--no-fetch
Skip the `git fetch' commands. Implied by --no-update.
--branch
Instead of checking out specific SHA1s, check out the submodule
branches that correspond with the current supermodule commit. By
default, this option will cause local commits in the submodules to
be rebased. With --no-update, the branches will be checked out, but
their heads will not move.
--ignore-submodules
Set git config to ignore submodules by default when doing operations
on the qt5 repo, such as `pull', `fetch', `diff' etc.
After using this option, pass `--ignore-submodules=none' to git to
override it as needed.
Repository options:
--berlin
Switch to internal URLs and make use of the Berlin git mirrors.
(Implies `--mirror').
--oslo
Switch to internal URLs and make use of the Oslo git mirrors.
(Implies `--mirror').
--codereview-username <Gerrit/JIRA username>
Specify the user name for the (potentially) writable `gerrit' remote
for each module, for use with the Gerrit code review tool.
If this option is omitted, the gerrit remote is created without a
username and port number, and thus relies on a correct SSH
configuration.
--alternates <path to other Qt5 repo>
Adds alternates for each submodule to another full qt5 checkout.
This makes this qt5 checkout very small, as it will use the object
store of the alternates before unique objects are stored in its own
object store.
This option has no effect when using `--no-update'.
NOTE: This will make this repo dependent on the alternate, which is
potentially dangerous! The dependency can be broken by also using
the `--copy-objects' option, or by running "git repack -a" in each
submodule, where required. Please read the note about the `--shared'
option in the documentation of `git clone' for more information.
--copy-objects
When `--alternates' is used, automatically do a "git repack -a" in
each submodule after cloning, to ensure that the repositories are
independent from the source used as a reference for cloning.
Note that this negates the disk usage benefits gained from the use
of `--alternates'.
--mirror <url-base>
Uses <url-base> as the base URL for submodule git mirrors.
For example:
--mirror user\@machine:/foo/bar/qt/
...will use the following as a mirror for qtbase:
user\@machine:/foo/bar/qt/qtbase.git
The mirror is permitted to contain a subset of the submodules; any
missing modules will fall back to the canonical URLs.
EOF
exit($ex);
}
use Carp qw( confess );
use Cwd qw( getcwd abs_path );
use English qw( -no_match_vars );
use File::Spec::Functions qw ( rel2abs );
use Getopt::Long qw( GetOptions );
my $script_path = abs_path($0);
$script_path =~ s,[/\\][^/\\]+$,,;
my $GERRIT_SSH_BASE
= 'ssh://@[email protected]@PORT@/qt/';
my $BER_MIRROR_URL_BASE
= 'git://hegel/qt/';
my $OSLO_MIRROR_URL_BASE
= 'git://qilin/qt/';
sub new
{
my ($class, @arguments) = @_;
my $self = {};
bless $self, $class;
$self->parse_arguments(@arguments);
return $self;
}
# Like `system', but possibly log the command, and die on non-zero exit code
sub exe
{
my ($self, @cmd) = @_;
if (!$self->{quiet}) {
print "+ @cmd\n";
}
if (system(@cmd) != 0) {
confess "@cmd exited with status $CHILD_ERROR";
}
return;
}
sub parse_arguments
{
my ($self) = @_;
%{$self} = (%{$self},
'alternates' => "",
'branch' => 0,
'codereview-username' => "",
'detach-alternates' => 0 ,
'force' => 0 ,
'force-hooks' => 0 ,
'ignore-submodules' => 0 ,
'mirror-url' => "",
'update' => 1 ,
'fetch' => 1 ,
'module-subset' => "default",
);
GetOptions(
'alternates=s' => \$self->{qw{ alternates }},
'branch' => \$self->{qw{ branch }},
'codereview-username=s' => \$self->{qw{ codereview-username }},
'copy-objects' => \$self->{qw{ detach-alternates }},
'force|f' => \$self->{qw{ force }},
'force-hooks' => \$self->{qw{ force-hooks }},
'ignore-submodules' => \$self->{qw{ ignore-submodules }},
'mirror=s' => \$self->{qw{ mirror-url }},
'quiet' => \$self->{qw{ quiet }},
'update!' => \$self->{qw{ update }},
'fetch!' => \$self->{qw{ fetch }},
'module-subset=s' => \$self->{qw{ module-subset }},
'help|?' => sub { printUsage(1); },
'berlin' => sub {
$self->{'mirror-url'} = $BER_MIRROR_URL_BASE;
},
'oslo' => sub {
$self->{'mirror-url'} = $OSLO_MIRROR_URL_BASE;
},
) || printUsage(2);
@ARGV && printUsage(2);
# Replace any double trailing slashes from end of mirror
$self->{'mirror-url'} =~ s{//+$}{/};
$self->{'module-subset'} =~ s/\bdefault\b/preview,essential,addon,deprecated/;
$self->{'module-subset'} = [ split(/,/, $self->{'module-subset'}) ];
$self->{'fetch'} = 0 if (!$self->{'update'});
return;
}
sub check_if_already_initialized
{
my ($self) = @_;
# We consider the repo as `initialized' if submodule.qtbase.url is set
if (qx(git config --get submodule.qtbase.url)) {
if (!$self->{force}) {
exit 0 if ($self->{quiet});
print "Will not reinitialize already initialized repository (use -f to force)!\n";
exit 1;
}
}
return;
}
sub git_submodule_init
{
my ($self, @init_args) = @_;
if ($self->{quiet}) {
unshift @init_args, '--quiet';
}
$self->exe('git', 'submodule', 'init', @init_args);
my $template = getcwd()."/.commit-template";
if (-e $template) {
$self->exe('git', 'config', 'commit.template', $template);
}
return;
}
use constant {
STS_PREVIEW => 1,
STS_ESSENTIAL => 2,
STS_ADDON => 3,
STS_DEPRECATED => 4,
STS_OBSOLETE => 5
};
sub git_clone_all_submodules
{
my ($self, $my_repo_base, $co_branch, $alternates, @subset) = @_;
my %subdirs = ();
my %subbranches = ();
my %subbases = ();
my %subinits = ();
my @submodconfig = qx(git config -l -f .gitmodules);
foreach my $line (@submodconfig) {
# Example line: submodule.qtqa.url=../qtqa.git
next if ($line !~ /^submodule\.([^.=]+)\.([^.=]+)=(.*)$/);
if ($2 eq "path") {
$subdirs{$1} = $3;
} elsif ($2 eq "branch") {
$subbranches{$1} = $3;
} elsif ($2 eq "url") {
my ($mod, $base) = ($1, $3);
next if ($base !~ /^\.\.\//);
$base = $my_repo_base.'/'.$base;
while ($base =~ s,(?!\.\./)[^/]+/\.\./,,g) {}
$subbases{$mod} = $base;
} elsif ($2 eq "update") {
push @subset, '-'.$1 if ($3 eq 'none');
} elsif ($2 eq "status") {
if ($3 eq "preview") {
$subinits{$1} = STS_PREVIEW;
} elsif ($3 eq "essential") {
$subinits{$1} = STS_ESSENTIAL;
} elsif ($3 eq "addon") {
$subinits{$1} = STS_ADDON;
} elsif ($3 eq "deprecated") {
$subinits{$1} = STS_DEPRECATED;
} elsif ($3 eq "obsolete") {
$subinits{$1} = STS_OBSOLETE;
} elsif ($3 eq "ignore") {
delete $subinits{$1};
} else {
die("Invalid subrepo status '$3' for '$1'.\n");
}
}
}
my %include = ();
foreach my $mod (@subset) {
my $del = ($mod =~ s/^-//);
my $fail = 0;
my @what;
if ($mod eq "all") {
@what = keys %subbases;
} elsif ($mod eq "essential") {
@what = grep { ($subinits{$_} || 0) eq STS_ESSENTIAL } keys %subbases;
} elsif ($mod eq "addon") {
@what = grep { ($subinits{$_} || 0) eq STS_ADDON } keys %subbases;
} elsif ($mod eq "preview") {
@what = grep { ($subinits{$_} || 0) eq STS_PREVIEW } keys %subbases;
} elsif ($mod eq "deprecated") {
@what = grep { ($subinits{$_} || 0) eq STS_DEPRECATED } keys %subbases;
} elsif ($mod eq "obsolete") {
@what = grep { ($subinits{$_} || 0) eq STS_OBSOLETE } keys %subbases;
} elsif ($mod eq "ignore") {
@what = grep { ($subinits{$_} || 0) eq 0 } keys %subbases;
} elsif (defined($subdirs{$mod})) {
push @what, $mod;
} else {
$fail = 1;
}
if ($del) {
print "Warning: excluding non-existent module '$mod'.\n"
if ($fail);
map { delete $include{$_} } @what;
} else {
die("Error: module subset names non-existent '$mod'.\n")
if ($fail);
map { $include{$_} = 1; } @what;
}
}
my @modules = sort keys %include;
$self->git_submodule_init(map { $subdirs{$_} } @modules);
# manually clone each repo here, so we can easily use reference repos, mirrors etc
my @configresult = qx(git config -l);
foreach my $line (@configresult) {
# Example line: submodule.qtqa.url=git://code.qt.io/qt/qtqa.git
next if ($line !~ /submodule\.([^.=]+)\.url=/);
my $module = $1;
if (!defined($include{$module})) {
$self->exe('git', 'config', '--remove-section', "submodule.$module");
next;
}
if ($self->{'ignore-submodules'}) {
$self->exe('git', 'config', "submodule.$module.ignore", 'all');
}
}
my $any_bad = 0;
foreach my $module (@modules) {
$any_bad = 1
if ($self->git_stat_one_submodule($subdirs{$module}));
}
die("Dirty submodule(s) present; cannot proceed.\n")
if ($any_bad);
foreach my $module (@modules) {
$self->git_clone_one_submodule($subdirs{$module}, $subbases{$module},
$co_branch && $subbranches{$module}, $alternates);
}
if ($co_branch) {
foreach my $module (@modules) {
my $branch = $subbranches{$module};
die("No branch defined for submodule $module.\n") if (!defined($branch));
my $orig_cwd = getcwd();
my $module_dir = $subdirs{$module};
chdir($module_dir) or confess "chdir $module_dir: $OS_ERROR";
my $br = qx(git rev-parse -q --verify $branch);
if (!$br) {
$self->exe('git', 'checkout', '-b', $branch, "origin/$branch");
} else {
$self->exe('git', 'checkout', $branch);
}
chdir("$orig_cwd") or confess "chdir $orig_cwd: $OS_ERROR";
}
}
if ($self->{update}) {
my @cmd = ('git', 'submodule', 'update', '--force', '--no-fetch');
push @cmd, '--remote', '--rebase' if ($co_branch);
$self->exe(@cmd);
foreach my $module (@modules) {
if (-f $module.'/.gitmodules') {
my $orig_cwd = getcwd();
chdir($module) or confess "chdir $module: $OS_ERROR";
$self->git_clone_all_submodules($subbases{$module}, 0, "$alternates/$module", "all");
chdir("$orig_cwd") or confess "chdir $orig_cwd: $OS_ERROR";
}
}
}
return;
}
sub git_add_remotes
{
my ($self, $gerrit_repo_basename) = @_;
my $gerrit_repo_url = $GERRIT_SSH_BASE;
# If given a username, make a "verbose" remote.
# Otherwise, rely on proper SSH configuration.
if ($self->{'codereview-username'}) {
$gerrit_repo_url =~ s,\@USER\@,$self->{'codereview-username'}\@,;
$gerrit_repo_url =~ s,\@PORT\@,:29418,;
} else {
$gerrit_repo_url =~ s,\@[^\@]+\@,,g;
}
$gerrit_repo_url .= $gerrit_repo_basename;
$self->exe('git', 'config', 'remote.gerrit.url', $gerrit_repo_url);
$self->exe('git', 'config', 'remote.gerrit.fetch', '+refs/heads/*:refs/remotes/gerrit/*', '/heads/');
}
sub git_stat_one_submodule
{
my ($self, $submodule) = @_;
return 0 if (! -e "$submodule/.git");
my $orig_cwd = getcwd();
chdir($submodule) or confess "chdir $submodule: $OS_ERROR";
my @sts = qx(git status --porcelain --untracked=no --ignore-submodules=all);
# After a git clone --no-checkout, git status reports all files as
# staged for deletion, but we still want to update the submodule.
# It's unlikely that a genuinely dirty index would have _only_ this
# type of modifications, and it doesn't seem like a horribly big deal
# to lose them anyway, so ignore them.
@sts = grep(!/^D /, @sts);
chdir($orig_cwd) or confess "cd $orig_cwd: $OS_ERROR";
return 0 if (!@sts);
print STDERR "$submodule is dirty.\n";
return -1;
}
sub git_clone_one_submodule
{
my ($self, $submodule, $repo_basename, $branch, $alternates) = @_;
my $mirror_url = $self->{ 'mirror-url' };
my $protocol = $self->{ 'protocol' };
# `--reference FOO' args for the clone, if any.
my @reference_args;
if ($alternates) {
# alternates is a qt5 repo, so the submodule will be under that.
if (-e "$alternates/$submodule/.git") {
@reference_args = ('--reference', "$alternates/$submodule");
}
else {
print " *** $alternates/$submodule not found, ignoring alternate for this submodule\n";
}
}
my $do_clone = (! -e "$submodule/.git");
my $url = $self->{'base-url'}.$repo_basename;
my $mirror;
if ($mirror_url && ($do_clone || $self->{fetch})) {
$mirror = $mirror_url.$repo_basename;
}
if ($mirror) {
# Only use the mirror if it can be reached.
eval { $self->exe('git', 'ls-remote', $mirror, 'test/if/mirror/exists') };
if ($@) {
warn "mirror [$mirror] is not accessible; $url will be used\n";
undef $mirror;
}
}
if ($do_clone) {
if ($branch) {
push @reference_args, '--branch', $branch;
} else {
push @reference_args, '--no-checkout';
}
$self->exe('git', 'clone', @reference_args,
($mirror ? $mirror : $url), $submodule);
}
my $orig_cwd = getcwd();
chdir($submodule) or confess "chdir $submodule: $OS_ERROR";
if ($mirror) {
# This is only for the user's convenience - we make no use of it.
$self->exe('git', 'config', 'remote.mirror.url', $mirror);
$self->exe('git', 'config', 'remote.mirror.fetch', '+refs/heads/*:refs/remotes/mirror/*');
}
if (!$do_clone && $self->{fetch}) {
# If we didn't clone, fetch from the right location. We always update
# the origin remote, so that submodule update --remote works.
$self->exe('git', 'config', 'remote.origin.url', ($mirror ? $mirror : $url));
$self->exe('git', 'fetch', 'origin');
}
if (!($do_clone || $self->{fetch}) || $mirror) {
# Leave the origin configured to the canonical URL. It's already correct
# if we cloned/fetched without a mirror; otherwise it may be anything.
$self->exe('git', 'config', 'remote.origin.url', $url);
}
my $template = $orig_cwd."/.commit-template";
if (-e $template) {
$self->exe('git', 'config', 'commit.template', $template);
}
$self->git_add_remotes($repo_basename);
if ($self->{'detach-alternates'}) {
$self->exe('git', 'repack', '-a');
my $alternates_path = '.git/objects/info/alternates';
if (-e $alternates_path) {
unlink($alternates_path) || confess "unlink $alternates_path: $OS_ERROR";
}
}
chdir($orig_cwd) or confess "cd $orig_cwd: $OS_ERROR";
return;
}
sub ensure_link
{
my ($self, $src, $tgt) = @_;
return if (!$self->{'force-hooks'} and -f $tgt);
unlink($tgt); # In case we have a dead symlink or pre-existing hook
print "Aliasing $src\n as $tgt ...\n" if (!$self->{quiet});
if ($^O ne "msys" && $^O ne "MSWin32") {
return if eval { symlink($src, $tgt) };
}
# Windows doesn't do (proper) symlinks. As the post_commit script needs
# them to locate itself, we write a forwarding script instead.
open SCRIPT, ">".$tgt or die "Cannot create forwarding script $tgt: $!\n";
# Make the path palatable for MSYS.
$src =~ s,\\,/,g;
$src =~ s,^(.):/,/$1/,g;
print SCRIPT "#!/bin/sh\nexec $src \"\$\@\"\n";
close SCRIPT;
}
sub git_install_hooks
{
my ($self) = @_;
my $hooks = $script_path.'/qtrepotools/git-hooks';
return if (!-d $hooks);
my @configresult = qx(git config --list --local);
foreach my $line (@configresult) {
next if ($line !~ /submodule\.([^.=]+)\.url=/);
my $module = $1;
my $module_gitdir = $module.'/.git';
if (!-d $module_gitdir) {
open GITD, $module_gitdir or die "Cannot open $module: $!\n";
my $gd = <GITD>;
close GITD;
chomp($gd);
$gd =~ s/^gitdir: // or die "Malformed .git file $module_gitdir\n";
$module_gitdir = rel2abs($gd, $module);
if (open COMD, $module_gitdir.'/commondir') {
my $cd = <COMD>;
chomp($cd);
$module_gitdir .= '/'.$cd;
$module_gitdir = abs_path($module_gitdir);
close COMD;
}
}
$self->ensure_link($hooks.'/gerrit_commit_msg_hook', $module_gitdir.'/hooks/commit-msg');
$self->ensure_link($hooks.'/git_post_commit_hook', $module_gitdir.'/hooks/post-commit');
}
}
sub run
{
my ($self) = @_;
$self->check_if_already_initialized;
chomp(my $url = `git config remote.origin.url`);
die("Have no origin remote.\n") if (!$url);
$url =~ s,\.git$,,;
$url =~ s/qt5$//;
$self->{'base-url'} = $url;
$self->git_clone_all_submodules('qt5', $self->{branch}, $self->{alternates}, @{$self->{'module-subset'}});
$self->git_add_remotes('qt5');
$self->git_install_hooks;
return;
}
#==============================================================================
Qt::InitRepository->new()->run if (!caller);
1;