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

WIP: Never move head when normalising #34

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using Testing;

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

fixture.Repository.CreateBranch(SpecificBranchName);
var branch = fixture.Repository.CreateBranch(SpecificBranchName);

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

var repositoryInfo = new RepositoryInfo
{
Url = fixture.RepositoryPath,
Branch = branchName
Url = fixture.RepositoryPath
};

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

gitRepository.IsDynamic.ShouldBe(true);
gitRepository.DotGitDirectory.ShouldBe(Path.Combine(expectedDynamicRepoLocation, ".git"));

var currentBranch = gitRepository.Repository.Head.CanonicalName;
var currentBranch = dynamicRepository.Repository.Head.CanonicalName;

currentBranch.ShouldEndWith(expectedBranchName);
}
Expand Down Expand Up @@ -85,26 +82,24 @@ public void UpdatesExistingDynamicRepository()
{
using (var mainRepositoryFixture = new EmptyRepositoryFixture())
{
mainRepositoryFixture.Repository.MakeCommits(1);
var commit = mainRepositoryFixture.Repository.MakeACommit();

var repositoryInfo = new RepositoryInfo
{
Url = mainRepositoryFixture.RepositoryPath,
Branch = "master"
Url = mainRepositoryFixture.RepositoryPath
};

using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
using (var dynamicRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", commit.Sha))
{
dynamicRepositoryPath = gitRepository.DotGitDirectory;
dynamicRepositoryPath = dynamicRepository.Repository.Info.Path;
}

var newCommit = mainRepositoryFixture.Repository.MakeACommit();

using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
using (var dynamicRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", newCommit.Sha))
{
mainRepositoryFixture.Repository.DumpGraph();
gitRepository.Repository.DumpGraph();
gitRepository.Repository.Commits.ShouldContain(c => c.Sha == newCommit.Sha);
dynamicRepository.Repository.Info.Path.ShouldBe(dynamicRepositoryPath);
dynamicRepository.Repository.Commits.ShouldContain(c => c.Sha == newCommit.Sha);
}
}
}
Expand Down Expand Up @@ -133,21 +128,19 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
{
using (var fixture = new EmptyRepositoryFixture())
{
fixture.Repository.CreateFileAndCommit("TestFile.txt");
var head = fixture.Repository.CreateFileAndCommit("TestFile.txt");
File.Copy(Path.Combine(fixture.RepositoryPath, "TestFile.txt"), Path.Combine(tempDir, "TestFile.txt"));
expectedDynamicRepoLocation = Path.Combine(tempPath, fixture.RepositoryPath.Split(Path.DirectorySeparatorChar).Last());
Directory.CreateDirectory(expectedDynamicRepoLocation);

var repositoryInfo = new RepositoryInfo
{
Url = fixture.RepositoryPath,
Branch = "master"
Url = fixture.RepositoryPath
};

using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
using (var dynamicRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", head.Sha))
{
gitRepository.IsDynamic.ShouldBe(true);
gitRepository.DotGitDirectory.ShouldBe(Path.Combine(expectedDynamicRepoLocation + "_1", ".git"));
dynamicRepository.Repository.Info.Path.ShouldBe(Path.Combine(expectedDynamicRepoLocation + "_1", ".git\\"));
}
}
}
Expand All @@ -166,18 +159,58 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
}
}

[Test]
[Category("NoMono")]
public void PicksAnotherDirectoryNameWhenDynamicRepoFolderIsInUse()
{
var tempPath = Path.GetTempPath();
var expectedDynamicRepoLocation = default(string);
var expectedDynamicRepo2Location = default(string);

try
{
using (var fixture = new EmptyRepositoryFixture())
{
var head = fixture.Repository.CreateFileAndCommit("TestFile.txt");
var repositoryInfo = new RepositoryInfo
{
Url = fixture.RepositoryPath
};

using (var dynamicRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", head.Sha))
using (var dynamicRepository2 = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", head.Sha))
{
expectedDynamicRepoLocation = dynamicRepository.Repository.Info.Path;
expectedDynamicRepo2Location = dynamicRepository2.Repository.Info.Path;
dynamicRepository.Repository.Info.Path.ShouldNotBe(dynamicRepository2.Repository.Info.Path);
}
}
}
finally
{
if (expectedDynamicRepoLocation != null)
{
DeleteHelper.DeleteDirectory(expectedDynamicRepoLocation, true);
}

if (expectedDynamicRepo2Location != null)
{
DeleteHelper.DeleteGitRepository(expectedDynamicRepo2Location);
}
}
}

[Test]
public void ThrowsExceptionWhenNotEnoughInfo()
{
var tempDir = Path.GetTempPath();

var repositoryInfo = new RepositoryInfo
{
Url = tempDir,
Branch = "master"
Url = tempDir
};

Should.Throw<Exception>(() => GitRepositoryFactory.CreateRepository(repositoryInfo));
Should.Throw<Exception>(() => DynamicRepositories.CreateOrOpen(repositoryInfo, tempDir, null, null));
}

[Test]
Expand All @@ -192,21 +225,19 @@ public void UsingDynamicRepositoryWithFeatureBranchWorks()
{
using (var mainRepositoryFixture = new EmptyRepositoryFixture())
{
mainRepositoryFixture.Repository.MakeACommit();
var commit = mainRepositoryFixture.Repository.MakeACommit();

var repositoryInfo = new RepositoryInfo
{
Url = mainRepositoryFixture.RepositoryPath,
Branch = "feature1"
Url = mainRepositoryFixture.RepositoryPath
};

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

Should.NotThrow(() =>
{
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
using (DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "feature1", commit.Sha))
{
// this code shouldn't throw
}
});
}
Expand All @@ -220,35 +251,46 @@ public void UsingDynamicRepositoryWithFeatureBranchWorks()
[Test]
public void UsingDynamicRepositoryWithoutTargetBranchFails()
{
var repoName = Guid.NewGuid().ToString();
var tempPath = Path.GetTempPath();
var tempDir = Path.Combine(tempPath, repoName);
Directory.CreateDirectory(tempDir);

try
using (var mainRepositoryFixture = new EmptyRepositoryFixture())
{
using (var mainRepositoryFixture = new EmptyRepositoryFixture())
{
mainRepositoryFixture.Repository.MakeACommit();
mainRepositoryFixture.Repository.MakeACommit();

var repositoryInfo = new RepositoryInfo
{
Url = mainRepositoryFixture.RepositoryPath,
Branch = null
};
var repositoryInfo = new RepositoryInfo
{
Url = mainRepositoryFixture.RepositoryPath
};

Should.Throw<Exception>(() =>
Should.Throw<GitToolsException>(() =>
{
using (DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, null, null))
{
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
{
// this code shouldn't throw
}
});
}
}
});
}
finally
}

[Test]
public void UsingDynamicRepositoryWithoutTargetBranchCommitFails()
{
var tempPath = Path.GetTempPath();

using (var mainRepositoryFixture = new EmptyRepositoryFixture())
{
Directory.Delete(tempDir, true);
mainRepositoryFixture.Repository.MakeACommit();

var repositoryInfo = new RepositoryInfo
{
Url = mainRepositoryFixture.RepositoryPath
};

Should.Throw<GitToolsException>(() =>
{
using (DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", null))
{
}
});
}
}

Expand All @@ -264,15 +306,13 @@ public void TestErrorThrownForInvalidRepository()
{
var repositoryInfo = new RepositoryInfo
{
Url = "http://127.0.0.1/testrepo.git",
Branch = "master"
Url = "http://127.0.0.1/testrepo.git"
};

Should.Throw<Exception>(() =>
{
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
using (DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", "sha"))
{
// this code shouldn't throw
}
});
}
Expand Down
76 changes: 61 additions & 15 deletions src/GitTools.Core.Tests/Git/GitRepositoryHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void NormalisationOfPullRequestsWithFetch()
localFixture.Checkout(commit.Sha);
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: string.Empty);

var normalisedPullBranch = localFixture.Repository.FindBranch("pull/3/merge");
var normalisedPullBranch = localFixture.Repository.Branches["pull/3/merge"];
normalisedPullBranch.ShouldNotBe(null);
}
}
Expand Down Expand Up @@ -51,24 +51,30 @@ public void NormalisationOfPullRequestsWithoutFetch()
}

[Test]
public void UpdatesLocalBranchesWhen()
public void NormalisationOfTag()
{
using (var fixture = new EmptyRepositoryFixture())
{
fixture.Repository.MakeACommit();

fixture.Repository.Checkout(fixture.Repository.CreateBranch("feature/foo"));
fixture.Repository.MakeACommit();

fixture.BranchTo("release/2.0.0");
fixture.MakeACommit();
fixture.MakeATaggedCommit("2.0.0-rc.1");
fixture.Checkout("master");
fixture.MergeNoFF("release/2.0.0");
fixture.Repository.Branches.Remove(fixture.Repository.Branches["release/2.0.0"]);
var remoteTagSha = fixture.Repository.Tags["2.0.0-rc.1"].Target.Sha;

using (var localFixture = fixture.CloneRepository())
{
localFixture.Checkout("feature/foo");
// Advance remote
var advancedCommit = fixture.Repository.MakeACommit();
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: null);
localFixture.Checkout(remoteTagSha);
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: string.Empty);

var normalisedBranch = localFixture.Repository.FindBranch("feature/foo");
normalisedBranch.ShouldNotBe(null);
normalisedBranch.Tip.Sha.ShouldBe(advancedCommit.Sha);
localFixture.Repository.Head.FriendlyName.ShouldBe("(no branch)");
localFixture.Repository.Head.Tip.Sha.ShouldBe(remoteTagSha);
}
}
}
Expand All @@ -89,13 +95,10 @@ public void UpdatesCurrentBranch()
var advancedCommit = fixture.Repository.MakeACommit();
Commands.Fetch((Repository)localFixture.Repository, localFixture.Repository.Network.Remotes["origin"].Name, new string[0], null, null);
localFixture.Repository.Checkout(advancedCommit.Sha);
localFixture.Repository.DumpGraph();
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: "ref/heads/develop");
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: "refs/heads/develop");

var normalisedBranch = localFixture.Repository.FindBranch("develop");
var normalisedBranch = localFixture.Repository.Branches["develop"];
normalisedBranch.ShouldNotBe(null);
fixture.Repository.DumpGraph();
localFixture.Repository.DumpGraph();
normalisedBranch.Tip.Sha.ShouldBe(advancedCommit.Sha);
localFixture.Repository.Head.Tip.Sha.ShouldBe(advancedCommit.Sha);
}
Expand Down Expand Up @@ -125,10 +128,53 @@ public void ShouldNotChangeBranchWhenNormalizingTheDirectory()

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

localFixture.Repository.DumpGraph();
localFixture.Repository.Head.Tip.Sha.ShouldBe(lastCommitOnDevelop.Sha);
}
}
}

[Test]
public void ShouldNotMoveLocalBranchWhenRemoteAdvances()
{
using (var fixture = new EmptyRepositoryFixture())
{
fixture.Repository.MakeACommit();

fixture.Repository.Checkout(fixture.Repository.CreateBranch("feature/foo"));
fixture.Repository.MakeACommit();
using (var localFixture = fixture.CloneRepository())
{
localFixture.Checkout("feature/foo");
var expectedTip = localFixture.Repository.Head.Tip;
// Advance remote
fixture.Repository.MakeACommit();
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: null);

var normalisedBranch = localFixture.Repository.Branches["feature/foo"];
normalisedBranch.ShouldNotBe(null);
normalisedBranch.Tip.Sha.ShouldBe(expectedTip.Sha);
}
}
}

[Test]
public void CheckedOutShaShouldNotChanged()
{
using (var fixture = new EmptyRepositoryFixture())
{
fixture.Repository.MakeACommit();
var commitToBuild = fixture.Repository.MakeACommit();
fixture.Repository.MakeACommit();

using (var localFixture = fixture.CloneRepository())
{
localFixture.Repository.Checkout(commitToBuild);
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: "refs/heads/master");

var normalisedBranch = localFixture.Repository.Branches["master"];
normalisedBranch.Tip.Sha.ShouldBe(commitToBuild.Sha);
}
}
}
}
}
Loading