Commit b49d715a authored by Friedemann Kleint's avatar Friedemann Kleint
Browse files

Added a gitorious clone wizard.

... based on the git clone wizard. Provide a wizard for browsing
gitorious hosts.

Task-number:  44831
parent 00f7dd45
......@@ -29,8 +29,6 @@
#include "clonewizard.h"
#include "clonewizardpage.h"
#include "gitplugin.h"
#include "gitclient.h"
#include <vcsbase/checkoutjobs.h>
#include <utils/qtcassert.h>
......@@ -75,18 +73,7 @@ QSharedPointer<VCSBase::AbstractCheckoutJob> CloneWizard::createJob(const QList<
// Collect parameters for the clone command.
const CloneWizardPage *cwp = qobject_cast<const CloneWizardPage *>(parameterPages.front());
QTC_ASSERT(cwp, return QSharedPointer<VCSBase::AbstractCheckoutJob>())
const GitClient *client = GitPlugin::instance()->gitClient();
QStringList args = client->binary();
const QString workingDirectory = cwp->path();
const QString directory = cwp->directory();
*checkoutPath = workingDirectory + QLatin1Char('/') + directory;
args << QLatin1String("clone") << cwp->repository() << directory;
const QString binary = args.front();
args.pop_front();
VCSBase::AbstractCheckoutJob *job = new VCSBase::ProcessCheckoutJob(binary, args, workingDirectory,
client->processEnvironment());
return QSharedPointer<VCSBase::AbstractCheckoutJob>(job);
return cwp->createCheckoutJob(checkoutPath);
}
} // namespace Internal
......
......@@ -28,20 +28,42 @@
**************************************************************************/
#include "clonewizardpage.h"
#include "gitplugin.h"
#include "gitclient.h"
#include <vcsbase/checkoutjobs.h>
#include <utils/qtcassert.h>
namespace Git {
namespace Internal {
struct CloneWizardPagePrivate {
CloneWizardPagePrivate();
const QString mainLinePostfix;
const QString gitPostFix;
const QString protocolDelimiter;
};
CloneWizardPagePrivate::CloneWizardPagePrivate() :
mainLinePostfix(QLatin1String("/mainline.git")),
gitPostFix(QLatin1String(".git")),
protocolDelimiter(QLatin1String("://"))
{
}
CloneWizardPage::CloneWizardPage(QWidget *parent) :
VCSBase::BaseCheckoutWizardPage(parent),
m_mainLinePostfix(QLatin1String("/mainline.git")),
m_gitPostFix(QLatin1String(".git")),
m_protocolDelimiter(QLatin1String("://"))
d(new CloneWizardPagePrivate)
{
setSubTitle(tr("Specify repository URL, checkout directory and path."));
setRepositoryLabel(tr("Clone URL:"));
}
CloneWizardPage::~CloneWizardPage()
{
delete d;
}
QString CloneWizardPage::directoryFromRepository(const QString &urlIn) const
{
/* Try to figure out a good directory name from something like:
......@@ -51,19 +73,19 @@ QString CloneWizardPage::directoryFromRepository(const QString &urlIn) const
QString url = urlIn.trimmed();
const QChar slash = QLatin1Char('/');
// remove host
const int protocolDelimiterPos = url.indexOf(m_protocolDelimiter); // "://"
const int startRepoSearchPos = protocolDelimiterPos == -1 ? 0 : protocolDelimiterPos + m_protocolDelimiter.size();
const int protocolDelimiterPos = url.indexOf(d->protocolDelimiter); // "://"
const int startRepoSearchPos = protocolDelimiterPos == -1 ? 0 : protocolDelimiterPos + d->protocolDelimiter.size();
int repoPos = url.indexOf(QLatin1Char(':'), startRepoSearchPos);
if (repoPos == -1)
repoPos = url.indexOf(slash, startRepoSearchPos);
if (repoPos != -1)
url.remove(0, repoPos + 1);
// Remove postfixes
if (url.endsWith(m_mainLinePostfix)) {
url.truncate(url.size() - m_mainLinePostfix.size());
if (url.endsWith(d->mainLinePostfix)) {
url.truncate(url.size() - d->mainLinePostfix.size());
} else {
if (url.endsWith(m_gitPostFix)) {
url.truncate(url.size() - m_gitPostFix.size());
if (url.endsWith(d->gitPostFix)) {
url.truncate(url.size() - d->gitPostFix.size());
}
}
// Check for equal parts, something like "qt/qt" -> "qt"
......@@ -79,5 +101,19 @@ QString CloneWizardPage::directoryFromRepository(const QString &urlIn) const
return url;
}
} // namespace Internal
QSharedPointer<VCSBase::AbstractCheckoutJob> CloneWizardPage::createCheckoutJob(QString *checkoutPath) const
{
const Internal::GitClient *client = Internal::GitPlugin::instance()->gitClient();
QStringList args = client->binary();
const QString workingDirectory = path();
const QString checkoutDir = directory();
*checkoutPath = workingDirectory + QLatin1Char('/') + checkoutDir;
args << QLatin1String("clone") << repository() << checkoutDir;
const QString binary = args.front();
args.pop_front();
VCSBase::AbstractCheckoutJob *job = new VCSBase::ProcessCheckoutJob(binary, args, workingDirectory,
client->processEnvironment());
return QSharedPointer<VCSBase::AbstractCheckoutJob>(job);
}
} // namespace Git
......@@ -32,25 +32,32 @@
#include <vcsbase/basecheckoutwizardpage.h>
#include <QtCore/QSharedPointer>
namespace VCSBase {
class AbstractCheckoutJob;
}
namespace Git {
namespace Internal {
struct CloneWizardPagePrivate;
// Used by gitorious as well.
class CloneWizardPage : public VCSBase::BaseCheckoutWizardPage
{
Q_OBJECT
public:
CloneWizardPage(QWidget *parent = 0);
explicit CloneWizardPage(QWidget *parent = 0);
virtual ~CloneWizardPage();
QSharedPointer<VCSBase::AbstractCheckoutJob> createCheckoutJob(QString *checkoutPath) const;
protected:
virtual QString directoryFromRepository(const QString &r) const;
private:
const QString m_mainLinePostfix;
const QString m_gitPostFix;
const QString m_protocolDelimiter;
CloneWizardPagePrivate *d;
};
} // namespace Internal
} // namespace Git
#endif // CLONEWIZARDPAGE_H
......@@ -47,3 +47,4 @@ FORMS += changeselectiondialog.ui \
branchdialog.ui
OTHER_FILES += ScmGit.pluginspec
include(gitorious/gitorious.pri)
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** 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
** contact the sales department at http://www.qtsoftware.com/contact.
**
**************************************************************************/
#include "gitorious.h"
#include <QtCore/QDebug>
#include <QtCore/QCoreApplication>
#include <QtCore/QXmlStreamReader>
#include <QtCore/QSettings>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
enum { debug = 0 };
enum Protocol { ListCategoriesProtocol, ListProjectsProtocol };
static const char *protocolPropertyC = "gitoriousProtocol";
static const char *hostNamePropertyC = "gitoriousHost";
static const char *pagePropertyC = "requestPage";
static const char *settingsKeyC = "GitoriousHosts";
// Gitorious paginates projects as 20 per page. It starts with page 1.
enum { ProjectsPageSize = 20 };
// Format an URL for a XML request
static inline QUrl xmlRequest(const QString &host, const QString &request, int page = -1)
{
QUrl url;
url.setScheme(QLatin1String("http"));
url.setHost(host);
url.setPath(QLatin1Char('/') + request);
url.addQueryItem(QLatin1String("format"), QLatin1String("xml"));
if (page >= 0)
url.addQueryItem(QLatin1String("page"), QString::number(page));
return url;
}
namespace Gitorious {
namespace Internal {
GitoriousRepository::GitoriousRepository() :
type(BaselineRepository),
id(0)
{
}
static inline GitoriousRepository::Type repositoryType(const QString &nspace)
{
if (nspace == QLatin1String("Repository::Namespace::BASELINE"))
return GitoriousRepository::BaselineRepository;
if (nspace == QLatin1String("Repository::Namespace::SHARED"))
return GitoriousRepository::SharedRepository;
if (nspace == QLatin1String("Repository::Namespace::PERSONAL"))
return GitoriousRepository::PersonalRepository;
return GitoriousRepository::BaselineRepository;
}
GitoriousCategory::GitoriousCategory(const QString &n) :
name(n)
{
}
GitoriousHost::GitoriousHost(const QString &h, const QString &d) :
hostName(h),
description(d),
state(ProjectsQueryRunning)
{
}
int GitoriousHost::findCategory(const QString &n) const
{
const int count = categories.size();
for (int i = 0; i < count; i++)
if (categories.at(i)->name == n)
return i;
return -1;
}
QDebug operator<<(QDebug d, const GitoriousRepository &r)
{
QDebug nospace = d.nospace();
nospace << "name=" << r.name << '/' << r.id << '/' << r.type << r.owner
<<" push=" << r.pushUrl << " clone=" << r.cloneUrl << " descr=" << r.description;
return d;
}
QDebug operator<<(QDebug d, const GitoriousProject &p)
{
QDebug nospace = d.nospace();
nospace << " project=" << p.name << " description=" << p.description << '\n';
foreach(const GitoriousRepository &r, p.repositories)
nospace << " " << r << '\n';
return d;
}
QDebug operator<<(QDebug d, const GitoriousCategory &c)
{
d.nospace() << " category=" << c.name << '\n';
return d;
}
QDebug operator<<(QDebug d, const GitoriousHost &h)
{
QDebug nospace = d.nospace();
nospace << " Host=" << h.hostName << " description=" << h.description << '\n';
foreach(const QSharedPointer<GitoriousCategory> &c, h.categories)
nospace << *c;
foreach(const QSharedPointer<GitoriousProject> &p, h.projects)
nospace << *p;
return d;
}
/* GitoriousProjectReader: Helper class for parsing project list output
* \code
projects...>
<project>
<bugtracker-url>
<created-at>
<description>... </description>
<home-url> (rarely set)
<license>
<mailinglist-url>
<slug> (name)
<title>MuleFTW</title>
<owner>
<repositories>
<mainlines> // Optional
<repository>
<id>
<name>
<owner>
<clone_url>
</repository>
</mainlines>
<clones> // Optional
</clones>
</repositories>
</project>
* \endcode */
class GitoriousProjectReader
{
Q_DISABLE_COPY(GitoriousProjectReader)
public:
typedef GitoriousCategory::ProjectList ProjectList;
GitoriousProjectReader();
ProjectList read(const QByteArray &a, QString *errorMessage);
private:
void readProjects(QXmlStreamReader &r);
QSharedPointer<GitoriousProject> readProject(QXmlStreamReader &r);
QList<GitoriousRepository> readRepositories(QXmlStreamReader &r);
GitoriousRepository readRepository(QXmlStreamReader &r, int defaultType = -1);
void readUnknownElement(QXmlStreamReader &r);
const QString m_mainLinesElement;
const QString m_clonesElement;
ProjectList m_projects;
};
GitoriousProjectReader::GitoriousProjectReader() :
m_mainLinesElement(QLatin1String("mainlines")),
m_clonesElement(QLatin1String("clones"))
{
}
GitoriousProjectReader::ProjectList GitoriousProjectReader::read(const QByteArray &a, QString *errorMessage)
{
m_projects.clear();
QXmlStreamReader reader(a);
while (!reader.atEnd()) {
reader.readNext();
if (reader.isStartElement()) {
if (reader.name() == QLatin1String("projects")) {
readProjects(reader);
} else {
readUnknownElement(reader);
}
}
}
if (reader.hasError()) {
*errorMessage = QString::fromLatin1("Error at %1:%2: %3").arg(reader.lineNumber()).arg(reader.columnNumber()).arg(reader.errorString());
m_projects.clear();
}
return m_projects;
}
bool gitoriousProjectLessThan(const QSharedPointer<GitoriousProject> &p1, const QSharedPointer<GitoriousProject> &p2)
{
return p1->name.compare(p2->name, Qt::CaseInsensitive) < 0;
}
void GitoriousProjectReader::readProjects(QXmlStreamReader &reader)
{
while (!reader.atEnd()) {
reader.readNext();
if (reader.isEndElement())
break;
if (reader.isStartElement()) {
if (reader.name() == "project") {
const QSharedPointer<GitoriousProject> p = readProject(reader);
if (!p->name.isEmpty())
m_projects.push_back(p);
} else {
readUnknownElement(reader);
}
}
}
}
QSharedPointer<GitoriousProject> GitoriousProjectReader::readProject(QXmlStreamReader &reader)
{
QSharedPointer<GitoriousProject> project(new GitoriousProject);
while (!reader.atEnd()) {
reader.readNext();
if (reader.isEndElement())
break;
if (reader.isStartElement()) {
const QStringRef name = reader.name();
if (name == QLatin1String("description")) {
project->description = reader.readElementText();
} else if (name == QLatin1String("title")) {
project->name = reader.readElementText();
} else if (name == QLatin1String("slug") && project->name.isEmpty()) {
project->name = reader.readElementText();
} else if (name == QLatin1String("repositories")) {
project->repositories = readRepositories(reader);
} else {
readUnknownElement(reader);
}
}
}
return project;
}
QList<GitoriousRepository> GitoriousProjectReader::readRepositories(QXmlStreamReader &reader)
{
QList<GitoriousRepository> repositories;
int defaultType = -1;
// The "mainlines"/"clones" elements are not used in the
// Nokia setup, handle them optionally.
while (!reader.atEnd()) {
reader.readNext();
if (reader.isEndElement()) {
const QStringRef name = reader.name();
if (name == m_mainLinesElement || name == m_clonesElement) {
defaultType = -1;
} else {
break;
}
}
if (reader.isStartElement()) {
const QStringRef name = reader.name();
if (reader.name() == QLatin1String("repository")) {
repositories.push_back(readRepository(reader, defaultType));
} else if (name == m_mainLinesElement) {
defaultType = GitoriousRepository::MainLineRepository;
} else if (name == m_clonesElement) {
defaultType = GitoriousRepository::CloneRepository;
} else {
readUnknownElement(reader);
}
}
}
return repositories;
}
GitoriousRepository GitoriousProjectReader::readRepository(QXmlStreamReader &reader, int defaultType)
{
GitoriousRepository repository;
if (defaultType >= 0)
repository.type = static_cast<GitoriousRepository::Type>(defaultType);
while (!reader.atEnd()) {
reader.readNext();
if (reader.isEndElement())
break;
if (reader.isStartElement()) {
const QStringRef name = reader.name();
if (name == QLatin1String("name")) {
repository.name = reader.readElementText();
} else if (name == QLatin1String("owner")) {
repository.owner = reader.readElementText();
} else if (name == QLatin1String("id")) {
repository.id = reader.readElementText().toInt();
} else if (name == QLatin1String("description")) {
repository.description = reader.readElementText();
} else if (name == QLatin1String("push_url")) {
repository.pushUrl = reader.readElementText();
} else if (name == QLatin1String("clone_url")) {
repository.cloneUrl = reader.readElementText();
} else if (name == QLatin1String("namespace")) {
repository.type = repositoryType(reader.readElementText());
} else {
readUnknownElement(reader);
}
}
}
return repository;
}
void GitoriousProjectReader::readUnknownElement(QXmlStreamReader &reader)
{
Q_ASSERT(reader.isStartElement());
while (!reader.atEnd()) {
reader.readNext();
if (reader.isEndElement())
break;
if (reader.isStartElement())
readUnknownElement(reader);
}
}
// --- Gitorious
Gitorious::Gitorious() :
m_networkManager(0)
{
}
Gitorious &Gitorious::instance()
{
static Gitorious gitorious;
return gitorious;
}
void Gitorious::emitError(const QString &e)
{
qWarning("%s\n", qPrintable(e));
emit error(e);
}
void Gitorious::addHost(const QString &addr, const QString &description)
{
addHost(GitoriousHost(addr, description));
}
void Gitorious::addHost(const GitoriousHost &host)
{
if (debug)
qDebug() << host;
const int index = m_hosts.size();
m_hosts.push_back(host);
if (host.categories.empty()) {
updateCategories(index);
m_hosts.back().state = GitoriousHost::ProjectsQueryRunning;
} else {
m_hosts.back().state = GitoriousHost::ProjectsComplete;
}
if (host.projects.empty())
updateProjectList(index);
emit hostAdded(index);
}
void Gitorious::removeAt(int index)
{
m_hosts.removeAt(index);
emit hostRemoved(index);
}
int Gitorious::findByHostName(const QString &hostName) const
{
const int size = m_hosts.size();
for (int i = 0; i < size; i++)
if (m_hosts.at(i).hostName == hostName)
return i;
return -1;
}
void Gitorious::setHostDescription(int index, const QString &s)
{
m_hosts[index].description = s;
}
QString Gitorious::hostDescription(int index) const
{
return m_hosts.at(index).description;
}
void Gitorious::listCategoriesReply(int index, QByteArray dataB)
{
/* For now, parse the HTML of the projects site for "Popular Categories":
* \code
* <h4>Popular Categories:</h4>
* <ul class="...">