Skip to content

Commit bcf3892

Browse files
committed
Make IsMissing more generic
1 parent d08d1f7 commit bcf3892

File tree

6 files changed

+27
-27
lines changed

6 files changed

+27
-27
lines changed

LibGit2Sharp.sln.DotSettings

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHING_EMPTY_BRACES/@EntryValue">True</s:Boolean>
1212
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES/@EntryValue">True</s:Boolean>
1313
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
14+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
15+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
16+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
1417
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
1518
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
1619
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean>

LibGit2Sharp/Blob.cs

+2-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public class Blob : GitObject
1515
{
1616
private readonly ILazy<Int64> lazySize;
1717
private readonly ILazy<bool> lazyIsBinary;
18-
private readonly ILazy<bool> lazyIsMissing;
1918

2019
/// <summary>
2120
/// Needed for mocking purposes.s
@@ -26,9 +25,8 @@ protected Blob()
2625
internal Blob(Repository repo, ObjectId id)
2726
: base(repo, id)
2827
{
29-
lazySize = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_blob_rawsize);
30-
lazyIsBinary = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_blob_is_binary);
31-
lazyIsMissing = GitObjectLazyGroup.Singleton(repo, id, handle => handle == null, throwsIfMissing: false);
28+
lazySize = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_blob_rawsize, throwIfMissing: true);
29+
lazyIsBinary = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_blob_is_binary, throwIfMissing: true);
3230
}
3331

3432
/// <summary>
@@ -47,12 +45,6 @@ internal Blob(Repository repo, ObjectId id)
4745
/// <exception cref="NotFoundException">Throws if blob is missing</exception>
4846
public virtual bool IsBinary => lazyIsBinary.Value;
4947

50-
51-
/// <summary>
52-
/// Determine if the blob content is missing ( with partially cloned repositories)
53-
/// </summary>
54-
public virtual bool IsMissing => lazyIsMissing.Value;
55-
5648
/// <summary>
5749
/// Gets the blob content in a <see cref="Stream"/>.
5850
/// </summary>

LibGit2Sharp/Core/GitObjectLazyGroup.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,16 @@ protected override void EvaluateInternal(Action<ObjectHandle> evaluator)
1717
{
1818
using (var osw = new ObjectSafeWrapper(id, repo.Handle))
1919
{
20-
if (osw.ObjectPtr == null)
21-
throw new NotFoundException($"No valid git object identified by '{id}' exists in the repository.");
22-
2320
evaluator(osw.ObjectPtr);
2421
}
2522
}
2623

27-
public static ILazy<TResult> Singleton<TResult>(Repository repo, ObjectId id, Func<ObjectHandle, TResult> resultSelector, bool throwsIfMissing = true)
24+
public static ILazy<TResult> Singleton<TResult>(Repository repo, ObjectId id, Func<ObjectHandle, TResult> resultSelector, bool throwIfMissing = false)
2825
{
2926
return Singleton(() =>
3027
{
31-
using (var osw = new ObjectSafeWrapper(id, repo.Handle))
28+
using (var osw = new ObjectSafeWrapper(id, repo.Handle, throwIfMissing: throwIfMissing))
3229
{
33-
if (throwsIfMissing && osw.ObjectPtr == null)
34-
throw new NotFoundException($"No valid git object identified by '{id}' exists in the repository.");
35-
3630
return resultSelector(osw.ObjectPtr);
3731
}
3832
});

LibGit2Sharp/Core/ObjectSafeWrapper.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ internal class ObjectSafeWrapper : IDisposable
77
{
88
private readonly ObjectHandle objectPtr;
99

10-
public unsafe ObjectSafeWrapper(ObjectId id, RepositoryHandle handle, bool allowNullObjectId = false)
10+
public unsafe ObjectSafeWrapper(ObjectId id, RepositoryHandle handle, bool allowNullObjectId = false, bool throwIfMissing = false)
1111
{
1212
Ensure.ArgumentNotNull(handle, "handle");
1313

@@ -20,13 +20,15 @@ public unsafe ObjectSafeWrapper(ObjectId id, RepositoryHandle handle, bool allow
2020
Ensure.ArgumentNotNull(id, "id");
2121
objectPtr = Proxy.git_object_lookup(handle, id, GitObjectType.Any);
2222
}
23-
}
2423

25-
public ObjectHandle ObjectPtr
26-
{
27-
get { return objectPtr; }
24+
if (objectPtr == null && throwIfMissing)
25+
{
26+
throw new NotFoundException($"No valid git object identified by '{id}' exists in the repository.");
27+
}
2828
}
2929

30+
public ObjectHandle ObjectPtr => objectPtr;
31+
3032
public void Dispose()
3133
{
3234
Dispose(true);

LibGit2Sharp/GitObject.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4-
using System.Globalization;
54
using LibGit2Sharp.Core;
6-
using LibGit2Sharp.Core.Handles;
75

86
namespace LibGit2Sharp
97
{
@@ -33,6 +31,8 @@ public abstract class GitObject : IEquatable<GitObject>, IBelongToARepository
3331
private static readonly LambdaEqualityHelper<GitObject> equalityHelper =
3432
new LambdaEqualityHelper<GitObject>(x => x.Id);
3533

34+
private readonly ILazy<bool> lazyIsMissing;
35+
3636
/// <summary>
3737
/// The <see cref="Repository"/> containing the object.
3838
/// </summary>
@@ -53,13 +53,22 @@ protected GitObject(Repository repo, ObjectId id)
5353
{
5454
this.repo = repo;
5555
Id = id;
56+
lazyIsMissing = GitObjectLazyGroup.Singleton(repo, id, handle => handle == null, throwIfMissing: false);
5657
}
5758

5859
/// <summary>
5960
/// Gets the id of this object
6061
/// </summary>
6162
public virtual ObjectId Id { get; private set; }
6263

64+
/// <summary>
65+
/// Determine if the object is missing
66+
/// </summary>
67+
/// <remarks>
68+
/// This is common when dealing with partially cloned repositories as blobs or trees could be missing
69+
/// </remarks>
70+
public virtual bool IsMissing => lazyIsMissing.Value;
71+
6372
/// <summary>
6473
/// Gets the 40 character sha1 of this object.
6574
/// </summary>

LibGit2Sharp/Tree.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ internal Tree(Repository repo, ObjectId id, string path)
3131
{
3232
this.path = path ?? "";
3333

34-
lazyCount = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_tree_entrycount);
34+
lazyCount = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_tree_entrycount, throwIfMissing: true);
3535
}
3636

3737
/// <summary>
3838
/// Gets the number of <see cref="TreeEntry"/> immediately under this <see cref="Tree"/>.
3939
/// </summary>
40-
public virtual int Count { get { return lazyCount.Value; } }
40+
public virtual int Count => lazyCount.Value;
4141

4242
/// <summary>
4343
/// Gets the <see cref="TreeEntry"/> pointed at by the <paramref name="relativePath"/> in this <see cref="Tree"/> instance.

0 commit comments

Comments
 (0)