Skip to content
Snippets Groups Projects
commitdata.cpp 6.05 KiB
Newer Older
/**************************************************************************
con's avatar
con committed
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
**
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
** 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.
con's avatar
con committed
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
con's avatar
con committed
**
**************************************************************************/
hjk's avatar
hjk committed

con's avatar
con committed
#include "commitdata.h"
con's avatar
con committed

#include <QCoreApplication>
#include <QDebug>
#include <QRegExp>
con's avatar
con committed
namespace Git {
namespace Internal {

void GitSubmitEditorPanelInfo::clear()
{
    repository.clear();
    description.clear();
    branch.clear();
}

QDebug operator<<(QDebug d, const GitSubmitEditorPanelInfo &data)
{
    d.nospace() << "Rep: " << data.repository << " Descr: " << data.description
        << " branch: " << data.branch;
    return d;
}

void GitSubmitEditorPanelData::clear()
{
    author.clear();
    email.clear();
    bypassHooks = false;
con's avatar
con committed
}

QString GitSubmitEditorPanelData::authorString() const
{
    QString rc;
    rc += author;

    if (email.isEmpty())
        return rc;

    rc += QLatin1String(" <");
con's avatar
con committed
    rc += email;
    rc += QLatin1Char('>');
con's avatar
con committed
    return rc;
}

QDebug operator<<(QDebug d, const GitSubmitEditorPanelData &data)
{
    d.nospace() << " author:" << data.author << " email: " << data.email
                << " bypass hooks: " << data.bypassHooks;
con's avatar
con committed
    return d;
}

void CommitData::clear()
{
    panelInfo.clear();
    panelData.clear();
    amendSHA1.clear();
con's avatar
con committed

    files.clear();
con's avatar
con committed
}

static CommitData::FileState stateFor(const QChar &c)
    switch (c.unicode()) {
    case ' ':
        return CommitData::UntrackedFile;
    case 'M':
        return CommitData::ModifiedFile;
    case 'A':
        return CommitData::AddedFile;
    case 'D':
        return CommitData::DeletedFile;
    case 'R':
        return CommitData::RenamedFile;
    case 'C':
        return CommitData::CopiedFile;
    case 'U':
        return CommitData::UpdatedFile;
    default:
        return CommitData::UnknownFileState;
    }
static bool checkLine(const QString &stateInfo, const QString &file, QList<CommitData::StateFilePair> *files)
    QTC_ASSERT(stateInfo.count() == 2, return false);
    QTC_ASSERT(files, return false);
    if (stateInfo == QLatin1String("??")) {
        files->append(qMakePair(CommitData::UntrackedFile, file));
        return true;
    }

    CommitData::FileState stagedState = stateFor(stateInfo.at(0));
    if (stagedState == CommitData::UnknownFileState)
        return false;

    stagedState = static_cast<CommitData::FileState>(stagedState | CommitData::StagedFile);
    if (stagedState != CommitData::StagedFile)
        files->append(qMakePair(stagedState, file));

    CommitData::FileState state = stateFor(stateInfo.at(1));
    if (state == CommitData::UnknownFileState)

    if (state != CommitData::UntrackedFile) {
        QString newFile = file;
        if (stagedState == CommitData::RenamedStagedFile || stagedState == CommitData::CopiedStagedFile)
            newFile = file.mid(file.indexOf(QLatin1String(" -> ")) + 4);

        files->append(qMakePair(state, newFile));
    }

    ## branch_name
    XY file
    \endcode */
bool CommitData::parseFilesFromStatus(const QString &output)
{
    const QStringList lines = output.split(QLatin1Char('\n'));

    foreach (const QString &line, lines) {
        if (line.isEmpty())
        if (line.startsWith(QLatin1String("## "))) {
            // Branch indication:
            panelInfo.branch = line.mid(3);
        QTC_ASSERT(line.at(2) == QLatin1Char(' '), continue);
        if (!checkLine(line.mid(0, 2), line.mid(3), &files))
            return false;
    return true;
QStringList CommitData::filterFiles(const CommitData::FileState &state) const
    QStringList result;
    foreach (const StateFilePair &p, files) {
        if (state == AllStates || state == p.first)
            result.append(p.second);
    }
    return result;
QString CommitData::stateDisplayName(const FileState &state)
con's avatar
con committed
{
    QString resultState;
    if (state == UntrackedFile)
        return QCoreApplication::translate("Git::Internal::CommitData", "untracked");

    if (state & StagedFile)
        resultState = QCoreApplication::translate("Git::Internal::CommitData", "staged + ");
    if (state & ModifiedFile)
        resultState.append(QCoreApplication::translate("Git::Internal::CommitData", "modified"));
    else if (state & AddedFile)
        resultState.append(QCoreApplication::translate("Git::Internal::CommitData", "added"));
    else if (state & DeletedFile)
        resultState.append(QCoreApplication::translate("Git::Internal::CommitData", "deleted"));
    else if (state & RenamedFile)
        resultState.append(QCoreApplication::translate("Git::Internal::CommitData", "renamed"));
    else if (state & CopiedFile)
        resultState.append(QCoreApplication::translate("Git::Internal::CommitData", "copied"));
    else if (state & UpdatedFile)
        resultState.append(QCoreApplication::translate("Git::Internal::CommitData", "updated"));
    return resultState;
con's avatar
con committed
}

hjk's avatar
hjk committed
} // namespace Internal
} // namespace Git