From b05a00f13f416b265903b86a9f38b6cca2e50b54 Mon Sep 17 00:00:00 2001 From: Julien Richard Date: Sun, 3 Oct 2021 19:23:22 +0200 Subject: [PATCH] Simplify dealing with missing git objects --- LibGit2Sharp.Tests/BlobFixture.cs | 34 ++++++++++++++++++++ LibGit2Sharp.Tests/TreeFixture.cs | 42 +++++++++++++++++++++++++ LibGit2Sharp/Blob.cs | 19 ++++++++--- LibGit2Sharp/Core/GitObjectLazyGroup.cs | 4 +-- LibGit2Sharp/Core/ObjectSafeWrapper.cs | 12 ++++--- LibGit2Sharp/Core/Proxy.cs | 6 ++-- LibGit2Sharp/GitObject.cs | 18 +++++++---- LibGit2Sharp/Tree.cs | 18 ++++++----- 8 files changed, 126 insertions(+), 27 deletions(-) diff --git a/LibGit2Sharp.Tests/BlobFixture.cs b/LibGit2Sharp.Tests/BlobFixture.cs index 98b30dcfc..ea35e59ef 100644 --- a/LibGit2Sharp.Tests/BlobFixture.cs +++ b/LibGit2Sharp.Tests/BlobFixture.cs @@ -15,6 +15,7 @@ public void CanGetBlobAsText() using (var repo = new Repository(path)) { var blob = repo.Lookup("a8233120f6ad708f843d861ce2b7228ec4e3dec6"); + Assert.False(blob.IsMissing); var text = blob.GetContentText(); @@ -36,6 +37,7 @@ public void CanGetBlobAsFilteredText(string autocrlf, string expectedText) repo.Config.Set("core.autocrlf", autocrlf); var blob = repo.Lookup("a8233120f6ad708f843d861ce2b7228ec4e3dec6"); + Assert.False(blob.IsMissing); var text = blob.GetContentText(new FilteringOptions("foo.txt")); @@ -67,6 +69,7 @@ public void CanGetBlobAsTextWithVariousEncodings(string encodingName, int expect var commit = repo.Commit("bom", Constants.Signature, Constants.Signature); var blob = (Blob)commit.Tree[bomFile].Target; + Assert.False(blob.IsMissing); Assert.Equal(expectedContentBytes, blob.Size); using (var stream = blob.GetContentStream()) { @@ -92,6 +95,7 @@ public void CanGetBlobSize() using (var repo = new Repository(path)) { var blob = repo.Lookup("a8233120f6ad708f843d861ce2b7228ec4e3dec6"); + Assert.False(blob.IsMissing); Assert.Equal(10, blob.Size); } } @@ -104,6 +108,7 @@ public void CanLookUpBlob() { var blob = repo.Lookup("a8233120f6ad708f843d861ce2b7228ec4e3dec6"); Assert.NotNull(blob); + Assert.False(blob.IsMissing); } } @@ -114,6 +119,7 @@ public void CanReadBlobStream() using (var repo = new Repository(path)) { var blob = repo.Lookup("a8233120f6ad708f843d861ce2b7228ec4e3dec6"); + Assert.False(blob.IsMissing); var contentStream = blob.GetContentStream(); Assert.Equal(blob.Size, contentStream.Length); @@ -140,6 +146,7 @@ public void CanReadBlobFilteredStream(string autocrlf, string expectedContent) repo.Config.Set("core.autocrlf", autocrlf); var blob = repo.Lookup("a8233120f6ad708f843d861ce2b7228ec4e3dec6"); + Assert.False(blob.IsMissing); var contentStream = blob.GetContentStream(new FilteringOptions("foo.txt")); Assert.Equal(expectedContent.Length, contentStream.Length); @@ -164,6 +171,7 @@ public void CanReadBlobFilteredStreamOfUnmodifiedBinary() using (var stream = new MemoryStream(binaryContent)) { Blob blob = repo.ObjectDatabase.CreateBlob(stream); + Assert.False(blob.IsMissing); using (var filtered = blob.GetContentStream(new FilteringOptions("foo.txt"))) { @@ -196,6 +204,7 @@ public void CanStageAFileGeneratedFromABlobContentStream() Assert.Equal("baae1fb3760a73481ced1fa03dc15614142c19ef", entry.Id.Sha); var blob = repo.Lookup(entry.Id.Sha); + Assert.False(blob.IsMissing); using (Stream stream = blob.GetContentStream()) using (Stream file = File.OpenWrite(Path.Combine(repo.Info.WorkingDirectory, "small.fromblob.txt"))) @@ -217,10 +226,35 @@ public void CanTellIfTheBlobContentLooksLikeBinary() using (var repo = new Repository(path)) { var blob = repo.Lookup("a8233120f6ad708f843d861ce2b7228ec4e3dec6"); + Assert.False(blob.IsMissing); Assert.False(blob.IsBinary); } } + [Fact] + public void CanTellIfABlobIsMissing() + { + string repoPath = SandboxBareTestRepo(); + + // Manually delete the objects directory to simulate a partial clone + Directory.Delete(Path.Combine(repoPath, "objects", "a8"), true); + + using (var repo = new Repository(repoPath)) + { + // Look up for the tree that reference the blob which is now missing + var tree = repo.Lookup("fd093bff70906175335656e6ce6ae05783708765"); + var blob = (Blob) tree["README"].Target; + + Assert.Equal("a8233120f6ad708f843d861ce2b7228ec4e3dec6", blob.Sha); + Assert.NotNull(blob); + Assert.True(blob.IsMissing); + Assert.Throws(() => blob.Size); + Assert.Throws(() => blob.IsBinary); + Assert.Throws(() => blob.GetContentText()); + Assert.Throws(() => blob.GetContentText(new FilteringOptions("foo.txt"))); + } + } + private static void SkipIfNotSupported(string autocrlf) { InconclusiveIf(() => autocrlf == "true" && Constants.IsRunningOnUnix, "Non-Windows does not support core.autocrlf = true"); diff --git a/LibGit2Sharp.Tests/TreeFixture.cs b/LibGit2Sharp.Tests/TreeFixture.cs index 31ca85c2d..a3a8d89eb 100644 --- a/LibGit2Sharp.Tests/TreeFixture.cs +++ b/LibGit2Sharp.Tests/TreeFixture.cs @@ -17,6 +17,7 @@ public void CanCompareTwoTreeEntries() using (var repo = new Repository(path)) { var tree = repo.Lookup(sha); + Assert.False(tree.IsMissing); TreeEntry treeEntry1 = tree["README"]; TreeEntry treeEntry2 = tree["README"]; Assert.Equal(treeEntry2, treeEntry1); @@ -31,6 +32,7 @@ public void CanConvertEntryToBlob() using (var repo = new Repository(path)) { var tree = repo.Lookup(sha); + Assert.False(tree.IsMissing); TreeEntry treeEntry = tree["README"]; var blob = treeEntry.Target as Blob; @@ -45,6 +47,7 @@ public void CanConvertEntryToTree() using (var repo = new Repository(path)) { var tree = repo.Lookup(sha); + Assert.False(tree.IsMissing); TreeEntry treeEntry = tree["1"]; var subtree = treeEntry.Target as Tree; @@ -59,6 +62,7 @@ public void CanEnumerateBlobs() using (var repo = new Repository(path)) { var tree = repo.Lookup(sha); + Assert.False(tree.IsMissing); IEnumerable blobs = tree .Where(e => e.TargetType == TreeEntryTargetType.Blob) @@ -76,6 +80,7 @@ public void CanEnumerateSubTrees() using (var repo = new Repository(path)) { var tree = repo.Lookup(sha); + Assert.False(tree.IsMissing); IEnumerable subTrees = tree .Where(e => e.TargetType == TreeEntryTargetType.Tree) @@ -93,6 +98,7 @@ public void CanEnumerateTreeEntries() using (var repo = new Repository(path)) { var tree = repo.Lookup(sha); + Assert.False(tree.IsMissing); Assert.Equal(tree.Count, tree.Count()); Assert.Equal(new[] { "1", "README", "branch_file.txt", "new.txt" }, tree.Select(te => te.Name).ToArray()); @@ -106,6 +112,7 @@ public void CanGetEntryByName() using (var repo = new Repository(path)) { var tree = repo.Lookup(sha); + Assert.False(tree.IsMissing); TreeEntry treeEntry = tree["README"]; Assert.Equal("a8233120f6ad708f843d861ce2b7228ec4e3dec6", treeEntry.Target.Sha); Assert.Equal("README", treeEntry.Name); @@ -119,6 +126,7 @@ public void GettingAnUknownTreeEntryReturnsNull() using (var repo = new Repository(path)) { var tree = repo.Lookup(sha); + Assert.False(tree.IsMissing); TreeEntry treeEntry = tree["I-do-not-exist"]; Assert.Null(treeEntry); } @@ -131,6 +139,7 @@ public void CanGetEntryCountFromTree() using (var repo = new Repository(path)) { var tree = repo.Lookup(sha); + Assert.False(tree.IsMissing); Assert.Equal(4, tree.Count); } } @@ -142,6 +151,7 @@ public void CanReadEntryAttributes() using (var repo = new Repository(path)) { var tree = repo.Lookup(sha); + Assert.False(tree.IsMissing); Assert.Equal(Mode.NonExecutableFile, tree["README"].Mode); } } @@ -154,6 +164,7 @@ public void CanReadTheTreeData() { var tree = repo.Lookup(sha); Assert.NotNull(tree); + Assert.False(tree.IsMissing); } } @@ -165,6 +176,7 @@ public void TreeDataIsPresent() { GitObject tree = repo.Lookup(sha); Assert.NotNull(tree); + Assert.False(tree.IsMissing); } } @@ -175,6 +187,7 @@ public void TreeUsesPosixStylePaths() { /* From a commit tree */ var commitTree = repo.Lookup("4c062a6").Tree; + Assert.False(commitTree.IsMissing); Assert.NotNull(commitTree["1/branch_file.txt"]); Assert.Null(commitTree["1\\branch_file.txt"]); } @@ -188,6 +201,7 @@ public void CanRetrieveTreeEntryPath() { /* From a commit tree */ var commitTree = repo.Lookup("4c062a6").Tree; + Assert.False(commitTree.IsMissing); TreeEntry treeTreeEntry = commitTree["1"]; Assert.Equal("1", treeTreeEntry.Path); @@ -201,6 +215,7 @@ public void CanRetrieveTreeEntryPath() // tree but exposes a complete path through its Path property var subTree = treeTreeEntry.Target as Tree; Assert.NotNull(subTree); + Assert.False(subTree.IsMissing); TreeEntry anInstance = subTree["branch_file.txt"]; Assert.NotEqual("branch_file.txt", anInstance.Path); @@ -239,6 +254,7 @@ public void CanParseSymlinkTreeEntries() .Add("A symlink", linkContent, Mode.SymbolicLink); Tree t = repo.ObjectDatabase.CreateTree(td); + Assert.False(t.IsMissing); var te = t["A symlink"]; @@ -248,5 +264,31 @@ public void CanParseSymlinkTreeEntries() Assert.Equal(linkContent, te.Target); } } + + [Fact] + public void CanTellIfATreeIsMissing() + { + var path = SandboxBareTestRepo(); + + // Manually delete the objects directory to simulate a partial clone + Directory.Delete(Path.Combine(path, "objects", "fd"), true); + + using (var repo = new Repository(path)) + { + // Look up for the commit that reference the tree which is now missing + var commit = repo.Lookup("4a202b346bb0fb0db7eff3cffeb3c70babbd2045"); + + Assert.True(commit.Tree.IsMissing); + Assert.Equal("fd093bff70906175335656e6ce6ae05783708765", commit.Tree.Sha); + Assert.Throws(() => commit.Tree.Count); + Assert.Throws(() => commit.Tree.Count()); + Assert.Throws(() => commit.Tree["README"]); + Assert.Throws(() => commit.Tree.ToArray()); + Assert.Throws(() => + { + foreach (var _ in commit.Tree) { } + }); + } + } } } diff --git a/LibGit2Sharp/Blob.cs b/LibGit2Sharp/Blob.cs index 9b14cb50f..d3cccf6ac 100644 --- a/LibGit2Sharp/Blob.cs +++ b/LibGit2Sharp/Blob.cs @@ -8,6 +8,9 @@ namespace LibGit2Sharp /// /// Stores the binary content of a tracked file. /// + /// + /// Since the introduction of partially cloned repositories, blobs might be missing on your local repository (see https://git-scm.com/docs/partial-clone) + /// public class Blob : GitObject { private readonly ILazy lazySize; @@ -22,8 +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); + lazySize = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_blob_rawsize, throwIfMissing: true); + lazyIsBinary = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_blob_is_binary, throwIfMissing: true); } /// @@ -33,16 +36,19 @@ internal Blob(Repository repo, ObjectId id) /// can be used. /// /// - public virtual long Size { get { return lazySize.Value; } } + /// Throws if blob is missing + public virtual long Size => lazySize.Value; /// /// Determine if the blob content is most certainly binary or not. /// - public virtual bool IsBinary { get { return lazyIsBinary.Value; } } + /// Throws if blob is missing + public virtual bool IsBinary => lazyIsBinary.Value; /// /// Gets the blob content in a . /// + /// Throws if blob is missing public virtual Stream GetContentStream() { return Proxy.git_blob_rawcontent_stream(repo.Handle, Id, Size); @@ -53,6 +59,7 @@ public virtual Stream GetContentStream() /// checked out to the working directory. /// Parameter controlling content filtering behavior /// + /// Throws if blob is missing public virtual Stream GetContentStream(FilteringOptions filteringOptions) { Ensure.ArgumentNotNull(filteringOptions, "filteringOptions"); @@ -64,6 +71,7 @@ public virtual Stream GetContentStream(FilteringOptions filteringOptions) /// Gets the blob content, decoded with UTF8 encoding if the encoding cannot be detected from the byte order mark /// /// Blob content as text. + /// Throws if blob is missing public virtual string GetContentText() { return ReadToEnd(GetContentStream(), null); @@ -75,6 +83,7 @@ public virtual string GetContentText() /// /// The encoding of the text to use, if it cannot be detected /// Blob content as text. + /// Throws if blob is missing public virtual string GetContentText(Encoding encoding) { Ensure.ArgumentNotNull(encoding, "encoding"); @@ -87,6 +96,7 @@ public virtual string GetContentText(Encoding encoding) /// /// Parameter controlling content filtering behavior /// Blob content as text. + /// Throws if blob is missing public virtual string GetContentText(FilteringOptions filteringOptions) { return GetContentText(filteringOptions, null); @@ -101,6 +111,7 @@ public virtual string GetContentText(FilteringOptions filteringOptions) /// Parameter controlling content filtering behavior /// The encoding of the text. (default: detected or UTF8) /// Blob content as text. + /// Throws if blob is missing public virtual string GetContentText(FilteringOptions filteringOptions, Encoding encoding) { Ensure.ArgumentNotNull(filteringOptions, "filteringOptions"); diff --git a/LibGit2Sharp/Core/GitObjectLazyGroup.cs b/LibGit2Sharp/Core/GitObjectLazyGroup.cs index 4e0ba384e..11c83a81e 100644 --- a/LibGit2Sharp/Core/GitObjectLazyGroup.cs +++ b/LibGit2Sharp/Core/GitObjectLazyGroup.cs @@ -21,11 +21,11 @@ protected override void EvaluateInternal(Action evaluator) } } - public static ILazy Singleton(Repository repo, ObjectId id, Func resultSelector) + 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)) { return resultSelector(osw.ObjectPtr); } diff --git a/LibGit2Sharp/Core/ObjectSafeWrapper.cs b/LibGit2Sharp/Core/ObjectSafeWrapper.cs index 8bb7e9633..f2ab4a9e1 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/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs index ca9a69f6d..1e63bef33 100644 --- a/LibGit2Sharp/Core/Proxy.cs +++ b/LibGit2Sharp/Core/Proxy.cs @@ -72,7 +72,7 @@ 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; + var handle = new ObjectSafeWrapper(id, repo, throwIfMissing: true).ObjectPtr; return new RawContentStream(handle, h => { @@ -85,7 +85,7 @@ 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; + var handle = new ObjectSafeWrapper(id, repo, throwIfMissing: true).ObjectPtr; return new RawContentStream(handle, h => NativeMethods.git_blob_rawcontent(h), h => size); } @@ -3263,7 +3263,7 @@ public static unsafe TreeEntryHandle git_tree_entry_byindex(ObjectHandle tree, l public static unsafe TreeEntryHandle git_tree_entry_bypath(RepositoryHandle repo, ObjectId id, string treeentry_path) { - using (var obj = new ObjectSafeWrapper(id, repo)) + using (var obj = new ObjectSafeWrapper(id, repo, throwIfMissing: true)) { git_tree_entry* treeEntryPtr; int res = NativeMethods.git_tree_entry_bypath(out treeEntryPtr, obj.ObjectPtr, treeentry_path); diff --git a/LibGit2Sharp/GitObject.cs b/LibGit2Sharp/GitObject.cs index 218f8f141..539fe6f8b 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,13 +61,18 @@ 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. /// - public virtual string Sha - { - get { return Id.Sha; } - } + public virtual string Sha => Id.Sha; internal static GitObject BuildFrom(Repository repo, ObjectId id, GitObjectType type, string path) { diff --git a/LibGit2Sharp/Tree.cs b/LibGit2Sharp/Tree.cs index ca7055183..f8494ad8b 100644 --- a/LibGit2Sharp/Tree.cs +++ b/LibGit2Sharp/Tree.cs @@ -13,6 +13,9 @@ namespace LibGit2Sharp /// /// A container which references a list of other s and s. /// + /// + /// Since the introduction of partially cloned repositories, trees might be missing on your local repository (see https://git-scm.com/docs/partial-clone) + /// [DebuggerDisplay("{DebuggerDisplay,nq}")] public class Tree : GitObject, IEnumerable { @@ -31,19 +34,21 @@ 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; } } + /// Throws if tree is missing + public virtual int Count => lazyCount.Value; /// /// Gets the pointed at by the in this instance. /// /// The relative path to the from this instance. /// null if nothing has been found, the otherwise. + /// Throws if tree is missing public virtual TreeEntry this[string relativePath] { get { return RetrieveFromPath(relativePath); } @@ -69,10 +74,7 @@ private unsafe TreeEntry RetrieveFromPath(string relativePath) } } - internal string Path - { - get { return path; } - } + internal string Path => path; #region IEnumerable Members @@ -103,9 +105,10 @@ internal static string CombinePath(string a, string b) /// Returns an enumerator that iterates through the collection. /// /// An object that can be used to iterate through the collection. + /// Throws if tree is missing public virtual IEnumerator GetEnumerator() { - using (var obj = new ObjectSafeWrapper(Id, repo.Handle)) + using (var obj = new ObjectSafeWrapper(Id, repo.Handle, throwIfMissing: true)) { for (uint i = 0; i < Count; i++) { yield return byIndex(obj, i, Id, repo, path); @@ -117,6 +120,7 @@ public virtual IEnumerator GetEnumerator() /// Returns an enumerator that iterates through the collection. /// /// An object that can be used to iterate through the collection. + /// Throws if tree is missing IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator();