gitplugin.cpp 62.9 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
#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/mimedatabase.h>
62
#include <coreplugin/vcsmanager.h>
hjk's avatar
hjk committed
63
64

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

con's avatar
con committed
67
#include <vcsbase/basevcseditorfactory.h>
68
#include <vcsbase/submitfilemodel.h>
con's avatar
con committed
69
70
#include <vcsbase/vcsbaseeditor.h>
#include <vcsbase/basevcssubmiteditorfactory.h>
71
#include <vcsbase/vcsbaseoutputwindow.h>
72
#include <vcsbase/cleandialog.h>
Friedemann Kleint's avatar
Friedemann Kleint committed
73
#include <locator/commandlocator.h>
con's avatar
con committed
74

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

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

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

hjk's avatar
hjk committed
88
static const VcsBase::VcsBaseEditorParameters editorParameters[] = {
con's avatar
con committed
89
{
90
    VcsBase::OtherContent,
91
92
    Git::Constants::GIT_COMMAND_LOG_EDITOR_ID,
    Git::Constants::GIT_COMMAND_LOG_EDITOR_DISPLAY_NAME,
93
    Git::Constants::C_GIT_COMMAND_LOG_EDITOR,
Orgad Shaneh's avatar
Orgad Shaneh committed
94
    "text/vnd.qtcreator.git.commandlog"},
hjk's avatar
hjk committed
95
{   VcsBase::LogOutput,
96
97
    Git::Constants::GIT_LOG_EDITOR_ID,
    Git::Constants::GIT_LOG_EDITOR_DISPLAY_NAME,
98
    Git::Constants::C_GIT_LOG_EDITOR,
Orgad Shaneh's avatar
Orgad Shaneh committed
99
    "text/vnd.qtcreator.git.log"},
hjk's avatar
hjk committed
100
{   VcsBase::AnnotateOutput,
101
102
    Git::Constants::GIT_BLAME_EDITOR_ID,
    Git::Constants::GIT_BLAME_EDITOR_DISPLAY_NAME,
103
    Git::Constants::C_GIT_BLAME_EDITOR,
Orgad Shaneh's avatar
Orgad Shaneh committed
104
    "text/vnd.qtcreator.git.annotation"},
hjk's avatar
hjk committed
105
{   VcsBase::DiffOutput,
106
107
    Git::Constants::GIT_DIFF_EDITOR_ID,
    Git::Constants::GIT_DIFF_EDITOR_DISPLAY_NAME,
108
    Git::Constants::C_GIT_DIFF_EDITOR,
109
    "text/x-patch"},
110
{   VcsBase::OtherContent,
111
112
113
114
    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"},
115
116
117
118
119
{   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
120
121
};

122
123
Q_DECLARE_METATYPE(Git::Internal::GitClientMemberFunc)

con's avatar
con committed
124
125
126
127
128
129
130
131
using namespace Git;
using namespace Git::Internal;

// GitPlugin

GitPlugin *GitPlugin::m_instance = 0;

GitPlugin::GitPlugin() :
Friedemann Kleint's avatar
Friedemann Kleint committed
132
    m_commandLocator(0),
con's avatar
con committed
133
134
135
136
    m_submitCurrentAction(0),
    m_diffSelectedFilesAction(0),
    m_undoAction(0),
    m_redoAction(0),
137
    m_menuAction(0),
138
    m_applyCurrentFilePatchAction(0),
con's avatar
con committed
139
    m_gitClient(0),
140
    m_submitActionTriggered(false)
con's avatar
con committed
141
142
{
    m_instance = this;
143
144
145
146
147
    const int mid = qRegisterMetaType<GitClientMemberFunc>();
    Q_UNUSED(mid)
    m_fileActions.reserve(10);
    m_projectActions.reserve(10);
    m_repositoryActions.reserve(15);
con's avatar
con committed
148
149
150
151
}

GitPlugin::~GitPlugin()
{
152
    cleanCommitMessageFile();
con's avatar
con committed
153
154
155
156
    delete m_gitClient;
    m_instance = 0;
}

157
void GitPlugin::cleanCommitMessageFile()
con's avatar
con committed
158
{
159
160
161
    if (!m_commitMessageFileName.isEmpty()) {
        QFile::remove(m_commitMessageFileName);
        m_commitMessageFileName.clear();
con's avatar
con committed
162
163
164
    }
}

165
166
167
168
169
bool GitPlugin::isCommitEditorOpen() const
{
    return !m_commitMessageFileName.isEmpty();
}

con's avatar
con committed
170
171
172
173
174
GitPlugin *GitPlugin::instance()
{
    return m_instance;
}

hjk's avatar
hjk committed
175
static const VcsBase::VcsBaseSubmitEditorParameters submitParameters = {
con's avatar
con committed
176
    Git::Constants::SUBMIT_MIMETYPE,
177
178
    Git::Constants::GITSUBMITEDITOR_ID,
    Git::Constants::GITSUBMITEDITOR_DISPLAY_NAME,
179
180
    Git::Constants::C_GITSUBMITEDITOR,
    VcsBase::VcsBaseSubmitEditorParameters::DiffRows
con's avatar
con committed
181
182
};

183
184
// Create a parameter action
ParameterActionCommandPair
Eike Ziller's avatar
Eike Ziller committed
185
        GitPlugin::createParameterAction(Core::ActionContainer *ac,
186
                                         const QString &defaultText, const QString &parameterText,
hjk's avatar
hjk committed
187
                                         const Core::Id &id, const Core::Context &context,
188
189
190
                                         bool addToLocator)
{
    Utils::ParameterAction *action = new Utils::ParameterAction(defaultText, parameterText,
191
192
                                                                Utils::ParameterAction::EnabledWithParameter,
                                                                this);
Eike Ziller's avatar
Eike Ziller committed
193
    Core::Command *command = Core::ActionManager::registerAction(action, id, context);
194
195
196
197
198
199
200
201
202
    command->setAttribute(Core::Command::CA_UpdateText);
    ac->addAction(command);
    if (addToLocator)
        m_commandLocator->appendCommand(command);
    return ParameterActionCommandPair(action, command);
}

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

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

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

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

// Action to act on the repository forwarded to a git client member function
// taking the directory. Store the member function as data on the action.
ActionCommandPair
Eike Ziller's avatar
Eike Ziller committed
259
        GitPlugin::createRepositoryAction(Core::ActionContainer *ac,
hjk's avatar
hjk committed
260
                                          const QString &text, const Core::Id &id,
261
                                          const Core::Context &context, bool addToLocator,
262
263
264
                                          GitClientMemberFunc func)
{
    // Set the member func as data and connect to generic slot
Eike Ziller's avatar
Eike Ziller committed
265
    const ActionCommandPair rc = createRepositoryAction(ac, text, id, context, addToLocator);
266
267
268
269
270
    rc.first->setData(qVariantFromValue(func));
    connect(rc.first, SIGNAL(triggered()), this, SLOT(gitClientMemberFuncRepositoryAction()));
    return rc;
}

271
bool GitPlugin::initialize(const QStringList &arguments, QString *errorMessage)
con's avatar
con committed
272
{
273
274
    Q_UNUSED(arguments)

hjk's avatar
hjk committed
275
    m_settings.readSettings(Core::ICore::settings());
276
277

    m_gitClient = new GitClient(&m_settings);
278

hjk's avatar
hjk committed
279
280
    typedef VcsBase::VcsEditorFactory<GitEditor> GitEditorFactory;
    typedef VcsBase::VcsSubmitEditorFactory<GitSubmitEditor> GitSubmitEditorFactory;
con's avatar
con committed
281

282
    initializeVcs(new GitVersionControl(m_gitClient));
con's avatar
con committed
283

284
    // Create the globalcontext list to register actions accordingly
285
    Core::Context globalcontext(Core::Constants::C_GLOBAL);
con's avatar
con committed
286
287

    // Create the settings Page
288
    addAutoReleasedObject(new SettingsPage());
con's avatar
con committed
289
290

    static const char *describeSlot = SLOT(show(QString,QString));
hjk's avatar
hjk committed
291
    const int editorCount = sizeof(editorParameters)/sizeof(VcsBase::VcsBaseEditorParameters);
292
293
    for (int i = 0; i < editorCount; i++)
        addAutoReleasedObject(new GitEditorFactory(editorParameters + i, m_gitClient, describeSlot));
con's avatar
con committed
294

295
296
    addAutoReleasedObject(new GitSubmitEditorFactory(&submitParameters));
    addAutoReleasedObject(new CloneWizard);
297
    addAutoReleasedObject(new Gitorious::Internal::GitoriousCloneWizard);
298

Friedemann Kleint's avatar
Friedemann Kleint committed
299
    const QString prefix = QLatin1String("git");
300
    m_commandLocator = new Locator::CommandLocator("Git", prefix, prefix);
Friedemann Kleint's avatar
Friedemann Kleint committed
301
302
    addAutoReleasedObject(m_commandLocator);

con's avatar
con committed
303
    //register actions
304
    Core::ActionContainer *toolsContainer =
Eike Ziller's avatar
Eike Ziller committed
305
        Core::ActionManager::actionContainer(Core::Constants::M_TOOLS);
con's avatar
con committed
306

Eike Ziller's avatar
Eike Ziller committed
307
    Core::ActionContainer *gitContainer = Core::ActionManager::createMenu("Git");
con's avatar
con committed
308
309
    gitContainer->menu()->setTitle(tr("&Git"));
    toolsContainer->addMenu(gitContainer);
310
    m_menuAction = gitContainer->menu()->menuAction();
311

Petar Perisin's avatar
Petar Perisin committed
312
    /*  "Current File" menu */
313
    Core::ActionContainer *currentFileMenu = Core::ActionManager::createMenu("Git.CurrentFileMenu");
Petar Perisin's avatar
Petar Perisin committed
314
315
    currentFileMenu->menu()->setTitle(tr("Current &File"));
    gitContainer->addMenu(currentFileMenu);
316

Petar Perisin's avatar
Petar Perisin committed
317
318
    ParameterActionCommandPair parameterActionCommand
            = createFileAction(currentFileMenu,
319
                               tr("Diff Current File"), tr("Diff of \"%1\""),
320
                               "Git.Diff", globalcontext, true,
321
                               SLOT(diffCurrentFile()));
322
    parameterActionCommand.second->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+G,Meta+D") : tr("Alt+G,Alt+D")));
323

324
    parameterActionCommand
Petar Perisin's avatar
Petar Perisin committed
325
            = createFileAction(currentFileMenu,
326
                               tr("Log Current File"), tr("Log of \"%1\""),
327
                               "Git.Log", globalcontext, true, SLOT(logFile()));
328
    parameterActionCommand.second->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+G,Meta+L") : tr("Alt+G,Alt+L")));
329

Petar Perisin's avatar
Petar Perisin committed
330
331
332
    parameterActionCommand
                = createFileAction(currentFileMenu,
                                   tr("Blame Current File"), tr("Blame for \"%1\""),
333
                                   "Git.Blame",
Petar Perisin's avatar
Petar Perisin committed
334
335
336
                                   globalcontext, true, SLOT(blameFile()));
    parameterActionCommand.second->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+G,Meta+B") : tr("Alt+G,Alt+B")));

337
    // ------
Petar Perisin's avatar
Petar Perisin committed
338
    currentFileMenu->addSeparator(globalcontext);
339
340

    parameterActionCommand
Petar Perisin's avatar
Petar Perisin committed
341
            = createFileAction(currentFileMenu,
342
                               tr("Stage File for Commit"), tr("Stage \"%1\" for Commit"),
343
                               "Git.Stage", globalcontext, true, SLOT(stageFile()));
344
    parameterActionCommand.second->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+G,Meta+A") : tr("Alt+G,Alt+A")));
345
346

    parameterActionCommand
Petar Perisin's avatar
Petar Perisin committed
347
            = createFileAction(currentFileMenu,
348
                               tr("Unstage File from Commit"), tr("Unstage \"%1\" from Commit"),
349
                               "Git.Unstage", globalcontext, true, SLOT(unstageFile()));
350

351
    parameterActionCommand
Petar Perisin's avatar
Petar Perisin committed
352
            = createFileAction(currentFileMenu,
353
                               tr("Undo Unstaged Changes"), tr("Undo Unstaged Changes for \"%1\""),
354
                               "Git.UndoUnstaged", globalcontext,
355
356
357
                               true, SLOT(undoUnstagedFileChanges()));

    parameterActionCommand
Petar Perisin's avatar
Petar Perisin committed
358
            = createFileAction(currentFileMenu,
359
                               tr("Undo Uncommitted Changes"), tr("Undo Uncommitted Changes for \"%1\""),
360
                               "Git.Undo", globalcontext,
361
                               true, SLOT(undoFileChanges()));
362
    parameterActionCommand.second->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+G,Meta+U") : tr("Alt+G,Alt+U")));
363

Petar Perisin's avatar
Petar Perisin committed
364
365
    /* \"Current File" menu */

366
    // ------------
Petar Perisin's avatar
Petar Perisin committed
367
368

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

373
    parameterActionCommand
Petar Perisin's avatar
Petar Perisin committed
374
            = createProjectAction(currentProjectMenu,
375
                                  tr("Diff Current Project"), tr("Diff Project \"%1\""),
376
                                  "Git.DiffProject", globalcontext, true,
377
                                  SLOT(diffCurrentProject()));
378
    parameterActionCommand.second->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+G,Meta+Shift+D") : tr("Alt+G,Alt+Shift+D")));
379
380

    parameterActionCommand
Petar Perisin's avatar
Petar Perisin committed
381
            = createProjectAction(currentProjectMenu,
382
                                  tr("Log Project"), tr("Log Project \"%1\""),
383
                                  "Git.LogProject", globalcontext, true,
384
                                  SLOT(logProject()));
385
    parameterActionCommand.second->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+G,Meta+K") : tr("Alt+G,Alt+K")));
386
387

    parameterActionCommand
Petar Perisin's avatar
Petar Perisin committed
388
                = createProjectAction(currentProjectMenu,
389
                                      tr("Clean Project..."), tr("Clean Project \"%1\"..."),
390
                                      "Git.CleanProject", globalcontext,
391
                                      true, SLOT(cleanProject()));
con's avatar
con committed
392

Petar Perisin's avatar
Petar Perisin committed
393
    /* \"Current Project" menu */
394
395

    // --------------
con's avatar
con committed
396

Petar Perisin's avatar
Petar Perisin committed
397
    /*  "Local Repository" menu */
398
    Core::ActionContainer *localRepositoryMenu = Core::ActionManager::createMenu("Git.LocalRepositoryMenu");
Petar Perisin's avatar
Petar Perisin committed
399
400
401
402
    localRepositoryMenu->menu()->setTitle(tr("&Local Repository"));
    gitContainer->addMenu(localRepositoryMenu);

    createRepositoryAction(localRepositoryMenu,
403
                           tr("Diff"), "Git.DiffRepository",
404
                           globalcontext, true, SLOT(diffRepository()));
405

Petar Perisin's avatar
Petar Perisin committed
406
    createRepositoryAction(localRepositoryMenu,
407
                           tr("Log"), "Git.LogRepository",
408
409
                           globalcontext, true,
                           SLOT(logRepository()));
410

Orgad Shaneh's avatar
Orgad Shaneh committed
411
    createRepositoryAction(localRepositoryMenu,
412
                           tr("Reflog"), "Git.ReflogRepository",
Orgad Shaneh's avatar
Orgad Shaneh committed
413
414
415
                           globalcontext, true,
                           SLOT(reflogRepository()));

Petar Perisin's avatar
Petar Perisin committed
416
    createRepositoryAction(localRepositoryMenu,
417
                           tr("Clean..."), "Git.CleanRepository",
Petar Perisin's avatar
Petar Perisin committed
418
419
420
                           globalcontext, true, SLOT(cleanRepository()));

    createRepositoryAction(localRepositoryMenu,
421
                           tr("Status"), "Git.StatusRepository",
422
                           globalcontext, true, &GitClient::status);
423

Petar Perisin's avatar
Petar Perisin committed
424
425
    // --------------
    localRepositoryMenu->addSeparator(globalcontext);
con's avatar
con committed
426

Petar Perisin's avatar
Petar Perisin committed
427
    ActionCommandPair actionCommand = createRepositoryAction(localRepositoryMenu,
428
                                                             tr("Commit..."), "Git.Commit",
Petar Perisin's avatar
Petar Perisin committed
429
430
                                                             globalcontext, true, SLOT(startCommit()));
    actionCommand.second->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+G,Meta+C") : tr("Alt+G,Alt+C")));
431

Petar Perisin's avatar
Petar Perisin committed
432
    createRepositoryAction(localRepositoryMenu,
433
                           tr("Amend Last Commit..."), "Git.AmendCommit",
Petar Perisin's avatar
Petar Perisin committed
434
                           globalcontext, true, SLOT(startAmendCommit()));
435

436
437
    m_fixupCommitAction =
            createRepositoryAction(localRepositoryMenu,
438
                                   tr("Fixup Previous Commit..."), "Git.FixupCommit",
439
                                   globalcontext, true, SLOT(startFixupCommit())).first;
440
441
    // --------------
    localRepositoryMenu->addSeparator(globalcontext);
442

Petar Perisin's avatar
Petar Perisin committed
443
    createRepositoryAction(localRepositoryMenu,
444
                           tr("Reset..."), "Git.Reset",
Orgad Shaneh's avatar
Orgad Shaneh committed
445
                           globalcontext, true, SLOT(resetRepository()));
446

447
448
    m_interactiveRebaseAction =
            createRepositoryAction(localRepositoryMenu,
449
                                   tr("Interactive Rebase..."), "Git.InteractiveRebase",
450
                                   globalcontext, true, SLOT(startRebase())).first;
Orgad Shaneh's avatar
Orgad Shaneh committed
451

Petar Perisin's avatar
Petar Perisin committed
452
453
    m_submoduleUpdateAction =
            createRepositoryAction(localRepositoryMenu,
454
                                   tr("Update Submodules"), "Git.SubmoduleUpdate",
Petar Perisin's avatar
Petar Perisin committed
455
                                   globalcontext, true, SLOT(updateSubmodules())).first;
Petar Perisin's avatar
Petar Perisin committed
456
457
    m_abortMergeAction =
            createRepositoryAction(localRepositoryMenu,
458
                                   tr("Abort Merge"), "Git.MergeAbort",
Petar Perisin's avatar
Petar Perisin committed
459
460
461
462
                                   globalcontext, true, SLOT(continueOrAbortCommand())).first;

    m_abortRebaseAction =
            createRepositoryAction(localRepositoryMenu,
463
                                   tr("Abort Rebase"), "Git.RebaseAbort",
Petar Perisin's avatar
Petar Perisin committed
464
465
466
467
                                   globalcontext, true, SLOT(continueOrAbortCommand())).first;

    m_abortCherryPickAction =
            createRepositoryAction(localRepositoryMenu,
468
                                   tr("Abort Cherry Pick"), "Git.CherryPickAbort",
Petar Perisin's avatar
Petar Perisin committed
469
470
471
472
                                   globalcontext, true, SLOT(continueOrAbortCommand())).first;

    m_abortRevertAction =
            createRepositoryAction(localRepositoryMenu,
473
                                   tr("Abort Revert"), "Git.RevertAbort",
Petar Perisin's avatar
Petar Perisin committed
474
475
476
477
                                   globalcontext, true, SLOT(continueOrAbortCommand())).first;

    m_continueRebaseAction =
            createRepositoryAction(localRepositoryMenu,
478
                                   tr("Continue Rebase"), "Git.RebaseContinue",
Petar Perisin's avatar
Petar Perisin committed
479
480
481
482
                                   globalcontext, true, SLOT(continueOrAbortCommand())).first;

    m_continueCherryPickAction =
            createRepositoryAction(localRepositoryMenu,
483
                                   tr("Continue Cherry Pick"), "Git.CherryPickContinue",
Petar Perisin's avatar
Petar Perisin committed
484
485
486
487
                                   globalcontext, true, SLOT(continueOrAbortCommand())).first;

    m_continueRevertAction =
            createRepositoryAction(localRepositoryMenu,
488
                                   tr("Continue Revert"), "Git.RevertContinue",
Petar Perisin's avatar
Petar Perisin committed
489
                                   globalcontext, true, SLOT(continueOrAbortCommand())).first;
Petar Perisin's avatar
Petar Perisin committed
490

491
    // --------------
Petar Perisin's avatar
Petar Perisin committed
492
    localRepositoryMenu->addSeparator(globalcontext);
493

Petar Perisin's avatar
Petar Perisin committed
494
    createRepositoryAction(localRepositoryMenu,
495
                           tr("Branches..."), "Git.BranchList",
Yuchen Deng's avatar
Yuchen Deng committed
496
                           globalcontext, true, SLOT(branchList()));
497
498

    // --------------
Petar Perisin's avatar
Petar Perisin committed
499
    localRepositoryMenu->addSeparator(globalcontext);
500

Petar Perisin's avatar
Petar Perisin committed
501
    // "Patch" menu
502
    Core::ActionContainer *patchMenu = Core::ActionManager::createMenu("Git.PatchMenu");
Petar Perisin's avatar
Petar Perisin committed
503
504
    patchMenu->menu()->setTitle(tr("&Patch"));
    localRepositoryMenu->addMenu(patchMenu);
505
506
507

    // Apply current file as patch is handled specially.
    parameterActionCommand =
Eike Ziller's avatar
Eike Ziller committed
508
            createParameterAction(patchMenu,
509
                                  tr("Apply from Editor"), tr("Apply \"%1\""),
510
                                  "Git.ApplyCurrentFilePatch",
511
512
513
514
515
                                  globalcontext, true);
    m_applyCurrentFilePatchAction = parameterActionCommand.first;
    connect(m_applyCurrentFilePatchAction, SIGNAL(triggered()), this,
            SLOT(applyCurrentFilePatch()));

Eike Ziller's avatar
Eike Ziller committed
516
    createRepositoryAction(patchMenu,
517
                           tr("Apply from File..."), "Git.ApplyPatch",
518
519
                           globalcontext, true, SLOT(promptApplyPatch()));

Petar Perisin's avatar
Petar Perisin committed
520
    // "Stash" menu
521
    Core::ActionContainer *stashMenu = Core::ActionManager::createMenu("Git.StashMenu");
Petar Perisin's avatar
Petar Perisin committed
522
523
    stashMenu->menu()->setTitle(tr("&Stash"));
    localRepositoryMenu->addMenu(stashMenu);
524

Eike Ziller's avatar
Eike Ziller committed
525
    createRepositoryAction(stashMenu,
526
                           tr("Stashes..."), "Git.StashList",
527
528
                           globalcontext, false, SLOT(stashList()));

529
    stashMenu->addSeparator(globalcontext);
530

Petar Perisin's avatar
Petar Perisin committed
531
    actionCommand = createRepositoryAction(stashMenu,
532
                                           tr("Stash"), "Git.Stash",
Petar Perisin's avatar
Petar Perisin committed
533
                                           globalcontext, true, SLOT(stash()));
534
535
    actionCommand.first->setToolTip(tr("Saves the current state of your work and resets the repository."));

Eike Ziller's avatar
Eike Ziller committed
536
    actionCommand = createRepositoryAction(stashMenu,
537
                                           tr("Take Snapshot..."), "Git.StashSnapshot",
538
539
540
                                           globalcontext, true, SLOT(stashSnapshot()));
    actionCommand.first->setToolTip(tr("Saves the current state of your work."));

541
    stashMenu->addSeparator(globalcontext);
542

Eike Ziller's avatar
Eike Ziller committed
543
    actionCommand = createRepositoryAction(stashMenu,
544
                                           tr("Stash Pop"), "Git.StashPop",
545
546
547
                                           globalcontext, true, &GitClient::stashPop);
    actionCommand.first->setToolTip(tr("Restores changes saved to the stash list using \"Stash\"."));

Petar Perisin's avatar
Petar Perisin committed
548
549
550
551
552
553

    /* \"Local Repository" menu */

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

    /*  "Remote Repository" menu */
554
    Core::ActionContainer *remoteRepositoryMenu = Core::ActionManager::createMenu("Git.RemoteRepositoryMenu");
Petar Perisin's avatar
Petar Perisin committed
555
556
557
558
    remoteRepositoryMenu->menu()->setTitle(tr("&Remote Repository"));
    gitContainer->addMenu(remoteRepositoryMenu);

    createRepositoryAction(remoteRepositoryMenu,
559
                           tr("Fetch"), "Git.Fetch",
Petar Perisin's avatar
Petar Perisin committed
560
561
562
                           globalcontext, true, SLOT(fetch()));

    createRepositoryAction(remoteRepositoryMenu,
563
                           tr("Pull"), "Git.Pull",
Petar Perisin's avatar
Petar Perisin committed
564
565
566
                           globalcontext, true, SLOT(pull()));

    actionCommand = createRepositoryAction(remoteRepositoryMenu,
567
                                           tr("Push"), "Git.Push",
Petar Perisin's avatar
Petar Perisin committed
568
569
570
571
572
573
                                           globalcontext, true, SLOT(push()));

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

    // "Subversion" menu
574
    Core::ActionContainer *subversionMenu = Core::ActionManager::createMenu("Git.Subversion");
Petar Perisin's avatar
Petar Perisin committed
575
576
    subversionMenu->menu()->setTitle(tr("&Subversion"));
    remoteRepositoryMenu->addMenu(subversionMenu);
577

Eike Ziller's avatar
Eike Ziller committed
578
    createRepositoryAction(subversionMenu,
579
                           tr("Log"), "Git.Subversion.Log",
580
581
                           globalcontext, false, &GitClient::subversionLog);

Eike Ziller's avatar
Eike Ziller committed
582
    createRepositoryAction(subversionMenu,
583
                           tr("Fetch"), "Git.Subversion.Fetch",
584
585
                           globalcontext, false, &GitClient::synchronousSubversionFetch);

Petar Perisin's avatar
Petar Perisin committed
586
587
    // --------------
    remoteRepositoryMenu->addSeparator(globalcontext);
588

Petar Perisin's avatar
Petar Perisin committed
589
    createRepositoryAction(remoteRepositoryMenu,
590
                           tr("Manage Remotes..."), "Git.RemoteList",
Petar Perisin's avatar
Petar Perisin committed
591
                           globalcontext, false, SLOT(remoteList()));
592

Petar Perisin's avatar
Petar Perisin committed
593
    /* \"Remote Repository" menu */
Robert Loehning's avatar
Robert Loehning committed
594

Petar Perisin's avatar
Petar Perisin committed
595
    // --------------
596

597
    /*  Actions only in locator */
598
    createRepositoryAction(0, tr("Show..."), "Git.Show",
599
600
                           globalcontext, true, SLOT(startChangeRelatedAction()));

601
    createRepositoryAction(0, tr("Revert..."), "Git.Revert",
602
603
                           globalcontext, true, SLOT(startChangeRelatedAction()));

604
    createRepositoryAction(0, tr("Cherry Pick..."), "Git.CherryPick",
605
606
                           globalcontext, true, SLOT(startChangeRelatedAction()));

607
    createRepositoryAction(0, tr("Checkout..."), "Git.Checkout",
608
609
                           globalcontext, true, SLOT(startChangeRelatedAction()));

610
    createRepositoryAction(0, tr("Rebase..."), "Git.Rebase",
611
612
                           globalcontext, true, SLOT(branchList()));

613
    createRepositoryAction(0, tr("Merge..."), "Git.Merge",
614
615
616
617
618
619
                           globalcontext, true, SLOT(branchList()));

    /*  \Actions only in locator */

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

Petar Perisin's avatar
Petar Perisin committed
620
    /*  "Git Tools" menu */
621
    Core::ActionContainer *gitToolsMenu = Core::ActionManager::createMenu("Git.GitToolsMenu");
Petar Perisin's avatar
Petar Perisin committed
622
623
    gitToolsMenu->menu()->setTitle(tr("Git &Tools"));
    gitContainer->addMenu(gitToolsMenu);
624

Petar Perisin's avatar
Petar Perisin committed
625
    createRepositoryAction(gitToolsMenu,
626
                           tr("Gitk"), "Git.LaunchGitK",
Petar Perisin's avatar
Petar Perisin committed
627
                           globalcontext, true, &GitClient::launchGitK);
628

Petar Perisin's avatar
Petar Perisin committed
629
630
631
    parameterActionCommand
            = createFileAction(gitToolsMenu,
                               tr("Gitk Current File"), tr("Gitk of \"%1\""),
632
                               "Git.GitkFile", globalcontext, true, SLOT(gitkForCurrentFile()));
Petar Perisin's avatar
Petar Perisin committed
633
634
635
636

    parameterActionCommand
            = createFileAction(gitToolsMenu,
                               tr("Gitk for folder of Current File"), tr("Gitk for folder of \"%1\""),
637
                               "Git.GitkFolder", globalcontext, true, SLOT(gitkForCurrentFolder()));
Petar Perisin's avatar
Petar Perisin committed
638
639
640
641

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

642
    createRepositoryAction(gitToolsMenu, tr("Git Gui"), "Git.GitGui",
643
644
645
646
647
                           globalcontext, true, SLOT(gitGui()));

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

Petar Perisin's avatar
Petar Perisin committed
648
649
    m_repositoryBrowserAction
            = createRepositoryAction(gitToolsMenu,
650
                                     tr("Repository Browser"), "Git.LaunchRepositoryBrowser",
Petar Perisin's avatar
Petar Perisin committed
651
                                     globalcontext, true, &GitClient::launchRepositoryBrowser).first;
652

Petar Perisin's avatar
Petar Perisin committed
653
654
    m_mergeToolAction =
            createRepositoryAction(gitToolsMenu,
655
                                   tr("Merge Tool"), "Git.MergeTool",
Petar Perisin's avatar
Petar Perisin committed
656
                                   globalcontext, true, SLOT(startMergeTool())).first;
657

Petar Perisin's avatar
Petar Perisin committed
658
659
660
    /* \"Git Tools" menu */

    // --------------
661
    gitContainer->addSeparator(globalcontext);
662

663
664
665
666
667
    QAction *changesAction = new QAction(tr("Actions on Commits..."), this);
    Core::Command *changesCommand = Core::ActionManager::registerAction(changesAction, "Git.ChangeActions", globalcontext);
    connect(changesAction, SIGNAL(triggered()), this, SLOT(startChangeRelatedAction()));
    gitContainer->addAction(changesCommand);

668
669
670
    QAction *repositoryAction = new QAction(tr("Create Repository..."), this);
    Core::Command *createRepositoryCommand = Core::ActionManager::registerAction(repositoryAction, "Git.CreateRepository", globalcontext);
    connect(repositoryAction, SIGNAL(triggered()), this, SLOT(createRepository()));
Petar Perisin's avatar
Petar Perisin committed
671
672
    gitContainer->addAction(createRepositoryCommand);

673
674
675
676
    if (0) {
        const QList<QAction*> snapShotActions = createSnapShotTestActions();
        const int count = snapShotActions.size();
        for (int i = 0; i < count; i++) {
677
            Core::Command *tCommand
Eike Ziller's avatar
Eike Ziller committed
678
                    = Core::ActionManager::registerAction(snapShotActions.at(i),
hjk's avatar
hjk committed
679
                                                    Core::Id("Git.Snapshot.").withSuffix(i),
680
                                                    globalcontext);
681
            gitContainer->addAction(tCommand);
682
683
684
        }
    }

con's avatar
con committed
685
    // Submit editor
686
    Core::Context submitContext(Constants::C_GITSUBMITEDITOR);
hjk's avatar
hjk committed
687
    m_submitCurrentAction = new QAction(VcsBase::VcsBaseSubmitEditor::submitIcon(), tr("Commit"), this);
Eike Ziller's avatar
Eike Ziller committed
688
    Core::Command *command = Core::ActionManager::registerAction(m_submitCurrentAction, Constants::SUBMIT_CURRENT, submitContext);
689
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
690
691
    connect(m_submitCurrentAction, SIGNAL(triggered()), this, SLOT(submitCurrentLog()));

hjk's avatar
hjk committed
692
    m_diffSelectedFilesAction = new QAction(VcsBase::VcsBaseSubmitEditor::diffIcon(), tr("Diff &Selected Files"), this);
Eike Ziller's avatar
Eike Ziller committed
693
    command = Core::ActionManager::registerAction(m_diffSelectedFilesAction, Constants::DIFF_SELECTED, submitContext);
con's avatar
con committed
694
695

    m_undoAction = new QAction(tr("&Undo"), this);
Eike Ziller's avatar
Eike Ziller committed
696
    command = Core::ActionManager::registerAction(m_undoAction, Core::Constants::UNDO, submitContext);
con's avatar
con committed
697
698

    m_redoAction = new QAction(tr("&Redo"), this);
Eike Ziller's avatar
Eike Ziller committed
699
    command = Core::ActionManager::registerAction(m_redoAction, Core::Constants::REDO, submitContext);
con's avatar
con committed
700

hjk's avatar
hjk committed
701
    connect(Core::VcsManager::instance(), SIGNAL(repositoryChanged(QString)),
702
            this, SLOT(updateContinueAndAbortCommands()));
hjk's avatar
hjk committed
703
    connect(Core::VcsManager::instance(), SIGNAL(repositoryChanged(QString)),
704
            this, SLOT(updateBranches(QString)), Qt::QueuedConnection);
705

hjk's avatar
hjk committed
706
    if (!Core::MimeDatabase::addMimeTypes(QLatin1String(RC_GIT_MIME_XML), errorMessage))
707
708
        return false;

Petar Perisin's avatar
Petar Perisin committed
709
    /* "Gerrit" */
710
711
712
713
714
715
    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
716
717
}

718
GitVersionControl *GitPlugin::gitVersionControl() const
719
{
720
    return static_cast<GitVersionControl *>(versionControl());
721
722
}

723
void GitPlugin::submitEditorDiff(const QStringList &unstaged, const QStringList &staged)
con's avatar
con committed
724
{
Orgad Shaneh's avatar
Orgad Shaneh committed
725
    m_gitClient->diff(m_submitRepository, unstaged, staged);
con's avatar
con committed
726
727
}

728
729
730
731
732
void GitPlugin::submitEditorMerge(const QStringList &unmerged)
{
    m_gitClient->merge(m_submitRepository, unmerged);
}