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 | |
repository.cpp (1254B)
1 #include <fstream> 2 3 #include "repository.hpp" 4 5 namespace startgit 6 { 7 8 repository::repository(const std::filesystem::path& path) 9 : m_path(path) 10 , m_repo(git2wrap::repository::open( 11 path.c_str(), GIT_REPOSITORY_OPEN_NO_SEARCH, nullptr)) 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(const std::filesystem::path& base, 37 const char* file) 38 { 39 std::ifstream ifs(base / file); 40 41 if (!ifs.is_open()) { 42 ifs = base / ".git" / file; 43 } 44 45 if (ifs.is_open()) { 46 std::string res; 47 std::getline(ifs, res, '\n'); 48 return res; 49 } 50 51 return "Unknown"; 52 } 53 54 } // namespace startgit