subversionplugin.cpp 52.1 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

hjk's avatar
hjk committed
85
86
87
88
using namespace Core;
using namespace Utils;
using namespace VcsBase;

89
90
namespace Subversion {
namespace Internal {
con's avatar
con committed
91

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
112

113
static const char nonInteractiveOptionC[] = "--non-interactive";
114

115
116


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

// Utility to find a parameter set by type
hjk's avatar
hjk committed
137
static const VcsBaseEditorParameters *findType(int ie)
con's avatar
con committed
138
{
hjk's avatar
hjk committed
139
140
    const EditorContentType et = static_cast<EditorContentType>(ie);
    return VcsBaseEditorWidget::findType(editorParameters, sizeof(editorParameters)/sizeof(editorParameters[0]), et);
con's avatar
con committed
141
142
143
144
}

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

148
149
150
151
152
153
154
155
156
157
158
159
160
161
// 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')) {
162
163
                const QString fileName = line.mid(7); // Column 8 starting from svn 1.6
                changeSet.push_back(SubversionSubmitEditor::StatusFilePair(QString(state), fileName.trimmed()));
164
165
166
167
168
169
170
            }

        }
    }
    return changeSet;
}

171
172
173
174
// Return a list of names for the internal svn directories
static inline QStringList svnDirectories()
{
    QStringList rc(QLatin1String(".svn"));
hjk's avatar
hjk committed
175
    if (HostOsInfo::isWindowsHost())
176
177
        // Option on Windows systems to avoid hassle with some IDEs
        rc.push_back(QLatin1String("_svn"));
178
179
180
    return rc;
}

con's avatar
con committed
181
182
183
184
// ------------- SubversionPlugin
SubversionPlugin *SubversionPlugin::m_subversionPluginInstance = 0;

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

SubversionPlugin::~SubversionPlugin()
{
217
    delete m_client;
218
    cleanCommitMessageFile();
con's avatar
con committed
219
220
}

221
void SubversionPlugin::cleanCommitMessageFile()
con's avatar
con committed
222
{
223
224
225
    if (!m_commitMessageFileName.isEmpty()) {
        QFile::remove(m_commitMessageFileName);
        m_commitMessageFileName.clear();
226
        m_commitRepository.clear();
con's avatar
con committed
227
228
229
    }
}

230
231
232
233
234
bool SubversionPlugin::isCommitEditorOpen() const
{
    return !m_commitMessageFileName.isEmpty();
}

hjk's avatar
hjk committed
235
const VcsBaseSubmitEditorParameters submitParameters = {
con's avatar
con committed
236
    Subversion::Constants::SUBVERSION_SUBMIT_MIMETYPE,
237
238
    Subversion::Constants::SUBVERSIONCOMMITEDITOR_ID,
    Subversion::Constants::SUBVERSIONCOMMITEDITOR_DISPLAY_NAME,
239
    Subversion::Constants::SUBVERSIONCOMMITEDITOR,
hjk's avatar
hjk committed
240
    VcsBaseSubmitEditorParameters::DiffFiles
con's avatar
con committed
241
242
};

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

    using namespace Core::Constants;
    using namespace ExtensionSystem;

252
    initializeVcs(new SubversionControl(this));
253

con's avatar
con committed
254
255
    m_subversionPluginInstance = this;

hjk's avatar
hjk committed
256
    if (!MimeDatabase::addMimeTypes(QLatin1String(":/trolltech.subversion/Subversion.mimetypes.xml"), errorMessage))
con's avatar
con committed
257
258
        return false;

hjk's avatar
hjk committed
259
    m_settings.readSettings(ICore::settings());
260
    m_client = new SubversionClient(&m_settings);
con's avatar
con committed
261

262
    addAutoReleasedObject(new SettingsPage);
con's avatar
con committed
263

264
    addAutoReleasedObject(new SubversionSubmitEditorFactory(&submitParameters));
con's avatar
con committed
265
266

    static const char *describeSlot = SLOT(describe(QString,QString));
hjk's avatar
hjk committed
267
    const int editorCount = sizeof(editorParameters) / sizeof(editorParameters[0]);
268
269
    for (int i = 0; i < editorCount; i++)
        addAutoReleasedObject(new SubversionEditorFactory(editorParameters + i, this, describeSlot));
con's avatar
con committed
270

271
272
    addAutoReleasedObject(new CheckoutWizard);

Friedemann Kleint's avatar
Friedemann Kleint committed
273
    const QString prefix = QLatin1String("svn");
hjk's avatar
hjk committed
274
    m_commandLocator = new CommandLocator("Subversion", prefix, prefix);
Friedemann Kleint's avatar
Friedemann Kleint committed
275
276
    addAutoReleasedObject(m_commandLocator);

hjk's avatar
hjk committed
277
278
    // Register actions
    ActionContainer *toolsContainer = ActionManager::actionContainer(M_TOOLS);
con's avatar
con committed
279

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

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

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

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

314
    subversionMenu->addSeparator(globalcontext);
con's avatar
con committed
315

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

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

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

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

350
    subversionMenu->addSeparator(globalcontext);
351

hjk's avatar
hjk committed
352
353
    m_diffProjectAction = new ParameterAction(tr("Diff Project"), tr("Diff Project \"%1\""), ParameterAction::EnabledWithParameter, this);
    command = ActionManager::registerAction(m_diffProjectAction, CMD_ID_DIFF_PROJECT,
354
355
356
357
        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
358
    m_commandLocator->appendCommand(command);
con's avatar
con committed
359

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

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

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

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

389
    subversionMenu->addSeparator(globalcontext);
390

391
    m_diffRepositoryAction = new QAction(tr("Diff Repository"), this);
hjk's avatar
hjk committed
392
    command = ActionManager::registerAction(m_diffRepositoryAction, CMD_ID_REPOSITORYDIFF, globalcontext);
393
394
395
396
397
    connect(m_diffRepositoryAction, SIGNAL(triggered()), this, SLOT(diffRepository()));
    subversionMenu->addAction(command);
    m_commandLocator->appendCommand(command);

    m_statusRepositoryAction = new QAction(tr("Repository Status"), this);
hjk's avatar
hjk committed
398
    command = ActionManager::registerAction(m_statusRepositoryAction, CMD_ID_REPOSITORYSTATUS, globalcontext);
399
400
401
402
403
    connect(m_statusRepositoryAction, SIGNAL(triggered()), this, SLOT(statusRepository()));
    subversionMenu->addAction(command);
    m_commandLocator->appendCommand(command);

    m_logRepositoryAction = new QAction(tr("Log Repository"), this);
hjk's avatar
hjk committed
404
    command = ActionManager::registerAction(m_logRepositoryAction, CMD_ID_REPOSITORYLOG, globalcontext);
405
406
    connect(m_logRepositoryAction, SIGNAL(triggered()), this, SLOT(logRepository()));
    subversionMenu->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
407
    m_commandLocator->appendCommand(command);
408

409
    m_updateRepositoryAction = new QAction(tr("Update Repository"), this);
hjk's avatar
hjk committed
410
    command = ActionManager::registerAction(m_updateRepositoryAction, CMD_ID_REPOSITORYUPDATE, globalcontext);
411
    connect(m_updateRepositoryAction, SIGNAL(triggered()), this, SLOT(updateRepository()));
412
    subversionMenu->addAction(command);
413
    m_commandLocator->appendCommand(command);
414
415

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

422
    m_describeAction = new QAction(tr("Describe..."), this);
hjk's avatar
hjk committed
423
    command = ActionManager::registerAction(m_describeAction, CMD_ID_DESCRIBE, globalcontext);
424
425
426
    connect(m_describeAction, SIGNAL(triggered()), this, SLOT(slotDescribe()));
    subversionMenu->addAction(command);

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

con's avatar
con committed
434
    // Actions of the submit editor
hjk's avatar
hjk committed
435
    Context svncommitcontext(Constants::SUBVERSIONCOMMITEDITOR);
con's avatar
con committed
436

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

hjk's avatar
hjk committed
442
443
    m_submitDiffAction = new QAction(VcsBaseSubmitEditor::diffIcon(), tr("Diff &Selected Files"), this);
    command = ActionManager::registerAction(m_submitDiffAction , Constants::DIFF_SELECTED, svncommitcontext);
con's avatar
con committed
444
445

    m_submitUndoAction = new QAction(tr("&Undo"), this);
hjk's avatar
hjk committed
446
    command = ActionManager::registerAction(m_submitUndoAction, Core::Constants::UNDO, svncommitcontext);
con's avatar
con committed
447
448

    m_submitRedoAction = new QAction(tr("&Redo"), this);
hjk's avatar
hjk committed
449
    command = ActionManager::registerAction(m_submitRedoAction, Core::Constants::REDO, svncommitcontext);
con's avatar
con committed
450
451
452
453

    return true;
}

454
bool SubversionPlugin::submitEditorAboutToClose()
con's avatar
con committed
455
{
456
    if (!isCommitEditorOpen())
con's avatar
con committed
457
458
        return true;

459
460
    SubversionSubmitEditor *editor = qobject_cast<SubversionSubmitEditor *>(submitEditor());
    QTC_ASSERT(editor, return true);
hjk's avatar
hjk committed
461
    IDocument *editorDocument = editor->document();
462
    QTC_ASSERT(editorDocument, return true);
con's avatar
con committed
463
464
465

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

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

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

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

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

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

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

    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
555
556
557
558
}

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

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

con's avatar
con committed
586
587
void SubversionPlugin::revertCurrentFile()
{
hjk's avatar
hjk committed
588
    const VcsBasePluginState state = currentState();
589
    QTC_ASSERT(state.hasFile(), return);
con's avatar
con committed
590
591

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

594
    const SubversionResponse diffResponse =
595
            runSvn(state.currentFileTopLevel(), args, m_settings.timeOutMs(), 0);
con's avatar
con committed
596
597
598
599
600
    if (diffResponse.error)
        return;

    if (diffResponse.stdOut.isEmpty())
        return;
hjk's avatar
hjk committed
601
    if (QMessageBox::warning(ICore::dialogParent(), QLatin1String("svn revert"),
602
                             tr("The file has been changed. Do you want to revert it?"),
con's avatar
con committed
603
604
605
                             QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
        return;

606

hjk's avatar
hjk committed
607
    FileChangeBlocker fcb(state.currentFile());
con's avatar
con committed
608
609
610

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

613
    const SubversionResponse revertResponse =
614
            runSvn(state.currentFileTopLevel(), args, m_settings.timeOutMs(),
615
616
                   SshPasswordPrompt|ShowStdOutInLogWindow);

617
    if (!revertResponse.error)
618
        subVersionControl()->emitFilesChanged(QStringList(state.currentFile()));
con's avatar
con committed
619
620
621
622
}

void SubversionPlugin::diffProject()
{
hjk's avatar
hjk committed
623
    const VcsBasePluginState state = currentState();
624
    QTC_ASSERT(state.hasProject(), return);
625
626
627
    const QString relativeProject = state.relativeCurrentProject();
    m_client->diff(state.currentProjectTopLevel(),
                      relativeProject.isEmpty() ? QStringList() : QStringList(relativeProject));
con's avatar
con committed
628
629
630
631
}

void SubversionPlugin::diffCurrentFile()
{
hjk's avatar
hjk committed
632
    const VcsBasePluginState state = currentState();
633
    QTC_ASSERT(state.hasFile(), return);
634
    m_client->diff(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()));
con's avatar
con committed
635
636
637
638
}

void SubversionPlugin::startCommitCurrentFile()
{
hjk's avatar
hjk committed
639
    const VcsBasePluginState state = currentState();
640
    QTC_ASSERT(state.hasFile(), return);
641
    startCommit(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()));
con's avatar
con committed
642
643
644
645
}

void SubversionPlugin::startCommitAll()
{
hjk's avatar
hjk committed
646
    const VcsBasePluginState state = currentState();
647
648
    QTC_ASSERT(state.hasTopLevel(), return);
    startCommit(state.topLevel());
con's avatar
con committed
649
650
}

651
652
void SubversionPlugin::startCommitProject()
{
hjk's avatar
hjk committed
653
    const VcsBasePluginState state = currentState();
654
655
656
657
    QTC_ASSERT(state.hasProject(), return);
    startCommit(state.currentProjectPath());
}

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

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

673
    const SubversionResponse response =
674
            runSvn(workingDir, args, m_settings.timeOutMs(), 0);
con's avatar
con committed
675
676
    if (response.error)
        return;
677

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

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"));
712
    args << QLatin1String(nonInteractiveOptionC) << QLatin1String("--file") << messageFile;
con's avatar
con committed
713
    args.append(subVersionFileList);
714
    const SubversionResponse response =
715
            runSvn(m_commitRepository, args, 10 * m_settings.timeOutMs(),
716
                   SshPasswordPrompt|ShowStdOutInLogWindow);
con's avatar
con committed
717
718
719
720
721
    return !response.error ;
}

void SubversionPlugin::filelogCurrentFile()
{
hjk's avatar
hjk committed
722
    const VcsBasePluginState state = currentState();
723
    QTC_ASSERT(state.hasFile(), return);
724
    filelog(state.currentFileTopLevel(), state.relativeCurrentFile(), true);
725
726
727
728
}

void SubversionPlugin::logProject()
{
hjk's avatar
hjk committed
729
    const VcsBasePluginState state = currentState();
730
    QTC_ASSERT(state.hasProject(), return);
731
732
733
734
735
    filelog(state.currentProjectTopLevel(), state.relativeCurrentProject());
}

void SubversionPlugin::logRepository()
{
hjk's avatar
hjk committed
736
    const VcsBasePluginState state = currentState();
737
    QTC_ASSERT(state.hasTopLevel(), return);
738
    filelog(state.topLevel());
con's avatar
con committed
739
740
}

741
742
void SubversionPlugin::diffRepository()
{
hjk's avatar
hjk committed
743
    const VcsBasePluginState state = currentState();
744
    QTC_ASSERT(state.hasTopLevel(), return);
745
    m_client->diff(state.topLevel(), QStringList());
746
747
748
749
}

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

void SubversionPlugin::updateRepository()
{
hjk's avatar
hjk committed
757
    const VcsBasePluginState state = currentState();
758
    QTC_ASSERT(state.hasTopLevel(), return);
759
760
761
    svnUpdate(state.topLevel());
}

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

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

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

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

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

void SubversionPlugin::updateProject()
{
hjk's avatar
hjk committed
818
    const VcsBasePluginState state = currentState();
819
    QTC_ASSERT(state.hasProject(), return);
820
821
    svnUpdate(state.currentProjectTopLevel(), state.relativeCurrentProject());
}
con's avatar
con committed
822

823
void SubversionPlugin::svnUpdate(const QString &workingDir, const QString &relativePath)
824
{
con's avatar
con committed
825
    QStringList args(QLatin1String("update"));
826
    args.push_back(QLatin1String(nonInteractiveOptionC));
827
828
    if (!relativePath.isEmpty())
        args.append(relativePath);
829
        const SubversionResponse response =
830
                runSvn(workingDir, args, 10 * m_settings.timeOutMs(),
831
                       SshPasswordPrompt|ShowStdOutInLogWindow);
832
    if (!response.error)
833
        subVersionControl()->emitRepositoryChanged(workingDir);
con's avatar
con committed
834
835
836
837
}

void SubversionPlugin::annotateCurrentFile()
{