Skip to content

Issue 203 #511

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

Merged
merged 8 commits into from
Jul 18, 2015
Merged
Show file tree
Hide file tree
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
39 changes: 33 additions & 6 deletions GitVersionCore/GitPreparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,7 @@ static string CreateDynamicRepository(string targetPath, Authentication authenti

Logger.WriteInfo(string.Format("Retrieving git info from url '{0}'", repositoryUrl));

Repository.Clone(repositoryUrl, gitDirectory,
new CloneOptions
{
Checkout = false,
CredentialsProvider = (url, usernameFromUrl, types) => credentials
});
CloneRepository(repositoryUrl, gitDirectory, credentials);

// Normalize (download branches) before using the branch
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch);
Expand Down Expand Up @@ -200,6 +195,38 @@ static string CreateDynamicRepository(string targetPath, Authentication authenti
return gitDirectory;
}

private static void CloneRepository(string repositoryUrl, string gitDirectory, Credentials credentials)
{
try
{
Repository.Clone(repositoryUrl, gitDirectory,
new CloneOptions
{
Checkout = false,
CredentialsProvider = (url, usernameFromUrl, types) => credentials
});
}
catch (LibGit2SharpException ex)
{
var message = ex.Message;
if (message.Contains("401"))
{
throw new Exception("Unauthorised: Incorrect username/password");
}
if (message.Contains("403"))
{
throw new Exception("Forbidden: Possbily Incorrect username/password");
}
if (message.Contains("404"))
{
throw new Exception("Not found: The repository was not found");
}

throw new Exception("There was an unknown problem with the Git repository you provided");

}
}

private static Reference GetLocalReference(Repository repository, string branchName)
{
var targetBranchName = branchName.GetCanonicalBranchName();
Expand Down
26 changes: 26 additions & 0 deletions GitVersionExe.Tests/GitPreparerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,5 +275,31 @@ public void UsingDynamicRepositoryWithoutTargetBranchFails()
}
}

[Test]
public void TestErrorThrownForInvalidRepository()
{
var repoName = Guid.NewGuid().ToString();
var tempPath = Path.GetTempPath();
var tempDir = Path.Combine(tempPath, repoName);
Directory.CreateDirectory(tempDir);

try
{
var arguments = new Arguments
{
TargetPath = tempDir,
TargetUrl = "http://127.0.0.1/testrepo.git"
};

var gitPreparer = new GitPreparer(arguments.TargetUrl, arguments.DynamicRepositoryLocation, arguments.Authentication, arguments.TargetBranch, arguments.NoFetch, arguments.TargetPath);

Assert.Throws<Exception>(() => gitPreparer.Initialise(true));
}
finally
{
Directory.Delete(tempDir, true);
}
}

// TODO test around normalisation
}