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

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

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

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

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

67
68
69
70
71
72
73
74
75
76
77
78
79
#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>
80
#include <limits.h>
con's avatar
con committed
81

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

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

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

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

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

116
117


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

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

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

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

        }
    }
    return changeSet;
}

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

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

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

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

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

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

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

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

    using namespace Core::Constants;
    using namespace ExtensionSystem;

253
    initializeVcs(new SubversionControl(this));
254

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

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

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

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

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

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

272
    addAutoReleasedObject(new CheckoutWizardFactory);
273

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

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

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

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

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

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

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

hjk's avatar
hjk committed
317
318
    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
319
        globalcontext);
320
    command->setAttribute(Core::Command::CA_UpdateText);
hjk's avatar
hjk committed
321
    command->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+S,Meta+A") : tr("Alt+S,Alt+A")));
322
    connect(m_addAction, SIGNAL(triggered()), this, SLOT(addCurrentFile()));
con's avatar
con committed
323
    subversionMenu->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
324
    m_commandLocator->appendCommand(command);
con's avatar
con committed
325

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

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

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

351
    subversionMenu->addSeparator(globalcontext);
352

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

hjk's avatar
hjk committed
361
362
    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
363
        globalcontext);
364
365
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_statusProjectAction, SIGNAL(triggered()), this, SLOT(projectStatus()));
con's avatar
con committed
366
    subversionMenu->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
367
    m_commandLocator->appendCommand(command);
con's avatar
con committed
368

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

hjk's avatar
hjk committed
376
377
    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
378
    connect(m_updateProjectAction, SIGNAL(triggered()), this, SLOT(updateProject()));
379
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
380
    subversionMenu->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
381
    m_commandLocator->appendCommand(command);
con's avatar
con committed
382

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

390
    subversionMenu->addSeparator(globalcontext);
391

392
    m_diffRepositoryAction = new QAction(tr("Diff Repository"), this);
hjk's avatar
hjk committed
393
    command = ActionManager::registerAction(m_diffRepositoryAction, CMD_ID_REPOSITORYDIFF, globalcontext);
394
395
396
397
398
    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
399
    command = ActionManager::registerAction(m_statusRepositoryAction, CMD_ID_REPOSITORYSTATUS, globalcontext);
400
401
402
403
404
    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
405
    command = ActionManager::registerAction(m_logRepositoryAction, CMD_ID_REPOSITORYLOG, globalcontext);
406
407
    connect(m_logRepositoryAction, SIGNAL(triggered()), this, SLOT(logRepository()));
    subversionMenu->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
408
    m_commandLocator->appendCommand(command);
409

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

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

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

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

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

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

hjk's avatar
hjk committed
443
444
    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
445
446

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

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

    return true;
}

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

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

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

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

509
void SubversionPlugin::diffCommitFiles(const QStringList &files)
con's avatar
con committed
510
{
511
    m_client->diff(m_commitRepository, files);
512
513
}

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

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

536
537
538
539
    const QString projectName = currentState().currentProjectName();
    m_diffProjectAction->setParameter(projectName);
    m_statusProjectAction->setParameter(projectName);
    m_updateProjectAction->setParameter(projectName);
540
    m_logProjectAction->setParameter(projectName);
541
    m_commitProjectAction->setParameter(projectName);
542
543
544
545

    const bool repoEnabled = currentState().hasTopLevel();
    m_commitAllAction->setEnabled(repoEnabled);
    m_describeAction->setEnabled(repoEnabled);
546
    m_revertRepositoryAction->setEnabled(repoEnabled);
547
548
549
    m_diffRepositoryAction->setEnabled(repoEnabled);
    m_statusRepositoryAction->setEnabled(repoEnabled);
    m_updateRepositoryAction->setEnabled(repoEnabled);
550
551
552
553
554
555
556
557
558
559

    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
560
561
562
563
}

void SubversionPlugin::addCurrentFile()
{
hjk's avatar
hjk committed
564
    const VcsBasePluginState state = currentState();
565
    QTC_ASSERT(state.hasFile(), return);
566
    vcsAdd(state.currentFileTopLevel(), state.relativeCurrentFile());
con's avatar
con committed
567
568
}

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

con's avatar
con committed
591
592
void SubversionPlugin::revertCurrentFile()
{
hjk's avatar
hjk committed
593
    const VcsBasePluginState state = currentState();
594
    QTC_ASSERT(state.hasFile(), return);
con's avatar
con committed
595
596

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

599
    const SubversionResponse diffResponse =
600
            runSvn(state.currentFileTopLevel(), args, m_settings.timeOutMs(), 0);
con's avatar
con committed
601
602
603
604
605
    if (diffResponse.error)
        return;

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

611

hjk's avatar
hjk committed
612
    FileChangeBlocker fcb(state.currentFile());
con's avatar
con committed
613
614
615

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

618
    const SubversionResponse revertResponse =
619
            runSvn(state.currentFileTopLevel(), args, m_settings.timeOutMs(),
620
621
                   SshPasswordPrompt|ShowStdOutInLogWindow);

622
    if (!revertResponse.error)
623
        subVersionControl()->emitFilesChanged(QStringList(state.currentFile()));
con's avatar
con committed
624
625
626
627
}

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

void SubversionPlugin::diffCurrentFile()
{
hjk's avatar
hjk committed
637
    const VcsBasePluginState state = currentState();
638
    QTC_ASSERT(state.hasFile(), return);
639
    m_client->diff(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()));
con's avatar
con committed
640
641
642
643
}

void SubversionPlugin::startCommitCurrentFile()
{
hjk's avatar
hjk committed
644
    const VcsBasePluginState state = currentState();
645
    QTC_ASSERT(state.hasFile(), return);
646
    startCommit(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()));
con's avatar
con committed
647
648
649
650
}

void SubversionPlugin::startCommitAll()
{
hjk's avatar
hjk committed
651
    const VcsBasePluginState state = currentState();
652
653
    QTC_ASSERT(state.hasTopLevel(), return);
    startCommit(state.topLevel());
con's avatar
con committed
654
655
}

656
657
void SubversionPlugin::startCommitProject()
{
hjk's avatar
hjk committed
658
    const VcsBasePluginState state = currentState();
659
660
661
662
    QTC_ASSERT(state.hasProject(), return);
    startCommit(state.currentProjectPath());
}

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

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

678
    const SubversionResponse response =
679
            runSvn(workingDir, args, m_settings.timeOutMs(), 0);
con's avatar
con committed
680
681
    if (response.error)
        return;
682

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

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

void SubversionPlugin::filelogCurrentFile()
{
hjk's avatar
hjk committed
727
    const VcsBasePluginState state = currentState();
728
    QTC_ASSERT(state.hasFile(), return);
729
    filelog(state.currentFileTopLevel(), state.relativeCurrentFile(), true);
730
731
732
733
}

void SubversionPlugin::logProject()
{
hjk's avatar
hjk committed
734
    const VcsBasePluginState state = currentState();
735
    QTC_ASSERT(state.hasProject(), return);
736
737
738
739
740
    filelog(state.currentProjectTopLevel(), state.relativeCurrentProject());
}

void SubversionPlugin::logRepository()
{
hjk's avatar
hjk committed
741
    const VcsBasePluginState state = currentState();
742
    QTC_ASSERT(state.hasTopLevel(), return);
743
    filelog(state.topLevel());
con's avatar
con committed
744
745
}

746
747
void SubversionPlugin::diffRepository()
{
hjk's avatar
hjk committed
748
    const VcsBasePluginState state = currentState();
749
    QTC_ASSERT(state.hasTopLevel(), return);
750
    m_client->diff(state.topLevel(), QStringList());
751
752
753
754
}

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

void SubversionPlugin::updateRepository()
{
hjk's avatar
hjk committed
762
    const VcsBasePluginState state = currentState();
763
    QTC_ASSERT(state.hasTopLevel(), return);
764
765
766
    svnUpdate(state.topLevel());
}

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

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

Roman Kovalev's avatar
Roman Kovalev committed
794
795
    // subversion stores log in UTF-8 and returns it back in user system locale.
    // So we do not need to encode it.