Skip to content

Dynamic repository location #399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
8 changes: 4 additions & 4 deletions GitVersionCore.Tests/GitDirFinderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,26 @@ public void Cleanup()
[Test]
public void From_WorkingDirectory()
{
Assert.AreEqual(gitDirectory, GitDirFinder.TreeWalkForGitDir(workDirectory));
Assert.AreEqual(gitDirectory, GitDirFinder.TreeWalkForDotGitDir(workDirectory));
}

[Test]
public void From_WorkingDirectory_Parent()
{
var parentDirectory = Directory.GetParent(workDirectory).FullName;
Assert.Null(GitDirFinder.TreeWalkForGitDir(parentDirectory));
Assert.Null(GitDirFinder.TreeWalkForDotGitDir(parentDirectory));
}

[Test]
public void From_GitDirectory()
{
Assert.AreEqual(gitDirectory, GitDirFinder.TreeWalkForGitDir(gitDirectory));
Assert.AreEqual(gitDirectory, GitDirFinder.TreeWalkForDotGitDir(gitDirectory));
}

[Test]
public void From_RefsDirectory()
{
var refsDirectory = Path.Combine(gitDirectory, "refs");
Assert.AreEqual(gitDirectory, GitDirFinder.TreeWalkForGitDir(refsDirectory));
Assert.AreEqual(gitDirectory, GitDirFinder.TreeWalkForDotGitDir(refsDirectory));
}
}
2 changes: 1 addition & 1 deletion GitVersionCore/GitDirFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public class GitDirFinder
{
public static string TreeWalkForGitDir(string currentDirectory)
public static string TreeWalkForDotGitDir(string currentDirectory)
{
var gitDirectory = Repository.Discover(currentDirectory);

Expand Down
1 change: 1 addition & 0 deletions GitVersionCore/GitVersionCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<Compile Include="Helpers\FileSystem.cs" />
<Compile Include="Helpers\IFileSystem.cs" />
<Compile Include="Helpers\ProcessHelper.cs" />
<Compile Include="Helpers\ServiceMessageEscapeHelper.cs" />
<Compile Include="OutputVariables\VersionVariables.cs" />
<Compile Include="Extensions\ExtensionMethods.git.cs" />
<Compile Include="SemanticVersionExtensions.cs" />
Expand Down
20 changes: 0 additions & 20 deletions GitVersionCore/Helpers/DeleteHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,6 @@
{
using System.IO;

public static class ServiceMessageEscapeHelper
{
public static string EscapeValue(string value)
{
if (value == null)
{
return null;
}
// List of escape values from http://confluence.jetbrains.com/display/TCD8/Build+Script+Interaction+with+TeamCity

value = value.Replace("|", "||");
value = value.Replace("'", "|'");
value = value.Replace("[", "|[");
value = value.Replace("]", "|]");
value = value.Replace("\r", "|r");
value = value.Replace("\n", "|n");

return value;
}
}
public static class DeleteHelper
{
public static void DeleteGitRepository(string directory)
Expand Down
23 changes: 23 additions & 0 deletions GitVersionCore/Helpers/ServiceMessageEscapeHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace GitVersion
{
public static class ServiceMessageEscapeHelper
{
public static string EscapeValue(string value)
{
if (value == null)
{
return null;
}
// List of escape values from http://confluence.jetbrains.com/display/TCD8/Build+Script+Interaction+with+TeamCity

value = value.Replace("|", "||");
value = value.Replace("'", "|'");
value = value.Replace("[", "|[");
value = value.Replace("]", "|]");
value = value.Replace("\r", "|r");
value = value.Replace("\n", "|n");

return value;
}
}
}
7 changes: 7 additions & 0 deletions GitVersionExe.Tests/ArgumentParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ public void update_assembly_info_with_relative_filename()
arguments.UpdateAssemblyInfoFileName.ShouldBe("..\\..\\CommonAssemblyInfo.cs");
}

[Test]
public void dynamicRepoLocation()
{
var arguments = ArgumentParser.ParseArguments("-dynamicRepoLocation c:\\foo\\");
arguments.DynamicRepositoryLocation.ShouldBe("c:\\foo\\");
}

[Test]
public void can_log_to_console()
{
Expand Down
125 changes: 83 additions & 42 deletions GitVersionExe.Tests/GitPreparerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.IO;
using System;
using System.IO;
using System.Linq;
using GitVersion;
using LibGit2Sharp;
using NUnit.Framework;
Expand All @@ -18,64 +20,103 @@ public GitPreparerTests()
const string SpecificBranchName = "feature/foo";

[Test]
[TestCase(null, DefaultBranchName, false)]
[TestCase(SpecificBranchName, SpecificBranchName, false)]
[TestCase(null, DefaultBranchName, true)]
[TestCase(SpecificBranchName, SpecificBranchName, true)]
public void WorksCorrectlyWithRemoteRepository(string branchName, string expectedBranchName, bool checkConfig)
[TestCase(null, DefaultBranchName)]
[TestCase(SpecificBranchName, SpecificBranchName)]
public void WorksCorrectlyWithRemoteRepository(string branchName, string expectedBranchName)
{
var tempDir = Path.GetTempPath();
var repoName = Guid.NewGuid().ToString();
var tempPath = Path.GetTempPath();
var tempDir = Path.Combine(tempPath, repoName);
Directory.CreateDirectory(tempDir);
string dynamicRepositoryPath = null;

using (var fixture = new EmptyRepositoryFixture(new Config()))
try
{
fixture.Repository.MakeCommits(5);

if (checkConfig)
using (var fixture = new EmptyRepositoryFixture(new Config()))
{
fixture.Repository.CreateFileAndCommit("GitVersionConfig.yaml");
}
var expectedDynamicRepoLocation = Path.Combine(tempPath, fixture.RepositoryPath.Split('\\').Last());

fixture.Repository.CreateBranch(SpecificBranchName);
fixture.Repository.MakeCommits(5);
fixture.Repository.CreateFileAndCommit("TestFile.txt");

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

fixture.Repository.CreateFileAndCommit("GitVersionConfig.yaml");
var arguments = new Arguments
{
TargetPath = tempDir,
TargetUrl = fixture.RepositoryPath
};

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

var arguments = new Arguments
{
TargetPath = tempDir,
TargetUrl = fixture.RepositoryPath
};
if (!string.IsNullOrWhiteSpace(branchName))
{
arguments.TargetBranch = branchName;
}

if (!string.IsNullOrWhiteSpace(branchName))
{
arguments.TargetBranch = branchName;
}
var gitPreparer = new GitPreparer(arguments);
gitPreparer.InitialiseDynamicRepositoryIfNeeded();
dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();

var gitPreparer = new GitPreparer(arguments);
var dynamicRepositoryPath = gitPreparer.Prepare();
gitPreparer.IsDynamicGitRepository.ShouldBe(true);
gitPreparer.DynamicGitRepositoryPath.ShouldBe(expectedDynamicRepoLocation + "\\.git");

dynamicRepositoryPath.ShouldBe(Path.Combine(tempDir, "_dynamicrepository", ".git"));
gitPreparer.IsDynamicGitRepository.ShouldBe(true);
using (var repository = new Repository(dynamicRepositoryPath))
{
var currentBranch = repository.Head.CanonicalName;

using (var repository = new Repository(dynamicRepositoryPath))
{
var currentBranch = repository.Head.CanonicalName;
currentBranch.EndsWith(expectedBranchName).ShouldBe(true);
}
}
}
finally
{
Directory.Delete(tempDir, true);
if (dynamicRepositoryPath != null)
DeleteHelper.DeleteGitRepository(dynamicRepositoryPath);
}
}

[Test]
public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
{
var repoName = Guid.NewGuid().ToString();
var tempPath = Path.GetTempPath();
var tempDir = Path.Combine(tempPath, repoName);
Directory.CreateDirectory(tempDir);
string expectedDynamicRepoLocation = null;

currentBranch.EndsWith(expectedBranchName).ShouldBe(true);
try
{
using (var fixture = new EmptyRepositoryFixture(new Config()))
{
fixture.Repository.CreateFileAndCommit("TestFile.txt");
File.Copy(Path.Combine(fixture.RepositoryPath, "TestFile.txt"), Path.Combine(tempDir, "TestFile.txt"));
expectedDynamicRepoLocation = Path.Combine(tempPath, fixture.RepositoryPath.Split('\\').Last());
Directory.CreateDirectory(expectedDynamicRepoLocation);

if (checkConfig)
var arguments = new Arguments
{
var expectedConfigPath = Path.Combine(dynamicRepositoryPath, "..\\GitVersionConfig.yaml");
File.Exists(expectedConfigPath).ShouldBe(true);
}
TargetPath = tempDir,
TargetUrl = fixture.RepositoryPath
};

var gitPreparer = new GitPreparer(arguments);
gitPreparer.InitialiseDynamicRepositoryIfNeeded();

gitPreparer.IsDynamicGitRepository.ShouldBe(true);
gitPreparer.DynamicGitRepositoryPath.ShouldBe(expectedDynamicRepoLocation + "_1\\.git");
}
}
finally
{
Directory.Delete(tempDir, true);
if (expectedDynamicRepoLocation != null)
Directory.Delete(expectedDynamicRepoLocation, true);
if (expectedDynamicRepoLocation != null)
DeleteHelper.DeleteGitRepository(expectedDynamicRepoLocation + "_1");
}
}

[Test]
Expand All @@ -89,7 +130,7 @@ public void WorksCorrectlyWithLocalRepository()
};

var gitPreparer = new GitPreparer(arguments);
var dynamicRepositoryPath = gitPreparer.Prepare();
var dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();

dynamicRepositoryPath.ShouldBe(null);
gitPreparer.IsDynamicGitRepository.ShouldBe(false);
Expand Down
1 change: 1 addition & 0 deletions GitVersionExe.Tests/HelpWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public void AllArgsAreInHelp()
{ "Init", "init" },
{ "TargetBranch", "/b" },
{ "LogFilePath" , "/l" },
{ "DynamicRepositoryLocation" , "/dynamicRepoLocation" },
{ "IsHelp", "/?" }
};
string helpText = null;
Expand Down
6 changes: 6 additions & 0 deletions GitVersionExe/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
continue;
}

if (IsSwitch("dynamicRepoLocation", name))
{
arguments.DynamicRepositoryLocation = value;
continue;
}

if (IsSwitch("url", name))
{
arguments.TargetUrl = value;
Expand Down
1 change: 1 addition & 0 deletions GitVersionExe/Arguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public Arguments()
public string TargetUrl;
public string TargetBranch;
public string CommitId;
public string DynamicRepositoryLocation;

public bool Init;

Expand Down
Loading