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.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 12 deletions(-) (limited to 'src/git.cpp') diff --git a/src/git.cpp b/src/git.cpp index 850b8b3..045d146 100644 --- a/src/git.cpp +++ b/src/git.cpp @@ -6,25 +6,63 @@ namespace GitFS { namespace Git { + template<> + [[noreturn]] void - throwError(int err) + throwError(int err) { -#if LIBGIT2_VER_MINOR >= 28 - const git_error * e = git_error_last(); -#else - const git_error * e = giterr_last(); -#endif - throw Error { err, e->klass, e->message }; + switch (err) { + case GIT_ENOTFOUND: + throw NetFS::SystemError(ENOENT); + default: + throw NetFS::SystemError(EIO); + } } - [[noreturn]] void ErrorToSystemError(const Error & e) + template<> + [[noreturn]] + void + throwError(int) { - if (e.err == GIT_ENOTFOUND) { - throw NetFS::SystemError(ENOENT); - } - throw NetFS::SystemError(EIO); + 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) + { + return gitSafeGet( + git_tree_entry_bypath, git_tree_entry_free, tree.get(), path.c_str() + 1); + } } } -- cgit v1.2.3