Skip to content

Commit 33b52d8

Browse files
committed
Added some initial tests around branch normalisation
1 parent aba2bec commit 33b52d8

File tree

3 files changed

+104
-8
lines changed

3 files changed

+104
-8
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
namespace GitVersionCore.Tests
2+
{
3+
using GitVersion;
4+
using LibGit2Sharp;
5+
using NUnit.Framework;
6+
using Shouldly;
7+
8+
public class GitHelperTests
9+
{
10+
[Test]
11+
public void NormalisationOfPullRequestsWithFetch()
12+
{
13+
using (var fixture = new EmptyRepositoryFixture(new Config()))
14+
{
15+
fixture.Repository.MakeACommit();
16+
17+
fixture.Repository.CreateBranch("feature/foo").Checkout();
18+
fixture.Repository.MakeACommit();
19+
var commit = fixture.Repository.CreatePullRequestRef("feature/foo", "master", prNumber: 3);
20+
using (var localFixture = fixture.CloneRepository())
21+
{
22+
localFixture.Checkout(commit.Sha);
23+
GitHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new Authentication(), noFetch: false, currentBranch: string.Empty);
24+
25+
var normalisedPullBranch = localFixture.Repository.FindBranch("pull/3/merge");
26+
normalisedPullBranch.ShouldNotBe(null);
27+
}
28+
}
29+
}
30+
31+
[Test]
32+
public void NormalisationOfPullRequestsWithoutFetch()
33+
{
34+
using (var fixture = new EmptyRepositoryFixture(new Config()))
35+
{
36+
fixture.Repository.MakeACommit();
37+
38+
fixture.Repository.CreateBranch("feature/foo").Checkout();
39+
fixture.Repository.MakeACommit();
40+
var commit = fixture.Repository.CreatePullRequestRef("feature/foo", "master", prNumber: 3, allowFastFowardMerge: true);
41+
using (var localFixture = fixture.CloneRepository())
42+
{
43+
localFixture.Checkout(commit.Sha);
44+
GitHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new Authentication(), noFetch: true, currentBranch: "refs/pull/3/merge");
45+
46+
var normalisedPullBranch = localFixture.Repository.FindBranch("pull/3/merge");
47+
normalisedPullBranch.ShouldNotBe(null);
48+
}
49+
}
50+
}
51+
52+
[Test]
53+
public void UpdatesLocalBranchesWhen()
54+
{
55+
using (var fixture = new EmptyRepositoryFixture(new Config()))
56+
{
57+
fixture.Repository.MakeACommit();
58+
59+
fixture.Repository.CreateBranch("feature/foo").Checkout();
60+
fixture.Repository.MakeACommit();
61+
using (var localFixture = fixture.CloneRepository())
62+
{
63+
localFixture.Checkout("feature/foo");
64+
// Advance remote
65+
var advancedCommit = fixture.Repository.MakeACommit();
66+
GitHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new Authentication(), noFetch: false, currentBranch: null);
67+
68+
var normalisedBranch = localFixture.Repository.FindBranch("feature/foo");
69+
normalisedBranch.ShouldNotBe(null);
70+
normalisedBranch.Tip.Sha.ShouldBe(advancedCommit.Sha);
71+
}
72+
}
73+
}
74+
}
75+
}

src/GitVersionCore.Tests/GitVersionCore.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
<Compile Include="Fixtures\RemoteRepositoryFixture.cs" />
126126
<Compile Include="GitDirFinderTests.cs" />
127127
<Compile Include="Fixtures\BaseGitFlowRepositoryFixture.cs" />
128+
<Compile Include="GitHelperTests.cs" />
128129
<Compile Include="GitPreparerTests.cs" />
129130
<Compile Include="GitVersionContextTests.cs" />
130131
<Compile Include="Helpers\DirectoryHelper.cs" />

src/GitVersionCore/BuildServers/GitHelper.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut
3232
}
3333

3434
EnsureLocalBranchExistsForCurrentBranch(repo, currentBranch);
35-
CreateMissingLocalBranchesFromRemoteTrackingOnes(repo, remote.Name);
35+
CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(repo, remote.Name);
3636

3737
var headSha = repo.Refs.Head.TargetIdentifier;
3838

@@ -104,12 +104,23 @@ static void EnsureLocalBranchExistsForCurrentBranch(Repository repo, string curr
104104
if (string.IsNullOrEmpty(currentBranch)) return;
105105
var isBranch = currentBranch.Contains("refs/heads");
106106
var localCanonicalName = isBranch ? currentBranch : currentBranch.Replace("refs/", "refs/heads/");
107-
if (repo.Branches[localCanonicalName] != null) return;
107+
var repoTip = repo.Head.Tip;
108+
var repoTipId = repoTip.Id;
108109

109-
Logger.WriteInfo(isBranch ?
110-
string.Format("Creating local branch {0}", localCanonicalName) :
111-
string.Format("Creating local branch {0} from ref {1}", localCanonicalName, currentBranch));
112-
repo.Refs.Add(localCanonicalName, repo.Head.Tip.Id);
110+
if (repo.Branches.All(b => b.CanonicalName != localCanonicalName))
111+
{
112+
Logger.WriteInfo(isBranch ?
113+
string.Format("Creating local branch {0}", localCanonicalName) :
114+
string.Format("Creating local branch {0} from ref {1}", localCanonicalName, currentBranch));
115+
repo.Refs.Add(localCanonicalName, repoTipId);
116+
}
117+
else
118+
{
119+
Logger.WriteInfo(isBranch ?
120+
string.Format("Updating local branch {0} to point at {1}", localCanonicalName, repoTip.Sha) :
121+
string.Format("Updating local branch {0} to match ref {1}", localCanonicalName, currentBranch));
122+
repo.Refs.UpdateTarget(repo.Refs[localCanonicalName], repoTipId);
123+
}
113124
}
114125

115126
public static bool LooksLikeAValidPullRequestNumber(string issueNumber)
@@ -241,7 +252,7 @@ static IEnumerable<DirectReference> GetRemoteTipsForAnonymousUser(Repository rep
241252
return repo.Network.ListReferences(remote);
242253
}
243254

244-
static void CreateMissingLocalBranchesFromRemoteTrackingOnes(Repository repo, string remoteName)
255+
static void CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(Repository repo, string remoteName)
245256
{
246257
var prefix = string.Format("refs/remotes/{0}/", remoteName);
247258
var remoteHeadCanonicalName = string.Format("{0}{1}", prefix, "HEAD");
@@ -254,7 +265,16 @@ static void CreateMissingLocalBranchesFromRemoteTrackingOnes(Repository repo, st
254265

255266
if (repo.Refs.Any(x => x.CanonicalName == localCanonicalName))
256267
{
257-
Logger.WriteInfo(string.Format("Skipping local branch creation since it already exists '{0}'.", remoteTrackingReference.CanonicalName));
268+
var localRef = repo.Refs[localCanonicalName];
269+
var remotedirectReference = remoteTrackingReference.ResolveToDirectReference();
270+
if (localRef.ResolveToDirectReference().TargetIdentifier == remotedirectReference.TargetIdentifier)
271+
{
272+
Logger.WriteInfo(string.Format("Skipping update of '{0}' as it already matches the remote ref.", remoteTrackingReference.CanonicalName));
273+
continue;
274+
}
275+
var remoteRefTipId = remotedirectReference.Target.Id;
276+
Logger.WriteInfo(string.Format("Updating local ref '{0}' to point at {1}.", remoteTrackingReference.CanonicalName, remoteRefTipId));
277+
repo.Refs.UpdateTarget(localRef, remoteRefTipId);
258278
continue;
259279
}
260280
Logger.WriteInfo(string.Format("Creating local branch from remote tracking '{0}'.", remoteTrackingReference.CanonicalName));

0 commit comments

Comments
 (0)