startgit

Static 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 (2207B)


0 #include "diff.hpp" 1 2 namespace startgit 3 { 4 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); 12 13 git2wrap::diff_options opts; 14 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION); 15 16 // NOLINTBEGIN(*hicpp-signed-bitwise*) 17 opts.flags = GIT_DIFF_DISABLE_PATHSPEC_MATCH | GIT_DIFF_IGNORE_SUBMODULES 18 | GIT_DIFF_INCLUDE_TYPECHANGE; 19 // NOLINTEND(*hicpp-signed-bitwise*) 20 21 m_diff = git2wrap::diff::tree_to_tree(ptree, cmmt.get_tree(), &opts); 22 m_stats = m_diff.get_stats(); 23 } 24 25 const std::vector<delta>& diff::get_deltas() const 26 { 27 if (!m_deltas.empty()) { 28 return m_deltas; 29 } 30 31 m_diff.foreach( 32 file_cb, nullptr, hunk_cb, line_cb, const_cast<diff*>(this) // NOLINT 33 ); 34 35 for (auto& delta : m_deltas) { 36 for (const auto& hunk : delta.get_hunks()) { 37 for (const auto& line : hunk.get_lines()) { 38 delta.m_adds += static_cast<uint32_t>(line.is_add()); 39 delta.m_dels += static_cast<uint32_t>(line.is_del()); 40 } 41 } 42 } 43 return m_deltas; 44 } 45 46 int diff::file_cb( 47 const git_diff_delta* delta, float /* progress */, void* payload 48 ) 49 { 50 diff& crnt = *reinterpret_cast<diff*>(payload); // NOLINT 51 crnt.m_deltas.emplace_back(delta); 52 return 0; 53 } 54 55 int diff::hunk_cb( 56 const git_diff_delta* /* delta */, const git_diff_hunk* hunk, void* payload 57 ) 58 { 59 diff& crnt = *reinterpret_cast<diff*>(payload); // NOLINT 60 crnt.m_deltas.back().m_hunks.emplace_back(hunk); 61 return 0; 62 } 63 64 int diff::line_cb( 65 const git_diff_delta* /* delta */, 66 const git_diff_hunk* /* hunk */, 67 const git_diff_line* line, 68 void* payload 69 ) 70 { 71 diff& crnt = *reinterpret_cast<diff*>(payload); // NOLINT 72 crnt.m_deltas.back().m_hunks.back().m_lines.emplace_back(line); 73 return 0; 74 } 75 76 std::string diff::get_files_changed() const 77 { 78 return std::to_string(m_stats.get_files_changed()); 79 } 80 81 std::string diff::get_insertions() const 82 { 83 return std::to_string(m_stats.get_insertions()); 84 } 85 86 std::string diff::get_deletions() const 87 { 88 return std::to_string(m_stats.get_deletions()); 89 } 90 91 } // namespace startgit