Browse Source

Manager: Add new pattern logic to pulling/pushing targets

master
Riyyi 3 years ago
parent
commit
ed13b1f6b5
  1. 25
      src/dotfile.cpp
  2. 4
      src/dotfile.h
  3. 7
      test/unit/testdotfile.cpp

25
src/dotfile.cpp

@ -106,7 +106,8 @@ void Dotfile::push(const std::vector<std::string>& 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<std::string>& 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<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)) {
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<std::string>& 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)

4
src/dotfile.h

@ -30,7 +30,8 @@ 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 filter(const std::filesystem::directory_entry& path,
const std::vector<std::string>& patterns);
private:
void pullOrPush(SyncType type, const std::vector<std::string>& targets = {});
@ -41,7 +42,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);
};

7
test/unit/testdotfile.cpp

@ -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().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);
}

Loading…
Cancel
Save