<commit_before>#include "stdafx.h"
#include "StatusCache.h"

StatusCache::StatusCache()
{
	m_directoryMonitor = std::make_unique<DirectoryMonitor>(
		[this](const boost::filesystem::path& path, DirectoryMonitor::FileAction action) { this->OnFileChanged(path, action); },
		[this] { this->InvalidateAllCacheEntries(); });

}

StatusCache::~StatusCache()
{
}

void StatusCache::OnFileChanged(const boost::filesystem::path& path, DirectoryMonitor::FileAction action)
{
	auto pathToSearch = path;
	if (path.has_filename())
	{
		pathToSearch = path.parent_path();
	}
	auto repositoryPath = m_git.DiscoverRepository(pathToSearch.c_str());
	if (std::get<0>(repositoryPath))
	{
		auto invalidatedEntry = this->InvalidateCacheEntry(std::get<1>(repositoryPath));
		if (invalidatedEntry)
		{
			Log("StatusCache.OnFileChanged.InvalidatedCacheEntry", Severity::Info)
				<< LR"(Invalidated git status in cache for file change. { "repositoryPath": ")" << std::get<1>(repositoryPath)
				<< LR"(", "filePath": ")" << path.c_str()
				<< LR"(" })";
		}
	}
}

void StatusCache::MonitorRepositoryDirectories(const Git::Status& status)
{
	auto workingDirectory = status.WorkingDirectory;
	if (!workingDirectory.empty())
	{
		m_directoryMonitor->AddDirectory(workingDirectory);
	}

	auto repositoryPath = status.RepositoryPath;
	if (!repositoryPath.empty())
	{
		if (workingDirectory.empty() || repositoryPath.find(workingDirectory) != 0)
		{<commit_msg>Check if file exists when handing file change notifications.<commit_after>#include "stdafx.h"
#include "StatusCache.h"
#include <boost/filesystem/operations.hpp>

StatusCache::StatusCache()
{
	m_directoryMonitor = std::make_unique<DirectoryMonitor>(
		[this](const boost::filesystem::path& path, DirectoryMonitor::FileAction action) { this->OnFileChanged(path, action); },
		[this] { this->InvalidateAllCacheEntries(); });

}

StatusCache::~StatusCache()
{
}

void StatusCache::OnFileChanged(const boost::filesystem::path& path, DirectoryMonitor::FileAction action)
{
	auto pathToSearch = path;
	if (path.has_filename())
	{
		pathToSearch = path.parent_path();
	}
	
	while (!boost::filesystem::exists(pathToSearch))
	{
		if (!pathToSearch.has_parent_path())
			return;
		pathToSearch = pathToSearch.parent_path();
	}

	auto repositoryPath = m_git.DiscoverRepository(pathToSearch.c_str());
	if (std::get<0>(repositoryPath))
	{
		auto invalidatedEntry = this->InvalidateCacheEntry(std::get<1>(repositoryPath));
		if (invalidatedEntry)
		{
			Log("StatusCache.OnFileChanged.InvalidatedCacheEntry", Severity::Info)
				<< LR"(Invalidated git status in cache for file change. { "repositoryPath": ")" << std::get<1>(repositoryPath)
				<< LR"(", "filePath": ")" << path.c_str()
				<< LR"(" })";
		}
	}
}

void StatusCache::MonitorRepositoryDirectories(const Git::Status& status)
{
	auto workingDirectory = status.WorkingDirectory;
	if (!workingDirectory.empty())
	{
		m_directoryMonitor->AddDirectory(workingDirectory);
	}

	auto repositoryPath = status.RepositoryPath;
	if (!repositoryPath.empty())
	{
		if (workingDirectory.empty() || repositoryPath.find(workingDirectory) != 0)
		{
{'fraction_non_alphanumeric': 0.15478484264611433, 'fraction_numerical': 0.002569043031470777, 'mean_word_length': 6.416666666666667, 'pattern_counts': {'":': 4, '<': 32, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4808882', 'n_tokens_mistral': 1040, 'n_tokens_neox': 972, 'n_words': 167}