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

[Proposal] Add git_blame_buffer #1979

Open
wants to merge 2 commits 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
49 changes: 41 additions & 8 deletions LibGit2Sharp/BlameHunkCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ namespace LibGit2Sharp
/// <summary>
/// The result of a blame operation.
/// </summary>
public class BlameHunkCollection : IEnumerable<BlameHunk>
public class BlameHunkCollection : IEnumerable<BlameHunk>, IDisposable
{
private readonly IRepository repo;
private readonly List<BlameHunk> hunks = new List<BlameHunk>();
private readonly RepositoryHandle repoHandle;
private readonly BlameHandle referenceHandle;
private readonly bool shouldDisposeHandle;

/// <summary>
/// For easy mocking
Expand Down Expand Up @@ -49,17 +52,29 @@ internal unsafe BlameHunkCollection(Repository repo, RepositoryHandle repoHandle
}
}

using (var blameHandle = Proxy.git_blame_file(repoHandle, path, rawopts))
referenceHandle = Proxy.git_blame_file(repoHandle, path, rawopts);
shouldDisposeHandle = true;
this.PopulateHunks(this.referenceHandle);
}

private unsafe BlameHunkCollection(IRepository repo, RepositoryHandle repoHandle, BlameHandle reference, byte[] buffer)
{
this.repo = repo;
this.repoHandle = repoHandle;
this.referenceHandle = reference;
this.shouldDisposeHandle = false;

using (var blameHandle = Proxy.git_blame_buffer(repoHandle, reference, buffer))
{
var numHunks = NativeMethods.git_blame_get_hunk_count(blameHandle);
for (uint i = 0; i < numHunks; ++i)
{
var rawHunk = Proxy.git_blame_get_hunk_byindex(blameHandle, i);
hunks.Add(new BlameHunk(this.repo, rawHunk));
}
this.PopulateHunks(blameHandle);
}
}

public BlameHunkCollection FromBuffer(byte[] buffer)
{
return new BlameHunkCollection(repo, repoHandle, this.referenceHandle, buffer);
}

/// <summary>
/// Access blame hunks by index.
/// </summary>
Expand Down Expand Up @@ -100,5 +115,23 @@ IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

private unsafe void PopulateHunks(BlameHandle blameHandle)
{
var numHunks = NativeMethods.git_blame_get_hunk_count(blameHandle);
for (uint i = 0; i < numHunks; ++i)
{
var rawHunk = Proxy.git_blame_get_hunk_byindex(blameHandle, i);
hunks.Add(new BlameHunk(this.repo, rawHunk));
}
}

public void Dispose()
{
if (shouldDisposeHandle)
{
this.referenceHandle.Dispose();
}
}
}
}
7 changes: 7 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ internal static extern unsafe int git_blame_file(
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string path,
git_blame_options options);

[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe int git_blame_buffer(
out git_blame* blame,
git_blame* reference,
IntPtr buffer,
UIntPtr len);

[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe void git_blame_free(git_blame* blame);

Expand Down
21 changes: 21 additions & 0 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ public static unsafe BlameHandle git_blame_file(
return NativeMethods.git_blame_get_hunk_byindex(blame, idx);
}

public static unsafe BlameHandle git_blame_buffer(
RepositoryHandle repo,
BlameHandle reference,
byte[] buffer)
{
git_blame* ptr;
int res;

unsafe
{
fixed (byte* p = buffer)
{
res = NativeMethods.git_blame_buffer(out ptr, reference, (IntPtr)p, (UIntPtr)buffer.Length);
}
}

Ensure.ZeroResult(res);

return new BlameHandle(ptr, true);
}

#endregion

#region git_blob_
Expand Down