1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** 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.
32 **************************************************************************/
34 #include "clonewizardpage.h"
35 #include "gitplugin.h"
36 #include "gitclient.h"
38 #include <vcsbase/checkoutjobs.h>
39 #include <utils/qtcassert.h>
41 #include <QtGui/QCheckBox>
45 struct CloneWizardPagePrivate {
46 CloneWizardPagePrivate();
48 bool urlIsLocal(const QString &url);
50 const QString mainLinePostfix;
51 const QString gitPostFix;
52 const QString protocolDelimiter;
53 QCheckBox *deleteMasterCheckBox;
57 CloneWizardPagePrivate::CloneWizardPagePrivate() :
58 mainLinePostfix(QLatin1String("/mainline.git")),
59 gitPostFix(QLatin1String(".git")),
60 protocolDelimiter(QLatin1String("://")),
61 deleteMasterCheckBox(0)
65 bool CloneWizardPagePrivate::urlIsLocal(const QString &url)
67 if (url.startsWith(QLatin1String("file://"))
68 || url.startsWith(QLatin1Char('/'))
69 || (url.at(0).isLetter() && url.at(1) == QChar(':') && url.at(2) == QChar('\\')))
74 CloneWizardPage::CloneWizardPage(QWidget *parent) :
75 VCSBase::BaseCheckoutWizardPage(parent),
76 d(new CloneWizardPagePrivate)
78 setTitle(tr("Location"));
79 setSubTitle(tr("Specify repository URL, checkout directory and path."));
80 setRepositoryLabel(tr("Clone URL:"));
81 d->deleteMasterCheckBox = new QCheckBox(tr("Delete master branch"));
82 d->deleteMasterCheckBox->setToolTip(tr("Delete the master branch after checking out the repository."));
83 addLocalControl(d->deleteMasterCheckBox);
84 setDeleteMasterBranch(true);
87 CloneWizardPage::~CloneWizardPage()
92 bool CloneWizardPage::deleteMasterBranch() const
94 return d->deleteMasterCheckBox->isChecked();
97 void CloneWizardPage::setDeleteMasterBranch(bool v)
99 d->deleteMasterCheckBox->setChecked(v);
102 QString CloneWizardPage::directoryFromRepository(const QString &urlIn) const
104 /* Try to figure out a good directory name from something like:
105 * 'user@host:qt/qt.git', 'http://host/qt/qt.git' 'local repo'
108 QString url = urlIn.trimmed().replace(QChar('\\'), QChar('/'));
110 const QChar slash = QLatin1Char('/');
112 const int protocolDelimiterPos = url.indexOf(d->protocolDelimiter); // "://"
113 const int startRepoSearchPos = protocolDelimiterPos == -1 ? 0 : protocolDelimiterPos + d->protocolDelimiter.size();
114 int repoPos = url.indexOf(QLatin1Char(':'), startRepoSearchPos);
116 repoPos = url.indexOf(slash, startRepoSearchPos);
118 url.remove(0, repoPos + 1);
120 if (url.endsWith(d->mainLinePostfix)) {
121 url.truncate(url.size() - d->mainLinePostfix.size());
123 if (url.endsWith(d->gitPostFix)) {
124 url.truncate(url.size() - d->gitPostFix.size());
127 // Check for equal parts, something like "qt/qt" -> "qt"
128 const int slashPos = url.indexOf(slash);
129 if (slashPos != -1 && slashPos == (url.size() - 1) / 2) {
130 if (url.leftRef(slashPos) == url.rightRef(slashPos))
131 url.truncate(slashPos);
133 // fix invalid characters
134 const QChar dash = QLatin1Char('-');
135 url.replace(QRegExp(QLatin1String("[^0-9a-zA-Z_.-]")), dash);
136 // trim leading dashes (they are annoying and get created when using local pathes)
137 url.replace(QRegExp(QLatin1String("^-+")), QString());
141 QSharedPointer<VCSBase::AbstractCheckoutJob> CloneWizardPage::createCheckoutJob(QString *checkoutPath) const
143 const Internal::GitClient *client = Internal::GitPlugin::instance()->gitClient();
144 const QString workingDirectory = path();
145 const QString checkoutDir = directory();
146 *checkoutPath = workingDirectory + QLatin1Char('/') + checkoutDir;
148 const QString binary = client->binary();
150 VCSBase::ProcessCheckoutJob *job = new VCSBase::ProcessCheckoutJob;
151 const QProcessEnvironment env = client->processEnvironment();
153 // 1) Basic checkout step
155 args << QLatin1String("clone") << repository() << checkoutDir;
156 job->addStep(binary, args, workingDirectory, env);
157 const QString checkoutBranch = branch();
159 // 2) Checkout branch, change to checkoutDir
160 if (!checkoutBranch.isEmpty() && checkoutBranch != d->headBranch) {
162 if (!d->urlIsLocal(repository())) {
164 args << QLatin1String("branch") << QLatin1String("--track")
165 << checkoutBranch << (QLatin1String("origin/") + checkoutBranch);
166 job->addStep(binary, args, *checkoutPath, env);
170 args << QLatin1String("checkout") << checkoutBranch;
171 job->addStep(binary, args, *checkoutPath, env);
172 if (deleteMasterBranch() && d->headBranch != QLatin1String("<detached HEAD>")) {
173 // Make sure we only have the requested branch:
175 args << QLatin1String("branch") << QLatin1String("-D") << d->headBranch;
177 job->addStep(binary, args, *checkoutPath, env);
180 return QSharedPointer<VCSBase::AbstractCheckoutJob>(job);
183 QStringList CloneWizardPage::branches(const QString &repository, int *current)
185 // Run git on remote repository if an URL was specified.
187 d->headBranch.clear();
189 if (repository.isEmpty())
190 return QStringList();
191 const QStringList branches = Internal::GitPlugin::instance()->gitClient()->synchronousRepositoryBranches(repository);
192 if (!branches.isEmpty()) {
193 *current = 0; // default branch is always returned first!
194 d->headBranch = branches.at(0);