#!/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 =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 =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=,... 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. After using this option, pass `--ignore-submodules=none' to git to override it as needed. =back B =over =item --berlin Switch to internal URLs and make use of the Berlin git mirrors. (Implies `--mirror'). =item --oslo Switch to internal URLs and make use of the Oslo git mirrors. (Implies `--mirror'). =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 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 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 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 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 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 Uses 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 = ( '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 qtmacextras qtmultimedia qtpim qtqa qtquick1 qtquickcontrols qtrepotools qtscript qtsensors qtserialport qtsvg qtsystems qttools qttranslations qtwayland qtwebkit qtwebkit-examples qtwinextras 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 qtx11extras qtxmlpatterns ); my $GERRIT_SSH_BASE = 'ssh://@USER@codereview.qt-project.org@PORT@/'; my $BER_MIRROR_URL_BASE = 'git://hegel/'; my $OSLO_MIRROR_URL_BASE = 'git://qilin/'; 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' => "", '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 }}, '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'; }, 'berlin' => sub { $self->{'mirror-url'} = $BER_MIRROR_URL_BASE; }, 'oslo' => sub { $self->{'mirror-url'} = $OSLO_MIRROR_URL_BASE; }, ) || pod2usage(2); # 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'); 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) { # rewrite URL to chosen protocol $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;