Skip to content

Commit c423ad1

Browse files
committed
Support for blobless repositories
1 parent 6329bea commit c423ad1

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

LibGit2Sharp/Blob.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class Blob : GitObject
1212
{
1313
private readonly ILazy<Int64> lazySize;
1414
private readonly ILazy<bool> lazyIsBinary;
15+
private readonly ILazy<bool> lazyIsDownloaded;
1516

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

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

3840
/// <summary>
3941
/// Determine if the blob content is most certainly binary or not.
4042
/// </summary>
41-
public virtual bool IsBinary { get { return lazyIsBinary.Value; } }
43+
public virtual bool IsBinary => lazyIsBinary.Value;
44+
45+
46+
/// <summary>
47+
/// Determine if the blob content was downloaded (happens with partially cloned repositories)
48+
/// </summary>
49+
public virtual bool IsDownloaded => lazyIsDownloaded.Value;
4250

4351
/// <summary>
4452
/// Gets the blob content in a <see cref="Stream"/>.

LibGit2Sharp/Core/Proxy.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ public static unsafe ObjectId git_blob_create_from_workdir(RepositoryHandle repo
7171

7272
public static unsafe UnmanagedMemoryStream git_blob_filtered_content_stream(RepositoryHandle repo, ObjectId id, string path, bool check_for_binary_data)
7373
{
74-
var buf = new GitBuf();
7574
var handle = new ObjectSafeWrapper(id, repo).ObjectPtr;
75+
if (handle == null)
76+
return null;
7677

78+
var buf = new GitBuf();
7779
return new RawContentStream(handle, h =>
7880
{
7981
Ensure.ZeroResult(NativeMethods.git_blob_filtered_content(buf, h, path, check_for_binary_data));
@@ -86,16 +88,25 @@ public static unsafe UnmanagedMemoryStream git_blob_filtered_content_stream(Repo
8688
public static unsafe UnmanagedMemoryStream git_blob_rawcontent_stream(RepositoryHandle repo, ObjectId id, Int64 size)
8789
{
8890
var handle = new ObjectSafeWrapper(id, repo).ObjectPtr;
91+
if (handle == null)
92+
return null;
93+
8994
return new RawContentStream(handle, h => NativeMethods.git_blob_rawcontent(h), h => size);
9095
}
9196

9297
public static unsafe long git_blob_rawsize(ObjectHandle obj)
9398
{
99+
if (obj == null)
100+
return 0;
101+
94102
return NativeMethods.git_blob_rawsize(obj);
95103
}
96104

97105
public static unsafe bool git_blob_is_binary(ObjectHandle obj)
98106
{
107+
if (obj == null)
108+
return false;
109+
99110
int res = NativeMethods.git_blob_is_binary(obj);
100111
Ensure.BooleanResult(res);
101112

0 commit comments

Comments
 (0)