summaryrefslogtreecommitdiffstats
path: root/init-repository
blob: 5a2bd2ce66c7b0410ea6ec9884a102c792f07956 (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
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
#!/usr/bin/env perl
#############################################################################
##
## Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
## Contact: http://www.qt-project.org/legal
##
## This file is part of the utilities of the Qt Toolkit.
##
## $QT_BEGIN_LICENSE:LGPL$
## 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 Digia.  For licensing terms and
## conditions see http://qt.digia.com/licensing.  For further information
## use the contact form at http://qt.digia.com/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 as published by the Free Software
## Foundation and appearing in the file LICENSE.LGPL included in the
## packaging of this file.  Please review the following information to
## ensure the GNU Lesser General Public License version 2.1 requirements
## will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
##
## In addition, as a special exception, Digia gives you certain additional
## rights.  These rights are described in the Digia Qt LGPL Exception
## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
##
## GNU General Public License Usage
## Alternatively, this file may be used under the terms of the GNU
## General Public License version 3.0 as published by the Free Software
## Foundation and appearing in the file LICENSE.GPL included in the
## packaging of this file.  Please review the following information to
## ensure the GNU General Public License version 3.0 requirements will be
## met: http://www.gnu.org/copyleft/gpl.html.
##
##
## $QT_END_LICENSE$
##
#############################################################################

use v5.8;
use strict;
use warnings;

package Qt::InitRepository;


=head1 NAME

init-repository - initialize the Qt5 repository and all submodules

=head1 SYNOPSIS

  ./init-repository [options]

This script may be run after an initial `git clone' of Qt5 in order to check
out all submodules.


=head1 OPTIONS

B<Global options:>

=over

=item --force, -f

Force initialization (even if the submodules are already checked out).


=item --quiet, -q

Be quiet.  Will exit cleanly if the repository is already initialized.

=back


B<Module options:>

=over

=item --no-webkit

Skip webkit and webkit examples submodules.
It may be desirable to skip these modules due to the large size of the webkit
git repository.

=item --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 default is the set of
maintained modules.

=item --no-update

Skip the `git submodule update' command.


=item --ignore-submodules

Set git config to ignore submodules by default when doing operations on the
qt5 repo, such as `pull', `fetch', `diff' etc.

This option is default for --nokia-developer.

After using this option, pass `--ignore-submodules=none' to git to override
it as needed.

=back


B<Repository options:>

=over

=item --nokia-developer

Switch to internal Nokia URLs.


=item --berlin

Switch to internal Nokia URLs and make use of the Berlin git mirrors.
(Implies `--mirror').


=item --ssh

Use the SSH protocol for git operations.  This may be useful if the git
protocol is blocked by a firewall. Note that this requires a user account
with an uploaded SSH key on all servers used.  (Implies `--nokia-developer').

The `--ssh' option does not affect the gerrit remotes.


=item --http

Use the HTTP protocol for git operations.  This may be useful if the git
protocol is blocked by a firewall.  Note that this only works with the
external Gitorious server.

The `--http' option does not affect the gerrit remotes.


=item --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.


=item --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'.

B<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 C<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.


=item --copy-objects

When `--alternates' is used, automatically do a C<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'.


=item --mirror <url-base>

Uses <url-base> as the base URL for submodule git mirrors.

For example:

  --mirror user@machine:/foo/bar

...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.

=back

=cut

use Carp         qw( confess             );
use English      qw( -no_match_vars      );
use Getopt::Long qw( GetOptionsFromArray );
use Pod::Usage   qw( pod2usage           );
use Cwd          qw( getcwd              );

my %PROTOCOLS = (
    'internal'  => 'git://scm.dev.nokia.troll.no/' ,
    'ssh'       => 'git@scm.dev.nokia.troll.no:'   ,
    'http'      => 'http://git.gitorious.org/'     ,
);

my %GERRIT_REPOS = map { $_ => "qt/$_" } qw(
    qt3d
    qt5
    qlalr
    qtactiveqt
    qtbase
    qtconnectivity
    qtdeclarative
    qtdoc
    qtdocgallery
    qtfeedback
    qtgraphicaleffects
    qtimageformats
    qtjsondb
    qtjsbackend
    qtlocation
    qtmultimedia
    qtpim
    qtqa
    qtquick1
    qtquickcontrols
    qtrepotools
    qtscript
    qtsensors
    qtserialport
    qtsvg
    qtsystems
    qttools
    qttranslations
    qtwayland
    qtwebkit
    qtwebkit-examples-and-demos
    qtx11extras
    qtxmlpatterns
);

my @DEFAULT_REPOS = qw(
    qtactiveqt
    qtbase
    qtdeclarative
    qtdoc
    qtgraphicaleffects
    qtimageformats
    qtjsbackend
    qtmultimedia
    qtqa
    qtquick1
    qtquickcontrols
    qtrepotools
    qtscript
    qtsensors
    qtserialport
    qtsvg
    qttools
    qttranslations
    qtwebkit
    qtwebkit-examples-and-demos
    qtx11extras
    qtxmlpatterns
);

my $GERRIT_SSH_BASE
    = 'ssh://@USER@codereview.qt-project.org@PORT@/';

my $BER_MIRROR_URL_BASE
    = 'git://ber-git.europe.nokia.com/';

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, @args) = @_;

    %{$self} = (%{$self},
        'alternates'          => "",
        'codereview-username' => "",
        'detach-alternates'   => 0 ,
        'force'               => 0 ,
        'ignore-submodules'   => 0 ,
        'mirror-url'          => "",
        'nokia-developer'     => 0 ,
        'protocol'            => "",
        'update'              => 1 ,
        'webkit'              => 1 ,
        'module-subset'       => join(",", @DEFAULT_REPOS),
    );

    GetOptionsFromArray(\@args,
        'alternates=s'      =>  \$self->{qw{ alternates        }},
        'codereview-username=s' => \$self->{qw{ codereview-username }},
        'copy-objects'      =>  \$self->{qw{ detach-alternates }},
        'force'             =>  \$self->{qw{ force             }},
        'ignore-submodules' =>  \$self->{qw{ ignore-submodules }},
        'mirror=s'          =>  \$self->{qw{ mirror-url        }},
        'nokia-developer'   =>  \$self->{qw{ nokia-developer   }},
        'quiet'             =>  \$self->{qw{ quiet             }},
        'update!'           =>  \$self->{qw{ update            }},
        'webkit!'           =>  \$self->{qw{ webkit            }},
        'module-subset=s'   =>  \$self->{qw{ module-subset     }},

        'help|?'            =>  sub { pod2usage(1);               },
        'http'              =>  sub { $self->{protocol} = 'http'; },
        'ssh|ssh-protocol'  =>  sub { $self->{protocol} = 'ssh';  },

        'berlin|berlin-nokia-developer' => sub {
            $self->{'nokia-developer'}   = 1;
            $self->{'protocol'}          = 'internal';
            $self->{'mirror-url'}        = $BER_MIRROR_URL_BASE;
        },

        'nokia-developer' => sub {
            $self->{'nokia-developer'}   = 1;
            $self->{'protocol'}          = 'internal';
            $self->{'ignore-submodules'} = 1;
        },
    ) || pod2usage(2);

    if ($self->{'nokia-developer'} && $self->{'protocol'} eq 'http') {
        print "*** Ignoring use of HTTP protocol, as it's only usable with external server\n";
        $self->{'protocol'} = '';
    }

    # Replace any double trailing slashes from end of mirror
    $self->{'mirror-url'} =~ s{//+$}{/};

    if ($self->{'module-subset'} eq "all") {
        $self->{'module-subset'} = "";
    } else {
        $self->{'module-subset'} = {
            map { $_ => 1 } split(qr{,}, $self->{'module-subset'})
        };
    }

    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}) {
            my @configresult = qx(git config -l);
            foreach (@configresult) {
                # Example line: submodule.qtqa.url=git://gitorious.org/qt/qtqa.git
                if (/(submodule\.[^.=]+)\.url=.*/) {
                    $self->exe('git', 'config', '--remove-section', $1);
                }
            }
        }
        else {
            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) = @_;

    my @init_args;
    if ($self->{quiet}) {
        push @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;
}

sub git_disable_webkit_submodule
{
    my ($self) = @_;

    $self->exe('git', 'config', '--remove', 'submodule.qtwebkit');
    $self->exe('git', 'config', '--remove', 'submodule.qtwebkit-examples-and-demos');

    return;
}

sub git_prune_submodules
{
    my ($self) = @_;

    my @configresult = qx(git config -l);
    foreach my $line (@configresult) {
        if ($line =~ /submodule\.([^.=]+)\.url=/) {
            my $module_name = $1;
            if (!$self->{'module-subset'}{$module_name}) {
                $self->exe('git', 'config', '--remove', "submodule.$module_name");
            }
        }
    }
}

sub git_set_submodule_config
{
    my ($self) = @_;

    my @configresult          = qx(git config -l);
    my $protocol              = $self->{protocol};
    my $url_base_for_protocol = $PROTOCOLS{$protocol};

    foreach my $line (@configresult) {
        # Example line: submodule.qtqa.url=git://gitorious.org/qt/qtqa.git
        next if ($line !~ /submodule\.([^.=]+)\.url=(.*)/);

        my $key   = $1;
        my $value = $2;

        if ($protocol) {
                # qt-labs projects are still hosted under qt internally.
                if ($protocol ne 'http') {
                    $value =~ s,^git://gitorious\.org/qt-labs/,${url_base_for_protocol}qt/,;
                }

                # assume all other projects hosted under gitorious publicly.
                $value =~ s,^git://gitorious\.org/,$url_base_for_protocol,;
        }

        $self->exe('git', 'config', "submodule.$key.url", $value);

        if ($self->{'ignore-submodules'}) {
            $self->exe('git', 'config', "submodule.$key.ignore", 'all');
        }
    }

    return;
}

sub git_clone_all_submodules
{
    my ($self) = @_;

    # manually clone each repo here, so we can easily use reference repos, mirrors etc
    my @configresult = qx(git config -l);
    foreach my $line (@configresult) {
        if ($line =~ /submodule\.([^.=]+)\.url=(.*)/) {
            $self->git_clone_one_submodule($1, $2);
        }
    }

    if ($self->{update}) {
        $self->exe('git', 'submodule', 'update', '--recursive');
    }

    return;
}

sub git_add_remotes
{
    my ($self, $repo_basename) = @_;

    my $gerrit_repo_basename = $GERRIT_REPOS{$repo_basename};
    if ($gerrit_repo_basename) {
        my $gerrit_repo_url;

        # If given a username, make a "verbose" remote.
        # Otherwise, rely on proper SSH configuration.
        if ($self->{'codereview-username'}) {
            $gerrit_repo_url = $GERRIT_SSH_BASE;
            $gerrit_repo_url =~ s,\@USER\@,$self->{'codereview-username'}\@,;
            $gerrit_repo_url =~ s,\@PORT\@,:29418,;
        }
        else {
            $gerrit_repo_url = $GERRIT_SSH_BASE;
            $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/');
    }

    return;
}

sub git_clone_one_submodule
{
    my ($self, $submodule, $url) = @_;

    my $alternates            = $self->{ '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 (-d "$alternates/$submodule") {
            @reference_args = ('--reference', "$alternates/$submodule");
        }
        else {
            print " *** $alternates/$submodule not found, ignoring alternate for this submodule\n";
        }
    }

    my $mirror;
    if ($mirror_url) {
        $mirror = $mirror_url."qt/$submodule";
        $mirror .= ".git" unless (-d $mirror); # Support local disk mirror
    }

    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;
        }
    }

    my $do_clone = (! -d "$submodule/.git");
    if ($do_clone) {
        $self->exe('git', 'clone', @reference_args, ($mirror ? $mirror : $url), $submodule);
    }

    chdir($submodule) or confess "chdir $submodule: $OS_ERROR";

    $self->exe('git', 'config', 'remote.origin.url', $url);
    if ($mirror) {
        $self->exe('git', 'config', 'remote.mirror.url', $mirror);
        $self->exe('git', 'config', 'remote.mirror.fetch', '+refs/heads/*:refs/remotes/mirror/*');
    }

    if (!$do_clone) {
        $self->exe('git', 'fetch', ($mirror ? $mirror : $url));
    }

    my $template = getcwd()."/../.commit-template";
    if (-e $template) {
        $self->exe('git', 'config', 'commit.template', $template);
    }

    $self->git_add_remotes($submodule);

    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("..") or confess "cd ..: $OS_ERROR";

    return;
}

sub run
{
    my ($self) = @_;

    $self->check_if_already_initialized;
    $self->git_submodule_init;

    if (!$self->{webkit}) {
        $self->git_disable_webkit_submodule;
    }

    if ($self->{'module-subset'}) {
        $self->git_prune_submodules;
    }

    $self->git_set_submodule_config;

    $self->git_clone_all_submodules;

    $self->git_add_remotes('qt5');

    return;
}

#==============================================================================

Qt::InitRepository->new(@ARGV)->run if (!caller);
1;