From e806ed12e81b04c111ea14e62d8f56e1426a9dfa Mon Sep 17 00:00:00 2001 From: "Christopher J. McClellan" Date: Wed, 25 Mar 2015 07:45:00 -0400 Subject: [PATCH] Added Branch class and interface --- RetailCoder.VBE/Interop/Branches.cs | 5 ++- RetailCoder.VBE/Interop/GitProvider.cs | 5 +++ RetailCoder.VBE/Rubberduck.csproj | 1 + RetailCoder.VBE/SourceControl/Branch.cs | 37 +++++++++++++++++++ RetailCoder.VBE/SourceControl/GitProvider.cs | 14 +++---- .../SourceControl/ISourceControlProvider.cs | 4 +- .../SourceControlProviderBase.cs | 4 +- .../UI/SourceControl/BranchesPresenter.cs | 4 +- .../SourceControl/BranchesPresenterTests.cs | 16 ++++++-- 9 files changed, 72 insertions(+), 18 deletions(-) create mode 100644 RetailCoder.VBE/SourceControl/Branch.cs diff --git a/RetailCoder.VBE/Interop/Branches.cs b/RetailCoder.VBE/Interop/Branches.cs index 8b5cc83559..985ec52c1a 100644 --- a/RetailCoder.VBE/Interop/Branches.cs +++ b/RetailCoder.VBE/Interop/Branches.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Runtime.InteropServices; using System.ComponentModel; +using Rubberduck.SourceControl; namespace Rubberduck.Interop { @@ -17,9 +18,9 @@ namespace Rubberduck.Interop public class Branches : IEnumerable { private IEnumerable branches; - internal Branches(IEnumerable branches) + internal Branches(IEnumerable branches) { - this.branches = branches; + this.branches = branches.Select(b => b.Name); } [DispId(-4)] diff --git a/RetailCoder.VBE/Interop/GitProvider.cs b/RetailCoder.VBE/Interop/GitProvider.cs index 3fedd23ca6..d63872b173 100644 --- a/RetailCoder.VBE/Interop/GitProvider.cs +++ b/RetailCoder.VBE/Interop/GitProvider.cs @@ -23,6 +23,11 @@ public GitProvider(VBProject project, IRepository repository) public GitProvider(VBProject project, IRepository repository, string userName, string passWord) : base(project, repository, userName, passWord){} + public new string CurrentBranch + { + get { return base.CurrentBranch.FriendlyName; } + } + public new IEnumerable Branches { get { return new Branches(base.Branches); } diff --git a/RetailCoder.VBE/Rubberduck.csproj b/RetailCoder.VBE/Rubberduck.csproj index a58b388757..99cdd37647 100644 --- a/RetailCoder.VBE/Rubberduck.csproj +++ b/RetailCoder.VBE/Rubberduck.csproj @@ -265,6 +265,7 @@ + diff --git a/RetailCoder.VBE/SourceControl/Branch.cs b/RetailCoder.VBE/SourceControl/Branch.cs new file mode 100644 index 0000000000..e4bd6f4cd0 --- /dev/null +++ b/RetailCoder.VBE/SourceControl/Branch.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Rubberduck.SourceControl +{ + //todo: expose to com + public interface IBranch + { + string Name { get; } + string FriendlyName { get; } + bool IsRemote { get; } + bool IsCurrentHead { get; } + } + + public class Branch : IBranch + { + public string Name { get; private set; } + public string FriendlyName { get; private set; } + public bool IsRemote { get; private set; } + public bool IsCurrentHead { get; private set; } + + public Branch(LibGit2Sharp.Branch branch) + :this(branch.CanonicalName, branch.Name, branch.IsRemote, branch.IsCurrentRepositoryHead) + { } + + public Branch(string name, string friendlyName, bool isRemote, bool isCurrentHead) + { + this.Name = name; + this.FriendlyName = friendlyName; + this.IsRemote = isRemote; + this.IsCurrentHead = isCurrentHead; + } + } +} diff --git a/RetailCoder.VBE/SourceControl/GitProvider.cs b/RetailCoder.VBE/SourceControl/GitProvider.cs index f68cc3a10a..2682ac7259 100644 --- a/RetailCoder.VBE/SourceControl/GitProvider.cs +++ b/RetailCoder.VBE/SourceControl/GitProvider.cs @@ -43,20 +43,20 @@ public GitProvider(VBProject project, IRepository repository, string userName, s } } - public override string CurrentBranch + public override IBranch CurrentBranch { get { - return _repo.Branches.First(b => !b.IsRemote && b.IsCurrentRepositoryHead).Name; + return this.Branches.First(b => !b.IsRemote && b.IsCurrentHead); } } - public override IEnumerable Branches + public override IEnumerable Branches { get { - return _repo.Branches.Where(b => !b.IsRemote) - .Select(b => b.Name); + //note: consider doing this once and refreshing if necessary + return _repo.Branches.Select(b => new Branch(b)); } } @@ -111,7 +111,7 @@ public override void Push() }; } - var branch = _repo.Branches[this.CurrentBranch]; + var branch = _repo.Branches[this.CurrentBranch.Name]; _repo.Network.Push(branch, options); } catch (LibGit2SharpException ex) @@ -317,7 +317,7 @@ public override void Undo(string filePath) { try { - _repo.CheckoutPaths(this.CurrentBranch, new List {filePath}); + _repo.CheckoutPaths(this.CurrentBranch.Name, new List {filePath}); base.Undo(filePath); } catch (LibGit2SharpException ex) diff --git a/RetailCoder.VBE/SourceControl/ISourceControlProvider.cs b/RetailCoder.VBE/SourceControl/ISourceControlProvider.cs index d8e476d391..61b44fff33 100644 --- a/RetailCoder.VBE/SourceControl/ISourceControlProvider.cs +++ b/RetailCoder.VBE/SourceControl/ISourceControlProvider.cs @@ -10,8 +10,8 @@ namespace Rubberduck.SourceControl public interface ISourceControlProvider { IRepository CurrentRepository { get; } - string CurrentBranch { get; } - IEnumerable Branches { get; } + IBranch CurrentBranch { get; } + IEnumerable Branches { get; } /// Clone a remote repository. /// Either a Url "https://github.com/retailcoder/Rubberduck.git" or a UNC path. "//server/share/path/to/repo.git" diff --git a/RetailCoder.VBE/SourceControl/SourceControlProviderBase.cs b/RetailCoder.VBE/SourceControl/SourceControlProviderBase.cs index 953c215f11..b69c2a1026 100644 --- a/RetailCoder.VBE/SourceControl/SourceControlProviderBase.cs +++ b/RetailCoder.VBE/SourceControl/SourceControlProviderBase.cs @@ -24,8 +24,8 @@ protected SourceControlProviderBase(VBProject project, IRepository repository) } public IRepository CurrentRepository { get; private set; } - public abstract string CurrentBranch { get; } - public abstract IEnumerable Branches { get; } + public abstract IBranch CurrentBranch { get; } + public abstract IEnumerable Branches { get; } public abstract IRepository Clone(string remotePathOrUrl, string workingDirectory); public abstract void Push(); public abstract void Fetch(string remoteName); diff --git a/RetailCoder.VBE/UI/SourceControl/BranchesPresenter.cs b/RetailCoder.VBE/UI/SourceControl/BranchesPresenter.cs index 43787de5d2..6588989751 100644 --- a/RetailCoder.VBE/UI/SourceControl/BranchesPresenter.cs +++ b/RetailCoder.VBE/UI/SourceControl/BranchesPresenter.cs @@ -22,8 +22,8 @@ public BranchesPresenter(ISourceControlProvider provider, IBranchesView view) public void RefreshView() { - _view.Branches = _provider.Branches.ToList(); - _view.CurrentBranch = _provider.CurrentBranch; + _view.Branches = _provider.Branches.Select(b => b.FriendlyName).ToList(); + _view.CurrentBranch = _provider.CurrentBranch.FriendlyName; } } } diff --git a/RubberduckTests/SourceControl/BranchesPresenterTests.cs b/RubberduckTests/SourceControl/BranchesPresenterTests.cs index 8752b89201..3732e49f9d 100644 --- a/RubberduckTests/SourceControl/BranchesPresenterTests.cs +++ b/RubberduckTests/SourceControl/BranchesPresenterTests.cs @@ -13,13 +13,23 @@ public class BranchesPresenterTests [TestMethod] public void SelectedBranchShouldBeCurrentBranchAfterRefresh() { + //arrange var _provider = new Mock(); var _view = new Mock(); - var branches = new List() { "master", "dev" }; + var expectedBranch = new Branch("dev", "dev", false, false); + + var branches = new List() + { + new Branch("master", "master", false, true), + expectedBranch, + new Branch("origin/master", "master", true, true), + new Branch("origin/dev", "dev", true, false) + }; + _provider.SetupGet(git => git.Branches).Returns(branches); - _provider.SetupGet(git => git.CurrentBranch).Returns("dev"); + _provider.SetupGet(git => git.CurrentBranch).Returns(expectedBranch); _view.SetupProperty(v => v.CurrentBranch); @@ -27,7 +37,7 @@ public void SelectedBranchShouldBeCurrentBranchAfterRefresh() var presenter = new BranchesPresenter(_provider.Object, _view.Object); //assert - Assert.AreEqual(_provider.Object.CurrentBranch, _view.Object.CurrentBranch); + Assert.AreEqual(_provider.Object.CurrentBranch.Name, _view.Object.CurrentBranch); } }