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 (2106B)
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;
15 using flag = git2wrap::diff_options::flag;
16 opts.flags() = flag::disable_pathspec_match | flag::ignore_submodules
17 | flag::include_typechange;
19 m_diff = git2wrap::diff::tree_to_tree(ptree, cmmt.get_tree(), opts);
20 m_stats = m_diff.get_stats();
21 }
23 const std::vector<delta>& diff::get_deltas() const
24 {
25 if (!m_deltas.empty()) {
26 return m_deltas;
27 }
29 m_diff.foreach(
30 file_cb, nullptr, hunk_cb, line_cb, const_cast<diff*>(this) // NOLINT
31 );
33 for (auto& delta : m_deltas) {
34 for (const auto& hunk : delta.get_hunks()) {
35 for (const auto& line : hunk.get_lines()) {
36 delta.m_adds += static_cast<uint32_t>(line.is_add());
37 delta.m_dels += static_cast<uint32_t>(line.is_del());
38 }
39 }
40 }
41 return m_deltas;
42 }
44 int diff::file_cb(
45 const git_diff_delta* delta, float /* progress */, void* payload
46 )
47 {
48 diff& crnt = *reinterpret_cast<diff*>(payload); // NOLINT
49 crnt.m_deltas.emplace_back(delta);
50 return 0;
51 }
53 int diff::hunk_cb(
54 const git_diff_delta* /* delta */, const git_diff_hunk* hunk, void* payload
55 )
56 {
57 diff& crnt = *reinterpret_cast<diff*>(payload); // NOLINT
58 crnt.m_deltas.back().m_hunks.emplace_back(hunk);
59 return 0;
60 }
62 int diff::line_cb(
63 const git_diff_delta* /* delta */,
64 const git_diff_hunk* /* hunk */,
65 const git_diff_line* line,
66 void* payload
67 )
68 {
69 diff& crnt = *reinterpret_cast<diff*>(payload); // NOLINT
70 crnt.m_deltas.back().m_hunks.back().m_lines.emplace_back(line);
71 return 0;
72 }
74 std::string diff::get_files_changed() const
75 {
76 return std::to_string(m_stats.get_files_changed());
77 }
79 std::string diff::get_insertions() const
80 {
81 return std::to_string(m_stats.get_insertions());
82 }
84 std::string diff::get_deletions() const
85 {
86 return std::to_string(m_stats.get_deletions());
87 }
89 } // namespace startgit