diff --git a/GitVersionCore.Tests/Helpers/GitTestExtensions.cs b/GitVersionCore.Tests/Helpers/GitTestExtensions.cs index e1ff69a596..1f15e15a4c 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; @@ -13,10 +14,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 +37,48 @@ 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()); + + // GHK: 2015-01-18: I know it's very ugly, but somehow we need to retry here otherwise "there is nothing to commit" + var 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) { var commit = repository.MakeACommit(); diff --git a/GitVersionExe.Tests/GitPreparerTests.cs b/GitVersionExe.Tests/GitPreparerTests.cs index 56a4605ff1..b53386e329 100644 --- a/GitVersionExe.Tests/GitPreparerTests.cs +++ b/GitVersionExe.Tests/GitPreparerTests.cs @@ -1,7 +1,7 @@ - using System.IO; - using GitVersion; - using LibGit2Sharp; - using NUnit.Framework; +using System.IO; +using GitVersion; +using LibGit2Sharp; +using NUnit.Framework; [TestFixture] public class GitPreparerTests @@ -17,21 +17,39 @@ 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, - TargetUrl = fixture.RepositoryPath - }; + { + TargetPath = tempDir, + TargetUrl = fixture.RepositoryPath + }; if (!string.IsNullOrWhiteSpace(branchName)) { @@ -49,6 +67,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)); + } } } } @@ -59,9 +83,9 @@ public void WorksCorrectlyWithLocalRepository() var tempDir = Path.GetTempPath(); var arguments = new Arguments - { - TargetPath = tempDir - }; + { + TargetPath = tempDir + }; var gitPreparer = new GitPreparer(arguments); var dynamicRepositoryPath = gitPreparer.Prepare(); diff --git a/GitVersionExe/GitPreparer.cs b/GitVersionExe/GitPreparer.cs index 6d6cab9a9f..10d3516b32 100644 --- a/GitVersionExe/GitPreparer.cs +++ b/GitVersionExe/GitPreparer.cs @@ -61,49 +61,56 @@ 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); } + + // < 3.0 method + repository.CheckoutFilesIfExist("NextVersion.txt"); + + // > 3.0 method + repository.CheckoutFilesIfExist("GitVersionConfig.yaml"); } DynamicGitRepositoryPath = gitDirectory;