Skip to content
This repository was archived by the owner on Jun 27, 2019. It is now read-only.

Commit f59fe04

Browse files
committed
Fixed issue where the sha checked out by build server can be updated
1 parent aaa24ad commit f59fe04

File tree

7 files changed

+161
-240
lines changed

7 files changed

+161
-240
lines changed

src/GitTools.Core.Tests/Git/GitRepositoryHelperTests.cs

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void NormalisationOfPullRequestsWithFetch()
2323
localFixture.Checkout(commit.Sha);
2424
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: string.Empty);
2525

26-
var normalisedPullBranch = localFixture.Repository.FindBranch("pull/3/merge");
26+
var normalisedPullBranch = localFixture.Repository.Branches["pull/3/merge"];
2727
normalisedPullBranch.ShouldNotBe(null);
2828
}
2929
}
@@ -51,24 +51,30 @@ public void NormalisationOfPullRequestsWithoutFetch()
5151
}
5252

5353
[Test]
54-
public void UpdatesLocalBranchesWhen()
54+
public void NormalisationOfTag()
5555
{
5656
using (var fixture = new EmptyRepositoryFixture())
5757
{
5858
fixture.Repository.MakeACommit();
5959

6060
fixture.Repository.Checkout(fixture.Repository.CreateBranch("feature/foo"));
6161
fixture.Repository.MakeACommit();
62+
63+
fixture.BranchTo("release/2.0.0");
64+
fixture.MakeACommit();
65+
fixture.MakeATaggedCommit("2.0.0-rc.1");
66+
fixture.Checkout("master");
67+
fixture.MergeNoFF("release/2.0.0");
68+
fixture.Repository.Branches.Remove(fixture.Repository.Branches["release/2.0.0"]);
69+
var remoteTagSha = fixture.Repository.Tags["2.0.0-rc.1"].Target.Sha;
70+
6271
using (var localFixture = fixture.CloneRepository())
6372
{
64-
localFixture.Checkout("feature/foo");
65-
// Advance remote
66-
var advancedCommit = fixture.Repository.MakeACommit();
67-
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: null);
73+
localFixture.Checkout(remoteTagSha);
74+
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: string.Empty);
6875

69-
var normalisedBranch = localFixture.Repository.FindBranch("feature/foo");
70-
normalisedBranch.ShouldNotBe(null);
71-
normalisedBranch.Tip.Sha.ShouldBe(advancedCommit.Sha);
76+
localFixture.Repository.Head.FriendlyName.ShouldBe("(no branch)");
77+
localFixture.Repository.Head.Tip.Sha.ShouldBe(remoteTagSha);
7278
}
7379
}
7480
}
@@ -89,13 +95,10 @@ public void UpdatesCurrentBranch()
8995
var advancedCommit = fixture.Repository.MakeACommit();
9096
Commands.Fetch((Repository)localFixture.Repository, localFixture.Repository.Network.Remotes["origin"].Name, new string[0], null, null);
9197
localFixture.Repository.Checkout(advancedCommit.Sha);
92-
localFixture.Repository.DumpGraph();
93-
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: "ref/heads/develop");
98+
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: "refs/heads/develop");
9499

95-
var normalisedBranch = localFixture.Repository.FindBranch("develop");
100+
var normalisedBranch = localFixture.Repository.Branches["develop"];
96101
normalisedBranch.ShouldNotBe(null);
97-
fixture.Repository.DumpGraph();
98-
localFixture.Repository.DumpGraph();
99102
normalisedBranch.Tip.Sha.ShouldBe(advancedCommit.Sha);
100103
localFixture.Repository.Head.Tip.Sha.ShouldBe(advancedCommit.Sha);
101104
}
@@ -125,10 +128,53 @@ public void ShouldNotChangeBranchWhenNormalizingTheDirectory()
125128

126129
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: null);
127130

128-
localFixture.Repository.DumpGraph();
129131
localFixture.Repository.Head.Tip.Sha.ShouldBe(lastCommitOnDevelop.Sha);
130132
}
131133
}
132134
}
135+
136+
[Test]
137+
public void ShouldNotMoveLocalBranchWhenRemoteAdvances()
138+
{
139+
using (var fixture = new EmptyRepositoryFixture())
140+
{
141+
fixture.Repository.MakeACommit();
142+
143+
fixture.Repository.Checkout(fixture.Repository.CreateBranch("feature/foo"));
144+
fixture.Repository.MakeACommit();
145+
using (var localFixture = fixture.CloneRepository())
146+
{
147+
localFixture.Checkout("feature/foo");
148+
var expectedTip = localFixture.Repository.Head.Tip;
149+
// Advance remote
150+
fixture.Repository.MakeACommit();
151+
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: null);
152+
153+
var normalisedBranch = localFixture.Repository.Branches["feature/foo"];
154+
normalisedBranch.ShouldNotBe(null);
155+
normalisedBranch.Tip.Sha.ShouldBe(expectedTip.Sha);
156+
}
157+
}
158+
}
159+
160+
[Test]
161+
public void CheckedOutShaShouldNotChanged()
162+
{
163+
using (var fixture = new EmptyRepositoryFixture())
164+
{
165+
fixture.Repository.MakeACommit();
166+
var commitToBuild = fixture.Repository.MakeACommit();
167+
fixture.Repository.MakeACommit();
168+
169+
using (var localFixture = fixture.CloneRepository())
170+
{
171+
localFixture.Repository.Checkout(commitToBuild);
172+
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: "refs/heads/master");
173+
174+
var normalisedBranch = localFixture.Repository.Branches["master"];
175+
normalisedBranch.Tip.Sha.ShouldBe(commitToBuild.Sha);
176+
}
177+
}
178+
}
133179
}
134180
}

src/GitTools.Core.Tests/GitRepositoryTests.cs

Lines changed: 0 additions & 159 deletions
This file was deleted.

src/GitTools.Core.Tests/GitTools.Core.Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
<Compile Include="..\SolutionAssemblyInfo.cs">
7272
<Link>Properties\SolutionAssemblyInfo.cs</Link>
7373
</Compile>
74-
<Compile Include="GitRepositoryTests.cs" />
7574
<Compile Include="Git\Extensions\AuthenticationInfoExtensionsTests.cs" />
7675
<Compile Include="Git\GitDirFinderTests.cs" />
7776
<Compile Include="Git\GitRepositoryHelperTests.cs" />

src/GitTools.Core/GitTools.Core.Shared/Git/Extensions/LibGitExtensions.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ public static void DumpGraph(string workingDirectory, Action<string> writer = nu
166166
e => output.AppendLineFormat("ERROR: {0}", e),
167167
null,
168168
"git",
169-
@"log --graph --format=""%h %cr %d"" --decorate --date=relative --all --remotes=*" + (maxCommits != null ? string.Format(" -n {0}", maxCommits) : null),
170-
//@"log --graph --abbrev-commit --decorate --date=relative --all --remotes=*",
169+
CreateGitLogArgs(maxCommits),
171170
workingDirectory);
172171
}
173172
catch (FileNotFoundException exception)
@@ -190,5 +189,10 @@ public static void DumpGraph(string workingDirectory, Action<string> writer = nu
190189
Console.Write(output.ToString());
191190
}
192191
}
192+
193+
public static string CreateGitLogArgs(int? maxCommits)
194+
{
195+
return @"log --graph --format=""%h %cr %d"" --decorate --date=relative --all --remotes=*" + (maxCommits != null ? string.Format(" -n {0}", maxCommits) : null);
196+
}
193197
}
194198
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace GitTools.Git
2+
{
3+
using System;
4+
5+
public class BugException : Exception
6+
{
7+
public BugException(string message) : base(message)
8+
{
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)