#include "git.h" #include #include #include #include #include #include namespace GitFS::Git { template<> [[noreturn]] void throwError(int err) { if (err == GIT_ENOTFOUND) { throw NetFS::SystemError(ENOENT); } throw NetFS::SystemError(EIO); } template<> [[noreturn]] void throwError(int) { throw NetFS::ConfigError(); } git_oid oidParse(const std::string_view str) { git_oid oid; gitSafe(git_oid_fromstrn, &oid, str.data(), str.length()); return oid; } RepositoryPtr repositoryOpenBare(const std::string & path) { return gitSafeGet(git_repository_open_bare, git_repository_free, path.c_str()); } BlobPtr blobLookup(const RepositoryPtr & repo, const git_oid & blob) { return gitSafeGet(git_blob_lookup, git_blob_free, repo.get(), &blob); } CommitPtr commitLookup(const RepositoryPtr & repo, const git_oid & commitId) { return gitSafeGet(git_commit_lookup, git_commit_free, repo.get(), &commitId); } TreePtr treeLookup(const RepositoryPtr & repo, const git_oid & treeId) { return gitSafeGet(git_tree_lookup, git_tree_free, repo.get(), &treeId); } TreeEntryPtr treeEntryByPath(const TreePtr & tree, const std::string & path) { // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) Skip leading / with C interface return gitSafeGet(git_tree_entry_bypath, git_tree_entry_free, tree.get(), path.c_str() + 1); } RefPtr commitish(const RepositoryPtr & repo, const std::string & name) { return gitSafeGet(git_reference_dwim, git_reference_free, repo.get(), name.c_str()); } RefPtr resolve(const RefPtr & ref) { return gitSafeGet(git_reference_resolve, git_reference_free, ref.get()); } } namespace NetFS { Attr & operator<<(Attr & attr, const git_tree_entry & entry) { attr.mode = git_tree_entry_filemode(&entry); if (S_ISDIR(attr.mode)) { attr.mode |= S_IXUSR | S_IXGRP | S_IXOTH | S_IRUSR | S_IRGRP | S_IROTH; } else if (S_ISLNK(attr.mode)) { attr.mode |= S_IRUSR | S_IRGRP | S_IROTH; } else { attr.mode ^= S_IWUSR; } return attr; } Attr & operator<<(Attr & attr, const git_commit & commit) { attr.ctime = attr.atime = attr.mtime = git_commit_time(&commit); return attr; } Attr & operator<<(Attr & attr, const git_blob & blob) { attr.blockSize = 1; attr.blocks = attr.size = safe {git_blob_rawsize(&blob)}; return attr; } } namespace std { std::ostream & operator<<(std::ostream & strm, const git_oid & oid) { std::array str {}; git_oid_tostr(str.data(), str.size(), &oid); strm.write(str.data(), GIT_OID_HEXSZ); return strm; } }