subversionclient.cpp 10.1 KB
Newer Older
1
2
/****************************************************************************
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4
5
6
7
8
9
10
11
12
** 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
Eike Ziller's avatar
Eike Ziller committed
13
14
** conditions see http://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/contact-us.
15
16
17
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18
19
20
21
22
23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
25
26
27
28
29
30
31
32
**
** 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"
33
#include "subversionconstants.h"
34

hjk's avatar
hjk committed
35
#include <vcsbase/vcscommand.h>
36
37
38
39
40
41
42
43
44
45
#include <vcsbase/vcsbaseplugin.h>
#include <vcsbase/vcsbaseconstants.h>
#include <vcsbase/vcsbaseeditorparameterwidget.h>
#include <utils/synchronousprocess.h>

#include <QDir>
#include <QFileInfo>
#include <QTextStream>
#include <QDebug>

hjk's avatar
hjk committed
46
47
48
using namespace Utils;
using namespace VcsBase;

49
50
51
52
53
54
55
56
57
58
59
60
61
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
hjk's avatar
hjk committed
62
class SubversionDiffParameterWidget : public VcsBaseEditorParameterWidget
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
{
    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)
hjk's avatar
hjk committed
80
    : VcsBaseEditorParameterWidget(parent), m_client(client), m_params(p)
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
{
    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) :
hjk's avatar
hjk committed
104
    VcsBaseClient(settings)
105
106
107
108
109
{
}

SubversionSettings *SubversionClient::settings() const
{
hjk's avatar
hjk committed
110
    return dynamic_cast<SubversionSettings *>(VcsBaseClient::settings());
111
112
}

hjk's avatar
hjk committed
113
114
115
116
VcsCommand *SubversionClient::createCommitCmd(const QString &repositoryRoot,
                                              const QStringList &files,
                                              const QString &commitMessageFile,
                                              const QStringList &extraOptions) const
117
118
119
120
121
{
    const QStringList svnExtraOptions =
            QStringList(extraOptions)
            << authenticationOptions(SubversionClient::CommitCommand)
            << QLatin1String(Constants::NON_INTERACTIVE_OPTION)
122
            << QLatin1String("--encoding") << QLatin1String("utf8")
123
124
            << QLatin1String("--file") << commitMessageFile;

hjk's avatar
hjk committed
125
    VcsCommand *cmd = createCommand(repositoryRoot);
126
    cmd->addFlags(VcsBasePlugin::ShowStdOutInLogWindow);
127
128
129
130
131
132
133
134
135
136
137
138
139
    QStringList args(vcsCommandString(CommitCommand));
    cmd->addJob(args << svnExtraOptions << files);
    return cmd;
}

void SubversionClient::commit(const QString &repositoryRoot,
                              const QStringList &files,
                              const QString &commitMessageFile,
                              const QStringList &extraOptions)
{
    if (Subversion::Constants::debug)
        qDebug() << Q_FUNC_INFO << commitMessageFile << files;

hjk's avatar
hjk committed
140
    VcsCommand *cmd = createCommitCmd(repositoryRoot, files, commitMessageFile, extraOptions);
141
142
143
    cmd->execute();
}

hjk's avatar
hjk committed
144
Core::Id SubversionClient::vcsEditorKind(VcsCommandTag cmd) const
145
146
147
148
149
150
151
152
153
154
155
156
157
158
{
    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");
hjk's avatar
hjk committed
159
160
161
162
        const SynchronousProcessResponse response =
                VcsBasePlugin::runVcs(QDir().absolutePath(), settings()->binaryPath(),
                                      args, settings()->timeOutMs());
        if (response.result == SynchronousProcessResponse::Finished &&
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
                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;
}

hjk's avatar
hjk committed
181
QStringList SubversionClient::authenticationOptions(VcsCommandTag cmd) const
182
183
184
185
186
187
188
189
190
191
{
    const bool hasAuth = settings()->hasAuthentication();
    const QString userName = hasAuth ? settings()->stringValue(SubversionSettings::userKey) : QString();
    const QString password = hasAuth ? settings()->stringValue(SubversionSettings::passwordKey) : QString();
    QStringList args(vcsCommandString(cmd));
    args = SubversionClient::addAuthenticationOptions(args, userName, password);
    args.removeFirst();
    return args;
}

192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// 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;
}

Tobias Hunger's avatar
Tobias Hunger committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
QString SubversionClient::synchronousTopic(const QString &repository)
{
    QStringList args;
    args << QLatin1String("info");

    QByteArray stdOut;
    if (!vcsFullySynchronousExec(repository, args, &stdOut))
        return QString();

    const QString revisionString = QLatin1String("Revision: ");
    // stdOut is ASCII only (at least in those areas we care about).
    QString output = SynchronousProcess::normalizeNewlines(QString::fromLocal8Bit(stdOut));
    foreach (const QString &line, output.split(QLatin1Char('\n'))) {
        if (line.startsWith(revisionString))
            return QString::fromLatin1("r") + line.mid(revisionString.count());
    }
    return QString();
}

233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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();
}

hjk's avatar
hjk committed
263
VcsBaseClient::StatusItem SubversionClient::parseStatusLine(const QString &line) const
264
265
{
    Q_UNUSED(line)
hjk's avatar
hjk committed
266
    return VcsBaseClient::StatusItem();
267
268
}

hjk's avatar
hjk committed
269
VcsBaseEditorParameterWidget *SubversionClient::createDiffEditor(
270
271
272
273
274
275
276
277
278
279
280
281
282
283
        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"