Skip to content

Commit c2647e7

Browse files
committed
Merge pull request #600 from JakeGinnivan/DynamicRepositoryCleanup
Removed normalisation related code from dynamic repositories. It uses…
2 parents 03d6bac + 19b2f76 commit c2647e7

File tree

6 files changed

+44
-111
lines changed

6 files changed

+44
-111
lines changed

src/GitVersionCore.Tests/Fixtures/RemoteRepositoryFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public RemoteRepositoryFixture(Config configuration)
2424
/// </summary>
2525
public void InitialiseRepo()
2626
{
27-
new GitPreparer(null, null, new Authentication(), null, false, LocalRepositoryFixture.RepositoryPath).Initialise(true, null);
27+
new GitPreparer(null, null, new Authentication(), false, LocalRepositoryFixture.RepositoryPath).Initialise(true, null);
2828
}
2929

3030
static IRepository CreateNewRepository(string path)

src/GitVersionCore.Tests/GitPreparerTests.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class GitPreparerTests
1313
const string SpecificBranchName = "feature/foo";
1414

1515
[Test]
16-
[TestCase(null, DefaultBranchName)]
16+
[TestCase(DefaultBranchName, DefaultBranchName)]
1717
[TestCase(SpecificBranchName, SpecificBranchName)]
1818
public void WorksCorrectlyWithRemoteRepository(string branchName, string expectedBranchName)
1919
{
@@ -37,8 +37,8 @@ public void WorksCorrectlyWithRemoteRepository(string branchName, string expecte
3737
// Copy contents into working directory
3838
File.Copy(Path.Combine(fixture.RepositoryPath, "TestFile.txt"), Path.Combine(tempDir, "TestFile.txt"));
3939

40-
var gitPreparer = new GitPreparer(fixture.RepositoryPath, null, new Authentication(), branchName, false, tempDir);
41-
gitPreparer.Initialise(false, null);
40+
var gitPreparer = new GitPreparer(fixture.RepositoryPath, null, new Authentication(), false, tempDir);
41+
gitPreparer.Initialise(false, branchName);
4242
dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();
4343

4444
gitPreparer.IsDynamicGitRepository.ShouldBe(true);
@@ -48,7 +48,7 @@ public void WorksCorrectlyWithRemoteRepository(string branchName, string expecte
4848
{
4949
var currentBranch = repository.Head.CanonicalName;
5050

51-
currentBranch.EndsWith(expectedBranchName).ShouldBe(true);
51+
currentBranch.ShouldEndWith(expectedBranchName);
5252
}
5353
}
5454
}
@@ -75,12 +75,12 @@ public void UpdatesExistingDynamicRepository()
7575
{
7676
mainRepositoryFixture.Repository.MakeCommits(1);
7777

78-
var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new Authentication(), "master", false, tempDir);
79-
gitPreparer.Initialise(false, null);
78+
var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new Authentication(), false, tempDir);
79+
gitPreparer.Initialise(false, "master");
8080
dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();
8181

8282
var newCommit = mainRepositoryFixture.Repository.MakeACommit();
83-
gitPreparer.Initialise(false, null);
83+
gitPreparer.Initialise(false, "master");
8484

8585
using (var repository = new Repository(dynamicRepositoryPath))
8686
{
@@ -116,8 +116,8 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
116116
expectedDynamicRepoLocation = Path.Combine(tempPath, fixture.RepositoryPath.Split('\\').Last());
117117
Directory.CreateDirectory(expectedDynamicRepoLocation);
118118

119-
var gitPreparer = new GitPreparer(fixture.RepositoryPath, null, new Authentication(), null, false, tempDir);
120-
gitPreparer.Initialise(false, null);
119+
var gitPreparer = new GitPreparer(fixture.RepositoryPath, null, new Authentication(), false, tempDir);
120+
gitPreparer.Initialise(false, "master");
121121

122122
gitPreparer.IsDynamicGitRepository.ShouldBe(true);
123123
gitPreparer.DynamicGitRepositoryPath.ShouldBe(expectedDynamicRepoLocation + "_1\\.git");
@@ -137,7 +137,7 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
137137
public void WorksCorrectlyWithLocalRepository()
138138
{
139139
var tempDir = Path.GetTempPath();
140-
var gitPreparer = new GitPreparer(null, null, null, null, false, tempDir);
140+
var gitPreparer = new GitPreparer(null, null, null, false, tempDir);
141141
var dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();
142142

143143
dynamicRepositoryPath.ShouldBe(null);
@@ -158,12 +158,12 @@ public void UsingDynamicRepositoryWithFeatureBranchWorks()
158158
{
159159
mainRepositoryFixture.Repository.MakeACommit();
160160

161-
var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new Authentication(), "feature1", false, tempDir);
162-
gitPreparer.Initialise(true, null);
161+
var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new Authentication(), false, tempDir);
162+
gitPreparer.Initialise(true, "feature1");
163163

164-
mainRepositoryFixture.Repository.CreateBranch("feature1").Checkout();
164+
mainRepositoryFixture.Repository.Checkout(mainRepositoryFixture.Repository.CreateBranch("feature1"));
165165

166-
Should.NotThrow(() => gitPreparer.Initialise(true, null));
166+
Should.NotThrow(() => gitPreparer.Initialise(true, "feature1"));
167167
}
168168
}
169169
finally
@@ -186,8 +186,7 @@ public void UsingDynamicRepositoryWithoutTargetBranchFails()
186186
{
187187
mainRepositoryFixture.Repository.MakeACommit();
188188

189-
var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new Authentication(), null, false, tempDir);
190-
gitPreparer.Initialise(true, null);
189+
var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new Authentication(), false, tempDir);
191190

192191
Should.Throw<Exception>(() => gitPreparer.Initialise(true, null));
193192
}
@@ -208,9 +207,9 @@ public void TestErrorThrownForInvalidRepository()
208207

209208
try
210209
{
211-
var gitPreparer = new GitPreparer("http://127.0.0.1/testrepo.git", null, new Authentication(), null, false, tempDir);
210+
var gitPreparer = new GitPreparer("http://127.0.0.1/testrepo.git", null, new Authentication(), false, tempDir);
212211

213-
Should.Throw<Exception>(() => gitPreparer.Initialise(true, null));
212+
Should.Throw<Exception>(() => gitPreparer.Initialise(true, "master"));
214213
}
215214
finally
216215
{

src/GitVersionCore/BuildServers/GitHelper.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut
3535
CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(repo, remote.Name);
3636

3737
var headSha = repo.Refs.Head.TargetIdentifier;
38-
38+
3939
if (!repo.Info.IsHeadDetached)
4040
{
4141
Logger.WriteInfo(string.Format("HEAD points at branch '{0}'.", headSha));
@@ -102,8 +102,10 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut
102102
static void EnsureLocalBranchExistsForCurrentBranch(Repository repo, string currentBranch)
103103
{
104104
if (string.IsNullOrEmpty(currentBranch)) return;
105+
106+
var isRef = currentBranch.Contains("refs");
105107
var isBranch = currentBranch.Contains("refs/heads");
106-
var localCanonicalName = isBranch ? currentBranch : currentBranch.Replace("refs/", "refs/heads/");
108+
var localCanonicalName = !isRef ? "refs/heads/" + currentBranch : isBranch ? currentBranch : currentBranch.Replace("refs/", "refs/heads/");
107109
var repoTip = repo.Head.Tip;
108110
var repoTipId = repoTip.Id;
109111

@@ -121,6 +123,8 @@ static void EnsureLocalBranchExistsForCurrentBranch(Repository repo, string curr
121123
string.Format("Updating local branch {0} to match ref {1}", localCanonicalName, currentBranch));
122124
repo.Refs.UpdateTarget(repo.Refs[localCanonicalName], repoTipId);
123125
}
126+
127+
repo.Checkout(localCanonicalName);
124128
}
125129

126130
public static bool LooksLikeAValidPullRequestNumber(string issueNumber)

src/GitVersionCore/ExecuteCore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ public static class ExecuteCore
99
public static VersionVariables ExecuteGitVersion(IFileSystem fileSystem, string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId)
1010
{
1111
// Normalise if we are running on build server
12-
var gitPreparer = new GitPreparer(targetUrl, dynamicRepositoryLocation, authentication, targetBranch, noFetch, workingDirectory);
12+
var gitPreparer = new GitPreparer(targetUrl, dynamicRepositoryLocation, authentication, noFetch, workingDirectory);
1313
var applicableBuildServers = BuildServerList.GetApplicableBuildServers();
1414
var buildServer = applicableBuildServers.FirstOrDefault();
1515
var currentBranch = buildServer == null ? null : buildServer.GetCurrentBranch();
1616
if (!string.IsNullOrEmpty(currentBranch))
1717
{
1818
Logger.WriteInfo("Branch from build environment: " + currentBranch);
1919
}
20-
gitPreparer.Initialise(buildServer != null, currentBranch);
20+
gitPreparer.Initialise(buildServer != null, currentBranch ?? targetBranch);
2121
var dotGitDirectory = gitPreparer.GetDotGitDirectory();
2222
var projectRoot = gitPreparer.GetProjectRootDirectory();
2323
Logger.WriteInfo(string.Format("Project root is: " + projectRoot));

src/GitVersionCore/GitPreparer.cs

Lines changed: 17 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@ public class GitPreparer
1010
string targetUrl;
1111
string dynamicRepositoryLocation;
1212
Authentication authentication;
13-
string targetBranch;
1413
bool noFetch;
1514
string targetPath;
1615

17-
public GitPreparer(string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string targetPath)
16+
public GitPreparer(string targetUrl, string dynamicRepositoryLocation, Authentication authentication, bool noFetch, string targetPath)
1817
{
1918
this.targetUrl = targetUrl;
2019
this.dynamicRepositoryLocation = dynamicRepositoryLocation;
2120
this.authentication = authentication;
22-
this.targetBranch = targetBranch;
2321
this.noFetch = noFetch;
2422
this.targetPath = targetPath;
2523
}
@@ -44,11 +42,7 @@ public void Initialise(bool normaliseGitDirectory, string currentBranch)
4442

4543
var tempRepositoryPath = CalculateTemporaryRepositoryPath(targetUrl, dynamicRepositoryLocation);
4644

47-
DynamicGitRepositoryPath = CreateDynamicRepository(tempRepositoryPath, authentication, targetUrl, targetBranch, noFetch);
48-
if (normaliseGitDirectory)
49-
{
50-
GitHelper.NormalizeGitDirectory(GetDotGitDirectory(), authentication, noFetch, currentBranch);
51-
}
45+
DynamicGitRepositoryPath = CreateDynamicRepository(tempRepositoryPath, authentication, targetUrl, currentBranch, noFetch);
5246
}
5347

5448
static string CalculateTemporaryRepositoryPath(string targetUrl, string dynamicRepositoryLocation)
@@ -110,32 +104,31 @@ public string GetProjectRootDirectory()
110104

111105
static string CreateDynamicRepository(string targetPath, Authentication authentication, string repositoryUrl, string targetBranch, bool noFetch)
112106
{
107+
if (string.IsNullOrWhiteSpace(targetBranch))
108+
{
109+
throw new Exception("Dynamic Git repositories must have a target branch (/b)");
110+
}
113111
Logger.WriteInfo(string.Format("Creating dynamic repository at '{0}'", targetPath));
114112

115113
var gitDirectory = Path.Combine(targetPath, ".git");
116114
if (Directory.Exists(targetPath))
117115
{
118116
Logger.WriteInfo("Git repository already exists");
119-
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, null);
120-
Logger.WriteInfo(string.Format("Updating branch '{0}'", targetBranch));
121-
using (var repo = new Repository(targetPath))
122-
{
123-
if (string.IsNullOrWhiteSpace(targetBranch))
124-
{
125-
throw new Exception("Dynamic Git repositories must have a target branch (/b)");
126-
}
127-
var targetGitBranch = repo.Branches[targetBranch];
128-
var trackedBranch = targetGitBranch.TrackedBranch;
129-
if (trackedBranch == null)
130-
throw new InvalidOperationException(string.Format("Expecting {0} to have a remote tracking branch", targetBranch));
131-
132-
targetGitBranch.Checkout();
133-
repo.Reset(ResetMode.Hard, trackedBranch.Tip);
134-
}
117+
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch);
135118

136119
return gitDirectory;
137120
}
138121

122+
CloneRepository(repositoryUrl, gitDirectory, authentication);
123+
124+
// Normalize (download branches) before using the branch
125+
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch);
126+
127+
return gitDirectory;
128+
}
129+
130+
private static void CloneRepository(string repositoryUrl, string gitDirectory, Authentication authentication)
131+
{
139132
Credentials credentials = null;
140133
if (!string.IsNullOrWhiteSpace(authentication.Username) && !string.IsNullOrWhiteSpace(authentication.Password))
141134
{
@@ -150,53 +143,6 @@ static string CreateDynamicRepository(string targetPath, Authentication authenti
150143

151144
Logger.WriteInfo(string.Format("Retrieving git info from url '{0}'", repositoryUrl));
152145

153-
CloneRepository(repositoryUrl, gitDirectory, credentials);
154-
155-
// Normalize (download branches) before using the branch
156-
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, null);
157-
158-
using (var repository = new Repository(gitDirectory))
159-
{
160-
if (string.IsNullOrWhiteSpace(targetBranch))
161-
{
162-
targetBranch = repository.Head.Name;
163-
}
164-
165-
Reference newHead = null;
166-
167-
var localReference = GetLocalReference(repository, targetBranch);
168-
if (localReference != null)
169-
{
170-
newHead = localReference;
171-
}
172-
173-
if (newHead == null)
174-
{
175-
var remoteReference = GetRemoteReference(repository, targetBranch, repositoryUrl, authentication);
176-
if (remoteReference != null)
177-
{
178-
repository.Network.Fetch(repositoryUrl, new[]
179-
{
180-
string.Format("{0}:{1}", remoteReference.CanonicalName, targetBranch)
181-
});
182-
183-
newHead = repository.Refs[string.Format("refs/heads/{0}", targetBranch)];
184-
}
185-
}
186-
187-
if (newHead != null)
188-
{
189-
Logger.WriteInfo(string.Format("Switching to branch '{0}'", targetBranch));
190-
191-
repository.Refs.UpdateTarget(repository.Refs.Head, newHead);
192-
}
193-
}
194-
195-
return gitDirectory;
196-
}
197-
198-
private static void CloneRepository(string repositoryUrl, string gitDirectory, Credentials credentials)
199-
{
200146
try
201147
{
202148
Repository.Clone(repositoryUrl, gitDirectory,
@@ -223,23 +169,7 @@ private static void CloneRepository(string repositoryUrl, string gitDirectory, C
223169
}
224170

225171
throw new Exception("There was an unknown problem with the Git repository you provided");
226-
227172
}
228173
}
229-
230-
private static Reference GetLocalReference(Repository repository, string branchName)
231-
{
232-
var targetBranchName = branchName.GetCanonicalBranchName();
233-
234-
return repository.Refs.FirstOrDefault(localRef => string.Equals(localRef.CanonicalName, targetBranchName));
235-
}
236-
237-
private static DirectReference GetRemoteReference(Repository repository, string branchName, string repositoryUrl, Authentication authentication)
238-
{
239-
var targetBranchName = branchName.GetCanonicalBranchName();
240-
241-
var remoteReferences = GitHelper.GetRemoteTipsUsingUsernamePasswordCredentials(repository, repositoryUrl, authentication.Username, authentication.Password);
242-
return remoteReferences.FirstOrDefault(remoteRef => string.Equals(remoteRef.CanonicalName, targetBranchName));
243-
}
244174
}
245175
}

src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void UsesGitVersionConfigWhenCreatingDynamicRepository()
7171
var configFile = Path.Combine(localRepoPath, "GitVersionConfig.yaml");
7272
File.WriteAllText(configFile, "next-version: 1.0.0");
7373

74-
var arguments = string.Format(" /url {0} /dynamicRepoLocation {1}", remote.RepositoryPath, repoBasePath);
74+
var arguments = string.Format(" /url {0} /dynamicRepoLocation {1} /b master", remote.RepositoryPath, repoBasePath);
7575
var results = GitVersionHelper.ExecuteIn(localRepoPath, arguments, false);
7676
results.OutputVariables.SemVer.ShouldBe("1.0.0");
7777
}

0 commit comments

Comments
 (0)