From 1ffaf37388cd691e88196b6e00455eda0e652cf0 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sun, 14 Feb 2016 20:52:29 +0100 Subject: Implement support for excluding arbitrary paths from tracking The library may be provided with a new-line separated list of regular expressions via the newly introduced `CHANGE_LOG_IGNORE_PATTERN_PATH`. Any proposed tracking path that is matched by any of the provided patterns is excluded from change reporting. This functionality uses the Standard's regular expression parsing functionality and as such doesn't introduce any new dependencies. If no file path is provided or the provided file path is unreadable all paths will be tracked. `change` was adapted to set `CHANGE_LOG_IGNORE_PATTERN_PATH` to `.change_log_ignore` which means that it will by default exclude any patterns provided via this file in the current working directory. An example for such a file customized for hiding _vim_'s internal write logic may look as follows: [0-9]+ [^~]*~ [.*\.viminfo .*\.swp Note that this is implemented in a fashion where it is not guaranteed that the full _canonical_ path is checked against the patterns. It remains to be decided if this is enough for all common use cases of this new functionality. `tracking::PathMatcher` lacks any explicit thread synchronization - according to my current knowledge this should not be necessary as we are only ever reading the private `std::vector` instance. If invalid regular expressions are provided they are silently ignored. --- src/tracking/path_matcher.cc | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/tracking/path_matcher.cc (limited to 'src/tracking/path_matcher.cc') diff --git a/src/tracking/path_matcher.cc b/src/tracking/path_matcher.cc new file mode 100644 index 0000000..e754586 --- /dev/null +++ b/src/tracking/path_matcher.cc @@ -0,0 +1,39 @@ +#include "path_matcher.h" + +#include + +#include +#include + +namespace tracking { + +PathMatcher::PathMatcher(const std::string& source_file_path) { + try { + boost::filesystem::ifstream file(source_file_path); + + if ( file.is_open() ) { + std::string current_line; + + while ( std::getline(file, current_line) ) { + try { + this->patterns_.emplace_back(current_line); + } catch ( const std::regex_error& ) { } + } + } + } catch ( boost::filesystem::filesystem_error& ) { + // invalid source path is not relevant as we can easily fall back to declining + // all candidate paths. + } +} + +bool PathMatcher::isMatching(const std::string& candidate) const { + return std::any_of( + this->patterns_.begin(), + this->patterns_.end(), + [&candidate](const std::regex& pattern) -> bool { + return std::regex_match(candidate, pattern); + } + ); +} + +} -- cgit v1.2.3