Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
3 changes: 2 additions & 1 deletion src/GitVersion.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -573,4 +573,5 @@ II.2.12 <HandlesEvent />


<s:String x:Key="/Default/FilterSettingsManager/AttributeFilterXml/@EntryValue">&lt;data /&gt;</s:String>
<s:String x:Key="/Default/FilterSettingsManager/CoverageFilterXml/@EntryValue">&lt;data&gt;&lt;IncludeFilters /&gt;&lt;ExcludeFilters /&gt;&lt;/data&gt;</s:String></wpf:ResourceDictionary>
<s:String x:Key="/Default/FilterSettingsManager/CoverageFilterXml/@EntryValue">&lt;data&gt;&lt;IncludeFilters /&gt;&lt;ExcludeFilters /&gt;&lt;/data&gt;</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Reacheable/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
10 changes: 5 additions & 5 deletions src/GitVersionCore.Tests/Core/GitVersionExecutorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public void CacheFileIsMissing()
var gitVersionOptions = new GitVersionOptions { WorkingDirectory = fixture.RepositoryPath };

fixture.Repository.MakeACommit();
var gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions, log, fixture.Repository);
var gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions, log, new GitRepository(fixture.Repository));

gitVersionCalculator.CalculateVersionVariables();

Expand Down Expand Up @@ -299,7 +299,7 @@ public void ConfigChangeInvalidatesCache()
CommitsSinceVersionSource: 19
CommitsSinceVersionSourcePadded: 0019
CommitDate: 2015-11-10
UncommittedChanges: 0
UncommittedChanges: 0
";

using var fixture = new EmptyRepositoryFixture();
Expand Down Expand Up @@ -483,7 +483,7 @@ public void DynamicRepositoriesShouldNotErrorWithFailedToFindGitDirectory()
}
};

var gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions, repository: fixture.Repository);
var gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions, repository: new GitRepository(fixture.Repository));
gitPreparer.Prepare();
gitVersionCalculator.CalculateVersionVariables();
}
Expand Down Expand Up @@ -566,7 +566,7 @@ public void CalculateVersionFromWorktreeHead()
version.Sha.ShouldBe(commits.First().Sha);
}

private IGitVersionCalculateTool GetGitVersionCalculator(GitVersionOptions gitVersionOptions, ILog logger = null, IRepository repository = null, IFileSystem fs = null)
private IGitVersionCalculateTool GetGitVersionCalculator(GitVersionOptions gitVersionOptions, ILog logger = null, IGitRepository repository = null, IFileSystem fs = null)
{
sp = GetServiceProvider(gitVersionOptions, logger, repository, fs);

Expand All @@ -578,7 +578,7 @@ private IGitVersionCalculateTool GetGitVersionCalculator(GitVersionOptions gitVe
return sp.GetService<IGitVersionCalculateTool>();
}

private static IServiceProvider GetServiceProvider(GitVersionOptions gitVersionOptions, ILog log = null, IRepository repository = null, IFileSystem fileSystem = null, IEnvironment environment = null)
private static IServiceProvider GetServiceProvider(GitVersionOptions gitVersionOptions, ILog log = null, IGitRepository repository = null, IFileSystem fileSystem = null, IEnvironment environment = null)
{
return ConfigureServices(services =>
{
Expand Down
155 changes: 72 additions & 83 deletions src/GitVersionCore.Tests/Core/RepositoryExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GitVersion;
using GitVersion.Extensions;
using GitVersion.Logging;
using GitVersionCore.Tests.Helpers;
using LibGit2Sharp;
using NSubstitute;
using NUnit.Framework;

Expand All @@ -13,29 +13,78 @@ namespace GitVersionCore.Tests
[TestFixture]
public class RepositoryExtensionsTests : TestBase
{
private static void EnsureLocalBranchExistsForCurrentBranch(IGitRepository repo, ILog log, Remote remote, string currentBranch)
{
if (log is null)
{
throw new ArgumentNullException(nameof(log));
}

if (remote is null)
{
throw new ArgumentNullException(nameof(remote));
}

if (string.IsNullOrEmpty(currentBranch)) return;

var isRef = currentBranch.Contains("refs");
var isBranch = currentBranch.Contains("refs/heads");
var localCanonicalName = !isRef
? "refs/heads/" + currentBranch
: isBranch
? currentBranch
: currentBranch.Replace("refs/", "refs/heads/");

var repoTip = repo.Head.Tip;

// We currently have the rep.Head of the *default* branch, now we need to look up the right one
var originCanonicalName = $"{remote.Name}/{currentBranch}";
var originBranch = repo.Branches[originCanonicalName];
if (originBranch != null)
{
repoTip = originBranch.Tip;
}

var repoTipId = repoTip.Id;

if (repo.Branches.All(b => !b.CanonicalName.IsEquivalentTo(localCanonicalName)))
{
log.Info(isBranch ? $"Creating local branch {localCanonicalName}"
: $"Creating local branch {localCanonicalName} pointing at {repoTipId}");
repo.Refs.Add(localCanonicalName, repoTipId);
}
else
{
log.Info(isBranch ? $"Updating local branch {localCanonicalName} to point at {repoTip.Sha}"
: $"Updating local branch {localCanonicalName} to match ref {currentBranch}");
var localRef = repo.Refs[localCanonicalName];
repo.Refs.UpdateTarget(localRef, repoTipId);
}

repo.Checkout(localCanonicalName);
}

[Test]
public void EnsureLocalBranchExistsForCurrentBranch_CaseInsensitivelyMatchesBranches()
{
var log = Substitute.For<ILog>();
var repository = MockRepository();
var remote = MockRemote(repository);

repository.EnsureLocalBranchExistsForCurrentBranch(log, remote, "refs/heads/featurE/feat-test");
EnsureLocalBranchExistsForCurrentBranch(repository, log, remote, "refs/heads/featurE/feat-test");
}

private IGitRepository MockRepository()
private static IGitRepository MockRepository()
{
var repository = Substitute.For<IGitRepository>();
var commands = Substitute.For<IGitRepositoryCommands>();
repository.Commands.Returns(commands);
return repository;
}

private Remote MockRemote(IGitRepository repository)
private static Remote MockRemote(IGitRepository repository)
{
var branches = new TestableBranchCollection(repository);
var branches = new TestableBranchCollection();
var tipId = new ObjectId("c6d8764d20ff16c0df14c73680e52b255b608926");
var tip = new TestableCommit(repository, tipId);
var tip = new TestableCommit(tipId);
var head = branches.Add("refs/heads/feature/feat-test", tip);
var remote = new TesatbleRemote("origin");
var references = new TestableReferenceCollection();
Expand All @@ -49,66 +98,23 @@ private Remote MockRemote(IGitRepository repository)

private class TestableBranchCollection : BranchCollection
{
private readonly IRepository repository;
public TestableBranchCollection(IRepository repository)
{
this.repository = repository;
}

IDictionary<string, Branch> branches = new Dictionary<string, Branch>();

public override Branch this[string name] =>
this.branches.ContainsKey(name)
? this.branches[name]
branches.ContainsKey(name)
? branches[name]
: null;

public override Branch Add(string name, Commit commit)
{
var branch = new TestableBranch(name, commit);
this.branches.Add(name, branch);
branches.Add(name, branch);
return branch;
}

public override Branch Add(string name, string committish)
{
var id = new ObjectId(committish);
var commit = new TestableCommit(this.repository, id);
return Add(name, commit);
}

public override Branch Add(string name, Commit commit, bool allowOverwrite)
{
return Add(name, commit);
}

public override Branch Add(string name, string committish, bool allowOverwrite)
{
return Add(name, committish);
}

public override IEnumerator<Branch> GetEnumerator()
{
return this.branches.Values.GetEnumerator();
}

public override void Remove(string name)
{
this.branches.Remove(name);
}

public override void Remove(string name, bool isRemote)
{
this.branches.Remove(name);
}

public override void Remove(Branch branch)
{
this.branches.Remove(branch.CanonicalName);
}

public override Branch Update(Branch branch, params Action<BranchUpdater>[] actions)
{
return base.Update(branch, actions);
return branches.Values.GetEnumerator();
}
}

Expand All @@ -123,23 +129,20 @@ public TestableBranch(string canonicalName, Commit tip)
this.canonicalName = canonicalName;
}

public override string CanonicalName => this.canonicalName;
public override Commit Tip => this.tip;
public override string CanonicalName => canonicalName;
public override Commit Tip => tip;
}

private class TestableCommit : Commit, IBelongToARepository
private class TestableCommit : Commit
{
private IRepository repository;
private ObjectId id;

public TestableCommit(IRepository repository, ObjectId id)
public TestableCommit(ObjectId id)
{
this.repository = repository;
this.id = id;
}

public override ObjectId Id => this.id;
public IRepository Repository => this.repository;
public override ObjectId Id => id;
}

private class TesatbleRemote : Remote
Expand All @@ -151,46 +154,32 @@ public TesatbleRemote(string name)
this.name = name;
}

public override string Name => this.name;
public override string Name => name;
}

private class TestableReferenceCollection : ReferenceCollection
{
Reference reference;

public override DirectReference Add(string name, ObjectId targetId)
{
throw new InvalidOperationException("Update should be invoked when case-insensitively comparing branches.");
}

public override Reference Add(string name, string canonicalRefNameOrObjectish)
{
return this.reference = new TestableReference(canonicalRefNameOrObjectish);
return reference = new TestableReference(canonicalRefNameOrObjectish);
}

public override Reference UpdateTarget(Reference directRef, ObjectId targetId)
{
return this.reference;
return reference;
}

public override Reference this[string name] => this.reference;
public override Reference this[string name] => reference;
}

private class TestableReference : Reference
{
private readonly string canonicalName;

public TestableReference(string canonicalName)
{
this.canonicalName = canonicalName;
this.CanonicalName = canonicalName;
}

public override string CanonicalName => this.canonicalName;

public override DirectReference ResolveToDirectReference()
{
throw new NotImplementedException();
}
public override string CanonicalName { get; }
}
}
}
Loading