Skip to content

Commit

Permalink
Added Branch class and interface
Browse files Browse the repository at this point in the history
  • Loading branch information
rubberduck203 committed Mar 25, 2015
1 parent 170800b commit e806ed1
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 18 deletions.
5 changes: 3 additions & 2 deletions RetailCoder.VBE/Interop/Branches.cs
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.ComponentModel;
using Rubberduck.SourceControl;

namespace Rubberduck.Interop
{
Expand All @@ -17,9 +18,9 @@ namespace Rubberduck.Interop
public class Branches : IEnumerable
{
private IEnumerable<string> branches;
internal Branches(IEnumerable<string> branches)
internal Branches(IEnumerable<IBranch> branches)
{
this.branches = branches;
this.branches = branches.Select(b => b.Name);
}

[DispId(-4)]
Expand Down
5 changes: 5 additions & 0 deletions RetailCoder.VBE/Interop/GitProvider.cs
Expand Up @@ -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); }
Expand Down
1 change: 1 addition & 0 deletions RetailCoder.VBE/Rubberduck.csproj
Expand Up @@ -265,6 +265,7 @@
<Compile Include="Interop\SourceControlClassFactory.cs" />
<Compile Include="SourceControl\FileStatus.cs" />
<Compile Include="SourceControl\FileStatusEntry.cs" />
<Compile Include="SourceControl\Branch.cs" />
<Compile Include="SourceControl\IRepository.cs" />
<Compile Include="SourceControl\Repository.cs" />
<Compile Include="UI\CodeExplorer\TreeNodeNavigateCodeEventArgs.cs" />
Expand Down
37 changes: 37 additions & 0 deletions 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;
}
}
}
14 changes: 7 additions & 7 deletions RetailCoder.VBE/SourceControl/GitProvider.cs
Expand Up @@ -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<string> Branches
public override IEnumerable<IBranch> 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));
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -317,7 +317,7 @@ public override void Undo(string filePath)
{
try
{
_repo.CheckoutPaths(this.CurrentBranch, new List<string> {filePath});
_repo.CheckoutPaths(this.CurrentBranch.Name, new List<string> {filePath});
base.Undo(filePath);
}
catch (LibGit2SharpException ex)
Expand Down
4 changes: 2 additions & 2 deletions RetailCoder.VBE/SourceControl/ISourceControlProvider.cs
Expand Up @@ -10,8 +10,8 @@ namespace Rubberduck.SourceControl
public interface ISourceControlProvider
{
IRepository CurrentRepository { get; }
string CurrentBranch { get; }
IEnumerable<string> Branches { get; }
IBranch CurrentBranch { get; }
IEnumerable<IBranch> Branches { get; }

/// <summary>Clone a remote repository.</summary>
/// <param name="remotePathOrUrl">Either a Url "https://github.com/retailcoder/Rubberduck.git" or a UNC path. "//server/share/path/to/repo.git"</param>
Expand Down
4 changes: 2 additions & 2 deletions RetailCoder.VBE/SourceControl/SourceControlProviderBase.cs
Expand Up @@ -24,8 +24,8 @@ protected SourceControlProviderBase(VBProject project, IRepository repository)
}

public IRepository CurrentRepository { get; private set; }
public abstract string CurrentBranch { get; }
public abstract IEnumerable<string> Branches { get; }
public abstract IBranch CurrentBranch { get; }
public abstract IEnumerable<IBranch> Branches { get; }
public abstract IRepository Clone(string remotePathOrUrl, string workingDirectory);
public abstract void Push();
public abstract void Fetch(string remoteName);
Expand Down
4 changes: 2 additions & 2 deletions RetailCoder.VBE/UI/SourceControl/BranchesPresenter.cs
Expand Up @@ -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;
}
}
}
16 changes: 13 additions & 3 deletions RubberduckTests/SourceControl/BranchesPresenterTests.cs
Expand Up @@ -13,21 +13,31 @@ public class BranchesPresenterTests
[TestMethod]
public void SelectedBranchShouldBeCurrentBranchAfterRefresh()
{

//arrange
var _provider = new Mock<ISourceControlProvider>();
var _view = new Mock<IBranchesView>();

var branches = new List<string>() { "master", "dev" };
var expectedBranch = new Branch("dev", "dev", false, false);

var branches = new List<IBranch>()
{
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);

//act
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);
}

}
Expand Down

0 comments on commit e806ed1

Please sign in to comment.