aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/tracking/change_tracker.cc26
-rw-r--r--src/tracking/change_tracker.h6
2 files changed, 17 insertions, 15 deletions
diff --git a/src/tracking/change_tracker.cc b/src/tracking/change_tracker.cc
index c32a3c5..74e656f 100644
--- a/src/tracking/change_tracker.cc
+++ b/src/tracking/change_tracker.cc
@@ -30,8 +30,8 @@ boost::process::context getDefaultContext() {
// diff -u --label $file_path - $file_path
//
std::string getDiffCommand(
- const std::string& diff_cmd, const std::string& full_path) {
- return diff_cmd + " --label " + full_path + " - " + full_path;
+ const std::string& diff_cmd, const std::string& file_path) {
+ return diff_cmd + " --label " + file_path + " - " + file_path;
}
}
@@ -74,6 +74,15 @@ bool ChangeTracker::is_tracked(const std::string& file_path) const {
) != this->children_.end();
}
+auto ChangeTracker::create_child(const std::string& file_path) {
+ std::lock_guard<std::mutex> guard(this->write_mutex_);
+
+ return this->children_.emplace(
+ boost::filesystem::canonical(file_path).string(),
+ std::make_unique<std::stringstream>()
+ );
+}
+
// Begins tracking changes to a file reachable by a given path
//
// _Tracking_ consists of adding the full file path to the `children_`
@@ -90,18 +99,7 @@ bool ChangeTracker::is_tracked(const std::string& file_path) const {
// the tracked file is modified after `diff` has been spawned.
//
bool ChangeTracker::track(const std::string& file_path) {
- const std::string full_file_path{
- boost::filesystem::canonical(file_path).string()
- };
-
- std::unique_lock<std::mutex> guard(this->write_mutex_);
-
- auto result = this->children_.emplace(
- full_file_path,
- std::make_unique<std::stringstream>()
- );
-
- guard.unlock();
+ auto result = this->create_child(file_path);
if ( std::get<EMPLACE_SUCCESS>(result) ) {
boost::filesystem::ifstream file(
diff --git a/src/tracking/change_tracker.h b/src/tracking/change_tracker.h
index 05f4021..04a5a00 100644
--- a/src/tracking/change_tracker.h
+++ b/src/tracking/change_tracker.h
@@ -25,9 +25,13 @@ class ChangeTracker {
std::mutex write_mutex_;
std::unordered_map<
- std::string, std::unique_ptr<std::stringstream>
+ std::string,
+ std::unique_ptr<std::stringstream>
> children_;
+ // threadsafe child emplacement
+ auto create_child(const std::string&);
+
};
}