From 6f1f1c3fe71f73fd08bb93d3a0f10e843bad6e11 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sun, 6 Mar 2022 17:21:16 +0100 Subject: [PATCH] Everywhere: Rename "excludePath" to "ignorePattern" --- README.org | 8 +++--- doc/manafiles.json | 2 +- src/config.cpp | 6 ++--- src/config.h | 6 ++--- src/dotfile.cpp | 52 +++++++++++++++++++-------------------- test/unit/testdotfile.cpp | 17 +++++++------ 6 files changed, 47 insertions(+), 44 deletions(-) diff --git a/README.org b/README.org index 553685f..cd7da4e 100644 --- a/README.org +++ b/README.org @@ -29,9 +29,9 @@ From there you can copy it to anywhere in the working directory, the config file is searched recursively. \\ ~$HOME//~ -**** Exclude paths +**** Ignore patterns -Everything in this list will get excluded when pulling/pushing config files from the working directory. \\ +Everything in this list will get ignored when pulling/pushing config files from the working directory. \\ Currently two types of file matching are supported: - ~literal~ matches a file or directory literally @@ -44,9 +44,9 @@ These behave similarly to a ~.gitignore~ pattern. - If the pattern ends with a slash, it matches only directories. When a directory is ignored, \\ all of its files and subdirectories are also ignored. -The excluded paths from the example config: +The ignore patterns from the example config: #+BEGIN_SRC javascript -"excludePaths" : [ +"ignorePatterns" : [ ".git/", "*.md", "manafiles.json", diff --git a/doc/manafiles.json b/doc/manafiles.json index 565575b..8103dc5 100644 --- a/doc/manafiles.json +++ b/doc/manafiles.json @@ -1,5 +1,5 @@ { - "excludePaths" : [ + "ignorePatterns" : [ ".git/", "*.md", "manafiles.json", diff --git a/src/config.cpp b/src/config.cpp index cc9f682..951b9af 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -75,15 +75,15 @@ void Config::parseConfigFile() void to_json(nlohmann::json& object, const Settings& settings) { object = nlohmann::json { - { "excludePaths", settings.excludePaths }, + { "ignorePatterns", settings.ignorePatterns }, { "systemDirectories", settings.systemDirectories } }; } void from_json(const nlohmann::json& object, Settings& settings) { - if (object.find("excludePaths") != object.end()) { - object.at("excludePaths").get_to(settings.excludePaths); + if (object.find("ignorePatterns") != object.end()) { + object.at("ignorePatterns").get_to(settings.ignorePatterns); } if (object.find("systemDirectories") != object.end()) { diff --git a/src/config.h b/src/config.h index f391dad..785f03b 100644 --- a/src/config.h +++ b/src/config.h @@ -17,7 +17,7 @@ #include "util/singleton.h" struct Settings { - std::vector excludePaths { + std::vector ignorePatterns { ".git/", "*.md", "manafiles.json", @@ -38,10 +38,10 @@ public: virtual ~Config(); void setSystemDirectories(const std::vector& systemDirectories) { m_settings.systemDirectories = systemDirectories; } - void setExcludePaths(const std::vector& excludePaths) { m_settings.excludePaths = excludePaths; } + void setIgnorePatterns(const std::vector& ignorePatterns) { m_settings.ignorePatterns = ignorePatterns; } void setVerbose(bool verbose) { m_verbose = verbose; } - const std::vector& excludePaths() const { return m_settings.excludePaths; } + const std::vector& ignorePatterns() const { return m_settings.ignorePatterns; } const std::vector& systemDirectories() const { return m_settings.systemDirectories; } const std::filesystem::path& workingDirectory() const { return m_workingDirectory; } diff --git a/src/dotfile.cpp b/src/dotfile.cpp index b2caa5b..994029d 100644 --- a/src/dotfile.cpp +++ b/src/dotfile.cpp @@ -115,21 +115,21 @@ 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& excludePath : Config::the().excludePaths()) { + for (const auto& ignorePattern : Config::the().ignorePatterns()) { - if (pathString == excludePath) { + if (pathString == ignorePattern) { return true; } // If starts with '/', only match in the working directory root bool onlyMatchInRoot = false; - if (excludePath.front() == '/') { + if (ignorePattern.front() == '/') { onlyMatchInRoot = true; } // If ends with '/', only match directories bool onlyMatchDirectories = false; - if (excludePath.back() == '/') { + if (ignorePattern.back() == '/') { onlyMatchDirectories = true; } @@ -138,7 +138,7 @@ bool Dotfile::filter(const std::filesystem::directory_entry& path) bool tryPatternState = true; size_t pathIterator = 0; - size_t excludeIterator = 0; + size_t ignoreIterator = 0; if (!onlyMatchInRoot) { pathIterator++; @@ -148,13 +148,13 @@ bool Dotfile::filter(const std::filesystem::directory_entry& path) // Example, iterator at []: [.]log/output.txt // [*].log if (pathIterator < pathString.length() - && excludeIterator < excludePath.length() - 1 - && excludePath.at(excludeIterator) == '*' - && pathString.at(pathIterator) == excludePath.at(excludeIterator + 1)) { - excludeIterator++; + && ignoreIterator < ignorePattern.length() - 1 + && ignorePattern.at(ignoreIterator) == '*' + && pathString.at(pathIterator) == ignorePattern.at(ignoreIterator + 1)) { + ignoreIterator++; } - for (; pathIterator < pathString.length() && excludeIterator < excludePath.length();) { + for (; pathIterator < pathString.length() && ignoreIterator < ignorePattern.length();) { char character = pathString.at(pathIterator); pathIterator++; @@ -167,11 +167,11 @@ bool Dotfile::filter(const std::filesystem::directory_entry& path) continue; } - if (character == excludePath.at(excludeIterator)) { + if (character == ignorePattern.at(ignoreIterator)) { // Fail if the final match hasn't reached the end of the ignore pattern // Example, iterator at []: doc/buil[d] // buil[d]/ - if (pathIterator == pathString.length() && excludeIterator < excludePath.length() - 1) { + if (pathIterator == pathString.length() && ignoreIterator < ignorePattern.length() - 1) { break; } @@ -179,17 +179,17 @@ bool Dotfile::filter(const std::filesystem::directory_entry& path) // Example, iterator at []: /includ[e]/header.h // /includ[e]*/ if (pathIterator < pathString.length() - && excludeIterator < excludePath.length() - 2 - && excludePath.at(excludeIterator + 1) == '*' - && pathString.at(pathIterator) == excludePath.at(excludeIterator + 2)) { - excludeIterator++; + && ignoreIterator < ignorePattern.length() - 2 + && ignorePattern.at(ignoreIterator + 1) == '*' + && pathString.at(pathIterator) == ignorePattern.at(ignoreIterator + 2)) { + ignoreIterator++; } - excludeIterator++; + ignoreIterator++; continue; } - if (excludePath.at(excludeIterator) == '*') { + if (ignorePattern.at(ignoreIterator) == '*') { // Fail if we're entering a subdirectory and we should only match in the root // Example, iterator at []: /src[/]include/header.h // /[*]include/ @@ -199,9 +199,9 @@ bool Dotfile::filter(const std::filesystem::directory_entry& path) // Next path character == next ignore pattern character if (pathIterator < pathString.length() - && excludeIterator + 1 < excludePath.length() - && pathString.at(pathIterator) == excludePath.at(excludeIterator + 1)) { - excludeIterator++; + && ignoreIterator + 1 < ignorePattern.length() + && pathString.at(pathIterator) == ignorePattern.at(ignoreIterator + 1)) { + ignoreIterator++; } continue; @@ -210,20 +210,20 @@ bool Dotfile::filter(const std::filesystem::directory_entry& path) // Reset filter pattern if it hasnt been completed at this point // Example, iterator at []: /[s]rc/include/header.h // /[i]nclude*/ - if (excludeIterator < excludePath.length() - 1) { - excludeIterator = 0; + if (ignoreIterator < ignorePattern.length() - 1) { + ignoreIterator = 0; } tryPatternState = false; } - if (excludeIterator == excludePath.length()) { + if (ignoreIterator == ignorePattern.length()) { return true; } - if (excludePath.back() == '*' && excludeIterator == excludePath.length() - 1) { + if (ignorePattern.back() == '*' && ignoreIterator == ignorePattern.length() - 1) { return true; } - if (onlyMatchDirectories && excludeIterator == excludePath.length() - 1) { + if (onlyMatchDirectories && ignoreIterator == ignorePattern.length() - 1) { return true; } } diff --git a/test/unit/testdotfile.cpp b/test/unit/testdotfile.cpp index 6c408dc..5f1fb4f 100644 --- a/test/unit/testdotfile.cpp +++ b/test/unit/testdotfile.cpp @@ -82,7 +82,7 @@ void removeTestDotfiles(const std::vector& files, bool deleteInHome } void testDotfileFilters(const std::unordered_map& tests, - const std::vector& testFilters) + const std::vector& testIgnorePatterns) { std::vector fileNames; std::vector fileContents; @@ -93,15 +93,15 @@ void testDotfileFilters(const std::unordered_map& tests, createTestDotfiles(fileNames, fileContents); - auto excludePaths = Config::the().excludePaths(); - Config::the().setExcludePaths(testFilters); + auto ignorePatterns = Config::the().ignorePatterns(); + Config::the().setIgnorePatterns(testIgnorePatterns); for (const auto& path : fileNames) { bool result = Dotfile::the().filter(std::filesystem::directory_entry { "/" + path }); EXPECT_EQ(result, tests.at(path), printf(" path = '%s'\n", path.c_str())); } - Config::the().setExcludePaths(excludePaths); + Config::the().setIgnorePatterns(ignorePatterns); removeTestDotfiles(fileNames, false); } @@ -476,7 +476,7 @@ TEST_CASE(PushDotfiles) removeTestDotfiles(fileNames); } -TEST_CASE(PushDotfilesWithExcludePath) +TEST_CASE(PushDotfilesWithIgnorePattern) { std::vector fileNames = { "__test-file-1", @@ -487,13 +487,16 @@ TEST_CASE(PushDotfilesWithExcludePath) createTestDotfiles(fileNames, { "", "", "", "" }); - Config::the().setExcludePaths({ + auto ignorePatterns = Config::the().ignorePatterns(); + Config::the().setIgnorePatterns({ "__test-file-1", "__subdir/", "*.test", }); + Dotfile::the().push(fileNames); - Config::the().setExcludePaths({}); + + Config::the().setIgnorePatterns(ignorePatterns); for (const auto& file : fileNames) { EXPECT(!std::filesystem::exists(homeDirectory / file));