Fixed #4399 (Exclude directory with absolute path does not work)

This commit is contained in:
Daniel Marjamäki 2016-10-02 13:02:29 +02:00
parent 3605457cbe
commit 03d6d34396
4 changed files with 32 additions and 37 deletions

View File

@ -21,6 +21,8 @@
#include <QDir> #include <QDir>
#include <QFileInfo> #include <QFileInfo>
#include "filelist.h" #include "filelist.h"
#include "path.h"
#include "pathmatch.h"
QStringList FileList::GetDefaultFilters() QStringList FileList::GetDefaultFilters()
{ {
@ -106,28 +108,28 @@ void FileList::AddExcludeList(const QStringList &paths)
mExcludedPaths = paths; mExcludedPaths = paths;
} }
static std::vector<std::string> toStdStringList(const QStringList &stringList)
{
std::vector<std::string> ret;
foreach (const QString &s, stringList) {
ret.push_back(s.toStdString());
}
return ret;
}
QStringList FileList::ApplyExcludeList() const QStringList FileList::ApplyExcludeList() const
{ {
#ifdef _WIN32
const PathMatch pathMatch(toStdStringList(mExcludedPaths), true);
#else
const PathMatch pathMatch(toStdStringList(mExcludedPaths), false);
#endif
QStringList paths; QStringList paths;
foreach (QFileInfo item, mFileList) { foreach (QFileInfo item, mFileList) {
QString name = QDir::fromNativeSeparators(item.canonicalFilePath()); QString name = QDir::fromNativeSeparators(item.canonicalFilePath());
if (!Match(name)) if (!pathMatch.Match(name.toStdString()))
paths << name; paths << name;
} }
return paths; return paths;
} }
bool FileList::Match(const QString &path) const
{
for (int i = 0; i < mExcludedPaths.size(); i++) {
if (mExcludedPaths[i].endsWith('/')) {
const QString pathexclude("/" + mExcludedPaths[i]);
if (path.indexOf(pathexclude) != -1)
return true;
} else {
if (path.endsWith(mExcludedPaths[i]))
return true;
}
}
return false;
}

View File

@ -91,13 +91,6 @@ protected:
*/ */
QStringList ApplyExcludeList() const; QStringList ApplyExcludeList() const;
/**
* @brief Test if path matches any of the exclude filters.
* @param path Path to test against exclude filters.
* @return true if any of the filters matches, false otherwise.
*/
bool Match(const QString &path) const;
private: private:
QFileInfoList mFileList; QFileInfoList mFileList;
QStringList mExcludedPaths; QStringList mExcludedPaths;

View File

@ -21,11 +21,11 @@
#include <algorithm> #include <algorithm>
#include <ctype.h> #include <ctype.h>
PathMatch::PathMatch(const std::vector<std::string> &masks, bool caseSensitive) PathMatch::PathMatch(const std::vector<std::string> &excludedPaths, bool caseSensitive)
: _masks(masks), _caseSensitive(caseSensitive) : _excludedPaths(excludedPaths), _caseSensitive(caseSensitive)
{ {
if (!_caseSensitive) if (!_caseSensitive)
for (std::vector<std::string>::iterator i = _masks.begin(); i != _masks.end(); ++i) for (std::vector<std::string>::iterator i = _excludedPaths.begin(); i != _excludedPaths.end(); ++i)
std::transform(i->begin(), i->end(), i->begin(), ::tolower); std::transform(i->begin(), i->end(), i->begin(), ::tolower);
} }
@ -37,38 +37,38 @@ bool PathMatch::Match(const std::string &path) const
std::vector<std::string> workingDirectory; std::vector<std::string> workingDirectory;
workingDirectory.push_back(Path::getCurrentPath()); workingDirectory.push_back(Path::getCurrentPath());
for (std::vector<std::string>::const_iterator iterMask = _masks.begin(); iterMask != _masks.end(); ++iterMask) { for (std::vector<std::string>::const_iterator i = _excludedPaths.begin(); i != _excludedPaths.end(); ++i) {
const std::string mask((!Path::isAbsolute(path) && Path::isAbsolute(*iterMask)) ? Path::getRelativePath(*iterMask, workingDirectory) : *iterMask); const std::string excludedPath((!Path::isAbsolute(path) && Path::isAbsolute(*i)) ? Path::getRelativePath(*i, workingDirectory) : *i);
std::string findpath = Path::fromNativeSeparators(path); std::string findpath = Path::fromNativeSeparators(path);
if (!_caseSensitive) if (!_caseSensitive)
std::transform(findpath.begin(), findpath.end(), findpath.begin(), ::tolower); std::transform(findpath.begin(), findpath.end(), findpath.begin(), ::tolower);
// Filtering directory name // Filtering directory name
if (mask[mask.length() - 1] == '/') { if (excludedPath[excludedPath.length() - 1] == '/') {
if (findpath[findpath.length() - 1] != '/') if (findpath[findpath.length() - 1] != '/')
findpath = RemoveFilename(findpath); findpath = RemoveFilename(findpath);
if (mask.length() > findpath.length()) if (excludedPath.length() > findpath.length())
continue; continue;
// Match relative paths starting with mask // Match relative paths starting with mask
// -isrc matches src/foo.cpp // -isrc matches src/foo.cpp
if (findpath.compare(0, mask.size(), mask) == 0) if (findpath.compare(0, excludedPath.size(), excludedPath) == 0)
return true; return true;
// Match only full directory name in middle or end of the path // Match only full directory name in middle or end of the path
// -isrc matches myproject/src/ but does not match // -isrc matches myproject/src/ but does not match
// myproject/srcfiles/ or myproject/mysrc/ // myproject/srcfiles/ or myproject/mysrc/
if (findpath.find("/" + mask) != std::string::npos) if (findpath.find("/" + excludedPath) != std::string::npos)
return true; return true;
} }
// Filtering filename // Filtering filename
else { else {
if (mask.length() > findpath.length()) if (excludedPath.length() > findpath.length())
continue; continue;
// Check if path ends with mask // Check if path ends with mask
// -ifoo.cpp matches (./)foo.c, src/foo.cpp and proj/src/foo.cpp // -ifoo.cpp matches (./)foo.c, src/foo.cpp and proj/src/foo.cpp
// -isrc/file.cpp matches src/foo.cpp and proj/src/foo.cpp // -isrc/file.cpp matches src/foo.cpp and proj/src/foo.cpp
if (findpath.compare(findpath.size() - mask.size(), findpath.size(), mask) == 0) if (findpath.compare(findpath.size() - excludedPath.size(), findpath.size(), excludedPath) == 0)
return true; return true;
} }

View File

@ -33,11 +33,11 @@ public:
/** /**
* The constructor. * The constructor.
* @param masks List of masks. * @param excludedPaths List of masks.
* @param caseSensitive Match the case of the characters when * @param caseSensitive Match the case of the characters when
* matching paths? * matching paths?
*/ */
explicit PathMatch(const std::vector<std::string> &masks, bool caseSensitive = true); explicit PathMatch(const std::vector<std::string> &excludedPaths, bool caseSensitive = true);
/** /**
* @brief Match path against list of masks. * @brief Match path against list of masks.
@ -56,7 +56,7 @@ protected:
static std::string RemoveFilename(const std::string &path); static std::string RemoveFilename(const std::string &path);
private: private:
std::vector<std::string> _masks; std::vector<std::string> _excludedPaths;
bool _caseSensitive; bool _caseSensitive;
}; };