Skip to content

Commit

Permalink
Add more features to Log (#9)
Browse files Browse the repository at this point in the history
* Add paths and reverse to Log

* Add path overloads to Corgit.LogAsync
  • Loading branch information
jzebedee authored Mar 15, 2019
1 parent 2edf0cb commit e0e88d7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/corgit/Corgit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,12 @@ public async Task<IEnumerable<GitFileStatus>> StatusAsync()
public Task<ExecutionResult> InitAsync()
=> RunGitAsync(GitArguments.Init());

public async Task<IEnumerable<GitCommit>> LogAsync(GitArguments.LogOptions options = default)
public Task<IEnumerable<GitCommit>> LogAsync(GitArguments.LogOptions options = default, params string[] paths)
=> LogAsync(options: options, paths: paths.AsEnumerable());

public async Task<IEnumerable<GitCommit>> LogAsync(GitArguments.LogOptions options = default, IEnumerable<string> paths = null)
{
var result = await RunGitAsync(GitArguments.Log(options));
var result = await RunGitAsync(GitArguments.Log(options, paths));
if (result.ExitCode == 1)
{
return Enumerable.Empty<GitCommit>();
Expand Down
23 changes: 19 additions & 4 deletions src/corgit/GitArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,40 @@ public static IEnumerable<string> Commit(CommitOptions options = default)
public struct LogOptions
{
public readonly int? MaxEntries;
public readonly bool Reverse;

public LogOptions(int? maxEntries = 32)
public LogOptions(int? maxEntries = 32, bool reverse = false)
{
MaxEntries = maxEntries;
Reverse = reverse;
}
}
public static IEnumerable<string> Log(LogOptions options = default)
public static IEnumerable<string> Log(LogOptions options = default, IEnumerable<string> paths = null)
{
const string CommitFormat = "%H\n%ae\n%P\n%B";
const string Separator = "%x00%x00";

yield return "log";
yield return $"--pretty=format:{CommitFormat}{Separator}";

if (options.MaxEntries.HasValue)
{
yield return $"-{options.MaxEntries}";
yield return $"-n {options.MaxEntries}";
}

yield return $"--pretty=format:{CommitFormat}{Separator}";
if (options.Reverse)
{
yield return "--reverse";
}

if (paths != null)
{
yield return "--";
foreach (var path in paths)
{
yield return path;
}
}
}

public static IEnumerable<string> Add(IEnumerable<string> paths = null)
Expand Down

0 comments on commit e0e88d7

Please sign in to comment.