From bcf38922acb68e6a77fce058af736aaddb8fbde5 Mon Sep 17 00:00:00 2001 From: Julien Richard Date: Sun, 3 Oct 2021 19:19:20 +0200 Subject: [PATCH] Make IsMissing more generic --- LibGit2Sharp.sln.DotSettings | 3 +++ LibGit2Sharp/Blob.cs | 12 ++---------- LibGit2Sharp/Core/GitObjectLazyGroup.cs | 10 ++-------- LibGit2Sharp/Core/ObjectSafeWrapper.cs | 12 +++++++----- LibGit2Sharp/GitObject.cs | 13 +++++++++++-- LibGit2Sharp/Tree.cs | 4 ++-- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/LibGit2Sharp.sln.DotSettings b/LibGit2Sharp.sln.DotSettings index 8bc2282a87..cf9a3e98a3 100644 --- a/LibGit2Sharp.sln.DotSettings +++ b/LibGit2Sharp.sln.DotSettings @@ -11,6 +11,9 @@ True True <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + True + True + True True True True diff --git a/LibGit2Sharp/Blob.cs b/LibGit2Sharp/Blob.cs index 73ddbda87a..e99a58e812 100644 --- a/LibGit2Sharp/Blob.cs +++ b/LibGit2Sharp/Blob.cs @@ -15,7 +15,6 @@ public class Blob : GitObject { private readonly ILazy lazySize; private readonly ILazy lazyIsBinary; - private readonly ILazy lazyIsMissing; /// /// Needed for mocking purposes.s @@ -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); } /// @@ -47,12 +45,6 @@ internal Blob(Repository repo, ObjectId id) /// Throws if blob is missing public virtual bool IsBinary => lazyIsBinary.Value; - - /// - /// Determine if the blob content is missing ( with partially cloned repositories) - /// - public virtual bool IsMissing => lazyIsMissing.Value; - /// /// Gets the blob content in a . /// diff --git a/LibGit2Sharp/Core/GitObjectLazyGroup.cs b/LibGit2Sharp/Core/GitObjectLazyGroup.cs index d6beeb6af8..11c83a81ed 100644 --- a/LibGit2Sharp/Core/GitObjectLazyGroup.cs +++ b/LibGit2Sharp/Core/GitObjectLazyGroup.cs @@ -17,22 +17,16 @@ protected override void EvaluateInternal(Action 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 Singleton(Repository repo, ObjectId id, Func resultSelector, bool throwsIfMissing = true) + public static ILazy Singleton(Repository repo, ObjectId id, Func 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); } }); diff --git a/LibGit2Sharp/Core/ObjectSafeWrapper.cs b/LibGit2Sharp/Core/ObjectSafeWrapper.cs index 8bb7e96334..f2ab4a9e16 100644 --- a/LibGit2Sharp/Core/ObjectSafeWrapper.cs +++ b/LibGit2Sharp/Core/ObjectSafeWrapper.cs @@ -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"); @@ -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); diff --git a/LibGit2Sharp/GitObject.cs b/LibGit2Sharp/GitObject.cs index 218f8f1413..ce2bdf5432 100644 --- a/LibGit2Sharp/GitObject.cs +++ b/LibGit2Sharp/GitObject.cs @@ -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 { @@ -33,6 +31,8 @@ public abstract class GitObject : IEquatable, IBelongToARepository private static readonly LambdaEqualityHelper equalityHelper = new LambdaEqualityHelper(x => x.Id); + private readonly ILazy lazyIsMissing; + /// /// The containing the object. /// @@ -53,6 +53,7 @@ protected GitObject(Repository repo, ObjectId id) { this.repo = repo; Id = id; + lazyIsMissing = GitObjectLazyGroup.Singleton(repo, id, handle => handle == null, throwIfMissing: false); } /// @@ -60,6 +61,14 @@ protected GitObject(Repository repo, ObjectId id) /// public virtual ObjectId Id { get; private set; } + /// + /// Determine if the object is missing + /// + /// + /// This is common when dealing with partially cloned repositories as blobs or trees could be missing + /// + public virtual bool IsMissing => lazyIsMissing.Value; + /// /// Gets the 40 character sha1 of this object. /// diff --git a/LibGit2Sharp/Tree.cs b/LibGit2Sharp/Tree.cs index ca70551839..95d0ab4fb4 100644 --- a/LibGit2Sharp/Tree.cs +++ b/LibGit2Sharp/Tree.cs @@ -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); } /// /// Gets the number of immediately under this . /// - public virtual int Count { get { return lazyCount.Value; } } + public virtual int Count => lazyCount.Value; /// /// Gets the pointed at by the in this instance.