Skip to content

Commit 8711021

Browse files
Therzoksevoku
authored andcommitted
Introduce blame options for rename tracking
1 parent a709ab8 commit 8711021

File tree

8 files changed

+53
-6
lines changed

8 files changed

+53
-6
lines changed

LibGit2Sharp/BlameHunkCollection.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,21 @@ internal unsafe BlameHunkCollection(Repository repo, RepositoryHandle repoHandle
2828
var rawopts = new git_blame_options
2929
{
3030
version = 1,
31+
FindOptions = new GitDiffFindOptions {
32+
Version = 1,
33+
},
3134
flags = options.Strategy.ToGitBlameOptionFlags(),
3235
min_line = new UIntPtr((uint)options.MinLine),
3336
max_line = new UIntPtr((uint)options.MaxLine),
3437
};
3538

39+
if (options.FindNoRenames)
40+
rawopts.FindOptions.Flags = GitDiffFindFlags.GIT_DIFF_FIND_NO_RENAMES;
41+
else if (options.FindExactRenames)
42+
rawopts.FindOptions.Flags = GitDiffFindFlags.GIT_DIFF_FIND_EXACT_MATCH_ONLY;
43+
else
44+
rawopts.FindOptions.Flags = GitDiffFindFlags.GIT_DIFF_FIND_RENAMES;
45+
3646
if (options.StartingAt != null)
3747
{
3848
fixed (byte* p = rawopts.newest_commit.Id)

LibGit2Sharp/BlameOptions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public enum BlameStrategy
77
{
88
/// <summary>
9-
/// Track renames of the file, but no block movement.
9+
/// Track renames of the file using diff rename detection, but no block movement.
1010
/// </summary>
1111
Default,
1212

@@ -58,5 +58,15 @@ public sealed class BlameOptions
5858
/// If this is set to 0, blame ends with the last line in the file.
5959
/// </summary>
6060
public int MaxLine { get; set; }
61+
62+
/// <summary>
63+
/// Disables rename heuristics, only matching renames on unmodified files.
64+
/// </summary>
65+
public bool FindExactRenames { get; set; }
66+
67+
/// <summary>
68+
/// Fully disable rename checking.
69+
/// </summary>
70+
public bool FindNoRenames { get; set; }
6171
}
6272
}

LibGit2Sharp/Core/GitBlame.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ internal enum GitBlameOptionFlags
4444
internal class git_blame_options
4545
{
4646
public uint version = 1;
47+
public GitDiffFindOptions FindOptions;
4748
public GitBlameOptionFlags flags;
4849

4950
public UInt16 min_match_characters;

LibGit2Sharp/Core/GitDiff.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ enum GitDiffFindFlags
350350
// turn on all finding features
351351
GIT_DIFF_FIND_ALL = (0x0ff),
352352

353+
// does no work trying to find renames
354+
GIT_DIFF_FIND_NO_RENAMES = (1 << 8),
355+
353356
// measure similarity ignoring leading whitespace (default)
354357
GIT_DIFF_FIND_IGNORE_LEADING_WHITESPACE = 0,
355358
// measure similarity ignoring all whitespace
@@ -367,9 +370,9 @@ enum GitDiffFindFlags
367370
}
368371

369372
[StructLayout(LayoutKind.Sequential)]
370-
internal class GitDiffFindOptions
373+
internal struct GitDiffFindOptions
371374
{
372-
public uint Version = 1;
375+
public uint Version;
373376
public GitDiffFindFlags Flags;
374377
public UInt16 RenameThreshold;
375378
public UInt16 RenameFromRewriteThreshold;

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,12 @@ internal static extern unsafe int git_diff_foreach(
618618
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
619619
internal static extern unsafe int git_diff_find_similar(
620620
git_diff* diff,
621-
GitDiffFindOptions options);
621+
IntPtr options);
622+
623+
[DllImport(libgit2)]
624+
internal static extern unsafe int git_diff_find_similar(
625+
git_diff* diff,
626+
ref GitDiffFindOptions options);
622627

623628
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
624629
internal static extern unsafe UIntPtr git_diff_num_deltas(git_diff* diff);

LibGit2Sharp/Core/Proxy.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,12 +827,18 @@ public static unsafe DiffHandle git_diff_tree_to_workdir(
827827
}
828828
}
829829

830-
public static unsafe void git_diff_find_similar(DiffHandle diff, GitDiffFindOptions options)
830+
public static unsafe void git_diff_find_similar(DiffHandle diff, IntPtr options)
831831
{
832832
int res = NativeMethods.git_diff_find_similar(diff, options);
833833
Ensure.ZeroResult(res);
834834
}
835835

836+
public static unsafe void git_diff_find_similar(DiffHandle diff, GitDiffFindOptions options)
837+
{
838+
int res = NativeMethods.git_diff_find_similar(diff, ref options);
839+
Ensure.ZeroResult(res);
840+
}
841+
836842
public static unsafe int git_diff_num_deltas(DiffHandle diff)
837843
{
838844
return (int)NativeMethods.git_diff_num_deltas(diff);

LibGit2Sharp/Diff.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ private static void DetectRenames(DiffHandle diffList, CompareOptions compareOpt
584584
var similarityOptions = (compareOptions == null) ? null : compareOptions.Similarity;
585585
if (similarityOptions == null || similarityOptions.RenameDetectionMode == RenameDetectionMode.Default)
586586
{
587-
Proxy.git_diff_find_similar(diffList, null);
587+
Proxy.git_diff_find_similar(diffList, IntPtr.Zero);
588588
return;
589589
}
590590

@@ -595,6 +595,7 @@ private static void DetectRenames(DiffHandle diffList, CompareOptions compareOpt
595595

596596
var opts = new GitDiffFindOptions
597597
{
598+
Version = 1,
598599
RenameThreshold = (ushort)similarityOptions.RenameThreshold,
599600
RenameFromRewriteThreshold = (ushort)similarityOptions.RenameFromRewriteThreshold,
600601
CopyThreshold = (ushort)similarityOptions.CopyThreshold,

LibGit2Sharp/DiffModifiers.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,16 @@ internal enum DiffModifiers
3636
/// diffing against the working directory.
3737
/// </summary>
3838
IncludeIgnored = (1 << 4),
39+
40+
/// <summary>
41+
/// Specifies that no rename heuristics will be used when checking for
42+
/// renames, renames being matched only on unmodified renamed files.
43+
/// </summary>
44+
FindExactRenames = (1 << 5),
45+
46+
/// <summary>
47+
/// Specifies that no renames will be searched for when running blame.
48+
/// </summary>
49+
FindNoRenames = (1 << 6),
3950
}
4051
}

0 commit comments

Comments
 (0)