CodeCompass
81 строка · 2.0 Кб
1#include <fstream>
2
3#include <boost/filesystem.hpp>
4
5#include <model/file.h>
6#include <model/file-odb.hxx>
7
8#include <util/hash.h>
9#include <util/odbtransaction.h>
10
11#include <parser/parsercontext.h>
12#include <parser/sourcemanager.h>
13
14namespace po = boost::program_options;
15
16namespace cc
17{
18namespace parser
19{
20
21ParserContext::ParserContext(
22std::shared_ptr<odb::database> db_,
23SourceManager& srcMgr_,
24std::string& compassRoot_,
25po::variables_map& options_) :
26db(db_),
27srcMgr(srcMgr_),
28compassRoot(compassRoot_),
29options(options_)
30{
31std::unordered_map<std::string, std::string> fileHashes;
32
33(util::OdbTransaction(this->db))([&]
34{
35// Fetch directory and binary type files from SourceManager
36auto func = [](model::FilePtr item)
37{
38return item->type != model::File::DIRECTORY_TYPE &&
39item->type != model::File::BINARY_TYPE;
40};
41std::vector<model::FilePtr> files = this->srcMgr.getFiles(func);
42
43for (model::FilePtr file : files)
44{
45if (boost::filesystem::exists(file->path))
46{
47if (!fileStatus.count(file->path))
48{
49model::FileContentPtr content = file->content.load();
50if (!content)
51continue;
52
53fileHashes[file->path] = content->hash;
54
55std::ifstream fileStream(file->path);
56std::string fileContent(
57std::istreambuf_iterator<char>{fileStream},
58std::istreambuf_iterator<char>{});
59fileStream.close();
60
61if (content->hash != util::sha1Hash(fileContent))
62{
63this->fileStatus.emplace(
64file->path, cc::parser::IncrementalStatus::MODIFIED);
65LOG(debug) << "File modified: " << file->path;
66}
67}
68}
69else
70{
71fileStatus.emplace(
72file->path, cc::parser::IncrementalStatus::DELETED);
73LOG(debug) << "File deleted: " << file->path;
74}
75}
76
77// TODO: detect ADDED files
78});
79}
80}
81}
82
83