Skip to content

Commit

Permalink
Merge pull request #511 from RaphHaddad/issue-203
Browse files Browse the repository at this point in the history
Issue 203
  • Loading branch information
JakeGinnivan committed Jul 18, 2015
2 parents 6430e67 + 51a3e81 commit a0d3d13
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
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
}

0 comments on commit a0d3d13

Please sign in to comment.