Skip to content

Commit 03d6bac

Browse files
committed
Updated a bit of the normalise code and logging
1 parent 7d5f6c7 commit 03d6bac

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

src/GitVersionCore.Tests/GitHelperTests.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void NormalisationOfPullRequestsWithFetch()
1414
{
1515
fixture.Repository.MakeACommit();
1616

17-
fixture.Repository.CreateBranch("feature/foo").Checkout();
17+
fixture.Repository.Checkout(fixture.Repository.CreateBranch("feature/foo"));
1818
fixture.Repository.MakeACommit();
1919
var commit = fixture.Repository.CreatePullRequestRef("feature/foo", "master", prNumber: 3);
2020
using (var localFixture = fixture.CloneRepository())
@@ -35,7 +35,7 @@ public void NormalisationOfPullRequestsWithoutFetch()
3535
{
3636
fixture.Repository.MakeACommit();
3737

38-
fixture.Repository.CreateBranch("feature/foo").Checkout();
38+
fixture.Repository.Checkout(fixture.Repository.CreateBranch("feature/foo"));
3939
fixture.Repository.MakeACommit();
4040
var commit = fixture.Repository.CreatePullRequestRef("feature/foo", "master", prNumber: 3, allowFastFowardMerge: true);
4141
using (var localFixture = fixture.CloneRepository())
@@ -56,7 +56,7 @@ public void UpdatesLocalBranchesWhen()
5656
{
5757
fixture.Repository.MakeACommit();
5858

59-
fixture.Repository.CreateBranch("feature/foo").Checkout();
59+
fixture.Repository.Checkout(fixture.Repository.CreateBranch("feature/foo"));
6060
fixture.Repository.MakeACommit();
6161
using (var localFixture = fixture.CloneRepository())
6262
{
@@ -71,5 +71,34 @@ public void UpdatesLocalBranchesWhen()
7171
}
7272
}
7373
}
74+
75+
[Test]
76+
public void UpdatesCurrentBranch()
77+
{
78+
using (var fixture = new EmptyRepositoryFixture(new Config()))
79+
{
80+
fixture.Repository.MakeACommit();
81+
fixture.Repository.Checkout(fixture.Repository.CreateBranch("develop"));
82+
fixture.Repository.MakeACommit();
83+
fixture.Repository.Checkout("master");
84+
using (var localFixture = fixture.CloneRepository())
85+
{
86+
// Advance remote
87+
fixture.Repository.Checkout("develop");
88+
var advancedCommit = fixture.Repository.MakeACommit();
89+
localFixture.Repository.Network.Fetch(localFixture.Repository.Network.Remotes["origin"]);
90+
localFixture.Repository.Checkout(advancedCommit.Sha);
91+
localFixture.Repository.DumpGraph();
92+
GitHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new Authentication(), noFetch: false, currentBranch: "ref/heads/develop");
93+
94+
var normalisedBranch = localFixture.Repository.FindBranch("develop");
95+
normalisedBranch.ShouldNotBe(null);
96+
fixture.Repository.DumpGraph();
97+
localFixture.Repository.DumpGraph();
98+
normalisedBranch.Tip.Sha.ShouldBe(advancedCommit.Sha);
99+
localFixture.Repository.Head.Tip.Sha.ShouldBe(advancedCommit.Sha);
100+
}
101+
}
102+
}
74103
}
75104
}

src/GitVersionCore/BuildServers/GitHelper.cs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut
4343
}
4444

4545
Logger.WriteInfo(string.Format("HEAD is detached and points at commit '{0}'.", headSha));
46-
Logger.WriteInfo(string.Format("Local Refs:\r\n" + string.Join(Environment.NewLine, repo.Refs.FromGlob("*").Select(r => r.CanonicalName))));
46+
Logger.WriteInfo(string.Format("Local Refs:\r\n" + string.Join(Environment.NewLine, repo.Refs.FromGlob("*").Select(r => string.Format("{0} ({1})", r.CanonicalName, r.TargetIdentifier)))));
4747

4848
// In order to decide whether a fake branch is required or not, first check to see if any local branches have the same commit SHA of the head SHA.
4949
// If they do, go ahead and checkout that branch
@@ -56,7 +56,7 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut
5656
if (matchingCurrentBranch != null)
5757
{
5858
Logger.WriteInfo(string.Format("Checking out local branch '{0}'.", currentBranch));
59-
matchingCurrentBranch.Checkout();
59+
repo.Checkout(matchingCurrentBranch);
6060
}
6161
else if (localBranchesWhereCommitShaIsHead.Count > 1)
6262
{
@@ -69,7 +69,7 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut
6969
if (master != null)
7070
{
7171
Logger.WriteWarning("Because one of the branches is 'master', will build master." + moveBranchMsg);
72-
master.Checkout();
72+
repo.Checkout(master);
7373
}
7474
else
7575
{
@@ -78,7 +78,7 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut
7878
{
7979
var branchWithoutSeparator = branchesWithoutSeparators[0];
8080
Logger.WriteWarning(string.Format("Choosing {0} as it is the only branch without / or - in it. " + moveBranchMsg, branchWithoutSeparator.CanonicalName));
81-
branchWithoutSeparator.Checkout();
81+
repo.Checkout(branchWithoutSeparator);
8282
}
8383
else
8484
{
@@ -94,7 +94,7 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut
9494
else
9595
{
9696
Logger.WriteInfo(string.Format("Checking out local branch 'refs/heads/{0}'.", localBranchesWhereCommitShaIsHead[0].Name));
97-
repo.Branches[localBranchesWhereCommitShaIsHead[0].Name].Checkout();
97+
repo.Checkout(repo.Branches[localBranchesWhereCommitShaIsHead[0].Name]);
9898
}
9999
}
100100
}
@@ -111,7 +111,7 @@ static void EnsureLocalBranchExistsForCurrentBranch(Repository repo, string curr
111111
{
112112
Logger.WriteInfo(isBranch ?
113113
string.Format("Creating local branch {0}", localCanonicalName) :
114-
string.Format("Creating local branch {0} from ref {1}", localCanonicalName, currentBranch));
114+
string.Format("Creating local branch {0} pointing at {1}", localCanonicalName, repoTipId));
115115
repo.Refs.Add(localCanonicalName, repoTipId);
116116
}
117117
else
@@ -273,21 +273,14 @@ static void CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(Repository repo, s
273273
continue;
274274
}
275275
var remoteRefTipId = remotedirectReference.Target.Id;
276-
Logger.WriteInfo(string.Format("Updating local ref '{0}' to point at {1}.", remoteTrackingReference.CanonicalName, remoteRefTipId));
276+
Logger.WriteInfo(string.Format("Updating local ref '{0}' to point at {1}.", localRef.CanonicalName, remoteRefTipId));
277277
repo.Refs.UpdateTarget(localRef, remoteRefTipId);
278+
repo.Checkout(branchName);
278279
continue;
279280
}
280-
Logger.WriteInfo(string.Format("Creating local branch from remote tracking '{0}'.", remoteTrackingReference.CanonicalName));
281281

282-
var symbolicReference = remoteTrackingReference as SymbolicReference;
283-
if (symbolicReference == null)
284-
{
285-
repo.Refs.Add(localCanonicalName, new ObjectId(remoteTrackingReference.TargetIdentifier), true);
286-
}
287-
else
288-
{
289-
repo.Refs.Add(localCanonicalName, new ObjectId(symbolicReference.ResolveToDirectReference().TargetIdentifier), true);
290-
}
282+
Logger.WriteInfo(string.Format("Creating local branch from remote tracking '{0}'.", remoteTrackingReference.CanonicalName));
283+
repo.Refs.Add(localCanonicalName, new ObjectId(remoteTrackingReference.ResolveToDirectReference().TargetIdentifier), true);
291284

292285
var branch = repo.Branches[branchName];
293286
repo.Branches.Update(branch, b => b.TrackedBranch = remoteTrackingReferenceName);

0 commit comments

Comments
 (0)