diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs
index 721133cc6..faf572255 100644
--- a/LibGit2Sharp/Repository.cs
+++ b/LibGit2Sharp/Repository.cs
@@ -249,6 +249,7 @@ private Repository(string path, RepositoryOptions options, RepositoryRequiredPar
/// would be the ".git" folder inside the working directory) or the path to the working directory.
///
/// True if a repository can be resolved through this path; false otherwise
+ /// If repository is corrupt or a system error occurs
static public bool IsValid(string path)
{
Ensure.ArgumentNotNull(path, "path");
@@ -258,15 +259,27 @@ static public bool IsValid(string path)
return false;
}
- try
- {
- Proxy.git_repository_open_ext(path, RepositoryOpenFlags.NoSearch, null);
- }
- catch (RepositoryNotFoundException)
+ return IsRepoFound(path);
+ }
+
+ private static unsafe bool IsRepoFound(string path)
+ {
+ git_repository* repo;
+
+ // Try to open the repository to check if it is found
+ int res = NativeMethods.git_repository_open_ext(out repo, path, RepositoryOpenFlags.NoSearch, null);
+ NativeMethods.git_repository_free(repo);
+
+ // If repo not found, then return false
+ if (res == (int)GitErrorCode.NotFound)
{
return false;
}
+ // Possibility for other errors, such as -1 for corrupt repo. Probably makes sense to throw exception in
+ // this case
+ Ensure.ZeroResult(res);
+
return true;
}