Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CompareOptions argument to Repository.Commits.QueryBy(path) #1757

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions LibGit2Sharp/CommitLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,14 @@ public IEnumerable<LogEntry> QueryBy(string path)
/// </summary>
/// <param name="path">The file's path.</param>
/// <param name="filter">The options used to control which commits will be returned.</param>
/// <param name="compareOptions">Additional options to define comparison behavior.</param>
/// <returns>A list of file history entries, ready to be enumerated.</returns>
public IEnumerable<LogEntry> QueryBy(string path, CommitFilter filter)
public IEnumerable<LogEntry> QueryBy(string path, CommitFilter filter, CompareOptions compareOptions = null)
{
Ensure.ArgumentNotNull(path, "path");
Ensure.ArgumentNotNull(filter, "filter");

return new FileHistory(repo, path, filter);
return new FileHistory(repo, path, filter, compareOptions);
}

private class CommitEnumerator : IEnumerator<Commit>
Expand Down
26 changes: 17 additions & 9 deletions LibGit2Sharp/Core/FileHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ internal class FileHistory : IEnumerable<LogEntry>
/// </summary>
private readonly CommitFilter _queryFilter;

/// <summary>
/// Options to define comparison behavior.
/// </summary>
private readonly CompareOptions _compareOptions;

#endregion

#region Constructors
Expand All @@ -49,7 +54,7 @@ internal class FileHistory : IEnumerable<LogEntry>
/// <param name="path">The file's path relative to the repository's root.</param>
/// <exception cref="ArgumentNullException">If any of the parameters is null.</exception>
internal FileHistory(Repository repo, string path)
: this(repo, path, new CommitFilter())
: this(repo, path, new CommitFilter(), new CompareOptions())
{ }

/// <summary>
Expand All @@ -62,9 +67,10 @@ internal FileHistory(Repository repo, string path)
/// <param name="repo">The repository.</param>
/// <param name="path">The file's path relative to the repository's root.</param>
/// <param name="queryFilter">The filter to be used in querying the commit log.</param>
/// <param name="compareOptions">Additional options to define comparison behavior.</param>
/// <exception cref="ArgumentNullException">If any of the parameters is null.</exception>
/// <exception cref="ArgumentException">When an unsupported commit sort strategy is specified.</exception>
internal FileHistory(Repository repo, string path, CommitFilter queryFilter)
internal FileHistory(Repository repo, string path, CommitFilter queryFilter, CompareOptions compareOptions)
{
Ensure.ArgumentNotNull(repo, "repo");
Ensure.ArgumentNotNull(path, "path");
Expand All @@ -80,6 +86,7 @@ internal FileHistory(Repository repo, string path, CommitFilter queryFilter)
_repo = repo;
_path = path;
_queryFilter = queryFilter;
_compareOptions = compareOptions;
}

#endregion
Expand All @@ -94,7 +101,7 @@ internal FileHistory(Repository repo, string path, CommitFilter queryFilter)
/// <returns>A <see cref="IEnumerator{LogEntry}"/>.</returns>
public IEnumerator<LogEntry> GetEnumerator()
{
return FullHistory(_repo, _path, _queryFilter).GetEnumerator();
return FullHistory(_repo, _path, _queryFilter, _compareOptions).GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
Expand All @@ -110,8 +117,9 @@ IEnumerator IEnumerable.GetEnumerator()
/// <param name="repo">The repository.</param>
/// <param name="path">The file's path relative to the repository's root.</param>
/// <param name="filter">The filter to be used in querying the commits log.</param>
/// <param name="compareOptions">Additional options to define comparison behavior.</param>
/// <returns>A collection of <see cref="LogEntry"/> instances.</returns>
private static IEnumerable<LogEntry> FullHistory(IRepository repo, string path, CommitFilter filter)
private static IEnumerable<LogEntry> FullHistory(IRepository repo, string path, CommitFilter filter, CompareOptions compareOptions)
{
var map = new Dictionary<Commit, string>();

Expand All @@ -132,7 +140,7 @@ private static IEnumerable<LogEntry> FullHistory(IRepository repo, string path,
}
else
{
DetermineParentPaths(repo, currentCommit, currentPath, map);
DetermineParentPaths(repo, currentCommit, currentPath, map, compareOptions);

if (parentCount != 1)
{
Expand All @@ -153,17 +161,17 @@ private static IEnumerable<LogEntry> FullHistory(IRepository repo, string path,
}
}

private static void DetermineParentPaths(IRepository repo, Commit currentCommit, string currentPath, IDictionary<Commit, string> map)
private static void DetermineParentPaths(IRepository repo, Commit currentCommit, string currentPath, IDictionary<Commit, string> map, CompareOptions compareOptions)
{
foreach (var parentCommit in currentCommit.Parents.Where(parentCommit => !map.ContainsKey(parentCommit)))
{
map.Add(parentCommit, ParentPath(repo, currentCommit, currentPath, parentCommit));
map.Add(parentCommit, ParentPath(repo, currentCommit, currentPath, parentCommit, compareOptions));
}
}

private static string ParentPath(IRepository repo, Commit currentCommit, string currentPath, Commit parentCommit)
private static string ParentPath(IRepository repo, Commit currentCommit, string currentPath, Commit parentCommit, CompareOptions compareOptions)
{
using (var treeChanges = repo.Diff.Compare<TreeChanges>(parentCommit.Tree, currentCommit.Tree))
using (var treeChanges = repo.Diff.Compare<TreeChanges>(parentCommit.Tree, currentCommit.Tree, compareOptions))
{
var treeEntryChanges = treeChanges.FirstOrDefault(c => c.Path == currentPath);
return treeEntryChanges != null && treeEntryChanges.Status == ChangeKind.Renamed
Expand Down
3 changes: 2 additions & 1 deletion LibGit2Sharp/IQueryableCommitLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public interface IQueryableCommitLog : ICommitLog
/// </summary>
/// <param name="path">The file's path.</param>
/// <param name="filter">The options used to control which commits will be returned.</param>
/// <param name="compareOptions">Additional options to define comparison behavior.</param>
/// <returns>A list of file history entries, ready to be enumerated.</returns>
IEnumerable<LogEntry> QueryBy(string path, CommitFilter filter);
IEnumerable<LogEntry> QueryBy(string path, CommitFilter filter, CompareOptions compareOptions = null);

}
}