subversionplugin.cpp 53 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** 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.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** 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
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
32
33
34
35
#include "subversionplugin.h"

#include "settingspage.h"
#include "subversioneditor.h"

#include "subversionsubmiteditor.h"
36
#include "subversionclient.h"
con's avatar
con committed
37
38
#include "subversionconstants.h"
#include "subversioncontrol.h"
39
#include "checkoutwizard.h"
con's avatar
con committed
40
41
42
43

#include <vcsbase/basevcseditorfactory.h>
#include <vcsbase/vcsbaseeditor.h>
#include <vcsbase/basevcssubmiteditorfactory.h>
44
#include <vcsbase/vcsbaseoutputwindow.h>
45
#include <vcsbase/vcsbaseeditorparameterwidget.h>
con's avatar
con committed
46
#include <utils/synchronousprocess.h>
47
#include <utils/parameteraction.h>
48
#include <utils/fileutils.h>
49
#include <utils/hostosinfo.h>
con's avatar
con committed
50
51
52

#include <coreplugin/icore.h>
#include <coreplugin/coreconstants.h>
53
#include <coreplugin/documentmanager.h>
con's avatar
con committed
54
55
#include <coreplugin/messagemanager.h>
#include <coreplugin/mimedatabase.h>
56
#include <coreplugin/actionmanager/actionmanager.h>
57
58
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/command.h>
59
#include <coreplugin/id.h>
con's avatar
con committed
60
#include <coreplugin/editormanager/editormanager.h>
Friedemann Kleint's avatar
Friedemann Kleint committed
61

62
#include <coreplugin/locator/commandlocator.h>
Friedemann Kleint's avatar
Friedemann Kleint committed
63

hjk's avatar
hjk committed
64
#include <utils/qtcassert.h>
con's avatar
con committed
65

66
67
68
69
70
71
72
73
74
75
76
77
78
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QTextCodec>
#include <QtPlugin>
#include <QProcessEnvironment>
#include <QUrl>
#include <QXmlStreamReader>
#include <QAction>
#include <QFileDialog>
#include <QMenu>
#include <QMessageBox>
#include <QInputDialog>
79
#include <limits.h>
con's avatar
con committed
80

81
82
83
84
#ifdef WITH_TESTS
#include <QTest>
#endif

85
86
namespace Subversion {
namespace Internal {
con's avatar
con committed
87

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
static const char CMD_ID_SUBVERSION_MENU[]    = "Subversion.Menu";
static const char CMD_ID_ADD[]                = "Subversion.Add";
static const char CMD_ID_DELETE_FILE[]        = "Subversion.Delete";
static const char CMD_ID_REVERT[]             = "Subversion.Revert";
static const char CMD_ID_DIFF_PROJECT[]       = "Subversion.DiffAll";
static const char CMD_ID_DIFF_CURRENT[]       = "Subversion.DiffCurrent";
static const char CMD_ID_COMMIT_ALL[]         = "Subversion.CommitAll";
static const char CMD_ID_REVERT_ALL[]         = "Subversion.RevertAll";
static const char CMD_ID_COMMIT_CURRENT[]     = "Subversion.CommitCurrent";
static const char CMD_ID_FILELOG_CURRENT[]    = "Subversion.FilelogCurrent";
static const char CMD_ID_ANNOTATE_CURRENT[]   = "Subversion.AnnotateCurrent";
static const char CMD_ID_STATUS[]             = "Subversion.Status";
static const char CMD_ID_PROJECTLOG[]         = "Subversion.ProjectLog";
static const char CMD_ID_REPOSITORYLOG[]      = "Subversion.RepositoryLog";
static const char CMD_ID_REPOSITORYUPDATE[]   = "Subversion.RepositoryUpdate";
static const char CMD_ID_REPOSITORYDIFF[]     = "Subversion.RepositoryDiff";
static const char CMD_ID_REPOSITORYSTATUS[]   = "Subversion.RepositoryStatus";
static const char CMD_ID_UPDATE[]             = "Subversion.Update";
static const char CMD_ID_COMMIT_PROJECT[]     = "Subversion.CommitProject";
static const char CMD_ID_DESCRIBE[]           = "Subversion.Describe";
con's avatar
con committed
108

109
static const char nonInteractiveOptionC[] = "--non-interactive";
110

111
112


hjk's avatar
hjk committed
113
static const VcsBase::VcsBaseEditorParameters editorParameters[] = {
con's avatar
con committed
114
{
Orgad Shaneh's avatar
Orgad Shaneh committed
115
    VcsBase::LogOutput,
116
117
    "Subversion File Log Editor",   // id
    QT_TRANSLATE_NOOP("VCS", "Subversion File Log Editor"),   // display_name
118
    "Subversion File Log Editor",   // context
Orgad Shaneh's avatar
Orgad Shaneh committed
119
    "text/vnd.qtcreator.svn.log"},
hjk's avatar
hjk committed
120
{    VcsBase::AnnotateOutput,
121
122
    "Subversion Annotation Editor",  // id
    QT_TRANSLATE_NOOP("VCS", "Subversion Annotation Editor"),   // display_name
123
    "Subversion Annotation Editor",  // context
Orgad Shaneh's avatar
Orgad Shaneh committed
124
    "text/vnd.qtcreator.svn.annotation"},
hjk's avatar
hjk committed
125
{   VcsBase::DiffOutput,
126
127
    "Subversion Diff Editor",  // id
    QT_TRANSLATE_NOOP("VCS", "Subversion Diff Editor"),   // display_name
128
    "Subversion Diff Editor",  // context
129
    "text/x-patch"}
con's avatar
con committed
130
131
132
};

// Utility to find a parameter set by type
hjk's avatar
hjk committed
133
static inline const VcsBase::VcsBaseEditorParameters *findType(int ie)
con's avatar
con committed
134
{
hjk's avatar
hjk committed
135
136
    const VcsBase::EditorContentType et = static_cast<VcsBase::EditorContentType>(ie);
    return  VcsBase::VcsBaseEditorWidget::findType(editorParameters, sizeof(editorParameters)/sizeof(VcsBase::VcsBaseEditorParameters), et);
con's avatar
con committed
137
138
139
140
}

static inline QString debugCodec(const QTextCodec *c)
{
141
    return c ? QString::fromLatin1(c->name()) : QString::fromLatin1("Null codec");
con's avatar
con committed
142
143
}

144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Parse "svn status" output for added/modified/deleted files
// "M<7blanks>file"
typedef QList<SubversionSubmitEditor::StatusFilePair> StatusList;

StatusList parseStatusOutput(const QString &output)
{
    StatusList changeSet;
    const QString newLine = QString(QLatin1Char('\n'));
    const QStringList list = output.split(newLine, QString::SkipEmptyParts);
    foreach (const QString &l, list) {
        const QString line =l.trimmed();
        if (line.size() > 8) {
            const QChar state = line.at(0);
            if (state == QLatin1Char('A') || state == QLatin1Char('D') || state == QLatin1Char('M')) {
158
159
                const QString fileName = line.mid(7); // Column 8 starting from svn 1.6
                changeSet.push_back(SubversionSubmitEditor::StatusFilePair(QString(state), fileName.trimmed()));
160
161
162
163
164
165
166
            }

        }
    }
    return changeSet;
}

167
168
169
170
// Return a list of names for the internal svn directories
static inline QStringList svnDirectories()
{
    QStringList rc(QLatin1String(".svn"));
171
172
173
    if (Utils::HostOsInfo::isWindowsHost())
        // Option on Windows systems to avoid hassle with some IDEs
        rc.push_back(QLatin1String("_svn"));
174
175
176
    return rc;
}

con's avatar
con committed
177
178
179
180
// ------------- SubversionPlugin
SubversionPlugin *SubversionPlugin::m_subversionPluginInstance = 0;

SubversionPlugin::SubversionPlugin() :
181
    m_svnDirectories(svnDirectories()),
Friedemann Kleint's avatar
Friedemann Kleint committed
182
    m_commandLocator(0),
con's avatar
con committed
183
184
185
186
187
    m_addAction(0),
    m_deleteAction(0),
    m_revertAction(0),
    m_diffProjectAction(0),
    m_diffCurrentAction(0),
188
189
    m_logProjectAction(0),
    m_logRepositoryAction(0),
con's avatar
con committed
190
    m_commitAllAction(0),
191
    m_revertRepositoryAction(0),
192
193
194
    m_diffRepositoryAction(0),
    m_statusRepositoryAction(0),
    m_updateRepositoryAction(0),
con's avatar
con committed
195
196
197
    m_commitCurrentAction(0),
    m_filelogCurrentAction(0),
    m_annotateCurrentAction(0),
198
    m_statusProjectAction(0),
con's avatar
con committed
199
    m_updateProjectAction(0),
200
    m_commitProjectAction(0),
201
    m_describeAction(0),
con's avatar
con committed
202
203
204
    m_submitCurrentLogAction(0),
    m_submitDiffAction(0),
    m_submitUndoAction(0),
205
    m_submitRedoAction(0),
206
    m_menuAction(0),
207
    m_submitActionTriggered(false)
con's avatar
con committed
208
209
210
211
212
{
}

SubversionPlugin::~SubversionPlugin()
{
213
    delete m_client;
214
    cleanCommitMessageFile();
con's avatar
con committed
215
216
}

217
void SubversionPlugin::cleanCommitMessageFile()
con's avatar
con committed
218
{
219
220
221
    if (!m_commitMessageFileName.isEmpty()) {
        QFile::remove(m_commitMessageFileName);
        m_commitMessageFileName.clear();
222
        m_commitRepository.clear();
con's avatar
con committed
223
224
225
    }
}

226
227
228
229
230
bool SubversionPlugin::isCommitEditorOpen() const
{
    return !m_commitMessageFileName.isEmpty();
}

hjk's avatar
hjk committed
231
static const VcsBase::VcsBaseSubmitEditorParameters submitParameters = {
con's avatar
con committed
232
    Subversion::Constants::SUBVERSION_SUBMIT_MIMETYPE,
233
234
    Subversion::Constants::SUBVERSIONCOMMITEDITOR_ID,
    Subversion::Constants::SUBVERSIONCOMMITEDITOR_DISPLAY_NAME,
235
236
    Subversion::Constants::SUBVERSIONCOMMITEDITOR,
    VcsBase::VcsBaseSubmitEditorParameters::DiffFiles
con's avatar
con committed
237
238
};

239
bool SubversionPlugin::initialize(const QStringList & /*arguments */, QString *errorMessage)
con's avatar
con committed
240
{
hjk's avatar
hjk committed
241
242
    typedef VcsBase::VcsSubmitEditorFactory<SubversionSubmitEditor> SubversionSubmitEditorFactory;
    typedef VcsBase::VcsEditorFactory<SubversionEditor> SubversionEditorFactory;
con's avatar
con committed
243
244
245
246
247
    using namespace Constants;

    using namespace Core::Constants;
    using namespace ExtensionSystem;

248
    initializeVcs(new SubversionControl(this));
249

con's avatar
con committed
250
251
    m_subversionPluginInstance = this;

hjk's avatar
hjk committed
252
    if (!Core::MimeDatabase::addMimeTypes(QLatin1String(":/trolltech.subversion/Subversion.mimetypes.xml"), errorMessage))
con's avatar
con committed
253
254
        return false;

255
    m_settings.readSettings(Core::ICore::settings());
256
    m_client = new SubversionClient(&m_settings);
con's avatar
con committed
257

258
    addAutoReleasedObject(new SettingsPage);
con's avatar
con committed
259

260
    addAutoReleasedObject(new SubversionSubmitEditorFactory(&submitParameters));
con's avatar
con committed
261
262

    static const char *describeSlot = SLOT(describe(QString,QString));
hjk's avatar
hjk committed
263
    const int editorCount = sizeof(editorParameters)/sizeof(VcsBase::VcsBaseEditorParameters);
264
265
    for (int i = 0; i < editorCount; i++)
        addAutoReleasedObject(new SubversionEditorFactory(editorParameters + i, this, describeSlot));
con's avatar
con committed
266

267
268
    addAutoReleasedObject(new CheckoutWizard);

Friedemann Kleint's avatar
Friedemann Kleint committed
269
    const QString prefix = QLatin1String("svn");
270
    m_commandLocator = new Core::CommandLocator("Subversion", prefix, prefix);
Friedemann Kleint's avatar
Friedemann Kleint committed
271
272
    addAutoReleasedObject(m_commandLocator);

con's avatar
con committed
273
    //register actions
Eike Ziller's avatar
Eike Ziller committed
274
    Core::ActionContainer *toolsContainer = Core::ActionManager::actionContainer(M_TOOLS);
con's avatar
con committed
275

276
    Core::ActionContainer *subversionMenu =
Eike Ziller's avatar
Eike Ziller committed
277
        Core::ActionManager::createMenu(Core::Id(CMD_ID_SUBVERSION_MENU));
con's avatar
con committed
278
279
    subversionMenu->menu()->setTitle(tr("&Subversion"));
    toolsContainer->addMenu(subversionMenu);
280
    m_menuAction = subversionMenu->menu()->menuAction();
281
    Core::Context globalcontext(C_GLOBAL);
con's avatar
con committed
282
    Core::Command *command;
283
284

    m_diffCurrentAction = new Utils::ParameterAction(tr("Diff Current File"), tr("Diff \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
285
    command = Core::ActionManager::registerAction(m_diffCurrentAction,
286
        CMD_ID_DIFF_CURRENT, globalcontext);
con's avatar
con committed
287
    command->setAttribute(Core::Command::CA_UpdateText);
288
    command->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+S,Meta+D") : tr("Alt+S,Alt+D")));
289
    connect(m_diffCurrentAction, SIGNAL(triggered()), this, SLOT(diffCurrentFile()));
con's avatar
con committed
290
    subversionMenu->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
291
    m_commandLocator->appendCommand(command);
con's avatar
con committed
292

293
    m_filelogCurrentAction = new Utils::ParameterAction(tr("Filelog Current File"), tr("Filelog \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
294
    command = Core::ActionManager::registerAction(m_filelogCurrentAction,
295
        CMD_ID_FILELOG_CURRENT, globalcontext);
con's avatar
con committed
296
    command->setAttribute(Core::Command::CA_UpdateText);
297
298
    connect(m_filelogCurrentAction, SIGNAL(triggered()), this,
        SLOT(filelogCurrentFile()));
con's avatar
con committed
299
    subversionMenu->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
300
    m_commandLocator->appendCommand(command);
con's avatar
con committed
301

302
    m_annotateCurrentAction = new Utils::ParameterAction(tr("Annotate Current File"), tr("Annotate \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
303
    command = Core::ActionManager::registerAction(m_annotateCurrentAction,
304
        CMD_ID_ANNOTATE_CURRENT, globalcontext);
con's avatar
con committed
305
    command->setAttribute(Core::Command::CA_UpdateText);
306
307
    connect(m_annotateCurrentAction, SIGNAL(triggered()), this,
        SLOT(annotateCurrentFile()));
con's avatar
con committed
308
    subversionMenu->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
309
    m_commandLocator->appendCommand(command);
con's avatar
con committed
310

311
    subversionMenu->addSeparator(globalcontext);
con's avatar
con committed
312

313
    m_addAction = new Utils::ParameterAction(tr("Add"), tr("Add \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
314
    command = Core::ActionManager::registerAction(m_addAction, CMD_ID_ADD,
con's avatar
con committed
315
        globalcontext);
316
    command->setAttribute(Core::Command::CA_UpdateText);
317
    command->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+S,Meta+A") : tr("Alt+S,Alt+A")));
318
    connect(m_addAction, SIGNAL(triggered()), this, SLOT(addCurrentFile()));
con's avatar
con committed
319
    subversionMenu->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
320
    m_commandLocator->appendCommand(command);
con's avatar
con committed
321

322
    m_commitCurrentAction = new Utils::ParameterAction(tr("Commit Current File"), tr("Commit \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
323
    command = Core::ActionManager::registerAction(m_commitCurrentAction,
324
        CMD_ID_COMMIT_CURRENT, globalcontext);
con's avatar
con committed
325
    command->setAttribute(Core::Command::CA_UpdateText);
326
    command->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+S,Meta+C") : tr("Alt+S,Alt+C")));
con's avatar
con committed
327
328
    connect(m_commitCurrentAction, SIGNAL(triggered()), this, SLOT(startCommitCurrentFile()));
    subversionMenu->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
329
    m_commandLocator->appendCommand(command);
con's avatar
con committed
330

331
    m_deleteAction = new Utils::ParameterAction(tr("Delete..."), tr("Delete \"%1\"..."), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
332
    command = Core::ActionManager::registerAction(m_deleteAction, CMD_ID_DELETE_FILE,
333
        globalcontext);
con's avatar
con committed
334
    command->setAttribute(Core::Command::CA_UpdateText);
335
    connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(promptToDeleteCurrentFile()));
con's avatar
con committed
336
    subversionMenu->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
337
    m_commandLocator->appendCommand(command);
con's avatar
con committed
338

339
    m_revertAction = new Utils::ParameterAction(tr("Revert..."), tr("Revert \"%1\"..."), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
340
    command = Core::ActionManager::registerAction(m_revertAction, CMD_ID_REVERT,
341
        globalcontext);
con's avatar
con committed
342
    command->setAttribute(Core::Command::CA_UpdateText);
343
    connect(m_revertAction, SIGNAL(triggered()), this, SLOT(revertCurrentFile()));
con's avatar
con committed
344
    subversionMenu->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
345
    m_commandLocator->appendCommand(command);
con's avatar
con committed
346

347
    subversionMenu->addSeparator(globalcontext);
348

349
    m_diffProjectAction = new Utils::ParameterAction(tr("Diff Project"), tr("Diff Project \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
350
    command = Core::ActionManager::registerAction(m_diffProjectAction, CMD_ID_DIFF_PROJECT,
351
352
353
354
        globalcontext);
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_diffProjectAction, SIGNAL(triggered()), this, SLOT(diffProject()));
    subversionMenu->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
355
    m_commandLocator->appendCommand(command);
con's avatar
con committed
356

357
    m_statusProjectAction = new Utils::ParameterAction(tr("Project Status"), tr("Status of Project \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
358
    command = Core::ActionManager::registerAction(m_statusProjectAction, CMD_ID_STATUS,
con's avatar
con committed
359
        globalcontext);
360
361
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_statusProjectAction, SIGNAL(triggered()), this, SLOT(projectStatus()));
con's avatar
con committed
362
    subversionMenu->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
363
    m_commandLocator->appendCommand(command);
con's avatar
con committed
364

Friedemann Kleint's avatar
Friedemann Kleint committed
365
    m_logProjectAction = new Utils::ParameterAction(tr("Log Project"), tr("Log Project \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
366
    command = Core::ActionManager::registerAction(m_logProjectAction, CMD_ID_PROJECTLOG, globalcontext);
367
368
369
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_logProjectAction, SIGNAL(triggered()), this, SLOT(logProject()));
    subversionMenu->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
370
    m_commandLocator->appendCommand(command);
371

372
    m_updateProjectAction = new Utils::ParameterAction(tr("Update Project"), tr("Update Project \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
373
    command = Core::ActionManager::registerAction(m_updateProjectAction, CMD_ID_UPDATE, globalcontext);
con's avatar
con committed
374
    connect(m_updateProjectAction, SIGNAL(triggered()), this, SLOT(updateProject()));
375
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
376
    subversionMenu->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
377
    m_commandLocator->appendCommand(command);
con's avatar
con committed
378

379
    m_commitProjectAction = new Utils::ParameterAction(tr("Commit Project"), tr("Commit Project \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
380
    command = Core::ActionManager::registerAction(m_commitProjectAction, CMD_ID_COMMIT_PROJECT, globalcontext);
381
382
383
384
385
    connect(m_commitProjectAction, SIGNAL(triggered()), this, SLOT(startCommitProject()));
    command->setAttribute(Core::Command::CA_UpdateText);
    subversionMenu->addAction(command);
    m_commandLocator->appendCommand(command);

386
    subversionMenu->addSeparator(globalcontext);
387

388
    m_diffRepositoryAction = new QAction(tr("Diff Repository"), this);
Eike Ziller's avatar
Eike Ziller committed
389
    command = Core::ActionManager::registerAction(m_diffRepositoryAction, CMD_ID_REPOSITORYDIFF, globalcontext);
390
391
392
393
394
    connect(m_diffRepositoryAction, SIGNAL(triggered()), this, SLOT(diffRepository()));
    subversionMenu->addAction(command);
    m_commandLocator->appendCommand(command);

    m_statusRepositoryAction = new QAction(tr("Repository Status"), this);
Eike Ziller's avatar
Eike Ziller committed
395
    command = Core::ActionManager::registerAction(m_statusRepositoryAction, CMD_ID_REPOSITORYSTATUS, globalcontext);
396
397
398
399
400
    connect(m_statusRepositoryAction, SIGNAL(triggered()), this, SLOT(statusRepository()));
    subversionMenu->addAction(command);
    m_commandLocator->appendCommand(command);

    m_logRepositoryAction = new QAction(tr("Log Repository"), this);
Eike Ziller's avatar
Eike Ziller committed
401
    command = Core::ActionManager::registerAction(m_logRepositoryAction, CMD_ID_REPOSITORYLOG, globalcontext);
402
403
    connect(m_logRepositoryAction, SIGNAL(triggered()), this, SLOT(logRepository()));
    subversionMenu->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
404
    m_commandLocator->appendCommand(command);
405

406
    m_updateRepositoryAction = new QAction(tr("Update Repository"), this);
Eike Ziller's avatar
Eike Ziller committed
407
    command = Core::ActionManager::registerAction(m_updateRepositoryAction, CMD_ID_REPOSITORYUPDATE, globalcontext);
408
    connect(m_updateRepositoryAction, SIGNAL(triggered()), this, SLOT(updateRepository()));
409
    subversionMenu->addAction(command);
410
    m_commandLocator->appendCommand(command);
411
412

    m_commitAllAction = new QAction(tr("Commit All Files"), this);
Eike Ziller's avatar
Eike Ziller committed
413
    command = Core::ActionManager::registerAction(m_commitAllAction, CMD_ID_COMMIT_ALL,
414
415
416
        globalcontext);
    connect(m_commitAllAction, SIGNAL(triggered()), this, SLOT(startCommitAll()));
    subversionMenu->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
417
    m_commandLocator->appendCommand(command);
418

419
    m_describeAction = new QAction(tr("Describe..."), this);
Eike Ziller's avatar
Eike Ziller committed
420
    command = Core::ActionManager::registerAction(m_describeAction, CMD_ID_DESCRIBE, globalcontext);
421
422
423
    connect(m_describeAction, SIGNAL(triggered()), this, SLOT(slotDescribe()));
    subversionMenu->addAction(command);

424
    m_revertRepositoryAction = new QAction(tr("Revert Repository..."), this);
Eike Ziller's avatar
Eike Ziller committed
425
    command = Core::ActionManager::registerAction(m_revertRepositoryAction, CMD_ID_REVERT_ALL,
426
427
428
        globalcontext);
    connect(m_revertRepositoryAction, SIGNAL(triggered()), this, SLOT(revertAll()));
    subversionMenu->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
429
    m_commandLocator->appendCommand(command);
430

con's avatar
con committed
431
    // Actions of the submit editor
432
    Core::Context svncommitcontext(Constants::SUBVERSIONCOMMITEDITOR);
con's avatar
con committed
433

hjk's avatar
hjk committed
434
    m_submitCurrentLogAction = new QAction(VcsBase::VcsBaseSubmitEditor::submitIcon(), tr("Commit"), this);
Eike Ziller's avatar
Eike Ziller committed
435
    command = Core::ActionManager::registerAction(m_submitCurrentLogAction, Constants::SUBMIT_CURRENT, svncommitcontext);
436
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
437
438
    connect(m_submitCurrentLogAction, SIGNAL(triggered()), this, SLOT(submitCurrentLog()));

hjk's avatar
hjk committed
439
    m_submitDiffAction = new QAction(VcsBase::VcsBaseSubmitEditor::diffIcon(), tr("Diff &Selected Files"), this);
Eike Ziller's avatar
Eike Ziller committed
440
    command = Core::ActionManager::registerAction(m_submitDiffAction , Constants::DIFF_SELECTED, svncommitcontext);
con's avatar
con committed
441
442

    m_submitUndoAction = new QAction(tr("&Undo"), this);
Eike Ziller's avatar
Eike Ziller committed
443
    command = Core::ActionManager::registerAction(m_submitUndoAction, Core::Constants::UNDO, svncommitcontext);
con's avatar
con committed
444
445

    m_submitRedoAction = new QAction(tr("&Redo"), this);
Eike Ziller's avatar
Eike Ziller committed
446
    command = Core::ActionManager::registerAction(m_submitRedoAction, Core::Constants::REDO, svncommitcontext);
con's avatar
con committed
447
448
449
450

    return true;
}

451
bool SubversionPlugin::submitEditorAboutToClose()
con's avatar
con committed
452
{
453
    if (!isCommitEditorOpen())
con's avatar
con committed
454
455
        return true;

456
457
458
459
    SubversionSubmitEditor *editor = qobject_cast<SubversionSubmitEditor *>(submitEditor());
    QTC_ASSERT(editor, return true);
    Core::IDocument *editorDocument = editor->document();
    QTC_ASSERT(editorDocument, return true);
con's avatar
con committed
460
461
462

    // Submit editor closing. Make it write out the commit message
    // and retrieve files
463
    const QFileInfo editorFile(editorDocument->filePath());
464
    const QFileInfo changeFile(m_commitMessageFileName);
con's avatar
con committed
465
466
467
    if (editorFile.absoluteFilePath() != changeFile.absoluteFilePath())
        return true; // Oops?!

468
469
    // Prompt user. Force a prompt unless submit was actually invoked (that
    // is, the editor was closed or shutdown).
470
    SubversionSettings newSettings = m_settings;
hjk's avatar
hjk committed
471
    const VcsBase::VcsBaseSubmitEditor::PromptSubmitResult answer =
472
473
            editor->promptSubmit(tr("Closing Subversion Editor"),
                                 tr("Do you want to commit the change?"),
474
                                 tr("The commit message check failed. Do you want to commit the change?"),
475
476
                                 newSettings.boolPointer(SubversionSettings::promptOnSubmitKey),
                                 !m_submitActionTriggered);
477
    m_submitActionTriggered = false;
con's avatar
con committed
478
    switch (answer) {
hjk's avatar
hjk committed
479
    case VcsBase::VcsBaseSubmitEditor::SubmitCanceled:
con's avatar
con committed
480
        return false; // Keep editing and change file
hjk's avatar
hjk committed
481
    case VcsBase::VcsBaseSubmitEditor::SubmitDiscarded:
482
        cleanCommitMessageFile();
con's avatar
con committed
483
484
485
486
        return true; // Cancel all
    default:
        break;
    }
487
    setSettings(newSettings); // in case someone turned prompting off
con's avatar
con committed
488
    const QStringList fileList = editor->checkedFiles();
489
    bool closeEditor = true;
con's avatar
con committed
490
491
    if (!fileList.empty()) {
        // get message & commit
492
        closeEditor = Core::DocumentManager::saveDocument(editorDocument);
493
494
        if (closeEditor)
            closeEditor = commit(m_commitMessageFileName, fileList);
con's avatar
con committed
495
    }
496
    if (closeEditor)
497
        cleanCommitMessageFile();
498
    return closeEditor;
con's avatar
con committed
499
500
}

501
void SubversionPlugin::diffCommitFiles(const QStringList &files)
con's avatar
con committed
502
{
503
    m_client->diff(m_commitRepository, files);
504
505
}

con's avatar
con committed
506
507
SubversionSubmitEditor *SubversionPlugin::openSubversionSubmitEditor(const QString &fileName)
{
hjk's avatar
hjk committed
508
    Core::IEditor *editor = Core::EditorManager::openEditor(fileName,
Eike Ziller's avatar
Eike Ziller committed
509
                                                            Constants::SUBVERSIONCOMMITEDITOR_ID);
con's avatar
con committed
510
    SubversionSubmitEditor *submitEditor = qobject_cast<SubversionSubmitEditor*>(editor);
511
    QTC_CHECK(submitEditor);
512
    setSubmitEditor(submitEditor);
513
    submitEditor->registerActions(m_submitUndoAction, m_submitRedoAction, m_submitCurrentLogAction, m_submitDiffAction);
514
    connect(submitEditor, SIGNAL(diffSelectedFiles(QStringList)), this, SLOT(diffCommitFiles(QStringList)));
515
    submitEditor->setCheckScriptWorkingDirectory(m_commitRepository);
con's avatar
con committed
516
517
518
    return submitEditor;
}

hjk's avatar
hjk committed
519
void SubversionPlugin::updateActions(VcsBase::VcsBasePlugin::ActionState as)
con's avatar
con committed
520
{
Friedemann Kleint's avatar
Friedemann Kleint committed
521
522
    if (!enableMenuAction(as, m_menuAction)) {
        m_commandLocator->setEnabled(false);
523
        return;
Friedemann Kleint's avatar
Friedemann Kleint committed
524
525
526
527
    }
    const bool hasTopLevel = currentState().hasTopLevel();
    m_commandLocator->setEnabled(hasTopLevel);
    m_logRepositoryAction->setEnabled(hasTopLevel);
528

529
530
531
532
    const QString projectName = currentState().currentProjectName();
    m_diffProjectAction->setParameter(projectName);
    m_statusProjectAction->setParameter(projectName);
    m_updateProjectAction->setParameter(projectName);
533
    m_logProjectAction->setParameter(projectName);
534
    m_commitProjectAction->setParameter(projectName);
535
536
537
538

    const bool repoEnabled = currentState().hasTopLevel();
    m_commitAllAction->setEnabled(repoEnabled);
    m_describeAction->setEnabled(repoEnabled);
539
    m_revertRepositoryAction->setEnabled(repoEnabled);
540
541
542
    m_diffRepositoryAction->setEnabled(repoEnabled);
    m_statusRepositoryAction->setEnabled(repoEnabled);
    m_updateRepositoryAction->setEnabled(repoEnabled);
543
544
545
546
547
548
549
550
551
552

    const QString fileName = currentState().currentFileName();

    m_addAction->setParameter(fileName);
    m_deleteAction->setParameter(fileName);
    m_revertAction->setParameter(fileName);
    m_diffCurrentAction->setParameter(fileName);
    m_commitCurrentAction->setParameter(fileName);
    m_filelogCurrentAction->setParameter(fileName);
    m_annotateCurrentAction->setParameter(fileName);
con's avatar
con committed
553
554
555
556
}

void SubversionPlugin::addCurrentFile()
{
hjk's avatar
hjk committed
557
    const VcsBase::VcsBasePluginState state = currentState();
558
    QTC_ASSERT(state.hasFile(), return);
559
    vcsAdd(state.currentFileTopLevel(), state.relativeCurrentFile());
con's avatar
con committed
560
561
}

562
563
void SubversionPlugin::revertAll()
{
hjk's avatar
hjk committed
564
    const VcsBase::VcsBasePluginState state = currentState();
565
    QTC_ASSERT(state.hasTopLevel(), return);
566
    const QString title = tr("Revert repository");
Tobias Hunger's avatar
Tobias Hunger committed
567
    if (QMessageBox::warning(0, title, tr("Revert all pending changes to the repository?"),
568
569
570
571
572
                             QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
        return;
    // NoteL: Svn "revert ." doesn not work.
    QStringList args;
    args << QLatin1String("revert") << QLatin1String("--recursive") << state.topLevel();
573
    const SubversionResponse revertResponse =
574
            runSvn(state.topLevel(), args, m_settings.timeOutMs(),
575
                   SshPasswordPrompt|ShowStdOutInLogWindow);
576
    if (revertResponse.error)
577
        QMessageBox::warning(0, title, tr("Revert failed: %1").arg(revertResponse.message), QMessageBox::Ok);
578
    else
579
580
581
        subVersionControl()->emitRepositoryChanged(state.topLevel());
}

con's avatar
con committed
582
583
void SubversionPlugin::revertCurrentFile()
{
hjk's avatar
hjk committed
584
    const VcsBase::VcsBasePluginState state = currentState();
585
    QTC_ASSERT(state.hasFile(), return);
con's avatar
con committed
586
587

    QStringList args(QLatin1String("diff"));
588
    args.push_back(state.relativeCurrentFile());
con's avatar
con committed
589

590
    const SubversionResponse diffResponse =
591
            runSvn(state.currentFileTopLevel(), args, m_settings.timeOutMs(), 0);
con's avatar
con committed
592
593
594
595
596
    if (diffResponse.error)
        return;

    if (diffResponse.stdOut.isEmpty())
        return;
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
597
    if (QMessageBox::warning(0, QLatin1String("svn revert"), tr("The file has been changed. Do you want to revert it?"),
con's avatar
con committed
598
599
600
                             QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
        return;

601

602
    Core::FileChangeBlocker fcb(state.currentFile());
con's avatar
con committed
603
604
605

    // revert
    args.clear();
606
    args << QLatin1String("revert") << state.relativeCurrentFile();
con's avatar
con committed
607

608
    const SubversionResponse revertResponse =
609
            runSvn(state.currentFileTopLevel(), args, m_settings.timeOutMs(),
610
611
                   SshPasswordPrompt|ShowStdOutInLogWindow);

612
    if (!revertResponse.error)
613
        subVersionControl()->emitFilesChanged(QStringList(state.currentFile()));
con's avatar
con committed
614
615
616
617
}

void SubversionPlugin::diffProject()
{
hjk's avatar
hjk committed
618
    const VcsBase::VcsBasePluginState state = currentState();
619
    QTC_ASSERT(state.hasProject(), return);
620
621
622
    const QString relativeProject = state.relativeCurrentProject();
    m_client->diff(state.currentProjectTopLevel(),
                      relativeProject.isEmpty() ? QStringList() : QStringList(relativeProject));
con's avatar
con committed
623
624
625
626
}

void SubversionPlugin::diffCurrentFile()
{
hjk's avatar
hjk committed
627
    const VcsBase::VcsBasePluginState state = currentState();
628
    QTC_ASSERT(state.hasFile(), return);
629
    m_client->diff(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()));
con's avatar
con committed
630
631
632
633
}

void SubversionPlugin::startCommitCurrentFile()
{
hjk's avatar
hjk committed
634
    const VcsBase::VcsBasePluginState state = currentState();
635
    QTC_ASSERT(state.hasFile(), return);
636
    startCommit(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()));
con's avatar
con committed
637
638
639
640
}

void SubversionPlugin::startCommitAll()
{
hjk's avatar
hjk committed
641
    const VcsBase::VcsBasePluginState state = currentState();
642
643
    QTC_ASSERT(state.hasTopLevel(), return);
    startCommit(state.topLevel());
con's avatar
con committed
644
645
}

646
647
void SubversionPlugin::startCommitProject()
{
hjk's avatar
hjk committed
648
    const VcsBase::VcsBasePluginState state = currentState();
649
650
651
652
    QTC_ASSERT(state.hasProject(), return);
    startCommit(state.currentProjectPath());
}

con's avatar
con committed
653
654
655
/* Start commit of files of a single repository by displaying
 * template and files in a submit editor. On closing, the real
 * commit will start. */
656
void SubversionPlugin::startCommit(const QString &workingDir, const QStringList &files)
con's avatar
con committed
657
{
658
    if (raiseSubmitEditor())
659
        return;
660
    if (isCommitEditorOpen()) {
hjk's avatar
hjk committed
661
        VcsBase::VcsBaseOutputWindow::instance()->appendWarning(tr("Another commit is currently being executed."));
con's avatar
con committed
662
663
664
665
666
667
        return;
    }

    QStringList args(QLatin1String("status"));
    args += files;

668
    const SubversionResponse response =
669
            runSvn(workingDir, args, m_settings.timeOutMs(), 0);
con's avatar
con committed
670
671
    if (response.error)
        return;
672

con's avatar
con committed
673
    // Get list of added/modified/deleted files
674
    const StatusList statusOutput = parseStatusOutput(response.stdOut);
con's avatar
con committed
675
    if (statusOutput.empty()) {
hjk's avatar
hjk committed
676
        VcsBase::VcsBaseOutputWindow::instance()->appendWarning(tr("There are no modified files."));
con's avatar
con committed
677
678
        return;
    }
679
    m_commitRepository = workingDir;
con's avatar
con committed
680
    // Create a new submit change file containing the submit template
681
682
683
    Utils::TempFileSaver saver;
    saver.setAutoRemove(false);
    // TODO: Retrieve submit template from
con's avatar
con committed
684
685
    const QString submitTemplate;
    // Create a submit
686
687
    saver.write(submitTemplate.toUtf8());
    if (!saver.finalize()) {
hjk's avatar
hjk committed
688
        VcsBase::VcsBaseOutputWindow::instance()->appendError(saver.errorString());
689
690
691
        return;
    }
    m_commitMessageFileName = saver.fileName();
con's avatar
con committed
692
    // Create a submit editor and set file list
693
    SubversionSubmitEditor *editor = openSubversionSubmitEditor(m_commitMessageFileName);
694
    editor->setStatusList(statusOutput);
con's avatar
con committed
695
696
697
698
699
700
701
702
703
704
705
}

bool SubversionPlugin::commit(const QString &messageFile,
                              const QStringList &subVersionFileList)
{
    if (Subversion::Constants::debug)
        qDebug() << Q_FUNC_INFO << messageFile << subVersionFileList;
    // Transform the status list which is sth
    // "[ADM]<blanks>file" into an args list. The files of the status log
    // can be relative or absolute depending on where the command was run.
    QStringList args = QStringList(QLatin1String("commit"));
706
    args << QLatin1String(nonInteractiveOptionC) << QLatin1String("--file") << messageFile;
con's avatar
con committed
707
    args.append(subVersionFileList);
708
    const SubversionResponse response =
709
            runSvn(m_commitRepository, args, 10 * m_settings.timeOutMs(),
710
                   SshPasswordPrompt|ShowStdOutInLogWindow);
con's avatar
con committed
711
712
713
714
715
    return !response.error ;
}

void SubversionPlugin::filelogCurrentFile()
{
hjk's avatar
hjk committed
716
    const VcsBase::VcsBasePluginState state = currentState();
717
    QTC_ASSERT(state.hasFile(), return);
718
    filelog(state.currentFileTopLevel(), state.relativeCurrentFile(), true);
719
720
721
722
}

void SubversionPlugin::logProject()
{
hjk's avatar
hjk committed
723
    const VcsBase::VcsBasePluginState state = currentState();
724
    QTC_ASSERT(state.hasProject(), return);
725
726
727
728
729
    filelog(state.currentProjectTopLevel(), state.relativeCurrentProject());
}

void SubversionPlugin::logRepository()
{
hjk's avatar
hjk committed
730
    const VcsBase::VcsBasePluginState state = currentState();
731
    QTC_ASSERT(state.hasTopLevel(), return);
732
    filelog(state.topLevel());
con's avatar
con committed
733
734
}

735
736
void SubversionPlugin::diffRepository()
{
hjk's avatar
hjk committed
737
    const VcsBase::VcsBasePluginState state = currentState();
738
    QTC_ASSERT(state.hasTopLevel(), return);
739
    m_client->diff(state.topLevel(), QStringList());
740
741
742
743
}

void SubversionPlugin::statusRepository()
{
hjk's avatar
hjk committed
744
    const VcsBase::VcsBasePluginState state = currentState();
745
    QTC_ASSERT(state.hasTopLevel(), return);
746
747
748
749
750
    svnStatus(state.topLevel());
}

void SubversionPlugin::updateRepository()
{
hjk's avatar
hjk committed
751
    const VcsBase::VcsBasePluginState state = currentState();
752
    QTC_ASSERT(state.hasTopLevel(), return);
753
754
755
    svnUpdate(state.topLevel());
}

756
void SubversionPlugin::svnStatus(const QString &workingDir, const QString &relativePath)
757
{
hjk's avatar
hjk committed
758
    const VcsBase::VcsBasePluginState state = currentState();
759
    QTC_ASSERT(state.hasTopLevel(), return);
760
    QStringList args(QLatin1String("status"));
761
762
    if (!relativePath.isEmpty())
        args.append(relativePath);
hjk's avatar
hjk committed
763
    VcsBase::VcsBaseOutputWindow *outwin = VcsBase::VcsBaseOutputWindow::instance();
764
    outwin->setRepository(workingDir);
765
    runSvn(workingDir, args, m_settings.timeOutMs(),
766
           ShowStdOutInLogWindow|ShowSuccessMessage);
767
768
769
    outwin->clearRepository();
}

770
void SubversionPlugin::filelog(const QString &workingDir,
771
                               const QString &file,
772
                               bool enableAnnotationContextMenu)
con's avatar
con committed
773
774
775
{
    // no need for temp file
    QStringList args(QLatin1String("log"));
776
777
778
779
    if (m_settings.intValue(SubversionSettings::logCountKey) > 0) {
        args << QLatin1String("-l")
             << QString::number(m_settings.intValue(SubversionSettings::logCountKey));
    }
780
    if (!file.isEmpty())
781
        args.append(QDir::toNativeSeparators(file));
con's avatar
con committed
782

Roman Kovalev's avatar
Roman Kovalev committed
783
784
    // subversion stores log in UTF-8 and returns it back in user system locale.
    // So we do not need to encode it.
785
    const SubversionResponse response =
786
            runSvn(workingDir, args, m_settings.timeOutMs(),
Roman Kovalev's avatar
Roman Kovalev committed
787
                   SshPasswordPrompt, 0/*codec*/);
con's avatar
con committed
788
789
790
791
792
793
    if (response.error)
        return;

    // Re-use an existing view if possible to support
    // the common usage pattern of continuously changing and diffing a file

794
795
796
    const QString id = VcsBase::VcsBaseEditorWidget::getTitleId(workingDir, QStringList(file));
    const QString tag = VcsBase::VcsBaseEditorWidget::editorTag(VcsBase::LogOutput, workingDir,
                                                                QStringList(file));
hjk's avatar
hjk committed
797
    if (Core::IEditor *editor = VcsBase::VcsBaseEditorWidget::locateEditorByTag(tag)) {
798
        editor->document()->setContents(response.stdOut.toUtf8());
Eike Ziller's avatar
Eike Ziller committed
799
        Core::EditorManager::activateEditor(editor);
con's avatar
con committed
800
    } else {
801
        const QString title = QString::fromLatin1("svn log %1").arg(id);
802
        const QString source = VcsBase::VcsBaseEditorWidget::getSource(workingDir, file);
hjk's avatar
hjk committed
803
804
        Core::IEditor *newEditor = showOutputInEditor(title, response.stdOut, VcsBase::LogOutput, source, /*codec*/0);
        VcsBase::VcsBaseEditorWidget::tagEditor(newEditor, tag);
805
        if (enableAnnotationContextMenu)
hjk's avatar
hjk committed
806
            VcsBase::VcsBaseEditorWidget::getVcsBaseEditor(newEditor)->setFileLogAnnotateEnabled(true);
con's avatar
con committed
807
808
809
810
811
    }
}

void SubversionPlugin::updateProject()
{
hjk's avatar
hjk committed
812
    const VcsBase::VcsBasePluginState state = currentState();
813
    QTC_ASSERT(state.hasProject(), return);
814
815
    svnUpdate(state.currentProjectTopLevel(), state.relativeCurrentProject());
}
con's avatar
con committed
816

817
void SubversionPlugin::svnUpdate(const QString &workingDir, const QString &relativePath)
818
{
con's avatar
con committed
819
    QStringList args(QLatin1String("update"));