Browse Source

Manager: Change match() path argument type

Changed the path argument from directory_iterator to string, as nothing
in this function relied on the functionality.
master
Riyyi 3 years ago
parent
commit
4cac25b5b3
  1. 21
      src/dotfile.cpp
  2. 3
      src/dotfile.h
  3. 2
      test/unit/testdotfile.cpp

21
src/dotfile.cpp

@ -51,8 +51,7 @@ void Dotfile::add(const std::vector<std::string>& targets)
continue;
}
if (match(std::filesystem::directory_entry { targets.at(i) },
Config::the().systemPatterns())) {
if (match(targets.at(i), Config::the().systemPatterns())) {
systemIndices.push_back(i);
}
else {
@ -107,15 +106,13 @@ void Dotfile::push(const std::vector<std::string>& targets)
pullOrPush(SyncType::Push, targets);
}
bool Dotfile::match(const std::filesystem::directory_entry& path,
const std::vector<std::string>& patterns)
bool Dotfile::match(const std::string& path, const std::vector<std::string>& patterns)
{
std::string pathString = path.path().string();
assert(pathString.front() == '/');
assert(path.front() == '/');
// Cut off working directory
size_t cutFrom = pathString.find(Config::the().workingDirectory()) == 0 ? Config::the().workingDirectorySize() : 0;
pathString = pathString.substr(cutFrom);
size_t cutFrom = path.find(Config::the().workingDirectory()) == 0 ? Config::the().workingDirectorySize() : 0;
std::string pathString = path.substr(cutFrom);
for (const auto& pattern : patterns) {
@ -244,7 +241,7 @@ void Dotfile::pullOrPush(SyncType type, const std::vector<std::string>& targets)
// Separate home and system targets
forEachDotfile(targets, [&](const std::filesystem::directory_entry& path, size_t index) {
dotfiles.push_back(path.path().string());
if (match(path, Config::the().systemPatterns())) {
if (match(path.path().string(), Config::the().systemPatterns())) {
systemIndices.push_back(index);
}
else {
@ -487,15 +484,17 @@ void Dotfile::forEachDotfile(const std::vector<std::string>& targets, const std:
{
size_t index = 0;
for (const auto& path : std::filesystem::recursive_directory_iterator { Config::the().workingDirectory() }) {
std::string pathString = path.path().string();
if (path.is_directory()) {
continue;
}
// Ignore pattern check
if (match(path, Config::the().ignorePatterns())) {
if (match(pathString, Config::the().ignorePatterns())) {
continue;
}
// Include check
if (!targets.empty() && !match(path, targets)) {
if (!targets.empty() && !match(pathString, targets)) {
continue;
}
callback(path, index++);

3
src/dotfile.h

@ -30,8 +30,7 @@ public:
void pull(const std::vector<std::string>& targets = {});
void push(const std::vector<std::string>& targets = {});
bool match(const std::filesystem::directory_entry& path,
const std::vector<std::string>& patterns);
bool match(const std::string& path, const std::vector<std::string>& patterns);
private:
void pullOrPush(SyncType type, const std::vector<std::string>& targets = {});

2
test/unit/testdotfile.cpp

@ -94,7 +94,7 @@ void testDotfileFilters(const std::unordered_map<std::string, bool>& tests,
createTestDotfiles(fileNames, fileContents);
for (const auto& path : fileNames) {
bool result = Dotfile::the().match(std::filesystem::directory_entry { "/" + path }, testIgnorePatterns);
bool result = Dotfile::the().match("/" + path, testIgnorePatterns);
EXPECT_EQ(result, tests.at(path), printf(" path = '%s'\n", path.c_str()));
}

Loading…
Cancel
Save