Skip to content

Commit

Permalink
reformating with resharper layout settings
Browse files Browse the repository at this point in the history
  • Loading branch information
tclem committed Apr 23, 2011
1 parent 37592c4 commit 84e9e65
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 107 deletions.
116 changes: 58 additions & 58 deletions LibGit2Sharp.Tests/BranchFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void CanCreateBranch()
newBranch.Tip.Sha.ShouldEqual("be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
repo.Branches.SingleOrDefault(p => p.Name == name).ShouldNotBeNull();

repo.Refs.Delete(newBranch.CanonicalName); //TODO: To be replaced with repo.Branches.Delete(newBranch.Name)
repo.Refs.Delete(newBranch.CanonicalName); //TODO: To be replaced with repo.Branches.Delete(newBranch.Name)
}
}

Expand All @@ -46,63 +46,39 @@ public void CanCreateBranchFromAnotherBranch()
newBranch.Tip.Sha.ShouldEqual("4c062a6361ae6959e06292c1fa5e2822d9c96345");
repo.Branches.SingleOrDefault(p => p.Name == name).ShouldNotBeNull();

repo.Refs.Delete(newBranch.CanonicalName); //TODO: To be replaced with repo.Branches.Delete(newBranch.Name)
repo.Refs.Delete(newBranch.CanonicalName); //TODO: To be replaced with repo.Branches.Delete(newBranch.Name)
}
}

[Test]
public void TwoBranchesPointingAtTheSameCommitAreNotBothCurrent()
{
using (var path = new TemporaryCloneOfTestRepo())
using (var repo = new Repository(path.RepositoryPath))
{
var master = repo.Branches["refs/heads/master"];

var newBranch = repo.Branches.Create("clone-of-master", master.Tip.Sha);
newBranch.IsCurrentRepositoryHead.ShouldBeFalse();
}
}

[Test]
public void OnlyOneBranchIsTheHead()
public void CanListAllBranches()
{
using (var repo = new Repository(Constants.TestRepoPath))
{
Branch head = null;

foreach (var branch in repo.Branches)
foreach (var r in repo.Branches)
{
bool isHead = branch.IsCurrentRepositoryHead;

if (!isHead)
{
continue;
}

if (head == null)
{
head = branch;
continue;
}

Assert.Fail("Both '{0}' and '{1}' appear to be Head.", head.CanonicalName, branch.CanonicalName);
Assert.Contains(r.Name, expectedBranches);
}

head.ShouldNotBeNull();
repo.Branches.Count().ShouldEqual(5);
}
}

[Test]
public void CanListAllBranches()
public void CanLookupABranchByItsCanonicalName()
{
using (var repo = new Repository(Constants.TestRepoPath))
{
foreach (var r in repo.Branches)
{
Assert.Contains(r.Name, expectedBranches);
}
var branch = repo.Branches["refs/heads/br2"];
branch.ShouldNotBeNull();
branch.Name.ShouldEqual("br2");

repo.Branches.Count().ShouldEqual(5);
var branch2 = repo.Branches["refs/heads/br2"];
branch2.ShouldNotBeNull();
branch2.Name.ShouldEqual("br2");

branch2.ShouldEqual(branch);
(branch2 == branch).ShouldBeTrue();
}
}

Expand All @@ -121,24 +97,6 @@ public void CanLookupLocalBranch()
}
}

[Test]
public void CanLookupABranchByItsCanonicalName()
{
using (var repo = new Repository(Constants.TestRepoPath))
{
var branch = repo.Branches["refs/heads/br2"];
branch.ShouldNotBeNull();
branch.Name.ShouldEqual("br2");

var branch2 = repo.Branches["refs/heads/br2"];
branch2.ShouldNotBeNull();
branch2.Name.ShouldEqual("br2");

branch2.ShouldEqual(branch);
(branch2 == branch).ShouldBeTrue();
}
}

[Test]
public void CanWalkCommitsFromAnotherBranch()
{
Expand Down Expand Up @@ -195,5 +153,47 @@ public void CreatingBranchWithNullTargetThrows()
Assert.Throws<ArgumentNullException>(() => repo.Branches.Create("bad_branch", (ObjectId) null));
}
}

[Test]
public void OnlyOneBranchIsTheHead()
{
using (var repo = new Repository(Constants.TestRepoPath))
{
Branch head = null;

foreach (var branch in repo.Branches)
{
bool isHead = branch.IsCurrentRepositoryHead;

if (!isHead)
{
continue;
}

if (head == null)
{
head = branch;
continue;
}

Assert.Fail("Both '{0}' and '{1}' appear to be Head.", head.CanonicalName, branch.CanonicalName);
}

head.ShouldNotBeNull();
}
}

[Test]
public void TwoBranchesPointingAtTheSameCommitAreNotBothCurrent()
{
using (var path = new TemporaryCloneOfTestRepo())
using (var repo = new Repository(path.RepositoryPath))
{
var master = repo.Branches["refs/heads/master"];

var newBranch = repo.Branches.Create("clone-of-master", master.Tip.Sha);
newBranch.IsCurrentRepositoryHead.ShouldBeFalse();
}
}
}
}
100 changes: 55 additions & 45 deletions LibGit2Sharp/Branch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ namespace LibGit2Sharp
/// </summary>
public class Branch : IEquatable<Branch>
{
private readonly Repository repo;

private static readonly LambdaEqualityHelper<Branch> equalityHelper =
new LambdaEqualityHelper<Branch>(new Func<Branch, object>[] { x => x.CanonicalName, x => x.Tip });
new LambdaEqualityHelper<Branch>(new Func<Branch, object>[] {x => x.CanonicalName, x => x.Tip});

private readonly Repository repo;

/// <summary>
/// Initializes a new instance of the <see cref = "Branch" /> class.
/// </summary>
/// <param name="tip">The commit which is pointed at by this Branch</param>
/// <param name = "tip">The commit which is pointed at by this Branch</param>
/// <param name = "repo">The repo.</param>
/// <param name="canonicalName">The full name of the reference</param>
/// <param name = "canonicalName">The full name of the reference</param>
internal Branch(string canonicalName, Commit tip, Repository repo)
{
this.repo = repo;
Expand All @@ -34,21 +34,27 @@ internal Branch(string canonicalName, Commit tip, Repository repo)
/// <summary>
/// Gets the name of this branch.
/// </summary>
public string Name { get { return ShortenName(CanonicalName); } }
public string Name
{
get { return ShortenName(CanonicalName); }
}

/// <summary>
/// Gets a value indicating whether this instance is a remote.
/// Gets a value indicating whether this instance is a remote.
/// </summary>
/// <value>
/// <c>true</c> if this instance is remote; otherwise, <c>false</c>.
/// </value>
public bool IsRemote { get { return IsRemoteBranch(CanonicalName); } }
public bool IsRemote
{
get { return IsRemoteBranch(CanonicalName); }
}

/// <summary>
/// Gets a value indicating whether this instance is current branch (HEAD) in the repository.
/// Gets a value indicating whether this instance is current branch (HEAD) in the repository.
/// </summary>
/// <value>
/// <c>true</c> if this instance is current branch; otherwise, <c>false</c>.
/// <c>true</c> if this instance is current branch; otherwise, <c>false</c>.
/// </value>
public bool IsCurrentRepositoryHead
{
Expand All @@ -68,6 +74,39 @@ public CommitCollection Commits
get { return repo.Commits.StartingAt(this); }
}

#region IEquatable<Branch> Members

/// <summary>
/// Determines whether the specified <see cref = "Branch" /> is equal to the current <see cref = "Branch" />.
/// </summary>
/// <param name = "other">The <see cref = "Branch" /> to compare with the current <see cref = "Branch" />.</param>
/// <returns>True if the specified <see cref = "Branch" /> is equal to the current <see cref = "Branch" />; otherwise, false.</returns>
public bool Equals(Branch other)
{
return equalityHelper.Equals(this, other);
}

#endregion

/// <summary>
/// Determines whether the specified <see cref = "Object" /> is equal to the current <see cref = "Branch" />.
/// </summary>
/// <param name = "obj">The <see cref = "Object" /> to compare with the current <see cref = "Branch" />.</param>
/// <returns>True if the specified <see cref = "Object" /> is equal to the current <see cref = "Branch" />; otherwise, false.</returns>
public override bool Equals(object obj)
{
return Equals(obj as Branch);
}

/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <returns>A 32-bit signed integer hash code.</returns>
public override int GetHashCode()
{
return equalityHelper.GetHashCode(this);
}

private static bool IsRemoteBranch(string canonicalName)
{
return canonicalName.StartsWith("refs/remotes/");
Expand All @@ -89,50 +128,21 @@ private static string ShortenName(string branchName)
}

/// <summary>
/// Determines whether the specified <see cref="Object"/> is equal to the current <see cref="Branch"/>.
/// </summary>
/// <param name="obj">The <see cref="Object"/> to compare with the current <see cref="Branch"/>.</param>
/// <returns>True if the specified <see cref="Object"/> is equal to the current <see cref="Branch"/>; otherwise, false.</returns>
public override bool Equals(object obj)
{
return Equals(obj as Branch);
}

/// <summary>
/// Determines whether the specified <see cref="Branch"/> is equal to the current <see cref="Branch"/>.
/// </summary>
/// <param name="other">The <see cref="Branch"/> to compare with the current <see cref="Branch"/>.</param>
/// <returns>True if the specified <see cref="Branch"/> is equal to the current <see cref="Branch"/>; otherwise, false.</returns>
public bool Equals(Branch other)
{
return equalityHelper.Equals(this, other);
}

/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <returns>A 32-bit signed integer hash code.</returns>
public override int GetHashCode()
{
return equalityHelper.GetHashCode(this);
}

/// <summary>
/// Tests if two <see cref="Branch"/> are equal.
/// Tests if two <see cref = "Branch" /> are equal.
/// </summary>
/// <param name="left">First <see cref="Branch"/> to compare.</param>
/// <param name="right">Second <see cref="Branch"/> to compare.</param>
/// <param name = "left">First <see cref = "Branch" /> to compare.</param>
/// <param name = "right">Second <see cref = "Branch" /> to compare.</param>
/// <returns>True if the two objects are equal; false otherwise.</returns>
public static bool operator ==(Branch left, Branch right)
{
return Equals(left, right);
}

/// <summary>
/// Tests if two <see cref="Branch"/> are different.
/// Tests if two <see cref = "Branch" /> are different.
/// </summary>
/// <param name="left">First <see cref="Branch"/> to compare.</param>
/// <param name="right">Second <see cref="Branch"/> to compare.</param>
/// <param name = "left">First <see cref = "Branch" /> to compare.</param>
/// <param name = "right">Second <see cref = "Branch" /> to compare.</param>
/// <returns>True if the two objects are different; false otherwise.</returns>
public static bool operator !=(Branch left, Branch right)
{
Expand Down
7 changes: 3 additions & 4 deletions LibGit2Sharp/BranchCollection.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using LibGit2Sharp.Core;

Expand Down Expand Up @@ -57,13 +56,13 @@ IEnumerator IEnumerable.GetEnumerator()
public Branch Create(string name, string target)
{
ObjectId id = ObjectId.CreateFromMaybeSha(target);
if(id != null)
if (id != null)
{
return Create(name, id);
}

repo.Refs.Create(NormalizeToCanonicalName(name), NormalizeToCanonicalName(target));

return this[name];
}

Expand All @@ -78,7 +77,7 @@ public Branch Create(string name, ObjectId target)
Ensure.ArgumentNotNull(target, "target");

repo.Refs.Create(NormalizeToCanonicalName(name), target);

return this[name];
}

Expand Down

0 comments on commit 84e9e65

Please sign in to comment.