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

Commit a1c9cdc

Browse files
committed
WIP, fixing up dynamic repositories so it can be used by gitversion
1 parent f59fe04 commit a1c9cdc

File tree

5 files changed

+54
-116
lines changed

5 files changed

+54
-116
lines changed

src/GitTools.Core.Tests/Git/GitRepositoryFactoryTests.cs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,22 @@ public void WorksCorrectlyWithRemoteRepository(string branchName, string expecte
3737
fixture.Repository.MakeCommits(5);
3838
fixture.Repository.CreateFileAndCommit("TestFile.txt");
3939

40-
fixture.Repository.CreateBranch(SpecificBranchName);
40+
var branch = fixture.Repository.CreateBranch(SpecificBranchName);
4141

4242
// Copy contents into working directory
4343
File.Copy(Path.Combine(fixture.RepositoryPath, "TestFile.txt"), Path.Combine(tempDir, "TestFile.txt"));
4444

4545
var repositoryInfo = new RepositoryInfo
4646
{
47-
Url = fixture.RepositoryPath,
48-
Branch = branchName
47+
Url = fixture.RepositoryPath
4948
};
5049

51-
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
50+
using (var gitRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, branchName, branch.Tip.Sha))
5251
{
53-
dynamicRepositoryPath = gitRepository.DotGitDirectory;
54-
55-
gitRepository.IsDynamic.ShouldBe(true);
56-
gitRepository.DotGitDirectory.ShouldBe(Path.Combine(expectedDynamicRepoLocation, ".git"));
52+
dynamicRepositoryPath = gitRepository.Info.Path;
53+
gitRepository.Info.Path.ShouldBe(Path.Combine(expectedDynamicRepoLocation, ".git"));
5754

58-
var currentBranch = gitRepository.Repository.Head.CanonicalName;
55+
var currentBranch = gitRepository.Head.CanonicalName;
5956

6057
currentBranch.ShouldEndWith(expectedBranchName);
6158
}
@@ -85,26 +82,24 @@ public void UpdatesExistingDynamicRepository()
8582
{
8683
using (var mainRepositoryFixture = new EmptyRepositoryFixture())
8784
{
88-
mainRepositoryFixture.Repository.MakeCommits(1);
85+
var commit = mainRepositoryFixture.Repository.MakeACommit();
8986

9087
var repositoryInfo = new RepositoryInfo
9188
{
92-
Url = mainRepositoryFixture.RepositoryPath,
93-
Branch = "master"
89+
Url = mainRepositoryFixture.RepositoryPath
9490
};
9591

96-
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
92+
using (var gitRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", commit.Sha))
9793
{
98-
dynamicRepositoryPath = gitRepository.DotGitDirectory;
94+
dynamicRepositoryPath = gitRepository.Info.Path;
9995
}
10096

10197
var newCommit = mainRepositoryFixture.Repository.MakeACommit();
10298

103-
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
99+
using (var gitRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", commit.Sha))
104100
{
105-
mainRepositoryFixture.Repository.DumpGraph();
106-
gitRepository.Repository.DumpGraph();
107-
gitRepository.Repository.Commits.ShouldContain(c => c.Sha == newCommit.Sha);
101+
gitRepository.Info.Path.ShouldBe(dynamicRepositoryPath);
102+
gitRepository.Commits.ShouldContain(c => c.Sha == newCommit.Sha);
108103
}
109104
}
110105
}
@@ -141,10 +136,10 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
141136
var repositoryInfo = new RepositoryInfo
142137
{
143138
Url = fixture.RepositoryPath,
144-
Branch = "master"
139+
TargetBranch = "master"
145140
};
146141

147-
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
142+
using (var gitRepository = DynamicRepositories.CreateOrOpen(repositoryInfo))
148143
{
149144
gitRepository.IsDynamic.ShouldBe(true);
150145
gitRepository.DotGitDirectory.ShouldBe(Path.Combine(expectedDynamicRepoLocation + "_1", ".git"));
@@ -174,10 +169,10 @@ public void ThrowsExceptionWhenNotEnoughInfo()
174169
var repositoryInfo = new RepositoryInfo
175170
{
176171
Url = tempDir,
177-
Branch = "master"
172+
TargetBranch = "master"
178173
};
179174

180-
Should.Throw<Exception>(() => GitRepositoryFactory.CreateRepository(repositoryInfo));
175+
Should.Throw<Exception>(() => DynamicRepositories.CreateOrOpen(repositoryInfo));
181176
}
182177

183178
[Test]
@@ -197,14 +192,14 @@ public void UsingDynamicRepositoryWithFeatureBranchWorks()
197192
var repositoryInfo = new RepositoryInfo
198193
{
199194
Url = mainRepositoryFixture.RepositoryPath,
200-
Branch = "feature1"
195+
TargetBranch = "feature1"
201196
};
202197

203198
mainRepositoryFixture.Repository.Checkout(mainRepositoryFixture.Repository.CreateBranch("feature1"));
204199

205200
Should.NotThrow(() =>
206201
{
207-
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
202+
using (var gitRepository = DynamicRepositories.CreateOrOpen(repositoryInfo))
208203
{
209204
// this code shouldn't throw
210205
}
@@ -234,12 +229,12 @@ public void UsingDynamicRepositoryWithoutTargetBranchFails()
234229
var repositoryInfo = new RepositoryInfo
235230
{
236231
Url = mainRepositoryFixture.RepositoryPath,
237-
Branch = null
232+
TargetBranch = null
238233
};
239234

240235
Should.Throw<Exception>(() =>
241236
{
242-
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
237+
using (var gitRepository = DynamicRepositories.CreateOrOpen(repositoryInfo))
243238
{
244239
// this code shouldn't throw
245240
}
@@ -265,12 +260,12 @@ public void TestErrorThrownForInvalidRepository()
265260
var repositoryInfo = new RepositoryInfo
266261
{
267262
Url = "http://127.0.0.1/testrepo.git",
268-
Branch = "master"
263+
TargetBranch = "master"
269264
};
270265

271266
Should.Throw<Exception>(() =>
272267
{
273-
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
268+
using (var gitRepository = DynamicRepositories.CreateOrOpen(repositoryInfo))
274269
{
275270
// this code shouldn't throw
276271
}

src/GitTools.Core/GitTools.Core.Shared/Git/GitRepositoryFactory.cs renamed to src/GitTools.Core/GitTools.Core.Shared/Git/DynamicRepositories.cs

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,30 @@
66
using LibGit2Sharp;
77
using Logging;
88

9-
public static class GitRepositoryFactory
9+
public static class DynamicRepositories
1010
{
1111
static readonly ILog Log = LogProvider.GetCurrentClassLogger();
1212

1313
/// <summary>
14-
/// Creates the repository based on the repository info. If the <see cref="RepositoryInfo.Directory"/> points
15-
/// to a valid directory, it will be used as the source for the git repository. Otherwise this method will create
16-
/// a dynamic repository based on the url and authentication info.
14+
/// Creates a dynamic repository based on the repository info
1715
/// </summary>
18-
/// <param name="repositoryInfo">The repository information.</param>
16+
/// <param name="repositoryInfo">The source repository information.</param>
17+
/// <param name="dynamicRepsitoryPath">The path to create the dynamic repository, NOT thread safe.</param>
18+
/// <param name="targetBranch"></param>
19+
/// <param name="targetCommit"></param>
1920
/// <param name="noFetch">If set to <c>true</c>, don't fetch anything.</param>
2021
/// <returns>The git repository.</returns>
21-
public static GitRepository CreateRepository(RepositoryInfo repositoryInfo, bool noFetch = false)
22+
public static Repository CreateOrOpen(RepositoryInfo repositoryInfo, string dynamicRepsitoryPath, string targetBranch, string targetCommit, bool noFetch = false)
2223
{
23-
bool isDynamicRepository = false;
24-
string repositoryDirectory = null;
25-
26-
// TODO: find a better way to check for existing repositories
27-
if (!string.IsNullOrWhiteSpace(repositoryInfo.Directory))
28-
{
29-
var expectedDirectory = Path.Combine(repositoryInfo.Directory, ".git");
30-
if (Directory.Exists(expectedDirectory))
31-
{
32-
repositoryDirectory = expectedDirectory;
33-
}
34-
}
35-
36-
if (string.IsNullOrWhiteSpace(repositoryDirectory))
37-
{
38-
isDynamicRepository = true;
24+
if (string.IsNullOrWhiteSpace(targetBranch))
25+
throw new GitToolsException("Dynamic Git repositories must have a target branch");
26+
if (string.IsNullOrWhiteSpace(targetCommit))
27+
throw new GitToolsException("Dynamic Git repositories must have a target commit");
3928

40-
var tempRepositoryPath = CalculateTemporaryRepositoryPath(repositoryInfo.Url, repositoryDirectory);
41-
repositoryDirectory = CreateDynamicRepository(tempRepositoryPath, repositoryInfo.Authentication,
42-
repositoryInfo.Url, repositoryInfo.Branch, noFetch);
43-
}
29+
var tempRepositoryPath = CalculateTemporaryRepositoryPath(repositoryInfo.Url, dynamicRepsitoryPath);
30+
var dynamicRepositoryPath = CreateDynamicRepository(tempRepositoryPath, repositoryInfo, noFetch, targetBranch, targetCommit);
4431

45-
if (string.IsNullOrWhiteSpace(repositoryDirectory))
46-
{
47-
Log.Warn("Could not create a repository, not enough information was specified");
48-
return null;
49-
}
50-
51-
// TODO: Should we do something with fetch for existing repositoriess?
52-
var repository = new Repository(repositoryDirectory);
53-
return new GitRepository(repository, isDynamicRepository);
32+
return new Repository(dynamicRepositoryPath);
5433
}
5534

5635
static string CalculateTemporaryRepositoryPath(string targetUrl, string dynamicRepositoryLocation)
@@ -98,33 +77,39 @@ static bool GitRepoHasMatchingRemote(string possiblePath, string targetUrl)
9877
}
9978
}
10079

101-
static string CreateDynamicRepository(string targetPath, AuthenticationInfo authentication, string repositoryUrl, string targetBranch, bool noFetch)
80+
static string CreateDynamicRepository(string targetPath, RepositoryInfo repositoryInfo, bool noFetch, string targetBranch, string targetCommit)
10281
{
103-
if (string.IsNullOrWhiteSpace(targetBranch))
104-
{
105-
throw new GitToolsException("Dynamic Git repositories must have a target branch (/b)");
106-
}
107-
10882
Log.Info(string.Format("Creating dynamic repository at '{0}'", targetPath));
10983

11084
var gitDirectory = Path.Combine(targetPath, ".git");
11185
if (Directory.Exists(targetPath))
11286
{
11387
Log.Info("Git repository already exists");
114-
GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch);
88+
CheckoutCommit(targetCommit, gitDirectory);
89+
GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, repositoryInfo.Authentication, noFetch, targetBranch);
11590

11691
return gitDirectory;
11792
}
11893

119-
CloneRepository(repositoryUrl, gitDirectory, authentication);
94+
CloneRepository(repositoryInfo.Url, gitDirectory, repositoryInfo.Authentication);
95+
CheckoutCommit(targetCommit, gitDirectory);
12096

12197
// Normalize (download branches) before using the branch
122-
GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch);
98+
GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, repositoryInfo.Authentication, noFetch, targetBranch);
12399

124100
return gitDirectory;
125101
}
126102

127-
private static void CloneRepository(string repositoryUrl, string gitDirectory, AuthenticationInfo authentication)
103+
static void CheckoutCommit(string targetCommit, string gitDirectory)
104+
{
105+
using (var repo = new Repository(gitDirectory))
106+
{
107+
Log.Info(string.Format("Checking out {0}", targetCommit));
108+
repo.Checkout(targetCommit);
109+
}
110+
}
111+
112+
static void CloneRepository(string repositoryUrl, string gitDirectory, AuthenticationInfo authentication)
128113
{
129114
Credentials credentials = null;
130115
if (!string.IsNullOrWhiteSpace(authentication.Username) && !string.IsNullOrWhiteSpace(authentication.Password))

src/GitTools.Core/GitTools.Core.Shared/Git/GitRepository.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/GitTools.Core/GitTools.Core.Shared/Git/RepositoryInfo.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ public RepositoryInfo()
88
}
99

1010
public AuthenticationInfo Authentication { get; set; }
11-
12-
public string Directory { get; set; }
13-
14-
public string Branch { get; set; }
15-
1611
public string Url { get; set; }
1712
}
1813
}

src/GitTools.Core/GitTools.Core.Shared/GitTools.Core.Shared.projitems

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
<Compile Include="$(MSBuildThisFileDirectory)Git\Extensions\IRepositoryExtensions.cs" />
1919
<Compile Include="$(MSBuildThisFileDirectory)Git\Extensions\LibGitExtensions.cs" />
2020
<Compile Include="$(MSBuildThisFileDirectory)Git\GitDirFinder.cs" />
21-
<Compile Include="$(MSBuildThisFileDirectory)Git\GitRepository.cs" />
22-
<Compile Include="$(MSBuildThisFileDirectory)Git\GitRepositoryFactory.cs" />
21+
<Compile Include="$(MSBuildThisFileDirectory)Git\DynamicRepositories.cs" />
2322
<Compile Include="$(MSBuildThisFileDirectory)Git\Helpers\BugException.cs" />
2423
<Compile Include="$(MSBuildThisFileDirectory)Git\Helpers\GitRepositoryHelper.cs" />
2524
<Compile Include="$(MSBuildThisFileDirectory)Git\RepositoryInfo.cs" />

0 commit comments

Comments
 (0)