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

repository.cpp (1233B)


0 #include <fstream> 1 2 #include "repository.hpp" 3 4 namespace startgit 5 { 6 7 repository::repository(const std::filesystem::path& path) 8 : m_path(path) 9 , m_repo(git2wrap::repository::open( 10 path.c_str(), GIT_REPOSITORY_OPEN_NO_SEARCH, nullptr 11 )) 12 , m_name(path.stem().string()) 13 , m_url(read_file(path, "url")) 14 , m_owner(read_file(path, "owner")) 15 , m_description(read_file(path, "description")) 16 { 17 // Get branches 18 for (auto it = m_repo.branch_begin(GIT_BRANCH_LOCAL); 19 it != m_repo.branch_end(); 20 ++it) 21 { 22 m_branches.emplace_back(it->dup(), *this); 23 } 24 25 // Get tags 26 auto callback = +[](const char*, git_oid* objid, void* payload_p) 27 { 28 auto& repo = *reinterpret_cast<repository*>(payload_p); // NOLINT 29 repo.m_tags.emplace_back(repo.m_repo.tag_lookup(git2wrap::oid(objid))); 30 return 0; 31 }; 32 33 m_repo.tag_foreach(callback, this); 34 } 35 36 std::string repository::read_file( 37 const std::filesystem::path& base, const char* file 38 ) 39 { 40 std::ifstream ifs(base / file); 41 42 if (!ifs.is_open()) { 43 ifs = base / ".git" / file; 44 } 45 46 if (ifs.is_open()) { 47 std::string res; 48 std::getline(ifs, res, '\n'); 49 return res; 50 } 51 52 return "Unknown"; 53 } 54 55 } // namespace startgit