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-project.org
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(
245 qtwebkit-examples-and-demos
248 $GERRIT_REPOS{qtquick3d} = "qt/quick3d";
250 # Protocol-specific repo overrides, if they differ from the values set in the git submodule config
251 # (e.g. because public vs private names differ for whatever reason)
252 my %PROTOCOL_REPOS = (
254 internal => "qt/quick3d", # instead of qt-quick3d/qt-quick3d on gitorious
260 = 'ssh://codereview.qt-project.org:29418/';
263 = 'http://codereview.qt-project.org/p/';
265 my $BNE_MIRROR_URL_BASE
266 = 'git://bq-git.apac.nokia.com/qtsoftware/';
268 my $BNE_MIRROR_WEBKIT_URL
269 = 'git://bq-git.apac.nokia.com/qtsoftware/research/gitorious-org-webkit-qtwebkit-mirror.git';
271 my $BNE_MIRROR_V8_URL
272 = 'git://bq-git.apac.nokia.com/github/v8.git';
274 my $BER_MIRROR_URL_BASE
275 = 'git://ber-git.europe.nokia.com/';
277 my $BER_MIRROR_WEBKIT_URL
278 = 'git://ber-git.europe.nokia.com/qtwebkit/qtwebkit.git';
283 my ($class, @arguments) = @_;
287 $self->parse_arguments(@arguments);
292 # Like `system', but possibly log the command, and die on non-zero exit code
295 my ($self, @cmd) = @_;
297 if (!$self->{quiet}) {
301 if (system(@cmd) != 0) {
302 confess "@cmd exited with status $CHILD_ERROR";
310 my ($self, @args) = @_;
312 %{$self} = (%{$self},
314 'codereview-username' => "",
315 'detach-alternates' => 0 ,
317 'ignore-submodules' => 0 ,
319 'mirror-webkit-url' => "",
320 'mirror-v8-url' => "",
321 'nokia-developer' => 0 ,
325 'module-subset' => "",
328 GetOptionsFromArray(\@args,
329 'alternates=s' => \$self->{qw{ alternates }},
330 'codereview-username=s' => \$self->{qw{ codereview-username }},
331 'copy-objects' => \$self->{qw{ detach-alternates }},
332 'force' => \$self->{qw{ force }},
333 'ignore-submodules' => \$self->{qw{ ignore-submodules }},
334 'mirror-webkit=s' => \$self->{qw{ mirror-webkit-url }},
335 'mirror=s' => \$self->{qw{ mirror-url }},
336 'nokia-developer' => \$self->{qw{ nokia-developer }},
337 'quiet' => \$self->{qw{ quiet }},
338 'update!' => \$self->{qw{ update }},
339 'webkit!' => \$self->{qw{ webkit }},
340 'module-subset=s' => \$self->{qw{ module-subset }},
342 'help|?' => sub { pod2usage(1); },
343 'http' => sub { $self->{protocol} = 'http'; },
344 'ssh|ssh-protocol' => sub { $self->{protocol} = 'ssh'; },
346 'brisbane|brisbane-nokia-developer' => sub {
347 $self->{'nokia-developer'} = 1;
348 $self->{'protocol'} = 'internal';
349 $self->{'mirror-url'} = $BNE_MIRROR_URL_BASE;
350 $self->{'mirror-v8-url'} = $BNE_MIRROR_V8_URL;
351 $self->{'mirror-webkit-url'} = $BNE_MIRROR_WEBKIT_URL;
352 $self->{'ignore-submodules'} = 1;
355 'berlin|berlin-nokia-developer' => sub {
356 $self->{'nokia-developer'} = 1;
357 $self->{'protocol'} = 'internal';
358 $self->{'mirror-url'} = $BER_MIRROR_URL_BASE;
359 $self->{'mirror-webkit-url'} = $BER_MIRROR_WEBKIT_URL;
362 'nokia-developer' => sub {
363 $self->{'nokia-developer'} = 1;
364 $self->{'protocol'} = 'internal';
365 $self->{'ignore-submodules'} = 1;
369 if ($self->{'nokia-developer'} && $self->{'protocol'} eq 'http') {
370 print "*** Ignoring use of HTTP protocol, as it's only usable with external server\n";
371 $self->{'protocol'} = '';
374 # Replace any double trailing slashes from end of mirror
375 $self->{'mirror-url'} =~ s{//+$}{/};
377 if ($self->{'module-subset'}) {
378 $self->{'module-subset'} = {
379 map { $_ => 1 } split(qr{,}, $self->{'module-subset'})
386 sub check_if_already_initialized
390 # We consider the repo as `initialized' if submodule.qtbase.url is set
391 if (qx(git config --get submodule.qtbase.url)) {
392 if ($self->{force}) {
393 my @configresult = qx(git config -l);
394 foreach (@configresult) {
395 # Example line: submodule.qtqa.url=git://gitorious.org/qt/qtqa.git
396 if (/(submodule\.[^.=]+)\.url=.*/) {
397 $self->exe('git', 'config', '--remove-section', $1);
402 exit 0 if ($self->{quiet});
403 print "Will not reinitialize already initialized repository (use -f to force)!\n";
411 sub git_submodule_init
416 if ($self->{quiet}) {
417 push @init_args, '--quiet';
419 $self->exe('git', 'submodule', 'init', @init_args);
421 my $template = getcwd()."/.commit-template";
423 $self->exe('git', 'config', 'commit.template', $template);
429 sub git_disable_webkit_submodule
433 $self->exe('git', 'config', '--remove', 'submodule.qtwebkit');
434 $self->exe('git', 'config', '--remove', 'submodule.qtwebkit-examples-and-demos');
439 sub git_prune_submodules
443 my @configresult = qx(git config -l);
444 foreach my $line (@configresult) {
445 if ($line =~ /submodule\.([^.=]+)\.url=/) {
446 my $module_name = $1;
447 if (!$self->{'module-subset'}{$module_name}) {
448 $self->exe('git', 'config', '--remove', "submodule.$module_name");
454 sub git_set_submodule_config
458 my @configresult = qx(git config -l);
459 my $protocol = $self->{protocol};
460 my $url_base_for_protocol = $PROTOCOLS{$protocol};
462 foreach my $line (@configresult) {
463 # Example line: submodule.qtqa.url=git://gitorious.org/qt/qtqa.git
464 next if ($line !~ /submodule\.([^.=]+)\.url=(.*)/);
470 # WebKit is special, and has only external link.
471 if ($key ne 'qtwebkit') {
472 # qt-labs projects are still hosted under qt internally.
473 if ($protocol ne 'http') {
474 $value =~ s,^git://gitorious\.org/qt-labs/,${url_base_for_protocol}qt/,;
477 if ($PROTOCOL_REPOS{$key}->{$protocol}) {
478 # If this repo has an explicitly set basename for this protocol, use it...
479 # e.g. 'git@example.com:' . 'qt/quick3d'
480 $value = $url_base_for_protocol . $PROTOCOL_REPOS{$key}->{$protocol};
483 # ...otherwise, assume the selected protocol uses same naming structure
485 # e.g. git://gitorious.org/qt/qt5 => git@example.com:qt/qt5
486 $value =~ s,^git://gitorious\.org/,$url_base_for_protocol,;
491 $self->exe('git', 'config', "submodule.$key.url", $value);
493 if ($self->{'ignore-submodules'}) {
494 $self->exe('git', 'config', "submodule.$key.ignore", 'all');
501 sub git_clone_all_submodules
505 # manually clone each repo here, so we can easily use reference repos, mirrors etc
506 my @configresult = qx(git config -l);
507 foreach my $line (@configresult) {
508 if ($line =~ /submodule\.([^.=]+)\.url=(.*)/) {
509 $self->git_clone_one_submodule($1, $2);
513 $self->exe('git', 'submodule', 'update');
520 my ($self, $repo_basename) = @_;
522 my $protocol = $self->{protocol};
523 my $url_base_for_protocol = $PROTOCOLS{$protocol};
526 for my $line (qx(git remote show)) {
528 $current_remotes{$line} = 1;
531 my $gerrit_repo_basename = $GERRIT_REPOS{$repo_basename};
532 if ($gerrit_repo_basename && !$current_remotes{'gerrit'}) {
535 # If given a username, we use writable remote (ssh).
536 # Otherwise, we use read-only (http).
537 if ($self->{'codereview-username'}) {
538 $gerrit_repo_url = $GERRIT_SSH_BASE;
539 $gerrit_repo_url =~ s[^ssh://][ssh://$self->{'codereview-username'}@];
542 $gerrit_repo_url = $GERRIT_HTTP_BASE;
545 $gerrit_repo_url .= $gerrit_repo_basename;
546 $self->exe('git', 'remote', 'add', 'gerrit', $gerrit_repo_url);
548 $current_remotes{'gerrit'} = 1;
551 # if repo still has no gerrit repo defined, alias it to origin
552 if (!$current_remotes{'gerrit'}) {
553 my @configresult = qx(git remote -v);
554 foreach (@configresult) {
555 if (/^origin\s+(\S+) \(fetch\)/) {
556 $self->exe('git', 'remote', 'add', 'gerrit', $1);
564 sub git_clone_one_submodule
566 my ($self, $submodule, $url) = @_;
568 my $alternates = $self->{ 'alternates' };
569 my $mirror_url = $self->{ 'mirror-url' };
570 my $mirror_webkit_url = $self->{ 'mirror-webkit-url' };
571 my $mirror_v8_url = $self->{ 'mirror-v8-url' };
572 my $protocol = $self->{ 'protocol' };
573 my $nokia_developer = $self->{ 'nokia-developer' };
575 # `--reference FOO' args for the clone, if any.
579 # alternates is a qt5 repo, so the submodule will be under that.
580 if (-d "$alternates/$submodule") {
581 @reference_args = ('--reference', "$alternates/$submodule");
584 print " *** $alternates/$submodule not found, ignoring alternate for this submodule\n";
589 if ($mirror_url && ($submodule ne 'qtwebkit')) {
590 $mirror = $mirror_url.( $PROTOCOL_REPOS{$submodule}->{internal} // "qt/$submodule" );
591 $mirror .= ".git" unless (-d $mirror); # Support local disk mirror
593 elsif ($mirror_webkit_url && ($submodule eq 'qtwebkit')) {
594 $mirror = $mirror_webkit_url;
597 my $do_clone = (! -d "$submodule/.git");
599 $self->exe('git', 'clone', @reference_args, ($mirror ? $mirror : $url), $submodule);
602 chdir($submodule) or confess "chdir $submodule: $OS_ERROR";
605 $self->exe('git', 'fetch', ($mirror ? $mirror : $url));
608 my $template = getcwd()."/../.commit-template";
610 $self->exe('git', 'config', 'commit.template', $template);
614 $self->exe('git', 'config', 'remote.origin.url', $url);
616 # In `force' mode, remove the mirror if it already exists,
617 # since we may be reinitializing the module.
618 if ($self->{force}) {
619 eval { $self->exe('git', 'remote', 'rm', 'mirror'); }; # failure is OK
622 $self->exe('git', 'remote', 'add', 'mirror', $mirror);
625 $self->git_add_remotes($submodule) if $nokia_developer;
627 if ($self->{'detach-alternates'}) {
628 $self->exe('git', 'repack', '-a');
630 my $alternates_path = '.git/objects/info/alternates';
631 unlink($alternates_path) || confess "unlink $alternates_path: $OS_ERROR";
634 if ($submodule eq "qtbase" || $submodule eq "qtdeclarative") { #Extra steps needed to setup base and declarative
635 $self->exe('git', 'submodule', 'init');
636 if ($mirror_v8_url || $protocol eq 'http') {
637 my @configresult = qx(git config -l);
639 foreach my $line (@configresult) {
640 # Example line: submodule.qtqa.url=git://gitorious.org/qt/qtqa.git
641 next if ($line !~ /submodule.src\/3rdparty\/v8.url=(.*)/);
646 if ($protocol eq 'http') {
647 # rewrite the git:// url to https://
648 if ($v8url =~ s{^git://github}{https://github}) {
649 $self->exe('git', 'config', 'submodule.src/3rdparty/v8.url', $v8url);
652 warn 'You requested git over http, but I could not figure out how to '
653 ."rewrite v8's giturl of $v8url";
657 if ($mirror_v8_url && $do_clone) {
658 chdir('src/3rdparty/') or confess "chdir $submodule/src/3rdparty: $OS_ERROR";
659 $self->exe('git', 'clone', $mirror_v8_url, 'v8');
660 chdir('v8') or confess "chdir $submodule/src/3rdparty/v8: $OS_ERROR";
661 $self->exe('git', 'config', 'remote.origin.url', $v8url);
662 chdir('../../..') or confess "cd ../../..: $OS_ERROR";
666 $self->exe('git', 'submodule', 'update');
669 chdir("..") or confess "cd ..: $OS_ERROR";
678 my $nokia_developer = $self->{ 'nokia-developer' };
680 $self->check_if_already_initialized;
681 $self->git_submodule_init;
683 if (!$self->{webkit}) {
684 $self->git_disable_webkit_submodule;
687 if ($self->{'module-subset'}) {
688 $self->git_prune_submodules;
691 $self->git_set_submodule_config;
693 if ($self->{update}) {
694 $self->git_clone_all_submodules;
697 $self->git_add_remotes('qt5') if $nokia_developer;
702 #==============================================================================
704 Qt::InitRepository->new(@ARGV)->run if (!caller);