Skip to content

Commit 5a3ba69

Browse files
committed
cleanup of IGitRepository, replace Repository with GitRepository
Removed fields that depend directly on Libgit2sharp Using proxy classes, and methods
1 parent a78492f commit 5a3ba69

File tree

12 files changed

+217
-226
lines changed

12 files changed

+217
-226
lines changed

src/GitVersionCore.Tests/Mocks/MockRepository.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
using System;
22
using GitVersion;
3-
using LibGit2Sharp;
4-
using Branch = GitVersion.Branch;
5-
using BranchCollection = GitVersion.BranchCollection;
6-
using Commit = GitVersion.Commit;
7-
using ReferenceCollection = GitVersion.ReferenceCollection;
8-
using TagCollection = GitVersion.TagCollection;
9-
3+
using GitVersion.Logging;
4+
using Remote = LibGit2Sharp.Remote;
105
namespace GitVersionCore.Tests.Mocks
116
{
127
public class MockRepository : IGitRepository
@@ -30,12 +25,16 @@ public CommitCollection Commits
3025

3126
public BranchCollection Branches { get; set; }
3227
public TagCollection Tags { get; set; }
33-
public Network Network { get; set; }
3428
public string Path { get; }
29+
public string WorkingDirectory { get; }
3530
public bool IsHeadDetached { get; }
3631
public int GetNumberOfUncommittedChanges() => 0;
3732
public Commit FindMergeBase(Commit commit, Commit otherCommit) => throw new NotImplementedException();
3833
public string ShortenObjectId(Commit commit) => throw new NotImplementedException();
34+
public void CreateBranchForPullRequestBranch(ILog log, AuthenticationInfo auth) => throw new NotImplementedException();
35+
public bool GitRepoHasMatchingRemote(string targetUrl) => throw new NotImplementedException();
36+
public void CleanupDuplicateOrigin(string defaultRemoteName) => throw new NotImplementedException();
37+
public Remote EnsureOnlyOneRemoteIsDefined(ILog log) => throw new NotImplementedException();
3938
public void Dispose() => throw new NotImplementedException();
4039
}
4140
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
using System;
2+
using GitVersion.Logging;
23
using LibGit2Sharp;
34

45
namespace GitVersion
56
{
67
public interface IGitRepository : IDisposable
78
{
89
string Path { get; }
10+
string WorkingDirectory { get; }
911
bool IsHeadDetached { get; }
1012
IGitRepositoryCommands Commands { get; }
1113
Branch Head { get; }
1214
CommitCollection Commits { get; }
1315
BranchCollection Branches { get; }
1416
TagCollection Tags { get; }
1517
ReferenceCollection Refs { get; }
16-
Network Network { get; }
18+
1719
int GetNumberOfUncommittedChanges();
1820
Commit FindMergeBase(Commit commit, Commit otherCommit);
1921
string ShortenObjectId(Commit commit);
22+
void CreateBranchForPullRequestBranch(ILog log, AuthenticationInfo auth);
23+
Remote EnsureOnlyOneRemoteIsDefined(ILog log);
24+
bool GitRepoHasMatchingRemote(string targetUrl);
25+
void CleanupDuplicateOrigin(string defaultRemoteName);
2026
}
2127
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
using System.Collections.Generic;
2-
using LibGit2Sharp;
32

43
namespace GitVersion
54
{
65
/// <summary>
76
/// Mockable and testable interface wrapper for the <c>static</c>
8-
/// <see cref="Commands"/> <c>class</c>.
97
/// </summary>
108
public interface IGitRepositoryCommands
119
{
1210
void Checkout(string committishOrBranchSpec);
1311
void Checkout(Branch branch);
14-
void Fetch(string remote, IEnumerable<string> refspecs, FetchOptions options, string logMessage);
12+
void Fetch(string remote, IEnumerable<string> refspecs, AuthenticationInfo auth, string logMessage);
1513
}
1614
}

src/GitVersionCore/Core/GitModel.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,48 @@
33
using System.Collections.Generic;
44
using GitVersion.Helpers;
55
using LibGit2Sharp;
6+
using LibGit2Sharp.Handlers;
67

78
namespace GitVersion
89
{
10+
public class AuthenticationInfo
11+
{
12+
public string Username { get; set; }
13+
public string Password { get; set; }
14+
public string Token { get; set; }
15+
public CredentialsHandler CredentialsProvider()
16+
{
17+
if (!string.IsNullOrWhiteSpace(Username))
18+
{
19+
return (url, user, types) => new UsernamePasswordCredentials
20+
{
21+
Username = Username,
22+
Password = Password ?? string.Empty
23+
};
24+
}
25+
return null;
26+
}
27+
public FetchOptions ToFetchOptions()
28+
{
29+
var fetchOptions = new FetchOptions
30+
{
31+
CredentialsProvider = CredentialsProvider()
32+
};
33+
34+
return fetchOptions;
35+
}
36+
public CloneOptions ToCloneOptions()
37+
{
38+
var cloneOptions = new CloneOptions
39+
{
40+
Checkout = false,
41+
CredentialsProvider = CredentialsProvider()
42+
};
43+
44+
return cloneOptions;
45+
}
46+
}
47+
948
public class Tag
1049
{
1150
private readonly LibGit2Sharp.Tag innerTag;
@@ -24,6 +63,7 @@ protected Tag()
2463
public virtual string FriendlyName => innerTag?.FriendlyName;
2564
public virtual TagAnnotation Annotation => innerTag?.Annotation;
2665
}
66+
2767
public class Commit
2868
{
2969
private static readonly LambdaEqualityHelper<Commit> equalityHelper =

src/GitVersionCore/Core/GitPreparer.cs

Lines changed: 8 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,8 @@ private string ResolveCurrentBranch()
8686

8787
private void CleanupDuplicateOrigin()
8888
{
89-
var remoteToKeep = DefaultRemoteName;
90-
using var repo = new Repository(options.Value.GitRootPath);
91-
92-
// check that we have a remote that matches defaultRemoteName if not take the first remote
93-
if (!repo.Network.Remotes.Any(remote => remote.Name.Equals(DefaultRemoteName, StringComparison.InvariantCultureIgnoreCase)))
94-
{
95-
remoteToKeep = repo.Network.Remotes.First().Name;
96-
}
97-
98-
var duplicateRepos = repo.Network
99-
.Remotes
100-
.Where(remote => !remote.Name.Equals(remoteToKeep, StringComparison.InvariantCultureIgnoreCase))
101-
.Select(remote => remote.Name);
102-
103-
// remove all remotes that are considered duplicates
104-
foreach (var repoName in duplicateRepos)
105-
{
106-
repo.Network.Remotes.Remove(repoName);
107-
}
89+
using IGitRepository repo = new GitRepository(options.Value.GitRootPath);
90+
repo.CleanupDuplicateOrigin(DefaultRemoteName);
10891
}
10992

11093
private void CreateDynamicRepository(string targetBranch)
@@ -144,53 +127,10 @@ private void NormalizeGitDirectory(string targetBranch, string gitDirectory, boo
144127

145128
private void CloneRepository(string repositoryUrl, string gitDirectory, AuthenticationInfo auth)
146129
{
147-
Credentials credentials = null;
148-
149-
if (auth != null)
130+
using (log.IndentLog($"Cloning repository from url '{repositoryUrl}'"))
150131
{
151-
if (!string.IsNullOrWhiteSpace(auth.Username))
152-
{
153-
log.Info($"Setting up credentials using name '{auth.Username}'");
154-
155-
credentials = new UsernamePasswordCredentials
156-
{
157-
Username = auth.Username,
158-
Password = auth.Password ?? string.Empty
159-
};
160-
}
161-
}
162-
163-
try
164-
{
165-
using (log.IndentLog($"Cloning repository from url '{repositoryUrl}'"))
166-
{
167-
var cloneOptions = new CloneOptions
168-
{
169-
Checkout = false,
170-
CredentialsProvider = (url, usernameFromUrl, types) => credentials
171-
};
172-
173-
var returnedPath = Repository.Clone(repositoryUrl, gitDirectory, cloneOptions);
174-
log.Info($"Returned path after repository clone: {returnedPath}");
175-
}
176-
}
177-
catch (LibGit2SharpException ex)
178-
{
179-
var message = ex.Message;
180-
if (message.Contains("401"))
181-
{
182-
throw new Exception("Unauthorized: Incorrect username/password");
183-
}
184-
if (message.Contains("403"))
185-
{
186-
throw new Exception("Forbidden: Possibly Incorrect username/password");
187-
}
188-
if (message.Contains("404"))
189-
{
190-
throw new Exception("Not found: The repository was not found");
191-
}
192-
193-
throw new Exception("There was an unknown problem with the Git repository you provided", ex);
132+
var returnedPath = GitRepository.Clone(repositoryUrl, gitDirectory, auth);
133+
log.Info($"Returned path after repository clone: {returnedPath}");
194134
}
195135
}
196136

@@ -201,7 +141,7 @@ private void CloneRepository(string repositoryUrl, string gitDirectory, Authenti
201141
private void NormalizeGitDirectory(string gitDirectory, bool noFetch, string currentBranch, bool isDynamicRepository)
202142
{
203143
var authentication = options.Value.Authentication;
204-
using var repository = new GitRepository(() => gitDirectory);
144+
using IGitRepository repository = new GitRepository(gitDirectory);
205145
// Need to ensure the HEAD does not move, this is essentially a BugCheck
206146
var expectedSha = repository.Head.Tip.Sha;
207147
var expectedBranchName = repository.Head.CanonicalName;
@@ -218,7 +158,7 @@ private void NormalizeGitDirectory(string gitDirectory, bool noFetch, string cur
218158
else
219159
{
220160
log.Info($"Fetching from remote '{remote.Name}' using the following refspecs: {string.Join(", ", remote.FetchRefSpecs.Select(r => r.Specification))}.");
221-
repository.Commands.Fetch(remote.Name, new string[0], authentication.ToFetchOptions(), null);
161+
repository.Commands.Fetch(remote.Name, new string[0], authentication, null);
222162
}
223163

224164
EnsureLocalBranchExistsForCurrentBranch(repository, log, remote, currentBranch);
@@ -292,7 +232,7 @@ private void NormalizeGitDirectory(string gitDirectory, bool noFetch, string cur
292232
else if (localBranchesWhereCommitShaIsHead.Count == 0)
293233
{
294234
log.Info($"No local branch pointing at the commit '{headSha}'. Fake branch needs to be created.");
295-
repository.CreateFakeBranchPointingAtThePullRequestTip(log, authentication);
235+
repository.CreateBranchForPullRequestBranch(log, authentication);
296236
}
297237
else
298238
{

0 commit comments

Comments
 (0)