Skip to content

Removed normalisation related code from dynamic repositories. It uses… #600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2015
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 @@ -24,7 +24,7 @@ public RemoteRepositoryFixture(Config configuration)
/// </summary>
public void InitialiseRepo()
{
new GitPreparer(null, null, new Authentication(), null, false, LocalRepositoryFixture.RepositoryPath).Initialise(true, null);
new GitPreparer(null, null, new Authentication(), false, LocalRepositoryFixture.RepositoryPath).Initialise(true, null);
}

static IRepository CreateNewRepository(string path)
Expand Down
35 changes: 17 additions & 18 deletions src/GitVersionCore.Tests/GitPreparerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class GitPreparerTests
const string SpecificBranchName = "feature/foo";

[Test]
[TestCase(null, DefaultBranchName)]
[TestCase(DefaultBranchName, DefaultBranchName)]
[TestCase(SpecificBranchName, SpecificBranchName)]
public void WorksCorrectlyWithRemoteRepository(string branchName, string expectedBranchName)
{
Expand All @@ -37,8 +37,8 @@ public void WorksCorrectlyWithRemoteRepository(string branchName, string expecte
// Copy contents into working directory
File.Copy(Path.Combine(fixture.RepositoryPath, "TestFile.txt"), Path.Combine(tempDir, "TestFile.txt"));

var gitPreparer = new GitPreparer(fixture.RepositoryPath, null, new Authentication(), branchName, false, tempDir);
gitPreparer.Initialise(false, null);
var gitPreparer = new GitPreparer(fixture.RepositoryPath, null, new Authentication(), false, tempDir);
gitPreparer.Initialise(false, branchName);
dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();

gitPreparer.IsDynamicGitRepository.ShouldBe(true);
Expand All @@ -48,7 +48,7 @@ public void WorksCorrectlyWithRemoteRepository(string branchName, string expecte
{
var currentBranch = repository.Head.CanonicalName;

currentBranch.EndsWith(expectedBranchName).ShouldBe(true);
currentBranch.ShouldEndWith(expectedBranchName);
}
}
}
Expand All @@ -75,12 +75,12 @@ public void UpdatesExistingDynamicRepository()
{
mainRepositoryFixture.Repository.MakeCommits(1);

var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new Authentication(), "master", false, tempDir);
gitPreparer.Initialise(false, null);
var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new Authentication(), false, tempDir);
gitPreparer.Initialise(false, "master");
dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();

var newCommit = mainRepositoryFixture.Repository.MakeACommit();
gitPreparer.Initialise(false, null);
gitPreparer.Initialise(false, "master");

using (var repository = new Repository(dynamicRepositoryPath))
{
Expand Down Expand Up @@ -116,8 +116,8 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
expectedDynamicRepoLocation = Path.Combine(tempPath, fixture.RepositoryPath.Split('\\').Last());
Directory.CreateDirectory(expectedDynamicRepoLocation);

var gitPreparer = new GitPreparer(fixture.RepositoryPath, null, new Authentication(), null, false, tempDir);
gitPreparer.Initialise(false, null);
var gitPreparer = new GitPreparer(fixture.RepositoryPath, null, new Authentication(), false, tempDir);
gitPreparer.Initialise(false, "master");

gitPreparer.IsDynamicGitRepository.ShouldBe(true);
gitPreparer.DynamicGitRepositoryPath.ShouldBe(expectedDynamicRepoLocation + "_1\\.git");
Expand All @@ -137,7 +137,7 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
public void WorksCorrectlyWithLocalRepository()
{
var tempDir = Path.GetTempPath();
var gitPreparer = new GitPreparer(null, null, null, null, false, tempDir);
var gitPreparer = new GitPreparer(null, null, null, false, tempDir);
var dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();

dynamicRepositoryPath.ShouldBe(null);
Expand All @@ -158,12 +158,12 @@ public void UsingDynamicRepositoryWithFeatureBranchWorks()
{
mainRepositoryFixture.Repository.MakeACommit();

var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new Authentication(), "feature1", false, tempDir);
gitPreparer.Initialise(true, null);
var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new Authentication(), false, tempDir);
gitPreparer.Initialise(true, "feature1");

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

Should.NotThrow(() => gitPreparer.Initialise(true, null));
Should.NotThrow(() => gitPreparer.Initialise(true, "feature1"));
}
}
finally
Expand All @@ -186,8 +186,7 @@ public void UsingDynamicRepositoryWithoutTargetBranchFails()
{
mainRepositoryFixture.Repository.MakeACommit();

var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new Authentication(), null, false, tempDir);
gitPreparer.Initialise(true, null);
var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new Authentication(), false, tempDir);

Should.Throw<Exception>(() => gitPreparer.Initialise(true, null));
}
Expand All @@ -208,9 +207,9 @@ public void TestErrorThrownForInvalidRepository()

try
{
var gitPreparer = new GitPreparer("http://127.0.0.1/testrepo.git", null, new Authentication(), null, false, tempDir);
var gitPreparer = new GitPreparer("http://127.0.0.1/testrepo.git", null, new Authentication(), false, tempDir);

Should.Throw<Exception>(() => gitPreparer.Initialise(true, null));
Should.Throw<Exception>(() => gitPreparer.Initialise(true, "master"));
}
finally
{
Expand Down
8 changes: 6 additions & 2 deletions src/GitVersionCore/BuildServers/GitHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut
CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(repo, remote.Name);

var headSha = repo.Refs.Head.TargetIdentifier;

if (!repo.Info.IsHeadDetached)
{
Logger.WriteInfo(string.Format("HEAD points at branch '{0}'.", headSha));
Expand Down Expand Up @@ -102,8 +102,10 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut
static void EnsureLocalBranchExistsForCurrentBranch(Repository repo, string currentBranch)
{
if (string.IsNullOrEmpty(currentBranch)) return;

var isRef = currentBranch.Contains("refs");
var isBranch = currentBranch.Contains("refs/heads");
var localCanonicalName = isBranch ? currentBranch : currentBranch.Replace("refs/", "refs/heads/");
var localCanonicalName = !isRef ? "refs/heads/" + currentBranch : isBranch ? currentBranch : currentBranch.Replace("refs/", "refs/heads/");
var repoTip = repo.Head.Tip;
var repoTipId = repoTip.Id;

Expand All @@ -121,6 +123,8 @@ static void EnsureLocalBranchExistsForCurrentBranch(Repository repo, string curr
string.Format("Updating local branch {0} to match ref {1}", localCanonicalName, currentBranch));
repo.Refs.UpdateTarget(repo.Refs[localCanonicalName], repoTipId);
}

repo.Checkout(localCanonicalName);
}

public static bool LooksLikeAValidPullRequestNumber(string issueNumber)
Expand Down
4 changes: 2 additions & 2 deletions src/GitVersionCore/ExecuteCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ public static class ExecuteCore
public static VersionVariables ExecuteGitVersion(IFileSystem fileSystem, string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId)
{
// Normalise if we are running on build server
var gitPreparer = new GitPreparer(targetUrl, dynamicRepositoryLocation, authentication, targetBranch, noFetch, workingDirectory);
var gitPreparer = new GitPreparer(targetUrl, dynamicRepositoryLocation, authentication, noFetch, workingDirectory);
var applicableBuildServers = BuildServerList.GetApplicableBuildServers();
var buildServer = applicableBuildServers.FirstOrDefault();
var currentBranch = buildServer == null ? null : buildServer.GetCurrentBranch();
if (!string.IsNullOrEmpty(currentBranch))
{
Logger.WriteInfo("Branch from build environment: " + currentBranch);
}
gitPreparer.Initialise(buildServer != null, currentBranch);
gitPreparer.Initialise(buildServer != null, currentBranch ?? targetBranch);
var dotGitDirectory = gitPreparer.GetDotGitDirectory();
var projectRoot = gitPreparer.GetProjectRootDirectory();
Logger.WriteInfo(string.Format("Project root is: " + projectRoot));
Expand Down
104 changes: 17 additions & 87 deletions src/GitVersionCore/GitPreparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ public class GitPreparer
string targetUrl;
string dynamicRepositoryLocation;
Authentication authentication;
string targetBranch;
bool noFetch;
string targetPath;

public GitPreparer(string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string targetPath)
public GitPreparer(string targetUrl, string dynamicRepositoryLocation, Authentication authentication, bool noFetch, string targetPath)
{
this.targetUrl = targetUrl;
this.dynamicRepositoryLocation = dynamicRepositoryLocation;
this.authentication = authentication;
this.targetBranch = targetBranch;
this.noFetch = noFetch;
this.targetPath = targetPath;
}
Expand All @@ -44,11 +42,7 @@ public void Initialise(bool normaliseGitDirectory, string currentBranch)

var tempRepositoryPath = CalculateTemporaryRepositoryPath(targetUrl, dynamicRepositoryLocation);

DynamicGitRepositoryPath = CreateDynamicRepository(tempRepositoryPath, authentication, targetUrl, targetBranch, noFetch);
if (normaliseGitDirectory)
{
GitHelper.NormalizeGitDirectory(GetDotGitDirectory(), authentication, noFetch, currentBranch);
}
DynamicGitRepositoryPath = CreateDynamicRepository(tempRepositoryPath, authentication, targetUrl, currentBranch, noFetch);
}

static string CalculateTemporaryRepositoryPath(string targetUrl, string dynamicRepositoryLocation)
Expand Down Expand Up @@ -110,32 +104,31 @@ public string GetProjectRootDirectory()

static string CreateDynamicRepository(string targetPath, Authentication authentication, string repositoryUrl, string targetBranch, bool noFetch)
{
if (string.IsNullOrWhiteSpace(targetBranch))
{
throw new Exception("Dynamic Git repositories must have a target branch (/b)");
}
Logger.WriteInfo(string.Format("Creating dynamic repository at '{0}'", targetPath));

var gitDirectory = Path.Combine(targetPath, ".git");
if (Directory.Exists(targetPath))
{
Logger.WriteInfo("Git repository already exists");
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, null);
Logger.WriteInfo(string.Format("Updating branch '{0}'", targetBranch));
using (var repo = new Repository(targetPath))
{
if (string.IsNullOrWhiteSpace(targetBranch))
{
throw new Exception("Dynamic Git repositories must have a target branch (/b)");
}
var targetGitBranch = repo.Branches[targetBranch];
var trackedBranch = targetGitBranch.TrackedBranch;
if (trackedBranch == null)
throw new InvalidOperationException(string.Format("Expecting {0} to have a remote tracking branch", targetBranch));

targetGitBranch.Checkout();
repo.Reset(ResetMode.Hard, trackedBranch.Tip);
}
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch);

return gitDirectory;
}

CloneRepository(repositoryUrl, gitDirectory, authentication);

// Normalize (download branches) before using the branch
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch);

return gitDirectory;
}

private static void CloneRepository(string repositoryUrl, string gitDirectory, Authentication authentication)
{
Credentials credentials = null;
if (!string.IsNullOrWhiteSpace(authentication.Username) && !string.IsNullOrWhiteSpace(authentication.Password))
{
Expand All @@ -150,53 +143,6 @@ static string CreateDynamicRepository(string targetPath, Authentication authenti

Logger.WriteInfo(string.Format("Retrieving git info from url '{0}'", repositoryUrl));

CloneRepository(repositoryUrl, gitDirectory, credentials);

// Normalize (download branches) before using the branch
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, null);

using (var repository = new Repository(gitDirectory))
{
if (string.IsNullOrWhiteSpace(targetBranch))
{
targetBranch = repository.Head.Name;
}

Reference newHead = null;

var localReference = GetLocalReference(repository, targetBranch);
if (localReference != null)
{
newHead = localReference;
}

if (newHead == null)
{
var remoteReference = GetRemoteReference(repository, targetBranch, repositoryUrl, authentication);
if (remoteReference != null)
{
repository.Network.Fetch(repositoryUrl, new[]
{
string.Format("{0}:{1}", remoteReference.CanonicalName, targetBranch)
});

newHead = repository.Refs[string.Format("refs/heads/{0}", targetBranch)];
}
}

if (newHead != null)
{
Logger.WriteInfo(string.Format("Switching to branch '{0}'", targetBranch));

repository.Refs.UpdateTarget(repository.Refs.Head, newHead);
}
}

return gitDirectory;
}

private static void CloneRepository(string repositoryUrl, string gitDirectory, Credentials credentials)
{
try
{
Repository.Clone(repositoryUrl, gitDirectory,
Expand All @@ -223,23 +169,7 @@ private static void CloneRepository(string repositoryUrl, string gitDirectory, C
}

throw new Exception("There was an unknown problem with the Git repository you provided");

}
}

private static Reference GetLocalReference(Repository repository, string branchName)
{
var targetBranchName = branchName.GetCanonicalBranchName();

return repository.Refs.FirstOrDefault(localRef => string.Equals(localRef.CanonicalName, targetBranchName));
}

private static DirectReference GetRemoteReference(Repository repository, string branchName, string repositoryUrl, Authentication authentication)
{
var targetBranchName = branchName.GetCanonicalBranchName();

var remoteReferences = GitHelper.GetRemoteTipsUsingUsernamePasswordCredentials(repository, repositoryUrl, authentication.Username, authentication.Password);
return remoteReferences.FirstOrDefault(remoteRef => string.Equals(remoteRef.CanonicalName, targetBranchName));
}
}
}
2 changes: 1 addition & 1 deletion src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void UsesGitVersionConfigWhenCreatingDynamicRepository()
var configFile = Path.Combine(localRepoPath, "GitVersionConfig.yaml");
File.WriteAllText(configFile, "next-version: 1.0.0");

var arguments = string.Format(" /url {0} /dynamicRepoLocation {1}", remote.RepositoryPath, repoBasePath);
var arguments = string.Format(" /url {0} /dynamicRepoLocation {1} /b master", remote.RepositoryPath, repoBasePath);
var results = GitVersionHelper.ExecuteIn(localRepoPath, arguments, false);
results.OutputVariables.SemVer.ShouldBe("1.0.0");
}
Expand Down