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

Commit 65fb363

Browse files
Merge pull request #25 from GitTools/feature/repositoryfactory
Migration to replace GitPreparer by GitRepositoryFactory
2 parents 8b78374 + 24c5835 commit 65fb363

File tree

11 files changed

+600
-365
lines changed

11 files changed

+600
-365
lines changed

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

Lines changed: 91 additions & 32 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.DotGitDirectory;
53+
54+
gitRepository.IsDynamic.ShouldBe(true);
55+
gitRepository.DotGitDirectory.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.DotGitDirectory;
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,32 +136,46 @@ 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.DotGitDirectory.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

140167
[Test]
141-
public void WorksCorrectlyWithLocalRepository()
168+
public void ThrowsExceptionWhenNotEnoughInfo()
142169
{
143170
var tempDir = Path.GetTempPath();
144-
var gitPreparer = new GitPreparer(null, null, null, false, tempDir);
145-
var dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();
146171

147-
dynamicRepositoryPath.ShouldBe(null);
148-
gitPreparer.IsDynamicGitRepository.ShouldBe(false);
172+
var repositoryInfo = new RepositoryInfo
173+
{
174+
Url = tempDir,
175+
Branch = "master"
176+
};
177+
178+
Should.Throw<Exception>(() => GitRepositoryFactory.CreateRepository(repositoryInfo));
149179
}
150180

151181
[Test]
@@ -162,12 +192,21 @@ public void UsingDynamicRepositoryWithFeatureBranchWorks()
162192
{
163193
mainRepositoryFixture.Repository.MakeACommit();
164194

165-
var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new AuthenticationInfo(), false, tempDir);
166-
gitPreparer.Initialise(true, "feature1");
195+
var repositoryInfo = new RepositoryInfo
196+
{
197+
Url = mainRepositoryFixture.RepositoryPath,
198+
Branch = "feature1"
199+
};
167200

168201
mainRepositoryFixture.Repository.Checkout(mainRepositoryFixture.Repository.CreateBranch("feature1"));
169202

170-
Should.NotThrow(() => gitPreparer.Initialise(true, "feature1"));
203+
Should.NotThrow(() =>
204+
{
205+
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
206+
{
207+
// this code shouldn't throw
208+
}
209+
});
171210
}
172211
}
173212
finally
@@ -190,9 +229,19 @@ public void UsingDynamicRepositoryWithoutTargetBranchFails()
190229
{
191230
mainRepositoryFixture.Repository.MakeACommit();
192231

193-
var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new AuthenticationInfo(), false, tempDir);
232+
var repositoryInfo = new RepositoryInfo
233+
{
234+
Url = mainRepositoryFixture.RepositoryPath,
235+
Branch = null
236+
};
194237

195-
Should.Throw<Exception>(() => gitPreparer.Initialise(true, null));
238+
Should.Throw<Exception>(() =>
239+
{
240+
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
241+
{
242+
// this code shouldn't throw
243+
}
244+
});
196245
}
197246
}
198247
finally
@@ -211,9 +260,19 @@ public void TestErrorThrownForInvalidRepository()
211260

212261
try
213262
{
214-
var gitPreparer = new GitPreparer("http://127.0.0.1/testrepo.git", null, new AuthenticationInfo(), false, tempDir);
263+
var repositoryInfo = new RepositoryInfo
264+
{
265+
Url = "http://127.0.0.1/testrepo.git",
266+
Branch = "master"
267+
};
215268

216-
Should.Throw<Exception>(() => gitPreparer.Initialise(true, "master"));
269+
Should.Throw<Exception>(() =>
270+
{
271+
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
272+
{
273+
// this code shouldn't throw
274+
}
275+
});
217276
}
218277
finally
219278
{

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)