gitplugin.cpp 30.6 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** 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.
24
**
25
26
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
con's avatar
con committed
27
**
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"
con's avatar
con committed
40
41
42
43
44
45

#include <coreplugin/icore.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/filemanager.h>
#include <coreplugin/messagemanager.h>
#include <coreplugin/uniqueidmanager.h>
46
#include <coreplugin/actionmanager/actionmanager.h>
con's avatar
con committed
47
#include <coreplugin/editormanager/editormanager.h>
hjk's avatar
hjk committed
48
49
50

#include <utils/qtcassert.h>

con's avatar
con committed
51
52
53
54
55
#include <vcsbase/basevcseditorfactory.h>
#include <vcsbase/vcsbaseeditor.h>
#include <vcsbase/basevcssubmiteditorfactory.h>

#include <QtCore/QDebug>
hjk's avatar
hjk committed
56
#include <QtCore/QDir>
con's avatar
con committed
57
58
#include <QtCore/QFileInfo>
#include <QtCore/QTemporaryFile>
59
#include <QtCore/QtPlugin>
hjk's avatar
hjk committed
60

con's avatar
con committed
61
#include <QtGui/QAction>
hjk's avatar
hjk committed
62
63
#include <QtGui/QFileDialog>
#include <QtGui/QMainWindow>
con's avatar
con committed
64
65
66
67
68
69
70
#include <QtGui/QMenu>
#include <QtGui/QMessageBox>

static const VCSBase::VCSBaseEditorParameters editorParameters[] = {
{
    VCSBase::RegularCommandOutput,
    Git::Constants::GIT_COMMAND_LOG_EDITOR_KIND,
71
    Git::Constants::C_GIT_COMMAND_LOG_EDITOR,
con's avatar
con committed
72
73
74
75
    "application/vnd.nokia.text.scs_git_commandlog",
    "gitlog"},
{   VCSBase::LogOutput,
    Git::Constants::GIT_LOG_EDITOR_KIND,
76
    Git::Constants::C_GIT_LOG_EDITOR,
con's avatar
con committed
77
78
79
80
    "application/vnd.nokia.text.scs_git_filelog",
    "gitfilelog"},
{   VCSBase::AnnotateOutput,
    Git::Constants::GIT_BLAME_EDITOR_KIND,
81
    Git::Constants::C_GIT_BLAME_EDITOR,
con's avatar
con committed
82
83
84
85
    "application/vnd.nokia.text.scs_git_annotation",
    "gitsannotate"},
{   VCSBase::DiffOutput,
    Git::Constants::GIT_DIFF_EDITOR_KIND,
86
    Git::Constants::C_GIT_DIFF_EDITOR,
con's avatar
con committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
    "text/x-patch","diff"}
};

// Utility to find a parameter set by type
static inline const VCSBase::VCSBaseEditorParameters *findType(int ie)
{
    const VCSBase::EditorContentType et = static_cast<VCSBase::EditorContentType>(ie);
    return  VCSBase::VCSBaseEditor::findType(editorParameters, sizeof(editorParameters)/sizeof(VCSBase::VCSBaseEditorParameters), et);
}

using namespace Git;
using namespace Git::Internal;

// CoreListener

bool CoreListener::editorAboutToClose(Core::IEditor *editor)
{
    return m_plugin->editorAboutToClose(editor);
}

// GitPlugin

GitPlugin *GitPlugin::m_instance = 0;

GitPlugin::GitPlugin() :
    m_core(0),
    m_diffAction(0),
    m_diffProjectAction(0),
    m_statusAction(0),
    m_statusProjectAction(0),
    m_logAction(0),
    m_blameAction(0),
    m_logProjectAction(0),
    m_undoFileAction(0),
    m_undoProjectAction(0),
    m_showAction(0),
123
124
125
    m_stageAction(0),
    m_unstageAction(0),
    m_revertAction(0),
con's avatar
con committed
126
127
128
129
130
131
132
    m_commitAction(0),
    m_pullAction(0),
    m_pushAction(0),
    m_submitCurrentAction(0),
    m_diffSelectedFilesAction(0),
    m_undoAction(0),
    m_redoAction(0),
133
134
135
136
    m_stashAction(0),
    m_stashPopAction(0),
    m_stashListAction(0),
    m_branchListAction(0),
con's avatar
con committed
137
138
139
140
141
142
143
    m_projectExplorer(0),
    m_gitClient(0),
    m_outputWindow(0),
    m_changeSelectionDialog(0),
    m_settingsPage(0),
    m_coreListener(0),
    m_submitEditorFactory(0),
144
    m_versionControl(0),
con's avatar
con committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    m_changeTmpFile(0)
{
    m_instance = this;
}

GitPlugin::~GitPlugin()
{
    if (m_outputWindow) {
        removeObject(m_outputWindow);
        delete m_outputWindow;
        m_outputWindow = 0;
    }

    if (m_settingsPage) {
        removeObject(m_settingsPage);
        delete m_settingsPage;
        m_settingsPage = 0;
    }

    if (!m_editorFactories.empty()) {
hjk's avatar
hjk committed
165
        foreach (Core::IEditorFactory* pf, m_editorFactories)
con's avatar
con committed
166
167
168
169
170
171
172
173
174
175
176
177
            removeObject(pf);
        qDeleteAll(m_editorFactories);
    }

    if (m_coreListener) {
        removeObject(m_coreListener);
        delete m_coreListener;
        m_coreListener = 0;
    }

    if (m_submitEditorFactory) {
        removeObject(m_submitEditorFactory);
dt's avatar
dt committed
178
        delete m_submitEditorFactory;
con's avatar
con committed
179
180
181
        m_submitEditorFactory = 0;
    }

182
183
184
185
186
187
    if (m_versionControl) {
        removeObject(m_versionControl);
        delete m_versionControl;
        m_versionControl = 0;
    }

con's avatar
con committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
    cleanChangeTmpFile();
    delete m_gitClient;
    m_instance = 0;
}

void GitPlugin::cleanChangeTmpFile()
{
    if (m_changeTmpFile) {
        delete m_changeTmpFile;
        m_changeTmpFile = 0;
    }
}

GitPlugin *GitPlugin::instance()
{
    return m_instance;
}

static const VCSBase::VCSBaseSubmitEditorParameters submitParameters = {
    Git::Constants::SUBMIT_MIMETYPE,
    Git::Constants::GITSUBMITEDITOR_KIND,
209
    Git::Constants::C_GITSUBMITEDITOR
con's avatar
con committed
210
211
};

con's avatar
con committed
212
static Core::Command *createSeparator(Core::ActionManager *am,
hjk's avatar
hjk committed
213
214
215
                                       const QList<int> &context,
                                       const QString &id,
                                       QObject *parent)
216
217
218
219
220
{
    QAction *a = new QAction(parent);
    a->setSeparator(true);
    return  am->registerAction(a, id, context);
}
con's avatar
con committed
221
222
223
224
225
226
227
228
229

bool GitPlugin::initialize(const QStringList &arguments, QString *error_message)
{
    typedef VCSBase::VCSEditorFactory<GitEditor> GitEditorFactory;
    typedef VCSBase::VCSSubmitEditorFactory<GitSubmitEditor> GitSubmitEditorFactory;

    Q_UNUSED(arguments);
    Q_UNUSED(error_message);

230
    m_core = Core::ICore::instance();
231
    m_gitClient = new GitClient(this);
con's avatar
con committed
232
233
    // Create the globalcontext list to register actions accordingly
    QList<int> globalcontext;
234
    globalcontext << m_core->uniqueIDManager()->uniqueIdentifier(Core::Constants::C_GLOBAL);
con's avatar
con committed
235
236
237
238
239
240
241
242
243
244
245
246

    // Create the output Window
    m_outputWindow = new GitOutputWindow();
    addObject(m_outputWindow);

    // Create the settings Page
    m_settingsPage = new SettingsPage();
    addObject(m_settingsPage);

    static const char *describeSlot = SLOT(show(QString,QString));
    const int editorCount = sizeof(editorParameters)/sizeof(VCSBase::VCSBaseEditorParameters);
    for (int i = 0; i < editorCount; i++) {
247
        m_editorFactories.push_back(new GitEditorFactory(editorParameters + i, m_gitClient, describeSlot));
con's avatar
con committed
248
249
250
251
252
253
254
255
256
        addObject(m_editorFactories.back());
    }

    m_coreListener = new CoreListener(this);
    addObject(m_coreListener);

    m_submitEditorFactory = new GitSubmitEditorFactory(&submitParameters);
    addObject(m_submitEditorFactory);

257
258
259
    m_versionControl = new GitVersionControl(m_gitClient);
    addObject(m_versionControl);

con's avatar
con committed
260
    //register actions
261
    Core::ActionManager *actionManager = m_core->actionManager();
con's avatar
con committed
262

263
    Core::ActionContainer *toolsContainer =
con's avatar
con committed
264
265
        actionManager->actionContainer(Core::Constants::M_TOOLS);

266
    Core::ActionContainer *gitContainer =
con's avatar
con committed
267
268
269
        actionManager->createMenu(QLatin1String("Git"));
    gitContainer->menu()->setTitle(tr("&Git"));
    toolsContainer->addMenu(gitContainer);
270
271
272
273
    if (QAction *ma = gitContainer->menu()->menuAction()) {
        ma->setEnabled(m_versionControl->isEnabled());
        connect(m_versionControl, SIGNAL(enabledChanged(bool)), ma, SLOT(setVisible(bool)));
    }
con's avatar
con committed
274

con's avatar
con committed
275
    Core::Command *command;
con's avatar
con committed
276
277
278

    m_diffAction = new QAction(tr("Diff current file"), this);
    command = actionManager->registerAction(m_diffAction, "Git.Diff", globalcontext);
con's avatar
con committed
279
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
280
281
282
283
284
285
286
    command->setDefaultKeySequence(QKeySequence(tr("Alt+G,Alt+D")));
    connect(m_diffAction, SIGNAL(triggered()), this, SLOT(diffCurrentFile()));
    gitContainer->addAction(command);

    m_statusAction = new QAction(tr("File Status"), this);
    command = actionManager->registerAction(m_statusAction, "Git.Status", globalcontext);
    command->setDefaultKeySequence(QKeySequence(tr("Alt+G,Alt+S")));
con's avatar
con committed
287
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
288
289
290
291
292
293
    connect(m_statusAction, SIGNAL(triggered()), this, SLOT(statusFile()));
    gitContainer->addAction(command);

    m_logAction = new QAction(tr("Log File"), this);
    command = actionManager->registerAction(m_logAction, "Git.Log", globalcontext);
    command->setDefaultKeySequence(QKeySequence(tr("Alt+G,Alt+L")));
con's avatar
con committed
294
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
295
296
297
298
299
300
    connect(m_logAction, SIGNAL(triggered()), this, SLOT(logFile()));
    gitContainer->addAction(command);

    m_blameAction = new QAction(tr("Blame"), this);
    command = actionManager->registerAction(m_blameAction, "Git.Blame", globalcontext);
    command->setDefaultKeySequence(QKeySequence(tr("Alt+G,Alt+B")));
con's avatar
con committed
301
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
302
303
304
305
306
307
    connect(m_blameAction, SIGNAL(triggered()), this, SLOT(blameFile()));
    gitContainer->addAction(command);

    m_undoFileAction = new QAction(tr("Undo Changes"), this);
    command = actionManager->registerAction(m_undoFileAction, "Git.Undo", globalcontext);
    command->setDefaultKeySequence(QKeySequence(tr("Alt+G,Alt+U")));
con's avatar
con committed
308
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
309
310
311
    connect(m_undoFileAction, SIGNAL(triggered()), this, SLOT(undoFileChanges()));
    gitContainer->addAction(command);

312
313
    m_stageAction = new QAction(tr("Stage file for commit"), this);
    command = actionManager->registerAction(m_stageAction, "Git.Stage", globalcontext);
con's avatar
con committed
314
    command->setDefaultKeySequence(QKeySequence(tr("Alt+G,Alt+A")));
con's avatar
con committed
315
    command->setAttribute(Core::Command::CA_UpdateText);
316
317
318
319
320
    connect(m_stageAction, SIGNAL(triggered()), this, SLOT(stageFile()));
    gitContainer->addAction(command);

    m_unstageAction = new QAction(tr("Unstage file from commit"), this);
    command = actionManager->registerAction(m_unstageAction, "Git.Unstage", globalcontext);
con's avatar
con committed
321
    command->setAttribute(Core::Command::CA_UpdateText);
322
323
324
325
326
    connect(m_unstageAction, SIGNAL(triggered()), this, SLOT(unstageFile()));
    gitContainer->addAction(command);

    m_revertAction = new QAction(tr("Revert..."), this);
    command = actionManager->registerAction(m_revertAction, "Git.Revert", globalcontext);
con's avatar
con committed
327
    command->setAttribute(Core::Command::CA_UpdateText);
328
    connect(m_revertAction, SIGNAL(triggered()), this, SLOT(revertFile()));
con's avatar
con committed
329
330
    gitContainer->addAction(command);

331
    gitContainer->addAction(createSeparator(actionManager, globalcontext, QLatin1String("Git.Sep.Project"), this));
con's avatar
con committed
332
333
334
335

    m_diffProjectAction = new QAction(tr("Diff current project"), this);
    command = actionManager->registerAction(m_diffProjectAction, "Git.DiffProject", globalcontext);
    command->setDefaultKeySequence(QKeySequence("Alt+G,Alt+Shift+D"));
con's avatar
con committed
336
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
337
338
339
340
341
    connect(m_diffProjectAction, SIGNAL(triggered()), this, SLOT(diffCurrentProject()));
    gitContainer->addAction(command);

    m_statusProjectAction = new QAction(tr("Project status"), this);
    command = actionManager->registerAction(m_statusProjectAction, "Git.StatusProject", globalcontext);
con's avatar
con committed
342
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
343
344
345
346
347
348
    connect(m_statusProjectAction, SIGNAL(triggered()), this, SLOT(statusProject()));
    gitContainer->addAction(command);

    m_logProjectAction = new QAction(tr("Log project"), this);
    command = actionManager->registerAction(m_logProjectAction, "Git.LogProject", globalcontext);
    command->setDefaultKeySequence(QKeySequence(tr("Alt+G,Alt+K")));
con's avatar
con committed
349
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
350
351
352
353
354
    connect(m_logProjectAction, SIGNAL(triggered()), this, SLOT(logProject()));
    gitContainer->addAction(command);

    m_undoProjectAction = new QAction(tr("Undo Project Changes"), this);
    command = actionManager->registerAction(m_undoProjectAction, "Git.UndoProject", globalcontext);
con's avatar
con committed
355
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
356
357
358
    connect(m_undoProjectAction, SIGNAL(triggered()), this, SLOT(undoProjectChanges()));
    gitContainer->addAction(command);

359
360
361
362
363
    gitContainer->addAction(createSeparator(actionManager, globalcontext, QLatin1String("Git.Sep.Global"), this));

    m_stashAction = new QAction(tr("Stash"), this);
    m_stashAction->setToolTip("Saves the current state of your work.");
    command = actionManager->registerAction(m_stashAction, "Git.Stash", globalcontext);
con's avatar
con committed
364
    command->setAttribute(Core::Command::CA_UpdateText);
365
    connect(m_stashAction, SIGNAL(triggered()), this, SLOT(stash()));
con's avatar
con committed
366
367
    gitContainer->addAction(command);

368
369
    m_pullAction = new QAction(tr("Pull"), this);
    command = actionManager->registerAction(m_pullAction, "Git.Pull", globalcontext);
con's avatar
con committed
370
    command->setAttribute(Core::Command::CA_UpdateText);
371
372
373
374
375
376
    connect(m_pullAction, SIGNAL(triggered()), this, SLOT(pull()));
    gitContainer->addAction(command);

    m_stashPopAction = new QAction(tr("Stash pop"), this);
    m_stashAction->setToolTip("Restores changes saved to the stash list using \"Stash\".");
    command = actionManager->registerAction(m_stashPopAction, "Git.StashPop", globalcontext);
con's avatar
con committed
377
    command->setAttribute(Core::Command::CA_UpdateText);
378
    connect(m_stashPopAction, SIGNAL(triggered()), this, SLOT(stashPop()));
con's avatar
con committed
379
380
381
382
383
    gitContainer->addAction(command);

    m_commitAction = new QAction(tr("Commit..."), this);
    command = actionManager->registerAction(m_commitAction, "Git.Commit", globalcontext);
    command->setDefaultKeySequence(QKeySequence(tr("Alt+G,Alt+C")));
con's avatar
con committed
384
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
385
386
387
388
389
    connect(m_commitAction, SIGNAL(triggered()), this, SLOT(startCommit()));
    gitContainer->addAction(command);

    m_pushAction = new QAction(tr("Push"), this);
    command = actionManager->registerAction(m_pushAction, "Git.Push", globalcontext);
con's avatar
con committed
390
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
391
392
393
    connect(m_pushAction, SIGNAL(triggered()), this, SLOT(push()));
    gitContainer->addAction(command);

394
395
    gitContainer->addAction(createSeparator(actionManager, globalcontext, QLatin1String("Git.Sep.Branch"), this));

396
    m_branchListAction = new QAction(tr("Branches..."), this);
397
    command = actionManager->registerAction(m_branchListAction, "Git.BranchList", globalcontext);
con's avatar
con committed
398
    command->setAttribute(Core::Command::CA_UpdateText);
399
400
401
402
403
    connect(m_branchListAction, SIGNAL(triggered()), this, SLOT(branchList()));
    gitContainer->addAction(command);

    m_stashListAction = new QAction(tr("List stashes"), this);
    command = actionManager->registerAction(m_stashListAction, "Git.StashList", globalcontext);
con's avatar
con committed
404
    command->setAttribute(Core::Command::CA_UpdateText);
405
406
407
408
409
    connect(m_stashListAction, SIGNAL(triggered()), this, SLOT(stashList()));
    gitContainer->addAction(command);

    m_showAction = new QAction(tr("Show commit..."), this);
    command = actionManager->registerAction(m_showAction, "Git.ShowCommit", globalcontext);
con's avatar
con committed
410
    command->setAttribute(Core::Command::CA_UpdateText);
411
412
413
    connect(m_showAction, SIGNAL(triggered()), this, SLOT(showCommit()));
    gitContainer->addAction(command);

con's avatar
con committed
414
415
416
    // Submit editor
    QList<int> submitContext;
    submitContext.push_back(m_core->uniqueIDManager()->uniqueIdentifier(QLatin1String(Constants::C_GITSUBMITEDITOR)));
417
    m_submitCurrentAction = new QAction(VCSBase::VCSBaseSubmitEditor::submitIcon(), tr("Commit"), this);
con's avatar
con committed
418
419
420
    command = actionManager->registerAction(m_submitCurrentAction, Constants::SUBMIT_CURRENT, submitContext);
    connect(m_submitCurrentAction, SIGNAL(triggered()), this, SLOT(submitCurrentLog()));

421
    m_diffSelectedFilesAction = new QAction(VCSBase::VCSBaseSubmitEditor::diffIcon(), tr("Diff Selected Files"), this);
con's avatar
con committed
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
    command = actionManager->registerAction(m_diffSelectedFilesAction, Constants::DIFF_SELECTED, submitContext);

    m_undoAction = new QAction(tr("&Undo"), this);
    command = actionManager->registerAction(m_undoAction, Core::Constants::UNDO, submitContext);

    m_redoAction = new QAction(tr("&Redo"), this);
    command = actionManager->registerAction(m_redoAction, Core::Constants::REDO, submitContext);

    // Ask for updates of our actions, in case context switches
    connect(m_core, SIGNAL(contextChanged(Core::IContext *)),
        this, SLOT(updateActions()));
    connect(m_core->fileManager(), SIGNAL(currentFileChanged(const QString &)),
        this, SLOT(updateActions()));

    return true;
}

void GitPlugin::extensionsInitialized()
{
441
    m_projectExplorer = ProjectExplorer::ProjectExplorerPlugin::instance();
con's avatar
con committed
442
443
}

444
void GitPlugin::submitEditorDiff(const QStringList &unstaged, const QStringList &staged)
con's avatar
con committed
445
{
446
    m_gitClient->diff(m_submitRepository, QStringList(), unstaged, staged);
con's avatar
con committed
447
448
449
450
}

void GitPlugin::diffCurrentFile()
{
451
452
453
454
    const QFileInfo fileInfo = currentFile();
    const QString fileName = fileInfo.fileName();
    const QString workingDirectory = fileInfo.absolutePath();
    m_gitClient->diff(workingDirectory, QStringList(), fileName);
con's avatar
con committed
455
456
457
458
459
460
461
}

void GitPlugin::diffCurrentProject()
{
    QString workingDirectory = getWorkingDirectory();
    if (workingDirectory.isEmpty())
        return;
462
    m_gitClient->diff(workingDirectory, QStringList(), QString());
con's avatar
con committed
463
464
}

465
QFileInfo GitPlugin::currentFile() const
con's avatar
con committed
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
{
    QString fileName = m_core->fileManager()->currentFile();
    QFileInfo fileInfo(fileName);
    return fileInfo;
}

QString GitPlugin::getWorkingDirectory()
{
    QString workingDirectory;
    if (m_projectExplorer && m_projectExplorer->currentNode()) {
        workingDirectory = QFileInfo(m_projectExplorer->currentNode()->path()).absolutePath();
    }
    if (Git::Constants::debug > 1)
        qDebug() << Q_FUNC_INFO << "Project" << workingDirectory;

    if (workingDirectory.isEmpty())
        workingDirectory = QFileInfo(m_core->fileManager()->currentFile()).absolutePath();
    if (Git::Constants::debug > 1)
        qDebug() << Q_FUNC_INFO << "file" << workingDirectory;

    if (workingDirectory.isEmpty()) {
        m_outputWindow->append(tr("Could not find working directory"));
488
        m_outputWindow->popup(false);
con's avatar
con committed
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
        return QString();
    }
    return workingDirectory;
}

void GitPlugin::statusProject()
{
    QString workingDirectory = getWorkingDirectory();
    if (workingDirectory.isEmpty())
        return;
    m_gitClient->status(workingDirectory);
}

void GitPlugin::statusFile()
{
    m_gitClient->status(currentFile().absolutePath());
}

void GitPlugin::logFile()
{
    const QFileInfo fileInfo = currentFile();
    const QString fileName = fileInfo.fileName();
    const QString workingDirectory = fileInfo.absolutePath();
    m_gitClient->log(workingDirectory, fileName);
}

void GitPlugin::blameFile()
{
    const QFileInfo fileInfo = currentFile();
    const QString fileName = fileInfo.fileName();
    const QString workingDirectory = fileInfo.absolutePath();
    m_gitClient->blame(workingDirectory, fileName);
}

void GitPlugin::logProject()
{
    QString workingDirectory = getWorkingDirectory();
    if (workingDirectory.isEmpty())
        return;
    m_gitClient->log(workingDirectory, QString());
}

void GitPlugin::undoFileChanges()
{
    QFileInfo fileInfo = currentFile();
    QString fileName = fileInfo.fileName();
    QString workingDirectory = fileInfo.absolutePath();
    m_gitClient->checkout(workingDirectory, fileName);
}

void GitPlugin::undoProjectChanges()
{
    QString workingDirectory = getWorkingDirectory();
    if (workingDirectory.isEmpty())
        return;
    m_gitClient->hardReset(workingDirectory, QString());
}

547
void GitPlugin::stageFile()
con's avatar
con committed
548
{
549
550
551
    const QFileInfo fileInfo = currentFile();
    const QString fileName = fileInfo.fileName();
    const QString workingDirectory = fileInfo.absolutePath();
con's avatar
con committed
552
553
554
    m_gitClient->addFile(workingDirectory, fileName);
}

555
556
557
558
559
560
561
562
563
564
565
566
567
568
void GitPlugin::unstageFile()
{
    const QFileInfo fileInfo = currentFile();
    const QString fileName = fileInfo.fileName();
    const QString workingDirectory = fileInfo.absolutePath();
    m_gitClient->synchronousReset(workingDirectory, QStringList(fileName));
}

void GitPlugin::revertFile()
{
    const QFileInfo fileInfo = currentFile();
    m_gitClient->revert(QStringList(fileInfo.absoluteFilePath()));
}

con's avatar
con committed
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
void GitPlugin::startCommit()
{
    if (m_changeTmpFile) {
        m_outputWindow->append(tr("Another submit is currently beeing executed."));
        m_outputWindow->popup(false);
        return;
    }

    // Find repository and get commit data
    const QFileInfo currentFileInfo = currentFile();
    if (!currentFileInfo.exists())
        return;

    const QString workingDirectory = currentFileInfo.absolutePath();
    QString errorMessage, commitTemplate;
    CommitData data;
    if (!m_gitClient->getCommitData(workingDirectory, &commitTemplate, &data, &errorMessage)) {
        m_outputWindow->append(errorMessage);
        m_outputWindow->popup(false);
        return;
    }

591
592
    // Store repository for diff and the original list of
    // files to be able to unstage files the user unchecks
con's avatar
con committed
593
    m_submitRepository = data.panelInfo.repository;
594
    m_submitOrigCommitFiles = data.stagedFileNames();
con's avatar
con committed
595
596
597
598
599
600
601
602
603

    if (Git::Constants::debug)
        qDebug() << Q_FUNC_INFO << data << commitTemplate;

    // Start new temp file with message template
    QTemporaryFile *changeTmpFile = new QTemporaryFile(this);
    changeTmpFile->setAutoRemove(true);
    if (!changeTmpFile->open()) {
        m_outputWindow->append(tr("Cannot create temporary file: %1").arg(changeTmpFile->errorString()));
604
        m_outputWindow->popup(false);
con's avatar
con committed
605
606
607
608
609
610
611
612
613
614
615
616
617
618
        delete changeTmpFile;
        return;
    }
    m_changeTmpFile = changeTmpFile;
    m_changeTmpFile->write(commitTemplate.toLocal8Bit());
    m_changeTmpFile->flush();
    // Keep the file alive, else it removes self and forgets
    // its name
    m_changeTmpFile->seek(0);
    openSubmitEditor(m_changeTmpFile->fileName(), data);
}

Core::IEditor *GitPlugin::openSubmitEditor(const QString &fileName, const CommitData &cd)
{
619
    Core::IEditor *editor = m_core->editorManager()->openEditor(fileName, QLatin1String(Constants::GITSUBMITEDITOR_KIND));
con's avatar
con committed
620
621
622
623
    if (Git::Constants::debug)
        qDebug() << Q_FUNC_INFO << fileName << editor;
    m_core->editorManager()->ensureEditorManagerVisible();
    GitSubmitEditor *submitEditor = qobject_cast<GitSubmitEditor*>(editor);
hjk's avatar
hjk committed
624
    QTC_ASSERT(submitEditor, return 0);
con's avatar
con committed
625
626
    // The actions are for some reason enabled by the context switching
    // mechanism. Disable them correctly.
627
    submitEditor->registerActions(m_undoAction, m_redoAction, m_submitCurrentAction, m_diffSelectedFilesAction);
con's avatar
con committed
628
    submitEditor->setCommitData(cd);
629
    connect(submitEditor, SIGNAL(diff(QStringList,QStringList)), this, SLOT(submitEditorDiff(QStringList,QStringList)));
con's avatar
con committed
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
    return editor;
}

void GitPlugin::submitCurrentLog()
{
    // Close the submit editor
    QList<Core::IEditor*> editors;
    editors.push_back(m_core->editorManager()->currentEditor());
    m_core->editorManager()->closeEditors(editors);
}

bool GitPlugin::editorAboutToClose(Core::IEditor *iEditor)
{
    // Closing a submit editor?
    if (!m_changeTmpFile || !iEditor || qstrcmp(iEditor->kind(), Constants::GITSUBMITEDITOR_KIND))
        return true;
    Core::IFile *fileIFace = iEditor->file();
    const GitSubmitEditor *editor = qobject_cast<GitSubmitEditor *>(iEditor);
    if (!fileIFace || !editor)
        return true;
    // Submit editor closing. Make it write out the commit message
    // and retrieve files
    const QFileInfo editorFile(fileIFace->fileName());
    const QFileInfo changeFile(m_changeTmpFile->fileName());
    // Paranoia!
    if (editorFile.absoluteFilePath() != changeFile.absoluteFilePath())
        return true;
    // Prompt user.
    const QMessageBox::StandardButton answer = QMessageBox::question(m_core->mainWindow(), tr("Closing git editor"),  tr("Do you want to commit the change?"),
                                                                     QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel, QMessageBox::Yes);
    switch (answer) {
    case QMessageBox::Cancel:
        return false; // Keep editing and change file
    case QMessageBox::No:
        cleanChangeTmpFile();
        return true; // Cancel all
    default:
        break;
    }
    // Go ahead!
    const QStringList fileList = editor->checkedFiles();
    if (Git::Constants::debug)
        qDebug() << Q_FUNC_INFO << fileList;
    if (!fileList.empty()) {
        // get message & commit
        m_core->fileManager()->blockFileChange(fileIFace);
        fileIFace->save();
        m_core->fileManager()->unblockFileChange(fileIFace);

        m_gitClient->addAndCommit(m_submitRepository,
                                  editor->panelData(),
                                  m_changeTmpFile->fileName(),
682
683
                                  fileList,
                                  m_submitOrigCommitFiles);
con's avatar
con committed
684
685
686
687
688
689
690
    }
    cleanChangeTmpFile();
    return true;
}

void GitPlugin::pull()
{
691
    const QString workingDirectory = getWorkingDirectory();
692
693
694
695
696
697
698
699
700
701
702
    if (workingDirectory.isEmpty())
        return;

    switch (m_gitClient->ensureStash(workingDirectory)) {
        case GitClient::StashUnchanged:
        case GitClient::Stashed:
        case GitClient::NotStashed:
            m_gitClient->pull(workingDirectory);
        default:
        break;
    }
con's avatar
con committed
703
704
705
706
}

void GitPlugin::push()
{
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
    const QString workingDirectory = getWorkingDirectory();
    if (!workingDirectory.isEmpty())
        m_gitClient->push(workingDirectory);
}

void GitPlugin::stash()
{
    const QString workingDirectory = getWorkingDirectory();
    if (!workingDirectory.isEmpty())
        m_gitClient->stash(workingDirectory);
}

void GitPlugin::stashPop()
{
    const QString workingDirectory = getWorkingDirectory();
    if (!workingDirectory.isEmpty())
        m_gitClient->stashPop(workingDirectory);
}

void GitPlugin::branchList()
{
    const QString workingDirectory = getWorkingDirectory();
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
    if (workingDirectory.isEmpty())
        return;
#ifndef USE_BRANCHDIALOG
    QString errorMessage;
    BranchDialog dialog(m_core->mainWindow());

    if (!dialog.init(m_gitClient, workingDirectory, &errorMessage)) {
        m_outputWindow->append(errorMessage);
        m_outputWindow->popup(false);
        return;
    }
    dialog.exec();
#else
    m_gitClient->branchList(workingDirectory);
#endif
744
745
746
747
748
749
750
}

void GitPlugin::stashList()
{
    const QString workingDirectory = getWorkingDirectory();
    if (!workingDirectory.isEmpty())
        m_gitClient->stashList(workingDirectory);
con's avatar
con committed
751
752
753
754
}

void GitPlugin::updateActions()
{
755
    const QFileInfo current = currentFile();
con's avatar
con committed
756
757
    const QString fileName = current.fileName();
    const QString currentDirectory = getWorkingDirectory();
758
    const QString repository = m_gitClient->findRepositoryForFile(current.absoluteFilePath());
con's avatar
con committed
759
760
    // First check for file commands and if the current file is inside
    // a Git-repository
761
762
763
764
765
766
767
768
    m_diffAction->setText(tr("Diff \"%1\"").arg(fileName));
    m_statusAction->setText(tr("Status related to \"%1\"").arg(fileName));
    m_logAction->setText(tr("Log \"%1\"").arg(fileName));
    m_blameAction->setText(tr("Blame \"%1\"").arg(fileName));
    m_undoFileAction->setText(tr("Undo changes for \"%1\"").arg(fileName));
    m_stageAction->setText(tr("Stage \"%1\" for commit").arg(fileName));
    m_unstageAction->setText(tr("Unstage \"%1\" from commit").arg(fileName));
    m_revertAction->setText(tr("Revert \"%1\"...").arg(fileName));
con's avatar
con committed
769
770
771
772
773
774
775
776
    if (repository.isEmpty()) {
        // If the file is not in a repository, the corresponding project will
        // be neither and we can disable everything and return
        m_diffAction->setEnabled(false);
        m_statusAction->setEnabled(false);
        m_logAction->setEnabled(false);
        m_blameAction->setEnabled(false);
        m_undoFileAction->setEnabled(false);
777
778
779
        m_stageAction->setEnabled(false);
        m_unstageAction->setEnabled(false);
        m_revertAction->setEnabled(false);
con's avatar
con committed
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
        m_diffProjectAction->setEnabled(false);
        m_diffProjectAction->setText(tr("Diff Project"));
        m_statusProjectAction->setText(tr("Status Project"));
        m_statusProjectAction->setEnabled(false);
        m_logProjectAction->setText(tr("Log Project"));
        m_logProjectAction->setEnabled(false);
        return;
    } else {
        // We only know the file is in some repository, we do not know
        // anything about any project so far.
        m_diffAction->setEnabled(true);
        m_statusAction->setEnabled(true);
        m_logAction->setEnabled(true);
        m_blameAction->setEnabled(true);
        m_undoFileAction->setEnabled(true);
795
796
797
        m_stageAction->setEnabled(true);
        m_unstageAction->setEnabled(true);
        m_revertAction->setEnabled(true);
con's avatar
con committed
798
799
800
801
    }

    if (m_projectExplorer && m_projectExplorer->currentNode()
        && m_projectExplorer->currentNode()->projectNode()) {
802
        const QString name = QFileInfo(m_projectExplorer->currentNode()->projectNode()->path()).baseName();
con's avatar
con committed
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
        m_diffProjectAction->setEnabled(true);
        m_diffProjectAction->setText(tr("Diff Project %1").arg(name));
        m_statusProjectAction->setEnabled(true);
        m_statusProjectAction->setText(tr("Status Project %1").arg(name));
        m_logProjectAction->setEnabled(true);
        m_logProjectAction->setText(tr("Log Project %1").arg(name));
    } else {
        m_diffProjectAction->setEnabled(false);
        m_diffProjectAction->setText(tr("Diff Project"));
        m_statusProjectAction->setEnabled(false);
        m_statusProjectAction->setText(tr("Status Project"));
        m_logProjectAction->setEnabled(false);
        m_logProjectAction->setText(tr("Log Project"));
    }
}

void GitPlugin::showCommit()
{
    if (!m_changeSelectionDialog)
        m_changeSelectionDialog = new ChangeSelectionDialog();

    const QFileInfo currentInfo = currentFile();
    QString repositoryLocation = m_gitClient->findRepositoryForFile(currentInfo.absoluteFilePath());
    if (!repositoryLocation.isEmpty())
        m_changeSelectionDialog->m_ui.repositoryEdit->setText(repositoryLocation);

    if (m_changeSelectionDialog->exec() != QDialog::Accepted)
        return;
    const QString change = m_changeSelectionDialog->m_ui.changeNumberEdit->text();
    if (change .isEmpty())
        return;

    m_gitClient->show(m_changeSelectionDialog->m_ui.repositoryEdit->text(), change);
}

838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
GitOutputWindow *GitPlugin::outputWindow() const
{
    return m_outputWindow;
}

GitSettings GitPlugin::settings() const
{
    return m_gitClient->settings();
}

void GitPlugin::setSettings(const GitSettings &s)
{
    m_gitClient->setSettings(s);
}

con's avatar
con committed
853
Q_EXPORT_PLUGIN(GitPlugin)