Skip to content

Commit

Permalink
Convert CommitCollection.StartingAt() overloads as extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltoken committed May 19, 2011
1 parent 65de100 commit cf9dbed
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 33 deletions.
61 changes: 28 additions & 33 deletions LibGit2Sharp/CommitCollection.cs
Expand Up @@ -12,15 +12,26 @@ public class CommitCollection : IEnumerable<Commit>
{
private readonly Repository repo;
private string pushedSha;
private GitSortOptions sortOptions = GitSortOptions.Time;
private readonly GitSortOptions sortOptions;

/// <summary>
/// Initializes a new instance of the <see cref = "CommitCollection"/> class.
/// The commits will be enumerated according in reverse chronological order.
/// </summary>
/// <param name = "repo">The repository.</param>
internal CommitCollection(Repository repo) : this (repo, GitSortOptions.Time)
{
}

/// <summary>
/// Initializes a new instance of the <see cref = "CommitCollection" /> class.
/// </summary>
/// <param name = "repo">The repo.</param>
internal CommitCollection(Repository repo)
/// <param name = "repo">The repository.</param>
/// <param name = "sortingStrategy">The sorting strategy which should be applied when enumerating the commits.</param>
internal CommitCollection(Repository repo, GitSortOptions sortingStrategy)
{
this.repo = repo;
sortOptions = sortingStrategy;
}

/// <summary>
Expand All @@ -32,7 +43,7 @@ internal CommitCollection(Repository repo)
}

/// <summary>
/// Gets the Count of commits (This is a fast count that does not hydrate real commit objects)
/// Gets the count of commits (This is a fast count that does not hydrate real commit objects)
/// </summary>
public int Count
{
Expand All @@ -47,6 +58,14 @@ public int Count
}
}

/// <summary>
/// Gets the current sorting strategy applied when enumerating the collection
/// </summary>
public GitSortOptions SortedBy
{
get { return sortOptions; }
}

#region IEnumerable<Commit> Members

/// <summary>
Expand Down Expand Up @@ -75,37 +94,13 @@ IEnumerator IEnumerable.GetEnumerator()
#endregion

/// <summary>
/// Sorts <see cref = "CommitCollection" /> with the specified options.
/// </summary>
/// <param name = "options">The options.</param>
/// <returns></returns>
public CommitCollection SortBy(GitSortOptions options)
{
return new CommitCollection(repo) { sortOptions = options, pushedSha = pushedSha };
}

/// <summary>
/// Starts enumeratoring the <see cref = "CommitCollection" /> at the specified branch.
/// </summary>
/// <param name = "branch">The branch.</param>
/// <returns></returns>
public CommitCollection StartingAt(Branch branch)
{
Ensure.ArgumentNotNull(branch, "branch");

return new CommitCollection(repo) { sortOptions = sortOptions, pushedSha = branch.Tip.Sha };
}

/// <summary>
/// Starts enumeratoring the <see cref = "CommitCollection" /> at the specified reference.
/// Sorts <see cref = "CommitCollection" /> according to the specified strategy.
/// </summary>
/// <param name = "reference">The reference.</param>
/// <param name = "sortingStrategy">The sorting strategy to be applied when enumerating the commits.</param>
/// <returns></returns>
public CommitCollection StartingAt(Reference reference)
public CommitCollection SortBy(GitSortOptions sortingStrategy)
{
Ensure.ArgumentNotNull(reference, "reference");

return new CommitCollection(repo) { sortOptions = sortOptions, pushedSha = reference.ResolveToDirectReference().Target.Sha };
return new CommitCollection(repo, sortingStrategy) { pushedSha = pushedSha };
}

/// <summary>
Expand All @@ -117,7 +112,7 @@ public CommitCollection StartingAt(string sha)
{
Ensure.ArgumentNotNullOrEmptyString(sha, "sha");

return new CommitCollection(repo) { sortOptions = sortOptions, pushedSha = sha };
return new CommitCollection(repo, sortOptions) { pushedSha = sha };
}

#region Nested type: CommitEnumerator
Expand Down
33 changes: 33 additions & 0 deletions LibGit2Sharp/CommitCollectionExtensions.cs
@@ -0,0 +1,33 @@
using LibGit2Sharp.Core;

namespace LibGit2Sharp
{
public static class CommitCollectionExtensions
{
/// <summary>
/// Starts enumerating the <paramref name="commitCollection"/> at the specified branch.
/// </summary>
/// <param name="commitCollection">The commit collection to enumerate.</param>
/// <param name = "branch">The branch.</param>
/// <returns></returns>
public static CommitCollection StartingAt(this CommitCollection commitCollection, Branch branch)
{
Ensure.ArgumentNotNull(branch, "branch");

return commitCollection.StartingAt(branch.Tip.Sha);
}

/// <summary>
/// Starts enumerating the <paramref name="commitCollection"/> at the specified reference.
/// </summary>
/// <param name="commitCollection">The commit collection to enumerate.</param>
/// <param name = "reference">The reference.</param>
/// <returns></returns>
public static CommitCollection StartingAt(this CommitCollection commitCollection, Reference reference)
{
Ensure.ArgumentNotNull(reference, "reference");

return commitCollection.StartingAt(reference.ResolveToDirectReference().Target.Sha);
}
}
}
1 change: 1 addition & 0 deletions LibGit2Sharp/LibGit2Sharp.csproj
Expand Up @@ -47,6 +47,7 @@
<Compile Include="Blob.cs" />
<Compile Include="Branch.cs" />
<Compile Include="BranchCollection.cs" />
<Compile Include="CommitCollectionExtensions.cs" />
<Compile Include="Commit.cs" />
<Compile Include="CommitCollection.cs" />
<Compile Include="Core\Ensure.cs" />
Expand Down

0 comments on commit cf9dbed

Please sign in to comment.