Commit ab85e734 authored by Orgad Shaneh's avatar Orgad Shaneh Committed by Orgad Shaneh

Git: Extract FileState from CommitData

* Add a QFlags type for it and use it
* Clean up GitClient::addAndCommit
* Rename Updated -> Unmerged

Change-Id: Id96f71fb78af923605f5773f65a5625244a49499
Reviewed-by: default avatarTobias Hunger <tobias.hunger@digia.com>
parent 0f2f2a59
......@@ -88,25 +88,25 @@ void CommitData::clear()
files.clear();
}
static CommitData::FileState stateFor(const QChar &c)
static FileStates stateFor(const QChar &c)
{
switch (c.unicode()) {
case ' ':
return CommitData::UntrackedFile;
return UntrackedFile;
case 'M':
return CommitData::ModifiedFile;
return ModifiedFile;
case 'A':
return CommitData::AddedFile;
return AddedFile;
case 'D':
return CommitData::DeletedFile;
return DeletedFile;
case 'R':
return CommitData::RenamedFile;
return RenamedFile;
case 'C':
return CommitData::CopiedFile;
return CopiedFile;
case 'U':
return CommitData::UpdatedFile;
return UnmergedFile;
default:
return CommitData::UnknownFileState;
return UnknownFileState;
}
}
......@@ -115,25 +115,25 @@ bool CommitData::checkLine(const QString &stateInfo, const QString &file)
QTC_ASSERT(stateInfo.count() == 2, return false);
if (stateInfo == QLatin1String("??")) {
files.append(qMakePair(UntrackedFile, file));
files.append(qMakePair(FileStates(UntrackedFile), file));
return true;
}
FileState stagedState = stateFor(stateInfo.at(0));
FileStates stagedState = stateFor(stateInfo.at(0));
if (stagedState == UnknownFileState)
return false;
stagedState = static_cast<FileState>(stagedState | StagedFile);
stagedState |= StagedFile;
if (stagedState != StagedFile)
files.append(qMakePair(stagedState, file));
FileState state = stateFor(stateInfo.at(1));
FileStates state = stateFor(stateInfo.at(1));
if (state == UnknownFileState)
return false;
if (state != UntrackedFile) {
QString newFile = file;
if (stagedState == RenamedStagedFile || stagedState == CopiedStagedFile)
if (stagedState & (RenamedFile | CopiedFile))
newFile = file.mid(file.indexOf(QLatin1String(" -> ")) + 4);
files.append(qMakePair(state, newFile));
......@@ -171,7 +171,7 @@ bool CommitData::parseFilesFromStatus(const QString &output)
return true;
}
QStringList CommitData::filterFiles(const CommitData::FileState &state) const
QStringList CommitData::filterFiles(const FileStates &state) const
{
QStringList result;
foreach (const StateFilePair &p, files) {
......@@ -181,7 +181,7 @@ QStringList CommitData::filterFiles(const CommitData::FileState &state) const
return result;
}
QString CommitData::stateDisplayName(const FileState &state)
QString CommitData::stateDisplayName(const FileStates &state)
{
QString resultState;
if (state == UntrackedFile)
......@@ -199,8 +199,8 @@ QString CommitData::stateDisplayName(const FileState &state)
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"));
else if (state & UnmergedFile)
resultState.append(QCoreApplication::translate("Git::Internal::CommitData", "unmerged"));
return resultState;
}
......
......@@ -64,33 +64,26 @@ struct GitSubmitEditorPanelData
QDebug operator<<(QDebug d, const GitSubmitEditorPanelData &);
enum FileState {
UntrackedFile = 0,
StagedFile = 0x01,
ModifiedFile = 0x02,
AddedFile = 0x04,
DeletedFile = 0x08,
RenamedFile = 0x10,
CopiedFile = 0x20,
UnmergedFile = 0x40,
UnknownFileState = 0x800
};
Q_DECLARE_FLAGS(FileStates, FileState)
class CommitData
{
public:
enum FileState {
UntrackedFile = 0,
StagedFile = 0x01,
ModifiedFile = 0x02,
AddedFile = 0x04,
DeletedFile = 0x08,
RenamedFile = 0x10,
CopiedFile = 0x20,
UpdatedFile = 0x40,
ModifiedStagedFile = StagedFile | ModifiedFile,
AddedStagedFile = StagedFile | AddedFile,
DeletedStagedFile = StagedFile | DeletedFile,
RenamedStagedFile = StagedFile | RenamedFile,
CopiedStagedFile = StagedFile | CopiedFile,
UpdatedStagedFile = StagedFile | UpdatedFile,
AllStates = UpdatedFile | CopiedFile | RenamedFile | DeletedFile | AddedFile | ModifiedFile | StagedFile,
UnknownFileState
};
// A pair of state string/file name ('modified', 'file.cpp').
typedef QPair<FileState, QString> StateFilePair;
typedef QPair<FileStates, QString> StateFilePair;
void clear();
// Parse the files and the branch of panelInfo
......@@ -99,9 +92,9 @@ public:
// Convenience to retrieve the file names from
// the specification list. Optionally filter for a certain state
QStringList filterFiles(const FileState &state) const;
QStringList filterFiles(const FileStates &state) const;
static QString stateDisplayName(const FileState &state);
static QString stateDisplayName(const FileStates &state);
QString amendSHA1;
QString commitEncoding;
......@@ -117,4 +110,6 @@ private:
} // namespace Internal
} // namespace Git
Q_DECLARE_OPERATORS_FOR_FLAGS(Git::Internal::FileStates)
#endif // COMMITDATA_H
......@@ -1750,13 +1750,13 @@ bool GitClient::getCommitData(const QString &workingDirectory,
}
// Filter out untracked files that are not part of the project
QStringList untrackedFiles = commitData->filterFiles(CommitData::UntrackedFile);
QStringList untrackedFiles = commitData->filterFiles(UntrackedFile);
VcsBase::VcsBaseSubmitEditor::filterUntrackedFilesOfProject(repoDirectory, &untrackedFiles);
QList<CommitData::StateFilePair> filteredFiles;
QList<CommitData::StateFilePair>::const_iterator it = commitData->files.constBegin();
for ( ; it != commitData->files.constEnd(); ++it) {
if (it->first == CommitData::UntrackedFile && !untrackedFiles.contains(it->second))
if (it->first == UntrackedFile && !untrackedFiles.contains(it->second))
continue;
filteredFiles.append(*it);
}
......@@ -1836,47 +1836,41 @@ bool GitClient::addAndCommit(const QString &repositoryDirectory,
int commitCount = 0;
for (int i = 0; i < model->rowCount(); ++i) {
const CommitData::FileState state = static_cast<CommitData::FileState>(model->extraData(i).toInt());
const FileStates state = static_cast<FileStates>(model->extraData(i).toInt());
QString file = model->file(i);
const bool checked = model->checked(i);
if (checked)
++commitCount;
if (state == CommitData::UntrackedFile && checked)
if (state == UntrackedFile && checked)
filesToAdd.append(file);
if (state == CommitData::ModifiedStagedFile && !checked) {
filesToReset.append(file);
} else if (state == CommitData::AddedStagedFile && !checked) {
filesToReset.append(file);
} else if (state == CommitData::DeletedStagedFile && !checked) {
filesToReset.append(file);
} else if (state == CommitData::RenamedStagedFile && !checked) {
const int pos = file.indexOf(QLatin1String(" -> "));
const QString newFile = file.mid(pos + 4);
filesToReset.append(newFile);
} else if (state == CommitData::CopiedStagedFile && !checked) {
const QString newFile = file.mid(file.indexOf(renameSeparator) + renameSeparator.count());
filesToReset.append(newFile);
} else if (state == CommitData::UpdatedStagedFile && !checked) {
QTC_ASSERT(false, continue); // There should not be updated files when commiting!
if ((state & StagedFile) && !checked) {
if (state & (AddedFile | DeletedFile)) {
filesToReset.append(file);
} else if (state & (RenamedFile | CopiedFile)) {
const QString newFile = file.mid(file.indexOf(renameSeparator) + renameSeparator.count());
filesToReset.append(newFile);
}
} else if (state & UnmergedFile && checked) {
QTC_ASSERT(false, continue); // There should not be unmerged files when commiting!
}
if (state == CommitData::ModifiedFile && checked) {
if (state == ModifiedFile && checked) {
filesToReset.removeAll(file);
filesToAdd.append(file);
} else if (state == CommitData::AddedFile && checked) {
} else if (state == AddedFile && checked) {
QTC_ASSERT(false, continue); // these should be untracked!
} else if (state == CommitData::DeletedFile && checked) {
} else if (state == DeletedFile && checked) {
filesToReset.removeAll(file);
filesToRemove.append(file);
} else if (state == CommitData::RenamedFile && checked) {
} else if (state == RenamedFile && checked) {
QTC_ASSERT(false, continue); // git mv directly stages.
} else if (state == CommitData::CopiedFile && checked) {
} else if (state == CopiedFile && checked) {
QTC_ASSERT(false, continue); // only is noticed after adding a new file to the index
} else if (state == CommitData::UpdatedFile && checked) {
QTC_ASSERT(false, continue); // There should not be updated files when commiting!
} else if (state == UnmergedFile && checked) {
QTC_ASSERT(false, continue); // There should not be unmerged files when commiting!
}
}
......@@ -1965,8 +1959,8 @@ GitClient::RevertResult GitClient::revertI(QStringList files,
}
// From the status output, determine all modified [un]staged files.
const QStringList allStagedFiles = data.filterFiles(CommitData::ModifiedStagedFile);
const QStringList allUnstagedFiles = data.filterFiles(CommitData::ModifiedFile);
const QStringList allStagedFiles = data.filterFiles(StagedFile | ModifiedFile);
const QStringList allUnstagedFiles = data.filterFiles(ModifiedFile);
// Unless a directory was passed, filter all modified files for the
// argument file list.
QStringList stagedFiles = allStagedFiles;
......
......@@ -1069,44 +1069,42 @@ GitClient *GitPlugin::gitClient() const
#ifdef WITH_TESTS
#include <QTest>
Q_DECLARE_METATYPE(CommitData::FileState)
Q_DECLARE_METATYPE(FileStates)
void GitPlugin::testStatusParsing_data()
{
QTest::addColumn<QString>("line");
QTest::addColumn<CommitData::FileState>("first");
QTest::addColumn<CommitData::FileState>("second");
#define STATUS_TEST(status, first, second) \
QTest::newRow(status) << QString::fromLatin1(status) << CommitData::first << CommitData::second;
STATUS_TEST(" M", ModifiedFile, UnknownFileState);
STATUS_TEST(" D", DeletedFile, UnknownFileState);
STATUS_TEST("M ", ModifiedStagedFile, UnknownFileState);
STATUS_TEST("MM", ModifiedStagedFile, ModifiedFile);
STATUS_TEST("MD", ModifiedStagedFile, DeletedFile);
STATUS_TEST("A ", AddedStagedFile, UnknownFileState);
STATUS_TEST("AM", AddedStagedFile, ModifiedFile);
STATUS_TEST("AD", AddedStagedFile, DeletedFile);
STATUS_TEST("D ", DeletedStagedFile, UnknownFileState);
STATUS_TEST("DM", DeletedStagedFile, ModifiedFile);
STATUS_TEST("R ", RenamedStagedFile, UnknownFileState);
STATUS_TEST("RM", RenamedStagedFile, ModifiedFile);
STATUS_TEST("RD", RenamedStagedFile, DeletedFile);
STATUS_TEST("C ", CopiedStagedFile, UnknownFileState);
STATUS_TEST("CM", CopiedStagedFile, ModifiedFile);
STATUS_TEST("CD", CopiedStagedFile, DeletedFile);
QTest::addColumn<FileStates>("first");
QTest::addColumn<FileStates>("second");
QTest::newRow(" M") << FileStates(ModifiedFile) << FileStates(UnknownFileState);
QTest::newRow(" D") << FileStates(DeletedFile) << FileStates(UnknownFileState);
QTest::newRow("M ") << (ModifiedFile | StagedFile) << FileStates(UnknownFileState);
QTest::newRow("MM") << (ModifiedFile | StagedFile) << FileStates(ModifiedFile);
QTest::newRow("MD") << (ModifiedFile | StagedFile) << FileStates(DeletedFile);
QTest::newRow("A ") << (AddedFile | StagedFile) << FileStates(UnknownFileState);
QTest::newRow("AM") << (AddedFile | StagedFile) << FileStates(ModifiedFile);
QTest::newRow("AD") << (AddedFile | StagedFile) << FileStates(DeletedFile);
QTest::newRow("D ") << (DeletedFile | StagedFile) << FileStates(UnknownFileState);
QTest::newRow("DM") << (DeletedFile | StagedFile) << FileStates(ModifiedFile);
QTest::newRow("R ") << (RenamedFile | StagedFile) << FileStates(UnknownFileState);
QTest::newRow("RM") << (RenamedFile | StagedFile) << FileStates(ModifiedFile);
QTest::newRow("RD") << (RenamedFile | StagedFile) << FileStates(DeletedFile);
QTest::newRow("C ") << (CopiedFile | StagedFile) << FileStates(UnknownFileState);
QTest::newRow("CM") << (CopiedFile | StagedFile) << FileStates(ModifiedFile);
QTest::newRow("CD") << (CopiedFile | StagedFile) << FileStates(DeletedFile);
}
void GitPlugin::testStatusParsing()
{
CommitData data;
QFETCH(QString, line);
QFETCH(CommitData::FileState, first);
QFETCH(CommitData::FileState, second);
QFETCH(FileStates, first);
QFETCH(FileStates, second);
QString output = QLatin1String("## master...origin/master [ahead 1]\n");
output += line + QLatin1String(" main.cpp\n");
output += QString::fromLatin1(QTest::currentDataTag()) + QLatin1String(" main.cpp\n");
data.parseFilesFromStatus(output);
QCOMPARE(data.files.at(0).first, first);
if (second != CommitData::UnknownFileState)
if (second == UnknownFileState)
QCOMPARE(data.files.size(), 1);
else
QCOMPARE(data.files.at(1).first, second);
}
#endif
......
......@@ -70,9 +70,9 @@ void GitSubmitEditor::setCommitData(const CommitData &d)
if (!d.files.isEmpty()) {
for (QList<CommitData::StateFilePair>::const_iterator it = d.files.constBegin();
it != d.files.constEnd(); ++it) {
const CommitData::FileState state = it->first;
const FileStates state = it->first;
const QString file = it->second;
m_model->addFile(file, CommitData::stateDisplayName(state), state & CommitData::StagedFile,
m_model->addFile(file, CommitData::stateDisplayName(state), state & StagedFile,
QVariant(static_cast<int>(state)));
}
}
......@@ -89,10 +89,10 @@ void GitSubmitEditor::slotDiffSelected(const QStringList &files)
for (int r = 0; r < rowCount; r++) {
const QString fileName = m_model->item(r, fileColumn)->text();
if (files.contains(fileName)) {
const CommitData::FileState state = static_cast<CommitData::FileState>(m_model->extraData(r).toInt());
if (state & CommitData::StagedFile)
const FileStates state = static_cast<FileStates>(m_model->extraData(r).toInt());
if (state & StagedFile)
stagedFiles.push_back(fileName);
else if (state != CommitData::UntrackedFile)
else if (state != UntrackedFile)
unstagedFiles.push_back(fileName);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment