From 9e0b0cbb77fe93b76fe80acdd33b22664324e583 Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Sat, 17 Jan 2015 16:14:43 +0100 Subject: [PATCH 1/6] #351 Fall back to default branch if not specified for dynamic repositories to support NextVersion.txt --- GitVersionExe/GitPreparer.cs | 57 +++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/GitVersionExe/GitPreparer.cs b/GitVersionExe/GitPreparer.cs index 6d6cab9a9f..97fc0d2a6a 100644 --- a/GitVersionExe/GitPreparer.cs +++ b/GitVersionExe/GitPreparer.cs @@ -61,49 +61,52 @@ string GetGitInfoFromUrl() Repository.Clone(arguments.TargetUrl, gitDirectory, new CloneOptions { - IsBare = true, + IsBare = true, Checkout = false, CredentialsProvider = (url, usernameFromUrl, types) => credentials }); - if (!string.IsNullOrWhiteSpace(arguments.TargetBranch)) - { - // Normalize (download branches) before using the branch - GitHelper.NormalizeGitDirectory(gitDirectory, arguments.Authentication); + // Normalize (download branches) before using the branch + GitHelper.NormalizeGitDirectory(gitDirectory, arguments.Authentication); - using (var repository = new Repository(gitDirectory)) + using (var repository = new Repository(gitDirectory)) + { + var targetBranch = arguments.TargetBranch; + if (string.IsNullOrWhiteSpace(targetBranch)) { - Reference newHead = null; + targetBranch = repository.Head.Name; + } - var localReference = GetLocalReference(repository, arguments.TargetBranch); - if (localReference != null) - { - newHead = localReference; - } + Reference newHead = null; + + var localReference = GetLocalReference(repository, targetBranch); + if (localReference != null) + { + newHead = localReference; + } - if (newHead == null) + if (newHead == null) + { + var remoteReference = GetRemoteReference(repository, targetBranch, arguments.TargetUrl); + if (remoteReference != null) { - var remoteReference = GetRemoteReference(repository, arguments.TargetBranch, arguments.TargetUrl); - if (remoteReference != null) - { - repository.Network.Fetch(arguments.TargetUrl, new[] + repository.Network.Fetch(arguments.TargetUrl, new[] { - string.Format("{0}:{1}", remoteReference.CanonicalName, arguments.TargetBranch) + string.Format("{0}:{1}", remoteReference.CanonicalName, targetBranch) }); - newHead = repository.Refs[string.Format("refs/heads/{0}", arguments.TargetBranch)]; - } + newHead = repository.Refs[string.Format("refs/heads/{0}", targetBranch)]; } + } - if (newHead != null) - { - Logger.WriteInfo(string.Format("Switching to branch '{0}'", arguments.TargetBranch)); - - repository.Refs.UpdateTarget(repository.Refs.Head, newHead); - } + if (newHead != null) + { + Logger.WriteInfo(string.Format("Switching to branch '{0}'", targetBranch)); - repository.CheckoutFilesIfExist("NextVersion.txt"); + repository.Refs.UpdateTarget(repository.Refs.Head, newHead); } + + repository.CheckoutFilesIfExist("NextVersion.txt"); } DynamicGitRepositoryPath = gitDirectory; From a66119e69f347cdab6d46689b8d2003aaa72aeb2 Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Sat, 17 Jan 2015 16:16:02 +0100 Subject: [PATCH 2/6] #351 Added support for GitVersionConfig.yaml --- GitVersionExe/GitPreparer.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/GitVersionExe/GitPreparer.cs b/GitVersionExe/GitPreparer.cs index 97fc0d2a6a..10d3516b32 100644 --- a/GitVersionExe/GitPreparer.cs +++ b/GitVersionExe/GitPreparer.cs @@ -106,7 +106,11 @@ string GetGitInfoFromUrl() repository.Refs.UpdateTarget(repository.Refs.Head, newHead); } + // < 3.0 method repository.CheckoutFilesIfExist("NextVersion.txt"); + + // > 3.0 method + repository.CheckoutFilesIfExist("GitVersionConfig.yaml"); } DynamicGitRepositoryPath = gitDirectory; From d556d5b554ee1c0e485a873cbf3d149f2e846d13 Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Sat, 17 Jan 2015 16:55:02 +0100 Subject: [PATCH 3/6] #351 Improved unit tests to support configuration checks in dynamic repositories --- .../Helpers/GitTestExtensions.cs | 25 +++++++++++--- GitVersionExe.Tests/GitPreparerTests.cs | 33 ++++++++++++++++--- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/GitVersionCore.Tests/Helpers/GitTestExtensions.cs b/GitVersionCore.Tests/Helpers/GitTestExtensions.cs index e1ff69a596..cb69ad7a0a 100644 --- a/GitVersionCore.Tests/Helpers/GitTestExtensions.cs +++ b/GitVersionCore.Tests/Helpers/GitTestExtensions.cs @@ -13,10 +13,7 @@ public static Commit MakeACommit(this IRepository repository) public static Commit MakeACommit(this IRepository repository, DateTimeOffset dateTimeOffset) { - var randomFile = Path.Combine(repository.Info.WorkingDirectory, Guid.NewGuid().ToString()); - File.WriteAllText(randomFile, string.Empty); - repository.Index.Stage(randomFile); - return repository.Commit("Test Commit", Constants.Signature(dateTimeOffset), Constants.Signature(dateTimeOffset)); + return CreateFileAndCommit(repository, Guid.NewGuid().ToString(), dateTimeOffset); } public static void MergeNoFF(this IRepository repository, string branch) @@ -39,6 +36,26 @@ public static Commit[] MakeCommits(this IRepository repository, int numCommitsTo .ToArray(); } + public static Commit CreateFileAndCommit(this IRepository repository, string relativeFileName, DateTimeOffset dateTimeOffset = default(DateTimeOffset)) + { + if (dateTimeOffset == default(DateTimeOffset)) + { + dateTimeOffset = DateTimeOffset.Now; + } + + var randomFile = Path.Combine(repository.Info.WorkingDirectory, relativeFileName); + if (File.Exists(randomFile)) + { + File.Delete(randomFile); + } + + File.WriteAllText(randomFile, Guid.NewGuid().ToString()); + + repository.Index.Stage(randomFile); + return repository.Commit(string.Format("Test Commit for file '{0}'", relativeFileName), + Constants.Signature(dateTimeOffset), Constants.Signature(dateTimeOffset)); + } + public static Tag MakeATaggedCommit(this IRepository repository, string tag) { var commit = repository.MakeACommit(); diff --git a/GitVersionExe.Tests/GitPreparerTests.cs b/GitVersionExe.Tests/GitPreparerTests.cs index 06bff07bde..aa29c0fe82 100644 --- a/GitVersionExe.Tests/GitPreparerTests.cs +++ b/GitVersionExe.Tests/GitPreparerTests.cs @@ -3,6 +3,7 @@ using System.IO; using GitVersion; using LibGit2Sharp; + using NSubstitute; using NUnit.Framework; [TestFixture] @@ -19,16 +20,34 @@ public GitPreparerTests() const string SpecificBranchName = "feature/foo"; [Test] - [TestCase(null, DefaultBranchName)] - [TestCase(SpecificBranchName, SpecificBranchName)] - public void WorksCorrectlyWithRemoteRepository(string branchName, string expectedBranchName) + [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) { var tempDir = Path.GetTempPath(); using (var fixture = new EmptyRepositoryFixture(new Config())) { fixture.Repository.MakeCommits(5); - fixture.Repository.CreateBranch("feature/foo"); + + if (checkConfig) + { + fixture.Repository.CreateFileAndCommit("GitVersionConfig.yaml"); + } + + fixture.Repository.CreateBranch(SpecificBranchName); + + if (checkConfig) + { + fixture.Repository.Refs.UpdateTarget(fixture.Repository.Refs.Head, fixture.Repository.Refs["refs/heads/" + SpecificBranchName]); + + fixture.Repository.CreateFileAndCommit("GitVersionConfig.yaml"); + + fixture.Repository.Refs.UpdateTarget(fixture.Repository.Refs.Head, fixture.Repository.Refs["refs/heads/" + DefaultBranchName]); + } + var arguments = new Arguments { TargetPath = tempDir, @@ -51,6 +70,12 @@ public void WorksCorrectlyWithRemoteRepository(string branchName, string expecte var currentBranch = repository.Head.CanonicalName; Assert.IsTrue(currentBranch.EndsWith(expectedBranchName)); + + if (checkConfig) + { + var expectedConfigPath = Path.Combine(dynamicRepositoryPath, "GitVersionConfig.yaml"); + Assert.IsTrue(File.Exists(expectedConfigPath)); + } } } } From d8d7593a6c12111ed78c7d0028ef4457010f672a Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Sat, 17 Jan 2015 17:53:34 +0100 Subject: [PATCH 4/6] Build fix --- GitVersionExe.Tests/GitPreparerTests.cs | 144 ++++++++++++------------ 1 file changed, 71 insertions(+), 73 deletions(-) diff --git a/GitVersionExe.Tests/GitPreparerTests.cs b/GitVersionExe.Tests/GitPreparerTests.cs index 73eb4fbe2d..57803564b8 100644 --- a/GitVersionExe.Tests/GitPreparerTests.cs +++ b/GitVersionExe.Tests/GitPreparerTests.cs @@ -1,98 +1,96 @@ - using System.IO; - using GitVersion; - using LibGit2Sharp; - using NSubstitute; - using NUnit.Framework; - - [TestFixture] - public class GitPreparerTests +using System.IO; +using GitVersion; +using LibGit2Sharp; +using NUnit.Framework; + +[TestFixture] +public class GitPreparerTests +{ + public GitPreparerTests() { - public GitPreparerTests() - { - Logger.WriteInfo = s => { }; - Logger.WriteWarning = s => { }; - Logger.WriteError = s => { }; - } + Logger.WriteInfo = s => { }; + Logger.WriteWarning = s => { }; + Logger.WriteError = s => { }; + } - const string DefaultBranchName = "master"; - const string SpecificBranchName = "feature/foo"; + const string DefaultBranchName = "master"; + 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) + { + var tempDir = Path.GetTempPath(); - [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) + using (var fixture = new EmptyRepositoryFixture(new Config())) { - var tempDir = Path.GetTempPath(); + fixture.Repository.MakeCommits(5); - using (var fixture = new EmptyRepositoryFixture(new Config())) + if (checkConfig) { - fixture.Repository.MakeCommits(5); - - if (checkConfig) - { - fixture.Repository.CreateFileAndCommit("GitVersionConfig.yaml"); - } + fixture.Repository.CreateFileAndCommit("GitVersionConfig.yaml"); + } - fixture.Repository.CreateBranch(SpecificBranchName); + fixture.Repository.CreateBranch(SpecificBranchName); - if (checkConfig) - { - fixture.Repository.Refs.UpdateTarget(fixture.Repository.Refs.Head, fixture.Repository.Refs["refs/heads/" + SpecificBranchName]); + if (checkConfig) + { + fixture.Repository.Refs.UpdateTarget(fixture.Repository.Refs.Head, fixture.Repository.Refs["refs/heads/" + SpecificBranchName]); - fixture.Repository.CreateFileAndCommit("GitVersionConfig.yaml"); + fixture.Repository.CreateFileAndCommit("GitVersionConfig.yaml"); - fixture.Repository.Refs.UpdateTarget(fixture.Repository.Refs.Head, fixture.Repository.Refs["refs/heads/" + DefaultBranchName]); - } + fixture.Repository.Refs.UpdateTarget(fixture.Repository.Refs.Head, fixture.Repository.Refs["refs/heads/" + DefaultBranchName]); + } - var arguments = new Arguments - { - TargetPath = tempDir, - TargetUrl = fixture.RepositoryPath - }; + 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); - var dynamicRepositoryPath = gitPreparer.Prepare(); + var gitPreparer = new GitPreparer(arguments); + var dynamicRepositoryPath = gitPreparer.Prepare(); - Assert.AreEqual(Path.Combine(tempDir, "_dynamicrepository", ".git"), dynamicRepositoryPath); - Assert.IsTrue(gitPreparer.IsDynamicGitRepository); + Assert.AreEqual(Path.Combine(tempDir, "_dynamicrepository", ".git"), dynamicRepositoryPath); + Assert.IsTrue(gitPreparer.IsDynamicGitRepository); - using (var repository = new Repository(dynamicRepositoryPath)) - { - var currentBranch = repository.Head.CanonicalName; + using (var repository = new Repository(dynamicRepositoryPath)) + { + var currentBranch = repository.Head.CanonicalName; - Assert.IsTrue(currentBranch.EndsWith(expectedBranchName)); + Assert.IsTrue(currentBranch.EndsWith(expectedBranchName)); - if (checkConfig) - { - var expectedConfigPath = Path.Combine(dynamicRepositoryPath, "GitVersionConfig.yaml"); - Assert.IsTrue(File.Exists(expectedConfigPath)); - } + if (checkConfig) + { + var expectedConfigPath = Path.Combine(dynamicRepositoryPath, "GitVersionConfig.yaml"); + Assert.IsTrue(File.Exists(expectedConfigPath)); } } } + } - [Test] - public void WorksCorrectlyWithLocalRepository() - { - var tempDir = Path.GetTempPath(); + [Test] + public void WorksCorrectlyWithLocalRepository() + { + var tempDir = Path.GetTempPath(); - var arguments = new Arguments - { - TargetPath = tempDir - }; + var arguments = new Arguments + { + TargetPath = tempDir + }; - var gitPreparer = new GitPreparer(arguments); - var dynamicRepositoryPath = gitPreparer.Prepare(); + var gitPreparer = new GitPreparer(arguments); + var dynamicRepositoryPath = gitPreparer.Prepare(); - Assert.AreEqual(null, dynamicRepositoryPath); - Assert.IsFalse(gitPreparer.IsDynamicGitRepository); - } + Assert.AreEqual(null, dynamicRepositoryPath); + Assert.IsFalse(gitPreparer.IsDynamicGitRepository); } -} +} \ No newline at end of file From e42b013fc12673bd61cfefec1f8f6526b0b8674f Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Sun, 18 Jan 2015 11:14:21 +0100 Subject: [PATCH 5/6] Fixed unit tests for dynamic repositories --- .../Helpers/GitTestExtensions.cs | 29 +++++++++++++++++-- GitVersionExe.Tests/GitPreparerTests.cs | 2 +- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/GitVersionCore.Tests/Helpers/GitTestExtensions.cs b/GitVersionCore.Tests/Helpers/GitTestExtensions.cs index cb69ad7a0a..7a64e0c856 100644 --- a/GitVersionCore.Tests/Helpers/GitTestExtensions.cs +++ b/GitVersionCore.Tests/Helpers/GitTestExtensions.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Linq; +using System.Threading; using GitVersion; using LibGit2Sharp; @@ -51,9 +52,31 @@ public static Commit[] MakeCommits(this IRepository repository, int numCommitsTo File.WriteAllText(randomFile, Guid.NewGuid().ToString()); - repository.Index.Stage(randomFile); - return repository.Commit(string.Format("Test Commit for file '{0}'", relativeFileName), - Constants.Signature(dateTimeOffset), Constants.Signature(dateTimeOffset)); + // GHK: 2015-01-18: I know it's very ugly, but somehow we need to retry here otherwise "there is nothing to commit" + int retryCount = 3; + while (retryCount > 0) + { + try + { + repository.Stage(randomFile); + + return repository.Commit(string.Format("Test Commit for file '{0}'", relativeFileName), + Constants.Signature(dateTimeOffset), Constants.Signature(dateTimeOffset)); + } + catch (EmptyCommitException) + { + if (retryCount <= 0) + { + throw; + } + + Thread.Sleep(100); + } + + retryCount--; + } + + return null; } public static Tag MakeATaggedCommit(this IRepository repository, string tag) diff --git a/GitVersionExe.Tests/GitPreparerTests.cs b/GitVersionExe.Tests/GitPreparerTests.cs index 57803564b8..b53386e329 100644 --- a/GitVersionExe.Tests/GitPreparerTests.cs +++ b/GitVersionExe.Tests/GitPreparerTests.cs @@ -70,7 +70,7 @@ public void WorksCorrectlyWithRemoteRepository(string branchName, string expecte if (checkConfig) { - var expectedConfigPath = Path.Combine(dynamicRepositoryPath, "GitVersionConfig.yaml"); + var expectedConfigPath = Path.Combine(dynamicRepositoryPath, "..\\GitVersionConfig.yaml"); Assert.IsTrue(File.Exists(expectedConfigPath)); } } From f6741a84e2778efd7d7bc700273a13d4db38512b Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Sun, 18 Jan 2015 12:47:10 +0100 Subject: [PATCH 6/6] Resharper fix --- GitVersionCore.Tests/Helpers/GitTestExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GitVersionCore.Tests/Helpers/GitTestExtensions.cs b/GitVersionCore.Tests/Helpers/GitTestExtensions.cs index 7a64e0c856..1f15e15a4c 100644 --- a/GitVersionCore.Tests/Helpers/GitTestExtensions.cs +++ b/GitVersionCore.Tests/Helpers/GitTestExtensions.cs @@ -53,7 +53,7 @@ public static Commit[] MakeCommits(this IRepository repository, int numCommitsTo File.WriteAllText(randomFile, Guid.NewGuid().ToString()); // GHK: 2015-01-18: I know it's very ugly, but somehow we need to retry here otherwise "there is nothing to commit" - int retryCount = 3; + var retryCount = 3; while (retryCount > 0) { try