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

Commit 1e4f94d

Browse files
authored
Merge pull request #34 from JakeGinnivan/NeverMoveHEADWhenNormalising
WIP: Never move head when normalising
2 parents aaa24ad + 9d81fe1 commit 1e4f94d

15 files changed

+540
-531
lines changed

src/GitTools.Core.Tests/Git/GitRepositoryFactoryTests.cs renamed to src/GitTools.Core.Tests/Git/DynamicRepositoriesTests.cs

Lines changed: 98 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using Testing;
1212

1313
[TestFixture]
14-
public class GitRepositoryFactoryTests
14+
public class DynamicRepositoriesTests
1515
{
1616
const string DefaultBranchName = "master";
1717
const string SpecificBranchName = "feature/foo";
@@ -37,25 +37,22 @@ public void WorksCorrectlyWithRemoteRepository(string branchName, string expecte
3737
fixture.Repository.MakeCommits(5);
3838
fixture.Repository.CreateFileAndCommit("TestFile.txt");
3939

40-
fixture.Repository.CreateBranch(SpecificBranchName);
40+
var branch = fixture.Repository.CreateBranch(SpecificBranchName);
4141

4242
// Copy contents into working directory
4343
File.Copy(Path.Combine(fixture.RepositoryPath, "TestFile.txt"), Path.Combine(tempDir, "TestFile.txt"));
4444

4545
var repositoryInfo = new RepositoryInfo
4646
{
47-
Url = fixture.RepositoryPath,
48-
Branch = branchName
47+
Url = fixture.RepositoryPath
4948
};
5049

51-
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
50+
using (var dynamicRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, branchName, branch.Tip.Sha))
5251
{
53-
dynamicRepositoryPath = gitRepository.DotGitDirectory;
52+
dynamicRepositoryPath = dynamicRepository.Repository.Info.Path;
53+
dynamicRepository.Repository.Info.Path.ShouldBe(Path.Combine(expectedDynamicRepoLocation, ".git\\"));
5454

55-
gitRepository.IsDynamic.ShouldBe(true);
56-
gitRepository.DotGitDirectory.ShouldBe(Path.Combine(expectedDynamicRepoLocation, ".git"));
57-
58-
var currentBranch = gitRepository.Repository.Head.CanonicalName;
55+
var currentBranch = dynamicRepository.Repository.Head.CanonicalName;
5956

6057
currentBranch.ShouldEndWith(expectedBranchName);
6158
}
@@ -85,26 +82,24 @@ public void UpdatesExistingDynamicRepository()
8582
{
8683
using (var mainRepositoryFixture = new EmptyRepositoryFixture())
8784
{
88-
mainRepositoryFixture.Repository.MakeCommits(1);
85+
var commit = mainRepositoryFixture.Repository.MakeACommit();
8986

9087
var repositoryInfo = new RepositoryInfo
9188
{
92-
Url = mainRepositoryFixture.RepositoryPath,
93-
Branch = "master"
89+
Url = mainRepositoryFixture.RepositoryPath
9490
};
9591

96-
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
92+
using (var dynamicRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", commit.Sha))
9793
{
98-
dynamicRepositoryPath = gitRepository.DotGitDirectory;
94+
dynamicRepositoryPath = dynamicRepository.Repository.Info.Path;
9995
}
10096

10197
var newCommit = mainRepositoryFixture.Repository.MakeACommit();
10298

103-
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
99+
using (var dynamicRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", newCommit.Sha))
104100
{
105-
mainRepositoryFixture.Repository.DumpGraph();
106-
gitRepository.Repository.DumpGraph();
107-
gitRepository.Repository.Commits.ShouldContain(c => c.Sha == newCommit.Sha);
101+
dynamicRepository.Repository.Info.Path.ShouldBe(dynamicRepositoryPath);
102+
dynamicRepository.Repository.Commits.ShouldContain(c => c.Sha == newCommit.Sha);
108103
}
109104
}
110105
}
@@ -133,21 +128,19 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
133128
{
134129
using (var fixture = new EmptyRepositoryFixture())
135130
{
136-
fixture.Repository.CreateFileAndCommit("TestFile.txt");
131+
var head = fixture.Repository.CreateFileAndCommit("TestFile.txt");
137132
File.Copy(Path.Combine(fixture.RepositoryPath, "TestFile.txt"), Path.Combine(tempDir, "TestFile.txt"));
138133
expectedDynamicRepoLocation = Path.Combine(tempPath, fixture.RepositoryPath.Split(Path.DirectorySeparatorChar).Last());
139134
Directory.CreateDirectory(expectedDynamicRepoLocation);
140135

141136
var repositoryInfo = new RepositoryInfo
142137
{
143-
Url = fixture.RepositoryPath,
144-
Branch = "master"
138+
Url = fixture.RepositoryPath
145139
};
146140

147-
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
141+
using (var dynamicRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", head.Sha))
148142
{
149-
gitRepository.IsDynamic.ShouldBe(true);
150-
gitRepository.DotGitDirectory.ShouldBe(Path.Combine(expectedDynamicRepoLocation + "_1", ".git"));
143+
dynamicRepository.Repository.Info.Path.ShouldBe(Path.Combine(expectedDynamicRepoLocation + "_1", ".git\\"));
151144
}
152145
}
153146
}
@@ -166,18 +159,58 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
166159
}
167160
}
168161

162+
[Test]
163+
[Category("NoMono")]
164+
public void PicksAnotherDirectoryNameWhenDynamicRepoFolderIsInUse()
165+
{
166+
var tempPath = Path.GetTempPath();
167+
var expectedDynamicRepoLocation = default(string);
168+
var expectedDynamicRepo2Location = default(string);
169+
170+
try
171+
{
172+
using (var fixture = new EmptyRepositoryFixture())
173+
{
174+
var head = fixture.Repository.CreateFileAndCommit("TestFile.txt");
175+
var repositoryInfo = new RepositoryInfo
176+
{
177+
Url = fixture.RepositoryPath
178+
};
179+
180+
using (var dynamicRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", head.Sha))
181+
using (var dynamicRepository2 = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", head.Sha))
182+
{
183+
expectedDynamicRepoLocation = dynamicRepository.Repository.Info.Path;
184+
expectedDynamicRepo2Location = dynamicRepository2.Repository.Info.Path;
185+
dynamicRepository.Repository.Info.Path.ShouldNotBe(dynamicRepository2.Repository.Info.Path);
186+
}
187+
}
188+
}
189+
finally
190+
{
191+
if (expectedDynamicRepoLocation != null)
192+
{
193+
DeleteHelper.DeleteDirectory(expectedDynamicRepoLocation, true);
194+
}
195+
196+
if (expectedDynamicRepo2Location != null)
197+
{
198+
DeleteHelper.DeleteGitRepository(expectedDynamicRepo2Location);
199+
}
200+
}
201+
}
202+
169203
[Test]
170204
public void ThrowsExceptionWhenNotEnoughInfo()
171205
{
172206
var tempDir = Path.GetTempPath();
173207

174208
var repositoryInfo = new RepositoryInfo
175209
{
176-
Url = tempDir,
177-
Branch = "master"
210+
Url = tempDir
178211
};
179212

180-
Should.Throw<Exception>(() => GitRepositoryFactory.CreateRepository(repositoryInfo));
213+
Should.Throw<Exception>(() => DynamicRepositories.CreateOrOpen(repositoryInfo, tempDir, null, null));
181214
}
182215

183216
[Test]
@@ -192,21 +225,19 @@ public void UsingDynamicRepositoryWithFeatureBranchWorks()
192225
{
193226
using (var mainRepositoryFixture = new EmptyRepositoryFixture())
194227
{
195-
mainRepositoryFixture.Repository.MakeACommit();
228+
var commit = mainRepositoryFixture.Repository.MakeACommit();
196229

197230
var repositoryInfo = new RepositoryInfo
198231
{
199-
Url = mainRepositoryFixture.RepositoryPath,
200-
Branch = "feature1"
232+
Url = mainRepositoryFixture.RepositoryPath
201233
};
202234

203235
mainRepositoryFixture.Repository.Checkout(mainRepositoryFixture.Repository.CreateBranch("feature1"));
204236

205237
Should.NotThrow(() =>
206238
{
207-
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
239+
using (DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "feature1", commit.Sha))
208240
{
209-
// this code shouldn't throw
210241
}
211242
});
212243
}
@@ -220,35 +251,46 @@ public void UsingDynamicRepositoryWithFeatureBranchWorks()
220251
[Test]
221252
public void UsingDynamicRepositoryWithoutTargetBranchFails()
222253
{
223-
var repoName = Guid.NewGuid().ToString();
224254
var tempPath = Path.GetTempPath();
225-
var tempDir = Path.Combine(tempPath, repoName);
226-
Directory.CreateDirectory(tempDir);
227255

228-
try
256+
using (var mainRepositoryFixture = new EmptyRepositoryFixture())
229257
{
230-
using (var mainRepositoryFixture = new EmptyRepositoryFixture())
231-
{
232-
mainRepositoryFixture.Repository.MakeACommit();
258+
mainRepositoryFixture.Repository.MakeACommit();
233259

234-
var repositoryInfo = new RepositoryInfo
235-
{
236-
Url = mainRepositoryFixture.RepositoryPath,
237-
Branch = null
238-
};
260+
var repositoryInfo = new RepositoryInfo
261+
{
262+
Url = mainRepositoryFixture.RepositoryPath
263+
};
239264

240-
Should.Throw<Exception>(() =>
265+
Should.Throw<GitToolsException>(() =>
266+
{
267+
using (DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, null, null))
241268
{
242-
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
243-
{
244-
// this code shouldn't throw
245-
}
246-
});
247-
}
269+
}
270+
});
248271
}
249-
finally
272+
}
273+
274+
[Test]
275+
public void UsingDynamicRepositoryWithoutTargetBranchCommitFails()
276+
{
277+
var tempPath = Path.GetTempPath();
278+
279+
using (var mainRepositoryFixture = new EmptyRepositoryFixture())
250280
{
251-
Directory.Delete(tempDir, true);
281+
mainRepositoryFixture.Repository.MakeACommit();
282+
283+
var repositoryInfo = new RepositoryInfo
284+
{
285+
Url = mainRepositoryFixture.RepositoryPath
286+
};
287+
288+
Should.Throw<GitToolsException>(() =>
289+
{
290+
using (DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", null))
291+
{
292+
}
293+
});
252294
}
253295
}
254296

@@ -264,15 +306,13 @@ public void TestErrorThrownForInvalidRepository()
264306
{
265307
var repositoryInfo = new RepositoryInfo
266308
{
267-
Url = "http://127.0.0.1/testrepo.git",
268-
Branch = "master"
309+
Url = "http://127.0.0.1/testrepo.git"
269310
};
270311

271312
Should.Throw<Exception>(() =>
272313
{
273-
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
314+
using (DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", "sha"))
274315
{
275-
// this code shouldn't throw
276316
}
277317
});
278318
}

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
}

0 commit comments

Comments
 (0)