Skip to content

Commit

Permalink
Added git log
Browse files Browse the repository at this point in the history
  • Loading branch information
devlead committed Dec 29, 2015
1 parent 457510f commit d28577f
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Cake.Git/Cake.Git.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<Compile Include="GitAliases.Commit.cs" />
<Compile Include="GitAliases.cs" />
<Compile Include="GitAliases.Init.cs" />
<Compile Include="GitAliases.Log.cs" />
<Compile Include="GitAliases.Pull.cs" />
<Compile Include="GitAliases.Push.cs" />
<Compile Include="GitCommit.cs" />
Expand Down
1 change: 0 additions & 1 deletion src/Cake.Git/GitAliases.Commit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ string message
)
)
);

}
}
}
173 changes: 173 additions & 0 deletions src/Cake.Git/GitAliases.Log.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Cake.Core;
using Cake.Core.Annotations;
using Cake.Core.IO;
using Cake.Git.Extensions;
using LibGit2Sharp;

// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global

namespace Cake.Git
{
// ReSharper disable once PublicMembersMustHaveComments
public static partial class GitAliases
{
/// <summary>
/// Get last commit
/// </summary>
/// <param name="context">The context.</param>
/// <param name="repositoryDirectoryPath">Path to repository.</param>
/// <returns>The path to the created repository.</returns>
/// <exception cref="ArgumentNullException"></exception>
[CakeMethodAlias]
[CakeAliasCategory("Log")]
[CakeNamespaceImport("LibGit2Sharp")]
public static GitCommit GitLogTip(
this ICakeContext context,
DirectoryPath repositoryDirectoryPath
)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (repositoryDirectoryPath == null)
{
throw new ArgumentNullException(nameof(repositoryDirectoryPath));
}

return context.UseRepository(
repositoryDirectoryPath,
repository => new GitCommit(repository.Head.Tip)
);
}

/// <summary>
/// Get commit log.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="repositoryDirectoryPath">Path to repository.</param>
/// <param name="count">Number of commits to fetch.</param>
/// <returns>The path to the created repository.</returns>
/// <exception cref="ArgumentNullException"></exception>
[CakeMethodAlias]
[CakeAliasCategory("Log")]
[CakeNamespaceImport("LibGit2Sharp")]
public static ICollection<GitCommit> GitLog(
this ICakeContext context,
DirectoryPath repositoryDirectoryPath,
int count
)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (repositoryDirectoryPath == null)
{
throw new ArgumentNullException(nameof(repositoryDirectoryPath));
}

return context.UseRepository(
repositoryDirectoryPath,
repository => repository.Commits
.Take(count)
.Select(commit => new GitCommit(commit))
.ToList()
);
}

/// <summary>
/// Get commit from certain commit id up to current.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="repositoryDirectoryPath">Path to repository.</param>
/// <param name="sinceCommitId">Commit id to start fetching from.</param>
/// <returns>The path to the created repository.</returns>
/// <exception cref="ArgumentNullException"></exception>
[CakeMethodAlias]
[CakeAliasCategory("Log")]
[CakeNamespaceImport("LibGit2Sharp")]
public static ICollection<GitCommit> GitLog(
this ICakeContext context,
DirectoryPath repositoryDirectoryPath,
string sinceCommitId
)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (repositoryDirectoryPath == null)
{
throw new ArgumentNullException(nameof(repositoryDirectoryPath));
}

if (string.IsNullOrWhiteSpace(sinceCommitId))
{
throw new ArgumentNullException(nameof(sinceCommitId));
}

return context.UseRepository(
repositoryDirectoryPath,
repository =>
{
var sinceCommit = repository.Lookup<Commit>(sinceCommitId);
var refsUpToCommit = repository.Refs.ReachableFrom(new[] {sinceCommit});
return repository.Commits
.QueryBy(new CommitFilter
{
IncludeReachableFrom = sinceCommit,
ExcludeReachableFrom = refsUpToCommit
})
.Select(commit => new GitCommit(commit))
.ToList();
}
);
}

/// <summary>
/// Get up to 15 commit logs.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="repositoryDirectoryPath">Path to repository.</param>
/// <param name="commitId">Commit id to lookup.</param>
/// <returns>The path to the created repository.</returns>
/// <exception cref="ArgumentNullException"></exception>
[CakeMethodAlias]
[CakeAliasCategory("Log")]
[CakeNamespaceImport("LibGit2Sharp")]
public static GitCommit GitLogLookup(
this ICakeContext context,
DirectoryPath repositoryDirectoryPath,
string commitId
)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (repositoryDirectoryPath == null)
{
throw new ArgumentNullException(nameof(repositoryDirectoryPath));
}

if (string.IsNullOrWhiteSpace(commitId))
{
throw new ArgumentNullException(nameof(commitId));
}

return context.UseRepository(
repositoryDirectoryPath,
repository =>new GitCommit(repository.Lookup<Commit>(commitId))
);
}
}
}

0 comments on commit d28577f

Please sign in to comment.