Compare commits

...
6 Commits
Author SHA1 Message Date
Riyyi 4cac25b5b3 Manager: Change match() path argument type
Changed the path argument from directory_iterator to string, as nothing
in this function relied on the functionality.
2022-03-08 00:18:04 +01:00
Riyyi f4fe44ad02 Manager: Run clang-format 2022-03-08 00:07:54 +01:00
Riyyi db22cc9f06 Manager: Add new pattern logic to system config files 2022-03-08 00:06:01 +01:00
Riyyi 3b4ea3f42d Manager: Rename filter() -> match()
The function returns true on a pattern match, so reflect this in the
name.
2022-03-08 00:06:01 +01:00
Riyyi 37be9506c8 Manager: Rename pattern and iterator variables
ignorePattern -> pattern
ignoreIterator -> patternIterator
2022-03-08 00:06:01 +01:00
Riyyi ed13b1f6b5 Manager: Add new pattern logic to pulling/pushing targets 2022-03-08 00:05:54 +01:00
7 changed files with 67 additions and 95 deletions
+7 -6
View File
@@ -58,14 +58,15 @@ The ignore patterns from the example config:
**** System config files
Directories in this list will be pushed to the root ~/~ of the system, instead of ~$HOME~.
Everything in this list will be pushed to the root ~/~ of the system, instead of ~$HOME~. \\
These support the same file matching as explained in the [[#ignore-patterns][Ignore patterns]] section.
The system directories from the example config:
The system patterns from the example config:
#+BEGIN_SRC javascript
"systemDirectories": [
"/boot",
"/etc",
"/usr/share"
"systemPatterns": [
"/boot/",
"/etc/",
"/usr/share/"
]
#+END_SRC
+4 -4
View File
@@ -7,9 +7,9 @@
"README.org",
"screenshot.png"
],
"systemDirectories": [
"/boot",
"/etc",
"/usr/share"
"systemPatterns": [
"/boot/",
"/etc/",
"/usr/share/"
]
}
+3 -3
View File
@@ -76,7 +76,7 @@ void to_json(nlohmann::json& object, const Settings& settings)
{
object = nlohmann::json {
{ "ignorePatterns", settings.ignorePatterns },
{ "systemDirectories", settings.systemDirectories }
{ "systemPatterns", settings.systemPatterns }
};
}
@@ -86,7 +86,7 @@ void from_json(const nlohmann::json& object, Settings& settings)
object.at("ignorePatterns").get_to(settings.ignorePatterns);
}
if (object.find("systemDirectories") != object.end()) {
object.at("systemDirectories").get_to(settings.systemDirectories);
if (object.find("systemPatterns") != object.end()) {
object.at("systemPatterns").get_to(settings.systemPatterns);
}
}
+6 -6
View File
@@ -25,10 +25,10 @@ struct Settings {
"README.org",
"screenshot.png",
};
std::vector<std::filesystem::path> systemDirectories {
"/boot",
"/etc",
"/usr/share"
std::vector<std::string> systemPatterns {
"/boot/",
"/etc/",
"/usr/share/"
};
};
@@ -37,12 +37,12 @@ public:
Config(s);
virtual ~Config();
void setSystemDirectories(const std::vector<std::filesystem::path>& systemDirectories) { m_settings.systemDirectories = systemDirectories; }
void setSystemPatterns(const std::vector<std::string>& systemPatterns) { m_settings.systemPatterns = systemPatterns; }
void setIgnorePatterns(const std::vector<std::string>& ignorePatterns) { m_settings.ignorePatterns = ignorePatterns; }
void setVerbose(bool verbose) { m_verbose = verbose; }
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::vector<std::string>& systemPatterns() const { return m_settings.systemPatterns; }
const std::filesystem::path& workingDirectory() const { return m_workingDirectory; }
size_t workingDirectorySize() const { return m_workingDirectorySize; }
+41 -63
View File
@@ -51,7 +51,7 @@ void Dotfile::add(const std::vector<std::string>& targets)
continue;
}
if (isSystemTarget(targets.at(i))) {
if (match(targets.at(i), Config::the().systemPatterns())) {
systemIndices.push_back(i);
}
else {
@@ -106,30 +106,29 @@ void Dotfile::push(const std::vector<std::string>& targets)
pullOrPush(SyncType::Push, targets);
}
bool Dotfile::filter(const std::filesystem::directory_entry& path)
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& ignorePattern : Config::the().ignorePatterns()) {
for (const auto& pattern : patterns) {
if (pathString == ignorePattern) {
if (pathString == pattern) {
return true;
}
// If starts with '/', only match in the working directory root
bool onlyMatchInRoot = false;
if (ignorePattern.front() == '/') {
if (pattern.front() == '/') {
onlyMatchInRoot = true;
}
// If ends with '/', only match directories
bool onlyMatchDirectories = false;
if (ignorePattern.back() == '/') {
if (pattern.back() == '/') {
onlyMatchDirectories = true;
}
@@ -138,7 +137,7 @@ bool Dotfile::filter(const std::filesystem::directory_entry& path)
bool tryPatternState = true;
size_t pathIterator = 0;
size_t ignoreIterator = 0;
size_t patternIterator = 0;
if (!onlyMatchInRoot) {
pathIterator++;
@@ -148,13 +147,13 @@ bool Dotfile::filter(const std::filesystem::directory_entry& path)
// Example, iterator at []: [.]log/output.txt
// [*].log
if (pathIterator < pathString.length()
&& ignoreIterator < ignorePattern.length() - 1
&& ignorePattern.at(ignoreIterator) == '*'
&& pathString.at(pathIterator) == ignorePattern.at(ignoreIterator + 1)) {
ignoreIterator++;
&& patternIterator < pattern.length() - 1
&& pattern.at(patternIterator) == '*'
&& pathString.at(pathIterator) == pattern.at(patternIterator + 1)) {
patternIterator++;
}
for (; pathIterator < pathString.length() && ignoreIterator < ignorePattern.length();) {
for (; pathIterator < pathString.length() && patternIterator < pattern.length();) {
char character = pathString.at(pathIterator);
pathIterator++;
@@ -167,11 +166,11 @@ bool Dotfile::filter(const std::filesystem::directory_entry& path)
continue;
}
if (character == ignorePattern.at(ignoreIterator)) {
if (character == pattern.at(patternIterator)) {
// 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() && ignoreIterator < ignorePattern.length() - 1) {
if (pathIterator == pathString.length() && patternIterator < pattern.length() - 1) {
break;
}
@@ -179,17 +178,17 @@ bool Dotfile::filter(const std::filesystem::directory_entry& path)
// Example, iterator at []: /includ[e]/header.h
// /includ[e]*/
if (pathIterator < pathString.length()
&& ignoreIterator < ignorePattern.length() - 2
&& ignorePattern.at(ignoreIterator + 1) == '*'
&& pathString.at(pathIterator) == ignorePattern.at(ignoreIterator + 2)) {
ignoreIterator++;
&& patternIterator < pattern.length() - 2
&& pattern.at(patternIterator + 1) == '*'
&& pathString.at(pathIterator) == pattern.at(patternIterator + 2)) {
patternIterator++;
}
ignoreIterator++;
patternIterator++;
continue;
}
if (ignorePattern.at(ignoreIterator) == '*') {
if (pattern.at(patternIterator) == '*') {
// 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 +198,9 @@ bool Dotfile::filter(const std::filesystem::directory_entry& path)
// Next path character == next ignore pattern character
if (pathIterator < pathString.length()
&& ignoreIterator + 1 < ignorePattern.length()
&& pathString.at(pathIterator) == ignorePattern.at(ignoreIterator + 1)) {
ignoreIterator++;
&& patternIterator + 1 < pattern.length()
&& pathString.at(pathIterator) == pattern.at(patternIterator + 1)) {
patternIterator++;
}
continue;
@@ -210,20 +209,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 (ignoreIterator < ignorePattern.length() - 1) {
ignoreIterator = 0;
if (patternIterator < pattern.length() - 1) {
patternIterator = 0;
}
tryPatternState = false;
}
if (ignoreIterator == ignorePattern.length()) {
if (patternIterator == pattern.length()) {
return true;
}
if (ignorePattern.back() == '*' && ignoreIterator == ignorePattern.length() - 1) {
if (pattern.back() == '*' && patternIterator == pattern.length() - 1) {
return true;
}
if (onlyMatchDirectories && ignoreIterator == ignorePattern.length() - 1) {
if (onlyMatchDirectories && patternIterator == pattern.length() - 1) {
return true;
}
}
@@ -242,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 (isSystemTarget(path.path().string())) {
if (match(path.path().string(), Config::the().systemPatterns())) {
systemIndices.push_back(index);
}
else {
@@ -485,40 +484,19 @@ 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() }) {
if (path.is_directory() || filter(path)) {
std::string pathString = path.path().string();
if (path.is_directory()) {
continue;
}
if (!targets.empty() && !include(path.path(), targets)) {
// Ignore pattern check
if (match(pathString, Config::the().ignorePatterns())) {
continue;
}
// Include check
if (!targets.empty() && !match(pathString, targets)) {
continue;
}
callback(path, index++);
}
}
bool Dotfile::include(const std::filesystem::path& path, const std::vector<std::string>& targets)
{
for (const auto& target : targets) {
if (path.string().find(Config::the().workingDirectory() / target) == 0) {
return true;
}
}
return false;
}
bool Dotfile::isSystemTarget(const std::string& target)
{
for (const auto& systemDirectory : Config::the().systemDirectories()) {
if (target.find(systemDirectory) == 0) {
return true;
}
// FIXME: The second filesystem::path cant have a "/" as the first character,
// as it will think the path is at the root.
if (target.find(Config::the().workingDirectory().string() + systemDirectory.string()) == 0) {
return true;
}
}
return false;
}
+1 -3
View File
@@ -30,7 +30,7 @@ public:
void pull(const std::vector<std::string>& targets = {});
void push(const std::vector<std::string>& targets = {});
bool filter(const std::filesystem::directory_entry& path);
bool match(const std::string& path, const std::vector<std::string>& patterns);
private:
void pullOrPush(SyncType type, const std::vector<std::string>& targets = {});
@@ -41,8 +41,6 @@ private:
void selectivelyCommentOrUncomment(const std::string& path);
void forEachDotfile(const std::vector<std::string>& targets, const std::function<void(const std::filesystem::directory_entry&, size_t)>& callback);
bool include(const std::filesystem::path& path, const std::vector<std::string>& targets);
bool isSystemTarget(const std::string& target);
};
#endif // DOTFILE_H
+5 -10
View File
@@ -93,16 +93,11 @@ void testDotfileFilters(const std::unordered_map<std::string, bool>& 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().match("/" + path, testIgnorePatterns);
EXPECT_EQ(result, tests.at(path), printf(" path = '%s'\n", path.c_str()));
}
Config::the().setIgnorePatterns(ignorePatterns);
removeTestDotfiles(fileNames, false);
}
@@ -873,9 +868,9 @@ TEST_CASE(AddSystemDotfiles)
{
EXPECT(geteuid() == 0, return );
Config::the().setSystemDirectories({ "/etc", "/usr/lib" });
Config::the().setSystemPatterns({ "/etc/", "/usr/lib/" });
Dotfile::the().add({ "/etc/group", "/usr/lib/os-release" });
Config::the().setSystemDirectories({});
Config::the().setSystemPatterns({});
EXPECT(std::filesystem::exists("etc/group"));
EXPECT(std::filesystem::exists("usr/lib/os-release"));
@@ -890,9 +885,9 @@ TEST_CASE(PullSystemDotfiles)
createTestDotfiles({ "etc/group" }, { "" }, true);
Config::the().setSystemDirectories({ "/etc" });
Config::the().setSystemPatterns({ "/etc/" });
Dotfile::the().pull({ "etc/group" });
Config::the().setSystemDirectories({});
Config::the().setSystemPatterns({});
Util::File lhs("/etc/group");
Util::File rhs(Config::the().workingDirectory() / "etc/group");