Skip to content

Commit 6276276

Browse files
committed
Merge pull request #425 from JakeGinnivan/FixStaleDynamicRepo
Fix stale dynamic repo
2 parents 101075a + dadf67d commit 6276276

File tree

9 files changed

+75
-15
lines changed

9 files changed

+75
-15
lines changed

GitVersionCore.Tests/GitVersionCore.Tests.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@
5757
<SpecificVersion>False</SpecificVersion>
5858
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
5959
</Reference>
60-
<Reference Include="Shouldly, Version=2.4.0.0, Culture=neutral, PublicKeyToken=6042cbcb05cbc941, processorArchitecture=MSIL">
61-
<SpecificVersion>False</SpecificVersion>
62-
<HintPath>..\packages\Shouldly.2.4.0\lib\net40\Shouldly.dll</HintPath>
60+
<Reference Include="Shouldly, Version=2.5.0.0, Culture=neutral, PublicKeyToken=6042cbcb05cbc941, processorArchitecture=MSIL">
61+
<HintPath>..\packages\Shouldly.2.5.0\lib\net40\Shouldly.dll</HintPath>
62+
<Private>True</Private>
6363
</Reference>
6464
<Reference Include="System" />
6565
<Reference Include="System.Core" />

GitVersionCore.Tests/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
<package id="Fody" version="1.28.1" targetFramework="net45" developmentDependency="true" />
77
<package id="LibGit2Sharp" version="0.20.1.0" targetFramework="net45" />
88
<package id="NUnit" version="2.6.4" targetFramework="net45" />
9-
<package id="Shouldly" version="2.4.0" targetFramework="net45" />
9+
<package id="Shouldly" version="2.5.0" targetFramework="net45" />
1010
<package id="YamlDotNet" version="3.5.1" targetFramework="net45" />
1111
</packages>

GitVersionCore/BuildServers/GitHelper.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ public static class GitHelper
1212
public static void NormalizeGitDirectory(string gitDirectory, Authentication authentication, bool noFetch)
1313
{
1414
//If noFetch is enabled, then GitVersion will assume that the git repository is normalized before execution, so that fetching from remotes is not required.
15-
if (noFetch) return;
15+
if (noFetch)
16+
{
17+
Logger.WriteInfo("Skipping fetching");
18+
return;
19+
}
1620

1721
using (var repo = new Repository(gitDirectory))
1822
{

GitVersionExe.Tests/GitPreparerTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,51 @@ public void WorksCorrectlyWithRemoteRepository(string branchName, string expecte
7878
}
7979
}
8080

81+
[Test]
82+
public void UpdatesExistingDynamicRepository()
83+
{
84+
var repoName = Guid.NewGuid().ToString();
85+
var tempPath = Path.GetTempPath();
86+
var tempDir = Path.Combine(tempPath, repoName);
87+
Directory.CreateDirectory(tempDir);
88+
string dynamicRepositoryPath = null;
89+
90+
try
91+
{
92+
using (var mainRepositoryFixture = new EmptyRepositoryFixture(new Config()))
93+
{
94+
mainRepositoryFixture.Repository.MakeCommits(1);
95+
96+
var arguments = new Arguments
97+
{
98+
TargetPath = tempDir,
99+
TargetUrl = mainRepositoryFixture.RepositoryPath,
100+
TargetBranch = "master"
101+
};
102+
103+
var gitPreparer = new GitPreparer(arguments);
104+
gitPreparer.InitialiseDynamicRepositoryIfNeeded();
105+
dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();
106+
107+
var newCommit = mainRepositoryFixture.Repository.MakeACommit();
108+
gitPreparer.InitialiseDynamicRepositoryIfNeeded();
109+
110+
using (var repository = new Repository(dynamicRepositoryPath))
111+
{
112+
mainRepositoryFixture.DumpGraph();
113+
repository.DumpGraph();
114+
repository.Commits.ShouldContain(c => c.Sha == newCommit.Sha);
115+
}
116+
}
117+
}
118+
finally
119+
{
120+
Directory.Delete(tempDir, true);
121+
if (dynamicRepositoryPath != null)
122+
DeleteHelper.DeleteGitRepository(dynamicRepositoryPath);
123+
}
124+
}
125+
81126
[Test]
82127
public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
83128
{

GitVersionExe.Tests/GitVersionExe.Tests.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@
5353
<SpecificVersion>False</SpecificVersion>
5454
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
5555
</Reference>
56-
<Reference Include="Shouldly, Version=2.4.0.0, Culture=neutral, PublicKeyToken=6042cbcb05cbc941, processorArchitecture=MSIL">
57-
<SpecificVersion>False</SpecificVersion>
58-
<HintPath>..\packages\Shouldly.2.4.0\lib\net40\Shouldly.dll</HintPath>
56+
<Reference Include="Shouldly, Version=2.5.0.0, Culture=neutral, PublicKeyToken=6042cbcb05cbc941, processorArchitecture=MSIL">
57+
<HintPath>..\packages\Shouldly.2.5.0\lib\net40\Shouldly.dll</HintPath>
58+
<Private>True</Private>
5959
</Reference>
6060
<Reference Include="System" />
6161
<Reference Include="System.Core" />

GitVersionExe.Tests/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
<package id="LibGit2Sharp" version="0.20.1.0" targetFramework="net45" />
66
<package id="NSubstitute" version="1.8.1.0" targetFramework="net45" />
77
<package id="NUnit" version="2.6.4" targetFramework="net45" />
8-
<package id="Shouldly" version="2.4.0" targetFramework="net45" />
8+
<package id="Shouldly" version="2.5.0" targetFramework="net45" />
99
</packages>

GitVersionExe/GitPreparer.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,19 @@ static string CreateDynamicRepository(string targetPath, Authentication authenti
8383
var gitDirectory = Path.Combine(targetPath, ".git");
8484
if (Directory.Exists(targetPath))
8585
{
86-
Logger.WriteInfo("Git repository already exists at {0}, skipping clone");
86+
Logger.WriteInfo(string.Format("Git repository already exists at {0}", targetPath));
87+
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch);
88+
Logger.WriteInfo(string.Format("Updating branch '{0}'", targetBranch));
89+
using (var repo = new Repository(targetPath))
90+
{
91+
var targetGitBranch = repo.Branches[targetBranch];
92+
var trackedBranch = targetGitBranch.TrackedBranch;
93+
if (trackedBranch == null)
94+
throw new InvalidOperationException(string.Format("Expecting {0} to have a remote tracking branch", targetBranch));
95+
96+
targetGitBranch.Checkout();
97+
repo.Reset(ResetMode.Hard, trackedBranch.Tip);
98+
}
8799

88100
return gitDirectory;
89101
}
@@ -105,7 +117,6 @@ static string CreateDynamicRepository(string targetPath, Authentication authenti
105117
Repository.Clone(repositoryUrl, gitDirectory,
106118
new CloneOptions
107119
{
108-
IsBare = true,
109120
Checkout = false,
110121
CredentialsProvider = (url, usernameFromUrl, types) => credentials
111122
});

GitVersionTask.Tests/GitVersionTask.Tests.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@
8080
<SpecificVersion>False</SpecificVersion>
8181
<HintPath>..\packages\ObjectApproval.1.0.0\Lib\NET40\ObjectApproval.dll</HintPath>
8282
</Reference>
83-
<Reference Include="Shouldly, Version=2.4.0.0, Culture=neutral, PublicKeyToken=6042cbcb05cbc941, processorArchitecture=MSIL">
84-
<SpecificVersion>False</SpecificVersion>
85-
<HintPath>..\packages\Shouldly.2.4.0\lib\net40\Shouldly.dll</HintPath>
83+
<Reference Include="Shouldly, Version=2.5.0.0, Culture=neutral, PublicKeyToken=6042cbcb05cbc941, processorArchitecture=MSIL">
84+
<HintPath>..\packages\Shouldly.2.5.0\lib\net40\Shouldly.dll</HintPath>
85+
<Private>True</Private>
8686
</Reference>
8787
<Reference Include="System" />
8888
<Reference Include="System.Collections.Immutable">

GitVersionTask.Tests/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<package id="ModuleInit.Fody" version="1.5.6.0" targetFramework="net45" developmentDependency="true" />
1212
<package id="NUnit" version="2.6.4" targetFramework="net45" />
1313
<package id="ObjectApproval" version="1.0.0" targetFramework="net45" />
14-
<package id="Shouldly" version="2.4.0" targetFramework="net45" />
14+
<package id="Shouldly" version="2.5.0" targetFramework="net45" />
1515
<package id="System.Collections.Immutable" version="1.1.32-beta" targetFramework="net45" />
1616
<package id="System.Reflection.Metadata" version="1.0.17-beta" targetFramework="net45" />
1717
</packages>

0 commit comments

Comments
 (0)