diff --git a/LibGit2Sharp/Core/GitOid.cs b/LibGit2Sharp/Core/GitOid.cs index 6b33d5200..027335719 100644 --- a/LibGit2Sharp/Core/GitOid.cs +++ b/LibGit2Sharp/Core/GitOid.cs @@ -12,5 +12,15 @@ internal struct GitOid /// [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] public byte[] Id; + + public static implicit operator ObjectId(GitOid oid) + { + return new ObjectId(oid); + } + + public static implicit operator ObjectId(GitOid? oid) + { + return oid == null ? null : new ObjectId(oid.Value); + } } } diff --git a/LibGit2Sharp/Core/Handles/OidSafeHandle.cs b/LibGit2Sharp/Core/Handles/OidSafeHandle.cs index d58bcc92d..a88673a4b 100644 --- a/LibGit2Sharp/Core/Handles/OidSafeHandle.cs +++ b/LibGit2Sharp/Core/Handles/OidSafeHandle.cs @@ -4,14 +4,14 @@ namespace LibGit2Sharp.Core.Handles { internal class OidSafeHandle : NotOwnedSafeHandleBase { - private GitOid MarshalAsGitOid() + private GitOid? MarshalAsGitOid() { - return (GitOid)Marshal.PtrToStructure(handle, typeof(GitOid)); + return (GitOid?)Marshal.PtrToStructure(handle, typeof(GitOid)); } public ObjectId MarshalAsObjectId() { - return new ObjectId(MarshalAsGitOid()); + return MarshalAsGitOid(); } } } diff --git a/LibGit2Sharp/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs index a162f9d39..7235d8f72 100644 --- a/LibGit2Sharp/Core/Proxy.cs +++ b/LibGit2Sharp/Core/Proxy.cs @@ -45,7 +45,7 @@ public static ObjectId git_blob_create_fromchunks(RepositorySafeHandle repo, Fil int res = NativeMethods.git_blob_create_fromchunks(ref oid, repo, hintpath, fileCallback, IntPtr.Zero); Ensure.ZeroResult(res); - return new ObjectId(oid); + return oid; } } @@ -57,7 +57,7 @@ public static ObjectId git_blob_create_fromdisk(RepositorySafeHandle repo, FileP int res = NativeMethods.git_blob_create_fromdisk(ref oid, repo, path); Ensure.ZeroResult(res); - return new ObjectId(oid); + return oid; } } @@ -69,7 +69,7 @@ public static ObjectId git_blob_create_fromfile(RepositorySafeHandle repo, FileP int res = NativeMethods.git_blob_create_fromworkdir(ref oid, repo, path); Ensure.ZeroResult(res); - return new ObjectId(oid); + return oid; } } @@ -265,7 +265,7 @@ public static ObjectId git_commit_create( committerHandle, encoding, prettifiedMessage, treePtr.ObjectPtr, parentObjectPtrs.Count, parentsPtrs); Ensure.ZeroResult(res); - return new ObjectId(commitOid); + return commitOid; } } @@ -769,7 +769,7 @@ public static void git_index_write(IndexSafeHandle index) } } - public static GitOid git_tree_create_fromindex(Index index) + public static ObjectId git_tree_create_fromindex(Index index) { using (ThreadAffinity()) { @@ -801,7 +801,7 @@ public static ObjectId git_merge_base(RepositorySafeHandle repo, Commit first, C Ensure.ZeroResult(res); - return new ObjectId(ret); + return ret; } } @@ -848,7 +848,7 @@ public static ObjectId git_note_create( int res = NativeMethods.git_note_create(out noteOid, repo, authorHandle, committerHandle, notes_ref, ref oid, note, force ? 1 : 0); Ensure.ZeroResult(res); - return new ObjectId(noteOid); + return noteOid; } } @@ -1699,7 +1699,7 @@ public static ObjectId git_revwalk_next(RevWalkerSafeHandle walker) Ensure.ZeroResult(res); - return new ObjectId(ret); + return ret; } } @@ -1798,7 +1798,7 @@ public static ObjectId git_tag_create( int res = NativeMethods.git_tag_create(out oid, repo, name, objectPtr.ObjectPtr, taggerHandle, message, allowOverwrite); Ensure.ZeroResult(res); - return new ObjectId(oid); + return oid; } } @@ -1811,7 +1811,7 @@ public static ObjectId git_tag_create_lightweight(RepositorySafeHandle repo, str int res = NativeMethods.git_tag_create_lightweight(out oid, repo, name, objectPtr.ObjectPtr, allowOverwrite); Ensure.ZeroResult(res); - return new ObjectId(oid); + return oid; } } @@ -1958,7 +1958,7 @@ public static ObjectId git_treebuilder_write(RepositorySafeHandle repo, TreeBuil int res = NativeMethods.git_treebuilder_write(out oid, repo, bld); Ensure.ZeroResult(res); - return new ObjectId(oid); + return oid; } } diff --git a/LibGit2Sharp/IndexEntry.cs b/LibGit2Sharp/IndexEntry.cs index ca7790e82..01991dcd2 100644 --- a/LibGit2Sharp/IndexEntry.cs +++ b/LibGit2Sharp/IndexEntry.cs @@ -61,7 +61,7 @@ internal static IndexEntry BuildFromPtr(Repository repo, IndexEntrySafeHandle ha return new IndexEntry { Path = path.Native, - Id = new ObjectId(entry.oid), + Id = entry.oid, state = () => repo.Index.RetrieveStatus(path.Native), StageLevel = Proxy.git_index_entry_stage(handle), Mode = (Mode)entry.Mode diff --git a/LibGit2Sharp/Network.cs b/LibGit2Sharp/Network.cs index bf7e64ffd..c45c8ee5f 100644 --- a/LibGit2Sharp/Network.cs +++ b/LibGit2Sharp/Network.cs @@ -40,7 +40,7 @@ public virtual IEnumerable FetchHeads return Proxy.git_repository_fetchhead_foreach( repository.Handle, - (name, url, oid, isMerge) => new FetchHead(repository, name, url, new ObjectId(oid), isMerge, i++)); + (name, url, oid, isMerge) => new FetchHead(repository, name, url, oid, isMerge, i++)); } } @@ -76,7 +76,7 @@ public virtual IEnumerable ListReferences(Remote remote) return -1; } - ObjectId oid = new ObjectId(remoteHead.Oid); + ObjectId oid = remoteHead.Oid; string name = Utf8Marshaler.FromNative(remoteHead.NamePtr); directReferences.Add(new DirectReference(name, this.repository, oid)); diff --git a/LibGit2Sharp/NoteCollection.cs b/LibGit2Sharp/NoteCollection.cs index a49fa6d30..5341f0595 100644 --- a/LibGit2Sharp/NoteCollection.cs +++ b/LibGit2Sharp/NoteCollection.cs @@ -114,7 +114,7 @@ public virtual IEnumerable this[string @namespace] string canonicalNamespace = NormalizeToCanonicalName(@namespace); return Proxy.git_note_foreach(repo.Handle, canonicalNamespace, - (blobId,annotatedObjId) => RetrieveNote(new ObjectId(annotatedObjId), canonicalNamespace)); + (blobId,annotatedObjId) => RetrieveNote(annotatedObjId, canonicalNamespace)); } } diff --git a/LibGit2Sharp/ObjectId.cs b/LibGit2Sharp/ObjectId.cs index 792ef4a5f..08fa11293 100644 --- a/LibGit2Sharp/ObjectId.cs +++ b/LibGit2Sharp/ObjectId.cs @@ -98,7 +98,7 @@ public virtual string Sha /// true if the parameter was converted successfully; otherwise, false. public static bool TryParse(string sha, out ObjectId result) { - result = BuildFrom(sha, false); + result = BuildOidFrom(sha, false); return result != null; } @@ -113,20 +113,6 @@ public static bool TryParse(string sha, out ObjectId result) return ToOid(sha); } - private static ObjectId BuildFrom(string sha, bool shouldThrowIfInvalid) - { - GitOid? oid = BuildOidFrom(sha, shouldThrowIfInvalid); - - if (!oid.HasValue) - { - return null; - } - - var objectId = new ObjectId(oid.Value); - - return objectId; - } - /// /// Determines whether the specified is equal to the current . /// diff --git a/LibGit2Sharp/RemoteCallbacks.cs b/LibGit2Sharp/RemoteCallbacks.cs index c5bc8e0e2..3cc40cfae 100644 --- a/LibGit2Sharp/RemoteCallbacks.cs +++ b/LibGit2Sharp/RemoteCallbacks.cs @@ -99,7 +99,7 @@ private int GitUpdateTipsHandler(IntPtr str, ref GitOid oldId, ref GitOid newId, if (onUpdateTips != null) { string refName = Utf8Marshaler.FromNative(str); - result = onUpdateTips(refName, new ObjectId(oldId), new ObjectId(newId)); + result = onUpdateTips(refName, oldId, newId); } return result; diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index f17276a6a..23d7c5954 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -662,8 +662,8 @@ public Commit Commit(string message, Signature author, Signature committer, bool throw new LibGit2SharpException("Can not amend anything. The Head doesn't point at any commit."); } - GitOid treeOid = Proxy.git_tree_create_fromindex(Index); - var tree = this.Lookup(new ObjectId(treeOid)); + var treeId = Proxy.git_tree_create_fromindex(Index); + var tree = this.Lookup(treeId); var parents = RetrieveParentsOfTheCommitBeingCreated(amendPreviousCommit); @@ -774,7 +774,7 @@ public virtual IEnumerable MergeHeads { int i = 0; return Proxy.git_repository_mergehead_foreach(Handle, - commitId => new MergeHead(this, new ObjectId(commitId), i++)); + commitId => new MergeHead(this, commitId, i++)); } } diff --git a/LibGit2Sharp/TreeChanges.cs b/LibGit2Sharp/TreeChanges.cs index a162b36a5..359134f7e 100644 --- a/LibGit2Sharp/TreeChanges.cs +++ b/LibGit2Sharp/TreeChanges.cs @@ -95,8 +95,8 @@ private TreeEntryChanges AddFileChange(GitDiffDelta delta, GitDiffLineOrigin lin var oldFilePath = FilePathMarshaler.FromNative(delta.OldFile.Path); var newMode = (Mode)delta.NewFile.Mode; var oldMode = (Mode)delta.OldFile.Mode; - var newOid = new ObjectId(delta.NewFile.Oid); - var oldOid = new ObjectId(delta.OldFile.Oid); + var newOid = delta.NewFile.Oid; + var oldOid = delta.OldFile.Oid; if (delta.Status == ChangeKind.Untracked) {