Skip to content

Fix tracking branches in dynamic repositories #464

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 2 commits into from
Jun 20, 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
7 changes: 6 additions & 1 deletion GitVersionCore/BuildServers/GitHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ static void CreateMissingLocalBranchesFromRemoteTrackingOnes(Repository repo, st

foreach (var remoteTrackingReference in repo.Refs.FromGlob(prefix + "*").Where(r => r.CanonicalName != remoteHeadCanonicalName))
{
var localCanonicalName = "refs/heads/" + remoteTrackingReference.CanonicalName.Substring(prefix.Length);
var remoteTrackingReferenceName = remoteTrackingReference.CanonicalName;
var branchName = remoteTrackingReferenceName.Substring(prefix.Length);
var localCanonicalName = "refs/heads/" + branchName;

if (repo.Refs.Any(x => x.CanonicalName == localCanonicalName))
{
Expand All @@ -203,6 +205,9 @@ static void CreateMissingLocalBranchesFromRemoteTrackingOnes(Repository repo, st
{
repo.Refs.Add(localCanonicalName, new ObjectId(symbolicReference.ResolveToDirectReference().TargetIdentifier), true);
}

var branch = repo.Branches[branchName];
repo.Branches.Update(branch, b => b.TrackedBranch = remoteTrackingReferenceName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would GV be run on a development environment, this would blindly overwrite the potentially existing tracking configuration for every considered branch. Is this expected?

/cc @JakeGinnivan

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these statements are only executed for newly created branches, otherwise they are skipped due to the if statement in line 192.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, this normalisation code should only ever happen on the build server.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even without that if statement which was highlighted. Are there any other scenarios this could happen?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(other than normalisation)

}
}

Expand Down
36 changes: 36 additions & 0 deletions GitVersionExe.Tests/GitPreparerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,41 @@ public void UsesGitVersionConfigWhenCreatingDynamicRepository()
}
}

[Test]
public void UsingDynamicRepositoryWithFeatureBranchWorks()
{
var repoName = Guid.NewGuid().ToString();
var tempPath = Path.GetTempPath();
var tempDir = Path.Combine(tempPath, repoName);
Directory.CreateDirectory(tempDir);

try
{
using (var mainRepositoryFixture = new EmptyRepositoryFixture(new Config()))
{
var commitId = mainRepositoryFixture.Repository.MakeACommit().Id.Sha;

var arguments = new Arguments
{
TargetPath = tempDir,
TargetUrl = mainRepositoryFixture.RepositoryPath,
TargetBranch = "feature1",
CommitId = commitId
};

var gitPreparer = new GitPreparer(arguments.TargetUrl, arguments.DynamicRepositoryLocation, arguments.Authentication, arguments.TargetBranch, arguments.NoFetch, arguments.TargetPath);
gitPreparer.Initialise(true);

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

Assert.DoesNotThrow(() => gitPreparer.Initialise(true));
}
}
finally
{
Directory.Delete(tempDir, true);
}
}

// TODO test around normalisation
}