Skip to content

Commit

Permalink
Make IsMissing more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
jairbubbles committed Oct 3, 2021
1 parent d08d1f7 commit bcf3892
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
3 changes: 3 additions & 0 deletions LibGit2Sharp.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHING_EMPTY_BRACES/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean>
Expand Down
12 changes: 2 additions & 10 deletions LibGit2Sharp/Blob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class Blob : GitObject
{
private readonly ILazy<Int64> lazySize;
private readonly ILazy<bool> lazyIsBinary;
private readonly ILazy<bool> lazyIsMissing;

/// <summary>
/// Needed for mocking purposes.s
Expand All @@ -26,9 +25,8 @@ protected Blob()
internal Blob(Repository repo, ObjectId id)
: base(repo, id)
{
lazySize = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_blob_rawsize);
lazyIsBinary = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_blob_is_binary);
lazyIsMissing = GitObjectLazyGroup.Singleton(repo, id, handle => handle == null, throwsIfMissing: false);
lazySize = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_blob_rawsize, throwIfMissing: true);
lazyIsBinary = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_blob_is_binary, throwIfMissing: true);
}

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


/// <summary>
/// Determine if the blob content is missing ( with partially cloned repositories)
/// </summary>
public virtual bool IsMissing => lazyIsMissing.Value;

/// <summary>
/// Gets the blob content in a <see cref="Stream"/>.
/// </summary>
Expand Down
10 changes: 2 additions & 8 deletions LibGit2Sharp/Core/GitObjectLazyGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,16 @@ protected override void EvaluateInternal(Action<ObjectHandle> evaluator)
{
using (var osw = new ObjectSafeWrapper(id, repo.Handle))
{
if (osw.ObjectPtr == null)
throw new NotFoundException($"No valid git object identified by '{id}' exists in the repository.");

evaluator(osw.ObjectPtr);
}
}

public static ILazy<TResult> Singleton<TResult>(Repository repo, ObjectId id, Func<ObjectHandle, TResult> resultSelector, bool throwsIfMissing = true)
public static ILazy<TResult> Singleton<TResult>(Repository repo, ObjectId id, Func<ObjectHandle, TResult> resultSelector, bool throwIfMissing = false)
{
return Singleton(() =>
{
using (var osw = new ObjectSafeWrapper(id, repo.Handle))
using (var osw = new ObjectSafeWrapper(id, repo.Handle, throwIfMissing: throwIfMissing))
{
if (throwsIfMissing && osw.ObjectPtr == null)
throw new NotFoundException($"No valid git object identified by '{id}' exists in the repository.");
return resultSelector(osw.ObjectPtr);
}
});
Expand Down
12 changes: 7 additions & 5 deletions LibGit2Sharp/Core/ObjectSafeWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal class ObjectSafeWrapper : IDisposable
{
private readonly ObjectHandle objectPtr;

public unsafe ObjectSafeWrapper(ObjectId id, RepositoryHandle handle, bool allowNullObjectId = false)
public unsafe ObjectSafeWrapper(ObjectId id, RepositoryHandle handle, bool allowNullObjectId = false, bool throwIfMissing = false)
{
Ensure.ArgumentNotNull(handle, "handle");

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

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

public ObjectHandle ObjectPtr => objectPtr;

public void Dispose()
{
Dispose(true);
Expand Down
13 changes: 11 additions & 2 deletions LibGit2Sharp/GitObject.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;

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

private readonly ILazy<bool> lazyIsMissing;

/// <summary>
/// The <see cref="Repository"/> containing the object.
/// </summary>
Expand All @@ -53,13 +53,22 @@ protected GitObject(Repository repo, ObjectId id)
{
this.repo = repo;
Id = id;
lazyIsMissing = GitObjectLazyGroup.Singleton(repo, id, handle => handle == null, throwIfMissing: false);
}

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

/// <summary>
/// Determine if the object is missing
/// </summary>
/// <remarks>
/// This is common when dealing with partially cloned repositories as blobs or trees could be missing
/// </remarks>
public virtual bool IsMissing => lazyIsMissing.Value;

/// <summary>
/// Gets the 40 character sha1 of this object.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions LibGit2Sharp/Tree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ internal Tree(Repository repo, ObjectId id, string path)
{
this.path = path ?? "";

lazyCount = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_tree_entrycount);
lazyCount = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_tree_entrycount, throwIfMissing: true);
}

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

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

0 comments on commit bcf3892

Please sign in to comment.