OSDN Git Service

629f1579e425cf0ae87aaa0b48ca75915fcc93fc
[qt-creator-jp/qt-creator-jp.git] / src / plugins / git / clonewizardpage.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "clonewizardpage.h"
35 #include "gitplugin.h"
36 #include "gitclient.h"
37
38 #include <vcsbase/checkoutjobs.h>
39 #include <utils/qtcassert.h>
40
41 #include <QtGui/QCheckBox>
42
43 namespace Git {
44
45 struct CloneWizardPagePrivate {
46     CloneWizardPagePrivate();
47
48     bool urlIsLocal(const QString &url);
49
50     const QString mainLinePostfix;
51     const QString gitPostFix;
52     const QString protocolDelimiter;
53     QCheckBox *deleteMasterCheckBox;
54     QString headBranch;
55 };
56
57 CloneWizardPagePrivate::CloneWizardPagePrivate() :
58     mainLinePostfix(QLatin1String("/mainline.git")),
59     gitPostFix(QLatin1String(".git")),
60     protocolDelimiter(QLatin1String("://")),
61     deleteMasterCheckBox(0)
62 {
63 }
64
65 bool CloneWizardPagePrivate::urlIsLocal(const QString &url)
66 {
67     if (url.startsWith(QLatin1String("file://"))
68         || url.startsWith(QLatin1Char('/'))
69         || (url.at(0).isLetter() && url.at(1) == QChar(':') && url.at(2) == QChar('\\')))
70         return true;
71     return false;
72 }
73
74 CloneWizardPage::CloneWizardPage(QWidget *parent) :
75     VCSBase::BaseCheckoutWizardPage(parent),
76     d(new CloneWizardPagePrivate)
77 {
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);
85 }
86
87 CloneWizardPage::~CloneWizardPage()
88 {
89     delete d;
90 }
91
92 bool CloneWizardPage::deleteMasterBranch() const
93 {
94     return d->deleteMasterCheckBox->isChecked();
95 }
96
97 void CloneWizardPage::setDeleteMasterBranch(bool v)
98 {
99     d->deleteMasterCheckBox->setChecked(v);
100 }
101
102 QString CloneWizardPage::directoryFromRepository(const QString &urlIn) const
103 {
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'
106      * ------> 'qt' .  */
107
108     QString url = urlIn.trimmed().replace(QChar('\\'), QChar('/'));
109
110     const QChar slash = QLatin1Char('/');
111     // remove host
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);
115     if (repoPos == -1)
116         repoPos = url.indexOf(slash, startRepoSearchPos);
117     if (repoPos != -1)
118         url.remove(0, repoPos + 1);
119     // Remove postfixes
120     if (url.endsWith(d->mainLinePostfix)) {
121         url.truncate(url.size() - d->mainLinePostfix.size());
122     } else {
123         if (url.endsWith(d->gitPostFix)) {
124             url.truncate(url.size() - d->gitPostFix.size());
125         }
126     }
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);
132     }
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());
138     return url;
139 }
140
141 QSharedPointer<VCSBase::AbstractCheckoutJob> CloneWizardPage::createCheckoutJob(QString *checkoutPath) const
142 {
143      const Internal::GitClient *client = Internal::GitPlugin::instance()->gitClient();
144      const QString workingDirectory = path();
145      const QString checkoutDir = directory();
146      *checkoutPath = workingDirectory + QLatin1Char('/') + checkoutDir;
147
148      const QString binary = client->binary();
149
150      VCSBase::ProcessCheckoutJob *job = new VCSBase::ProcessCheckoutJob;
151      const QProcessEnvironment env = client->processEnvironment();
152
153      // 1) Basic checkout step
154      QStringList args;
155      args << QLatin1String("clone") << repository() << checkoutDir;
156      job->addStep(binary, args, workingDirectory, env);
157      const QString checkoutBranch = branch();
158
159      // 2) Checkout branch, change to checkoutDir
160      if (!checkoutBranch.isEmpty() && checkoutBranch != d->headBranch) {
161          // Create branch
162          if (!d->urlIsLocal(repository())) {
163              args.clear();
164              args << QLatin1String("branch") << QLatin1String("--track")
165                   << checkoutBranch << (QLatin1String("origin/")  + checkoutBranch);
166              job->addStep(binary, args, *checkoutPath, env);
167          }
168          // Checkout branch
169          args.clear();
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:
174              args.clear();
175              args << QLatin1String("branch") << QLatin1String("-D") << d->headBranch;
176          }
177          job->addStep(binary, args, *checkoutPath, env);
178      }
179
180      return QSharedPointer<VCSBase::AbstractCheckoutJob>(job);
181 }
182
183 QStringList CloneWizardPage::branches(const QString &repository, int *current)
184 {
185     // Run git on remote repository if an URL was specified.
186     *current = -1;
187     d->headBranch.clear();
188
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);
195      }
196      return branches;
197 }
198
199 } // namespace Git