Skip to content
Snippets Groups Projects
gitclient.cpp 93.4 KiB
Newer Older
// Split a line of "<commit> <parent1> ..." to obtain parents from "rev-list" or "log".
static inline bool splitCommitParents(const QString &line,
                                      QString *commit = 0,
                                      QStringList *parents = 0)
{
    if (commit)
        commit->clear();
    if (parents)
        parents->clear();
    QStringList tokens = line.trimmed().split(QLatin1Char(' '));
    if (tokens.size() < 2)
        return false;
    if (commit)
        *commit = tokens.front();
    tokens.pop_front();
    if (parents)
        *parents = tokens;
    return true;
}

// Find out the immediate parent revisions of a revision of the repository.
// Might be several in case of merges.
bool GitClient::synchronousParentRevisions(const QString &workingDirectory,
                                           const QStringList &files /* = QStringList() */,
                                           const QString &revision,
                                           QStringList *parents,
                                           QString *errorMessage)
{
    QByteArray outputTextData;
    QByteArray errorText;
    QStringList arguments;
    arguments << QLatin1String("rev-list") << QLatin1String(GitClient::noColorOption)
              << QLatin1String("--parents") << QLatin1String("--max-count=1") << revision;
    if (!files.isEmpty()) {
        arguments.append(QLatin1String("--"));
        arguments.append(files);
    }
    const bool rc = fullySynchronousGit(workingDirectory, arguments, &outputTextData, &errorText);
        *errorMessage = msgParentRevisionFailed(workingDirectory, revision, commandOutputFromLocal8Bit(errorText));
        return false;
    }
    // Should result in one line of blank-delimited revisions, specifying current first
    // unless it is top.
    QString outputText = commandOutputFromLocal8Bit(outputTextData);
    outputText.remove(QLatin1Char('\n'));
    if (!splitCommitParents(outputText, 0, parents)) {
        *errorMessage = msgParentRevisionFailed(workingDirectory, revision, msgInvalidRevision());
        return false;
    }
    return true;
}

// Short SHA1, author, subject
static const char defaultShortLogFormatC[] = "%h (%an \"%s\")";

bool GitClient::synchronousShortDescription(const QString &workingDirectory, const QString &revision,
Tobias Hunger's avatar
Tobias Hunger committed
                                            QString *description, QString *errorMessage)
{
    // Short SHA 1, author, subject
    return synchronousShortDescription(workingDirectory, revision,
Tobias Hunger's avatar
Tobias Hunger committed
                                       QLatin1String(defaultShortLogFormatC),
                                       description, errorMessage);
}

// Convenience working on a list of revisions
bool GitClient::synchronousShortDescriptions(const QString &workingDirectory, const QStringList &revisions,
Tobias Hunger's avatar
Tobias Hunger committed
                                             QStringList *descriptions, QString *errorMessage)
{
    descriptions->clear();
    foreach (const QString &revision, revisions) {
        QString description;
        if (!synchronousShortDescription(workingDirectory, revision, &description, errorMessage)) {
            descriptions->clear();
            return false;
        }
        descriptions->push_back(description);
    }
    return true;
}

static inline QString msgCannotDetermineBranch(const QString &workingDirectory, const QString &why)
{
Tobias Hunger's avatar
Tobias Hunger committed
    return GitClient::tr("Cannot retrieve branch of \"%1\": %2").arg(QDir::toNativeSeparators(workingDirectory), why);
}

// Retrieve head revision/branch
Tobias Hunger's avatar
Tobias Hunger committed
bool GitClient::synchronousTopRevision(const QString &workingDirectory, QString *revision,
                                       QString *branch, QString *errorMessageIn)
{
    QByteArray outputTextData;
    QByteArray errorText;
    QStringList arguments;
    QString errorMessage;
    do {
        // get revision
        if (revision) {
            revision->clear();
            arguments << QLatin1String("log") << QLatin1String(noColorOption)
                    <<  QLatin1String("--max-count=1") << QLatin1String("--pretty=format:%H");
            if (!fullySynchronousGit(workingDirectory, arguments, &outputTextData, &errorText)) {
Tobias Hunger's avatar
Tobias Hunger committed
                errorMessage = tr("Cannot retrieve top revision of \"%1\": %2").arg(QDir::toNativeSeparators(workingDirectory), commandOutputFromLocal8Bit(errorText));
                break;
            }
            *revision = commandOutputFromLocal8Bit(outputTextData);
            revision->remove(QLatin1Char('\n'));
        } // revision desired
        // get branch
        if (branch) {
            branch->clear();
            arguments.clear();
            arguments << QLatin1String("branch") << QLatin1String(noColorOption);
            if (!fullySynchronousGit(workingDirectory, arguments, &outputTextData, &errorText)) {
                errorMessage = msgCannotDetermineBranch(workingDirectory, commandOutputFromLocal8Bit(errorText));
                break;
            }
            /* parse output for current branch: \code
* master
  branch2
\endcode */
            const QString branchPrefix = QLatin1String("* ");
            foreach(const QString &line, commandOutputLinesFromLocal8Bit(outputTextData)) {
                if (line.startsWith(branchPrefix)) {
                    *branch = line;
                    branch->remove(0, branchPrefix.size());
                    break;
                }
            }
            if (branch->isEmpty()) {
                errorMessage = msgCannotDetermineBranch(workingDirectory,
                                                        QString::fromLatin1("Internal error: Failed to parse output: %1").arg(commandOutputFromLocal8Bit(outputTextData)));
                break;
            }
        } // branch
    } while (false);
    const bool failed = (revision && revision->isEmpty()) || (branch && branch->isEmpty());
    if (failed && !errorMessage.isEmpty()) {
        if (errorMessageIn) {
            *errorMessageIn = errorMessage;
        } else {
            outputWindow()->appendError(errorMessage);
// Format an entry in a one-liner for selection list using git log.
Tobias Hunger's avatar
Tobias Hunger committed
bool GitClient::synchronousShortDescription(const QString &workingDirectory, const QString &revision,
                                            const QString &format, QString *description,
                                            QString *errorMessage)
{
    QByteArray outputTextData;
    QByteArray errorText;
    QStringList arguments;
    arguments << QLatin1String("log") << QLatin1String(GitClient::noColorOption)
              << (QLatin1String("--pretty=format:") + format)
              << QLatin1String("--max-count=1") << revision;
    const bool rc = fullySynchronousGit(workingDirectory, arguments, &outputTextData, &errorText);
Tobias Hunger's avatar
Tobias Hunger committed
        *errorMessage = tr("Cannot describe revision \"%1\" in \"%2\": %3").arg(revision, workingDirectory, commandOutputFromLocal8Bit(errorText));
    *description = commandOutputFromLocal8Bit(outputTextData);
    if (description->endsWith(QLatin1Char('\n')))
        description->truncate(description->size() - 1);
    return true;
}

// Create a default message to be used for describing stashes
static inline QString creatorStashMessage(const QString &keyword = QString())
{
    QString rc = QCoreApplication::applicationName();
    rc += QLatin1Char(' ');
    if (!keyword.isEmpty()) {
        rc += keyword;
        rc += QLatin1Char(' ');
    }
    rc += QDateTime::currentDateTime().toString(Qt::ISODate);
    return rc;
}

/* Do a stash and return the message as identifier. Note that stash names (stash{n})
 * shift as they are pushed, so, enforce the use of messages to identify them. Flags:
 * StashPromptDescription: Prompt the user for a description message.
 * StashImmediateRestore: Immediately re-apply this stash (used for snapshots), user keeps on working
 * StashIgnoreUnchanged: Be quiet about unchanged repositories (used for IVersionControl's snapshots). */

Tobias Hunger's avatar
Tobias Hunger committed
QString GitClient::synchronousStash(const QString &workingDirectory, const QString &messageKeyword,
                                    unsigned flags, bool *unchanged)
{
    if (unchanged)
        *unchanged = false;
    QString message;
    bool success = false;
    // Check for changes and stash
    QString errorMessage;
    switch (gitStatus(workingDirectory, false, 0, &errorMessage)) {
    case  StatusChanged: {
            message = creatorStashMessage(messageKeyword);
            do {
                if ((flags & StashPromptDescription)) {
                    if (!inputText(Core::ICore::instance()->mainWindow(),
                                   tr("Stash Description"), tr("Description:"), &message))
                        break;
                }
                if (!executeSynchronousStash(workingDirectory, message))
                    break;
                if ((flags & StashImmediateRestore)
                    && !synchronousStashRestore(workingDirectory, QLatin1String("stash@{0}")))
                    break;
                success = true;
            } while (false);
        }
        break;
    case StatusUnchanged:
        if (unchanged)
            *unchanged = true;
        if (!(flags & StashIgnoreUnchanged))
            outputWindow()->append(msgNoChangedFiles());
        break;
    case StatusFailed:
        outputWindow()->append(errorMessage);
        break;
    }
    if (!success)
        message.clear();
    return message;
}

bool GitClient::executeSynchronousStash(const QString &workingDirectory,
Tobias Hunger's avatar
Tobias Hunger committed
                                        const QString &message,
                                        QString *errorMessage)
{
    QByteArray outputText;
    QByteArray errorText;
    QStringList arguments;
    arguments << QLatin1String("stash");
    if (!message.isEmpty())
        arguments << QLatin1String("save") << message;
    const bool rc = fullySynchronousGit(workingDirectory, arguments, &outputText, &errorText);
Tobias Hunger's avatar
Tobias Hunger committed
        const QString msg = tr("Cannot stash in \"%1\": %2").
                            arg(QDir::toNativeSeparators(workingDirectory),
                                commandOutputFromLocal8Bit(errorText));
        if (errorMessage) {
            *errorMessage = msg;
        } else {
            outputWindow()->append(msg);
// Resolve a stash name from message
bool GitClient::stashNameFromMessage(const QString &workingDirectory,
                                     const QString &message, QString *name,
Tobias Hunger's avatar
Tobias Hunger committed
                                     QString *errorMessage)
{
    // All happy
    if (message.startsWith(QLatin1String(stashNamePrefix))) {
        *name = message;
        return true;
    }
    // Retrieve list and find via message
    QList<Stash> stashes;
    if (!synchronousStashList(workingDirectory, &stashes, errorMessage))
        return false;
    foreach (const Stash &s, stashes) {
        if (s.message == message) {
            *name = s.name;
            return true;
        }
    }
Friedemann Kleint's avatar
Friedemann Kleint committed
    //: Look-up of a stash via its descriptive message failed.
Tobias Hunger's avatar
Tobias Hunger committed
    const QString msg = tr("Cannot resolve stash message \"%1\" in \"%2\".").arg(message, workingDirectory);
    if (errorMessage) {
        *errorMessage = msg;
    } else {
        outputWindow()->append(msg);
bool GitClient::synchronousBranchCmd(const QString &workingDirectory, QStringList branchArgs,
                                     QString *output, QString *errorMessage)
{
    branchArgs.push_front(QLatin1String("branch"));
    QByteArray outputText;
    QByteArray errorText;
    const bool rc = fullySynchronousGit(workingDirectory, branchArgs, &outputText, &errorText);
Tobias Hunger's avatar
Tobias Hunger committed
        *errorMessage = tr("Cannot run \"git branch\" in \"%1\": %2").arg(QDir::toNativeSeparators(workingDirectory), commandOutputFromLocal8Bit(errorText));
    *output = commandOutputFromLocal8Bit(outputText);
bool GitClient::synchronousRemoteCmd(const QString &workingDirectory, QStringList remoteArgs,
                                     QString *output, QString *errorMessage)
{
    remoteArgs.push_front(QLatin1String("remote"));
    QByteArray outputText;
    QByteArray errorText;
    const bool rc = fullySynchronousGit(workingDirectory, remoteArgs, &outputText, &errorText);
    if (!rc) {
Tobias Hunger's avatar
Tobias Hunger committed
        *errorMessage = tr("Cannot run \"git remote\" in \"%1\": %2").arg(QDir::toNativeSeparators(workingDirectory), commandOutputFromLocal8Bit(errorText));
        return false;
    }
    *output = commandOutputFromLocal8Bit(outputText);
    return true;
}

bool GitClient::synchronousShow(const QString &workingDirectory, const QString &id,
                                 QString *output, QString *errorMessage)
{
    if (!canShow(id)) {
        *errorMessage = msgCannotShow(id);
        return false;
    }
    QStringList args(QLatin1String("show"));
    args << QLatin1String(decorateOption) << QLatin1String(noColorOption) << id;
    QByteArray outputText;
    QByteArray errorText;
    const bool rc = fullySynchronousGit(workingDirectory, args, &outputText, &errorText);
Tobias Hunger's avatar
Tobias Hunger committed
        *errorMessage = tr("Cannot run \"git show\" in \"%1\": %2").arg(QDir::toNativeSeparators(workingDirectory), commandOutputFromLocal8Bit(errorText));
    *output = commandOutputFromLocal8Bit(outputText);
// Retrieve list of files to be cleaned
bool GitClient::synchronousCleanList(const QString &workingDirectory,
                                     QStringList *files, QString *errorMessage)
{
    files->clear();
    QStringList args;
    args << QLatin1String("clean") << QLatin1String("--dry-run") << QLatin1String("-dxf");
    QByteArray outputText;
    QByteArray errorText;
    const bool rc = fullySynchronousGit(workingDirectory, args, &outputText, &errorText);
Tobias Hunger's avatar
Tobias Hunger committed
        *errorMessage = tr("Cannot run \"git clean\" in \"%1\": %2").arg(QDir::toNativeSeparators(workingDirectory), commandOutputFromLocal8Bit(errorText));
        return false;
    }
    // Filter files that git would remove
    const QString prefix = QLatin1String("Would remove ");
    foreach(const QString &line, commandOutputLinesFromLocal8Bit(outputText))
        if (line.startsWith(prefix))
            files->push_back(line.mid(prefix.size()));
    return true;
}

bool GitClient::synchronousApplyPatch(const QString &workingDirectory,
                                      const QString &file, QString *errorMessage)
{
    QStringList args;
    args << QLatin1String("apply") << QLatin1String("--whitespace=fix") << file;
    QByteArray outputText;
    QByteArray errorText;
    const bool rc = fullySynchronousGit(workingDirectory, args, &outputText, &errorText);
    if (rc) {
        if (!errorText.isEmpty())
Tobias Hunger's avatar
Tobias Hunger committed
            *errorMessage = tr("There were warnings while applying \"%1\" to \"%2\":\n%3").arg(file, workingDirectory, commandOutputFromLocal8Bit(errorText));
Tobias Hunger's avatar
Tobias Hunger committed
        *errorMessage = tr("Cannot apply patch \"%1\" to \"%2\": %3").arg(file, workingDirectory, commandOutputFromLocal8Bit(errorText));
// Factory function to create an asynchronous command
GitCommand *GitClient::createCommand(const QString &workingDirectory,
Tobias Hunger's avatar
Tobias Hunger committed
                                     VCSBase::VCSBaseEditorWidget* editor,
                                     bool outputToWindow,
                                     int editorLineNumber)
con's avatar
con committed
{
    VCSBase::VCSBaseOutputWindow *outputWindow = VCSBase::VCSBaseOutputWindow::instance();
    GitCommand* command = new GitCommand(binary(), workingDirectory, processEnvironment(), QVariant(editorLineNumber));
    if (editor)
        connect(command, SIGNAL(finished(bool,int,QVariant)), editor, SLOT(commandFinishedGotoLine(bool,int,QVariant)));
con's avatar
con committed
    if (outputToWindow) {
        if (editor) { // assume that the commands output is the important thing
            connect(command, SIGNAL(outputData(QByteArray)), outputWindow, SLOT(appendDataSilently(QByteArray)));
        } else {
            connect(command, SIGNAL(outputData(QByteArray)), outputWindow, SLOT(appendData(QByteArray)));
        }
con's avatar
con committed
    } else {
        connect(command, SIGNAL(outputData(QByteArray)), editor, SLOT(setPlainTextDataFiltered(QByteArray)));
con's avatar
con committed
    }

    if (outputWindow)
        connect(command, SIGNAL(errorText(QString)), outputWindow, SLOT(appendError(QString)));
con's avatar
con committed

GitCommand *GitClient::executeGit(const QString &workingDirectory,
                                  const QStringList &arguments,
                                  VCSBase::VCSBaseEditorWidget* editor,
                                  bool outputToWindow,
                                  GitCommand::TerminationReportMode tm,
                                  int editorLineNumber,
                                  bool unixTerminalDisabled)
    outputWindow()->appendCommand(workingDirectory, QLatin1String(Constants::GIT_BINARY), arguments);
    GitCommand *command = createCommand(workingDirectory, editor, outputToWindow, editorLineNumber);
    command->addJob(arguments, m_settings.timeoutSeconds);
    command->setTerminationReportMode(tm);
    command->setUnixTerminalDisabled(unixTerminalDisabled);
con's avatar
con committed
}

// Return fixed arguments required to run
QString GitClient::binary() const
    return m_binaryPath;
// Determine a value for the HOME variable on Windows
// working around MSys git not finding it when run outside git bash
// (then looking for the SSH keys under "\program files\git").

QString GitClient::fakeWinHome(const QProcessEnvironment &e)
{
    const QString homeDrive = e.value("HOMEDRIVE");
    const QString homePath = e.value("HOMEPATH");
    QTC_ASSERT(!homeDrive.isEmpty() && !homePath.isEmpty(), return QString())
    return homeDrive + homePath;
}

QProcessEnvironment GitClient::processEnvironment() const
    QProcessEnvironment environment = QProcessEnvironment::systemEnvironment();
        environment.insert(QLatin1String("PATH"), m_settings.path);
#ifdef Q_OS_WIN
    if (m_settings.winSetHomeEnvironment) {
        const QString home = fakeWinHome(environment);
        environment.insert(QLatin1String("HOME"), home);
    }
#endif // Q_OS_WIN
    // Set up SSH and C locale (required by git using perl).
    VCSBase::VCSBasePlugin::setProcessEnvironment(&environment, false);
// Synchronous git execution using Utils::SynchronousProcess, with
// log windows updating.
Tobias Hunger's avatar
Tobias Hunger committed
Utils::SynchronousProcessResponse GitClient::synchronousGit(const QString &workingDirectory,
                                                            const QStringList &gitArguments,
                                                            unsigned flags,
                                                            QTextCodec *stdOutCodec)
    return VCSBase::VCSBasePlugin::runVCS(workingDirectory, binary(), gitArguments,
                                          m_settings.timeoutSeconds * 1000,
                                          processEnvironment(),
                                          flags, stdOutCodec);
}

bool GitClient::fullySynchronousGit(const QString &workingDirectory,
                                    const QStringList &gitArguments,
                                    QByteArray* outputText,
                                    QByteArray* errorText,
                                    bool logCommandToWindow)
con's avatar
con committed
{
        outputWindow()->appendCommand(workingDirectory, m_binaryPath, gitArguments);
con's avatar
con committed
    process.setWorkingDirectory(workingDirectory);
    process.setProcessEnvironment(processEnvironment());
con's avatar
con committed

    process.start(binary(), gitArguments);
    process.closeWriteChannel();
    if (!process.waitForStarted()) {
        if (errorText) {
            const QString msg = QString::fromLatin1("Unable to execute '%1': %2:")
                                .arg(binary(), process.errorString());
            *errorText = msg.toLocal8Bit();
        }
        return false;
    }
    if (!Utils::SynchronousProcess::readDataFromProcess(process, m_settings.timeoutSeconds * 1000,
                                                        outputText, errorText, true)) {
        errorText->append(GitCommand::msgTimeout(m_settings.timeoutSeconds).toLocal8Bit());
        Utils::SynchronousProcess::stopProcess(process);
con's avatar
con committed
        return false;
    }

    return process.exitStatus() == QProcess::NormalExit && process.exitCode() == 0;
con's avatar
con committed
}

Tobias Hunger's avatar
Tobias Hunger committed
static inline int askWithDetailedText(QWidget *parent,
                                      const QString &title, const QString &msg,
                                      const QString &inf,
                                      QMessageBox::StandardButton defaultButton,
                                      QMessageBox::StandardButtons buttons = QMessageBox::Yes|QMessageBox::No)
{
    QMessageBox msgBox(QMessageBox::Question, title, msg, buttons, parent);
    msgBox.setDetailedText(inf);
    msgBox.setDefaultButton(defaultButton);
    return msgBox.exec();
}

// Convenience that pops up an msg box.
GitClient::StashResult GitClient::ensureStash(const QString &workingDirectory)
{
    QString errorMessage;
    const StashResult sr = ensureStash(workingDirectory, &errorMessage);
        outputWindow()->appendError(errorMessage);
    return sr;
}

// Ensure that changed files are stashed before a pull or similar
GitClient::StashResult GitClient::ensureStash(const QString &workingDirectory, QString *errorMessage)
{
    QString statusOutput;
    switch (gitStatus(workingDirectory, false, &statusOutput, errorMessage)) {
        case StatusChanged:
        break;
        case StatusUnchanged:
        return StashUnchanged;
        case StatusFailed:
        return StashFailed;
    }

    const int answer = askWithDetailedText(m_core->mainWindow(), tr("Changes"),
Tobias Hunger's avatar
Tobias Hunger committed
                             tr("Would you like to stash your changes?"),
                             statusOutput, QMessageBox::Yes, QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
    switch (answer) {
        case QMessageBox::Cancel:
            return StashCanceled;
        case QMessageBox::Yes:
            if (!executeSynchronousStash(workingDirectory, creatorStashMessage(QLatin1String("push")), errorMessage))
                return StashFailed;
            break;
        case QMessageBox::No: // At your own risk, so.
            return NotStashed;
        }

    return Stashed;
 }

// Trim a git status file spec: "modified:    foo .cpp" -> "modified: foo .cpp"
static inline QString trimFileSpecification(QString fileSpec)
{
    const int colonIndex = fileSpec.indexOf(QLatin1Char(':'));
    if (colonIndex != -1) {
        // Collapse the sequence of spaces
        const int filePos = colonIndex + 2;
        int nonBlankPos = filePos;
        for ( ; fileSpec.at(nonBlankPos).isSpace(); nonBlankPos++) ;
        if (nonBlankPos > filePos)
            fileSpec.remove(filePos, nonBlankPos - filePos);
    }
    return fileSpec;
}

GitClient::StatusResult GitClient::gitStatus(const QString &workingDirectory,
                                             bool untracked,
                                             QString *output,
{
    // Run 'status'. Note that git returns exitcode 1 if there are no added files.
    QByteArray outputText;
    QByteArray errorText;
    // @TODO: Use "--no-color" once it is supported
    QStringList statusArgs(QLatin1String("status"));
    if (untracked)
        statusArgs << QLatin1String("-u");
    const bool statusRc = fullySynchronousGit(workingDirectory, statusArgs, &outputText, &errorText);
        *output = commandOutputFromLocal8Bit(outputText);
    const bool branchKnown = outputText.contains(kBranchIndicatorC);
    if (onBranch)
        *onBranch = branchKnown;
    // Is it something really fatal?
    if (!statusRc && !branchKnown && !outputText.contains("# Not currently on any branch.")) {
            const QString error = commandOutputFromLocal8Bit(errorText);
Tobias Hunger's avatar
Tobias Hunger committed
            *errorMessage = tr("Cannot obtain status: %1").arg(error);
Friedemann Kleint's avatar
Friedemann Kleint committed
    // Unchanged (output text depending on whether -u was passed)
    if (outputText.contains("nothing to commit"))
    if (outputText.contains("nothing added to commit but untracked files present"))
        return untracked ? StatusChanged : StatusUnchanged;
// Quietly retrieve branch list of remote repository URL
Tobias Hunger's avatar
Tobias Hunger committed
//
// The branch HEAD is pointing to is always returned first.
QStringList GitClient::synchronousRepositoryBranches(const QString &repositoryURL)
{
    QStringList arguments(QLatin1String("ls-remote"));
Tobias Hunger's avatar
Tobias Hunger committed
    arguments << repositoryURL << QLatin1String("HEAD") << QLatin1String("refs/heads/*");
    const unsigned flags =
            VCSBase::VCSBasePlugin::SshPasswordPrompt|
            VCSBase::VCSBasePlugin::SuppressStdErrInLogWindow|
            VCSBase::VCSBasePlugin::SuppressFailMessageInLogWindow;
    const Utils::SynchronousProcessResponse resp = synchronousGit(QString(), arguments, flags);
    QStringList branches;
Tobias Hunger's avatar
Tobias Hunger committed
    branches << "<detached HEAD>";
    QString headSha;
    if (resp.result == Utils::SynchronousProcessResponse::Finished) {
        // split "82bfad2f51d34e98b18982211c82220b8db049b<tab>refs/heads/master"
        foreach(const QString &line, resp.stdOut.split(QLatin1Char('\n'))) {
Tobias Hunger's avatar
Tobias Hunger committed
            if (line.endsWith("\tHEAD")) {
                Q_ASSERT(headSha.isNull());
                headSha = line.left(line.indexOf(QChar('\t')));
                continue;
            }

            const int slashPos = line.lastIndexOf(QLatin1Char('/'));
Tobias Hunger's avatar
Tobias Hunger committed
            const QString branchName = line.mid(slashPos + 1);
            if (slashPos != -1) {
                if (line.startsWith(headSha))
                    branches[0] = branchName;
                else
                    branches.push_back(branchName);
            }
void GitClient::launchGitK(const QString &workingDirectory)
{
    VCSBase::VCSBaseOutputWindow *outwin = VCSBase::VCSBaseOutputWindow::instance();
    // Locate git in (potentially) custom path. m_binaryPath can be absolute,
    // which will be handled correctly.
    QTC_ASSERT(!m_binaryPath.isEmpty(), return);
    const QString gitBinary = QLatin1String(Constants::GIT_BINARY);
    const QProcessEnvironment env = processEnvironment();
    const QString path = env.value(QLatin1String("PATH"));
    const QString fullGitBinary = Utils::SynchronousProcess::locateBinary(path, m_binaryPath);
    if (fullGitBinary.isEmpty()) {
Tobias Hunger's avatar
Tobias Hunger committed
        outwin->appendError(tr("Cannot locate \"%1\".").arg(gitBinary));
        return;
    }
    const QString gitBinDirectory = QFileInfo(fullGitBinary).absolutePath();
    QDir foundBinDir = gitBinDirectory;
    const bool foundBinDirIsCmdDir = foundBinDir.dirName() == "cmd";
    if (!tryLauchingGitK(env, workingDirectory, gitBinDirectory, foundBinDirIsCmdDir)) {
        if (foundBinDirIsCmdDir) {
            foundBinDir.cdUp();
            tryLauchingGitK(env, workingDirectory, foundBinDir.path() + "/bin", false);
        }
    }
}

bool GitClient::tryLauchingGitK(const QProcessEnvironment &env,
                                const QString &workingDirectory,
                                const QString &gitBinDirectory,
                                bool silent)
{
#ifdef Q_OS_WIN
    // Launch 'wish' shell from git binary directory with the gitk located there
    const QString binary = gitBinDirectory + QLatin1String("/wish");
    QStringList arguments(gitBinDirectory + QLatin1String("/gitk"));
#else
    // Simple: Run gitk from binary path
    const QString binary = gitBinDirectory + QLatin1String("/gitk");
    VCSBase::VCSBaseOutputWindow *outwin = VCSBase::VCSBaseOutputWindow::instance();
    if (!m_settings.gitkOptions.isEmpty())
        arguments.append(Utils::QtcProcess::splitArgs(m_settings.gitkOptions));
    outwin->appendCommand(workingDirectory, binary, arguments);
    // This should always use QProcess::startDetached (as not to kill
    // the child), but that does not have an environment parameter.
    bool success = false;
    if (m_settings.adoptPath) {
        QProcess *process = new QProcess(this);
        process->setWorkingDirectory(workingDirectory);
        process->setProcessEnvironment(env);
        process->start(binary, arguments);
        success = process->waitForStarted();
Tobias Hunger's avatar
Tobias Hunger committed
        if (success)
            connect(process, SIGNAL(finished(int)), process, SLOT(deleteLater()));
Tobias Hunger's avatar
Tobias Hunger committed
        else
            delete process;
    } else {
        success = QProcess::startDetached(binary, arguments, workingDirectory);
    if (!success) {
Tobias Hunger's avatar
Tobias Hunger committed
        const QString error = tr("Cannot launch \"%1\".").arg(binary);
        if (silent)
            outwin->appendSilently(error);
        else
            outwin->appendError(error);
    }
    return success;
con's avatar
con committed
bool GitClient::getCommitData(const QString &workingDirectory,
con's avatar
con committed
                              QString *commitTemplate,
                              CommitData *commitData,
con's avatar
con committed
                              QString *errorMessage)
{
    commitData->clear();
con's avatar
con committed

    // Find repo
    const QString repoDirectory = GitClient::findRepositoryForDirectory(workingDirectory);
    if (repoDirectory.isEmpty()) {
        *errorMessage = msgRepositoryNotFound(workingDirectory);
con's avatar
con committed
        return false;
    }

    commitData->panelInfo.repository = repoDirectory;
con's avatar
con committed

    QDir gitDir(repoDirectory);
    if (!gitDir.cd(QLatin1String(kGitDirectoryC))) {
Tobias Hunger's avatar
Tobias Hunger committed
        *errorMessage = tr("The repository \"%1\" is not initialized.").arg(repoDirectory);
con's avatar
con committed
        return false;
    }

    // Read description
    const QString descriptionFile = gitDir.absoluteFilePath(QLatin1String("description"));
    if (QFileInfo(descriptionFile).isFile()) {
        Utils::FileReader reader;
        if (!reader.fetch(descriptionFile, QIODevice::Text, errorMessage))
            return false;
        commitData->panelInfo.description = commandOutputFromLocal8Bit(reader.data()).trimmed();
con's avatar
con committed
    }

    // Run status. Note that it has exitcode 1 if there are no added files.
    const StatusResult status = gitStatus(repoDirectory, true, &output, errorMessage, &onBranch);
    switch (status) {
        if (!onBranch) {
            *errorMessage = tr("You did not checkout a branch.");
            return false;
        }
        *errorMessage = msgNoChangedFiles();
        return false;
    case StatusFailed:
        return false;
con's avatar
con committed
    }

    //    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
    //    #       modified:   submodule (modified content)
    //    #       modified:   submodule2 (new commit)
con's avatar
con committed
    //    #
    //    # Untracked files:
    //    #   (use "git add <file>..." to include in what will be committed)
    //    #
    //    #       list of files...

    if (status != StatusUnchanged) {
        if (!commitData->parseFilesFromStatus(output)) {
            *errorMessage = msgParseFilesFailed();
            return false;
        }
        // Filter out untracked files that are not part of the project
        VCSBase::VCSBaseSubmitEditor::filterUntrackedFilesOfProject(repoDirectory, &commitData->untrackedFiles);
        if (commitData->filesEmpty()) {
            *errorMessage = msgNoChangedFiles();
            return false;
        }
con's avatar
con committed

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

    // Get the commit template or the last commit message
    if (amend) {
        // Amend: get last commit data as "SHA1@message". TODO: Figure out codec.
        QStringList args(QLatin1String("log"));
        const QString format = synchronousGitVersion(true) > 0x010701 ? "%h@%B" : "%h@%s%n%n%b";
        args << QLatin1String("--max-count=1") << QLatin1String("--pretty=format:") + format;
        const Utils::SynchronousProcessResponse sp = synchronousGit(repoDirectory, args);
        if (sp.result != Utils::SynchronousProcessResponse::Finished) {
Tobias Hunger's avatar
Tobias Hunger committed
            *errorMessage = tr("Cannot retrieve last commit data of repository \"%1\".").arg(repoDirectory);
            return false;
        }
        const int separatorPos = sp.stdOut.indexOf(QLatin1Char('@'));
        QTC_ASSERT(separatorPos != -1, return false)
        commitData->amendSHA1= sp.stdOut.left(separatorPos);
        *commitTemplate = sp.stdOut.mid(separatorPos + 1);
    } else {
        // Commit: Get the commit template
        QString templateFilename = readConfigValue(workingDirectory, QLatin1String("commit.template"));
        if (!templateFilename.isEmpty()) {
            // Make relative to repository
            const QFileInfo templateFileInfo(templateFilename);
            if (templateFileInfo.isRelative())
                templateFilename = repoDirectory + QLatin1Char('/') + templateFilename;
            Utils::FileReader reader;
            if (!reader.fetch(templateFilename, QIODevice::Text, errorMessage))
                return false;
            *commitTemplate = QString::fromLocal8Bit(reader.data());
con's avatar
con committed
        }
    }
    return true;
}

// Log message for commits/amended commits to go to output window
static inline QString msgCommitted(const QString &amendSHA1, int fileCount)
{
    if (amendSHA1.isEmpty())
        return GitClient::tr("Committed %n file(s).\n", 0, fileCount);
    if (fileCount)
Tobias Hunger's avatar
Tobias Hunger committed
        return GitClient::tr("Amended \"%1\" (%n file(s)).\n", 0, fileCount).arg(amendSHA1);
    return GitClient::tr("Amended \"%1\".").arg(amendSHA1);
// addAndCommit:
bool GitClient::addAndCommit(const QString &repositoryDirectory,
con's avatar
con committed
                             const GitSubmitEditorPanelData &data,
                             const QString &amendSHA1,
con's avatar
con committed
                             const QString &messageFile,
                             const QStringList &checkedFiles,
                             const QStringList &origCommitFiles,
                             const QStringList &origDeletedFiles)
con's avatar
con committed
{
    const QString renamedSeparator = QLatin1String(" -> ");
    const bool amend = !amendSHA1.isEmpty();

    // Do we need to reset any files that had been added before
    // (did the user uncheck any previously added files)
    // Split up  renamed files ('foo.cpp -> foo2.cpp').
    QStringList resetFiles = origCommitFiles.toSet().subtract(checkedFiles.toSet()).toList();
    for (QStringList::iterator it = resetFiles.begin(); it != resetFiles.end(); ++it) {
        const int renamedPos = it->indexOf(renamedSeparator);
        if (renamedPos != -1) {
            const QString newFile = it->mid(renamedPos + renamedSeparator.size());
            it->truncate(renamedPos);
            it = resetFiles.insert(++it, newFile);
        }
    }

    if (!resetFiles.isEmpty())
        if (!synchronousReset(repositoryDirectory, resetFiles))
    // Re-add all to make sure we have the latest changes, but only add those that aren't marked
    // for deletion. Purge out renamed files ('foo.cpp -> foo2.cpp').
    QStringList addFiles = checkedFiles.toSet().subtract(origDeletedFiles.toSet()).toList();
    for (QStringList::iterator it = addFiles.begin(); it != addFiles.end(); ) {
        if (it->contains(renamedSeparator)) {
            it = addFiles.erase(it);
        } else {
            ++it;
        }
    }
        if (!synchronousAdd(repositoryDirectory, false, addFiles))
con's avatar
con committed

    // Do the final commit
    QStringList args;
    args << QLatin1String("commit")
         << QLatin1String("-F") << QDir::toNativeSeparators(messageFile);
    if (amend)
        args << QLatin1String("--amend");
    const QString &authorString =  data.authorString();
    if (!authorString.isEmpty())
         args << QLatin1String("--author") << authorString;
con's avatar
con committed

    QByteArray outputText;
    QByteArray errorText;
    const bool rc = fullySynchronousGit(repositoryDirectory, args, &outputText, &errorText);
        outputWindow()->append(msgCommitted(amendSHA1, checkedFiles.size()));
Tobias Hunger's avatar
Tobias Hunger committed
        outputWindow()->appendError(tr("Cannot commit %n file(s): %1\n", 0, checkedFiles.size()).arg(commandOutputFromLocal8Bit(errorText)));
con's avatar
con committed
    return rc;
}

/* Revert: This function can be called with a file list (to revert single
 * files)  or a single directory (revert all). Qt Creator currently has only
 * 'revert single' in its VCS menus, but the code is prepared to deal with
 * reverting a directory pending a sophisticated selection dialog in the
 * VCSBase plugin. */

GitClient::RevertResult GitClient::revertI(QStringList files,
                                           bool *ptrToIsDirectory,
                                           QString *errorMessage,
                                           bool revertStaging)
{
    if (files.empty())
        return RevertCanceled;

    // Figure out the working directory
    const QFileInfo firstFile(files.front());
    const bool isDirectory = firstFile.isDir();
    if (ptrToIsDirectory)
        *ptrToIsDirectory = isDirectory;
    const QString workingDirectory = isDirectory ? firstFile.absoluteFilePath() : firstFile.absolutePath();

    const QString repoDirectory = GitClient::findRepositoryForDirectory(workingDirectory);
    if (repoDirectory.isEmpty()) {
        *errorMessage = msgRepositoryNotFound(workingDirectory);
        return RevertFailed;
    }

    // Check for changes
    QString output;
    switch (gitStatus(repoDirectory, false, &output, errorMessage)) {
    case StatusChanged:
        break;
    case StatusUnchanged:
        return RevertUnchanged;
    case StatusFailed:
        return RevertFailed;
    }
    if (!data.parseFilesFromStatus(output)) {
        *errorMessage = msgParseFilesFailed();
        return RevertFailed;
    }

    // If we are looking at files, make them relative to the repository
    // directory to match them in the status output list.
    if (!isDirectory) {
        const QDir repoDir(repoDirectory);
        const QStringList::iterator cend = files.end();
        for (QStringList::iterator it = files.begin(); it != cend; ++it)
            *it = repoDir.relativeFilePath(*it);
    }

    // From the status output, determine all modified [un]staged files.
    const QString modifiedState = QLatin1String("modified");
    const QStringList allStagedFiles = data.stagedFileNames(modifiedState);
    const QStringList allUnstagedFiles = data.unstagedFileNames(modifiedState);
    // Unless a directory was passed, filter all modified files for the
    // argument file list.
    QStringList stagedFiles = allStagedFiles;
    QStringList unstagedFiles = allUnstagedFiles;
    if (!isDirectory) {
        const QSet<QString> filesSet = files.toSet();
        stagedFiles = allStagedFiles.toSet().intersect(filesSet).toList();
        unstagedFiles = allUnstagedFiles.toSet().intersect(filesSet).toList();
    }
    if ((!revertStaging || stagedFiles.empty()) && unstagedFiles.empty())
        return RevertUnchanged;

    // Ask to revert (to do: Handle lists with a selection dialog)
    const QMessageBox::StandardButton answer
        = QMessageBox::question(m_core->mainWindow(),
                                tr("Revert"),
                                tr("The file has been changed. Do you want to revert it?"),
                                QMessageBox::Yes|QMessageBox::No,
                                QMessageBox::No);
    if (answer == QMessageBox::No)
        return RevertCanceled;

    // Unstage the staged files
    if (revertStaging && !stagedFiles.empty() && !synchronousReset(repoDirectory, stagedFiles, errorMessage))
    QStringList filesToRevert = unstagedFiles;
    if (revertStaging)
        filesToRevert += stagedFiles;
    if (!synchronousCheckoutFiles(repoDirectory, filesToRevert, QString(), errorMessage, revertStaging))
void GitClient::revert(const QStringList &files, bool revertStaging)
    switch (revertI(files, &isDirectory, &errorMessage, revertStaging)) {
        m_plugin->gitVersionControl()->emitFilesChanged(files);