Skip to content
Snippets Groups Projects
gitplugin.cpp 62.7 KiB
Newer Older
hjk's avatar
hjk committed
/****************************************************************************
con's avatar
con committed
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
**
hjk's avatar
hjk committed
** This file is part of Qt Creator.
con's avatar
con committed
**
hjk's avatar
hjk committed
** 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.
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
** 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
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
****************************************************************************/
hjk's avatar
hjk committed

con's avatar
con committed
#include "gitplugin.h"
hjk's avatar
hjk committed

#include "changeselectiondialog.h"
#include "commitdata.h"
con's avatar
con committed
#include "gitclient.h"
#include "gitconstants.h"
hjk's avatar
hjk committed
#include "giteditor.h"
con's avatar
con committed
#include "gitsubmiteditor.h"
hjk's avatar
hjk committed
#include "gitversioncontrol.h"
#include "branchdialog.h"
#include "remotedialog.h"
#include "gitorious/gitoriousclonewizard.h"
#include "stashdialog.h"
#include "settingspage.h"
#include "logchangedialog.h"
#include "mergetool.h"
Orgad Shaneh's avatar
Orgad Shaneh committed
#include "gitutils.h"
con's avatar
con committed

#include "gerrit/gerritplugin.h"
con's avatar
con committed
#include <coreplugin/icore.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/documentmanager.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/command.h>
Orgad Shaneh's avatar
Orgad Shaneh committed
#include <coreplugin/infobar.h>
con's avatar
con committed
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/mimedatabase.h>
#include <coreplugin/vcsmanager.h>
hjk's avatar
hjk committed

#include <utils/qtcassert.h>
#include <utils/parameteraction.h>
con's avatar
con committed
#include <vcsbase/basevcseditorfactory.h>
#include <vcsbase/submitfilemodel.h>
con's avatar
con committed
#include <vcsbase/vcsbaseeditor.h>
#include <vcsbase/basevcssubmiteditorfactory.h>
#include <vcsbase/vcsbaseoutputwindow.h>
#include <vcsbase/cleandialog.h>
Friedemann Kleint's avatar
Friedemann Kleint committed
#include <locator/commandlocator.h>
con's avatar
con committed

#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QtPlugin>
#include <QAction>
#include <QFileDialog>
#include <QMessageBox>
#include <QScopedPointer>
con's avatar
con committed

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

hjk's avatar
hjk committed
static const VcsBase::VcsBaseEditorParameters editorParameters[] = {
con's avatar
con committed
{
    VcsBase::OtherContent,
    Git::Constants::GIT_COMMAND_LOG_EDITOR_ID,
    Git::Constants::GIT_COMMAND_LOG_EDITOR_DISPLAY_NAME,
    Git::Constants::C_GIT_COMMAND_LOG_EDITOR,
Orgad Shaneh's avatar
Orgad Shaneh committed
    "text/vnd.qtcreator.git.commandlog"},
hjk's avatar
hjk committed
{   VcsBase::LogOutput,
    Git::Constants::GIT_LOG_EDITOR_ID,
    Git::Constants::GIT_LOG_EDITOR_DISPLAY_NAME,
    Git::Constants::C_GIT_LOG_EDITOR,
Orgad Shaneh's avatar
Orgad Shaneh committed
    "text/vnd.qtcreator.git.log"},
hjk's avatar
hjk committed
{   VcsBase::AnnotateOutput,
    Git::Constants::GIT_BLAME_EDITOR_ID,
    Git::Constants::GIT_BLAME_EDITOR_DISPLAY_NAME,
    Git::Constants::C_GIT_BLAME_EDITOR,
Orgad Shaneh's avatar
Orgad Shaneh committed
    "text/vnd.qtcreator.git.annotation"},
hjk's avatar
hjk committed
{   VcsBase::DiffOutput,
    Git::Constants::GIT_DIFF_EDITOR_ID,
    Git::Constants::GIT_DIFF_EDITOR_DISPLAY_NAME,
    Git::Constants::C_GIT_DIFF_EDITOR,
{   VcsBase::OtherContent,
    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"},
{   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
};

Q_DECLARE_METATYPE(Git::Internal::GitClientMemberFunc)

con's avatar
con committed
using namespace Git;
using namespace Git::Internal;

// GitPlugin

GitPlugin *GitPlugin::m_instance = 0;

GitPlugin::GitPlugin() :
Friedemann Kleint's avatar
Friedemann Kleint committed
    m_commandLocator(0),
con's avatar
con committed
    m_submitCurrentAction(0),
    m_diffSelectedFilesAction(0),
    m_undoAction(0),
    m_redoAction(0),
    m_applyCurrentFilePatchAction(0),
con's avatar
con committed
    m_gitClient(0),
    m_submitActionTriggered(false)
con's avatar
con committed
{
    m_instance = this;
    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
}

GitPlugin::~GitPlugin()
{
con's avatar
con committed
    delete m_gitClient;
    m_instance = 0;
}

void GitPlugin::cleanCommitMessageFile()
con's avatar
con committed
{
    if (!m_commitMessageFileName.isEmpty()) {
        QFile::remove(m_commitMessageFileName);
        m_commitMessageFileName.clear();
con's avatar
con committed
    }
}

bool GitPlugin::isCommitEditorOpen() const
{
    return !m_commitMessageFileName.isEmpty();
}

con's avatar
con committed
GitPlugin *GitPlugin::instance()
{
    return m_instance;
}

hjk's avatar
hjk committed
static const VcsBase::VcsBaseSubmitEditorParameters submitParameters = {
con's avatar
con committed
    Git::Constants::SUBMIT_MIMETYPE,
    Git::Constants::GITSUBMITEDITOR_ID,
    Git::Constants::GITSUBMITEDITOR_DISPLAY_NAME,
    Git::Constants::C_GITSUBMITEDITOR,
    VcsBase::VcsBaseSubmitEditorParameters::DiffRows
con's avatar
con committed
};

// Create a parameter action
ParameterActionCommandPair
Eike Ziller's avatar
Eike Ziller committed
        GitPlugin::createParameterAction(Core::ActionContainer *ac,
                                         const QString &defaultText, const QString &parameterText,
hjk's avatar
hjk committed
                                         const Core::Id &id, const Core::Context &context,
                                         bool addToLocator)
{
    Utils::ParameterAction *action = new Utils::ParameterAction(defaultText, parameterText,
                                                                Utils::ParameterAction::EnabledWithParameter,
                                                                this);
Eike Ziller's avatar
Eike Ziller committed
    Core::Command *command = Core::ActionManager::registerAction(action, id, context);
    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
        GitPlugin::createFileAction(Core::ActionContainer *ac,
                                    const QString &defaultText, const QString &parameterText,
hjk's avatar
hjk committed
                                    const Core::Id &id, const Core::Context &context, bool addToLocator,
Eike Ziller's avatar
Eike Ziller committed
    const ParameterActionCommandPair rc = createParameterAction(ac, defaultText, parameterText, id, context, addToLocator);
    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
        GitPlugin::createProjectAction(Core::ActionContainer *ac,
                                       const QString &defaultText, const QString &parameterText,
hjk's avatar
hjk committed
                                       const Core::Id &id, const Core::Context &context, bool addToLocator,
Eike Ziller's avatar
Eike Ziller committed
    const ParameterActionCommandPair rc = createParameterAction(ac, defaultText, parameterText, id, context, addToLocator);
    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
        GitPlugin::createRepositoryAction(Core::ActionContainer *ac,
hjk's avatar
hjk committed
                                          const QString &text, const Core::Id &id,
                                          const Core::Context &context, bool addToLocator)
{
    QAction  *action = new QAction(text, this);
Eike Ziller's avatar
Eike Ziller committed
    Core::Command *command = Core::ActionManager::registerAction(action, id, context);
    if (ac)
        ac->addAction(command);
    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
        GitPlugin::createRepositoryAction(Core::ActionContainer *ac,
hjk's avatar
hjk committed
                                          const QString &text, const Core::Id &id,
                                          const Core::Context &context, bool addToLocator,
Eike Ziller's avatar
Eike Ziller committed
    const ActionCommandPair rc = createRepositoryAction(ac, text, id, context, addToLocator);
    connect(rc.first, SIGNAL(triggered()), this, pluginSlot);
    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
        GitPlugin::createRepositoryAction(Core::ActionContainer *ac,
hjk's avatar
hjk committed
                                          const QString &text, const Core::Id &id,
                                          const Core::Context &context, bool addToLocator,
                                          GitClientMemberFunc func)
{
    // Set the member func as data and connect to generic slot
Eike Ziller's avatar
Eike Ziller committed
    const ActionCommandPair rc = createRepositoryAction(ac, text, id, context, addToLocator);
    rc.first->setData(qVariantFromValue(func));
    connect(rc.first, SIGNAL(triggered()), this, SLOT(gitClientMemberFuncRepositoryAction()));
    return rc;
}

bool GitPlugin::initialize(const QStringList &arguments, QString *errorMessage)
con's avatar
con committed
{
hjk's avatar
hjk committed
    m_settings.readSettings(Core::ICore::settings());

    m_gitClient = new GitClient(&m_settings);
hjk's avatar
hjk committed
    typedef VcsBase::VcsEditorFactory<GitEditor> GitEditorFactory;
    typedef VcsBase::VcsSubmitEditorFactory<GitSubmitEditor> GitSubmitEditorFactory;
con's avatar
con committed

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

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

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

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

    addAutoReleasedObject(new GitSubmitEditorFactory(&submitParameters));
    addAutoReleasedObject(new CloneWizard);
    addAutoReleasedObject(new Gitorious::Internal::GitoriousCloneWizard);
Friedemann Kleint's avatar
Friedemann Kleint committed
    const QString prefix = QLatin1String("git");
    m_commandLocator = new Locator::CommandLocator("Git", prefix, prefix);
Friedemann Kleint's avatar
Friedemann Kleint committed
    addAutoReleasedObject(m_commandLocator);

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

Eike Ziller's avatar
Eike Ziller committed
    Core::ActionContainer *gitContainer = Core::ActionManager::createMenu("Git");
con's avatar
con committed
    gitContainer->menu()->setTitle(tr("&Git"));
    toolsContainer->addMenu(gitContainer);
    m_menuAction = gitContainer->menu()->menuAction();
Petar Perisin's avatar
Petar Perisin committed
    /*  "Current File" menu */
    Core::ActionContainer *currentFileMenu = Core::ActionManager::createMenu(Core::Id("Git.CurrentFileMenu"));
    currentFileMenu->menu()->setTitle(tr("Current &File"));
    gitContainer->addMenu(currentFileMenu);
Petar Perisin's avatar
Petar Perisin committed
    ParameterActionCommandPair parameterActionCommand
            = createFileAction(currentFileMenu,
                               tr("Diff Current File"), tr("Diff of \"%1\""),
hjk's avatar
hjk committed
                               Core::Id("Git.Diff"), globalcontext, true,
                               SLOT(diffCurrentFile()));
    parameterActionCommand.second->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+G,Meta+D") : tr("Alt+G,Alt+D")));
Petar Perisin's avatar
Petar Perisin committed
            = createFileAction(currentFileMenu,
                               tr("Log Current File"), tr("Log of \"%1\""),
hjk's avatar
hjk committed
                               Core::Id("Git.Log"), globalcontext, true, SLOT(logFile()));
    parameterActionCommand.second->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+G,Meta+L") : tr("Alt+G,Alt+L")));
Petar Perisin's avatar
Petar Perisin committed
    parameterActionCommand
                = createFileAction(currentFileMenu,
                                   tr("Blame Current File"), tr("Blame for \"%1\""),
                                   Core::Id("Git.Blame"),
                                   globalcontext, true, SLOT(blameFile()));
    parameterActionCommand.second->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+G,Meta+B") : tr("Alt+G,Alt+B")));

Petar Perisin's avatar
Petar Perisin committed
    currentFileMenu->addSeparator(globalcontext);
Petar Perisin's avatar
Petar Perisin committed
            = createFileAction(currentFileMenu,
                               tr("Stage File for Commit"), tr("Stage \"%1\" for Commit"),
hjk's avatar
hjk committed
                               Core::Id("Git.Stage"), globalcontext, true, SLOT(stageFile()));
    parameterActionCommand.second->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+G,Meta+A") : tr("Alt+G,Alt+A")));
Petar Perisin's avatar
Petar Perisin committed
            = createFileAction(currentFileMenu,
                               tr("Unstage File from Commit"), tr("Unstage \"%1\" from Commit"),
hjk's avatar
hjk committed
                               Core::Id("Git.Unstage"), globalcontext, true, SLOT(unstageFile()));
    parameterActionCommand
Petar Perisin's avatar
Petar Perisin committed
            = createFileAction(currentFileMenu,
                               tr("Undo Unstaged Changes"), tr("Undo Unstaged Changes for \"%1\""),
hjk's avatar
hjk committed
                               Core::Id("Git.UndoUnstaged"), globalcontext,
                               true, SLOT(undoUnstagedFileChanges()));

    parameterActionCommand
Petar Perisin's avatar
Petar Perisin committed
            = createFileAction(currentFileMenu,
                               tr("Undo Uncommitted Changes"), tr("Undo Uncommitted Changes for \"%1\""),
hjk's avatar
hjk committed
                               Core::Id("Git.Undo"), globalcontext,
                               true, SLOT(undoFileChanges()));
    parameterActionCommand.second->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+G,Meta+U") : tr("Alt+G,Alt+U")));
Petar Perisin's avatar
Petar Perisin committed
    /* \"Current File" menu */

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

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

Petar Perisin's avatar
Petar Perisin committed
            = createProjectAction(currentProjectMenu,
                                  tr("Diff Current Project"), tr("Diff Project \"%1\""),
hjk's avatar
hjk committed
                                  Core::Id("Git.DiffProject"),
                                  globalcontext, true,
                                  SLOT(diffCurrentProject()));
    parameterActionCommand.second->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+G,Meta+Shift+D") : tr("Alt+G,Alt+Shift+D")));
Petar Perisin's avatar
Petar Perisin committed
            = createProjectAction(currentProjectMenu,
                                  tr("Log Project"), tr("Log Project \"%1\""),
hjk's avatar
hjk committed
                                  Core::Id("Git.LogProject"), globalcontext, true,
    parameterActionCommand.second->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+G,Meta+K") : tr("Alt+G,Alt+K")));
Petar Perisin's avatar
Petar Perisin committed
                = createProjectAction(currentProjectMenu,
                                      tr("Clean Project..."), tr("Clean Project \"%1\"..."),
hjk's avatar
hjk committed
                                      Core::Id("Git.CleanProject"), globalcontext,
con's avatar
con committed

Petar Perisin's avatar
Petar Perisin committed
    /* \"Current Project" menu */
con's avatar
con committed

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

    createRepositoryAction(localRepositoryMenu,
hjk's avatar
hjk committed
                           tr("Diff"), Core::Id("Git.DiffRepository"),
                           globalcontext, true, SLOT(diffRepository()));
Petar Perisin's avatar
Petar Perisin committed
    createRepositoryAction(localRepositoryMenu,
hjk's avatar
hjk committed
                           tr("Log"), Core::Id("Git.LogRepository"),
                           globalcontext, true,
                           SLOT(logRepository()));
Petar Perisin's avatar
Petar Perisin committed
    createRepositoryAction(localRepositoryMenu,
                           tr("Clean..."), Core::Id("Git.CleanRepository"),
                           globalcontext, true, SLOT(cleanRepository()));

    createRepositoryAction(localRepositoryMenu,
hjk's avatar
hjk committed
                           tr("Status"), Core::Id("Git.StatusRepository"),
                           globalcontext, true, &GitClient::status);
Petar Perisin's avatar
Petar Perisin committed
    // --------------
    localRepositoryMenu->addSeparator(globalcontext);
con's avatar
con committed

Petar Perisin's avatar
Petar Perisin committed
    ActionCommandPair actionCommand = createRepositoryAction(localRepositoryMenu,
                                                             tr("Commit..."), Core::Id("Git.Commit"),
                                                             globalcontext, true, SLOT(startCommit()));
    actionCommand.second->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+G,Meta+C") : tr("Alt+G,Alt+C")));
Petar Perisin's avatar
Petar Perisin committed
    createRepositoryAction(localRepositoryMenu,
                           tr("Amend Last Commit..."), Core::Id("Git.AmendCommit"),
                           globalcontext, true, SLOT(startAmendCommit()));
    m_fixupCommitAction =
            createRepositoryAction(localRepositoryMenu,
                                   tr("Fixup Previous Commit..."), Core::Id("Git.FixupCommit"),
                                   globalcontext, true, SLOT(startFixupCommit())).first;
    // --------------
    localRepositoryMenu->addSeparator(globalcontext);
Petar Perisin's avatar
Petar Perisin committed
    createRepositoryAction(localRepositoryMenu,
                           tr("Reset..."), Core::Id("Git.Reset"),
Orgad Shaneh's avatar
Orgad Shaneh committed
                           globalcontext, true, SLOT(resetRepository()));
    m_interactiveRebaseAction =
            createRepositoryAction(localRepositoryMenu,
                                   tr("Interactive Rebase..."), Core::Id("Git.InteractiveRebase"),
                                   globalcontext, true, SLOT(startRebase())).first;
    m_submoduleUpdateAction =
            createRepositoryAction(localRepositoryMenu,
                                   tr("Update Submodules"), Core::Id("Git.SubmoduleUpdate"),
                                   globalcontext, true, SLOT(updateSubmodules())).first;
    m_abortMergeAction =
            createRepositoryAction(localRepositoryMenu,
                                   tr("Abort Merge"), Core::Id("Git.MergeAbort"),
                                   globalcontext, true, SLOT(continueOrAbortCommand())).first;

    m_abortRebaseAction =
            createRepositoryAction(localRepositoryMenu,
                                   tr("Abort Rebase"), Core::Id("Git.RebaseAbort"),
                                   globalcontext, true, SLOT(continueOrAbortCommand())).first;

    m_abortCherryPickAction =
            createRepositoryAction(localRepositoryMenu,
                                   tr("Abort Cherry Pick"), Core::Id("Git.CherryPickAbort"),
                                   globalcontext, true, SLOT(continueOrAbortCommand())).first;

    m_abortRevertAction =
            createRepositoryAction(localRepositoryMenu,
                                   tr("Abort Revert"), Core::Id("Git.RevertAbort"),
                                   globalcontext, true, SLOT(continueOrAbortCommand())).first;

    m_continueRebaseAction =
            createRepositoryAction(localRepositoryMenu,
                                   tr("Continue Rebase"), Core::Id("Git.RebaseContinue"),
                                   globalcontext, true, SLOT(continueOrAbortCommand())).first;

    m_continueCherryPickAction =
            createRepositoryAction(localRepositoryMenu,
                                   tr("Continue Cherry Pick"), Core::Id("Git.CherryPickContinue"),
                                   globalcontext, true, SLOT(continueOrAbortCommand())).first;

    m_continueRevertAction =
            createRepositoryAction(localRepositoryMenu,
                                   tr("Continue Revert"), Core::Id("Git.RevertContinue"),
                                   globalcontext, true, SLOT(continueOrAbortCommand())).first;
    // --------------
Petar Perisin's avatar
Petar Perisin committed
    localRepositoryMenu->addSeparator(globalcontext);
Petar Perisin's avatar
Petar Perisin committed
    createRepositoryAction(localRepositoryMenu,
hjk's avatar
hjk committed
                           tr("Branches..."), Core::Id("Git.BranchList"),
                           globalcontext, true, SLOT(branchList()));
Petar Perisin's avatar
Petar Perisin committed
    localRepositoryMenu->addSeparator(globalcontext);
Petar Perisin's avatar
Petar Perisin committed
    // "Patch" menu
Eike Ziller's avatar
Eike Ziller committed
    Core::ActionContainer *patchMenu = Core::ActionManager::createMenu(Core::Id("Git.PatchMenu"));
Petar Perisin's avatar
Petar Perisin committed
    patchMenu->menu()->setTitle(tr("&Patch"));
    localRepositoryMenu->addMenu(patchMenu);

    // Apply current file as patch is handled specially.
    parameterActionCommand =
Eike Ziller's avatar
Eike Ziller committed
            createParameterAction(patchMenu,
                                  tr("Apply from Editor"), tr("Apply \"%1\""),
hjk's avatar
hjk committed
                                  Core::Id("Git.ApplyCurrentFilePatch"),
                                  globalcontext, true);
    m_applyCurrentFilePatchAction = parameterActionCommand.first;
    connect(m_applyCurrentFilePatchAction, SIGNAL(triggered()), this,
            SLOT(applyCurrentFilePatch()));

Eike Ziller's avatar
Eike Ziller committed
    createRepositoryAction(patchMenu,
hjk's avatar
hjk committed
                           tr("Apply from File..."), Core::Id("Git.ApplyPatch"),
                           globalcontext, true, SLOT(promptApplyPatch()));

Petar Perisin's avatar
Petar Perisin committed
    // "Stash" menu
Eike Ziller's avatar
Eike Ziller committed
    Core::ActionContainer *stashMenu = Core::ActionManager::createMenu(Core::Id("Git.StashMenu"));
Petar Perisin's avatar
Petar Perisin committed
    stashMenu->menu()->setTitle(tr("&Stash"));
    localRepositoryMenu->addMenu(stashMenu);
Eike Ziller's avatar
Eike Ziller committed
    createRepositoryAction(stashMenu,
hjk's avatar
hjk committed
                           tr("Stashes..."), Core::Id("Git.StashList"),
                           globalcontext, false, SLOT(stashList()));

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

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

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

Petar Perisin's avatar
Petar Perisin committed

    /* \"Local Repository" menu */

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

    /*  "Remote Repository" menu */
    Core::ActionContainer *remoteRepositoryMenu = Core::ActionManager::createMenu(Core::Id("Git.RemoteRepositoryMenu"));
    remoteRepositoryMenu->menu()->setTitle(tr("&Remote Repository"));
    gitContainer->addMenu(remoteRepositoryMenu);

    createRepositoryAction(remoteRepositoryMenu,
                           tr("Fetch"), Core::Id("Git.Fetch"),
                           globalcontext, true, SLOT(fetch()));

    createRepositoryAction(remoteRepositoryMenu,
                           tr("Pull"), Core::Id("Git.Pull"),
                           globalcontext, true, SLOT(pull()));

    actionCommand = createRepositoryAction(remoteRepositoryMenu,
                                           tr("Push"), Core::Id("Git.Push"),
                                           globalcontext, true, SLOT(push()));

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

    // "Subversion" menu
Eike Ziller's avatar
Eike Ziller committed
    Core::ActionContainer *subversionMenu = Core::ActionManager::createMenu(Core::Id("Git.Subversion"));
Petar Perisin's avatar
Petar Perisin committed
    subversionMenu->menu()->setTitle(tr("&Subversion"));
    remoteRepositoryMenu->addMenu(subversionMenu);
Eike Ziller's avatar
Eike Ziller committed
    createRepositoryAction(subversionMenu,
hjk's avatar
hjk committed
                           tr("Log"), Core::Id("Git.Subversion.Log"),
                           globalcontext, false, &GitClient::subversionLog);

Eike Ziller's avatar
Eike Ziller committed
    createRepositoryAction(subversionMenu,
hjk's avatar
hjk committed
                           tr("Fetch"), Core::Id("Git.Subversion.Fetch"),
                           globalcontext, false, &GitClient::synchronousSubversionFetch);

Petar Perisin's avatar
Petar Perisin committed
    // --------------
    remoteRepositoryMenu->addSeparator(globalcontext);
Petar Perisin's avatar
Petar Perisin committed
    createRepositoryAction(remoteRepositoryMenu,
                           tr("Manage Remotes..."), Core::Id("Git.RemoteList"),
                           globalcontext, false, SLOT(remoteList()));
Petar Perisin's avatar
Petar Perisin committed
    /* \"Remote Repository" menu */
Petar Perisin's avatar
Petar Perisin committed
    // --------------
    /*  Actions only in locator */
    createRepositoryAction(0, tr("Show..."), Core::Id("Git.Show"),
                           globalcontext, true, SLOT(startChangeRelatedAction()));

    createRepositoryAction(0, tr("Revert..."), Core::Id("Git.Revert"),
                           globalcontext, true, SLOT(startChangeRelatedAction()));

    createRepositoryAction(0, tr("Cherry Pick..."), Core::Id("Git.CherryPick"),
                           globalcontext, true, SLOT(startChangeRelatedAction()));

    createRepositoryAction(0, tr("Checkout..."), Core::Id("Git.Checkout"),
                           globalcontext, true, SLOT(startChangeRelatedAction()));

    createRepositoryAction(0, tr("Rebase..."), Core::Id("Git.Rebase"),
                           globalcontext, true, SLOT(branchList()));

    createRepositoryAction(0, tr("Merge..."), Core::Id("Git.Merge"),
                           globalcontext, true, SLOT(branchList()));

    /*  \Actions only in locator */

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

Petar Perisin's avatar
Petar Perisin committed
    /*  "Git Tools" menu */
    Core::ActionContainer *gitToolsMenu = Core::ActionManager::createMenu(Core::Id("Git.GitToolsMenu"));
    gitToolsMenu->menu()->setTitle(tr("Git &Tools"));
    gitContainer->addMenu(gitToolsMenu);
Petar Perisin's avatar
Petar Perisin committed
    createRepositoryAction(gitToolsMenu,
                           tr("Gitk"), Core::Id("Git.LaunchGitK"),
                           globalcontext, true, &GitClient::launchGitK);
Petar Perisin's avatar
Petar Perisin committed
    parameterActionCommand
            = createFileAction(gitToolsMenu,
                               tr("Gitk Current File"), tr("Gitk of \"%1\""),
                               Core::Id("Git.GitkFile"), globalcontext, true, SLOT(gitkForCurrentFile()));

    parameterActionCommand
            = createFileAction(gitToolsMenu,
                               tr("Gitk for folder of Current File"), tr("Gitk for folder of \"%1\""),
                               Core::Id("Git.GitkFolder"), globalcontext, true, SLOT(gitkForCurrentFolder()));

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

    createRepositoryAction(gitToolsMenu, tr("Git Gui"), Core::Id("Git.GitGui"),
                           globalcontext, true, SLOT(gitGui()));

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

Petar Perisin's avatar
Petar Perisin committed
    m_repositoryBrowserAction
            = createRepositoryAction(gitToolsMenu,
                                     tr("Repository Browser"), Core::Id("Git.LaunchRepositoryBrowser"),
                                     globalcontext, true, &GitClient::launchRepositoryBrowser).first;
    m_mergeToolAction =
            createRepositoryAction(gitToolsMenu,
                                   tr("Merge Tool"), Core::Id("Git.MergeTool"),
                                   globalcontext, true, SLOT(startMergeTool())).first;
Petar Perisin's avatar
Petar Perisin committed
    /* \"Git Tools" menu */

    // --------------
    gitContainer->addSeparator(globalcontext);
    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);

    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
    gitContainer->addAction(createRepositoryCommand);

    if (0) {
        const QList<QAction*> snapShotActions = createSnapShotTestActions();
        const int count = snapShotActions.size();
        for (int i = 0; i < count; i++) {
Eike Ziller's avatar
Eike Ziller committed
                    = Core::ActionManager::registerAction(snapShotActions.at(i),
hjk's avatar
hjk committed
                                                    Core::Id("Git.Snapshot.").withSuffix(i),
                                                    globalcontext);
            gitContainer->addAction(tCommand);
con's avatar
con committed
    // Submit editor
    Core::Context submitContext(Constants::C_GITSUBMITEDITOR);
hjk's avatar
hjk committed
    m_submitCurrentAction = new QAction(VcsBase::VcsBaseSubmitEditor::submitIcon(), tr("Commit"), this);
Eike Ziller's avatar
Eike Ziller committed
    Core::Command *command = Core::ActionManager::registerAction(m_submitCurrentAction, Constants::SUBMIT_CURRENT, submitContext);
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
    connect(m_submitCurrentAction, SIGNAL(triggered()), this, SLOT(submitCurrentLog()));

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

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

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

    connect(Core::ICore::vcsManager(), SIGNAL(repositoryChanged(QString)),
            this, SLOT(updateContinueAndAbortCommands()));
    connect(Core::ICore::vcsManager(), SIGNAL(repositoryChanged(QString)),
            this, SLOT(updateBranches(QString)), Qt::QueuedConnection);
    if (!Core::ICore::mimeDatabase()->addMimeTypes(QLatin1String(RC_GIT_MIME_XML), errorMessage))
        return false;

Petar Perisin's avatar
Petar Perisin committed
    /* "Gerrit" */
    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
}

GitVersionControl *GitPlugin::gitVersionControl() const
    return static_cast<GitVersionControl *>(versionControl());
void GitPlugin::submitEditorDiff(const QStringList &unstaged, const QStringList &staged)
con's avatar
con committed
{
    m_gitClient->diff(m_submitRepository, QStringList(), unstaged, staged);
con's avatar
con committed
}

void GitPlugin::submitEditorMerge(const QStringList &unmerged)
{
    m_gitClient->merge(m_submitRepository, unmerged);
}

static bool ensureAllDocumentsSaved()
{
    bool cancelled;
    Core::DocumentManager::saveModifiedDocuments(Core::DocumentManager::modifiedDocuments(),
                                                 &cancelled);
    return !cancelled;
}

con's avatar
con committed
void GitPlugin::diffCurrentFile()
{
hjk's avatar
hjk committed
    const VcsBase::VcsBasePluginState state = currentState();
    QTC_ASSERT(state.hasFile(), return);
    m_gitClient->diff(state.currentFileTopLevel(), QStringList(), state.relativeCurrentFile());
con's avatar
con committed
}

void GitPlugin::diffCurrentProject()
{
hjk's avatar
hjk committed
    const VcsBase::VcsBasePluginState state = currentState();
    QTC_ASSERT(state.hasProject(), return);
    m_gitClient->diff(state.currentProjectTopLevel(), QStringList(), state.relativeCurrentProject());
con's avatar
con committed
}

void GitPlugin::diffRepository()
{
hjk's avatar
hjk committed
    const VcsBase::VcsBasePluginState state = currentState();
    QTC_ASSERT(state.hasTopLevel(), return);
    m_gitClient->diff(state.topLevel(), QStringList(), QStringList());
}

con's avatar
con committed
void GitPlugin::logFile()
{
hjk's avatar
hjk committed
    const VcsBase::VcsBasePluginState state = currentState();
    QTC_ASSERT(state.hasFile(), return);
    m_gitClient->log(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()), true);
con's avatar
con committed
}

void GitPlugin::blameFile()
{
hjk's avatar
hjk committed
    const VcsBase::VcsBasePluginState state = currentState();
    QTC_ASSERT(state.hasFile(), return);
hjk's avatar
hjk committed
    const int lineNumber = VcsBase::VcsBaseEditorWidget::lineNumberOfCurrentEditor(state.currentFile());
    m_gitClient->blame(state.currentFileTopLevel(), QStringList(), state.relativeCurrentFile(), QString(), lineNumber);
con's avatar
con committed
}

void GitPlugin::logProject()
{
hjk's avatar
hjk committed
    const VcsBase::VcsBasePluginState state = currentState();
    QTC_ASSERT(state.hasProject(), return);
    m_gitClient->log(state.currentProjectTopLevel(), state.relativeCurrentProject());
con's avatar
con committed
}

void GitPlugin::logRepository()
{
    const VcsBase::VcsBasePluginState state = currentState();
    QTC_ASSERT(state.hasTopLevel(), return);
    m_gitClient->log(state.topLevel());
}

void GitPlugin::undoFileChanges(bool revertStaging)
con's avatar
con committed
{
    if (!ensureAllDocumentsSaved())
        return;
hjk's avatar
hjk committed
    const VcsBase::VcsBasePluginState state = currentState();
    QTC_ASSERT(state.hasFile(), return);
    Core::FileChangeBlocker fcb(state.currentFile());
    m_gitClient->revert(QStringList(state.currentFile()), revertStaging);
}

void GitPlugin::undoUnstagedFileChanges()
{
    if (!ensureAllDocumentsSaved())
        return;
    undoFileChanges(false);
con's avatar
con committed
}

void GitPlugin::resetRepository()
con's avatar
con committed
{
    if (!ensureAllDocumentsSaved())
        return;
hjk's avatar
hjk committed
    const VcsBase::VcsBasePluginState state = currentState();
    QTC_ASSERT(state.hasTopLevel(), return);
    QString topLevel = state.topLevel();
    LogChangeDialog dialog(true);
    dialog.setWindowTitle(tr("Undo Changes to %1").arg(QDir::toNativeSeparators(topLevel)));
    if (dialog.runDialog(topLevel))
Orgad Shaneh's avatar
Orgad Shaneh committed
        m_gitClient->reset(topLevel, dialog.resetFlag(), dialog.commit());
con's avatar
con committed
}

Orgad Shaneh's avatar
Orgad Shaneh committed
void GitPlugin::startRebase()
{
    if (!ensureAllDocumentsSaved())
        return;
    const VcsBase::VcsBasePluginState state = currentState();
    QTC_ASSERT(state.hasTopLevel(), return);
    const QString topLevel = state.topLevel();
    if (topLevel.isEmpty() || !m_gitClient->canRebase(topLevel))
Orgad Shaneh's avatar
Orgad Shaneh committed
        return;
    if (!m_gitClient->beginStashScope(topLevel, QLatin1String("Rebase-i")))
Orgad Shaneh's avatar
Orgad Shaneh committed
        return;
    LogChangeDialog dialog(false);
    dialog.setWindowTitle(tr("Interactive Rebase"));
    if (dialog.runDialog(topLevel, QString(), false))
        m_gitClient->interactiveRebase(topLevel, dialog.commit(), false);
        m_gitClient->endStashScope(topLevel);
void GitPlugin::startChangeRelatedAction()
Orgad Shaneh's avatar
Orgad Shaneh committed
    const VcsBase::VcsBasePluginState state = currentState();
Orgad Shaneh's avatar
Orgad Shaneh committed
        return;
Orgad Shaneh's avatar
Orgad Shaneh committed
    ChangeSelectionDialog dialog(state.topLevel(), Core::ICore::mainWindow());
Orgad Shaneh's avatar
Orgad Shaneh committed
    int result = dialog.exec();
Orgad Shaneh's avatar
Orgad Shaneh committed
    if (result == QDialog::Rejected)
Orgad Shaneh's avatar
Orgad Shaneh committed
        return;
Orgad Shaneh's avatar
Orgad Shaneh committed
    const QString workingDirectory = dialog.workingDirectory();
    const QString change = dialog.change();
    if (workingDirectory.isEmpty() || change.isEmpty())
        return;

Orgad Shaneh's avatar
Orgad Shaneh committed
    if (dialog.command() == Show) {
        m_gitClient->show(workingDirectory, change);
Orgad Shaneh's avatar
Orgad Shaneh committed
        return;
    if (!ensureAllDocumentsSaved())
        return;
    bool (GitClient::*commandFunction)(const QString&, const QString&);
Orgad Shaneh's avatar
Orgad Shaneh committed
    switch (dialog.command()) {
    case CherryPick:
        commandFunction = &GitClient::synchronousCherryPick;
        break;
    case Revert:
        commandFunction = &GitClient::synchronousRevert;
    case Checkout:
        if (!m_gitClient->beginStashScope(workingDirectory, QLatin1String("Checkout")))
            return;
        commandFunction = &GitClient::synchronousCheckout;
        break;
    (m_gitClient->*commandFunction)(workingDirectory, change);
con's avatar
con committed
{
hjk's avatar
hjk committed
    const VcsBase::VcsBasePluginState state = currentState();
    QTC_ASSERT(state.hasFile(), return);
    m_gitClient->addFile(state.currentFileTopLevel(), state.relativeCurrentFile());
con's avatar
con committed
}

hjk's avatar
hjk committed
    const VcsBase::VcsBasePluginState state = currentState();
    QTC_ASSERT(state.hasFile(), return);
    m_gitClient->synchronousReset(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()));
Petar Perisin's avatar
Petar Perisin committed
void GitPlugin::gitkForCurrentFile()
{
    const VcsBase::VcsBasePluginState state = currentState();
    QTC_ASSERT(state.hasFile(), return);
    m_gitClient->launchGitK(state.currentFileTopLevel(), state.relativeCurrentFile());
}

void GitPlugin::gitkForCurrentFolder()
{
    const VcsBase::VcsBasePluginState state = currentState();
    QTC_ASSERT(state.hasFile(), return);

    /*
     *  entire lower part of the code can be easily replaced with one line:
     *
     *  m_gitClient->launchGitK(dir.currentFileDirectory(), QLatin1String("."));
     *
     *  However, there is a bug in gitk in version 1.7.9.5, and if you run above
     *  command, there will be no documents listed in lower right section.
     *
     *  This is why I use lower combination in order to avoid this problems in gitk.
     *
     *  Git version 1.7.10.4 does not have this issue, and it can easily use
     *  one line command mentioned above.
     *
     */
    QDir dir(state.currentFileDirectory());
    if (QFileInfo(dir,QLatin1String(".git")).exists() || dir.cd(QLatin1String(".git")))
        m_gitClient->launchGitK(state.currentFileDirectory());
    else {
        QString folderName = dir.absolutePath();
        dir.cdUp();
        folderName = folderName.remove(0, dir.absolutePath().length() + 1);
        m_gitClient->launchGitK(dir.absolutePath(), folderName);
    }
}

void GitPlugin::gitGui()
{
    const VcsBase::VcsBasePluginState state = currentState();
    QTC_ASSERT(state.hasTopLevel(), return);
    m_gitClient->launchGitGui(state.topLevel());
}

void GitPlugin::startAmendCommit()
{
    startCommit(AmendCommit);
void GitPlugin::startFixupCommit()
{
    startCommit(FixupCommit);
}

con's avatar
con committed
void GitPlugin::startCommit()
{
    startCommit(SimpleCommit);
void GitPlugin::startCommit(CommitType commitType)
    if (raiseSubmitEditor())
hjk's avatar
hjk committed
        VcsBase::VcsBaseOutputWindow::instance()->appendWarning(tr("Another submit is currently being executed."));
con's avatar
con committed
        return;
    }

hjk's avatar
hjk committed
    const VcsBase::VcsBasePluginState state = currentState();
    QTC_ASSERT(state.hasTopLevel(), return);
con's avatar
con committed

    QString errorMessage, commitTemplate;
    CommitData data(commitType);
    if (!m_gitClient->getCommitData(state.topLevel(), &commitTemplate, data, &errorMessage)) {
hjk's avatar
hjk committed
        VcsBase::VcsBaseOutputWindow::instance()->append(errorMessage);
con's avatar
con committed
        return;
    }

    // Store repository for diff and the original list of
    // files to be able to unstage files the user unchecks
con's avatar
con committed
    m_submitRepository = data.panelInfo.repository;

    // Start new temp file with message template
    Utils::TempFileSaver saver;
    // Keep the file alive, else it removes self and forgets its name
    saver.setAutoRemove(false);
    saver.write(commitTemplate.toLocal8Bit());
    if (!saver.finalize()) {
hjk's avatar
hjk committed
        VcsBase::VcsBaseOutputWindow::instance()->append(saver.errorString());
con's avatar
con committed
        return;
    }
    m_commitMessageFileName = saver.fileName();
    openSubmitEditor(m_commitMessageFileName, data);
con's avatar
con committed
}

Orgad Shaneh's avatar
Orgad Shaneh committed
void GitPlugin::updateVersionWarning()
{
    unsigned version = m_gitClient->gitVersion();
    if (!version || version >= minimumRequiredVersion)
Orgad Shaneh's avatar
Orgad Shaneh committed
        return;
    Core::IEditor *curEditor = Core::EditorManager::currentEditor();
    if (!curEditor)
        return;
    Core::IDocument *curDocument = curEditor->document();
    if (!curDocument)