Browse Source

Everywhere: Rename "excludePath" to "ignorePattern"

master
Riyyi 3 years ago
parent
commit
6f1f1c3fe7
  1. 8
      README.org
  2. 2
      doc/manafiles.json
  3. 6
      src/config.cpp
  4. 6
      src/config.h
  5. 52
      src/dotfile.cpp
  6. 17
      test/unit/testdotfile.cpp

8
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/<dotfiles>/<anywhere>~
**** 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",

2
doc/manafiles.json

@ -1,5 +1,5 @@
{
"excludePaths" : [
"ignorePatterns" : [
".git/",
"*.md",
"manafiles.json",

6
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()) {

6
src/config.h

@ -17,7 +17,7 @@
#include "util/singleton.h"
struct Settings {
std::vector<std::string> excludePaths {
std::vector<std::string> ignorePatterns {
".git/",
"*.md",
"manafiles.json",
@ -38,10 +38,10 @@ public:
virtual ~Config();
void setSystemDirectories(const std::vector<std::filesystem::path>& systemDirectories) { m_settings.systemDirectories = systemDirectories; }
void setExcludePaths(const std::vector<std::string>& excludePaths) { m_settings.excludePaths = excludePaths; }
void setIgnorePatterns(const std::vector<std::string>& ignorePatterns) { m_settings.ignorePatterns = ignorePatterns; }
void setVerbose(bool verbose) { m_verbose = verbose; }
const std::vector<std::string>& excludePaths() const { return m_settings.excludePaths; }
const std::vector<std::string>& ignorePatterns() const { return m_settings.ignorePatterns; }
const std::vector<std::filesystem::path>& systemDirectories() const { return m_settings.systemDirectories; }
const std::filesystem::path& workingDirectory() const { return m_workingDirectory; }

52
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;
}
}

17
test/unit/testdotfile.cpp

@ -82,7 +82,7 @@ void removeTestDotfiles(const std::vector<std::string>& files, bool deleteInHome
}
void testDotfileFilters(const std::unordered_map<std::string, bool>& tests,
const std::vector<std::string>& testFilters)
const std::vector<std::string>& testIgnorePatterns)
{
std::vector<std::string> fileNames;
std::vector<std::string> fileContents;
@ -93,15 +93,15 @@ void testDotfileFilters(const std::unordered_map<std::string, bool>& 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<std::string> 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));

Loading…
Cancel
Save