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 (1266B)
    0 #include <fstream>
          
              2 #include "repository.hpp"
          
              4 namespace startgit
              5 {
          
              7 repository::repository(const std::filesystem::path& path)
              8     : m_path(path)
              9     , m_repo(git2wrap::repository::open(
             10           path.c_str(), git2wrap::repository::flags_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(git2wrap::branch::flags_list::local);
             19        it != m_repo.branch_end();
             20        ++it)
             21   {
             22     m_branches.emplace_back(it->dup(), *this);
             23   }
          
             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   };
          
             33   m_repo.tag_foreach(callback, this);
             34 }
          
             36 std::string repository::read_file(
             37     const std::filesystem::path& base, const char* file
             38 )
             39 {
             40   std::ifstream ifs(base / file);
          
             42   if (!ifs.is_open()) {
             43     ifs = base / ".git" / file;
             44   }
          
             46   if (ifs.is_open()) {
             47     std::string res;
             48     std::getline(ifs, res, '\n');
             49     return res;
             50   }
          
             52   return "Unknown";
             53 }
          
             55 }  // namespace startgit