Skip to content
This repository was archived by the owner on Jun 27, 2019. It is now read-only.

Commit cef7cf5

Browse files
Migration to replace GitPreparer by GitRepositoryFactory
1 parent 8b78374 commit cef7cf5

File tree

11 files changed

+587
-370
lines changed

11 files changed

+587
-370
lines changed

src/GitTools.Core.Tests/Git/GitPreparerTests.cs renamed to src/GitTools.Core.Tests/Git/GitRepositoryFactoryTests.cs

Lines changed: 83 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using Testing;
1212

1313
[TestFixture]
14-
public class GitPreparerTests
14+
public class GitRepositoryFactoryTests
1515
{
1616
const string DefaultBranchName = "master";
1717
const string SpecificBranchName = "feature/foo";
@@ -41,16 +41,20 @@ public void WorksCorrectlyWithRemoteRepository(string branchName, string expecte
4141
// Copy contents into working directory
4242
File.Copy(Path.Combine(fixture.RepositoryPath, "TestFile.txt"), Path.Combine(tempDir, "TestFile.txt"));
4343

44-
var gitPreparer = new GitPreparer(fixture.RepositoryPath, null, new AuthenticationInfo(), false, tempDir);
45-
gitPreparer.Initialise(false, branchName);
46-
dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();
47-
48-
gitPreparer.IsDynamicGitRepository.ShouldBe(true);
49-
gitPreparer.DynamicGitRepositoryPath.ShouldBe(expectedDynamicRepoLocation + "\\.git");
44+
var repositoryInfo = new RepositoryInfo
45+
{
46+
Url = fixture.RepositoryPath,
47+
Branch = branchName
48+
};
5049

51-
using (var repository = new Repository(dynamicRepositoryPath))
50+
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
5251
{
53-
var currentBranch = repository.Head.CanonicalName;
52+
dynamicRepositoryPath = gitRepository.GetDotGitDirectory();
53+
54+
gitRepository.IsDynamic.ShouldBe(true);
55+
gitRepository.GetDotGitDirectory().ShouldBe(expectedDynamicRepoLocation + "\\.git");
56+
57+
var currentBranch = gitRepository.Repository.Head.CanonicalName;
5458

5559
currentBranch.ShouldEndWith(expectedBranchName);
5660
}
@@ -59,8 +63,11 @@ public void WorksCorrectlyWithRemoteRepository(string branchName, string expecte
5963
finally
6064
{
6165
Directory.Delete(tempDir, true);
66+
6267
if (dynamicRepositoryPath != null)
68+
{
6369
DeleteHelper.DeleteGitRepository(dynamicRepositoryPath);
70+
}
6471
}
6572
}
6673

@@ -79,26 +86,35 @@ public void UpdatesExistingDynamicRepository()
7986
{
8087
mainRepositoryFixture.Repository.MakeCommits(1);
8188

82-
var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new AuthenticationInfo(), false, tempDir);
83-
gitPreparer.Initialise(false, "master");
84-
dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();
89+
var repositoryInfo = new RepositoryInfo
90+
{
91+
Url = mainRepositoryFixture.RepositoryPath,
92+
Branch = "master"
93+
};
94+
95+
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
96+
{
97+
dynamicRepositoryPath = gitRepository.GetDotGitDirectory();
98+
}
8599

86100
var newCommit = mainRepositoryFixture.Repository.MakeACommit();
87-
gitPreparer.Initialise(false, "master");
88101

89-
using (var repository = new Repository(dynamicRepositoryPath))
102+
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
90103
{
91104
mainRepositoryFixture.Repository.DumpGraph();
92-
repository.DumpGraph();
93-
repository.Commits.ShouldContain(c => c.Sha == newCommit.Sha);
105+
gitRepository.Repository.DumpGraph();
106+
gitRepository.Repository.Commits.ShouldContain(c => c.Sha == newCommit.Sha);
94107
}
95108
}
96109
}
97110
finally
98111
{
99112
Directory.Delete(tempDir, true);
113+
100114
if (dynamicRepositoryPath != null)
115+
{
101116
DeleteHelper.DeleteGitRepository(dynamicRepositoryPath);
117+
}
102118
}
103119
}
104120

@@ -120,34 +136,34 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
120136
expectedDynamicRepoLocation = Path.Combine(tempPath, fixture.RepositoryPath.Split('\\').Last());
121137
Directory.CreateDirectory(expectedDynamicRepoLocation);
122138

123-
var gitPreparer = new GitPreparer(fixture.RepositoryPath, null, new AuthenticationInfo(), false, tempDir);
124-
gitPreparer.Initialise(false, "master");
139+
var repositoryInfo = new RepositoryInfo
140+
{
141+
Url = fixture.RepositoryPath,
142+
Branch = "master"
143+
};
125144

126-
gitPreparer.IsDynamicGitRepository.ShouldBe(true);
127-
gitPreparer.DynamicGitRepositoryPath.ShouldBe(expectedDynamicRepoLocation + "_1\\.git");
145+
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
146+
{
147+
gitRepository.IsDynamic.ShouldBe(true);
148+
gitRepository.GetDotGitDirectory().ShouldBe(expectedDynamicRepoLocation + "_1\\.git");
149+
}
128150
}
129151
}
130152
finally
131153
{
132154
Directory.Delete(tempDir, true);
133155
if (expectedDynamicRepoLocation != null)
156+
{
134157
Directory.Delete(expectedDynamicRepoLocation, true);
158+
}
159+
135160
if (expectedDynamicRepoLocation != null)
161+
{
136162
DeleteHelper.DeleteGitRepository(expectedDynamicRepoLocation + "_1");
163+
}
137164
}
138165
}
139166

140-
[Test]
141-
public void WorksCorrectlyWithLocalRepository()
142-
{
143-
var tempDir = Path.GetTempPath();
144-
var gitPreparer = new GitPreparer(null, null, null, false, tempDir);
145-
var dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();
146-
147-
dynamicRepositoryPath.ShouldBe(null);
148-
gitPreparer.IsDynamicGitRepository.ShouldBe(false);
149-
}
150-
151167
[Test]
152168
public void UsingDynamicRepositoryWithFeatureBranchWorks()
153169
{
@@ -162,12 +178,21 @@ public void UsingDynamicRepositoryWithFeatureBranchWorks()
162178
{
163179
mainRepositoryFixture.Repository.MakeACommit();
164180

165-
var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new AuthenticationInfo(), false, tempDir);
166-
gitPreparer.Initialise(true, "feature1");
181+
var repositoryInfo = new RepositoryInfo
182+
{
183+
Url = mainRepositoryFixture.RepositoryPath,
184+
Branch = "feature1"
185+
};
167186

168187
mainRepositoryFixture.Repository.Checkout(mainRepositoryFixture.Repository.CreateBranch("feature1"));
169188

170-
Should.NotThrow(() => gitPreparer.Initialise(true, "feature1"));
189+
Should.NotThrow(() =>
190+
{
191+
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
192+
{
193+
// this code shouldn't throw
194+
}
195+
});
171196
}
172197
}
173198
finally
@@ -190,9 +215,19 @@ public void UsingDynamicRepositoryWithoutTargetBranchFails()
190215
{
191216
mainRepositoryFixture.Repository.MakeACommit();
192217

193-
var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new AuthenticationInfo(), false, tempDir);
218+
var repositoryInfo = new RepositoryInfo
219+
{
220+
Url = mainRepositoryFixture.RepositoryPath,
221+
Branch = null
222+
};
194223

195-
Should.Throw<Exception>(() => gitPreparer.Initialise(true, null));
224+
Should.Throw<Exception>(() =>
225+
{
226+
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
227+
{
228+
// this code shouldn't throw
229+
}
230+
});
196231
}
197232
}
198233
finally
@@ -211,9 +246,19 @@ public void TestErrorThrownForInvalidRepository()
211246

212247
try
213248
{
214-
var gitPreparer = new GitPreparer("http://127.0.0.1/testrepo.git", null, new AuthenticationInfo(), false, tempDir);
249+
var repositoryInfo = new RepositoryInfo
250+
{
251+
Url = "http://127.0.0.1/testrepo.git",
252+
Branch = "master"
253+
};
215254

216-
Should.Throw<Exception>(() => gitPreparer.Initialise(true, "master"));
255+
Should.Throw<Exception>(() =>
256+
{
257+
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
258+
{
259+
// this code shouldn't throw
260+
}
261+
});
217262
}
218263
finally
219264
{

src/GitTools.Core.Tests/GitRepositoryTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void NormalisationOfPullRequestsWithFetch()
2222
using (var localFixture = fixture.CloneRepository())
2323
{
2424
localFixture.Checkout(commit.Sha);
25-
GitRepository.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: string.Empty);
25+
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: string.Empty);
2626

2727
var normalisedPullBranch = localFixture.Repository.Branches["pull/3/merge"];
2828
normalisedPullBranch.ShouldNotBe(null);
@@ -43,7 +43,7 @@ public void NormalisationOfPullRequestsWithoutFetch()
4343
using (var localFixture = fixture.CloneRepository())
4444
{
4545
localFixture.Checkout(commit.Sha);
46-
GitRepository.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: true, currentBranch: "refs/pull/3/merge");
46+
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: true, currentBranch: "refs/pull/3/merge");
4747

4848
var normalisedPullBranch = localFixture.Repository.Branches["pull/3/merge"];
4949
normalisedPullBranch.ShouldNotBe(null);
@@ -65,7 +65,7 @@ public void UpdatesLocalBranchesWhen()
6565
localFixture.Checkout("feature/foo");
6666
// Advance remote
6767
var advancedCommit = fixture.Repository.MakeACommit();
68-
GitRepository.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: null);
68+
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: null);
6969

7070
var normalisedBranch = localFixture.Repository.Branches["feature/foo"];
7171
normalisedBranch.ShouldNotBe(null);
@@ -90,7 +90,7 @@ public void UpdatesCurrentBranch()
9090
var advancedCommit = fixture.Repository.MakeACommit();
9191
localFixture.Repository.Network.Fetch(localFixture.Repository.Network.Remotes["origin"]);
9292
localFixture.Repository.Checkout(advancedCommit.Sha);
93-
GitRepository.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: "ref/heads/develop");
93+
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: "ref/heads/develop");
9494

9595
var normalisedBranch = localFixture.Repository.Branches["develop"];
9696
normalisedBranch.ShouldNotBe(null);
@@ -121,7 +121,7 @@ public void ShouldNotChangeBranchWhenNormalizingTheDirectory()
121121
fixture.Checkout("feature/foo");
122122
fixture.Repository.MakeACommit();
123123

124-
GitRepository.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: null);
124+
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: null);
125125

126126
localFixture.Repository.Head.Tip.Sha.ShouldBe(lastCommitOnDevelop.Sha);
127127
}

src/GitTools.Core.Tests/GitTools.Core.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
<Compile Include="Git\Extensions\AuthenticationInfoExtensionsTests.cs" />
8080
<Compile Include="Git\GitDirFinderTests.cs" />
8181
<Compile Include="Git\GitHelperTests.cs" />
82-
<Compile Include="Git\GitPreparerTests.cs" />
82+
<Compile Include="Git\GitRepositoryFactoryTests.cs" />
8383
<Compile Include="GlobalInitialization.cs" />
8484
<Compile Include="ModuleInitializer.cs" />
8585
<Compile Include="Properties\AssemblyInfo.cs" />
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
namespace GitTools
2+
{
3+
using System;
4+
using Logging;
5+
6+
/// <summary>
7+
/// Base class for disposable objects.
8+
/// </summary>
9+
public abstract class Disposable : IDisposable
10+
{
11+
#region Fields
12+
private static readonly ILog Log = LogProvider.GetCurrentClassLogger();
13+
14+
private readonly object _syncRoot = new object();
15+
16+
private bool _disposing;
17+
#endregion
18+
19+
#region Constructors
20+
/// <summary>
21+
/// Finalizes an instance of the <see cref="Disposable"/> class.
22+
/// </summary>
23+
~Disposable()
24+
{
25+
Dispose(false);
26+
}
27+
#endregion
28+
29+
#region Properties
30+
private bool IsDisposed { get; set; }
31+
#endregion
32+
33+
#region Methods
34+
/// <summary>
35+
/// Disposes this instance.
36+
/// </summary>
37+
public void Dispose()
38+
{
39+
Dispose(true);
40+
GC.SuppressFinalize(this);
41+
}
42+
43+
/// <summary>
44+
/// Checks whether the object is disposed. If so, it will throw the <see cref="ObjectDisposedException"/>.
45+
/// </summary>
46+
/// <exception cref="System.ObjectDisposedException">The object is disposed.</exception>
47+
protected void CheckDisposed()
48+
{
49+
lock (_syncRoot)
50+
{
51+
if (IsDisposed)
52+
{
53+
throw new ObjectDisposedException(GetType().FullName);
54+
}
55+
}
56+
}
57+
58+
/// <summary>
59+
/// Disposes the managed resources.
60+
/// </summary>
61+
protected virtual void DisposeManaged()
62+
{
63+
}
64+
65+
/// <summary>
66+
/// Disposes the unmanaged resources.
67+
/// </summary>
68+
protected virtual void DisposeUnmanaged()
69+
{
70+
}
71+
72+
/// <summary>
73+
/// Releases unmanaged and - optionally - managed resources.
74+
/// </summary>
75+
/// <param name="isDisposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
76+
private void Dispose(bool isDisposing)
77+
{
78+
lock (_syncRoot)
79+
{
80+
if (!IsDisposed)
81+
{
82+
if (!_disposing)
83+
{
84+
_disposing = true;
85+
86+
if (isDisposing)
87+
{
88+
try
89+
{
90+
DisposeManaged();
91+
}
92+
catch (Exception ex)
93+
{
94+
//if (ex.IsCritical())
95+
//{
96+
// throw;
97+
//}
98+
99+
Log.ErrorException("Error while disposing managed resources of '{0}'.", ex, GetType().FullName);
100+
}
101+
}
102+
103+
try
104+
{
105+
DisposeUnmanaged();
106+
}
107+
catch (Exception ex)
108+
{
109+
//if (ex.IsCritical())
110+
//{
111+
// throw;
112+
//}
113+
114+
Log.ErrorException("Error while disposing unmanaged resources of '{0}'.", ex, GetType().FullName);
115+
}
116+
117+
IsDisposed = true;
118+
_disposing = false;
119+
}
120+
}
121+
}
122+
}
123+
#endregion
124+
}
125+
}

0 commit comments

Comments
 (0)