Skip to content

Commit

Permalink
Support for blobless repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
jairbubbles committed Sep 12, 2021
1 parent 6329bea commit c423ad1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
12 changes: 10 additions & 2 deletions LibGit2Sharp/Blob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Blob : GitObject
{
private readonly ILazy<Int64> lazySize;
private readonly ILazy<bool> lazyIsBinary;
private readonly ILazy<bool> lazyIsDownloaded;

/// <summary>
/// Needed for mocking purposes.
Expand All @@ -24,6 +25,7 @@ internal Blob(Repository repo, ObjectId id)
{
lazySize = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_blob_rawsize);
lazyIsBinary = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_blob_is_binary);
lazyIsDownloaded = GitObjectLazyGroup.Singleton(repo, id, handle => handle != null);
}

/// <summary>
Expand All @@ -33,12 +35,18 @@ internal Blob(Repository repo, ObjectId id)
/// can be used.
/// </para>
/// </summary>
public virtual long Size { get { return lazySize.Value; } }
public virtual long Size => lazySize.Value;

/// <summary>
/// Determine if the blob content is most certainly binary or not.
/// </summary>
public virtual bool IsBinary { get { return lazyIsBinary.Value; } }
public virtual bool IsBinary => lazyIsBinary.Value;


/// <summary>
/// Determine if the blob content was downloaded (happens with partially cloned repositories)
/// </summary>
public virtual bool IsDownloaded => lazyIsDownloaded.Value;

/// <summary>
/// Gets the blob content in a <see cref="Stream"/>.
Expand Down
13 changes: 12 additions & 1 deletion LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ public static unsafe ObjectId git_blob_create_from_workdir(RepositoryHandle repo

public static unsafe UnmanagedMemoryStream git_blob_filtered_content_stream(RepositoryHandle repo, ObjectId id, string path, bool check_for_binary_data)
{
var buf = new GitBuf();
var handle = new ObjectSafeWrapper(id, repo).ObjectPtr;
if (handle == null)
return null;

var buf = new GitBuf();
return new RawContentStream(handle, h =>
{
Ensure.ZeroResult(NativeMethods.git_blob_filtered_content(buf, h, path, check_for_binary_data));
Expand All @@ -86,16 +88,25 @@ public static unsafe UnmanagedMemoryStream git_blob_filtered_content_stream(Repo
public static unsafe UnmanagedMemoryStream git_blob_rawcontent_stream(RepositoryHandle repo, ObjectId id, Int64 size)
{
var handle = new ObjectSafeWrapper(id, repo).ObjectPtr;
if (handle == null)
return null;

return new RawContentStream(handle, h => NativeMethods.git_blob_rawcontent(h), h => size);
}

public static unsafe long git_blob_rawsize(ObjectHandle obj)
{
if (obj == null)
return 0;

return NativeMethods.git_blob_rawsize(obj);
}

public static unsafe bool git_blob_is_binary(ObjectHandle obj)
{
if (obj == null)
return false;

int res = NativeMethods.git_blob_is_binary(obj);
Ensure.BooleanResult(res);

Expand Down

0 comments on commit c423ad1

Please sign in to comment.