Skip to content

Commit 55d6f2c

Browse files
committed
Merge pull request #399 from JakeGinnivan/DynamicRepositoryLocation
Dynamic repository location
2 parents 5415748 + c7ea305 commit 55d6f2c

19 files changed

+333
-226
lines changed

GitVersionCore.Tests/GitDirFinderTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,26 @@ public void Cleanup()
3030
[Test]
3131
public void From_WorkingDirectory()
3232
{
33-
Assert.AreEqual(gitDirectory, GitDirFinder.TreeWalkForGitDir(workDirectory));
33+
Assert.AreEqual(gitDirectory, GitDirFinder.TreeWalkForDotGitDir(workDirectory));
3434
}
3535

3636
[Test]
3737
public void From_WorkingDirectory_Parent()
3838
{
3939
var parentDirectory = Directory.GetParent(workDirectory).FullName;
40-
Assert.Null(GitDirFinder.TreeWalkForGitDir(parentDirectory));
40+
Assert.Null(GitDirFinder.TreeWalkForDotGitDir(parentDirectory));
4141
}
4242

4343
[Test]
4444
public void From_GitDirectory()
4545
{
46-
Assert.AreEqual(gitDirectory, GitDirFinder.TreeWalkForGitDir(gitDirectory));
46+
Assert.AreEqual(gitDirectory, GitDirFinder.TreeWalkForDotGitDir(gitDirectory));
4747
}
4848

4949
[Test]
5050
public void From_RefsDirectory()
5151
{
5252
var refsDirectory = Path.Combine(gitDirectory, "refs");
53-
Assert.AreEqual(gitDirectory, GitDirFinder.TreeWalkForGitDir(refsDirectory));
53+
Assert.AreEqual(gitDirectory, GitDirFinder.TreeWalkForDotGitDir(refsDirectory));
5454
}
5555
}

GitVersionCore/GitDirFinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
public class GitDirFinder
77
{
8-
public static string TreeWalkForGitDir(string currentDirectory)
8+
public static string TreeWalkForDotGitDir(string currentDirectory)
99
{
1010
var gitDirectory = Repository.Discover(currentDirectory);
1111

GitVersionCore/GitVersionCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<Compile Include="Helpers\FileSystem.cs" />
8585
<Compile Include="Helpers\IFileSystem.cs" />
8686
<Compile Include="Helpers\ProcessHelper.cs" />
87+
<Compile Include="Helpers\ServiceMessageEscapeHelper.cs" />
8788
<Compile Include="OutputVariables\VersionVariables.cs" />
8889
<Compile Include="Extensions\ExtensionMethods.git.cs" />
8990
<Compile Include="SemanticVersionExtensions.cs" />

GitVersionCore/Helpers/DeleteHelper.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,6 @@
22
{
33
using System.IO;
44

5-
public static class ServiceMessageEscapeHelper
6-
{
7-
public static string EscapeValue(string value)
8-
{
9-
if (value == null)
10-
{
11-
return null;
12-
}
13-
// List of escape values from http://confluence.jetbrains.com/display/TCD8/Build+Script+Interaction+with+TeamCity
14-
15-
value = value.Replace("|", "||");
16-
value = value.Replace("'", "|'");
17-
value = value.Replace("[", "|[");
18-
value = value.Replace("]", "|]");
19-
value = value.Replace("\r", "|r");
20-
value = value.Replace("\n", "|n");
21-
22-
return value;
23-
}
24-
}
255
public static class DeleteHelper
266
{
277
public static void DeleteGitRepository(string directory)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace GitVersion
2+
{
3+
public static class ServiceMessageEscapeHelper
4+
{
5+
public static string EscapeValue(string value)
6+
{
7+
if (value == null)
8+
{
9+
return null;
10+
}
11+
// List of escape values from http://confluence.jetbrains.com/display/TCD8/Build+Script+Interaction+with+TeamCity
12+
13+
value = value.Replace("|", "||");
14+
value = value.Replace("'", "|'");
15+
value = value.Replace("[", "|[");
16+
value = value.Replace("]", "|]");
17+
value = value.Replace("\r", "|r");
18+
value = value.Replace("\n", "|n");
19+
20+
return value;
21+
}
22+
}
23+
}

GitVersionExe.Tests/ArgumentParserTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ public void update_assembly_info_with_relative_filename()
205205
arguments.UpdateAssemblyInfoFileName.ShouldBe("..\\..\\CommonAssemblyInfo.cs");
206206
}
207207

208+
[Test]
209+
public void dynamicRepoLocation()
210+
{
211+
var arguments = ArgumentParser.ParseArguments("-dynamicRepoLocation c:\\foo\\");
212+
arguments.DynamicRepositoryLocation.ShouldBe("c:\\foo\\");
213+
}
214+
208215
[Test]
209216
public void can_log_to_console()
210217
{

GitVersionExe.Tests/GitPreparerTests.cs

Lines changed: 83 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
3+
using System.Linq;
24
using GitVersion;
35
using LibGit2Sharp;
46
using NUnit.Framework;
@@ -18,64 +20,103 @@ public GitPreparerTests()
1820
const string SpecificBranchName = "feature/foo";
1921

2022
[Test]
21-
[TestCase(null, DefaultBranchName, false)]
22-
[TestCase(SpecificBranchName, SpecificBranchName, false)]
23-
[TestCase(null, DefaultBranchName, true)]
24-
[TestCase(SpecificBranchName, SpecificBranchName, true)]
25-
public void WorksCorrectlyWithRemoteRepository(string branchName, string expectedBranchName, bool checkConfig)
23+
[TestCase(null, DefaultBranchName)]
24+
[TestCase(SpecificBranchName, SpecificBranchName)]
25+
public void WorksCorrectlyWithRemoteRepository(string branchName, string expectedBranchName)
2626
{
27-
var tempDir = Path.GetTempPath();
27+
var repoName = Guid.NewGuid().ToString();
28+
var tempPath = Path.GetTempPath();
29+
var tempDir = Path.Combine(tempPath, repoName);
30+
Directory.CreateDirectory(tempDir);
31+
string dynamicRepositoryPath = null;
2832

29-
using (var fixture = new EmptyRepositoryFixture(new Config()))
33+
try
3034
{
31-
fixture.Repository.MakeCommits(5);
32-
33-
if (checkConfig)
35+
using (var fixture = new EmptyRepositoryFixture(new Config()))
3436
{
35-
fixture.Repository.CreateFileAndCommit("GitVersionConfig.yaml");
36-
}
37+
var expectedDynamicRepoLocation = Path.Combine(tempPath, fixture.RepositoryPath.Split('\\').Last());
3738

38-
fixture.Repository.CreateBranch(SpecificBranchName);
39+
fixture.Repository.MakeCommits(5);
40+
fixture.Repository.CreateFileAndCommit("TestFile.txt");
3941

40-
if (checkConfig)
41-
{
42-
fixture.Repository.Refs.UpdateTarget(fixture.Repository.Refs.Head, fixture.Repository.Refs["refs/heads/" + SpecificBranchName]);
42+
fixture.Repository.CreateBranch(SpecificBranchName);
4343

44-
fixture.Repository.CreateFileAndCommit("GitVersionConfig.yaml");
44+
var arguments = new Arguments
45+
{
46+
TargetPath = tempDir,
47+
TargetUrl = fixture.RepositoryPath
48+
};
4549

46-
fixture.Repository.Refs.UpdateTarget(fixture.Repository.Refs.Head, fixture.Repository.Refs["refs/heads/" + DefaultBranchName]);
47-
}
50+
// Copy contents into working directory
51+
File.Copy(Path.Combine(fixture.RepositoryPath, "TestFile.txt"), Path.Combine(tempDir, "TestFile.txt"));
4852

49-
var arguments = new Arguments
50-
{
51-
TargetPath = tempDir,
52-
TargetUrl = fixture.RepositoryPath
53-
};
53+
if (!string.IsNullOrWhiteSpace(branchName))
54+
{
55+
arguments.TargetBranch = branchName;
56+
}
5457

55-
if (!string.IsNullOrWhiteSpace(branchName))
56-
{
57-
arguments.TargetBranch = branchName;
58-
}
58+
var gitPreparer = new GitPreparer(arguments);
59+
gitPreparer.InitialiseDynamicRepositoryIfNeeded();
60+
dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();
5961

60-
var gitPreparer = new GitPreparer(arguments);
61-
var dynamicRepositoryPath = gitPreparer.Prepare();
62+
gitPreparer.IsDynamicGitRepository.ShouldBe(true);
63+
gitPreparer.DynamicGitRepositoryPath.ShouldBe(expectedDynamicRepoLocation + "\\.git");
6264

63-
dynamicRepositoryPath.ShouldBe(Path.Combine(tempDir, "_dynamicrepository", ".git"));
64-
gitPreparer.IsDynamicGitRepository.ShouldBe(true);
65+
using (var repository = new Repository(dynamicRepositoryPath))
66+
{
67+
var currentBranch = repository.Head.CanonicalName;
6568

66-
using (var repository = new Repository(dynamicRepositoryPath))
67-
{
68-
var currentBranch = repository.Head.CanonicalName;
69+
currentBranch.EndsWith(expectedBranchName).ShouldBe(true);
70+
}
71+
}
72+
}
73+
finally
74+
{
75+
Directory.Delete(tempDir, true);
76+
if (dynamicRepositoryPath != null)
77+
DeleteHelper.DeleteGitRepository(dynamicRepositoryPath);
78+
}
79+
}
80+
81+
[Test]
82+
public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
83+
{
84+
var repoName = Guid.NewGuid().ToString();
85+
var tempPath = Path.GetTempPath();
86+
var tempDir = Path.Combine(tempPath, repoName);
87+
Directory.CreateDirectory(tempDir);
88+
string expectedDynamicRepoLocation = null;
6989

70-
currentBranch.EndsWith(expectedBranchName).ShouldBe(true);
90+
try
91+
{
92+
using (var fixture = new EmptyRepositoryFixture(new Config()))
93+
{
94+
fixture.Repository.CreateFileAndCommit("TestFile.txt");
95+
File.Copy(Path.Combine(fixture.RepositoryPath, "TestFile.txt"), Path.Combine(tempDir, "TestFile.txt"));
96+
expectedDynamicRepoLocation = Path.Combine(tempPath, fixture.RepositoryPath.Split('\\').Last());
97+
Directory.CreateDirectory(expectedDynamicRepoLocation);
7198

72-
if (checkConfig)
99+
var arguments = new Arguments
73100
{
74-
var expectedConfigPath = Path.Combine(dynamicRepositoryPath, "..\\GitVersionConfig.yaml");
75-
File.Exists(expectedConfigPath).ShouldBe(true);
76-
}
101+
TargetPath = tempDir,
102+
TargetUrl = fixture.RepositoryPath
103+
};
104+
105+
var gitPreparer = new GitPreparer(arguments);
106+
gitPreparer.InitialiseDynamicRepositoryIfNeeded();
107+
108+
gitPreparer.IsDynamicGitRepository.ShouldBe(true);
109+
gitPreparer.DynamicGitRepositoryPath.ShouldBe(expectedDynamicRepoLocation + "_1\\.git");
77110
}
78111
}
112+
finally
113+
{
114+
Directory.Delete(tempDir, true);
115+
if (expectedDynamicRepoLocation != null)
116+
Directory.Delete(expectedDynamicRepoLocation, true);
117+
if (expectedDynamicRepoLocation != null)
118+
DeleteHelper.DeleteGitRepository(expectedDynamicRepoLocation + "_1");
119+
}
79120
}
80121

81122
[Test]
@@ -89,7 +130,7 @@ public void WorksCorrectlyWithLocalRepository()
89130
};
90131

91132
var gitPreparer = new GitPreparer(arguments);
92-
var dynamicRepositoryPath = gitPreparer.Prepare();
133+
var dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();
93134

94135
dynamicRepositoryPath.ShouldBe(null);
95136
gitPreparer.IsDynamicGitRepository.ShouldBe(false);

GitVersionExe.Tests/HelpWriterTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public void AllArgsAreInHelp()
1515
{ "Init", "init" },
1616
{ "TargetBranch", "/b" },
1717
{ "LogFilePath" , "/l" },
18+
{ "DynamicRepositoryLocation" , "/dynamicRepoLocation" },
1819
{ "IsHelp", "/?" }
1920
};
2021
string helpText = null;

GitVersionExe/ArgumentParser.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
8484
continue;
8585
}
8686

87+
if (IsSwitch("dynamicRepoLocation", name))
88+
{
89+
arguments.DynamicRepositoryLocation = value;
90+
continue;
91+
}
92+
8793
if (IsSwitch("url", name))
8894
{
8995
arguments.TargetUrl = value;

GitVersionExe/Arguments.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public Arguments()
1515
public string TargetUrl;
1616
public string TargetBranch;
1717
public string CommitId;
18+
public string DynamicRepositoryLocation;
1819

1920
public bool Init;
2021

0 commit comments

Comments
 (0)