Skip to content

Commit 3051e21

Browse files
Merge pull request #1767 from GitTools/pr/dynamic-repositories
Allow head to move for dynamic repositories
2 parents 11ee72a + c408e4e commit 3051e21

File tree

4 files changed

+45
-29
lines changed

4 files changed

+45
-29
lines changed
Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.IO;
1+
using System.Diagnostics;
2+
using System.IO;
23
using GitVersion;
34
using GitVersionCore.Tests;
45
using NUnit.Framework;
@@ -9,43 +10,58 @@ public class DynamicRepositoryTests : TestBase
910
string workDirectory;
1011

1112

12-
[SetUp]
13+
[OneTimeSetUp]
1314
public void CreateTemporaryRepository()
1415
{
1516
// Note: we can't use guid because paths will be too long
16-
workDirectory = Path.Combine(Path.GetTempPath(), "DynRepoTests");
17+
workDirectory = Path.Combine(Path.GetTempPath(), "GV");
18+
19+
// Clean directory upfront, some build agents are having troubles
20+
if (Directory.Exists(workDirectory))
21+
{
22+
Directory.Delete(workDirectory, true);
23+
}
24+
25+
Directory.CreateDirectory(workDirectory);
1726
}
1827

1928

20-
//[TearDown]
21-
//public void Cleanup()
22-
//{
23-
// Directory.Delete(workDirectory, true);
24-
//}
29+
[OneTimeTearDown]
30+
public void Cleanup()
31+
{
2532

33+
}
34+
35+
// Note: use same name twice to see if changing commits works on same (cached) repository
2636
[Ignore("These tests are slow and fail on the second run in Test Explorer and need to be re-written")]
27-
[TestCase("GV_master_1", "https://github.com/GitTools/GitVersion", "master", "4783d325521463cd6cf1b61074352da84451f25d", "4.0.0+1126")]
28-
[TestCase("GV_master_2", "https://github.com/GitTools/GitVersion", "master", "3bdcd899530b4e9b37d13639f317da04a749e728", "4.0.0+1132")]
37+
[NonParallelizable]
38+
[TestCase("GV_master", "https://github.com/GitTools/GitVersion", "master", "4783d325521463cd6cf1b61074352da84451f25d", "4.0.0+1086")]
39+
[TestCase("GV_master", "https://github.com/GitTools/GitVersion", "master", "3bdcd899530b4e9b37d13639f317da04a749e728", "4.0.0+1092")]
40+
[TestCase("Ctl_develop", "https://github.com/Catel/Catel", "develop", "0e2b6c125a730d2fa5e24394ef64abe62c98e9e9", "5.12.0-alpha.188")]
41+
[TestCase("Ctl_develop", "https://github.com/Catel/Catel", "develop", "71e71359f37581784e18c94e7a45eee72cbeeb30", "5.12.0-alpha.192")]
42+
[TestCase("Ctl_master", "https://github.com/Catel/Catel", "master", "f5de8964c35180a5f8607f5954007d5828aa849f", "5.10.0")]
43+
[TestCase("CtlA_develop", "https://github.com/Catel/Catel.Analyzers", "develop", "be0aa94642d6ff1df6209e2180a7fe0de9aab384", "0.1.0-alpha.21")]
44+
[TestCase("CtlA_develop", "https://github.com/Catel/Catel.Analyzers", "develop", "0e2b6c125a730d2fa5e24394ef64abe62c98e9e9", "0.1.0-alpha.23")]
2945
public void FindsVersionInDynamicRepo(string name, string url, string targetBranch, string commitId, string expectedFullSemVer)
3046
{
3147
var root = Path.Combine(workDirectory, name);
32-
var dynamicDirectory = Path.Combine(root, "dynamic");
33-
var workingDirectory = Path.Combine(root, "working");
34-
35-
// Clear upfront
36-
if (Directory.Exists(root))
37-
{
38-
Directory.Delete(root, true);
39-
}
48+
var dynamicDirectory = Path.Combine(root, "D"); // dynamic, keeping directory as short as possible
49+
var workingDirectory = Path.Combine(root, "W"); // working, keeping directory as short as possible
4050

4151
Directory.CreateDirectory(dynamicDirectory);
4252
Directory.CreateDirectory(workingDirectory);
4353

54+
Logger.AddLoggersTemporarily(
55+
x => Debug.WriteLine($"[DEBUG] {x}"),
56+
x => Debug.WriteLine($"[INFO] {x}"),
57+
x => Debug.WriteLine($"[WARNING] {x}"),
58+
x => Debug.WriteLine($"[ERROR] {x}"));
59+
4460
var executeCore = new ExecuteCore(new TestFileSystem());
4561

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

4965
Assert.AreEqual(expectedFullSemVer, versionVariables.FullSemVer);
5066
}
51-
}
67+
}

src/GitVersionCore.Tests/IntegrationTests/RemoteRepositoryScenarios.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void GivenARemoteGitRepositoryWithCommitsAndBranches_ThenClonedLocalShoul
4040
return repo;
4141
}))
4242
{
43-
GitRepositoryHelper.NormalizeGitDirectory(fixture.LocalRepositoryFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: string.Empty);
43+
GitRepositoryHelper.NormalizeGitDirectory(fixture.LocalRepositoryFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: string.Empty, true);
4444

4545
fixture.AssertFullSemver("1.0.0-beta.1+5");
4646
fixture.AssertFullSemver("1.0.0-beta.1+5", fixture.LocalRepositoryFixture.Repository);

src/GitVersionCore/GitPreparer.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ public GitPreparer(string targetUrl, string dynamicRepositoryLocation, Authentic
2020
{
2121
this.targetUrl = targetUrl;
2222
this.dynamicRepositoryLocation = dynamicRepositoryLocation;
23-
this.authentication = authentication == null ?
24-
null :
23+
this.authentication =
2524
new AuthenticationInfo
2625
{
27-
Username = authentication.Username,
28-
Password = authentication.Password
26+
Username = authentication?.Username,
27+
Password = authentication?.Password
2928
};
3029
this.noFetch = noFetch;
3130
this.targetPath = targetPath.TrimEnd('/', '\\');
@@ -51,7 +50,7 @@ public void Initialise(bool normaliseGitDirectory, string currentBranch, bool sh
5150
{
5251
CleanupDuplicateOrigin();
5352
}
54-
GitRepositoryHelper.NormalizeGitDirectory(GetDotGitDirectory(), authentication, noFetch, currentBranch);
53+
GitRepositoryHelper.NormalizeGitDirectory(GetDotGitDirectory(), authentication, noFetch, currentBranch, IsDynamicGitRepository);
5554
}
5655
}
5756
return;
@@ -181,7 +180,7 @@ static string CreateDynamicRepository(string targetPath, AuthenticationInfo auth
181180
Logger.WriteInfo("Git repository already exists");
182181
using (Logger.IndentLog($"Normalizing git directory for branch '{targetBranch}'"))
183182
{
184-
GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch);
183+
GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch, true);
185184
}
186185

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

198197
return gitDirectory;

src/GitVersionCore/Helpers/GitRepositoryHelper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public static class GitRepositoryHelper
1111
/// 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.
1212
/// It is not recommended to run normalisation against a local repository
1313
/// </summary>
14-
public static void NormalizeGitDirectory(string gitDirectory, AuthenticationInfo authentication, bool noFetch, string currentBranch)
14+
public static void NormalizeGitDirectory(string gitDirectory, AuthenticationInfo authentication,
15+
bool noFetch, string currentBranch, bool isDynamicRepository)
1516
{
1617
using (var repo = new Repository(gitDirectory))
1718
{
@@ -41,7 +42,7 @@ public static void NormalizeGitDirectory(string gitDirectory, AuthenticationInfo
4142
// Bug fix for https://github.com/GitTools/GitVersion/issues/1754, head maybe have been changed
4243
// if this is a dynamic repository. But only allow this in case the branches are different (branch switch)
4344
if (expectedSha != repo.Head.Tip.Sha &&
44-
!expectedBranchName.IsBranch(currentBranch))
45+
(isDynamicRepository || !expectedBranchName.IsBranch(currentBranch)))
4546
{
4647
var newExpectedSha = repo.Head.Tip.Sha;
4748
var newExpectedBranchName = repo.Head.CanonicalName;

0 commit comments

Comments
 (0)