From b37a8087e9183bac73824be348ed0333f280e7f5 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sun, 6 Mar 2022 17:10:08 +0100 Subject: [PATCH] Everywhere: Change exclude path configuration formatting --- README.org | 32 +++++++------ doc/manafiles.json | 16 +++---- src/config.cpp | 8 ---- src/config.h | 18 +++---- src/dotfile.cpp | 3 +- test/unit/testdotfile.cpp | 98 +++++++++++++++++++++------------------ 6 files changed, 91 insertions(+), 84 deletions(-) diff --git a/README.org b/README.org index 0d33178..553685f 100644 --- a/README.org +++ b/README.org @@ -31,23 +31,29 @@ the config file is searched recursively. \\ **** Exclude paths -Everything in this list will get excluded when pushing config files from the working directory to the system. \\ -Currently three types of file matching are supported: +Everything in this list will get excluded when pulling/pushing config files from the working directory. \\ +Currently two types of file matching are supported: -- ~file~ full name of the file matches -- ~directory~ full name of the directory matches -- ~endsWith~ end of the path matches +- ~literal~ matches a file or directory literally +- ~wildcard~ the asterisk matches zero or more characters + +These behave similarly to a ~.gitignore~ pattern. + +- If the pattern starts with a slash, it matches files and directories in the working directory root only. +- If the pattern doesn’t start with a slash, it matches files and directories in any directory or subdirectory. +- 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: #+BEGIN_SRC javascript -"excludePaths" : { - ".git": "directory", - ".md": "endsWith", - "manafiles.json": "endsWith", - "packages": "file", - "README.org": "endsWith", - "screenshot.png": "file" -} +"excludePaths" : [ + ".git/", + "*.md", + "manafiles.json", + "packages", + "README.org", + "screenshot.png" +] #+END_SRC **** System config files diff --git a/doc/manafiles.json b/doc/manafiles.json index 17e65a7..565575b 100644 --- a/doc/manafiles.json +++ b/doc/manafiles.json @@ -1,12 +1,12 @@ { - "excludePaths" : { - ".git": "directory", - ".md": "endsWith", - "manafiles.json": "endsWith", - "packages": "file", - "README.org": "endsWith", - "screenshot.png": "file" - }, + "excludePaths" : [ + ".git/", + "*.md", + "manafiles.json", + "packages", + "README.org", + "screenshot.png" + ], "systemDirectories": [ "/boot", "/etc", diff --git a/src/config.cpp b/src/config.cpp index 198de57..cc9f682 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -89,12 +89,4 @@ void from_json(const nlohmann::json& object, Settings& settings) if (object.find("systemDirectories") != object.end()) { object.at("systemDirectories").get_to(settings.systemDirectories); } - - // Check for correct exclude type values - for (const auto& path : settings.excludePaths) { - if (path.second != "file" && path.second != "directory" && path.second != "endsWith") { - fprintf(stderr, "\033[31;1mConfig:\033[0m invalid exclude type '%s'\n", path.second.c_str()); - raise(SIGABRT); - } - } } diff --git a/src/config.h b/src/config.h index b487778..f391dad 100644 --- a/src/config.h +++ b/src/config.h @@ -9,7 +9,6 @@ #include // size_t #include // path -#include #include #include @@ -18,12 +17,13 @@ #include "util/singleton.h" struct Settings { - std::map excludePaths { - { ".git", "directory" }, - { ".md", "endsWith" }, - { "packages", "file" }, - { "README.org", "endsWith" }, - { "screenshot.png", "file" }, + std::vector excludePaths { + ".git/", + "*.md", + "manafiles.json", + "packages", + "README.org", + "screenshot.png", }; std::vector systemDirectories { "/boot", @@ -38,10 +38,10 @@ public: virtual ~Config(); void setSystemDirectories(const std::vector& systemDirectories) { m_settings.systemDirectories = systemDirectories; } - void setExcludePaths(const std::map& excludePaths) { m_settings.excludePaths = excludePaths; } + void setExcludePaths(const std::vector& excludePaths) { m_settings.excludePaths = excludePaths; } void setVerbose(bool verbose) { m_verbose = verbose; } - const std::map& excludePaths() const { return m_settings.excludePaths; } + const std::vector& excludePaths() const { return m_settings.excludePaths; } 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 328e270..b2caa5b 100644 --- a/src/dotfile.cpp +++ b/src/dotfile.cpp @@ -115,8 +115,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& excludePathMapEntry : Config::the().excludePaths()) { - const auto& excludePath = excludePathMapEntry.first; + for (const auto& excludePath : Config::the().excludePaths()) { if (pathString == excludePath) { return true; diff --git a/test/unit/testdotfile.cpp b/test/unit/testdotfile.cpp index e649d96..6c408dc 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::unordered_map& testFilters) + const std::vector& testFilters) { std::vector fileNames; std::vector fileContents; @@ -117,8 +117,8 @@ TEST_CASE(DotfilesLiteralIgnoreAllFiles) { "error.log", false }, }; - std::unordered_map testFilters = { - { "access.log", "" }, + std::vector testFilters = { + "access.log", }; testDotfileFilters(tests, testFilters); @@ -133,8 +133,8 @@ TEST_CASE(DotfilesLiteralIgnoreFileInRoot) { "error.log", false }, }; - std::unordered_map testFilters = { - { "/access.log", "" }, + std::vector testFilters = { + "/access.log", }; testDotfileFilters(tests, testFilters); @@ -147,14 +147,15 @@ TEST_CASE(DotfilesLiteralIgnoreDirectories) { "doc/build", false }, }; - std::unordered_map testFilters = { - { "build/", "" }, + std::vector testFilters = { + "build/", }; testDotfileFilters(tests, testFilters); } -TEST_CASE(DotfilesWildcardIgnoreAllFilesWithExtension) { +TEST_CASE(DotfilesWildcardIgnoreAllFilesWithExtension) +{ std::unordered_map tests = { { "error.log", true }, { "logs/debug.log", true }, @@ -164,14 +165,15 @@ TEST_CASE(DotfilesWildcardIgnoreAllFilesWithExtension) { { "log.txt", false }, }; - std::unordered_map testFilters = { - { "*.log", "" }, + std::vector testFilters = { + "*.log", }; testDotfileFilters(tests, testFilters); } -TEST_CASE(DotfilesWildcardIgnoreAllFilesInRootWithExtension) { +TEST_CASE(DotfilesWildcardIgnoreAllFilesInRootWithExtension) +{ std::unordered_map tests = { { "error.log", true }, { "logs/debug.log", false }, @@ -181,14 +183,15 @@ TEST_CASE(DotfilesWildcardIgnoreAllFilesInRootWithExtension) { { "log.txt", false }, }; - std::unordered_map testFilters = { - { "/*.log", "" }, + std::vector testFilters = { + "/*.log", }; testDotfileFilters(tests, testFilters); } -TEST_CASE(DotfilesWildcardIgnoreFileWithAllExtensions) { +TEST_CASE(DotfilesWildcardIgnoreFileWithAllExtensions) +{ std::unordered_map tests = { { "README.md", true }, { "doc/README.org", true }, @@ -198,14 +201,15 @@ TEST_CASE(DotfilesWildcardIgnoreFileWithAllExtensions) { { "Config.org", false }, }; - std::unordered_map testFilters = { - { "README.*", "" }, + std::vector testFilters = { + "README.*", }; testDotfileFilters(tests, testFilters); } -TEST_CASE(DotfilesWildcardIgnoreAllWithStartingPattern) { +TEST_CASE(DotfilesWildcardIgnoreAllWithStartingPattern) +{ std::unordered_map tests = { { "cmake/uninstall.cmake.in", true }, { "cmake/templates/template.cmake.in", true }, @@ -214,14 +218,15 @@ TEST_CASE(DotfilesWildcardIgnoreAllWithStartingPattern) { { "CMakeLists.txt", false }, }; - std::unordered_map testFilters = { - { "cmake*", "" }, + std::vector testFilters = { + "cmake*", }; testDotfileFilters(tests, testFilters); } -TEST_CASE(DotfilesWildcardIgnoreAllInRootWithStartingPattern) { +TEST_CASE(DotfilesWildcardIgnoreAllInRootWithStartingPattern) +{ std::unordered_map tests = { { "project-directory/README.org", true }, { "project-directory/build/executable", true }, @@ -229,28 +234,30 @@ TEST_CASE(DotfilesWildcardIgnoreAllInRootWithStartingPattern) { { "doc/project-instructions.txt", false }, }; - std::unordered_map testFilters = { - { "/project*", "" }, + std::vector testFilters = { + "/project*", }; testDotfileFilters(tests, testFilters); } -TEST_CASE(DotfilesWildcardIgnoreAllInDirectory) { +TEST_CASE(DotfilesWildcardIgnoreAllInDirectory) +{ std::unordered_map tests = { { "build/x32/executable", true }, { "build/x64/executable", true }, { "build/executable", true }, }; - std::unordered_map testFilters = { - { "build/*", "" }, + std::vector testFilters = { + "build/*", }; testDotfileFilters(tests, testFilters); } -TEST_CASE(DotfilesWildcardIgnoreFileInSubDirectory) { +TEST_CASE(DotfilesWildcardIgnoreFileInSubDirectory) +{ std::unordered_map tests = { { "build/x32/executable", true }, { "build/x64/executable", true }, @@ -259,14 +266,15 @@ TEST_CASE(DotfilesWildcardIgnoreFileInSubDirectory) { { "build/executable", false }, }; - std::unordered_map testFilters = { - { "build/*/executable", "" }, + std::vector testFilters = { + "build/*/executable", }; testDotfileFilters(tests, testFilters); } -TEST_CASE(DotfilesWildcardIgnoreAllInSubDirectory) { +TEST_CASE(DotfilesWildcardIgnoreAllInSubDirectory) +{ std::unordered_map tests = { { "build/x32/executable", true }, { "build/x64/executable", true }, @@ -275,14 +283,15 @@ TEST_CASE(DotfilesWildcardIgnoreAllInSubDirectory) { { "build/executable", false }, }; - std::unordered_map testFilters = { - { "build/*/*", "" }, + std::vector testFilters = { + "build/*/*", }; testDotfileFilters(tests, testFilters); } -TEST_CASE(DotfilesWildcardIgnoreAllDirectoriesWithStartingPattern) { +TEST_CASE(DotfilesWildcardIgnoreAllDirectoriesWithStartingPattern) +{ std::unordered_map tests = { { "include/header.h", true }, { "include-dependency/header.h", true }, @@ -290,14 +299,15 @@ TEST_CASE(DotfilesWildcardIgnoreAllDirectoriesWithStartingPattern) { { "src/include/header.h", true }, }; - std::unordered_map testFilters = { - { "include*/", "" }, + std::vector testFilters = { + "include*/", }; testDotfileFilters(tests, testFilters); } -TEST_CASE(DotfilesWildcardIgnoreAllDirectoriesInRootWithStartingPattern) { +TEST_CASE(DotfilesWildcardIgnoreAllDirectoriesInRootWithStartingPattern) +{ std::unordered_map tests = { { "include/header.h", true }, { "include-dependency/header.h", true }, @@ -305,8 +315,8 @@ TEST_CASE(DotfilesWildcardIgnoreAllDirectoriesInRootWithStartingPattern) { { "src/include/header.h", false }, }; - std::unordered_map testFilters = { - { "/include*/", "" }, + std::vector testFilters = { + "/include*/", }; testDotfileFilters(tests, testFilters); @@ -321,8 +331,8 @@ TEST_CASE(DotfilesWildcardIgnoreAllDirectoriesWithEndingPattern) { "src/include/header.h", true }, }; - std::unordered_map testFilters = { - { "*include/", "" }, + std::vector testFilters = { + "*include/", }; testDotfileFilters(tests, testFilters); @@ -337,8 +347,8 @@ TEST_CASE(DotfilesWildcardIgnoreAllDirectoriesInRootWithEndingPattern) { "src/include/header.h", false }, }; - std::unordered_map testFilters = { - { "/*include/", "" }, + std::vector testFilters = { + "/*include/", }; testDotfileFilters(tests, testFilters); @@ -478,9 +488,9 @@ TEST_CASE(PushDotfilesWithExcludePath) createTestDotfiles(fileNames, { "", "", "", "" }); Config::the().setExcludePaths({ - { "__test-file-1", "file" }, - { "__subdir/", "directory" }, - { "*.test", "endsWith" }, + "__test-file-1", + "__subdir/", + "*.test", }); Dotfile::the().push(fileNames); Config::the().setExcludePaths({});