gitplugin.cpp 60.3 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
#include "gitplugin.h"
hjk's avatar
hjk committed
31 32 33

#include "changeselectiondialog.h"
#include "commitdata.h"
con's avatar
con committed
34 35
#include "gitclient.h"
#include "gitconstants.h"
hjk's avatar
hjk committed
36
#include "giteditor.h"
con's avatar
con committed
37
#include "gitsubmiteditor.h"
hjk's avatar
hjk committed
38
#include "gitversioncontrol.h"
39
#include "branchdialog.h"
40
#include "remotedialog.h"
41
#include "clonewizard.h"
42
#include "gitorious/gitoriousclonewizard.h"
43
#include "stashdialog.h"
44
#include "settingspage.h"
45
#include "logchangedialog.h"
46
#include "mergetool.h"
Orgad Shaneh's avatar
Orgad Shaneh committed
47
#include "gitutils.h"
con's avatar
con committed
48

49
#include "gerrit/gerritplugin.h"
50

con's avatar
con committed
51 52
#include <coreplugin/icore.h>
#include <coreplugin/coreconstants.h>
53
#include <coreplugin/documentmanager.h>
54
#include <coreplugin/actionmanager/actionmanager.h>
55 56
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/command.h>
57
#include <coreplugin/id.h>
Orgad Shaneh's avatar
Orgad Shaneh committed
58
#include <coreplugin/infobar.h>
con's avatar
con committed
59
#include <coreplugin/editormanager/editormanager.h>
60
#include <coreplugin/editormanager/ieditor.h>
61
#include <coreplugin/locator/commandlocator.h>
62
#include <coreplugin/mimedatabase.h>
63
#include <coreplugin/vcsmanager.h>
64
#include <coreplugin/coreconstants.h>
hjk's avatar
hjk committed
65 66

#include <utils/qtcassert.h>
67
#include <utils/parameteraction.h>
hjk's avatar
hjk committed
68

con's avatar
con committed
69
#include <vcsbase/basevcseditorfactory.h>
70
#include <vcsbase/submitfilemodel.h>
con's avatar
con committed
71
#include <vcsbase/vcsbaseeditor.h>
72
#include <vcsbase/vcsbaseconstants.h>
con's avatar
con committed
73
#include <vcsbase/basevcssubmiteditorfactory.h>
74
#include <vcsbase/vcsoutputwindow.h>
75
#include <vcsbase/cleandialog.h>
con's avatar
con committed
76

77 78 79 80
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QtPlugin>
hjk's avatar
hjk committed
81

82 83 84
#include <QAction>
#include <QFileDialog>
#include <QMessageBox>
85
#include <QScopedPointer>
con's avatar
con committed
86

hjk's avatar
hjk committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
#ifdef WITH_TESTS
#include "clonewizardpage.h"
#include <QTest>
#endif

Q_DECLARE_METATYPE(Git::Internal::FileStates)

using namespace Core;
using namespace Utils;
using namespace VcsBase;

namespace Git {
namespace Internal {

const unsigned minimumRequiredVersion = 0x010702;
const char RC_GIT_MIME_XML[] = ":/git/Git.mimetypes.xml";
Orgad Shaneh's avatar
Orgad Shaneh committed
103

hjk's avatar
hjk committed
104
const VcsBaseEditorParameters editorParameters[] = {
con's avatar
con committed
105
{
106
    VcsBase::OtherContent,
107 108
    Git::Constants::GIT_COMMAND_LOG_EDITOR_ID,
    Git::Constants::GIT_COMMAND_LOG_EDITOR_DISPLAY_NAME,
109
    Git::Constants::C_GIT_COMMAND_LOG_EDITOR,
Orgad Shaneh's avatar
Orgad Shaneh committed
110
    "text/vnd.qtcreator.git.commandlog"},
hjk's avatar
hjk committed
111
{   VcsBase::LogOutput,
112 113
    Git::Constants::GIT_LOG_EDITOR_ID,
    Git::Constants::GIT_LOG_EDITOR_DISPLAY_NAME,
114
    Git::Constants::C_GIT_LOG_EDITOR,
Orgad Shaneh's avatar
Orgad Shaneh committed
115
    "text/vnd.qtcreator.git.log"},
hjk's avatar
hjk committed
116
{   VcsBase::AnnotateOutput,
117 118
    Git::Constants::GIT_BLAME_EDITOR_ID,
    Git::Constants::GIT_BLAME_EDITOR_DISPLAY_NAME,
119
    Git::Constants::C_GIT_BLAME_EDITOR,
Orgad Shaneh's avatar
Orgad Shaneh committed
120
    "text/vnd.qtcreator.git.annotation"},
121
{   VcsBase::OtherContent,
122 123 124 125
    Git::Constants::GIT_COMMIT_TEXT_EDITOR_ID,
    Git::Constants::GIT_COMMIT_TEXT_EDITOR_DISPLAY_NAME,
    Git::Constants::C_GIT_COMMIT_TEXT_EDITOR,
    "text/vnd.qtcreator.git.commit"},
126 127 128 129 130
{   VcsBase::OtherContent,
    Git::Constants::GIT_REBASE_EDITOR_ID,
    Git::Constants::GIT_REBASE_EDITOR_DISPLAY_NAME,
    Git::Constants::C_GIT_REBASE_EDITOR,
    "text/vnd.qtcreator.git.rebase"},
con's avatar
con committed
131 132 133 134 135
};


// GitPlugin

hjk's avatar
hjk committed
136
static GitPlugin *m_instance = 0;
con's avatar
con committed
137 138

GitPlugin::GitPlugin() :
Friedemann Kleint's avatar
Friedemann Kleint committed
139
    m_commandLocator(0),
con's avatar
con committed
140 141 142 143
    m_submitCurrentAction(0),
    m_diffSelectedFilesAction(0),
    m_undoAction(0),
    m_redoAction(0),
144
    m_menuAction(0),
145
    m_applyCurrentFilePatchAction(0),
con's avatar
con committed
146
    m_gitClient(0),
147
    m_submitActionTriggered(false)
con's avatar
con committed
148 149
{
    m_instance = this;
150 151
    m_fileActions.reserve(10);
    m_projectActions.reserve(10);
152
    m_repositoryActions.reserve(50);
con's avatar
con committed
153 154 155 156
}

GitPlugin::~GitPlugin()
{
157
    cleanCommitMessageFile();
con's avatar
con committed
158 159 160 161
    delete m_gitClient;
    m_instance = 0;
}

162
void GitPlugin::cleanCommitMessageFile()
con's avatar
con committed
163
{
164 165 166
    if (!m_commitMessageFileName.isEmpty()) {
        QFile::remove(m_commitMessageFileName);
        m_commitMessageFileName.clear();
con's avatar
con committed
167 168 169
    }
}

170 171 172 173 174
bool GitPlugin::isCommitEditorOpen() const
{
    return !m_commitMessageFileName.isEmpty();
}

con's avatar
con committed
175 176 177 178 179
GitPlugin *GitPlugin::instance()
{
    return m_instance;
}

hjk's avatar
hjk committed
180
const VcsBaseSubmitEditorParameters submitParameters = {
con's avatar
con committed
181
    Git::Constants::SUBMIT_MIMETYPE,
182 183
    Git::Constants::GITSUBMITEDITOR_ID,
    Git::Constants::GITSUBMITEDITOR_DISPLAY_NAME,
184
    Git::Constants::C_GITSUBMITEDITOR,
hjk's avatar
hjk committed
185
    VcsBaseSubmitEditorParameters::DiffRows
con's avatar
con committed
186 187
};

188
// Create a parameter action
hjk's avatar
hjk committed
189 190 191 192
ParameterAction *GitPlugin::createParameterAction(ActionContainer *ac,
                                                  const QString &defaultText, const QString &parameterText,
                                                  Id id, const Context &context,
                                                  bool addToLocator, const QKeySequence &keys)
193
{
hjk's avatar
hjk committed
194 195
    auto action = new ParameterAction(defaultText, parameterText, ParameterAction::EnabledWithParameter, this);
    Core::Command *command = ActionManager::registerAction(action, id, context);
hjk's avatar
hjk committed
196 197
    if (!keys.isEmpty())
        command->setDefaultKeySequence(keys);
198 199 200 201
    command->setAttribute(Core::Command::CA_UpdateText);
    ac->addAction(command);
    if (addToLocator)
        m_commandLocator->appendCommand(command);
hjk's avatar
hjk committed
202
    return action;
203 204 205
}

// Create an action to act on a file with a slot.
hjk's avatar
hjk committed
206 207 208 209
QAction *GitPlugin::createFileAction(ActionContainer *ac,
                                     const QString &defaultText, const QString &parameterText,
                                     Id id, const Context &context, bool addToLocator,
                                     const char *pluginSlot, const QKeySequence &keys)
210
{
hjk's avatar
hjk committed
211 212 213 214
    ParameterAction *action = createParameterAction(ac, defaultText, parameterText, id, context, addToLocator, keys);
    m_fileActions.push_back(action);
    connect(action, SIGNAL(triggered()), this, pluginSlot);
    return action;
215 216 217
}

// Create an action to act on a project with slot.
hjk's avatar
hjk committed
218 219 220 221
QAction *GitPlugin::createProjectAction(ActionContainer *ac,
                                        const QString &defaultText, const QString &parameterText,
                                        Id id, const Context &context, bool addToLocator,
                                        const char *pluginSlot, const QKeySequence &keys)
222
{
hjk's avatar
hjk committed
223 224 225 226
    ParameterAction *action = createParameterAction(ac, defaultText, parameterText, id, context, addToLocator, keys);
    m_projectActions.push_back(action);
    connect(action, SIGNAL(triggered()), this, pluginSlot);
    return action;
227 228 229
}

// Create an action to act on the repository
hjk's avatar
hjk committed
230 231 232 233
QAction *GitPlugin::createRepositoryAction(ActionContainer *ac,
                                           const QString &text, Id id,
                                           const Context &context, bool addToLocator,
                                           const QKeySequence &keys)
234
{
hjk's avatar
hjk committed
235
    QAction *action = new QAction(text, this);
hjk's avatar
hjk committed
236
    Core::Command *command = ActionManager::registerAction(action, id, context);
hjk's avatar
hjk committed
237 238
    if (!keys.isEmpty())
        command->setDefaultKeySequence(keys);
239 240
    if (ac)
        ac->addAction(command);
241 242 243
    m_repositoryActions.push_back(action);
    if (addToLocator)
        m_commandLocator->appendCommand(command);
hjk's avatar
hjk committed
244
    return action;
245 246 247
}

// Create an action to act on the repository with slot
hjk's avatar
hjk committed
248 249 250 251
QAction *GitPlugin::createRepositoryAction(ActionContainer *ac,
                                           const QString &text, Id id,
                                           const Context &context, bool addToLocator,
                                           const char *pluginSlot, const QKeySequence &keys)
252
{
hjk's avatar
hjk committed
253 254 255 256
    QAction *action = createRepositoryAction(ac, text, id, context, addToLocator, keys);
    connect(action, SIGNAL(triggered()), this, pluginSlot);
    action->setData(id.uniqueIdentifier());
    return action;
257 258 259
}

// Action to act on the repository forwarded to a git client member function
hjk's avatar
hjk committed
260
// taking the directory.
hjk's avatar
hjk committed
261 262 263 264
QAction *GitPlugin::createRepositoryAction(ActionContainer *ac,
                                           const QString &text, Id id,
                                           const Context &context, bool addToLocator,
                                           GitClientMemberFunc func, const QKeySequence &keys)
265 266
{
    // Set the member func as data and connect to generic slot
hjk's avatar
hjk committed
267
    QAction *action = createRepositoryAction(ac, text, id, context, addToLocator, keys);
hjk's avatar
hjk committed
268 269 270 271
    connect(action, &QAction::triggered, [this, func]() -> void {
        QTC_ASSERT(currentState().hasTopLevel(), return);
        (m_gitClient->*func)(currentState().topLevel());
    });
hjk's avatar
hjk committed
272
    return action;
273 274
}

275
bool GitPlugin::initialize(const QStringList &arguments, QString *errorMessage)
con's avatar
con committed
276
{
277 278
    Q_UNUSED(arguments)

hjk's avatar
hjk committed
279
    m_settings.readSettings(ICore::settings());
280 281

    m_gitClient = new GitClient(&m_settings);
282

hjk's avatar
hjk committed
283
    typedef VcsSubmitEditorFactory<GitSubmitEditor> GitSubmitEditorFactory;
con's avatar
con committed
284

285
    initializeVcs(new GitVersionControl(m_gitClient));
con's avatar
con committed
286

287
    // Create the globalcontext list to register actions accordingly
hjk's avatar
hjk committed
288
    Context globalcontext(Core::Constants::C_GLOBAL);
con's avatar
con committed
289 290

    // Create the settings Page
291
    addAutoReleasedObject(new SettingsPage());
con's avatar
con committed
292 293

    static const char *describeSlot = SLOT(show(QString,QString));
hjk's avatar
hjk committed
294
    const int editorCount = sizeof(editorParameters) / sizeof(editorParameters[0]);
295
    const auto widgetCreator = []() { return new GitEditorWidget; };
296
    for (int i = 0; i < editorCount; i++)
297
        addAutoReleasedObject(new VcsEditorFactory(editorParameters + i, widgetCreator, m_gitClient, describeSlot));
con's avatar
con committed
298

299
    addAutoReleasedObject(new GitSubmitEditorFactory(&submitParameters));
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320

    auto cloneWizardFactory = new VcsBase::BaseCheckoutWizardFactory;
    cloneWizardFactory->setId(QLatin1String(VcsBase::Constants::VCS_ID_GIT));
    cloneWizardFactory->setIcon(QIcon(QLatin1String(":/git/images/git.png")));
    cloneWizardFactory->setDescription(tr("Clones a Git repository and tries to load the contained project."));
    cloneWizardFactory->setDisplayName(tr("Git Repository Clone"));
    cloneWizardFactory->setWizardCreator([this] (const Utils::FileName &path, QWidget *parent) {
        return new CloneWizard(path, parent);
    });
    addAutoReleasedObject(cloneWizardFactory);

    // A wizard allowing for browsing Gitorious-hosted projects.
    cloneWizardFactory = new VcsBase::BaseCheckoutWizardFactory;
    cloneWizardFactory->setId(QLatin1String(VcsBase::Constants::VCS_ID_GIT));
    cloneWizardFactory->setIcon(QIcon(QLatin1String(":/git/images/gitorious.png")));
    cloneWizardFactory->setDescription(tr("Clones a Gitorious repository and tries to load the contained project."));
    cloneWizardFactory->setDisplayName(tr("Gitorious Repository Clone"));
    cloneWizardFactory->setWizardCreator([this] (const Utils::FileName &path, QWidget *parent) {
        return new Gitorious::Internal::GitoriousCloneWizard(path, parent);
    });
    addAutoReleasedObject(cloneWizardFactory);
321

Friedemann Kleint's avatar
Friedemann Kleint committed
322
    const QString prefix = QLatin1String("git");
hjk's avatar
hjk committed
323
    m_commandLocator = new CommandLocator("Git", prefix, prefix);
Friedemann Kleint's avatar
Friedemann Kleint committed
324 325
    addAutoReleasedObject(m_commandLocator);

con's avatar
con committed
326
    //register actions
hjk's avatar
hjk committed
327
    ActionContainer *toolsContainer = ActionManager::actionContainer(Core::Constants::M_TOOLS);
con's avatar
con committed
328

hjk's avatar
hjk committed
329
    ActionContainer *gitContainer = ActionManager::createMenu("Git");
con's avatar
con committed
330 331
    gitContainer->menu()->setTitle(tr("&Git"));
    toolsContainer->addMenu(gitContainer);
332
    m_menuAction = gitContainer->menu()->menuAction();
333

hjk's avatar
hjk committed
334

Petar Perisin's avatar
Petar Perisin committed
335
    /*  "Current File" menu */
hjk's avatar
hjk committed
336
    ActionContainer *currentFileMenu = ActionManager::createMenu("Git.CurrentFileMenu");
Petar Perisin's avatar
Petar Perisin committed
337 338
    currentFileMenu->menu()->setTitle(tr("Current &File"));
    gitContainer->addMenu(currentFileMenu);
339

hjk's avatar
hjk committed
340 341 342 343 344 345 346
    createFileAction(currentFileMenu, tr("Diff Current File"), tr("Diff of \"%1\""),
                     "Git.Diff", globalcontext, true, SLOT(diffCurrentFile()),
                      QKeySequence(UseMacShortcuts ? tr("Meta+G,Meta+D") : tr("Alt+G,Alt+D")));

    createFileAction(currentFileMenu, tr("Log Current File"), tr("Log of \"%1\""),
                     "Git.Log", globalcontext, true, SLOT(logFile()),
                     QKeySequence(UseMacShortcuts ? tr("Meta+G,Meta+L") : tr("Alt+G,Alt+L")));
347

hjk's avatar
hjk committed
348 349 350 351 352
    createFileAction(currentFileMenu, tr("Blame Current File"), tr("Blame for \"%1\""),
                     "Git.Blame", globalcontext, true, SLOT(blameFile()),
                     QKeySequence(UseMacShortcuts ? tr("Meta+G,Meta+B") : tr("Alt+G,Alt+B")));

    currentFileMenu->addSeparator(globalcontext);
353

hjk's avatar
hjk committed
354 355 356
    createFileAction(currentFileMenu, tr("Stage File for Commit"), tr("Stage \"%1\" for Commit"),
                     "Git.Stage", globalcontext, true, SLOT(stageFile()),
                     QKeySequence(UseMacShortcuts ? tr("Meta+G,Meta+A") : tr("Alt+G,Alt+A")));
357

hjk's avatar
hjk committed
358 359
    createFileAction(currentFileMenu, tr("Unstage File from Commit"), tr("Unstage \"%1\" from Commit"),
                     "Git.Unstage", globalcontext, true, SLOT(unstageFile()));
360

hjk's avatar
hjk committed
361 362 363
    createFileAction(currentFileMenu, tr("Undo Unstaged Changes"), tr("Undo Unstaged Changes for \"%1\""),
                     "Git.UndoUnstaged", globalcontext,
                     true, SLOT(undoUnstagedFileChanges()));
364

hjk's avatar
hjk committed
365 366 367 368
    createFileAction(currentFileMenu, tr("Undo Uncommitted Changes"), tr("Undo Uncommitted Changes for \"%1\""),
                     "Git.Undo", globalcontext,
                     true, SLOT(undoFileChanges()),
                     QKeySequence(UseMacShortcuts ? tr("Meta+G,Meta+U") : tr("Alt+G,Alt+U")));
Petar Perisin's avatar
Petar Perisin committed
369 370 371


    /*  "Current Project" menu */
hjk's avatar
hjk committed
372
    ActionContainer *currentProjectMenu = ActionManager::createMenu("Git.CurrentProjectMenu");
Petar Perisin's avatar
Petar Perisin committed
373 374
    currentProjectMenu->menu()->setTitle(tr("Current &Project"));
    gitContainer->addMenu(currentProjectMenu);
con's avatar
con committed
375

hjk's avatar
hjk committed
376 377 378
    createProjectAction(currentProjectMenu, tr("Diff Current Project"), tr("Diff Project \"%1\""),
                        "Git.DiffProject", globalcontext, true, SLOT(diffCurrentProject()),
                        QKeySequence(UseMacShortcuts ? tr("Meta+G,Meta+Shift+D") : tr("Alt+G,Alt+Shift+D")));
379

hjk's avatar
hjk committed
380 381 382
    createProjectAction(currentProjectMenu, tr("Log Project"), tr("Log Project \"%1\""),
                        "Git.LogProject", globalcontext, true, SLOT(logProject()),
                        QKeySequence(UseMacShortcuts ? tr("Meta+G,Meta+K") : tr("Alt+G,Alt+K")));
383

hjk's avatar
hjk committed
384 385
    createProjectAction(currentProjectMenu, tr("Clean Project..."), tr("Clean Project \"%1\"..."),
                        "Git.CleanProject", globalcontext, true, SLOT(cleanProject()));
con's avatar
con committed
386 387


Petar Perisin's avatar
Petar Perisin committed
388
    /*  "Local Repository" menu */
hjk's avatar
hjk committed
389
    ActionContainer *localRepositoryMenu = ActionManager::createMenu("Git.LocalRepositoryMenu");
Petar Perisin's avatar
Petar Perisin committed
390 391 392
    localRepositoryMenu->menu()->setTitle(tr("&Local Repository"));
    gitContainer->addMenu(localRepositoryMenu);

hjk's avatar
hjk committed
393
    createRepositoryAction(localRepositoryMenu, tr("Diff"), "Git.DiffRepository",
394
                           globalcontext, true, SLOT(diffRepository()));
395

hjk's avatar
hjk committed
396
    createRepositoryAction(localRepositoryMenu, tr("Log"), "Git.LogRepository",
397 398
                           globalcontext, true,
                           SLOT(logRepository()));
399

hjk's avatar
hjk committed
400
    createRepositoryAction(localRepositoryMenu, tr("Reflog"), "Git.ReflogRepository",
Orgad Shaneh's avatar
Orgad Shaneh committed
401 402 403
                           globalcontext, true,
                           SLOT(reflogRepository()));

hjk's avatar
hjk committed
404
    createRepositoryAction(localRepositoryMenu, tr("Clean..."), "Git.CleanRepository",
Petar Perisin's avatar
Petar Perisin committed
405 406
                           globalcontext, true, SLOT(cleanRepository()));

hjk's avatar
hjk committed
407
    createRepositoryAction(localRepositoryMenu, tr("Status"), "Git.StatusRepository",
408
                           globalcontext, true, &GitClient::status);
409

Petar Perisin's avatar
Petar Perisin committed
410 411
    // --------------
    localRepositoryMenu->addSeparator(globalcontext);
con's avatar
con committed
412

hjk's avatar
hjk committed
413
    createRepositoryAction(localRepositoryMenu, tr("Commit..."), "Git.Commit",
414 415
                           globalcontext, true, SLOT(startCommit()),
                           QKeySequence(UseMacShortcuts ? tr("Meta+G,Meta+C") : tr("Alt+G,Alt+C")));
416

Petar Perisin's avatar
Petar Perisin committed
417
    createRepositoryAction(localRepositoryMenu,
418
                           tr("Amend Last Commit..."), "Git.AmendCommit",
Petar Perisin's avatar
Petar Perisin committed
419
                           globalcontext, true, SLOT(startAmendCommit()));
420

421 422
    m_fixupCommitAction =
            createRepositoryAction(localRepositoryMenu,
423
                                   tr("Fixup Previous Commit..."), "Git.FixupCommit",
hjk's avatar
hjk committed
424 425
                                   globalcontext, true, SLOT(startFixupCommit()));

426 427
    // --------------
    localRepositoryMenu->addSeparator(globalcontext);
428

Petar Perisin's avatar
Petar Perisin committed
429
    createRepositoryAction(localRepositoryMenu,
430
                           tr("Reset..."), "Git.Reset",
Orgad Shaneh's avatar
Orgad Shaneh committed
431
                           globalcontext, true, SLOT(resetRepository()));
432

433 434
    m_interactiveRebaseAction =
            createRepositoryAction(localRepositoryMenu,
435
                                   tr("Interactive Rebase..."), "Git.InteractiveRebase",
hjk's avatar
hjk committed
436
                                   globalcontext, true, SLOT(startRebase()));
Orgad Shaneh's avatar
Orgad Shaneh committed
437

Petar Perisin's avatar
Petar Perisin committed
438 439
    m_submoduleUpdateAction =
            createRepositoryAction(localRepositoryMenu,
440
                                   tr("Update Submodules"), "Git.SubmoduleUpdate",
hjk's avatar
hjk committed
441
                                   globalcontext, true, SLOT(updateSubmodules()));
Petar Perisin's avatar
Petar Perisin committed
442 443
    m_abortMergeAction =
            createRepositoryAction(localRepositoryMenu,
444
                                   tr("Abort Merge"), "Git.MergeAbort",
hjk's avatar
hjk committed
445
                                   globalcontext, true, SLOT(continueOrAbortCommand()));
Petar Perisin's avatar
Petar Perisin committed
446 447 448

    m_abortRebaseAction =
            createRepositoryAction(localRepositoryMenu,
449
                                   tr("Abort Rebase"), "Git.RebaseAbort",
hjk's avatar
hjk committed
450
                                   globalcontext, true, SLOT(continueOrAbortCommand()));
Petar Perisin's avatar
Petar Perisin committed
451 452 453

    m_abortCherryPickAction =
            createRepositoryAction(localRepositoryMenu,
454
                                   tr("Abort Cherry Pick"), "Git.CherryPickAbort",
hjk's avatar
hjk committed
455
                                   globalcontext, true, SLOT(continueOrAbortCommand()));
Petar Perisin's avatar
Petar Perisin committed
456 457 458

    m_abortRevertAction =
            createRepositoryAction(localRepositoryMenu,
459
                                   tr("Abort Revert"), "Git.RevertAbort",
hjk's avatar
hjk committed
460
                                   globalcontext, true, SLOT(continueOrAbortCommand()));
Petar Perisin's avatar
Petar Perisin committed
461 462 463

    m_continueRebaseAction =
            createRepositoryAction(localRepositoryMenu,
464
                                   tr("Continue Rebase"), "Git.RebaseContinue",
hjk's avatar
hjk committed
465
                                   globalcontext, true, SLOT(continueOrAbortCommand()));
Petar Perisin's avatar
Petar Perisin committed
466 467 468

    m_continueCherryPickAction =
            createRepositoryAction(localRepositoryMenu,
469
                                   tr("Continue Cherry Pick"), "Git.CherryPickContinue",
hjk's avatar
hjk committed
470
                                   globalcontext, true, SLOT(continueOrAbortCommand()));
Petar Perisin's avatar
Petar Perisin committed
471 472 473

    m_continueRevertAction =
            createRepositoryAction(localRepositoryMenu,
474
                                   tr("Continue Revert"), "Git.RevertContinue",
hjk's avatar
hjk committed
475
                                   globalcontext, true, SLOT(continueOrAbortCommand()));
Petar Perisin's avatar
Petar Perisin committed
476

477
    // --------------
Petar Perisin's avatar
Petar Perisin committed
478
    localRepositoryMenu->addSeparator(globalcontext);
479

Petar Perisin's avatar
Petar Perisin committed
480
    createRepositoryAction(localRepositoryMenu,
481
                           tr("Branches..."), "Git.BranchList",
Yuchen Deng's avatar
Yuchen Deng committed
482
                           globalcontext, true, SLOT(branchList()));
483 484

    // --------------
Petar Perisin's avatar
Petar Perisin committed
485
    localRepositoryMenu->addSeparator(globalcontext);
486

Petar Perisin's avatar
Petar Perisin committed
487
    // "Patch" menu
hjk's avatar
hjk committed
488
    ActionContainer *patchMenu = ActionManager::createMenu("Git.PatchMenu");
Petar Perisin's avatar
Petar Perisin committed
489 490
    patchMenu->menu()->setTitle(tr("&Patch"));
    localRepositoryMenu->addMenu(patchMenu);
491 492

    // Apply current file as patch is handled specially.
hjk's avatar
hjk committed
493
    m_applyCurrentFilePatchAction =
Eike Ziller's avatar
Eike Ziller committed
494
            createParameterAction(patchMenu,
495
                                  tr("Apply from Editor"), tr("Apply \"%1\""),
496
                                  "Git.ApplyCurrentFilePatch",
497
                                  globalcontext, true);
hjk's avatar
hjk committed
498

499 500 501
    connect(m_applyCurrentFilePatchAction, SIGNAL(triggered()), this,
            SLOT(applyCurrentFilePatch()));

Eike Ziller's avatar
Eike Ziller committed
502
    createRepositoryAction(patchMenu,
503
                           tr("Apply from File..."), "Git.ApplyPatch",
504 505
                           globalcontext, true, SLOT(promptApplyPatch()));

Petar Perisin's avatar
Petar Perisin committed
506
    // "Stash" menu
hjk's avatar
hjk committed
507
    ActionContainer *stashMenu = ActionManager::createMenu("Git.StashMenu");
Petar Perisin's avatar
Petar Perisin committed
508 509
    stashMenu->menu()->setTitle(tr("&Stash"));
    localRepositoryMenu->addMenu(stashMenu);
510

Eike Ziller's avatar
Eike Ziller committed
511
    createRepositoryAction(stashMenu,
512
                           tr("Stashes..."), "Git.StashList",
513 514
                           globalcontext, false, SLOT(stashList()));

515
    stashMenu->addSeparator(globalcontext);
516

hjk's avatar
hjk committed
517 518 519
    QAction *action = createRepositoryAction(stashMenu, tr("Stash"), "Git.Stash",
                                             globalcontext, true, SLOT(stash()));
    action->setToolTip(tr("Saves the current state of your work and resets the repository."));
520

hjk's avatar
hjk committed
521 522 523
    action = createRepositoryAction(stashMenu, tr("Take Snapshot..."), "Git.StashSnapshot",
                                    globalcontext, true, SLOT(stashSnapshot()));
    action->setToolTip(tr("Saves the current state of your work."));
524

525
    stashMenu->addSeparator(globalcontext);
526

hjk's avatar
hjk committed
527 528 529
    action = createRepositoryAction(stashMenu, tr("Stash Pop"), "Git.StashPop",
                                    globalcontext, true, &GitClient::stashPop);
    action->setToolTip(tr("Restores changes saved to the stash list using \"Stash\"."));
530

Petar Perisin's avatar
Petar Perisin committed
531 532 533 534 535 536

    /* \"Local Repository" menu */

    // --------------

    /*  "Remote Repository" menu */
hjk's avatar
hjk committed
537
    ActionContainer *remoteRepositoryMenu = ActionManager::createMenu("Git.RemoteRepositoryMenu");
Petar Perisin's avatar
Petar Perisin committed
538 539 540
    remoteRepositoryMenu->menu()->setTitle(tr("&Remote Repository"));
    gitContainer->addMenu(remoteRepositoryMenu);

hjk's avatar
hjk committed
541
    createRepositoryAction(remoteRepositoryMenu, tr("Fetch"), "Git.Fetch",
Petar Perisin's avatar
Petar Perisin committed
542 543
                           globalcontext, true, SLOT(fetch()));

hjk's avatar
hjk committed
544
    createRepositoryAction(remoteRepositoryMenu, tr("Pull"), "Git.Pull",
Petar Perisin's avatar
Petar Perisin committed
545 546
                           globalcontext, true, SLOT(pull()));

hjk's avatar
hjk committed
547 548
    createRepositoryAction(remoteRepositoryMenu, tr("Push"), "Git.Push",
                           globalcontext, true, SLOT(push()));
Petar Perisin's avatar
Petar Perisin committed
549 550 551 552 553

    // --------------
    remoteRepositoryMenu->addSeparator(globalcontext);

    // "Subversion" menu
hjk's avatar
hjk committed
554
    ActionContainer *subversionMenu = ActionManager::createMenu("Git.Subversion");
Petar Perisin's avatar
Petar Perisin committed
555 556
    subversionMenu->menu()->setTitle(tr("&Subversion"));
    remoteRepositoryMenu->addMenu(subversionMenu);
557

Eike Ziller's avatar
Eike Ziller committed
558
    createRepositoryAction(subversionMenu,
559
                           tr("Log"), "Git.Subversion.Log",
560 561
                           globalcontext, false, &GitClient::subversionLog);

Eike Ziller's avatar
Eike Ziller committed
562
    createRepositoryAction(subversionMenu,
563
                           tr("Fetch"), "Git.Subversion.Fetch",
564 565
                           globalcontext, false, &GitClient::synchronousSubversionFetch);

Petar Perisin's avatar
Petar Perisin committed
566 567
    // --------------
    remoteRepositoryMenu->addSeparator(globalcontext);
568

Petar Perisin's avatar
Petar Perisin committed
569
    createRepositoryAction(remoteRepositoryMenu,
570
                           tr("Manage Remotes..."), "Git.RemoteList",
Petar Perisin's avatar
Petar Perisin committed
571
                           globalcontext, false, SLOT(remoteList()));
572

Petar Perisin's avatar
Petar Perisin committed
573
    /* \"Remote Repository" menu */
Robert Loehning's avatar
Robert Loehning committed
574

Petar Perisin's avatar
Petar Perisin committed
575
    // --------------
576

577
    /*  Actions only in locator */
578
    createRepositoryAction(0, tr("Show..."), "Git.Show",
579 580
                           globalcontext, true, SLOT(startChangeRelatedAction()));

581
    createRepositoryAction(0, tr("Revert..."), "Git.Revert",
582 583
                           globalcontext, true, SLOT(startChangeRelatedAction()));

584
    createRepositoryAction(0, tr("Cherry Pick..."), "Git.CherryPick",
585 586
                           globalcontext, true, SLOT(startChangeRelatedAction()));

587
    createRepositoryAction(0, tr("Checkout..."), "Git.Checkout",
588 589
                           globalcontext, true, SLOT(startChangeRelatedAction()));

590
    createRepositoryAction(0, tr("Rebase..."), "Git.Rebase",
591 592
                           globalcontext, true, SLOT(branchList()));

593
    createRepositoryAction(0, tr("Merge..."), "Git.Merge",
594 595 596 597 598 599
                           globalcontext, true, SLOT(branchList()));

    /*  \Actions only in locator */

    // --------------

Petar Perisin's avatar
Petar Perisin committed
600
    /*  "Git Tools" menu */
hjk's avatar
hjk committed
601
    ActionContainer *gitToolsMenu = ActionManager::createMenu("Git.GitToolsMenu");
Petar Perisin's avatar
Petar Perisin committed
602 603
    gitToolsMenu->menu()->setTitle(tr("Git &Tools"));
    gitContainer->addMenu(gitToolsMenu);
604

Petar Perisin's avatar
Petar Perisin committed
605
    createRepositoryAction(gitToolsMenu,
606
                           tr("Gitk"), "Git.LaunchGitK",
Petar Perisin's avatar
Petar Perisin committed
607
                           globalcontext, true, &GitClient::launchGitK);
608

hjk's avatar
hjk committed
609 610
    createFileAction(gitToolsMenu, tr("Gitk Current File"), tr("Gitk of \"%1\""),
                     "Git.GitkFile", globalcontext, true, SLOT(gitkForCurrentFile()));
Petar Perisin's avatar
Petar Perisin committed
611

hjk's avatar
hjk committed
612 613
    createFileAction(gitToolsMenu, tr("Gitk for folder of Current File"), tr("Gitk for folder of \"%1\""),
                     "Git.GitkFolder", globalcontext, true, SLOT(gitkForCurrentFolder()));
Petar Perisin's avatar
Petar Perisin committed
614 615 616 617

    // --------------
    gitToolsMenu->addSeparator(globalcontext);

618
    createRepositoryAction(gitToolsMenu, tr("Git Gui"), "Git.GitGui",
619 620 621 622 623
                           globalcontext, true, SLOT(gitGui()));

    // --------------
    gitToolsMenu->addSeparator(globalcontext);

hjk's avatar
hjk committed
624 625 626 627
    m_repositoryBrowserAction =
            createRepositoryAction(gitToolsMenu,
                                   tr("Repository Browser"), "Git.LaunchRepositoryBrowser",
                                   globalcontext, true, &GitClient::launchRepositoryBrowser);
628

Petar Perisin's avatar
Petar Perisin committed
629 630
    m_mergeToolAction =
            createRepositoryAction(gitToolsMenu,
631
                                   tr("Merge Tool"), "Git.MergeTool",
hjk's avatar
hjk committed
632
                                   globalcontext, true, SLOT(startMergeTool()));
633

Petar Perisin's avatar
Petar Perisin committed
634 635 636
    /* \"Git Tools" menu */

    // --------------
637
    gitContainer->addSeparator(globalcontext);
638

639 640
    createRepositoryAction(gitContainer, tr("Actions on Commits..."), "Git.ChangeActions",
                           globalcontext, false, SLOT(startChangeRelatedAction()));
641

642
    QAction *repositoryAction = new QAction(tr("Create Repository..."), this);
hjk's avatar
hjk committed
643
    Core::Command *createRepositoryCommand = ActionManager::registerAction(repositoryAction, "Git.CreateRepository", globalcontext);
644
    connect(repositoryAction, SIGNAL(triggered()), this, SLOT(createRepository()));
Petar Perisin's avatar
Petar Perisin committed
645 646
    gitContainer->addAction(createRepositoryCommand);

con's avatar
con committed
647
    // Submit editor
hjk's avatar
hjk committed
648 649 650
    Context submitContext(Constants::C_GITSUBMITEDITOR);
    m_submitCurrentAction = new QAction(VcsBaseSubmitEditor::submitIcon(), tr("Commit"), this);
    Core::Command *command = ActionManager::registerAction(m_submitCurrentAction, Constants::SUBMIT_CURRENT, submitContext);
651
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
652 653
    connect(m_submitCurrentAction, SIGNAL(triggered()), this, SLOT(submitCurrentLog()));

hjk's avatar
hjk committed
654 655
    m_diffSelectedFilesAction = new QAction(VcsBaseSubmitEditor::diffIcon(), tr("Diff &Selected Files"), this);
    command = ActionManager::registerAction(m_diffSelectedFilesAction, Constants::DIFF_SELECTED, submitContext);
con's avatar
con committed
656 657

    m_undoAction = new QAction(tr("&Undo"), this);
hjk's avatar
hjk committed
658
    command = ActionManager::registerAction(m_undoAction, Core::Constants::UNDO, submitContext);
con's avatar
con committed
659 660

    m_redoAction = new QAction(tr("&Redo"), this);
hjk's avatar
hjk committed
661
    command = ActionManager::registerAction(m_redoAction, Core::Constants::REDO, submitContext);
con's avatar
con committed
662

hjk's avatar
hjk committed
663
    connect(VcsManager::instance(), SIGNAL(repositoryChanged(QString)),
664
            this, SLOT(updateContinueAndAbortCommands()));
hjk's avatar
hjk committed
665
    connect(VcsManager::instance(), SIGNAL(repositoryChanged(QString)),
666
            this, SLOT(updateBranches(QString)), Qt::QueuedConnection);
667

hjk's avatar
hjk committed
668
    if (!MimeDatabase::addMimeTypes(QLatin1String(RC_GIT_MIME_XML), errorMessage))
669 670
        return false;

Petar Perisin's avatar
Petar Perisin committed
671
    /* "Gerrit" */
672 673 674 675 676 677
    m_gerritPlugin = new Gerrit::Internal::GerritPlugin(this);
    const bool ok = m_gerritPlugin->initialize(remoteRepositoryMenu);
    m_gerritPlugin->updateActions(currentState().hasTopLevel());
    m_gerritPlugin->addToLocator(m_commandLocator);

    return ok;
con's avatar
con committed
678 679
}

680
GitVersionControl *GitPlugin::gitVersionControl() const
681
{
682
    return static_cast<GitVersionControl *>(versionControl());
683 684
}

685
void GitPlugin::submitEditorDiff(const QStringList &unstaged, const QStringList &staged)
con's avatar
con committed
686
{
Orgad Shaneh's avatar
Orgad Shaneh committed
687
    m_gitClient->diff(m_submitRepository, unstaged, staged);
con's avatar
con committed
688 689
}

690 691 692 693 694
void GitPlugin::submitEditorMerge(const QStringList &unmerged)
{
    m_gitClient->merge(m_submitRepository, unmerged);
}

con's avatar
con committed
695 696
void GitPlugin::diffCurrentFile()
{
hjk's avatar
hjk committed
697
    const VcsBasePluginState state = currentState();
698
    QTC_ASSERT(state.hasFile(), return);
Orgad Shaneh's avatar
Orgad Shaneh committed
699
    m_gitClient->diff(state.currentFileTopLevel(), state.relativeCurrentFile());
con's avatar
con committed
700 701 702 703
}

void GitPlugin::diffCurrentProject()
{
hjk's avatar
hjk committed
704
    const VcsBasePluginState state = currentState();
705
    QTC_ASSERT(state.hasProject(), return);
706 707 708
    const QString relativeProject = state.relativeCurrentProject();
    m_gitClient->diff(state.currentProjectTopLevel(),
                      relativeProject.isEmpty() ? QStringList() : QStringList(relativeProject));
con's avatar
con committed
709 710
}

711 712
void GitPlugin::diffRepository()
{
hjk's avatar
hjk committed
713
    const VcsBasePluginState state = currentState();
714
    QTC_ASSERT(state.hasTopLevel(), return);
Orgad Shaneh's avatar
Orgad Shaneh committed
715
    m_gitClient->diff(state.topLevel(), QStringList());
716 717
}

con's avatar
con committed
718 719
void GitPlugin::logFile()
{
hjk's avatar
hjk committed
720
    const VcsBasePluginState state = currentState();
721
    QTC_ASSERT(state.hasFile(), return);
722
    m_gitClient->log(state.currentFileTopLevel(), state.relativeCurrentFile(), true);
con's avatar
con committed
723 724 725 726
}

void GitPlugin::blameFile()
{
hjk's avatar
hjk committed
727
    const VcsBasePluginState state = currentState();
728
    QTC_ASSERT(state.hasFile(), return);
729
    const int lineNumber = VcsBaseEditor::lineNumberOfCurrentEditor(state.currentFile());
730
    m_gitClient->blame(state.currentFileTopLevel(), QStringList(), state.relativeCurrentFile(), QString(), lineNumber);
con's avatar
con committed
731 732 733 734
}

void GitPlugin::logProject()
{
hjk's avatar
hjk committed
735
    const VcsBasePluginState state = currentState();