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 ## No Commercial Usage
12 ## This file contains pre-release code and may not be distributed.
13 ## You may use this file in accordance with the terms and conditions
14 ## contained in the Technology Preview License Agreement accompanying
17 ## GNU Lesser General Public License Usage
18 ## Alternatively, this file may be used under the terms of the GNU Lesser
19 ## General Public License version 2.1 as published by the Free Software
20 ## Foundation and appearing in the file LICENSE.LGPL included in the
21 ## packaging of this file. Please review the following information to
22 ## ensure the GNU Lesser General Public License version 2.1 requirements
23 ## will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ## In addition, as a special exception, Nokia gives you certain additional
26 ## rights. These rights are described in the Nokia Qt LGPL Exception
27 ## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ## If you have questions regarding the use of this file, please contact
30 ## Nokia at qt-info@nokia.com.
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(
241 qtwebkit-examples-and-demos
246 = 'ssh://codereview.qt.nokia.com:29418/';
249 = 'http://codereview.qt.nokia.com/p/';
251 my $BNE_MIRROR_URL_BASE
252 = 'git://bq-git.apac.nokia.com/qtsoftware/qt/';
254 my $BNE_MIRROR_WEBKIT_URL
255 = 'git://bq-git.apac.nokia.com/qtsoftware/research/gitorious-org-webkit-qtwebkit-mirror.git';
257 my $BER_MIRROR_URL_BASE
258 = 'git://ber-git.europe.nokia.com/qt/';
260 my $BER_MIRROR_WEBKIT_URL
261 = 'git://ber-git.europe.nokia.com/qtwebkit/qtwebkit.git';
266 my ($class, @arguments) = @_;
270 $self->parse_arguments(@arguments);
275 # Like `system', but possibly log the command, and die on non-zero exit code
278 my ($self, @cmd) = @_;
280 if (!$self->{quiet}) {
284 if (system(@cmd) != 0) {
285 confess "@cmd exited with status $CHILD_ERROR";
293 my ($self, @args) = @_;
295 %{$self} = (%{$self},
297 'codereview-username' => "",
298 'detach-alternates' => 0 ,
300 'ignore-submodules' => 0 ,
302 'mirror-webkit-url' => "",
303 'nokia-developer' => 0 ,
307 'module-subset' => "",
310 GetOptionsFromArray(\@args,
311 'alternates=s' => \$self->{qw{ alternates }},
312 'codereview-username=s' => \$self->{qw{ codereview-username }},
313 'copy-objects' => \$self->{qw{ detach-alternates }},
314 'force' => \$self->{qw{ force }},
315 'ignore-submodules' => \$self->{qw{ ignore-submodules }},
316 'mirror-webkit=s' => \$self->{qw{ mirror-webkit-url }},
317 'mirror=s' => \$self->{qw{ mirror-url }},
318 'nokia-developer' => \$self->{qw{ nokia-developer }},
319 'quiet' => \$self->{qw{ quiet }},
320 'update!' => \$self->{qw{ update }},
321 'webkit!' => \$self->{qw{ webkit }},
322 'module-subset=s' => \$self->{qw{ module-subset }},
324 'help|?' => sub { pod2usage(1); },
325 'http' => sub { $self->{protocol} = 'http'; },
326 'ssh|ssh-protocol' => sub { $self->{protocol} = 'ssh'; },
328 'brisbane|brisbane-nokia-developer' => sub {
329 $self->{'nokia-developer'} = 1;
330 $self->{'protocol'} = 'internal';
331 $self->{'mirror-url'} = $BNE_MIRROR_URL_BASE;
332 $self->{'mirror-webkit-url'} = $BNE_MIRROR_WEBKIT_URL;
333 $self->{'ignore-submodules'} = 1;
336 'berlin|berlin-nokia-developer' => sub {
337 $self->{'nokia-developer'} = 1;
338 $self->{'protocol'} = 'internal';
339 $self->{'mirror-url'} = $BER_MIRROR_URL_BASE;
340 $self->{'mirror-webkit-url'} = $BER_MIRROR_WEBKIT_URL;
343 'nokia-developer' => sub {
344 $self->{'nokia-developer'} = 1;
345 $self->{'protocol'} = 'internal';
346 $self->{'ignore-submodules'} = 1;
350 if ($self->{'nokia-developer'} && $self->{'protocol'} eq 'http') {
351 print "*** Ignoring use of HTTP protocol, as it's only usable with external server\n";
352 $self->{'protocol'} = '';
355 # Replace any double trailing slashes from end of mirror
356 $self->{'mirror-url'} =~ s{//+$}{/};
358 if ($self->{'module-subset'}) {
359 $self->{'module-subset'} = {
360 map { $_ => 1 } split(qr{,}, $self->{'module-subset'})
367 sub check_if_already_initialized
371 # We consider the repo as `initialized' if submodule.qtbase.url is set
372 if (qx(git config --get submodule.qtbase.url)) {
373 if ($self->{force}) {
374 my @configresult = qx(git config -l);
375 foreach (@configresult) {
376 # Example line: submodule.qtqa.url=git://gitorious.org/qt/qtqa.git
377 if (/(submodule\.[^.=]+)\.url=.*/) {
378 $self->exe('git', 'config', '--remove-section', $1);
383 exit 0 if ($self->{quiet});
384 print "Will not reinitialize already initialized repository (use -f to force)!\n";
392 sub git_submodule_init
397 if ($self->{quiet}) {
398 push @init_args, '--quiet';
400 $self->exe('git', 'submodule', 'init', @init_args);
402 my $template = getcwd()."/.commit-template";
404 $self->exe('git', 'config', 'commit.template', $template);
410 sub git_disable_webkit_submodule
414 $self->exe('git', 'config', '--remove', 'submodule.qtwebkit');
415 $self->exe('git', 'config', '--remove', 'submodule.qtwebkit-examples-and-demos');
420 sub git_prune_submodules
424 my @configresult = qx(git config -l);
425 foreach my $line (@configresult) {
426 if ($line =~ /submodule\.([^.=]+)\.url=/) {
427 my $module_name = $1;
428 if (!$self->{'module-subset'}{$module_name}) {
429 $self->exe('git', 'config', '--remove', "submodule.$module_name");
435 sub git_set_submodule_config
439 my @configresult = qx(git config -l);
440 my $protocol = $self->{protocol};
441 my $url_base_for_protocol = $PROTOCOLS{$protocol};
443 foreach my $line (@configresult) {
444 # Example line: submodule.qtqa.url=git://gitorious.org/qt/qtqa.git
445 next if ($line !~ /(submodule\.[^.=]+\.url)=(.*)/);
451 # WebKit is special, and has only external link.
452 if ($key ne 'submodule.qtwebkit.url') {
453 # qt-labs projects are still hosted under qt internally.
454 if ($protocol ne 'http') {
455 $value =~ s,^git://gitorious\.org/qt-labs/,${url_base_for_protocol}qt/,;
457 $value =~ s,^git://gitorious\.org/,$url_base_for_protocol,;
461 $self->exe('git', 'config', $key, $value);
463 if ($self->{'ignore-submodules'}) {
464 $key =~ s,\.url,.ignore,;
465 $self->exe('git', 'config', $key, 'all');
472 sub git_clone_all_submodules
476 # manually clone each repo here, so we can easily use reference repos, mirrors etc
477 my @configresult = qx(git config -l);
478 foreach my $line (@configresult) {
479 if ($line =~ /submodule\.([^.=]+)\.url=(.*)/) {
480 $self->git_clone_one_submodule($1, $2);
484 $self->exe('git', 'submodule', 'update');
491 my ($self, $repo_basename) = @_;
493 my $protocol = $self->{protocol};
494 my $url_base_for_protocol = $PROTOCOLS{$protocol};
497 for my $line (qx(git remote show)) {
499 $current_remotes{$line} = 1;
502 my $gerrit_repo_basename = $GERRIT_REPOS{$repo_basename};
503 if ($gerrit_repo_basename && !$current_remotes{'gerrit'}) {
506 # If given a username, we use writable remote (ssh).
507 # Otherwise, we use read-only (http).
508 if ($self->{'codereview-username'}) {
509 $gerrit_repo_url = $GERRIT_SSH_BASE;
510 $gerrit_repo_url =~ s[^ssh://][ssh://$self->{'codereview-username'}@];
513 $gerrit_repo_url = $GERRIT_HTTP_BASE;
516 $gerrit_repo_url .= $gerrit_repo_basename;
517 $self->exe('git', 'remote', 'add', 'gerrit', $gerrit_repo_url);
519 $current_remotes{'gerrit'} = 1;
522 # if repo still has no gerrit repo defined, alias it to origin
523 if (!$current_remotes{'gerrit'}) {
524 my @configresult = qx(git remote -v);
525 foreach (@configresult) {
526 if (/^origin\s+(\S+) \(fetch\)/) {
527 $self->exe('git', 'remote', 'add', 'gerrit', $1);
535 sub git_clone_one_submodule
537 my ($self, $submodule, $url) = @_;
539 my $alternates = $self->{ 'alternates' };
540 my $mirror_url = $self->{ 'mirror-url' };
541 my $mirror_webkit_url = $self->{ 'mirror-webkit-url' };
543 # `--reference FOO' args for the clone, if any.
547 # alternates is a qt5 repo, so the submodule will be under that.
548 if (-d "$alternates/$submodule") {
549 @reference_args = ('--reference', "$alternates/$submodule");
552 print " *** $alternates/$submodule not found, ignoring alternate for this submodule\n";
557 if ($mirror_url && ($submodule ne 'qtwebkit')) {
558 $mirror = $mirror_url.$submodule;
559 $mirror .= ".git" unless (-d $mirror); # Support local disk mirror
561 elsif ($mirror_webkit_url && ($submodule eq 'qtwebkit')) {
562 $mirror = $mirror_webkit_url;
565 my $do_clone = (! -d "$submodule/.git");
567 $self->exe('git', 'clone', @reference_args, ($mirror ? $mirror : $url), $submodule);
570 chdir($submodule) or confess "chdir $submodule: $OS_ERROR";
573 $self->exe('git', 'fetch', ($mirror ? $mirror : $url));
576 my $template = getcwd()."/../.commit-template";
578 $self->exe('git', 'config', 'commit.template', $template);
582 $self->exe('git', 'config', 'remote.origin.url', $url);
584 # In `force' mode, remove the mirror if it already exists,
585 # since we may be reinitializing the module.
586 if ($self->{force}) {
587 eval { $self->exe('git', 'remote', 'rm', 'mirror'); }; # failure is OK
590 $self->exe('git', 'remote', 'add', 'mirror', $mirror);
593 $self->git_add_remotes($submodule);
595 if ($self->{'detach-alternates'}) {
596 $self->exe('git', 'repack', '-a');
598 my $alternates_path = '.git/objects/info/alternates';
599 unlink($alternates_path) || confess "unlink $alternates_path: $OS_ERROR";
602 if ($submodule eq "qtdeclarative") { #Extra step needed to setup declarative
603 $self->exe('git', 'submodule', 'init');
604 $self->exe('git', 'submodule', 'update');
607 chdir("..") or confess "cd ..: $OS_ERROR";
616 $self->check_if_already_initialized;
617 $self->git_submodule_init;
619 if (!$self->{webkit}) {
620 $self->git_disable_webkit_submodule;
623 if ($self->{'module-subset'}) {
624 $self->git_prune_submodules;
627 $self->git_set_submodule_config;
629 if ($self->{update}) {
630 $self->git_clone_all_submodules;
633 $self->git_add_remotes('qt5');
638 #==============================================================================
640 Qt::InitRepository->new(@ARGV)->run if (!caller);