From ed13b1f6b53fc5b47e87da8cd6f2153117e69b0c Mon Sep 17 00:00:00 2001 From: Riyyi Date: Mon, 7 Mar 2022 22:33:53 +0100 Subject: [PATCH] Manager: Add new pattern logic to pulling/pushing targets --- src/dotfile.cpp | 25 ++++++++++--------------- src/dotfile.h | 4 ++-- test/unit/testdotfile.cpp | 7 +------ 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/src/dotfile.cpp b/src/dotfile.cpp index 994029d..67c5c34 100644 --- a/src/dotfile.cpp +++ b/src/dotfile.cpp @@ -106,7 +106,8 @@ void Dotfile::push(const std::vector& targets) pullOrPush(SyncType::Push, targets); } -bool Dotfile::filter(const std::filesystem::directory_entry& path) +bool Dotfile::filter(const std::filesystem::directory_entry& path, + const std::vector& patterns) { std::string pathString = path.path().string(); assert(pathString.front() == '/'); @@ -115,7 +116,7 @@ bool Dotfile::filter(const std::filesystem::directory_entry& path) size_t cutFrom = pathString.find(Config::the().workingDirectory()) == 0 ? Config::the().workingDirectorySize() : 0; pathString = pathString.substr(cutFrom); - for (const auto& ignorePattern : Config::the().ignorePatterns()) { + for (const auto& ignorePattern : patterns) { if (pathString == ignorePattern) { return true; @@ -485,25 +486,19 @@ void Dotfile::forEachDotfile(const std::vector& targets, const std: { size_t index = 0; for (const auto& path : std::filesystem::recursive_directory_iterator { Config::the().workingDirectory() }) { - if (path.is_directory() || filter(path)) { + if (path.is_directory()) { continue; } - if (!targets.empty() && !include(path.path(), targets)) { + // Ignore pattern check + if (filter(path, Config::the().ignorePatterns())) { continue; } - callback(path, index++); - } -} - -bool Dotfile::include(const std::filesystem::path& path, const std::vector& targets) -{ - for (const auto& target : targets) { - if (path.string().find(Config::the().workingDirectory() / target) == 0) { - return true; + // Include check + if (!targets.empty() && !filter(path, targets)) { + continue; } + callback(path, index++); } - - return false; } bool Dotfile::isSystemTarget(const std::string& target) diff --git a/src/dotfile.h b/src/dotfile.h index 036594a..449df01 100644 --- a/src/dotfile.h +++ b/src/dotfile.h @@ -30,7 +30,8 @@ public: void pull(const std::vector& targets = {}); void push(const std::vector& targets = {}); - bool filter(const std::filesystem::directory_entry& path); + bool filter(const std::filesystem::directory_entry& path, + const std::vector& patterns); private: void pullOrPush(SyncType type, const std::vector& targets = {}); @@ -41,7 +42,6 @@ private: void selectivelyCommentOrUncomment(const std::string& path); void forEachDotfile(const std::vector& targets, const std::function& callback); - bool include(const std::filesystem::path& path, const std::vector& targets); bool isSystemTarget(const std::string& target); }; diff --git a/test/unit/testdotfile.cpp b/test/unit/testdotfile.cpp index 5952f02..2fe8a2a 100644 --- a/test/unit/testdotfile.cpp +++ b/test/unit/testdotfile.cpp @@ -93,16 +93,11 @@ void testDotfileFilters(const std::unordered_map& tests, createTestDotfiles(fileNames, fileContents); - auto ignorePatterns = Config::the().ignorePatterns(); - Config::the().setIgnorePatterns(testIgnorePatterns); - for (const auto& path : fileNames) { - bool result = Dotfile::the().filter(std::filesystem::directory_entry { "/" + path }); + bool result = Dotfile::the().filter(std::filesystem::directory_entry { "/" + path }, testIgnorePatterns); EXPECT_EQ(result, tests.at(path), printf(" path = '%s'\n", path.c_str())); } - Config::the().setIgnorePatterns(ignorePatterns); - removeTestDotfiles(fileNames, false); }