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 <QFileInfo>
#include "filelist.h"
#include "path.h"
#include "pathmatch.h"
QStringList FileList::GetDefaultFilters()
{
@ -106,28 +108,28 @@ void FileList::AddExcludeList(const QStringList &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
{
#ifdef _WIN32
const PathMatch pathMatch(toStdStringList(mExcludedPaths), true);
#else
const PathMatch pathMatch(toStdStringList(mExcludedPaths), false);
#endif
QStringList paths;
foreach (QFileInfo item, mFileList) {
QString name = QDir::fromNativeSeparators(item.canonicalFilePath());
if (!Match(name))
if (!pathMatch.Match(name.toStdString()))
paths << name;
}
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;
/**
* @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:
QFileInfoList mFileList;
QStringList mExcludedPaths;

View File

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

View File

@ -33,11 +33,11 @@ public:
/**
* The constructor.
* @param masks List of masks.
* @param excludedPaths List of masks.
* @param caseSensitive Match the case of the characters when
* 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.
@ -56,7 +56,7 @@ protected:
static std::string RemoveFilename(const std::string &path);
private:
std::vector<std::string> _masks;
std::vector<std::string> _excludedPaths;
bool _caseSensitive;
};