From 6168d85a886286a43b55a0cd6739a1e1e3987d90 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 22 Jul 2019 20:08:39 +0100 Subject: Unline git helpers and throw NetFS exceptions --- src/git.h | 71 ++++++++++++++++++++++----------------------------------------- 1 file changed, 25 insertions(+), 46 deletions(-) (limited to 'src/git.h') diff --git a/src/git.h b/src/git.h index 9107f8b..56a7cf6 100644 --- a/src/git.h +++ b/src/git.h @@ -7,81 +7,60 @@ namespace GitFS { namespace Git { - struct Error { - int err; - int klass; - const char * message; - }; - void throwError(int err); - - template + template + [[noreturn]] void throwError(int err); + + template void gitSafe(int (*func)(P...), A ... p) { if (int _giterror = func(p...); _giterror != 0) { - throwError(_giterror); + throwError(_giterror); } } - [[noreturn]] void ErrorToSystemError(const Error & e); - template using TPtr = std::shared_ptr; - template + template auto gitSafeGet(int(*get)(R**, P...), void(*release)(R*), A ... p) { R * r = nullptr; - gitSafe(get, &r, p...); + gitSafe(get, &r, p...); return TPtr(r, release); } - template + template auto gitSafeGet(int(*get)(R**, P...), A ... p) { R * r = nullptr; - gitSafe(get, &r, p...); + gitSafe(get, &r, p...); return r; } - inline auto OidParse(const std::string_view & str) - { - git_oid oid; - gitSafe(git_oid_fromstrn, &oid, str.data(), str.length()); - return oid; - } + git_oid OidParse(const std::string_view & str); - inline auto RepositoryOpenBare(const std::string & path) - { - return gitSafeGet(git_repository_open_bare, git_repository_free, path.c_str()); - } - using RepositoryPtr = decltype(RepositoryOpenBare("")); + using RepositoryPtr = decltype(gitSafeGet( + git_repository_open_bare, git_repository_free, nullptr)); + RepositoryPtr RepositoryOpenBare(const std::string & path); - inline auto BlobLookup(const RepositoryPtr & repo, const git_oid & blob) - { - return gitSafeGet(git_blob_lookup, git_blob_free, repo.get(), &blob); - } - using BlobPtr = decltype(BlobLookup({}, {})); + using BlobPtr = decltype(gitSafeGet( + git_blob_lookup, git_blob_free, nullptr, nullptr)); + BlobPtr BlobLookup(const RepositoryPtr & repo, const git_oid & blob); - inline auto CommitLookup(const RepositoryPtr & repo, const git_oid & commitId) - { - return gitSafeGet(git_commit_lookup, git_commit_free, repo.get(), &commitId); - } - using CommitPtr = decltype(CommitLookup({}, {})); + using CommitPtr = decltype(gitSafeGet( + git_commit_lookup, git_commit_free, nullptr, nullptr)); + CommitPtr CommitLookup(const RepositoryPtr & repo, const git_oid & commitId); - inline auto TreeLookup(const RepositoryPtr & repo, const git_oid & treeId) - { - return gitSafeGet(git_tree_lookup, git_tree_free, repo.get(), &treeId); - } - using TreePtr = decltype(TreeLookup({}, {})); + using TreePtr = decltype(gitSafeGet( + git_tree_lookup, git_tree_free, nullptr, nullptr)); + TreePtr TreeLookup(const RepositoryPtr & repo, const git_oid & treeId); - inline auto TreeEntryByPath(const TreePtr & tree, const std::string & path) - { - return gitSafeGet(git_tree_entry_bypath, git_tree_entry_free, tree.get(), path.c_str() + 1); - } - using TreeEntryPtr = decltype(TreeEntryByPath({}, {})); + using TreeEntryPtr = decltype(gitSafeGet( + git_tree_entry_bypath, git_tree_entry_free, nullptr, nullptr)); + TreeEntryPtr TreeEntryByPath(const TreePtr & tree, const std::string & path); } } -- cgit v1.2.3