private static int ReadStream( out IntPtr stream_out, IntPtr backend, ref GitOid oid) { stream_out = IntPtr.Zero; OdbBackend odbBackend = MarshalOdbBackend(backend); if (odbBackend == null) { return((int)GitErrorCode.Error); } try { OdbBackendStream stream; int toReturn = odbBackend.ReadStream(new ObjectId(oid), out stream); if (toReturn == 0) { stream_out = stream.GitOdbBackendStreamPointer; } return(toReturn); } catch (Exception ex) { Proxy.giterr_set_str(GitErrorCategory.Odb, ex); return((int)GitErrorCode.Error); } }
private static int ExistsPrefix( ref GitOid found_oid, IntPtr backend, ref GitOid short_oid, UIntPtr len) { OdbBackend odbBackend = MarshalOdbBackend(backend); if (odbBackend == null) { return((int)GitErrorCode.Error); } try { ObjectId found; var shortSha = ObjectId.ToString(short_oid.Id, (int)len); found_oid.Id = ObjectId.Zero.RawId; int result = odbBackend.ExistsPrefix(shortSha, out found); if (result == (int)GitErrorCode.Ok) { found_oid.Id = found.RawId; } return(result); } catch (Exception ex) { Proxy.giterr_set_str(GitErrorCategory.Odb, ex); return((int)GitErrorCode.Error); } }
/// <summary> /// Deletes the note on the specified object, and for the given namespace. /// </summary> /// <param name = "targetId">The target <see cref = "ObjectId"/>, for which the note will be created.</param> /// <param name = "author">The author.</param> /// <param name = "committer">The committer.</param> /// <param name = "namespace">The namespace on which the note will be removed. It can be either a canonical namespace or an abbreviated namespace ('refs/notes/myNamespace' or just 'myNamespace').</param> public void Delete(ObjectId targetId, Signature author, Signature committer, string @namespace) { Ensure.ArgumentNotNull(targetId, "targetId"); Ensure.ArgumentNotNull(author, "author"); Ensure.ArgumentNotNull(committer, "committer"); Ensure.ArgumentNotNullOrEmptyString(@namespace, "@namespace"); string canonicalNamespace = NormalizeToCanonicalName(@namespace); GitOid oid = targetId.Oid; int res; using (SignatureSafeHandle authorHandle = author.BuildHandle()) using (SignatureSafeHandle committerHandle = committer.BuildHandle()) { res = NativeMethods.git_note_remove(repo.Handle, canonicalNamespace, authorHandle, committerHandle, ref oid); } if (res == (int)GitErrorCode.GIT_ENOTFOUND) { return; } Ensure.Success(res); }
/// <summary> /// Try to lookup an object by its <see cref = "ObjectId" /> and <see cref = "GitObjectType" />. If no matching object is found, null will be returned. /// </summary> /// <param name = "id">The id to lookup.</param> /// <param name = "type">The kind of GitObject being looked up</param> /// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns> public GitObject Lookup(ObjectId id, GitObjectType type = GitObjectType.Any) { Ensure.ArgumentNotNull(id, "id"); GitOid oid = id.Oid; IntPtr obj; int res; if (id is AbbreviatedObjectId) { res = NativeMethods.git_object_lookup_prefix(out obj, handle, ref oid, (uint)((AbbreviatedObjectId)id).Length, type); } else { res = NativeMethods.git_object_lookup(out obj, handle, ref oid, type); } if (res == (int)GitErrorCode.GIT_ENOTFOUND || res == (int)GitErrorCode.GIT_EINVALIDTYPE) { return(null); } Ensure.Success(res); if (id is AbbreviatedObjectId) { id = GitObject.ObjectIdOf(obj); } return(GitObject.CreateFromPtr(obj, id, this)); }
private static int ReadStream( out IntPtr stream_out, IntPtr backend, ref GitOid oid) { stream_out = IntPtr.Zero; OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend; if (odbBackend != null) { OdbBackendStream stream; try { int toReturn = odbBackend.ReadStream(oid.Id, out stream); if (0 == toReturn) { stream_out = stream.GitOdbBackendStreamPointer; } return(toReturn); } catch (Exception ex) { Proxy.giterr_set_str(GitErrorCategory.Odb, ex); } } return((int)GitErrorCode.Error); }
private static unsafe int Write( IntPtr backend, ref GitOid oid, IntPtr data, UIntPtr len, GitObjectType type) { long length = ConverToLong(len); OdbBackend odbBackend = MarshalOdbBackend(backend); if (odbBackend == null) { return((int)GitErrorCode.Error); } try { using (var stream = new UnmanagedMemoryStream((byte *)data, length)) { return(odbBackend.Write(new ObjectId(oid), stream, length, type.ToObjectType())); } } catch (Exception ex) { Proxy.giterr_set_str(GitErrorCategory.Odb, ex); return((int)GitErrorCode.Error); } }
private static int FinalizeWrite( out GitOid oid_p, IntPtr stream) { oid_p = default(GitOid); OdbBackendStream odbBackendStream = GCHandle.FromIntPtr(Marshal.ReadIntPtr(stream, GitOdbBackendStream.GCHandleOffset)).Target as OdbBackendStream; if (odbBackendStream != null) { byte[] computedObjectId; try { int toReturn = odbBackendStream.FinalizeWrite(out computedObjectId); if (0 == toReturn) { oid_p.Id = computedObjectId; } return(toReturn); } catch (Exception ex) { Proxy.giterr_set_str(GitErrorCategory.Odb, ex); } } return((int)GitErrorCode.Error); }
private IEnumerable <Commit> RetrieveParentsOfCommit(GitOid oid) //TODO: Convert to a ParentEnumerator { IntPtr obj; int res = NativeMethods.git_object_lookup(out obj, repo.Handle, ref oid, GitObjectType.Commit); Ensure.Success(res); var parentsOfCommits = new List <Commit>(); try { uint parentsCount = NativeMethods.git_commit_parentcount(obj); for (uint i = 0; i < parentsCount; i++) { IntPtr parentCommit; res = NativeMethods.git_commit_parent(out parentCommit, obj, i); Ensure.Success(res); parentsOfCommits.Add((Commit)CreateFromPtr(parentCommit, ObjectIdOf(parentCommit), repo)); } } finally { NativeMethods.git_object_close(obj); } return(parentsOfCommits); }
private unsafe static int Read( out IntPtr buffer_p, out UIntPtr len_p, out GitObjectType type_p, IntPtr backend, ref GitOid oid) { buffer_p = IntPtr.Zero; len_p = UIntPtr.Zero; type_p = GitObjectType.Bad; OdbBackend odbBackend = MarshalOdbBackend(backend); if (odbBackend == null) { return((int)GitErrorCode.Error); } Stream dataStream = null; try { ObjectType objectType; int toReturn = odbBackend.Read(new ObjectId(oid), out dataStream, out objectType); if (toReturn != 0) { return(toReturn); } // Caller is expected to give us back a stream created with the Allocate() method. var memoryStream = dataStream as UnmanagedMemoryStream; if (memoryStream == null) { return((int)GitErrorCode.Error); } len_p = new UIntPtr((ulong)memoryStream.Capacity); type_p = objectType.ToGitObjectType(); memoryStream.Seek(0, SeekOrigin.Begin); buffer_p = new IntPtr(memoryStream.PositionPointer); } catch (Exception ex) { Proxy.giterr_set_str(GitErrorCategory.Odb, ex); return((int)GitErrorCode.Error); } finally { if (dataStream != null) { dataStream.Dispose(); } } return((int)GitErrorCode.Ok); }
private int CallbackMethod(byte[] oid) { GitOid gitOid = new GitOid(); gitOid.Id = oid; return(cb(ref gitOid, data)); }
private int CreateDirectReference(string name, ObjectId targetId, bool allowOverwrite, out IntPtr reference) { targetId = Unabbreviate(targetId); GitOid oid = targetId.Oid; return(NativeMethods.git_reference_create_oid(out reference, repo.Handle, name, ref oid, allowOverwrite)); }
internal AbbreviatedObjectId(GitOid oid, int length) : base(oid) { if (length < MinHexSize || length > HexSize) { throw new ArgumentException(string.Format("Expected length should be comprised between {0} and {1}.", MinHexSize, HexSize), "length"); } Length = length; }
///<summary> /// Tells if the specified sha exists in the repository. /// /// Exceptions: /// ArgumentException /// ArgumentNullException ///</summary> ///<param name = "sha">The sha.</param> ///<returns></returns> public bool HasObject(string sha) //TODO: To be removed from front facing API (maybe should we create an Repository.Advanced to hold those kind of functions)? { var id = new ObjectId(sha); IntPtr odb = NativeMethods.git_repository_database(handle); GitOid oid = id.Oid; return(NativeMethods.git_odb_exists(odb, ref oid)); }
private static GitOid MarshalAsGitOid(System.IntPtr data) { var gitOid = new GitOid { Id = new byte[GitOid.Size] }; Marshal.Copy(data, gitOid.Id, 0, GitOid.Size); return(gitOid); }
private unsafe static int Read( out IntPtr buffer_p, out UIntPtr len_p, out GitObjectType type_p, IntPtr backend, ref GitOid oid) { buffer_p = IntPtr.Zero; len_p = UIntPtr.Zero; type_p = GitObjectType.Bad; OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend; if (odbBackend != null) { Stream dataStream = null; GitObjectType objectType; try { int toReturn = odbBackend.Read(oid.Id, out dataStream, out objectType); if (0 == toReturn) { // Caller is expected to give us back a stream created with the Allocate() method. UnmanagedMemoryStream memoryStream = dataStream as UnmanagedMemoryStream; if (null == memoryStream) { return((int)GitErrorCode.Error); } len_p = new UIntPtr((ulong)memoryStream.Capacity); type_p = objectType; memoryStream.Seek(0, SeekOrigin.Begin); buffer_p = new IntPtr(memoryStream.PositionPointer); } return(toReturn); } catch (Exception ex) { Proxy.giterr_set_str(GitErrorCategory.Odb, ex); } finally { if (null != dataStream) { dataStream.Dispose(); } } } return((int)GitErrorCode.Error); }
/// <summary> /// Initializes a new instance of the <see cref="ObjectId"/> class. /// </summary> /// <param name="oid">The oid.</param> internal ObjectId(GitOid oid) { if (oid.Id == null || oid.Id.Length != rawSize) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "A non null array of {0} bytes is expected.", rawSize), "oid"); } this.oid = oid; sha = ToString(oid.Id, oid.Id.Length * 2); }
private void InternalHidePush(IList <object> identifier, HidePushSignature hidePush) { IEnumerable <ObjectId> oids = RetrieveCommitOids(identifier).TakeWhile(o => o != null); foreach (ObjectId actedOn in oids) { GitOid oid = actedOn.Oid; int res = hidePush(handle, ref oid); Ensure.Success(res); } }
private ReferenceSafeHandle CreateDirectReference(string name, ObjectId targetId, bool allowOverwrite) { targetId = Unabbreviate(targetId); GitOid oid = targetId.Oid; ReferenceSafeHandle handle; Ensure.Success(NativeMethods.git_reference_create_oid(out handle, repo.Handle, name, ref oid, allowOverwrite)); return(handle); }
/// <summary> /// Initializes a new instance of the <see cref="ObjectId"/> class. /// </summary> /// <param name="sha">The sha.</param> public ObjectId(string sha) { GitOid?parsedOid = BuildOidFrom(sha, true); if (!parsedOid.HasValue) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "'{0}' is not a valid Sha-1.", sha)); } oid = parsedOid.Value; this.sha = sha; }
/// <summary> /// Handler for libgit2 update_tips callback. Converts values /// received from libgit2 callback to more suitable types /// and calls delegate provided by LibGit2Sharp consumer. /// </summary> /// <param name="str">IntPtr to string</param> /// <param name="oldId">Old reference ID</param> /// <param name="newId">New referene ID</param> /// <param name="data">IntPtr to optional payload passed back to the callback.</param> /// <returns>0 on success; a negative value to abort the process.</returns> private int GitUpdateTipsHandler(IntPtr str, ref GitOid oldId, ref GitOid newId, IntPtr data) { UpdateTipsHandler onUpdateTips = UpdateTips; bool shouldContinue = true; if (onUpdateTips != null) { string refName = LaxUtf8Marshaler.FromNative(str); shouldContinue = onUpdateTips(refName, oldId, newId); } return(Proxy.ConvertResultToCancelFlag(shouldContinue)); }
/// <summary> /// Handler for libgit2 update_tips callback. Converts values /// received from libgit2 callback to more suitable types /// and calls delegate provided by LibGit2Sharp consumer. /// </summary> /// <param name="str">IntPtr to string</param> /// <param name="oldId">Old reference ID</param> /// <param name="newId">New referene ID</param> /// <param name="data"></param> /// <returns></returns> private int GitUpdateTipsHandler(IntPtr str, ref GitOid oldId, ref GitOid newId, IntPtr data) { UpdateTipsHandler onUpdateTips = UpdateTips; int result = 0; if (onUpdateTips != null) { string refName = Utf8Marshaler.FromNative(str); result = onUpdateTips(refName, oldId, newId); } return(result); }
/// <summary> /// Updates the target on a reference. /// </summary> /// <param name = "name">The name of the reference.</param> /// <param name = "target">The target which can be either a sha or the name of another reference.</param> public Reference UpdateTarget(string name, string target) { Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNullOrEmptyString(target, "target"); if (name == "HEAD") { return(Create("HEAD", target, true)); } using (ReferenceSafeHandle referencePtr = RetrieveReferencePtr(name)) { int res; ObjectId id; bool isObjectIdentifier = ObjectId.TryParse(target, out id); GitReferenceType type = NativeMethods.git_reference_type(referencePtr); switch (type) { case GitReferenceType.Oid: if (!isObjectIdentifier) { throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The reference specified by {0} is an Oid reference, you must provide a sha as the target.", name), "target"); } GitOid oid = id.Oid; res = NativeMethods.git_reference_set_oid(referencePtr, ref oid); break; case GitReferenceType.Symbolic: if (isObjectIdentifier) { throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The reference specified by {0} is a Symbolic reference, you must provide a reference canonical name as the target.", name), "target"); } res = NativeMethods.git_reference_set_target(referencePtr, target); break; default: throw new LibGit2Exception(string.Format(CultureInfo.InvariantCulture, "Reference '{0}' has an unexpected type ('{1}').", name, Enum.GetName(typeof(GitReferenceType), type))); } Ensure.Success(res); return(Reference.BuildFromPtr <Reference>(referencePtr, repo)); } }
private NoteSafeHandle BuildNoteSafeHandle(ObjectId id, string canonicalNamespace) { NoteSafeHandle noteHandle; GitOid oid = id.Oid; int res = NativeMethods.git_note_read(out noteHandle, repo.Handle, canonicalNamespace, ref oid); if (res == (int)GitErrorCode.GIT_ENOTFOUND) { return(null); } Ensure.Success(res); return(noteHandle); }
internal unsafe ObjectId(byte *rawId) { byte[] id = new byte[GitOid.Size]; fixed(byte *p = id) { for (int i = 0; i < rawSize; i++) { p[i] = rawId[i]; } } this.oid = new GitOid { Id = id }; this.sha = ToString(oid.Id, oid.Id.Length * 2); }
/// <summary> /// Inserts a <see cref="Blob"/> into the object database, created from the content of a file. /// </summary> /// <param name="path">Path to the file to create the blob from.</param> /// <returns>The created <see cref="Blob"/>.</returns> public Blob CreateBlob(string path) { Ensure.ArgumentNotNullOrEmptyString(path, "path"); var oid = new GitOid(); if (!repo.Info.IsBare && !Path.IsPathRooted(path)) { Ensure.Success(NativeMethods.git_blob_create_fromfile(ref oid, repo.Handle, path)); } else { Ensure.Success(NativeMethods.git_blob_create_fromdisk(ref oid, repo.Handle, path)); } return(repo.Lookup <Blob>(new ObjectId(oid))); }
private static int FinalizeWrite(IntPtr stream, ref GitOid oid) { OdbBackendStream odbBackendStream = GCHandle.FromIntPtr(Marshal.ReadIntPtr(stream, GitOdbBackendStream.GCHandleOffset)).Target as OdbBackendStream; if (odbBackendStream != null) { try { return(odbBackendStream.FinalizeWrite(new ObjectId(oid))); } catch (Exception ex) { Proxy.git_error_set_str(GitErrorCategory.Odb, ex); } } return((int)GitErrorCode.Error); }
private static bool Exists( IntPtr backend, ref GitOid oid) { OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend; if (odbBackend != null) { try { return(odbBackend.Exists(oid.Id)); } catch (Exception ex) { Proxy.giterr_set_str(GitErrorCategory.Odb, ex); } } return(false); }
private static bool Exists( IntPtr backend, ref GitOid oid) { OdbBackend odbBackend = MarshalOdbBackend(backend); if (odbBackend == null) { return(false); // Weird } try { return(odbBackend.Exists(new ObjectId(oid))); } catch (Exception ex) { Proxy.giterr_set_str(GitErrorCategory.Odb, ex); return(false); } }
private static unsafe int Write( ref GitOid oid, IntPtr backend, IntPtr data, UIntPtr len, GitObjectType type) { OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend; ObjectType objectType = type.ToObjectType(); if (odbBackend != null && len.ToUInt64() < long.MaxValue) { try { using (UnmanagedMemoryStream stream = new UnmanagedMemoryStream((byte *)data, (long)len.ToUInt64())) { byte[] finalOid; int toReturn = odbBackend.Write(oid.Id, stream, (long)len.ToUInt64(), objectType, out finalOid); if (0 == toReturn) { oid.Id = finalOid; } return(toReturn); } } catch (Exception ex) { Proxy.giterr_set_str(GitErrorCategory.Odb, ex); } } return((int)GitErrorCode.Error); }
private static int ReadHeader( out UIntPtr len_p, out GitObjectType type_p, IntPtr backend, ref GitOid oid) { len_p = UIntPtr.Zero; type_p = GitObjectType.Bad; OdbBackend odbBackend = MarshalOdbBackend(backend); if (odbBackend == null) { return((int)GitErrorCode.Error); } try { int length; ObjectType objectType; int toReturn = odbBackend.ReadHeader(new ObjectId(oid), out length, out objectType); if (toReturn != 0) { return(toReturn); } len_p = new UIntPtr((uint)length); type_p = objectType.ToGitObjectType(); } catch (Exception ex) { Proxy.giterr_set_str(GitErrorCategory.Odb, ex); return((int)GitErrorCode.Error); } return((int)GitErrorCode.Ok); }
private static GitOid MarshalAsGitOid(System.IntPtr data) { var gitOid = new GitOid { Id = new byte[GitOid.Size] }; Marshal.Copy(data, gitOid.Id, 0, GitOid.Size); return gitOid; }