Browse Source

Simplified Git::get_repository

merge-requests/365/head
eidheim 9 years ago
parent
commit
6d56df787e
  1. 33
      src/git.cc
  2. 3
      src/git.h

33
src/git.cc

@ -3,8 +3,6 @@
bool Git::initialized=false; bool Git::initialized=false;
std::mutex Git::mutex; std::mutex Git::mutex;
std::unordered_map<std::string, std::pair<std::unique_ptr<Git::Repository>, size_t> > Git::repositories;
std::mutex Git::repositories_mutex;
std::string Git::Error::message() noexcept { std::string Git::Error::message() noexcept {
const git_error *last_error = giterr_last(); const git_error *last_error = giterr_last();
@ -262,25 +260,18 @@ void Git::initialize() noexcept {
std::shared_ptr<Git::Repository> Git::get_repository(const boost::filesystem::path &path) { std::shared_ptr<Git::Repository> Git::get_repository(const boost::filesystem::path &path) {
initialize(); initialize();
std::lock_guard<std::mutex> lock(repositories_mutex); static std::unordered_map<std::string, std::weak_ptr<Git::Repository> > cache;
auto root_path=std::make_shared<std::string>(Repository::get_root_path(path).generic_string()); static std::mutex mutex;
auto it=repositories.find(*root_path);
Repository *repository_ptr; std::lock_guard<std::mutex> lock(mutex);
if(it!=repositories.end()) { auto root_path=Repository::get_root_path(path).generic_string();
it->second.second++; auto it=cache.find(root_path);
repository_ptr=it->second.first.get(); if(it==cache.end())
} it=cache.emplace(root_path, std::weak_ptr<Git::Repository>()).first;
else { auto instance=it->second.lock();
it=repositories.emplace(*root_path, std::make_pair(std::unique_ptr<Repository>(new Repository(*root_path)), 1)).first; if(!instance)
repository_ptr=it->second.first.get(); it->second=instance=std::shared_ptr<Repository>(new Repository(root_path));
} return instance;
return std::shared_ptr<Repository>(repository_ptr, [root_path](Repository *) {
std::lock_guard<std::mutex> lock(repositories_mutex);
auto it=repositories.find(*root_path);
it->second.second--;
if(it->second.second==0)
repositories.erase(it);
});
} }
boost::filesystem::path Git::path(const char *cpath, size_t cpath_length) noexcept { boost::filesystem::path Git::path(const char *cpath, size_t cpath_length) noexcept {

3
src/git.h

@ -88,9 +88,6 @@ private:
///Mutex for thread safe operations ///Mutex for thread safe operations
static std::mutex mutex; static std::mutex mutex;
static std::unordered_map<std::string, std::pair<std::unique_ptr<Git::Repository>, size_t> > repositories;
static std::mutex repositories_mutex;
///Call initialize in public static methods ///Call initialize in public static methods
static void initialize() noexcept; static void initialize() noexcept;

Loading…
Cancel
Save