Skip to content

Allow head to move for dynamic repositories #1767

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 12 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions src/GitVersionCore.Tests/DynamicRepositoryTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Diagnostics;
using System.IO;
using GitVersion;
using GitVersionCore.Tests;
using NUnit.Framework;
Expand All @@ -9,43 +10,58 @@ public class DynamicRepositoryTests : TestBase
string workDirectory;


[SetUp]
[OneTimeSetUp]
public void CreateTemporaryRepository()
{
// Note: we can't use guid because paths will be too long
workDirectory = Path.Combine(Path.GetTempPath(), "DynRepoTests");
workDirectory = Path.Combine(Path.GetTempPath(), "GV");

// Clean directory upfront, some build agents are having troubles
if (Directory.Exists(workDirectory))
{
Directory.Delete(workDirectory, true);
}

Directory.CreateDirectory(workDirectory);
}


//[TearDown]
//public void Cleanup()
//{
// Directory.Delete(workDirectory, true);
//}
[OneTimeTearDown]
public void Cleanup()
{

}

// Note: use same name twice to see if changing commits works on same (cached) repository
[Ignore("These tests are slow and fail on the second run in Test Explorer and need to be re-written")]
[TestCase("GV_master_1", "https://github.com/GitTools/GitVersion", "master", "4783d325521463cd6cf1b61074352da84451f25d", "4.0.0+1126")]
[TestCase("GV_master_2", "https://github.com/GitTools/GitVersion", "master", "3bdcd899530b4e9b37d13639f317da04a749e728", "4.0.0+1132")]
[NonParallelizable]
[TestCase("GV_master", "https://github.com/GitTools/GitVersion", "master", "4783d325521463cd6cf1b61074352da84451f25d", "4.0.0+1086")]
[TestCase("GV_master", "https://github.com/GitTools/GitVersion", "master", "3bdcd899530b4e9b37d13639f317da04a749e728", "4.0.0+1092")]
[TestCase("Ctl_develop", "https://github.com/Catel/Catel", "develop", "0e2b6c125a730d2fa5e24394ef64abe62c98e9e9", "5.12.0-alpha.188")]
[TestCase("Ctl_develop", "https://github.com/Catel/Catel", "develop", "71e71359f37581784e18c94e7a45eee72cbeeb30", "5.12.0-alpha.192")]
[TestCase("Ctl_master", "https://github.com/Catel/Catel", "master", "f5de8964c35180a5f8607f5954007d5828aa849f", "5.10.0")]
[TestCase("CtlA_develop", "https://github.com/Catel/Catel.Analyzers", "develop", "be0aa94642d6ff1df6209e2180a7fe0de9aab384", "0.1.0-alpha.21")]
[TestCase("CtlA_develop", "https://github.com/Catel/Catel.Analyzers", "develop", "0e2b6c125a730d2fa5e24394ef64abe62c98e9e9", "0.1.0-alpha.23")]
public void FindsVersionInDynamicRepo(string name, string url, string targetBranch, string commitId, string expectedFullSemVer)
{
var root = Path.Combine(workDirectory, name);
var dynamicDirectory = Path.Combine(root, "dynamic");
var workingDirectory = Path.Combine(root, "working");

// Clear upfront
if (Directory.Exists(root))
{
Directory.Delete(root, true);
}
var dynamicDirectory = Path.Combine(root, "D"); // dynamic, keeping directory as short as possible
var workingDirectory = Path.Combine(root, "W"); // working, keeping directory as short as possible

Directory.CreateDirectory(dynamicDirectory);
Directory.CreateDirectory(workingDirectory);

Logger.AddLoggersTemporarily(
x => Debug.WriteLine($"[DEBUG] {x}"),
x => Debug.WriteLine($"[INFO] {x}"),
x => Debug.WriteLine($"[WARNING] {x}"),
x => Debug.WriteLine($"[ERROR] {x}"));

var executeCore = new ExecuteCore(new TestFileSystem());

var versionVariables = executeCore.ExecuteGitVersion(url, dynamicDirectory, null, targetBranch,
false, workingDirectory, commitId);

Assert.AreEqual(expectedFullSemVer, versionVariables.FullSemVer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void GivenARemoteGitRepositoryWithCommitsAndBranches_ThenClonedLocalShoul
return repo;
}))
{
GitRepositoryHelper.NormalizeGitDirectory(fixture.LocalRepositoryFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: string.Empty);
GitRepositoryHelper.NormalizeGitDirectory(fixture.LocalRepositoryFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: string.Empty, true);

fixture.AssertFullSemver("1.0.0-beta.1+5");
fixture.AssertFullSemver("1.0.0-beta.1+5", fixture.LocalRepositoryFixture.Repository);
Expand Down
13 changes: 6 additions & 7 deletions src/GitVersionCore/GitPreparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ public GitPreparer(string targetUrl, string dynamicRepositoryLocation, Authentic
{
this.targetUrl = targetUrl;
this.dynamicRepositoryLocation = dynamicRepositoryLocation;
this.authentication = authentication == null ?
null :
this.authentication =
new AuthenticationInfo
{
Username = authentication.Username,
Password = authentication.Password
Username = authentication?.Username,
Password = authentication?.Password
};
this.noFetch = noFetch;
this.targetPath = targetPath.TrimEnd('/', '\\');
Expand All @@ -51,7 +50,7 @@ public void Initialise(bool normaliseGitDirectory, string currentBranch, bool sh
{
CleanupDuplicateOrigin();
}
GitRepositoryHelper.NormalizeGitDirectory(GetDotGitDirectory(), authentication, noFetch, currentBranch);
GitRepositoryHelper.NormalizeGitDirectory(GetDotGitDirectory(), authentication, noFetch, currentBranch, IsDynamicGitRepository);
}
}
return;
Expand Down Expand Up @@ -181,7 +180,7 @@ static string CreateDynamicRepository(string targetPath, AuthenticationInfo auth
Logger.WriteInfo("Git repository already exists");
using (Logger.IndentLog($"Normalizing git directory for branch '{targetBranch}'"))
{
GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch);
GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch, true);
}

return gitDirectory;
Expand All @@ -192,7 +191,7 @@ static string CreateDynamicRepository(string targetPath, AuthenticationInfo auth
using (Logger.IndentLog($"Normalizing git directory for branch '{targetBranch}'"))
{
// Normalize (download branches) before using the branch
GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch);
GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch, true);
}

return gitDirectory;
Expand Down
5 changes: 3 additions & 2 deletions src/GitVersionCore/Helpers/GitRepositoryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public static class GitRepositoryHelper
/// Normalisation of a git directory turns all remote branches into local branches, turns pull request refs into a real branch and a few other things. This is designed to be run *only on the build server* which checks out repositories in different ways.
/// It is not recommended to run normalisation against a local repository
/// </summary>
public static void NormalizeGitDirectory(string gitDirectory, AuthenticationInfo authentication, bool noFetch, string currentBranch)
public static void NormalizeGitDirectory(string gitDirectory, AuthenticationInfo authentication,
bool noFetch, string currentBranch, bool isDynamicRepository)
{
using (var repo = new Repository(gitDirectory))
{
Expand Down Expand Up @@ -41,7 +42,7 @@ public static void NormalizeGitDirectory(string gitDirectory, AuthenticationInfo
// Bug fix for https://github.com/GitTools/GitVersion/issues/1754, head maybe have been changed
// if this is a dynamic repository. But only allow this in case the branches are different (branch switch)
if (expectedSha != repo.Head.Tip.Sha &&
!expectedBranchName.IsBranch(currentBranch))
(isDynamicRepository || !expectedBranchName.IsBranch(currentBranch)))
{
var newExpectedSha = repo.Head.Tip.Sha;
var newExpectedBranchName = repo.Head.CanonicalName;
Expand Down