startgitStatic page generator for git repositories |
git clone git://git.dimitrijedobrota.com/startgit.git |
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |
diff.cpp (2220B)
0 #include "diff.hpp"
2 namespace startgit
3 {
5 diff::diff(const git2wrap::commit& cmmt)
6 : m_diff(nullptr, nullptr)
7 , m_stats(nullptr)
8 {
9 const auto ptree = cmmt.get_parentcount() > 0
10 ? cmmt.get_parent().get_tree()
11 : git2wrap::tree(nullptr, nullptr);
13 git2wrap::diff_options opts;
14 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
15 opts.flags = GIT_DIFF_DISABLE_PATHSPEC_MATCH | GIT_DIFF_IGNORE_SUBMODULES
16 | GIT_DIFF_INCLUDE_TYPECHANGE;
18 m_diff = git2wrap::diff::tree_to_tree(ptree, cmmt.get_tree(), &opts);
19 m_stats = m_diff.get_stats();
20 }
22 const std::vector<delta>& diff::get_deltas() const
23 {
24 if (!m_deltas.empty()) {
25 return m_deltas;
26 }
28 m_diff.foreach(
29 file_cb, nullptr, hunk_cb, line_cb, const_cast<diff*>(this)); // NOLINT
31 for (auto& delta : m_deltas) {
32 for (const auto& hunk : delta.get_hunks()) {
33 for (const auto& line : hunk.get_lines()) {
34 delta.m_adds += static_cast<uint32_t>(line.is_add());
35 delta.m_dels += static_cast<uint32_t>(line.is_del());
36 }
37 }
38 }
40 return m_deltas;
41 }
43 int diff::file_cb(const git_diff_delta* delta,
44 float /* progress */,
45 void* payload)
46 {
47 diff& crnt = *reinterpret_cast<diff*>(payload); // NOLINT
48 crnt.m_deltas.emplace_back(delta);
49 return 0;
50 }
52 int diff::hunk_cb(const git_diff_delta* /* delta */,
53 const git_diff_hunk* hunk,
54 void* payload)
55 {
56 diff& crnt = *reinterpret_cast<diff*>(payload); // NOLINT
57 crnt.m_deltas.back().m_hunks.emplace_back(hunk);
58 return 0;
59 }
61 int diff::line_cb(const git_diff_delta* /* delta */,
62 const git_diff_hunk* /* hunk */,
63 const git_diff_line* line,
64 void* payload)
65 {
66 diff& crnt = *reinterpret_cast<diff*>(payload); // NOLINT
67 crnt.m_deltas.back().m_hunks.back().m_lines.emplace_back(line);
68 return 0;
69 }
71 std::string diff::get_files_changed() const
72 {
73 return std::to_string(m_stats.get_files_changed());
74 }
76 std::string diff::get_insertions() const
77 {
78 return std::to_string(m_stats.get_insertions());
79 }
81 std::string diff::get_deletions() const
82 {
83 return std::to_string(m_stats.get_deletions());
84 }
86 } // namespace startgit