clonewizardpage.cpp 6.81 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
27
28
29
30
**
**************************************************************************/

#include "clonewizardpage.h"
31
32
33
34
35
#include "gitplugin.h"
#include "gitclient.h"

#include <vcsbase/checkoutjobs.h>
#include <utils/qtcassert.h>
36

37
38
#include <QtGui/QCheckBox>

39
namespace Git {
40
41
42
43

struct CloneWizardPagePrivate {
    CloneWizardPagePrivate();

44
45
    bool urlIsLocal(const QString &url);

46
47
48
    const QString mainLinePostfix;
    const QString gitPostFix;
    const QString protocolDelimiter;
49
    QCheckBox *deleteMasterCheckBox;
Tobias Hunger's avatar
Tobias Hunger committed
50
    QString headBranch;
51
52
53
54
55
};

CloneWizardPagePrivate::CloneWizardPagePrivate() :
    mainLinePostfix(QLatin1String("/mainline.git")),
    gitPostFix(QLatin1String(".git")),
56
57
    protocolDelimiter(QLatin1String("://")),
    deleteMasterCheckBox(0)
58
59
{
}
60

61
62
63
64
bool CloneWizardPagePrivate::urlIsLocal(const QString &url)
{
    if (url.startsWith(QLatin1String("file://"))
        || url.startsWith(QLatin1Char('/'))
65
        || (url.at(0).isLetter() && url.at(1) == QChar(':') && url.at(2) == QChar('\\')))
66
67
68
69
        return true;
    return false;
}

70
71
CloneWizardPage::CloneWizardPage(QWidget *parent) :
    VCSBase::BaseCheckoutWizardPage(parent),
72
    d(new CloneWizardPagePrivate)
73
{
74
    setTitle(tr("Location"));
75
76
    setSubTitle(tr("Specify repository URL, checkout directory and path."));
    setRepositoryLabel(tr("Clone URL:"));
77
    d->deleteMasterCheckBox = new QCheckBox(tr("Delete master branch"));
78
79
    d->deleteMasterCheckBox->setToolTip(tr("Delete the master branch after checking out the repository."));
    addLocalControl(d->deleteMasterCheckBox);
80
    setDeleteMasterBranch(true);
81
82
}

83
84
85
86
87
CloneWizardPage::~CloneWizardPage()
{
    delete d;
}

88
89
90
91
92
93
94
95
96
97
bool CloneWizardPage::deleteMasterBranch() const
{
    return d->deleteMasterCheckBox->isChecked();
}

void CloneWizardPage::setDeleteMasterBranch(bool v)
{
    d->deleteMasterCheckBox->setChecked(v);
}

98
99
100
101
102
103
QString CloneWizardPage::directoryFromRepository(const QString &urlIn) const
{
    /* Try to figure out a good directory name from something like:
     * 'user@host:qt/qt.git', 'http://host/qt/qt.git' 'local repo'
     * ------> 'qt' .  */

104
105
    QString url = urlIn.trimmed().replace(QChar('\\'), QChar('/'));

106
107
    const QChar slash = QLatin1Char('/');
    // remove host
108
109
    const int protocolDelimiterPos = url.indexOf(d->protocolDelimiter); // "://"
    const int startRepoSearchPos = protocolDelimiterPos == -1 ? 0 : protocolDelimiterPos + d->protocolDelimiter.size();
110
111
112
113
114
115
    int repoPos = url.indexOf(QLatin1Char(':'), startRepoSearchPos);
    if (repoPos == -1)
        repoPos = url.indexOf(slash, startRepoSearchPos);
    if (repoPos != -1)
        url.remove(0, repoPos + 1);
    // Remove postfixes
116
117
    if (url.endsWith(d->mainLinePostfix)) {
        url.truncate(url.size() - d->mainLinePostfix.size());
118
    } else {
119
120
        if (url.endsWith(d->gitPostFix)) {
            url.truncate(url.size() - d->gitPostFix.size());
121
122
123
124
125
126
127
128
129
130
        }
    }
    // Check for equal parts, something like "qt/qt" -> "qt"
    const int slashPos = url.indexOf(slash);
    if (slashPos != -1 && slashPos == (url.size() - 1) / 2) {
        if (url.leftRef(slashPos) == url.rightRef(slashPos))
            url.truncate(slashPos);
    }
    // fix invalid characters
    const QChar dash = QLatin1Char('-');
131
132
133
    url.replace(QRegExp(QLatin1String("[^0-9a-zA-Z_-]")), dash);
    // trim leading dashes (they are annoying and get created when using local pathes)
    url.replace(QRegExp(QLatin1String("^-+")), QString());
134
135
136
    return url;
}

137
138
139
140
141
142
QSharedPointer<VCSBase::AbstractCheckoutJob> CloneWizardPage::createCheckoutJob(QString *checkoutPath) const
{
     const Internal::GitClient *client = Internal::GitPlugin::instance()->gitClient();
     const QString workingDirectory = path();
     const QString checkoutDir = directory();
     *checkoutPath = workingDirectory + QLatin1Char('/') + checkoutDir;
143

144
     const QString binary = client->binary();
145
146
147

     VCSBase::ProcessCheckoutJob *job = new VCSBase::ProcessCheckoutJob;
     const QProcessEnvironment env = client->processEnvironment();
Tobias Hunger's avatar
Tobias Hunger committed
148

149
     // 1) Basic checkout step
Tobias Hunger's avatar
Tobias Hunger committed
150
151
     QStringList args;
     args << QLatin1String("clone") << repository() << checkoutDir;
152
     job->addStep(binary, args, workingDirectory, env);
153
154
155
     const QString checkoutBranch = branch();

     // 2) Checkout branch, change to checkoutDir
Tobias Hunger's avatar
Tobias Hunger committed
156
     if (!checkoutBranch.isEmpty() && checkoutBranch != d->headBranch) {
157
         // Create branch
158
159
160
161
162
163
         if (!d->urlIsLocal(repository())) {
             args.clear();
             args << QLatin1String("branch") << QLatin1String("--track")
                  << checkoutBranch << (QLatin1String("origin/")  + checkoutBranch);
             job->addStep(binary, args, *checkoutPath, env);
         }
164
165
166
         // Checkout branch
         args.clear();
         args << QLatin1String("checkout") << checkoutBranch;
167
         job->addStep(binary, args, *checkoutPath, env);
Tobias Hunger's avatar
Tobias Hunger committed
168
169
         if (deleteMasterBranch() && d->headBranch != QLatin1String("<detached HEAD>")) {
             // Make sure we only have the requested branch:
170
             args.clear();
Tobias Hunger's avatar
Tobias Hunger committed
171
             args << QLatin1String("branch") << QLatin1String("-D") << d->headBranch;
172
         }
Tobias Hunger's avatar
Tobias Hunger committed
173
         job->addStep(binary, args, *checkoutPath, env);
174
175
     }

176
177
178
     return QSharedPointer<VCSBase::AbstractCheckoutJob>(job);
}

179
180
QStringList CloneWizardPage::branches(const QString &repository, int *current)
{
181
    // Run git on remote repository if an URL was specified.
Tobias Hunger's avatar
Tobias Hunger committed
182
183
184
    *current = -1;
    d->headBranch.clear();

185
    if (repository.isEmpty())
186
187
        return QStringList();
     const QStringList branches = Internal::GitPlugin::instance()->gitClient()->synchronousRepositoryBranches(repository);
Tobias Hunger's avatar
Tobias Hunger committed
188
189
190
191
     if (!branches.isEmpty()) {
         *current = 0; // default branch is always returned first!
         d->headBranch = branches.at(0);
     }
192
193
194
     return branches;
}

195
} // namespace Git