Skip to content

Commit a0d3d13

Browse files
committed
Merge pull request #511 from RaphHaddad/issue-203
Issue 203
2 parents 6430e67 + 51a3e81 commit a0d3d13

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

GitVersionCore/GitPreparer.cs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,7 @@ static string CreateDynamicRepository(string targetPath, Authentication authenti
150150

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

153-
Repository.Clone(repositoryUrl, gitDirectory,
154-
new CloneOptions
155-
{
156-
Checkout = false,
157-
CredentialsProvider = (url, usernameFromUrl, types) => credentials
158-
});
153+
CloneRepository(repositoryUrl, gitDirectory, credentials);
159154

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

198+
private static void CloneRepository(string repositoryUrl, string gitDirectory, Credentials credentials)
199+
{
200+
try
201+
{
202+
Repository.Clone(repositoryUrl, gitDirectory,
203+
new CloneOptions
204+
{
205+
Checkout = false,
206+
CredentialsProvider = (url, usernameFromUrl, types) => credentials
207+
});
208+
}
209+
catch (LibGit2SharpException ex)
210+
{
211+
var message = ex.Message;
212+
if (message.Contains("401"))
213+
{
214+
throw new Exception("Unauthorised: Incorrect username/password");
215+
}
216+
if (message.Contains("403"))
217+
{
218+
throw new Exception("Forbidden: Possbily Incorrect username/password");
219+
}
220+
if (message.Contains("404"))
221+
{
222+
throw new Exception("Not found: The repository was not found");
223+
}
224+
225+
throw new Exception("There was an unknown problem with the Git repository you provided");
226+
227+
}
228+
}
229+
203230
private static Reference GetLocalReference(Repository repository, string branchName)
204231
{
205232
var targetBranchName = branchName.GetCanonicalBranchName();

GitVersionExe.Tests/GitPreparerTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,5 +275,31 @@ public void UsingDynamicRepositoryWithoutTargetBranchFails()
275275
}
276276
}
277277

278+
[Test]
279+
public void TestErrorThrownForInvalidRepository()
280+
{
281+
var repoName = Guid.NewGuid().ToString();
282+
var tempPath = Path.GetTempPath();
283+
var tempDir = Path.Combine(tempPath, repoName);
284+
Directory.CreateDirectory(tempDir);
285+
286+
try
287+
{
288+
var arguments = new Arguments
289+
{
290+
TargetPath = tempDir,
291+
TargetUrl = "http://127.0.0.1/testrepo.git"
292+
};
293+
294+
var gitPreparer = new GitPreparer(arguments.TargetUrl, arguments.DynamicRepositoryLocation, arguments.Authentication, arguments.TargetBranch, arguments.NoFetch, arguments.TargetPath);
295+
296+
Assert.Throws<Exception>(() => gitPreparer.Initialise(true));
297+
}
298+
finally
299+
{
300+
Directory.Delete(tempDir, true);
301+
}
302+
}
303+
278304
// TODO test around normalisation
279305
}

0 commit comments

Comments
 (0)