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

commit 750a74cb6b59a8692fba827fb4bf1be352bbef43
parent ca1520b757d1d7eee862d92d07f5bceed0864608
author Dimitrije Dobrota <mail@dimitrijedobrota.com>
date Tue, 21 Jan 2025 21:22:25 +0100

Add per file change indicator in the commit

Diffstat:
M .clang-tidy | -
M CMakeLists.txt | + -
M source/diff.cpp | +++++++++
M source/diff.hpp | ++++++ -
M source/main.cpp | +++++++++++++++++++++++++++++++++++ -------

5 files changed, 51 insertions(+), 10 deletions(-)


diff --git a/ .clang-tidy b/ .clang-tidy

@@ -15,7 +15,6 @@ Checks: "*,\ -modernize-use-trailing-return-type,\ -cppcoreguidelines-pro-type-vararg,\ -hicpp-vararg,\
-readability-avoid-nested-conditional-operator,\
-misc-include-cleaner,\ -misc-non-private-member-variables-in-classes" WarningsAsErrors: ''

diff --git a/ CMakeLists.txt b/ CMakeLists.txt

@@ -4,7 +4,7 @@ include(cmake/prelude.cmake) project( startgit
VERSION 0.1.28
VERSION 0.1.29
DESCRIPTION "Static page generator for git repositories" HOMEPAGE_URL "https://git.dimitrijedobrota.com/stargit.git" LANGUAGES CXX

diff --git a/ source/diff.cpp b/ source/diff.cpp

@@ -20,6 +20,15 @@ diff::diff(const git2wrap::commit& cmmt) m_stats = m_diff.get_stats(); m_diff.foreach(file_cb, nullptr, hunk_cb, line_cb, this);
for (auto& delta : m_deltas) {
for (const auto& hunk : delta.get_hunks()) {
for (const auto& line : hunk.get_lines()) {
delta.m_adds += static_cast<uint32_t>(line.is_add());
delta.m_dels += static_cast<uint32_t>(line.is_del());
}
}
}
} int diff::file_cb(const git_diff_delta* delta,

diff --git a/ source/diff.hpp b/ source/diff.hpp

@@ -18,7 +18,8 @@ struct line } const std::string& get_content() const { return m_content; }
char get_origin() const { return m_origin; }
bool is_add() const { return m_origin == '+'; }
bool is_del() const { return m_origin == '-'; }
private: std::string m_content;

@@ -50,10 +51,14 @@ struct delta } const auto* operator->() const { return &m_ptr; } const auto& get_hunks() const { return m_hunks; }
auto get_adds() const { return m_adds; }
auto get_dels() const { return m_dels; }
private: git_diff_delta m_ptr; std::vector<hunk> m_hunks;
uint32_t m_adds = 0;
uint32_t m_dels = 0;
}; class diff

diff --git a/ source/main.cpp b/ source/main.cpp

@@ -1,3 +1,5 @@
#include <cmath>
#include <cstring>
#include <filesystem> #include <format> #include <fstream>

@@ -344,12 +346,32 @@ void write_file_changes(std::ostream& ost, const startgit::diff& diff) const std::string link = std::format("#{}", delta->new_file.path);
uint32_t add = delta.get_adds();
uint32_t del = delta.get_dels();
const uint32_t changed = add + del;
const uint32_t total = 80;
if (changed > total) {
const double percent = 1.0 * total / changed;
if (add > 0) {
add = static_cast<uint32_t>(std::lround(percent * add) + 1);
}
if (del > 0) {
del = static_cast<uint32_t>(std::lround(percent * del) + 1);
}
}
ost << html::tr() .add(html::td(std::string(1, marker[delta->status]))) // NOLINT .add(html::td().add( html::a(delta->new_file.path).set("href", link))) .add(html::td("|"))
.add(html::td("..."));
.add(html::td()
.add(html::span(" " + std::string(add, '+'))
.set("class", "add"))
.add(html::span(" " + std::string(del, '-'))
.set("class", "del")));
} ost << html::tbody() << html::table();

@@ -389,14 +411,16 @@ void write_file_diffs(std::ostream& ost, const startgit::diff& diff) ost << html::span().set("style", "white-space: pre"); for (const auto& line : hunk.get_lines()) {
ost << html::div().set(
"style",
line.get_origin() == '+' ? "color: var(--theme_green)"
: line.get_origin() == '-' ? "color: var(--theme_red)"
: "");
auto div = html::div();
if (line.is_add()) {
div.set("class", "add");
} else if (line.is_del()) {
div.set("class", "del");
}
ost << div;
startgit::xmlencode(ost, line.get_content());
ost << html::div();
ost << div;
} ost << html::span(); }

@@ -524,6 +548,10 @@ void write_footer(std::ostream& ost) "} select option {" " color: var(--theme_fg2) !important;" " background-color: var(--theme_bg3) !important;"
"} .add {"
" color: var(--theme_green);"
"} .del {"
" color: var(--theme_red);"
"}"); ost << html::body(); ost << html::html();