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 aaea18699ba8c13e617c79051e1d3a9b57d5cb0a
parent 0bd4339e2a40d584ef655ff7a6ba81d73ecfe369
author Dimitrije Dobrota <mail@dimitrijedobrota.com>
date Wed, 15 Jan 2025 22:22:18 +0100

Render all the files

Diffstat:
M CMakeLists.txt | + -
M source/file.cpp | +++++++++++++++++++
M source/file.hpp | ++++++
M source/main.cpp | +++++++++++++++++++++++++++++++++++++++++++++++++++ ----

4 files changed, 77 insertions(+), 5 deletions(-)


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

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

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

@@ -1,5 +1,7 @@ #include "file.hpp"
#include <git2wrap/repository.hpp>
#include "utils.hpp" namespace startgit

@@ -8,7 +10,24 @@ namespace startgit file::file(const git2wrap::tree_entry& entry, std::string path) : m_filemode(filemode(entry.get_filemode())) , m_path(std::move(path))
, m_blob(
git2wrap::repository(entry.get_owner()).blob_lookup(entry.get_id()))
{
}
bool file::is_binary() const
{
return m_blob.is_binary();
}
const char* file::get_content() const
{
return static_cast<const char*>(m_blob.get_rawcontent());
}
git2wrap::object_size_t file::get_size() const
{
return m_blob.get_rawsize();
} } // namespace startgit

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

@@ -2,6 +2,7 @@ #include <string>
#include <git2wrap/blob.hpp>
#include <git2wrap/tree.hpp> namespace startgit

@@ -15,9 +16,14 @@ public: std::string get_filemode() const { return m_filemode; } std::string get_path() const { return m_path; }
bool is_binary() const;
const char* get_content() const;
git2wrap::object_size_t get_size() const;
private: std::string m_filemode; std::string m_path;
git2wrap::blob m_blob;
}; } // namespace startgit

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

@@ -405,6 +405,25 @@ void write_commit_diff(std::ostream& ost, const startgit::commit& commit) write_file_diffs(ost, commit.get_diff()); }
void write_file_content(std::ostream& ost, const startgit::file& file)
{
using namespace hemplate; // NOLINT
const std::string str(file.get_content(), file.get_size());
std::stringstream sstr(str);
std::string line;
ost << html::span().set("style", "white-space: pre;");
for (int count = 1; std::getline(sstr, line, '\n'); count++) {
ost << std::format(R"(<a id="{}" href="#{}">{:5}</a>)", count, count, count);
ost << " ";
startgit::xmlencode(ost, line);
ost << '\n';
}
ost << html::span();
}
void write_footer(std::ostream& ost) { using namespace hemplate; // NOLINT

@@ -456,9 +475,9 @@ void write_log(const std::filesystem::path& base, write_footer(ofs); }
void write_files(const std::filesystem::path& base,
const startgit::repository& repo,
const startgit::branch& branch)
void write_file(const std::filesystem::path& base,
const startgit::repository& repo,
const startgit::branch& branch)
{ std::ofstream ofs(base / "files.html");

@@ -496,6 +515,29 @@ void write_commits(const std::filesystem::path& base, } }
void write_files(const std::filesystem::path& base,
const startgit::repository& repo,
const startgit::branch& branch)
{
for (const auto& file : branch.get_files()) {
const std::filesystem::path path = base / (file.get_path() + ".html");
std::filesystem::create_directories(path.parent_path());
std::ofstream ofs(path);
std::string back = "../";
for (const char chr : file.get_path()) {
if (chr == '/') {
back += "../";
}
}
write_header(ofs, repo, branch, file.get_path());
write_title(ofs, repo, branch.get_name(), back);
write_file_content(ofs, file);
write_footer(ofs);
}
}
int parse_opt(int key, const char* arg, poafloc::Parser* parser) { auto* args = static_cast<arguments_t*>(parser->input());

@@ -558,13 +600,18 @@ int main(int argc, char* argv[]) std::filesystem::create_directory(base_branch); write_log(base_branch, repo, branch);
write_files(base_branch, repo, branch);
write_file(base_branch, repo, branch);
write_refs(base_branch, repo, branch); const std::filesystem::path commit = base_branch / "commit"; std::filesystem::create_directory(commit); write_commits(commit, repo, branch);
const std::filesystem::path file = base_branch / "file";
std::filesystem::create_directory(file);
write_files(file, repo, branch);
} write_repo_table_entry(index, repo);