2 #############################################################################
4 ## Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
5 ## All rights reserved.
6 ## Contact: Nokia Corporation (qt-info@nokia.com)
8 ## This file is part of the utilities of the Qt Toolkit.
10 ## $QT_BEGIN_LICENSE:LGPL$
11 ## GNU Lesser General Public License Usage
12 ## This file may be used under the terms of the GNU Lesser General Public
13 ## License version 2.1 as published by the Free Software Foundation and
14 ## appearing in the file LICENSE.LGPL included in the packaging of this
15 ## file. Please review the following information to ensure the GNU Lesser
16 ## General Public License version 2.1 requirements will be met:
17 ## http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ## In addition, as a special exception, Nokia gives you certain additional
20 ## rights. These rights are described in the Nokia Qt LGPL Exception
21 ## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
23 ## GNU General Public License Usage
24 ## Alternatively, this file may be used under the terms of the GNU General
25 ## Public License version 3.0 as published by the Free Software Foundation
26 ## and appearing in the file LICENSE.GPL included in the packaging of this
27 ## file. Please review the following information to ensure the GNU General
28 ## Public License version 3.0 requirements will be met:
29 ## http://www.gnu.org/copyleft/gpl.html.
32 ## Alternatively, this file may be used in accordance with the terms and
33 ## conditions contained in a signed written agreement between you and Nokia.
41 #############################################################################
47 package Qt::InitRepository;
52 init-repository - initialize the Qt5 repository and all submodules
56 ./init-repository [options]
58 This script may be run after an initial `git clone' of Qt5 in order to check
70 Force initialization (even if the submodules are already checked out).
75 Be quiet. Will exit cleanly if the repository is already initialized.
86 Skip webkit and webkit examples submodules.
87 It may be desirable to skip these modules due to the large size of the webkit
90 =item --module-subset=<module1>,<module2>...
92 Only initialize the specified subset of modules given as the argument. Specified
93 modules must already exist in .gitmodules.
97 Skip the `git submodule update' command.
100 =item --ignore-submodules
102 Set git config to ignore submodules by default when doing operations on the
103 qt5 repo, such as `pull', `fetch', `diff' etc.
105 This option is default for --nokia-developer/--brisbane.
107 After using this option, pass `--ignore-submodules=none' to git to override
113 B<Repository options:>
117 =item --nokia-developer
119 Switch to internal Nokia URLs.
124 Switch to internal Nokia URLs and make use of the Brisbane git mirrors.
125 (Implies `--mirror' and `--mirror-webkit').
129 Switch to internal Nokia URLs and make use of the Berlin git mirrors.
130 (Implies `--mirror' and `--mirror-webkit').
135 Use the SSH protocol for git operations. This may be useful if the git
136 protocol is blocked by a firewall. Note that this requires a user account
137 with an uploaded SSH key on all servers used. (Implies `--nokia-developer').
139 The `--ssh' option does not affect the gerrit remotes.
144 Use the HTTP protocol for git operations. This may be useful if the git
145 protocol is blocked by a firewall. Note that this only works with the
146 external Gitorious server.
148 The `--http' option does not affect the gerrit remotes.
151 =item --codereview-username <Gerrit/JIRA username>
153 Adds a (potentially) writable remote named `gerrit' for each module,
154 for use with the Gerrit code review tool.
155 This requires a username for SSH access to the codereview.qt.nokia.com
156 server, which will be the same username you have for the bugtracker at
157 bugreports.qt.nokia.com.
159 If this option is omitted, the gerrit remote is created with read-only
160 access (using HTTP protocol).
163 =item --alternates <path to other Qt5 repo>
165 Adds alternates for each submodule to another full qt5 checkout. This makes
166 this qt5 checkout very small, as it will use the object store of the
167 alternates before unique objects are stored in its own object store.
169 This option has no effect when using `--no-update'.
171 B<NOTE:> This will make this repo dependent on the alternate, which is
172 potentially dangerous! The dependency can be broken by also using
173 the `--copy-objects' option, or by running C<git repack -a> in each
174 submodule, where required. Please read the note about the `--shared' option
175 in the documentation of `git clone' for more information.
180 When `--alternates' is used, automatically do a C<git repack -a> in each
181 submodule after cloning, to ensure that the repositories are independent
182 from the source used as a reference for cloning.
184 Note that this negates the disk usage benefits gained from the use of
188 =item --mirror <url-base>
190 Uses <url-base> as the base URL for submodule git mirrors.
194 --mirror user@machine:/foo/bar
196 ...will use the following as a mirror for qtbase:
198 user@machine:/foo/bar/qtbase.git
201 =item --mirror-webkit <url>
203 Uses <url> as the URL for the webkit git mirror.
209 use Carp qw( confess );
210 use English qw( -no_match_vars );
211 use Getopt::Long qw( GetOptionsFromArray );
212 use Pod::Usage qw( pod2usage );
213 use Cwd qw( getcwd );
216 'internal' => 'git://scm.dev.nokia.troll.no/' ,
217 'ssh' => 'git@scm.dev.nokia.troll.no:' ,
218 'http' => 'http://git.gitorious.org/' ,
221 my %GERRIT_REPOS = map { $_ => "qt/$_" } qw(
243 qtwebkit-examples-and-demos
246 $GERRIT_REPOS{qtquick3d} = "qt/quick3d";
248 # Protocol-specific repo overrides, if they differ from the values set in the git submodule config
249 # (e.g. because public vs private names differ for whatever reason)
250 my %PROTOCOL_REPOS = (
252 internal => "qt/quick3d", # instead of qt-quick3d/qt-quick3d on gitorious
258 = 'ssh://codereview.qt.nokia.com:29418/';
261 = 'http://codereview.qt.nokia.com/p/';
263 my $BNE_MIRROR_URL_BASE
264 = 'git://bq-git.apac.nokia.com/qtsoftware/';
266 my $BNE_MIRROR_WEBKIT_URL
267 = 'git://bq-git.apac.nokia.com/qtsoftware/research/gitorious-org-webkit-qtwebkit-mirror.git';
269 my $BNE_MIRROR_V8_URL
270 = 'git://bq-git.apac.nokia.com/github/v8.git';
272 my $BER_MIRROR_URL_BASE
273 = 'git://ber-git.europe.nokia.com/';
275 my $BER_MIRROR_WEBKIT_URL
276 = 'git://ber-git.europe.nokia.com/qtwebkit/qtwebkit.git';
281 my ($class, @arguments) = @_;
285 $self->parse_arguments(@arguments);
290 # Like `system', but possibly log the command, and die on non-zero exit code
293 my ($self, @cmd) = @_;
295 if (!$self->{quiet}) {
299 if (system(@cmd) != 0) {
300 confess "@cmd exited with status $CHILD_ERROR";
308 my ($self, @args) = @_;
310 %{$self} = (%{$self},
312 'codereview-username' => "",
313 'detach-alternates' => 0 ,
315 'ignore-submodules' => 0 ,
317 'mirror-webkit-url' => "",
318 'mirror-v8-url' => "",
319 'nokia-developer' => 0 ,
323 'module-subset' => "",
326 GetOptionsFromArray(\@args,
327 'alternates=s' => \$self->{qw{ alternates }},
328 'codereview-username=s' => \$self->{qw{ codereview-username }},
329 'copy-objects' => \$self->{qw{ detach-alternates }},
330 'force' => \$self->{qw{ force }},
331 'ignore-submodules' => \$self->{qw{ ignore-submodules }},
332 'mirror-webkit=s' => \$self->{qw{ mirror-webkit-url }},
333 'mirror=s' => \$self->{qw{ mirror-url }},
334 'nokia-developer' => \$self->{qw{ nokia-developer }},
335 'quiet' => \$self->{qw{ quiet }},
336 'update!' => \$self->{qw{ update }},
337 'webkit!' => \$self->{qw{ webkit }},
338 'module-subset=s' => \$self->{qw{ module-subset }},
340 'help|?' => sub { pod2usage(1); },
341 'http' => sub { $self->{protocol} = 'http'; },
342 'ssh|ssh-protocol' => sub { $self->{protocol} = 'ssh'; },
344 'brisbane|brisbane-nokia-developer' => sub {
345 $self->{'nokia-developer'} = 1;
346 $self->{'protocol'} = 'internal';
347 $self->{'mirror-url'} = $BNE_MIRROR_URL_BASE;
348 $self->{'mirror-v8-url'} = $BNE_MIRROR_V8_URL;
349 $self->{'mirror-webkit-url'} = $BNE_MIRROR_WEBKIT_URL;
350 $self->{'ignore-submodules'} = 1;
353 'berlin|berlin-nokia-developer' => sub {
354 $self->{'nokia-developer'} = 1;
355 $self->{'protocol'} = 'internal';
356 $self->{'mirror-url'} = $BER_MIRROR_URL_BASE;
357 $self->{'mirror-webkit-url'} = $BER_MIRROR_WEBKIT_URL;
360 'nokia-developer' => sub {
361 $self->{'nokia-developer'} = 1;
362 $self->{'protocol'} = 'internal';
363 $self->{'ignore-submodules'} = 1;
367 if ($self->{'nokia-developer'} && $self->{'protocol'} eq 'http') {
368 print "*** Ignoring use of HTTP protocol, as it's only usable with external server\n";
369 $self->{'protocol'} = '';
372 # Replace any double trailing slashes from end of mirror
373 $self->{'mirror-url'} =~ s{//+$}{/};
375 if ($self->{'module-subset'}) {
376 $self->{'module-subset'} = {
377 map { $_ => 1 } split(qr{,}, $self->{'module-subset'})
384 sub check_if_already_initialized
388 # We consider the repo as `initialized' if submodule.qtbase.url is set
389 if (qx(git config --get submodule.qtbase.url)) {
390 if ($self->{force}) {
391 my @configresult = qx(git config -l);
392 foreach (@configresult) {
393 # Example line: submodule.qtqa.url=git://gitorious.org/qt/qtqa.git
394 if (/(submodule\.[^.=]+)\.url=.*/) {
395 $self->exe('git', 'config', '--remove-section', $1);
400 exit 0 if ($self->{quiet});
401 print "Will not reinitialize already initialized repository (use -f to force)!\n";
409 sub git_submodule_init
414 if ($self->{quiet}) {
415 push @init_args, '--quiet';
417 $self->exe('git', 'submodule', 'init', @init_args);
419 my $template = getcwd()."/.commit-template";
421 $self->exe('git', 'config', 'commit.template', $template);
427 sub git_disable_webkit_submodule
431 $self->exe('git', 'config', '--remove', 'submodule.qtwebkit');
432 $self->exe('git', 'config', '--remove', 'submodule.qtwebkit-examples-and-demos');
437 sub git_prune_submodules
441 my @configresult = qx(git config -l);
442 foreach my $line (@configresult) {
443 if ($line =~ /submodule\.([^.=]+)\.url=/) {
444 my $module_name = $1;
445 if (!$self->{'module-subset'}{$module_name}) {
446 $self->exe('git', 'config', '--remove', "submodule.$module_name");
452 sub git_set_submodule_config
456 my @configresult = qx(git config -l);
457 my $protocol = $self->{protocol};
458 my $url_base_for_protocol = $PROTOCOLS{$protocol};
460 foreach my $line (@configresult) {
461 # Example line: submodule.qtqa.url=git://gitorious.org/qt/qtqa.git
462 next if ($line !~ /submodule\.([^.=]+)\.url=(.*)/);
468 # WebKit is special, and has only external link.
469 if ($key ne 'qtwebkit') {
470 # qt-labs projects are still hosted under qt internally.
471 if ($protocol ne 'http') {
472 $value =~ s,^git://gitorious\.org/qt-labs/,${url_base_for_protocol}qt/,;
475 if ($PROTOCOL_REPOS{$key}->{$protocol}) {
476 # If this repo has an explicitly set basename for this protocol, use it...
477 # e.g. 'git@example.com:' . 'qt/quick3d'
478 $value = $url_base_for_protocol . $PROTOCOL_REPOS{$key}->{$protocol};
481 # ...otherwise, assume the selected protocol uses same naming structure
483 # e.g. git://gitorious.org/qt/qt5 => git@example.com:qt/qt5
484 $value =~ s,^git://gitorious\.org/,$url_base_for_protocol,;
489 $self->exe('git', 'config', "submodule.$key.url", $value);
491 if ($self->{'ignore-submodules'}) {
492 $self->exe('git', 'config', "submodule.$key.ignore", 'all');
499 sub git_clone_all_submodules
503 # manually clone each repo here, so we can easily use reference repos, mirrors etc
504 my @configresult = qx(git config -l);
505 foreach my $line (@configresult) {
506 if ($line =~ /submodule\.([^.=]+)\.url=(.*)/) {
507 $self->git_clone_one_submodule($1, $2);
511 $self->exe('git', 'submodule', 'update');
518 my ($self, $repo_basename) = @_;
520 my $protocol = $self->{protocol};
521 my $url_base_for_protocol = $PROTOCOLS{$protocol};
524 for my $line (qx(git remote show)) {
526 $current_remotes{$line} = 1;
529 my $gerrit_repo_basename = $GERRIT_REPOS{$repo_basename};
530 if ($gerrit_repo_basename && !$current_remotes{'gerrit'}) {
533 # If given a username, we use writable remote (ssh).
534 # Otherwise, we use read-only (http).
535 if ($self->{'codereview-username'}) {
536 $gerrit_repo_url = $GERRIT_SSH_BASE;
537 $gerrit_repo_url =~ s[^ssh://][ssh://$self->{'codereview-username'}@];
540 $gerrit_repo_url = $GERRIT_HTTP_BASE;
543 $gerrit_repo_url .= $gerrit_repo_basename;
544 $self->exe('git', 'remote', 'add', 'gerrit', $gerrit_repo_url);
546 $current_remotes{'gerrit'} = 1;
549 # if repo still has no gerrit repo defined, alias it to origin
550 if (!$current_remotes{'gerrit'}) {
551 my @configresult = qx(git remote -v);
552 foreach (@configresult) {
553 if (/^origin\s+(\S+) \(fetch\)/) {
554 $self->exe('git', 'remote', 'add', 'gerrit', $1);
562 sub git_clone_one_submodule
564 my ($self, $submodule, $url) = @_;
566 my $alternates = $self->{ 'alternates' };
567 my $mirror_url = $self->{ 'mirror-url' };
568 my $mirror_webkit_url = $self->{ 'mirror-webkit-url' };
569 my $mirror_v8_url = $self->{ 'mirror-v8-url' };
570 my $protocol = $self->{ 'protocol' };
572 # `--reference FOO' args for the clone, if any.
576 # alternates is a qt5 repo, so the submodule will be under that.
577 if (-d "$alternates/$submodule") {
578 @reference_args = ('--reference', "$alternates/$submodule");
581 print " *** $alternates/$submodule not found, ignoring alternate for this submodule\n";
586 if ($mirror_url && ($submodule ne 'qtwebkit')) {
587 $mirror = $mirror_url.( $PROTOCOL_REPOS{$submodule}->{internal} // "qt/$submodule" );
588 $mirror .= ".git" unless (-d $mirror); # Support local disk mirror
590 elsif ($mirror_webkit_url && ($submodule eq 'qtwebkit')) {
591 $mirror = $mirror_webkit_url;
594 my $do_clone = (! -d "$submodule/.git");
596 $self->exe('git', 'clone', @reference_args, ($mirror ? $mirror : $url), $submodule);
599 chdir($submodule) or confess "chdir $submodule: $OS_ERROR";
602 $self->exe('git', 'fetch', ($mirror ? $mirror : $url));
605 my $template = getcwd()."/../.commit-template";
607 $self->exe('git', 'config', 'commit.template', $template);
611 $self->exe('git', 'config', 'remote.origin.url', $url);
613 # In `force' mode, remove the mirror if it already exists,
614 # since we may be reinitializing the module.
615 if ($self->{force}) {
616 eval { $self->exe('git', 'remote', 'rm', 'mirror'); }; # failure is OK
619 $self->exe('git', 'remote', 'add', 'mirror', $mirror);
622 $self->git_add_remotes($submodule);
624 if ($self->{'detach-alternates'}) {
625 $self->exe('git', 'repack', '-a');
627 my $alternates_path = '.git/objects/info/alternates';
628 unlink($alternates_path) || confess "unlink $alternates_path: $OS_ERROR";
631 if ($submodule eq "qtdeclarative") { #Extra step needed to setup declarative
632 $self->exe('git', 'submodule', 'init');
633 if ($mirror_v8_url || $protocol eq 'http') {
634 my @configresult = qx(git config -l);
636 foreach my $line (@configresult) {
637 # Example line: submodule.qtqa.url=git://gitorious.org/qt/qtqa.git
638 next if ($line !~ /submodule.src\/3rdparty\/v8.url=(.*)/);
642 if ($protocol eq 'http') {
643 # rewrite the git:// url to https://
644 if ($v8url =~ s{^git://github}{https://github}) {
645 $self->exe('git', 'config', 'submodule.src/3rdparty/v8.url', $v8url);
648 warn 'You requested git over http, but I could not figure out how to '
649 ."rewrite v8's giturl of $v8url";
653 if ($mirror_v8_url && $do_clone) {
654 chdir('src/3rdparty/') or confess "chdir $submodule/src/3rdparty: $OS_ERROR";
655 $self->exe('git', 'clone', $mirror_v8_url, 'v8');
656 chdir('v8') or confess "chdir $submodule/src/3rdparty/v8: $OS_ERROR";
657 $self->exe('git', 'config', 'remote.origin.url', $v8url);
658 chdir('../../..') or confess "cd ../../..: $OS_ERROR";
661 $self->exe('git', 'submodule', 'update');
664 chdir("..") or confess "cd ..: $OS_ERROR";
673 $self->check_if_already_initialized;
674 $self->git_submodule_init;
676 if (!$self->{webkit}) {
677 $self->git_disable_webkit_submodule;
680 if ($self->{'module-subset'}) {
681 $self->git_prune_submodules;
684 $self->git_set_submodule_config;
686 if ($self->{update}) {
687 $self->git_clone_all_submodules;
690 $self->git_add_remotes('qt5');
695 #==============================================================================
697 Qt::InitRepository->new(@ARGV)->run if (!caller);