forked from qt-creator/qt-creator
VCS: Refactor fileNameFromDiffSpecification
Use diff file pattern to match file name. Avoid duplication. Include unit tests for Git Change-Id: Ib68a08368270a27976a3e16bdd1cb219a52b8889 Reviewed-by: Tobias Hunger <tobias.hunger@digia.com>
This commit is contained in:
committed by
Orgad Shaneh
parent
57ee50d52f
commit
7b39f41c05
@@ -66,6 +66,13 @@ GitEditor::GitEditor(const VcsBase::VcsBaseEditorParameters *type,
|
||||
{
|
||||
QTC_ASSERT(m_changeNumberPattern8.isValid(), return);
|
||||
QTC_ASSERT(m_changeNumberPattern40.isValid(), return);
|
||||
/* Diff format:
|
||||
diff --git a/src/plugins/git/giteditor.cpp b/src/plugins/git/giteditor.cpp
|
||||
index 40997ff..4e49337 100644
|
||||
--- a/src/plugins/git/giteditor.cpp
|
||||
+++ b/src/plugins/git/giteditor.cpp
|
||||
*/
|
||||
setDiffFilePattern(QRegExp(QLatin1String("^(?:diff --git a/|index |[+-]{3} (?:/dev/null|[ab]/(.+$)))")));
|
||||
setAnnotateRevisionTextFormat(tr("Blame %1"));
|
||||
setAnnotatePreviousRevisionTextFormat(tr("Blame Parent Revision %1"));
|
||||
}
|
||||
@@ -107,46 +114,12 @@ QString GitEditor::changeUnderCursor(const QTextCursor &c) const
|
||||
return QString();
|
||||
}
|
||||
|
||||
QRegExp GitEditor::diffFilePattern() const
|
||||
{
|
||||
return QRegExp(QLatin1String("^(diff --git a/|index |[+-][+-][+-] [ab/]).*$"));
|
||||
}
|
||||
|
||||
VcsBase::BaseAnnotationHighlighter *GitEditor::createAnnotationHighlighter(const QSet<QString> &changes,
|
||||
const QColor &bg) const
|
||||
{
|
||||
return new GitAnnotationHighlighter(changes, bg);
|
||||
}
|
||||
|
||||
QString GitEditor::fileNameFromDiffSpecification(const QTextBlock &inBlock) const
|
||||
{
|
||||
// Check for "+++ b/src/plugins/git/giteditor.cpp" (blame and diff)
|
||||
// as well as "--- a/src/plugins/git/giteditor.cpp".
|
||||
// Go back chunks.
|
||||
bool checkForOld = false;
|
||||
|
||||
const QString oldFileIndicator = QLatin1String("--- a/");
|
||||
const QString newFileIndicator = QLatin1String("+++ ");
|
||||
for (QTextBlock block = inBlock; block.isValid(); block = block.previous()) {
|
||||
QString diffFileName = block.text();
|
||||
if (diffFileName.startsWith(oldFileIndicator) && checkForOld) {
|
||||
diffFileName.remove(0, oldFileIndicator.size());
|
||||
checkForOld = false;
|
||||
return diffFileName;
|
||||
} else if (diffFileName.startsWith(newFileIndicator)) {
|
||||
diffFileName.remove(0, newFileIndicator.size());
|
||||
if (diffFileName == QLatin1String("/dev/null")) {
|
||||
checkForOld = true;
|
||||
continue;
|
||||
}
|
||||
diffFileName.remove(0, 2); // remove "b/"
|
||||
return findDiffFile(diffFileName);
|
||||
}
|
||||
checkForOld = false;
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
/* Remove the date specification from annotation, which is tabular:
|
||||
\code
|
||||
8ca887aa (author YYYY-MM-DD HH:MM:SS <offset> <line>)<content>
|
||||
|
||||
@@ -57,9 +57,7 @@ public slots:
|
||||
private:
|
||||
QSet<QString> annotationChanges() const;
|
||||
QString changeUnderCursor(const QTextCursor &) const;
|
||||
QRegExp diffFilePattern() const;
|
||||
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes, const QColor &bg) const;
|
||||
QString fileNameFromDiffSpecification(const QTextBlock &diffFileName) const;
|
||||
QString decorateVersion(const QString &revision) const;
|
||||
QStringList annotationPreviousVersions(const QString &revision) const;
|
||||
bool isValidRevision(const QString &revision) const;
|
||||
|
||||
@@ -1253,8 +1253,14 @@ GitClient *GitPlugin::gitClient() const
|
||||
}
|
||||
|
||||
#ifdef WITH_TESTS
|
||||
#include "giteditor.h"
|
||||
|
||||
#include <QTest>
|
||||
#include <QTextBlock>
|
||||
#include <QTextDocument>
|
||||
|
||||
Q_DECLARE_METATYPE(FileStates)
|
||||
|
||||
void GitPlugin::testStatusParsing_data()
|
||||
{
|
||||
QTest::addColumn<FileStates>("first");
|
||||
@@ -1301,6 +1307,42 @@ void GitPlugin::testStatusParsing()
|
||||
else
|
||||
QCOMPARE(data.files.at(1).first, second);
|
||||
}
|
||||
|
||||
void GitPlugin::testDiffFileResolving_data()
|
||||
{
|
||||
QTest::addColumn<QByteArray>("header");
|
||||
QTest::addColumn<QByteArray>("fileName");
|
||||
|
||||
QTest::newRow("New") << QByteArray(
|
||||
"diff --git a/src/plugins/git/giteditor.cpp b/src/plugins/git/giteditor.cpp\n"
|
||||
"new file mode 100644\n"
|
||||
"index 0000000..40997ff\n"
|
||||
"--- /dev/null\n"
|
||||
"+++ b/src/plugins/git/giteditor.cpp\n"
|
||||
"@@ -0,0 +1,281 @@\n\n")
|
||||
<< QByteArray("src/plugins/git/giteditor.cpp");
|
||||
QTest::newRow("Deleted") << QByteArray(
|
||||
"diff --git a/src/plugins/git/giteditor.cpp b/src/plugins/git/giteditor.cpp\n"
|
||||
"deleted file mode 100644\n"
|
||||
"index 40997ff..0000000\n"
|
||||
"--- a/src/plugins/git/giteditor.cpp\n"
|
||||
"+++ /dev/null\n"
|
||||
"@@ -1,281 +0,0 @@\n\n")
|
||||
<< QByteArray("src/plugins/git/giteditor.cpp");
|
||||
QTest::newRow("Normal") << QByteArray(
|
||||
"diff --git a/src/plugins/git/giteditor.cpp b/src/plugins/git/giteditor.cpp\n"
|
||||
"index 69e0b52..8fc974d 100644\n"
|
||||
"--- a/src/plugins/git/giteditor.cpp\n"
|
||||
"+++ b/src/plugins/git/giteditor.cpp\n"
|
||||
"@@ -49,6 +49,8 @@\n\n")
|
||||
<< QByteArray("src/plugins/git/giteditor.cpp");
|
||||
}
|
||||
|
||||
void GitPlugin::testDiffFileResolving()
|
||||
{
|
||||
GitEditor editor(editorParameters + 3, 0);
|
||||
VcsBase::VcsBaseEditorWidget::testDiffFileResolving(&editor);
|
||||
}
|
||||
#endif
|
||||
|
||||
Q_EXPORT_PLUGIN(GitPlugin)
|
||||
|
||||
@@ -144,6 +144,8 @@ private slots:
|
||||
#ifdef WITH_TESTS
|
||||
void testStatusParsing_data();
|
||||
void testStatusParsing();
|
||||
void testDiffFileResolving_data();
|
||||
void testDiffFileResolving();
|
||||
#endif
|
||||
protected:
|
||||
void updateActions(VcsBase::VcsBasePlugin::ActionState);
|
||||
|
||||
Reference in New Issue
Block a user