Skip to content
Snippets Groups Projects
gitclient.cpp 23.2 KiB
Newer Older
con's avatar
con committed
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
con's avatar
con committed
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
con's avatar
con committed
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception
** version 1.2, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/
con's avatar
con committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 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 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
#include "gitclient.h"
#include "gitplugin.h"
#include "gitconstants.h"
#include "commitdata.h"

#include <coreplugin/icore.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/messagemanager.h>
#include <coreplugin/uniqueidmanager.h>
#include <coreplugin/actionmanager/actionmanagerinterface.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/progressmanager/progressmanagerinterface.h>
#include <vcsbase/vcsbaseeditor.h>
#include <texteditor/itexteditor.h>

#include <QtCore/QRegExp>
#include <QtCore/QTemporaryFile>
#include <QtCore/QFuture>

#include <QtGui/QErrorMessage>

using namespace Git;
using namespace Git::Internal;

const char* const kGitCommand = "git";
const char* const kGitDirectoryC = ".git";
const char* const kBranchIndicatorC = "# On branch";

static inline QString msgServerFailure()
{
    return GitClient::tr(
"Note that the git plugin for QtCreator is not able to interact with the server "
"so far. Thus, manual ssh-identification etc. will not work.");
}

inline Core::IEditor* locateEditor(const Core::ICore *core, const char *property, const QString &entry)
{
    foreach (Core::IEditor *ed, core->editorManager()->openedEditors())
        if (ed->property(property).toString() == entry)
            return ed;
    return 0;
}

GitClient::GitClient(GitPlugin* plugin, Core::ICore *core) :
    m_msgWait(tr("Waiting for data...")),
    m_plugin(plugin),
    m_core(core)
{
}

GitClient::~GitClient()
{
}

bool GitClient::vcsOpen(const QString &fileName)
{
    return m_plugin->vcsOpen(fileName);
}

QString GitClient::findRepositoryForFile(const QString &fileName)
{
    const QString gitDirectory = QLatin1String(kGitDirectoryC);
    const QFileInfo info(fileName);
    QDir dir = info.absoluteDir();
    do {
        if (dir.entryList(QDir::AllDirs|QDir::Hidden).contains(gitDirectory))
            return dir.absolutePath();
    } while (dir.cdUp());

    return QString();
}

QString GitClient::findRepositoryForDirectory(const QString &dir)
{
    const QString gitDirectory = QLatin1String(kGitDirectoryC);
    QDir directory(dir);
    do {
        if (directory.entryList(QDir::AllDirs|QDir::Hidden).contains(gitDirectory))
            return directory.absolutePath();
    } while (directory.cdUp());

    return QString();
}

// Return source file or directory string depending on parameters
// ('git diff XX' -> 'XX' , 'git diff XX file' -> 'XX/file').
static QString source(const QString &workingDirectory, const QString &fileName)
{
    if (fileName.isEmpty())
        return workingDirectory;
    QString rc = workingDirectory;
    if (!rc.isEmpty() && !rc.endsWith(QDir::separator()))
        rc += QDir::separator();
    rc += fileName;
    return rc;
}

/* Create an editor associated to VCS output of a source file/directory
 * (using the file's codec). Makes use of a dynamic property to find an
 * existing instance and to reuse it (in case, say, 'git diff foo' is
 * already open). */
VCSBase::VCSBaseEditor
    *GitClient::createVCSEditor(const QString &kind,
                                QString title,
                                // Source file or directory
                                const QString &source,
                                bool setSourceCodec,
                                // Dynamic property and value to identify that editor
                                const char *registerDynamicProperty,
                                const QString &dynamicPropertyValue) const
{
    VCSBase::VCSBaseEditor *rc = 0;
    Core::IEditor* outputEditor = locateEditor(m_core, registerDynamicProperty, dynamicPropertyValue);
    if (outputEditor) {
         // Exists already
        outputEditor->createNew(m_msgWait);
        rc = VCSBase::VCSBaseEditor::getVcsBaseEditor(outputEditor);
        Q_ASSERT(rc);
        m_core->editorManager()->setCurrentEditor(outputEditor);
    } else {
        // Create new, set wait message, set up with source and codec
        outputEditor = m_core->editorManager()->newFile(kind, &title, m_msgWait);
        outputEditor->setProperty(registerDynamicProperty, dynamicPropertyValue);
        rc = VCSBase::VCSBaseEditor::getVcsBaseEditor(outputEditor);
        Q_ASSERT(rc);
        rc->setSource(source);
        if (setSourceCodec)
            rc->setCodec(VCSBase::VCSBaseEditor::getCodec(m_core, source));
    }
    return rc;
}

void GitClient::diff(const QString &workingDirectory, const QStringList &fileNames)
{
      if (Git::Constants::debug)
        qDebug() << "diff" << workingDirectory << fileNames;
    QStringList arguments;
    arguments << QLatin1String("diff") << fileNames;

    const QString kind = QLatin1String(Git::Constants::GIT_DIFF_EDITOR_KIND);
    const QString title = tr("Git Diff");

    VCSBase::VCSBaseEditor *editor = createVCSEditor(kind, title, workingDirectory, true, "originalFileName", workingDirectory);
    executeGit(workingDirectory, arguments, m_plugin->m_outputWindow, editor);

}

void GitClient::diff(const QString &workingDirectory, const QString &fileName)
{
    if (Git::Constants::debug)
        qDebug() << "diff" << workingDirectory << fileName;
    QStringList arguments;
    arguments << QLatin1String("diff");
    if (!fileName.isEmpty())
            arguments << fileName;

    const QString kind = QLatin1String(Git::Constants::GIT_DIFF_EDITOR_KIND);
    const QString title = tr("Git Diff %1").arg(fileName);
    const QString sourceFile = source(workingDirectory, fileName);

    VCSBase::VCSBaseEditor *editor = createVCSEditor(kind, title, sourceFile, true, "originalFileName", sourceFile);
    executeGit(workingDirectory, arguments, m_plugin->m_outputWindow, editor);
}

void GitClient::status(const QString &workingDirectory)
{
    executeGit(workingDirectory, QStringList(QLatin1String("status")), m_plugin->m_outputWindow, 0,true);
}

void GitClient::log(const QString &workingDirectory, const QString &fileName)
{
    if (Git::Constants::debug)
        qDebug() << "log" << workingDirectory << fileName;
    QStringList arguments;
    int logCount = 10;
    if (m_plugin->m_settingsPage && m_plugin->m_settingsPage->logCount() > 0)
        logCount = m_plugin->m_settingsPage->logCount();

    arguments << QLatin1String("log") << QLatin1String("-n")
        << QString::number(logCount);
    if (!fileName.isEmpty())
        arguments << fileName;

    const QString title = tr("Git Log %1").arg(fileName);
    const QString kind = QLatin1String(Git::Constants::GIT_LOG_EDITOR_KIND);
    const QString sourceFile = source(workingDirectory, fileName);
    VCSBase::VCSBaseEditor *editor = createVCSEditor(kind, title, sourceFile, false, "logFileName", sourceFile);
    executeGit(workingDirectory, arguments, m_plugin->m_outputWindow, editor);
}

void GitClient::show(const QString &source, const QString &id)
{
    if (Git::Constants::debug)
        qDebug() << "show" << source << id;
    QStringList arguments(QLatin1String("show"));
    arguments << id;

    const QString title =  tr("Git Show %1").arg(id);
    const QString kind = QLatin1String(Git::Constants::GIT_DIFF_EDITOR_KIND);
    VCSBase::VCSBaseEditor *editor = createVCSEditor(kind, title, source, true, "show", id);

    const QFileInfo sourceFi(source);
    const QString workDir = sourceFi.isDir() ? sourceFi.absoluteFilePath() : sourceFi.absolutePath();
    executeGit(workDir, arguments, m_plugin->m_outputWindow, editor);
}

void GitClient::blame(const QString &workingDirectory, const QString &fileName)
{
    if (Git::Constants::debug)
        qDebug() << "blame" << workingDirectory << fileName;
    QStringList arguments(QLatin1String("blame"));
    arguments << fileName;

    const QString kind = QLatin1String(Git::Constants::GIT_BLAME_EDITOR_KIND);
    const QString title = tr("Git Blame %1").arg(fileName);
    const QString sourceFile = source(workingDirectory, fileName);

    VCSBase::VCSBaseEditor *editor = createVCSEditor(kind, title, sourceFile, true, "blameFileName", sourceFile);
    executeGit(workingDirectory, arguments, m_plugin->m_outputWindow, editor);
}

void GitClient::checkout(const QString &workingDirectory, const QString &fileName)
{
    // Passing an empty argument as the file name is very dangereous, since this makes
    // git checkout apply to all files. Almost looks like a bug in git.
    if (fileName.isEmpty())
        return;

    QStringList arguments;
    arguments << QLatin1String("checkout") << QLatin1String("HEAD") << QLatin1String("--")
            << fileName;

    executeGit(workingDirectory, arguments, m_plugin->m_outputWindow, 0,true);
}

void GitClient::hardReset(const QString &workingDirectory, const QString &commit)
{
    QStringList arguments;
    arguments << QLatin1String("reset") << QLatin1String("--hard");
    if (!commit.isEmpty())
        arguments << commit;

    executeGit(workingDirectory, arguments, m_plugin->m_outputWindow, 0,true);
}

void GitClient::addFile(const QString &workingDirectory, const QString &fileName)
{
    QStringList arguments;
    arguments << QLatin1String("add") << fileName;

    executeGit(workingDirectory, arguments, m_plugin->m_outputWindow, 0,true);
}

bool GitClient::synchronousAdd(const QString &workingDirectory, const QStringList &files)
{
    QByteArray outputText;
    QByteArray errorText;
    QStringList arguments;
    arguments << "add" << files;
    const bool rc = synchronousGit(workingDirectory, arguments, &outputText, &errorText);
    if (!rc) {
        const QString errorMessage = tr("Unable to add %n file(s) to %1: %2", 0, files.size()).
                                     arg(workingDirectory, QString::fromLocal8Bit(errorText));
        m_plugin->m_outputWindow->append(errorMessage);
        m_plugin->m_outputWindow->popup(false);
    }
    return rc;
}

void GitClient::executeGit(const QString &workingDirectory, const QStringList &arguments,
                           GitOutputWindow *outputWindow, VCSBase::VCSBaseEditor* editor,
                           bool outputToWindow)
{
    if (Git::Constants::debug)
        qDebug() << "executeGit" << workingDirectory << arguments << editor;
    outputWindow->clearContents();

    QProcess process;
    ProjectExplorer::Environment environment = ProjectExplorer::Environment::systemEnvironment();

    if (m_plugin->m_settingsPage && !m_plugin->m_settingsPage->adoptEnvironment())
        environment.set(QLatin1String("PATH"), m_plugin->m_settingsPage->path());

    GitCommand* command = new GitCommand();
    if (outputToWindow) {
        Q_ASSERT(outputWindow);
        connect(command, SIGNAL(outputText(QString)), outputWindow, SLOT(append(QString)));
        connect(command, SIGNAL(outputData(QByteArray)), outputWindow, SLOT(appendData(QByteArray)));
    } else {
        Q_ASSERT(editor);
        connect(command, SIGNAL(outputText(QString)), editor, SLOT(setPlainText(QString)));
        connect(command, SIGNAL(outputData(QByteArray)), editor, SLOT(setPlainTextData(QByteArray)));
    }

    if (outputWindow)
        connect(command, SIGNAL(errorText(QString)), outputWindow, SLOT(append(QString)));

    command->execute(arguments, workingDirectory, environment);
}

bool GitClient::synchronousGit(const QString &workingDirectory
                                , const QStringList &arguments
                                , QByteArray* outputText
                                , QByteArray* errorText)
{
    if (Git::Constants::debug)
        qDebug() << "synchronousGit" << workingDirectory << arguments;
    QProcess process;

    process.setWorkingDirectory(workingDirectory);

    ProjectExplorer::Environment environment = ProjectExplorer::Environment::systemEnvironment();
    if (m_plugin->m_settingsPage && !m_plugin->m_settingsPage->adoptEnvironment())
        environment.set(QLatin1String("PATH"), m_plugin->m_settingsPage->path());
    process.setEnvironment(environment.toStringList());

    process.start(QLatin1String(kGitCommand), arguments);
    if (!process.waitForFinished()) {
        if (errorText)
            *errorText = "Error: Git timed out";
        return false;
    }

    if (outputText)
        *outputText = process.readAllStandardOutput();

    if (errorText)
        *errorText = process.readAllStandardError();

    if (Git::Constants::debug)
        qDebug() << "synchronousGit ex=" << process.exitCode();
    return process.exitCode() == 0;
}

/* Parse a git status file list:
 * \code
    # Changes to be committed:
    #<tab>modified:<blanks>git.pro
    # Changed but not updated:
    #<tab>modified:<blanks>git.pro
    # Untracked files:
    #<tab>modified:<blanks>git.pro
    \endcode
*/
static bool parseFiles(const QStringList &lines, CommitData *d)
{
    enum State { None, CommitFiles, NotUpdatedFiles, UntrackedFiles };

    const QString branchIndicator = QLatin1String(kBranchIndicatorC);
    const QString commitIndicator = QLatin1String("# Changes to be committed:");
    const QString notUpdatedIndicator = QLatin1String("# Changed but not updated:");
    const QString untrackedIndicator = QLatin1String("# Untracked files:");

    State s = None;

    const QRegExp filesPattern(QLatin1String("#\\t[^:]+:\\s+[^ ]+"));
    Q_ASSERT(filesPattern.isValid());

    const QStringList::const_iterator cend = lines.constEnd();
    for (QStringList::const_iterator it =  lines.constBegin(); it != cend; ++it) {
        const QString line = *it;
        if (line.startsWith(branchIndicator)) {
            d->panelInfo.branch = line.mid(branchIndicator.size() + 1);
        } else {
            if (line.startsWith(commitIndicator)) {
                s = CommitFiles;
            } else {
                if (line.startsWith(notUpdatedIndicator)) {
                    s = NotUpdatedFiles;
                } else {
                    if (line.startsWith(untrackedIndicator)) {
                        s = UntrackedFiles;
                    } else {
                        if (filesPattern.exactMatch(line)) {
                            const QString fileSpec = line.mid(2).simplified();
                            switch (s) {
                            case CommitFiles:
                                d->commitFiles.push_back(fileSpec);
                            break;
                            case NotUpdatedFiles:
                                d->notUpdatedFiles.push_back(fileSpec);
                                break;
                            case UntrackedFiles:
                                d->untrackedFiles.push_back(fileSpec);
                                break;
                            case None:
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    return !d->commitFiles.empty() || !d->notUpdatedFiles.empty() || !d->untrackedFiles.empty();
}

bool GitClient::getCommitData(const QString &workingDirectory,
                              QString *commitTemplate,
                              CommitData *d,
                              QString *errorMessage)
{
    d->clear();

    // Find repo
    const QString repoDirectory = GitClient::findRepositoryForDirectory(workingDirectory);
    if (repoDirectory.isEmpty()) {
        *errorMessage = tr("Unable to determine the repository for %1.").arg(workingDirectory);
        return false;
    }

    d->panelInfo.repository = repoDirectory;

    QDir gitDir(repoDirectory);
    if (!gitDir.cd(QLatin1String(kGitDirectoryC))) {
        *errorMessage = tr("The repository %1 is not initialized yet.").arg(repoDirectory);
        return false;
    }

    // Read description
    const QString descriptionFile = gitDir.absoluteFilePath(QLatin1String("description"));
    if (QFileInfo(descriptionFile).isFile()) {
        QFile file(descriptionFile);
        if (file.open(QIODevice::ReadOnly|QIODevice::Text))
            d->panelInfo.description = QString::fromLocal8Bit(file.readAll()).trimmed();
    }

    // Run status. Note that it has exitcode 1 if there are no added files.
    QByteArray outputText;
    QByteArray errorText;
    const bool statusRc = synchronousGit(workingDirectory, QStringList(QLatin1String("status")), &outputText, &errorText);
    if (!statusRc) {
        // Something fatal
        if (!outputText.contains(kBranchIndicatorC)) {
            *errorMessage = tr("Unable to obtain the project status: %1").arg(QString::fromLocal8Bit(errorText));
            return false;
        }
        // All unchanged
        if (outputText.contains("nothing to commit")) {
            *errorMessage = tr("There are no modified files.");
            return false;
        }
    }

    //    Output looks like:
    //    # On branch [branchname]
    //    # Changes to be committed:
    //    #   (use "git reset HEAD <file>..." to unstage)
    //    #
    //    #       modified:   somefile.cpp
    //    #       new File:   somenew.h
    //    #
    //    # Changed but not updated:
    //    #   (use "git add <file>..." to update what will be committed)
    //    #
    //    #       modified:   someother.cpp
    //    #
    //    # Untracked files:
    //    #   (use "git add <file>..." to include in what will be committed)
    //    #
    //    #       list of files...

    const QStringList lines = QString::fromLocal8Bit(outputText).remove(QLatin1Char('\r')).split(QLatin1Char('\n'));
    if (!parseFiles(lines, d)) {
        *errorMessage = tr("Unable to parse the file output.");
        return false;
    }

    d->panelData.author = readConfigValue(workingDirectory, QLatin1String("user.name"));
    d->panelData.email = readConfigValue(workingDirectory, QLatin1String("user.email"));

    // Get the commit template
    const QString templateFilename = readConfigValue(workingDirectory, QLatin1String("commit.template"));
    if (!templateFilename.isEmpty()) {
        QFile templateFile(templateFilename);
        if (templateFile.open(QIODevice::ReadOnly|QIODevice::Text)) {
            *commitTemplate = QString::fromLocal8Bit(templateFile.readAll());
        } else {
            qWarning("Unable to read commit template %s: %s",
                     qPrintable(templateFilename),
                     qPrintable(templateFile.errorString()));
        }
    }
    return true;
}

bool GitClient::addAndCommit(const QString &workingDirectory,
                             const GitSubmitEditorPanelData &data,
                             const QString &messageFile,
                             const QStringList &files)
{
    // Re-add all to make sure we have the latest changes
    if (!synchronousAdd(workingDirectory, files))
        return false;

    // Do the final commit
    QStringList args;
    args << QLatin1String("commit")
         << QLatin1String("-F") << QDir::toNativeSeparators(messageFile)
         << QLatin1String("--author") << data.authorString();

    QByteArray outputText;
    QByteArray errorText;
    const bool rc = synchronousGit(workingDirectory, args, &outputText, &errorText);
    const QString message = rc ?
        tr("Committed %n file(s).", 0, files.size()) :
        tr("Unable to commit %n file(s): %1", 0, files.size()).arg(QString::fromLocal8Bit(errorText));

    m_plugin->m_outputWindow->append(message);
    m_plugin->m_outputWindow->popup(false);
    return rc;
}

void GitClient::pull(const QString &workingDirectory)
{
    executeGit(workingDirectory, QStringList(QLatin1String("pull")), m_plugin->m_outputWindow, 0,true);
}

void GitClient::push(const QString &workingDirectory)
{
    executeGit(workingDirectory, QStringList(QLatin1String("push")), m_plugin->m_outputWindow, 0,true);
}

QString GitClient::readConfig(const QString &workingDirectory, const QStringList &configVar)
{
    QStringList arguments;
    arguments << QLatin1String("config") << configVar;

    QByteArray outputText;
    if (synchronousGit(workingDirectory, arguments, &outputText))
        return outputText;
    return QString();
}

// Read a single-line config value, return trimmed
QString GitClient::readConfigValue(const QString &workingDirectory, const QString &configVar)
{
    return readConfig(workingDirectory, QStringList(configVar)).remove(QLatin1Char('\n'));
}

GitCommand::GitCommand()
{
}

GitCommand::~GitCommand()
{
}

void GitCommand::execute(const QStringList &arguments
                 , const QString &workingDirectory
                 , const ProjectExplorer::Environment &environment)
{
    if (Git::Constants::debug)
        qDebug() << "GitCommand::execute" << workingDirectory << arguments;

    // For some reason QtConcurrent::run() only works on this
    QFuture<void> task = QtConcurrent::run(this, &GitCommand::run
                                           , arguments
                                           , workingDirectory
                                           , environment);
    QString taskName = QLatin1String("Git ") + arguments[0];

    Core::ICore *core = ExtensionSystem::PluginManager::instance()->getObject<Core::ICore>();
    core->progressManager()->addTask(task, taskName
                            , QLatin1String("Git.action")
                            , Core::ProgressManagerInterface::CloseOnSuccess);
}

void GitCommand::run(const QStringList &arguments
                 , const QString &workingDirectory
                 , const ProjectExplorer::Environment &environment)
{
    if (Git::Constants::debug)
        qDebug() << "GitCommand::run" << workingDirectory << arguments;
    QProcess process;
    if (!workingDirectory.isEmpty())
        process.setWorkingDirectory(workingDirectory);

    ProjectExplorer::Environment env = environment;
    if (env.toStringList().isEmpty())
        env = ProjectExplorer::Environment::systemEnvironment();
    process.setEnvironment(env.toStringList());

    process.start(QLatin1String(kGitCommand), arguments);
    if (!process.waitForFinished()) {
        emit errorText(QLatin1String("Error: Git timed out"));
        return;
    }

    const QByteArray output = process.readAllStandardOutput();
    if (output.isEmpty()) {
        if (arguments.at(0) == QLatin1String("diff"))
            emit outputText(tr("The file does not differ from HEAD"));
    } else {
        emit outputData(output);
    }
    const QByteArray error = process.readAllStandardError();
    if (!error.isEmpty())
        emit errorText(QString::fromLocal8Bit(error));

    // As it is used asynchronously, we need to delete ourselves
    this->deleteLater();
}