Skip to content

Commit 1a7a2c7

Browse files
committed
Dynamic repo is now updated properly when it exists
1 parent 835e4f6 commit 1a7a2c7

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,57 @@ 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(5);
95+
mainRepositoryFixture.Repository.CreateFileAndCommit("TestFile.txt");
96+
97+
mainRepositoryFixture.Repository.CreateBranch(SpecificBranchName);
98+
99+
var arguments = new Arguments
100+
{
101+
TargetPath = tempDir,
102+
TargetUrl = mainRepositoryFixture.RepositoryPath
103+
};
104+
105+
// Copy contents into working directory
106+
File.Copy(Path.Combine(mainRepositoryFixture.RepositoryPath, "TestFile.txt"), Path.Combine(tempDir, "TestFile.txt"));
107+
arguments.TargetBranch = "master";
108+
109+
var gitPreparer = new GitPreparer(arguments);
110+
gitPreparer.InitialiseDynamicRepositoryIfNeeded();
111+
dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();
112+
113+
var newCommit = mainRepositoryFixture.Repository.MakeACommit();
114+
gitPreparer.InitialiseDynamicRepositoryIfNeeded();
115+
116+
using (var repository = new Repository(dynamicRepositoryPath))
117+
{
118+
mainRepositoryFixture.DumpGraph();
119+
repository.DumpGraph();
120+
repository.Commits.ShouldContain(c => c.Sha == newCommit.Sha);
121+
}
122+
}
123+
}
124+
finally
125+
{
126+
Directory.Delete(tempDir, true);
127+
if (dynamicRepositoryPath != null)
128+
DeleteHelper.DeleteGitRepository(dynamicRepositoryPath);
129+
}
130+
}
131+
81132
[Test]
82133
public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
83134
{

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
});

0 commit comments

Comments
 (0)