Commit 0f4808a6 authored by jkobus's avatar jkobus Committed by Jarek Kobus

Use VcsBaseClient::diff instead of svn's one

The first step in cleaning a mess in VCS

Change-Id: I06ee1d9952cc91dc8f599f906d221850ffb0835b
Reviewed-by: default avatarJarek Kobus <jaroslaw.kobus@digia.com>
parent 8474a6f1
......@@ -30,6 +30,7 @@
#include "checkoutwizard.h"
#include "checkoutwizardpage.h"
#include "subversionplugin.h"
#include "subversionclient.h"
#include <coreplugin/iversioncontrol.h>
#include <vcsbase/command.h>
......@@ -78,7 +79,7 @@ VcsBase::Command *CheckoutWizard::createCommand(const QList<QWizardPage*> &param
if (settings.hasAuthentication()) {
const QString user = settings.stringValue(SubversionSettings::userKey);
const QString pwd = settings.stringValue(SubversionSettings::passwordKey);
args = SubversionPlugin::addAuthenticationOptions(args, user, pwd);
args = SubversionClient::addAuthenticationOptions(args, user, pwd);
}
VcsBase::Command *command = new VcsBase::Command(binary, workingDirectory,
QProcessEnvironment::systemEnvironment());
......
......@@ -2,6 +2,7 @@ include(../../qtcreatorplugin.pri)
HEADERS += annotationhighlighter.h \
subversionplugin.h \
subversionclient.h \
subversioncontrol.h \
settingspage.h \
subversioneditor.h \
......@@ -13,6 +14,7 @@ HEADERS += annotationhighlighter.h \
SOURCES += annotationhighlighter.cpp \
subversionplugin.cpp \
subversionclient.cpp \
subversioncontrol.cpp \
settingspage.cpp \
subversioneditor.cpp \
......
......@@ -23,6 +23,8 @@ QtcPlugin {
"settingspage.h",
"settingspage.ui",
"subversion.qrc",
"subversionclient.cpp",
"subversionclient.h",
"subversionconstants.h",
"subversioncontrol.cpp",
"subversioncontrol.h",
......
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** 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.
**
****************************************************************************/
#include "subversionclient.h"
#include "subversionsettings.h"
#include <vcsbase/vcsbaseplugin.h>
#include <vcsbase/vcsbaseconstants.h>
#include <vcsbase/vcsbaseeditorparameterwidget.h>
#include <utils/synchronousprocess.h>
#include <QDir>
#include <QFileInfo>
#include <QTextStream>
#include <QDebug>
namespace Subversion {
namespace Internal {
// Collect all parameters required for a diff to be able to associate them
// with a diff editor and re-run the diff with parameters.
struct SubversionDiffParameters
{
QString workingDir;
QStringList extraOptions;
QStringList files;
};
// Parameter widget controlling whitespace diff mode, associated with a parameter
class SubversionDiffParameterWidget : public VcsBase::VcsBaseEditorParameterWidget
{
Q_OBJECT
public:
explicit SubversionDiffParameterWidget(SubversionClient *client,
const SubversionDiffParameters &p,
QWidget *parent = 0);
QStringList arguments() const;
void executeCommand();
private:
SubversionClient *m_client;
const SubversionDiffParameters m_params;
};
SubversionDiffParameterWidget::SubversionDiffParameterWidget(SubversionClient *client,
const SubversionDiffParameters &p,
QWidget *parent)
: VcsBase::VcsBaseEditorParameterWidget(parent), m_client(client), m_params(p)
{
mapSetting(addToggleButton(QLatin1String("w"), tr("Ignore Whitespace")),
client->settings()->boolPointer(SubversionSettings::diffIgnoreWhiteSpaceKey));
}
QStringList SubversionDiffParameterWidget::arguments() const
{
QStringList args;
// Subversion wants" -x -<ext-args>", default being -u
const QStringList formatArguments = VcsBaseEditorParameterWidget::arguments();
if (!formatArguments.isEmpty()) {
args << QLatin1String("-x")
<< (QLatin1String("-u") + formatArguments.join(QString()));
}
return args;
}
void SubversionDiffParameterWidget::executeCommand()
{
m_client->diff(m_params.workingDir, m_params.files, m_params.extraOptions);
}
SubversionClient::SubversionClient(SubversionSettings *settings) :
VcsBase::VcsBaseClient(settings)
{
}
SubversionSettings *SubversionClient::settings() const
{
return dynamic_cast<SubversionSettings *>(VcsBase::VcsBaseClient::settings());
}
Core::Id SubversionClient::vcsEditorKind(VcsCommand cmd) const
{
switch (cmd) {
case DiffCommand:
return "Subversion Diff Editor"; // TODO: create subversionconstants.h
default:
return Core::Id();
}
}
SubversionClient::Version SubversionClient::svnVersion()
{
if (m_svnVersionBinary != settings()->binaryPath()) {
QStringList args;
args << QLatin1String("--version") << QLatin1String("-q");
const Utils::SynchronousProcessResponse response =
VcsBase::VcsBasePlugin::runVcs(QDir().absolutePath(), settings()->binaryPath(),
args, settings()->timeOutMs());
if (response.result == Utils::SynchronousProcessResponse::Finished &&
response.exitCode == 0) {
m_svnVersionBinary = settings()->binaryPath();
m_svnVersion = response.stdOut.trimmed();
} else {
m_svnVersionBinary.clear();
m_svnVersion.clear();
}
}
SubversionClient::Version v;
if (::sscanf(m_svnVersion.toLatin1().constData(), "%d.%d.%d",
&v.majorVersion, &v.minorVersion, &v.patchVersion) != 3) {
v.majorVersion = v.minorVersion = v.patchVersion = -1;
}
return v;
}
// Add authorization options to the command line arguments.
// SVN pre 1.5 does not accept "--userName" for "add", which is most likely
// an oversight. As no password is needed for the option, generally omit it.
QStringList SubversionClient::addAuthenticationOptions(const QStringList &args,
const QString &userName,
const QString &password)
{
if (userName.isEmpty())
return args;
if (!args.empty() && args.front() == QLatin1String("add"))
return args;
QStringList rc;
rc.push_back(QLatin1String("--username"));
rc.push_back(userName);
if (!password.isEmpty()) {
rc.push_back(QLatin1String("--password"));
rc.push_back(password);
}
rc.append(args);
return rc;
}
void SubversionClient::diff(const QString &workingDir, const QStringList &files,
const QStringList &extraOptions)
{
QStringList args(extraOptions);
Version v = svnVersion();
// --internal-diff is new in v1.7.0
if (v.majorVersion > 1 || (v.majorVersion == 1 && v.minorVersion >= 7))
args.append(QLatin1String("--internal-diff"));
const bool hasAuth = settings()->hasAuthentication();
const QString userName = hasAuth ? settings()->stringValue(SubversionSettings::userKey) : QString();
const QString password = hasAuth ? settings()->stringValue(SubversionSettings::passwordKey) : QString();
args = addAuthenticationOptions(args, userName, password);
VcsBaseClient::diff(workingDir, files, args);
}
QString SubversionClient::findTopLevelForFile(const QFileInfo &file) const
{
Q_UNUSED(file)
return QString();
}
QStringList SubversionClient::revisionSpec(const QString &revision) const
{
Q_UNUSED(revision)
return QStringList();
}
VcsBase::VcsBaseClient::StatusItem SubversionClient::parseStatusLine(const QString &line) const
{
Q_UNUSED(line)
return VcsBase::VcsBaseClient::StatusItem();
}
VcsBase::VcsBaseEditorParameterWidget *SubversionClient::createDiffEditor(
const QString &workingDir, const QStringList &files, const QStringList &extraOptions)
{
Q_UNUSED(extraOptions)
SubversionDiffParameters p;
p.workingDir = workingDir;
p.files = files;
p.extraOptions = extraOptions;
return new SubversionDiffParameterWidget(this, p);
}
} // namespace Internal
} // namespace Subversion
#include "subversionclient.moc"
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** 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.
**
****************************************************************************/
#ifndef SUBVERSIONCLIENT_H
#define SUBVERSIONCLIENT_H
#include "subversionsettings.h"
#include <vcsbase/vcsbaseclient.h>
namespace Subversion {
namespace Internal {
class SubversionSettings;
class SubversionClient : public VcsBase::VcsBaseClient
{
Q_OBJECT
public:
SubversionClient(SubversionSettings *settings);
SubversionSettings *settings() const;
void diff(const QString &workingDir, const QStringList &files,
const QStringList &extraOptions = QStringList());
QString findTopLevelForFile(const QFileInfo &file) const;
QStringList revisionSpec(const QString &revision) const;
StatusItem parseStatusLine(const QString &line) const;
class Version {
public:
int majorVersion;
int minorVersion;
int patchVersion;
};
Version svnVersion();
// Add authorization options to the command line arguments.
static QStringList addAuthenticationOptions(const QStringList &args,
const QString &userName = QString(),
const QString &password = QString());
protected:
Core::Id vcsEditorKind(VcsCommand cmd) const;
VcsBase::VcsBaseEditorParameterWidget *createDiffEditor(const QString &workingDir,
const QStringList &files,
const QStringList &extraOptions);
private:
QString m_svnVersionBinary;
QString m_svnVersion;
};
} // namespace Internal
} // namespace Subversion
#endif // SUBVERSIONCLIENT_H
......@@ -33,6 +33,7 @@
#include "subversioneditor.h"
#include "subversionsubmiteditor.h"
#include "subversionclient.h"
#include "subversionconstants.h"
#include "subversioncontrol.h"
#include "checkoutwizard.h"
......@@ -209,6 +210,7 @@ SubversionPlugin::SubversionPlugin() :
SubversionPlugin::~SubversionPlugin()
{
delete m_client;
cleanCommitMessageFile();
}
......@@ -251,6 +253,7 @@ bool SubversionPlugin::initialize(const QStringList & /*arguments */, QString *e
return false;
m_settings.readSettings(Core::ICore::settings());
m_client = new SubversionClient(&m_settings);
addAutoReleasedObject(new SettingsPage);
......@@ -497,52 +500,7 @@ bool SubversionPlugin::submitEditorAboutToClose()
void SubversionPlugin::diffCommitFiles(const QStringList &files)
{
svnDiff(m_commitRepository, files);
}
// Collect all parameters required for a diff to be able to associate them
// with a diff editor and re-run the diff with parameters.
struct SubversionDiffParameters
{
QString workingDir;
QStringList arguments;
QStringList files;
QString diffName;
};
// Parameter widget controlling whitespace diff mode, associated with a parameter
class SubversionDiffParameterWidget : public VcsBase::VcsBaseEditorParameterWidget
{
Q_OBJECT
public:
explicit SubversionDiffParameterWidget(const SubversionDiffParameters &p, QWidget *parent = 0);
signals:
void reRunDiff(const Subversion::Internal::SubversionDiffParameters &);
private slots:
void triggerReRun();
private:
const SubversionDiffParameters m_parameters;
};
SubversionDiffParameterWidget::SubversionDiffParameterWidget(const SubversionDiffParameters &p, QWidget *parent) :
VcsBase::VcsBaseEditorParameterWidget(parent), m_parameters(p)
{
setBaseArguments(p.arguments);
addToggleButton(QLatin1String("w"), tr("Ignore Whitespace"));
connect(this, SIGNAL(argumentsChanged()), this, SLOT(triggerReRun()));
}
void SubversionDiffParameterWidget::triggerReRun()
{
SubversionDiffParameters effectiveParameters = m_parameters;
// Subversion wants" -x -<ext-args>", default being -u
const QStringList a = arguments();
if (!a.isEmpty())
effectiveParameters.arguments << QLatin1String("-x") << (QLatin1String("-u") + a.join(QString()));
emit reRunDiff(effectiveParameters);
m_client->diff(m_commitRepository, files);
}
static inline void setWorkingDirectory(Core::IEditor *editor, const QString &wd)
......@@ -551,65 +509,6 @@ static inline void setWorkingDirectory(Core::IEditor *editor, const QString &wd)
ve->setWorkingDirectory(wd);
}
void SubversionPlugin::svnDiff(const QString &workingDir, const QStringList &files, QString diffname)
{
SubversionDiffParameters p;
p.workingDir = workingDir;
p.files = files;
p.diffName = diffname;
svnDiff(p);
}
void SubversionPlugin::svnDiff(const Subversion::Internal::SubversionDiffParameters &p)
{
if (Subversion::Constants::debug)
qDebug() << Q_FUNC_INFO << p.files << p.diffName;
const QString source = VcsBase::VcsBaseEditorWidget::getSource(p.workingDir, p.files);
QTextCodec *codec = source.isEmpty() ? static_cast<QTextCodec *>(0) : VcsBase::VcsBaseEditorWidget::getCodec(source);
const QString diffName = p.files.count() == 1 && p.diffName.isEmpty() ?
QFileInfo(p.files.front()).fileName() : p.diffName;
QStringList args(QLatin1String("diff"));
Version v = svnVersion();
if (v.majorVersion > 1
|| (v.majorVersion == 1 && v.minorVersion >= 7)) // --internal-diff is new in v1.7.0
args.append(QLatin1String("--internal-diff"));
args.append(p.arguments);
args << p.files;
const SubversionResponse response =
runSvn(p.workingDir, args, m_settings.timeOutMs(), 0, codec);
if (response.error)
return;
// diff of a single file? re-use an existing view if possible to support
// the common usage pattern of continuously changing and diffing a file
const QString tag = VcsBase::VcsBaseEditorWidget::editorTag(VcsBase::DiffOutput, p.workingDir, p.files);
// Show in the same editor if diff has been executed before
if (Core::IEditor *existingEditor = VcsBase::VcsBaseEditorWidget::locateEditorByTag(tag)) {
existingEditor->document()->setContents(response.stdOut.toUtf8());
Core::EditorManager::activateEditor(existingEditor);
setWorkingDirectory(existingEditor, p.workingDir);
return;
}
const QString title = QString::fromLatin1("svn diff %1").arg(diffName);
Core::IEditor *editor = showOutputInEditor(title, response.stdOut, VcsBase::DiffOutput, source, codec);
setWorkingDirectory(editor, p.workingDir);
VcsBase::VcsBaseEditorWidget::tagEditor(editor, tag);
SubversionEditor *diffEditorWidget = qobject_cast<SubversionEditor *>(editor->widget());
QTC_ASSERT(diffEditorWidget, return);
// Wire up the parameter widget to trigger a re-run on
// parameter change and 'revert' from inside the diff editor.
SubversionDiffParameterWidget *pw = new SubversionDiffParameterWidget(p);
connect(pw, SIGNAL(reRunDiff(Subversion::Internal::SubversionDiffParameters)),
this, SLOT(svnDiff(Subversion::Internal::SubversionDiffParameters)));
connect(diffEditorWidget, SIGNAL(diffChunkReverted(VcsBase::DiffChunk)),
pw, SLOT(triggerReRun()));
diffEditorWidget->setConfigurationWidget(pw);
}
SubversionSubmitEditor *SubversionPlugin::openSubversionSubmitEditor(const QString &fileName)
{
Core::IEditor *editor = Core::EditorManager::openEditor(fileName,
......@@ -724,15 +623,16 @@ void SubversionPlugin::diffProject()
{
const VcsBase::VcsBasePluginState state = currentState();
QTC_ASSERT(state.hasProject(), return);
svnDiff(state.currentProjectTopLevel(), QStringList(state.relativeCurrentProject()),
state.currentProjectName());
const QString relativeProject = state.relativeCurrentProject();
m_client->diff(state.currentProjectTopLevel(),
relativeProject.isEmpty() ? QStringList() : QStringList(relativeProject));
}
void SubversionPlugin::diffCurrentFile()
{
const VcsBase::VcsBasePluginState state = currentState();
QTC_ASSERT(state.hasFile(), return);
svnDiff(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()));
m_client->diff(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()));
}
void SubversionPlugin::startCommitCurrentFile()
......@@ -842,7 +742,7 @@ void SubversionPlugin::diffRepository()
{
const VcsBase::VcsBasePluginState state = currentState();
QTC_ASSERT(state.hasTopLevel(), return);
svnDiff(state.topLevel(), QStringList());
m_client->diff(state.topLevel(), QStringList());
}
void SubversionPlugin::statusRepository()
......@@ -1092,53 +992,6 @@ SubversionResponse
arguments, timeOut, flags, outputCodec);
}
// Add authorization options to the command line arguments.
// SVN pre 1.5 does not accept "--userName" for "add", which is most likely
// an oversight. As no password is needed for the option, generally omit it.
QStringList SubversionPlugin::addAuthenticationOptions(const QStringList &args,
const QString &userName, const QString &password)
{
if (userName.isEmpty())
return args;
if (!args.empty() && args.front() == QLatin1String("add"))
return args;
QStringList rc;
rc.push_back(QLatin1String("--username"));
rc.push_back(userName);
if (!password.isEmpty()) {
rc.push_back(QLatin1String("--password"));
rc.push_back(password);
}
rc.append(args);
return rc;
}
SubversionPlugin::Version SubversionPlugin::svnVersion()
{
if (m_svnVersionBinary != m_settings.binaryPath()) {
QStringList args;
args << QLatin1String("--version") << QLatin1String("-q");
const Utils::SynchronousProcessResponse response =
VcsBase::VcsBasePlugin::runVcs(QDir().absolutePath(), m_settings.binaryPath(),
args, m_settings.timeOutMs());
if (response.result == Utils::SynchronousProcessResponse::Finished &&
response.exitCode == 0) {
m_svnVersionBinary = m_settings.binaryPath();
m_svnVersion = response.stdOut.trimmed();
} else {
m_svnVersionBinary.clear();
m_svnVersion.clear();
}
}
SubversionPlugin::Version v;
if (::sscanf(m_svnVersion.toLatin1().constData(), "%d.%d.%d",
&v.majorVersion, &v.minorVersion, &v.patchVersion) != 3)
v.majorVersion = v.minorVersion = v.patchVersion = -1;
return v;
}
SubversionResponse SubversionPlugin::runSvn(const QString &workingDir,
const QString &userName, const QString &password,
const QStringList &arguments, int timeOut,
......@@ -1152,7 +1005,7 @@ SubversionResponse SubversionPlugin::runSvn(const QString &workingDir,
return response;
}
const QStringList completeArguments = SubversionPlugin::addAuthenticationOptions(arguments, userName, password);
const QStringList completeArguments = SubversionClient::addAuthenticationOptions(arguments, userName, password);
const Utils::SynchronousProcessResponse sp_resp =
VcsBase::VcsBasePlugin::runVcs(workingDir, executable, completeArguments, timeOut,
flags, outputCodec);
......@@ -1446,4 +1299,3 @@ void SubversionPlugin::testLogResolving()
Q_EXPORT_PLUGIN(Subversion::Internal::SubversionPlugin)
#include "subversionplugin.moc"
......@@ -62,7 +62,7 @@ namespace Internal {
class SubversionSubmitEditor;
class SubversionControl;
struct SubversionDiffParameters;
class SubversionClient;
struct SubversionResponse
{
......@@ -84,8 +84,6 @@ public:
bool initialize(const QStringList &arguments, QString *errorMessage);
void svnDiff(const QString &workingDir, const QStringList &files, QString diffname = QString());
SubversionSubmitEditor *openSubversionSubmitEditor(const QString &fileName);
SubversionSettings settings() const;
......@@ -102,24 +100,9 @@ public:
static SubversionPlugin *instance();