Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Repository.IsValid test to remove exception #1817

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions LibGit2Sharp/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </param>
/// <returns>True if a repository can be resolved through this path; false otherwise</returns>
/// <exception cref="LibGit2SharpException">If repository is corrupt or a system error occurs</exception>
static public bool IsValid(string path)
{
Ensure.ArgumentNotNull(path, "path");
Expand All @@ -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;
}

Expand Down