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

Expose diff whitespace settings #2007

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions LibGit2Sharp/CompareOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

namespace LibGit2Sharp
{
/// <summary>
/// Represents a mode for handling whitespace while making diff.
/// </summary>
public enum DiffWhitespaceMode
{
/// <summary>
/// Ignore all whitespace
/// </summary>
IgnoreAllWhitespaces,
/// <summary>
/// Ignore changes in amount of whitespace
/// </summary>
IgnoreWhitespaceChange,
/// <summary>
/// Ignore whitespace at end of line
/// </summary>
IgnoreWhitespaceEol
}

/// <summary>
/// Options to define file comparison behavior.
/// </summary>
Expand Down Expand Up @@ -34,6 +53,12 @@ public CompareOptions()
/// </summary>
public SimilarityOptions Similarity { get; set; }

/// <summary>
/// Represents a mode for handling whitespace while making diff.
/// By default is null - it means no extra flag is passed
/// </summary>
public DiffWhitespaceMode? WhitespaceMode { get; set; }

/// <summary>
/// Include "unmodified" entries in the results.
/// </summary>
Expand Down
18 changes: 17 additions & 1 deletion LibGit2Sharp/Diff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ private static GitDiffOptions BuildOptions(DiffModifiers diffOptions, FilePath[]
options.Flags |= GitDiffOptionFlags.GIT_DIFF_MINIMAL;
}

if (compareOptions.WhitespaceMode != null)
{
if (compareOptions.WhitespaceMode == DiffWhitespaceMode.IgnoreAllWhitespaces)
{
options.Flags |= GitDiffOptionFlags.GIT_DIFF_IGNORE_WHITESPACE;
}
else if (compareOptions.WhitespaceMode == DiffWhitespaceMode.IgnoreWhitespaceChange)
{
options.Flags |= GitDiffOptionFlags.GIT_DIFF_IGNORE_WHITESPACE_CHANGE;
}
else
{
options.Flags |= GitDiffOptionFlags.GIT_DIFF_IGNORE_WHITESPACE_EOL;
}
}
Comment on lines +61 to +75
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought:

Suggested change
if (compareOptions.WhitespaceMode != null)
{
if (compareOptions.WhitespaceMode == DiffWhitespaceMode.IgnoreAllWhitespaces)
{
options.Flags |= GitDiffOptionFlags.GIT_DIFF_IGNORE_WHITESPACE;
}
else if (compareOptions.WhitespaceMode == DiffWhitespaceMode.IgnoreWhitespaceChange)
{
options.Flags |= GitDiffOptionFlags.GIT_DIFF_IGNORE_WHITESPACE_CHANGE;
}
else
{
options.Flags |= GitDiffOptionFlags.GIT_DIFF_IGNORE_WHITESPACE_EOL;
}
}
if (compareOptions.WhitespaceMode == DiffWhitespaceMode.IgnoreAllWhitespaces)
{
options.Flags |= GitDiffOptionFlags.GIT_DIFF_IGNORE_WHITESPACE;
}
else if (compareOptions.WhitespaceMode == DiffWhitespaceMode.IgnoreWhitespaceChange)
{
options.Flags |= GitDiffOptionFlags.GIT_DIFF_IGNORE_WHITESPACE_CHANGE;
}
else if (compareOptions.WhitespaceMode == DiffWhitespaceMode.IgnoreWhitespaceEol)
{
options.Flags |= GitDiffOptionFlags.GIT_DIFF_IGNORE_WHITESPACE_EOL;
}


if (diffOptions.HasFlag(DiffModifiers.DisablePathspecMatch))
{
options.Flags |= GitDiffOptionFlags.GIT_DIFF_DISABLE_PATHSPEC_MATCH;
Expand Down Expand Up @@ -546,7 +562,7 @@ private DiffHandle BuildDiffList(

MatchedPathsAggregator matchedPaths = null;

// We can't match paths unless we've got something to match
// We can't match paths unless we've got something to match
// against and we're told to do so.
if (filePaths != null && explicitPathsOptions != null)
{
Expand Down