subversionplugin.cpp 58.8 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 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 36 37
#include "subversionplugin.h"

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

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

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

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

#include <locator/commandlocator.h>

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

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

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

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

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_SEPARATOR2[]         = "Subversion.Separator2";
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
    cleanCommitMessageFile();
con's avatar
con committed
214 215
}

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

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

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

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

    using namespace Core::Constants;
    using namespace ExtensionSystem;

247
    initializeVcs(new SubversionControl(this));
248

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

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

254
    m_settings.readSettings(Core::ICore::settings());
con's avatar
con committed
255

256
    addAutoReleasedObject(new SettingsPage);
con's avatar
con committed
257

258
    addAutoReleasedObject(new SubversionSubmitEditorFactory(&submitParameters));
con's avatar
con committed
259 260

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

265 266
    addAutoReleasedObject(new CheckoutWizard);

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

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

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

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

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

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

309
    subversionMenu->addSeparator(globalcontext);
con's avatar
con committed
310

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

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

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

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

345
    subversionMenu->addSeparator(globalcontext);
346

347
    m_diffProjectAction = new Utils::ParameterAction(tr("Diff Project"), tr("Diff Project \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
348
    command = Core::ActionManager::registerAction(m_diffProjectAction, CMD_ID_DIFF_PROJECT,
349 350 351 352
        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
353
    m_commandLocator->appendCommand(command);
con's avatar
con committed
354

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

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

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

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

384
    subversionMenu->addSeparator(globalcontext);
385

386
    m_diffRepositoryAction = new QAction(tr("Diff Repository"), this);
Eike Ziller's avatar
Eike Ziller committed
387
    command = Core::ActionManager::registerAction(m_diffRepositoryAction, CMD_ID_REPOSITORYDIFF, globalcontext);
388 389 390 391 392
    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
393
    command = Core::ActionManager::registerAction(m_statusRepositoryAction, CMD_ID_REPOSITORYSTATUS, globalcontext);
394 395 396 397 398
    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
399
    command = Core::ActionManager::registerAction(m_logRepositoryAction, CMD_ID_REPOSITORYLOG, globalcontext);
400 401
    connect(m_logRepositoryAction, SIGNAL(triggered()), this, SLOT(logRepository()));
    subversionMenu->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
402
    m_commandLocator->appendCommand(command);
403

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

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

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

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

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

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

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

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

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

    return true;
}

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

454 455 456 457
    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
458 459 460

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

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

499
void SubversionPlugin::diffCommitFiles(const QStringList &files)
con's avatar
con committed
500
{
501
    svnDiff(m_commitRepository, files);
con's avatar
con committed
502 503
}

504 505 506 507 508 509 510 511 512 513 514
// Collect all parameters required for a diff to be able to associate them
// with a diff editor and re-run the diff with parameters.
struct SubversionDiffParameters
{
    QString workingDir;
    QStringList arguments;
    QStringList files;
    QString diffName;
};

// Parameter widget controlling whitespace diff mode, associated with a parameter
hjk's avatar
hjk committed
515
class SubversionDiffParameterWidget : public VcsBase::VcsBaseEditorParameterWidget
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
{
    Q_OBJECT
public:
    explicit SubversionDiffParameterWidget(const SubversionDiffParameters &p, QWidget *parent = 0);

signals:
    void reRunDiff(const Subversion::Internal::SubversionDiffParameters &);

private slots:
    void triggerReRun();

private:
    const SubversionDiffParameters m_parameters;
};

SubversionDiffParameterWidget::SubversionDiffParameterWidget(const SubversionDiffParameters &p, QWidget *parent) :
hjk's avatar
hjk committed
532
    VcsBase::VcsBaseEditorParameterWidget(parent), m_parameters(p)
533 534
{
    setBaseArguments(p.arguments);
535
    addToggleButton(QLatin1String("w"), tr("Ignore whitespace"));
536 537 538 539 540 541 542 543 544 545 546 547 548
    connect(this, SIGNAL(argumentsChanged()), this, SLOT(triggerReRun()));
}

void SubversionDiffParameterWidget::triggerReRun()
{
    SubversionDiffParameters effectiveParameters = m_parameters;
    // Subversion wants" -x -<ext-args>", default being -u
    const QStringList a = arguments();
    if (!a.isEmpty())
        effectiveParameters.arguments << QLatin1String("-x") << (QLatin1String("-u") + a.join(QString()));
    emit reRunDiff(effectiveParameters);
}

549 550
static inline void setDiffBaseDirectory(Core::IEditor *editor, const QString &db)
{
hjk's avatar
hjk committed
551
    if (VcsBase::VcsBaseEditorWidget *ve = qobject_cast<VcsBase::VcsBaseEditorWidget*>(editor->widget()))
552 553 554
        ve->setDiffBaseDirectory(db);
}

555
void SubversionPlugin::svnDiff(const QString &workingDir, const QStringList &files, QString diffname)
556 557 558 559 560 561 562 563 564
{
    SubversionDiffParameters p;
    p.workingDir = workingDir;
    p.files = files;
    p.diffName = diffname;
    svnDiff(p);
}

void SubversionPlugin::svnDiff(const Subversion::Internal::SubversionDiffParameters &p)
con's avatar
con committed
565 566
{
    if (Subversion::Constants::debug)
567
        qDebug() << Q_FUNC_INFO << p.files << p.diffName;
hjk's avatar
hjk committed
568 569
    const QString source = VcsBase::VcsBaseEditorWidget::getSource(p.workingDir, p.files);
    QTextCodec *codec = source.isEmpty() ? static_cast<QTextCodec *>(0) : VcsBase::VcsBaseEditorWidget::getCodec(source);
con's avatar
con committed
570

571 572
    const QString diffName = p.files.count() == 1 && p.diffName.isEmpty() ?
                             QFileInfo(p.files.front()).fileName() : p.diffName;
con's avatar
con committed
573 574

    QStringList args(QLatin1String("diff"));
575 576 577
    Version v = svnVersion();
    if (v.majorVersion >= 1 && v.minorVersion >= 7) // --internal-diff is new in v1.7.0
        args.append(QLatin1String("--internal-diff"));
578 579
    args.append(p.arguments);
    args << p.files;
con's avatar
con committed
580

581
    const SubversionResponse response =
582
            runSvn(p.workingDir, args, m_settings.timeOutMs(), 0, codec);
con's avatar
con committed
583 584 585 586 587
    if (response.error)
        return;

    // diff of a single file? re-use an existing view if possible to support
    // the common usage pattern of continuously changing and diffing a file
hjk's avatar
hjk committed
588
    const QString tag = VcsBase::VcsBaseEditorWidget::editorTag(VcsBase::DiffOutput, p.workingDir, p.files);
589
    // Show in the same editor if diff has been executed before
hjk's avatar
hjk committed
590
    if (Core::IEditor *existingEditor = VcsBase::VcsBaseEditorWidget::locateEditorByTag(tag)) {
591
        existingEditor->document()->setContents(response.stdOut.toUtf8());
Eike Ziller's avatar
Eike Ziller committed
592
        Core::EditorManager::activateEditor(existingEditor);
593 594
        setDiffBaseDirectory(existingEditor, p.workingDir);
        return;
con's avatar
con committed
595
    }
596
    const QString title = QString::fromLatin1("svn diff %1").arg(diffName);
hjk's avatar
hjk committed
597
    Core::IEditor *editor = showOutputInEditor(title, response.stdOut, VcsBase::DiffOutput, source, codec);
598
    setDiffBaseDirectory(editor, p.workingDir);
hjk's avatar
hjk committed
599
    VcsBase::VcsBaseEditorWidget::tagEditor(editor, tag);
600
    SubversionEditor *diffEditorWidget = qobject_cast<SubversionEditor *>(editor->widget());
601
    QTC_ASSERT(diffEditorWidget, return);
602 603 604 605 606 607

    // Wire up the parameter widget to trigger a re-run on
    // parameter change and 'revert' from inside the diff editor.
    SubversionDiffParameterWidget *pw = new SubversionDiffParameterWidget(p);
    connect(pw, SIGNAL(reRunDiff(Subversion::Internal::SubversionDiffParameters)),
            this, SLOT(svnDiff(Subversion::Internal::SubversionDiffParameters)));
hjk's avatar
hjk committed
608
    connect(diffEditorWidget, SIGNAL(diffChunkReverted(VcsBase::DiffChunk)),
609 610
            pw, SLOT(triggerReRun()));
    diffEditorWidget->setConfigurationWidget(pw);
con's avatar
con committed
611 612 613 614
}

SubversionSubmitEditor *SubversionPlugin::openSubversionSubmitEditor(const QString &fileName)
{
hjk's avatar
hjk committed
615
    Core::IEditor *editor = Core::EditorManager::openEditor(fileName,
Eike Ziller's avatar
Eike Ziller committed
616
                                                            Constants::SUBVERSIONCOMMITEDITOR_ID);
con's avatar
con committed
617
    SubversionSubmitEditor *submitEditor = qobject_cast<SubversionSubmitEditor*>(editor);
618
    QTC_CHECK(submitEditor);
619
    setSubmitEditor(submitEditor);
620
    submitEditor->registerActions(m_submitUndoAction, m_submitRedoAction, m_submitCurrentLogAction, m_submitDiffAction);
621
    connect(submitEditor, SIGNAL(diffSelectedFiles(QStringList)), this, SLOT(diffCommitFiles(QStringList)));
622
    submitEditor->setCheckScriptWorkingDirectory(m_commitRepository);
con's avatar
con committed
623 624 625
    return submitEditor;
}

hjk's avatar
hjk committed
626
void SubversionPlugin::updateActions(VcsBase::VcsBasePlugin::ActionState as)
con's avatar
con committed
627
{
Friedemann Kleint's avatar
Friedemann Kleint committed
628 629
    if (!enableMenuAction(as, m_menuAction)) {
        m_commandLocator->setEnabled(false);
630
        return;
Friedemann Kleint's avatar
Friedemann Kleint committed
631 632 633 634
    }
    const bool hasTopLevel = currentState().hasTopLevel();
    m_commandLocator->setEnabled(hasTopLevel);
    m_logRepositoryAction->setEnabled(hasTopLevel);
635

636 637 638 639
    const QString projectName = currentState().currentProjectName();
    m_diffProjectAction->setParameter(projectName);
    m_statusProjectAction->setParameter(projectName);
    m_updateProjectAction->setParameter(projectName);
640
    m_logProjectAction->setParameter(projectName);
641
    m_commitProjectAction->setParameter(projectName);
642 643 644 645

    const bool repoEnabled = currentState().hasTopLevel();
    m_commitAllAction->setEnabled(repoEnabled);
    m_describeAction->setEnabled(repoEnabled);
646
    m_revertRepositoryAction->setEnabled(repoEnabled);
647 648 649
    m_diffRepositoryAction->setEnabled(repoEnabled);
    m_statusRepositoryAction->setEnabled(repoEnabled);
    m_updateRepositoryAction->setEnabled(repoEnabled);
650 651 652 653 654 655 656 657 658 659

    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
660 661 662 663
}

void SubversionPlugin::addCurrentFile()
{
hjk's avatar
hjk committed
664
    const VcsBase::VcsBasePluginState state = currentState();
665
    QTC_ASSERT(state.hasFile(), return);
666
    vcsAdd(state.currentFileTopLevel(), state.relativeCurrentFile());
con's avatar
con committed
667 668
}

669 670
void SubversionPlugin::revertAll()
{
hjk's avatar
hjk committed
671
    const VcsBase::VcsBasePluginState state = currentState();
672
    QTC_ASSERT(state.hasTopLevel(), return);
673
    const QString title = tr("Revert repository");
Tobias Hunger's avatar
Tobias Hunger committed
674
    if (QMessageBox::warning(0, title, tr("Revert all pending changes to the repository?"),
675 676 677 678 679
                             QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
        return;
    // NoteL: Svn "revert ." doesn not work.
    QStringList args;
    args << QLatin1String("revert") << QLatin1String("--recursive") << state.topLevel();
680
    const SubversionResponse revertResponse =
681
            runSvn(state.topLevel(), args, m_settings.timeOutMs(),
682
                   SshPasswordPrompt|ShowStdOutInLogWindow);
683
    if (revertResponse.error)
684
        QMessageBox::warning(0, title, tr("Revert failed: %1").arg(revertResponse.message), QMessageBox::Ok);
685
    else
686 687 688
        subVersionControl()->emitRepositoryChanged(state.topLevel());
}

con's avatar
con committed
689 690
void SubversionPlugin::revertCurrentFile()
{
hjk's avatar
hjk committed
691
    const VcsBase::VcsBasePluginState state = currentState();
692
    QTC_ASSERT(state.hasFile(), return);
con's avatar
con committed
693 694

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

697
    const SubversionResponse diffResponse =
698
            runSvn(state.currentFileTopLevel(), args, m_settings.timeOutMs(), 0);
con's avatar
con committed
699 700 701 702 703
    if (diffResponse.error)
        return;

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

708

709
    Core::FileChangeBlocker fcb(state.currentFile());
con's avatar
con committed
710 711 712

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

715
    const SubversionResponse revertResponse =
716
            runSvn(state.currentFileTopLevel(), args, m_settings.timeOutMs(),
717 718
                   SshPasswordPrompt|ShowStdOutInLogWindow);

719
    if (!revertResponse.error)
720
        subVersionControl()->emitFilesChanged(QStringList(state.currentFile()));
con's avatar
con committed
721 722 723 724
}

void SubversionPlugin::diffProject()
{
hjk's avatar
hjk committed
725
    const VcsBase::VcsBasePluginState state = currentState();
726
    QTC_ASSERT(state.hasProject(), return);
727 728
    svnDiff(state.currentProjectTopLevel(), QStringList(state.relativeCurrentProject()),
            state.currentProjectName());
con's avatar
con committed
729 730 731 732
}

void SubversionPlugin::diffCurrentFile()
{
hjk's avatar
hjk committed
733
    const VcsBase::VcsBasePluginState state = currentState();
734
    QTC_ASSERT(state.hasFile(), return);
735
    svnDiff(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()));
con's avatar
con committed
736 737 738 739
}

void SubversionPlugin::startCommitCurrentFile()
{
hjk's avatar
hjk committed
740
    const VcsBase::VcsBasePluginState state = currentState();
741
    QTC_ASSERT(state.hasFile(), return);
742
    startCommit(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()));
con's avatar
con committed
743 744 745 746
}

void SubversionPlugin::startCommitAll()
{
hjk's avatar
hjk committed
747
    const VcsBase::VcsBasePluginState state = currentState();
748 749
    QTC_ASSERT(state.hasTopLevel(), return);
    startCommit(state.topLevel());
con's avatar
con committed
750 751
}

752 753
void SubversionPlugin::startCommitProject()
{
hjk's avatar
hjk committed
754
    const VcsBase::VcsBasePluginState state = currentState();
755 756 757 758
    QTC_ASSERT(state.hasProject(), return);
    startCommit(state.currentProjectPath());
}

con's avatar
con committed
759 760 761
/* Start commit of files of a single repository by displaying
 * template and files in a submit editor. On closing, the real
 * commit will start. */
762
void SubversionPlugin::startCommit(const QString &workingDir, const QStringList &files)
con's avatar
con committed
763
{
764
    if (raiseSubmitEditor())
765
        return;
766
    if (isCommitEditorOpen()) {
hjk's avatar
hjk committed
767
        VcsBase::VcsBaseOutputWindow::instance()->appendWarning(tr("Another commit is currently being executed."));
con's avatar
con committed
768 769 770 771 772 773
        return;
    }

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

774
    const SubversionResponse response =
775
            runSvn(workingDir, args, m_settings.timeOutMs(), 0);
con's avatar
con committed
776 777
    if (response.error)
        return;
778

con's avatar
con committed
779
    // Get list of added/modified/deleted files
780
    const StatusList statusOutput = parseStatusOutput(response.stdOut);
con's avatar
con committed
781
    if (statusOutput.empty()) {
hjk's avatar
hjk committed
782
        VcsBase::VcsBaseOutputWindow::instance()->appendWarning(tr("There are no modified files."));
con's avatar
con committed
783 784
        return;
    }
785
    m_commitRepository = workingDir;
con's avatar
con committed
786 </