-
Notifications
You must be signed in to change notification settings - Fork 467
Use git_odb_exists instead of git_revparse_single for ObjectExists #2006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tyrielv
wants to merge
1
commit into
microsoft:master
Choose a base branch
from
tyrielv:tyrielv/odb-exists
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+72
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ namespace GVFS.Common.Git | |
| public class LibGit2Repo : IDisposable | ||
| { | ||
| private bool disposedValue = false; | ||
| private IntPtr odbHandle = IntPtr.Zero; | ||
|
|
||
| public delegate void MultiVarConfigCallback(string value); | ||
|
|
||
|
|
@@ -104,6 +105,42 @@ public virtual bool CommitAndRootTreeExists(string commitish, out string treeSha | |
| } | ||
|
|
||
| public virtual bool ObjectExists(string sha) | ||
| { | ||
| if (this.odbHandle == IntPtr.Zero) | ||
| { | ||
| if (Native.Odb.GetOdb(out this.odbHandle, this.RepoHandle) != Native.ResultCode.Success) | ||
| { | ||
| return this.ObjectExistsFallback(sha); | ||
| } | ||
| } | ||
|
|
||
| GitOid oid; | ||
| if (Native.Odb.OidFromStr(out oid, sha) != Native.ResultCode.Success) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ObjectExists now only works for full 40-char SHAs; refs or abbreviated SHAs (like FastFetch's raw --commit value) that revparse used to resolve will now be reported as missing. |
||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return Native.Odb.Exists(this.odbHandle, ref oid) == 1; | ||
| } | ||
|
|
||
| private bool ObjectExistsFallback(string sha) | ||
| { | ||
| IntPtr objHandle; | ||
| if (Native.RevParseSingle(out objHandle, this.RepoHandle, sha) != Native.ResultCode.Success) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| Native.Object.Free(objHandle); | ||
| return true; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Checks whether the object can be fully parsed by libgit2 (not just that it exists). | ||
| /// Use this when you need to detect corrupt objects. For simple existence checks, | ||
| /// prefer <see cref="ObjectExists"/> which is faster. | ||
| /// </summary> | ||
| public virtual bool ObjectCanBeParsed(string sha) | ||
| { | ||
| IntPtr objHandle; | ||
| if (Native.RevParseSingle(out objHandle, this.RepoHandle, sha) != Native.ResultCode.Success) | ||
|
|
@@ -360,6 +397,12 @@ protected virtual void Dispose(bool disposing) | |
| { | ||
| if (!this.disposedValue) | ||
| { | ||
| if (this.odbHandle != IntPtr.Zero) | ||
| { | ||
| Native.Odb.Free(this.odbHandle); | ||
| this.odbHandle = IntPtr.Zero; | ||
| } | ||
|
|
||
| Native.Repo.Free(this.RepoHandle); | ||
| Native.Shutdown(); | ||
| this.disposedValue = true; | ||
|
|
@@ -504,6 +547,22 @@ public static class Repo | |
| public static extern void Free(IntPtr repoHandle); | ||
| } | ||
|
|
||
| public static class Odb | ||
| { | ||
| [DllImport(Git2NativeLibName, EntryPoint = "git_repository_odb")] | ||
| public static extern ResultCode GetOdb(out IntPtr odbHandle, IntPtr repoHandle); | ||
|
|
||
| /// <returns>1 if the object exists, 0 otherwise</returns> | ||
| [DllImport(Git2NativeLibName, EntryPoint = "git_odb_exists")] | ||
| public static extern int Exists(IntPtr odbHandle, ref GitOid id); | ||
|
|
||
| [DllImport(Git2NativeLibName, EntryPoint = "git_odb_free")] | ||
| public static extern void Free(IntPtr odbHandle); | ||
|
|
||
| [DllImport(Git2NativeLibName, EntryPoint = "git_oid_fromstr")] | ||
| public static extern ResultCode OidFromStr(out GitOid oid, string str); | ||
| } | ||
|
|
||
| public static class Config | ||
| { | ||
| [DllImport(Git2NativeLibName, EntryPoint = "git_repository_config")] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The shared LibGit2Repo can run ObjectExists on multiple threads at once, and the lazy ODB-handle setup has no lock, so two first-time calls could each acquire the odb and leak one reference.