Пример #1
0
 public GitRef(string commit, GitObjectType objectType, string name, string subject)
 {
     Commit     = commit;
     ObjectType = objectType;
     Name       = name;
     Subject    = subject;
 }
Пример #2
0
        private static ObjectHash GetObjectHash(GitObjectType type, byte[] contentBytes)
        {
            var bytesWithHeader = GetBytesWithHeader(type, contentBytes);
            var hash            = new ObjectHash(Hash.Create(bytesWithHeader));

            return(hash);
        }
Пример #3
0
            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);
                }
            }
Пример #4
0
        internal static async ValueTask <GitObject> FromBucketAsync(GitRepository repository, GitObjectBucket rdr, GitId id, GitObjectType type = GitObjectType.None)
        {
            GitObjectType tp;

            if (type == GitObjectType.None)
            {
                tp = await rdr.ReadTypeAsync().ConfigureAwait(false);
            }
            else
            {
                tp = type;
            }

            switch (tp)
            {
            case GitObjectType.Commit:
                return(new GitCommit(repository, rdr, id));

            case GitObjectType.Tree:
                return(new GitTree(repository, rdr, id));

            case GitObjectType.Blob:
                return(new GitBlob(repository, rdr, id));

            case GitObjectType.Tag:
                return(new GitTagObject(repository, rdr, id));

            default:
                throw new ArgumentOutOfRangeException(nameof(type));
            }
        }
Пример #5
0
        public static byte[] GetBytesWithHeader(GitObjectType type, byte[] contentBytes)
        {
            string header;

            if (type == GitObjectType.Commit)
            {
                header = "commit " + contentBytes.Length + '\0';
            }
            else if (type == GitObjectType.Tag)
            {
                header = "tag " + contentBytes.Length + '\0';
            }
            else if (type == GitObjectType.Tree)
            {
                header = "tree " + contentBytes.Length + '\0';
            }
            else if (type == GitObjectType.Blob)
            {
                header = "blob " + contentBytes.Length + '\0';
            }
            else
            {
                throw new NotImplementedException();
            }

            var headerBytes  = Encoding.ASCII.GetBytes(header);
            var resultBuffer = new byte[headerBytes.Length + contentBytes.Length];

            Array.Copy(headerBytes, resultBuffer, headerBytes.Length);
            Array.Copy(contentBytes, 0, resultBuffer, headerBytes.Length, contentBytes.Length);

            return(resultBuffer);
        }
Пример #6
0
 public MockOdbBackendStream(MockOdbBackend backend, GitObjectType objectType, long length)
     : base(backend)
 {
     m_type   = objectType;
     m_length = length;
     m_hash   = new SHA1CryptoServiceProvider();
 }
Пример #7
0
            public override int Write(byte[] oid, Stream dataStream, long length, GitObjectType objectType, out byte[] finalOid)
            {
                using (var sha1 = new SHA1CryptoServiceProvider())
                {
                    finalOid = sha1.ComputeHash(dataStream);

                    dataStream.Seek(0, SeekOrigin.Begin);
                }

                if (m_objectIdToContent.ContainsKey(finalOid))
                {
                    return(GIT_EEXISTS);
                }

                if (length > (long)int.MaxValue)
                {
                    return(GIT_ERROR);
                }

                byte[] buffer    = new byte[length];
                int    bytesRead = dataStream.Read(buffer, 0, (int)length);

                if (bytesRead != (int)length)
                {
                    return(GIT_ERROR);
                }

                m_objectIdToContent.Add(finalOid, new MockGitObject(finalOid, objectType, buffer));

                return(GIT_OK);
            }
Пример #8
0
        internal static GitObject CreateFromPtr(IntPtr obj, ObjectId id, Repository repo)
        {
            try
            {
                GitObjectType type = NativeMethods.git_object_type(obj);
                switch (type)
                {
                case GitObjectType.Commit:
                    return(Commit.BuildFromPtr(obj, id, repo));

                case GitObjectType.Tree:
                    return(Tree.BuildFromPtr(obj, id, repo));

                case GitObjectType.Tag:
                    return(TagAnnotation.BuildFromPtr(obj, id, repo));

                case GitObjectType.Blob:
                    return(Blob.BuildFromPtr(obj, id, repo));

                default:
                    throw new LibGit2Exception(string.Format(CultureInfo.InvariantCulture, "Unexpected type '{0}' for object '{1}'.", type, id));
                }
            }
            finally
            {
                NativeMethods.git_object_close(obj);
            }
        }
Пример #9
0
            private static int WriteStream(
                out IntPtr stream_out,
                IntPtr backend,
                UIntPtr length,
                GitObjectType type)
            {
                stream_out = IntPtr.Zero;

                OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend;

                if (odbBackend != null &&
                    length.ToUInt64() < long.MaxValue)
                {
                    OdbBackendStream stream;

                    try
                    {
                        int toReturn = odbBackend.WriteStream((long)length.ToUInt64(), type, 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);
            }
Пример #10
0
 public GitItem(int mode, GitObjectType objectType, string guid, string name)
 {
     Mode       = mode;
     ObjectType = objectType;
     Guid       = guid;
     FileName   = Name = name;
 }
Пример #11
0
            private static int WriteStream(
                out IntPtr stream_out,
                IntPtr backend,
                long len,
                GitObjectType type)
            {
                stream_out = IntPtr.Zero;

                OdbBackend odbBackend = MarshalOdbBackend(backend);

                if (odbBackend == null)
                {
                    return((int)GitErrorCode.Error);
                }

                ObjectType objectType = type.ToObjectType();

                try
                {
                    OdbBackendStream stream;
                    int toReturn = odbBackend.WriteStream(len, objectType, 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);
                }
            }
Пример #12
0
        /// <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));
        }
Пример #13
0
 public GitItem(int mode, GitObjectType objectType, ObjectId objectId, string name)
 {
     Mode       = mode;
     ObjectType = objectType;
     ObjectId   = objectId;
     FileName   = Name = name;
 }
Пример #14
0
        protected GitObjectBase(ObjectHash hash, GitObjectType type)
        {
            Hash = hash;
            Type = type;

            _hashCode = hash.GetHashCode();
        }
Пример #15
0
            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);
            }
Пример #16
0
            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);
            }
Пример #17
0
        public async ValueTask <(GitId, GitObjectType)> ReadObjectIdAsync()
        {
            if (_objectId is not null)
            {
                return(_objectId, _type);
            }

            var(bb, eol) = await Inner.ReadUntilEolFullAsync(AcceptedEols, null, 7 /* "object " */ + GitId.MaxHashLength * 2 + 2 /* ALL EOL */).ConfigureAwait(false);

            if (bb.IsEof || eol == BucketEol.None || !bb.StartsWithASCII("object "))
            {
                throw new GitBucketException($"Expected 'object' record at start of tag in '{Inner.Name}'");
            }

            if (GitId.TryParse(bb.Slice(7, eol), out var id))
            {
                _objectId = id;
            }
            else
            {
                throw new GitBucketException($"Expected valid 'object' record at start of tag in '{Inner.Name}'");
            }

            (bb, eol) = await Inner.ReadUntilEolFullAsync(AcceptedEols, null, 5 /* "type " */ + 6 /* "commit" */ + 2 /* ALL EOL */).ConfigureAwait(false);

            if (bb.IsEof || eol == BucketEol.None || !bb.StartsWithASCII("type "))
            {
                _objectId = null;
                throw new GitBucketException($"Expected 'type' record of tag in '{Inner.Name}'");
            }

            bb = bb.Slice(5, eol);

            if (bb.EqualsASCII("commit"))
            {
                _type = GitObjectType.Commit;
            }
            else if (bb.EqualsASCII("tree"))
            {
                _type = GitObjectType.Tree;
            }
            else if (bb.EqualsASCII("blob"))
            {
                _type = GitObjectType.Blob;
            }
            else if (bb.EqualsASCII("tag"))
            {
                _type = GitObjectType.Tag;
            }
            else
            {
                throw new GitBucketException($"Expected valid 'type' record in tag in '{Inner.Name}'");
            }

            return(_objectId, _type);
        }
Пример #18
0
                public MockGitObject(byte[] objectId, GitObjectType objectType, byte[] data)
                {
                    if (objectId.Length != 20)
                    {
                        throw new InvalidOperationException();
                    }

                    this.ObjectId   = objectId;
                    this.ObjectType = objectType;
                    this.Data       = data;
                }
Пример #19
0
        public void CanRetrieveEntries(string path, string expectedAttributes, GitObjectType expectedType, string expectedSha)
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree);

                TreeEntryDefinition ted = td[path];

                Assert.Equal(ToMode(expectedAttributes), ted.Mode);
                Assert.Equal(expectedType, ted.Type);
                Assert.Equal(new ObjectId(expectedSha), ted.TargetId);
            }
        }
Пример #20
0
        public override async ValueTask <GitObjectType> ReadTypeAsync()
        {
            await PrepareState(frame_state.type_done).ConfigureAwait(false);

            if ((_type == GitObjectType.None || _type > GitObjectType.Tag) &&
                reader is GitObjectBucket gob)
            {
                return(_type = await gob.ReadTypeAsync().ConfigureAwait(false));
            }

            Debug.Assert(_type >= GitObjectType.Commit && _type <= GitObjectType.Tag, "Bad Git Type");
            return(_type);
        }
Пример #21
0
        public override async ValueTask <GitObjectType> ReadTypeAsync()
        {
            if (_type != GitObjectType.None)
            {
                return(_type);
            }

            if (_inner == null)
            {
                _inner = await Repository.ObjectRepository.ResolveById(Id).ConfigureAwait(false) ?? throw new InvalidOperationException($"Can't fetch {Id}");
            }

            return(_type = await _inner.ReadTypeAsync().ConfigureAwait(false));
        }
Пример #22
0
        internal unsafe TreeEntry(TreeEntryHandle entry, ObjectId parentTreeId, Repository repo, string parentPath)
        {
            this.parentTreeId = parentTreeId;
            this.repo         = repo;
            targetOid         = Proxy.git_tree_entry_id(entry);

            GitObjectType treeEntryTargetType = Proxy.git_tree_entry_type(entry);

            TargetType = treeEntryTargetType.ToTreeEntryTargetType();

            target = new Lazy <GitObject>(RetrieveTreeEntryTarget);

            Mode = Proxy.git_tree_entry_attributes(entry);
            Name = Proxy.git_tree_entry_name(entry);
            path = new Lazy <string>(() => Tree.CombinePath(parentPath, Name));
        }
Пример #23
0
        internal TreeEntry(SafeHandle obj, ObjectId parentTreeId, Repository repo, FilePath parentPath)
        {
            this.parentTreeId = parentTreeId;
            this.repo         = repo;
            targetOid         = Proxy.git_tree_entry_id(obj);

            GitObjectType treeEntryTargetType = Proxy.git_tree_entry_type(obj);

            TargetType = treeEntryTargetType.ToTreeEntryTargetType();

            target = new Lazy <GitObject>(RetrieveTreeEntryTarget);

            Mode = Proxy.git_tree_entry_attributes(obj);
            Name = Proxy.git_tree_entry_name(obj);
            path = new Lazy <string>(() => System.IO.Path.Combine(parentPath.Native, Name));
        }
Пример #24
0
        public static TreeEntryTargetType ToTreeEntryTargetType(this GitObjectType type)
        {
            switch (type)
            {
            case GitObjectType.Commit:
                return(TreeEntryTargetType.GitLink);

            case GitObjectType.Tree:
                return(TreeEntryTargetType.Tree);

            case GitObjectType.Blob:
                return(TreeEntryTargetType.Blob);

            default:
                throw new InvalidOperationException(string.Format("Cannot map {0} to a TreeEntryTargetType.", type));
            }
        }
Пример #25
0
        /// <summary>
        ///   Try to lookup an object by its sha or a reference canonical name and <see cref = "GitObjectType" />. If no matching object is found, null will be returned.
        /// </summary>
        /// <param name = "shaOrReferenceName">The sha or reference canonical name to lookup.</param>
        /// <param name = "type">The kind of <see cref = "GitObject" /> being looked up</param>
        /// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
        public GitObject Lookup(string shaOrReferenceName, GitObjectType type = GitObjectType.Any)
        {
            ObjectId id;

            if (ObjectId.TryParse(shaOrReferenceName, out id))
            {
                return(Lookup(id, type));
            }

            Reference reference = Refs[shaOrReferenceName];

            if (!IsReferencePeelable(reference))
            {
                return(null);
            }

            return(Lookup(reference.ResolveToDirectReference().TargetIdentifier, type));
        }
Пример #26
0
            public override int Read(byte[] oid, out Stream data, out GitObjectType objectType)
            {
                data       = null;
                objectType = GitObjectType.Bad;

                MockGitObject gitObject;

                if (m_objectIdToContent.TryGetValue(oid, out gitObject))
                {
                    data = Allocate(gitObject.Data.LongLength);
                    data.Write(gitObject.Data, 0, gitObject.Data.Length);

                    objectType = gitObject.ObjectType;

                    return(GIT_OK);
                }

                return(GIT_ENOTFOUND);
            }
Пример #27
0
        internal static Type AsType(this GitObjectType type)
        {
            switch (type)
            {
            case GitObjectType.Commit:
                return(typeof(GitCommit));

            case GitObjectType.Tree:
                return(typeof(GitTree));

            case GitObjectType.Blob:
                return(typeof(GitBlob));

            case GitObjectType.Tag:
                return(typeof(GitTag));

            default:
                throw new ArgumentOutOfRangeException(nameof(type));
            }
        }
Пример #28
0
        public static ObjectType ToObjectType(this GitObjectType type)
        {
            switch (type)
            {
            case GitObjectType.Commit:
                return(ObjectType.Commit);

            case GitObjectType.Tree:
                return(ObjectType.Tree);

            case GitObjectType.Blob:
                return(ObjectType.Blob);

            case GitObjectType.Tag:
                return(ObjectType.Tag);

            default:
                throw new InvalidOperationException(string.Format("Cannot map {0} to a ObjectType.", type));
            }
        }
Пример #29
0
        internal static GitObject BuildFrom(Repository repo, ObjectId id, GitObjectType type, FilePath path)
        {
            switch (type)
            {
            case GitObjectType.Commit:
                return(new Commit(repo, id));

            case GitObjectType.Tree:
                return(new Tree(repo, id, path));

            case GitObjectType.Tag:
                return(new TagAnnotation(repo, id));

            case GitObjectType.Blob:
                return(new Blob(repo, id));

            default:
                throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture, "Unexpected type '{0}' for object '{1}'.", type, id));
            }
        }
Пример #30
0
        internal GitObject Lookup(string shaOrReferenceName, GitObjectType type, LookUpOptions lookUpOptions)
        {
            ObjectId id;

            Reference reference = Refs[shaOrReferenceName];

            if (reference != null)
            {
                id = reference.PeelToTargetObjectId();
            }
            else
            {
                ObjectId.TryParse(shaOrReferenceName, out id);
            }

            if (id == null)
            {
                if (lookUpOptions.Has(LookUpOptions.ThrowWhenNoGitObjectHasBeenFound))
                {
                    Ensure.GitObjectIsNotNull(null, shaOrReferenceName);
                }

                return(null);
            }

            GitObject gitObj = Lookup(id, type);

            if (lookUpOptions.Has(LookUpOptions.ThrowWhenNoGitObjectHasBeenFound))
            {
                Ensure.GitObjectIsNotNull(gitObj, shaOrReferenceName);
            }

            if (!lookUpOptions.Has(LookUpOptions.DereferenceResultToCommit))
            {
                return(gitObj);
            }

            return(gitObj.DereferenceToCommit(shaOrReferenceName, lookUpOptions.Has(LookUpOptions.ThrowWhenCanNotBeDereferencedToACommit)));
        }
Пример #31
0
            private static unsafe 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;
            }
Пример #32
0
        /// <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");

            var oid = id.Oid;
            IntPtr obj;
            var 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);

            return GitObject.CreateFromPtr(obj, id, this);
        }
Пример #33
0
 /// <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)
 {
     return LookupInternal(id, type, null);
 }
Пример #34
0
        internal GitObject LookupInternal(ObjectId id, GitObjectType type, FilePath knownPath)
        {
            Ensure.ArgumentNotNull(id, "id");

            GitOid oid = id.Oid;
            GitObjectSafeHandle obj = null;

            try
            {
                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)
                {
                    return null;
                }

                Ensure.Success(res);

                if (id is AbbreviatedObjectId)
                {
                    id = GitObject.ObjectIdOf(obj);
                }

                return GitObject.CreateFromPtr(obj, id, this, knownPath);
            }
            finally
            {
                obj.SafeDispose();
            }
        }
Пример #35
0
        /// <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);
        }
Пример #36
0
 /// <summary>
 ///   Requests that this backend read an object.
 /// </summary>
 public abstract int Read(byte[] oid,
     out Stream data,
     out GitObjectType objectType);
Пример #37
0
        internal GitObject Lookup(string shaOrReferenceName, GitObjectType type, LookUpOptions lookUpOptions)
        {
            ObjectId id;

            Reference reference = Refs[shaOrReferenceName];
            if (reference != null)
            {
                id = reference.PeelToTargetObjectId();
            }
            else
            {
                ObjectId.TryParse(shaOrReferenceName, out id);
            }

            if (id == null)
            {
                if (lookUpOptions.Has(LookUpOptions.ThrowWhenNoGitObjectHasBeenFound))
                {
                    Ensure.GitObjectIsNotNull(null, shaOrReferenceName);
                }

                return null;
            }

            GitObject gitObj = Lookup(id, type);

            if (lookUpOptions.Has(LookUpOptions.ThrowWhenNoGitObjectHasBeenFound))
            {
                Ensure.GitObjectIsNotNull(gitObj, shaOrReferenceName);
            }

            if (!lookUpOptions.Has(LookUpOptions.DereferenceResultToCommit))
            {
                return gitObj;
            }

            return gitObj.DereferenceToCommit(shaOrReferenceName, lookUpOptions.Has(LookUpOptions.ThrowWhenCanNotBeDereferencedToACommit));
        }
Пример #38
0
 public static extern int git_tag_create_f(out GitOid oid, RepositorySafeHandle repo, string name, ref GitOid target, GitObjectType type, GitSignature signature, string message);
Пример #39
0
 public static extern int git_object_lookup_prefix(out IntPtr obj, RepositorySafeHandle repo, ref GitOid id, uint len, GitObjectType type);
Пример #40
0
        public void CanRetrieveEntries(string path, string expectedAttributes, GitObjectType expectedType, string expectedSha)
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree);

                TreeEntryDefinition ted = td[path];

                Assert.Equal(ToMode(expectedAttributes), ted.Mode);
                Assert.Equal(expectedType, ted.Type);
                Assert.Equal(new ObjectId(expectedSha), ted.TargetId);
            }
        }
Пример #41
0
            private static int WriteStream(
                out IntPtr stream_out,
                IntPtr backend,
                UIntPtr length,
                GitObjectType type)
            {
                stream_out = IntPtr.Zero;

                OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend;

                ObjectType objectType = type.ToObjectType();

                if (odbBackend != null &&
                    length.ToUInt64() < long.MaxValue)
                {
                    OdbBackendStream stream;

                    try
                    {
                        int toReturn = odbBackend.WriteStream((long)length.ToUInt64(), objectType, 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;
            }
Пример #42
0
            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;
            }
Пример #43
0
            private static unsafe int ReadPrefix(
                out GitOid out_oid,
                out IntPtr buffer_p,
                out UIntPtr len_p,
                out GitObjectType type_p,
                IntPtr backend,
                ref GitOid short_oid,
                UIntPtr len)
            {
                out_oid = default(GitOid);
                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)
                {
                    byte[] oid;
                    Stream dataStream = null;
                    ObjectType objectType;

                    try
                    {
                        // The length of short_oid is described in characters (40 per full ID) vs. bytes (20)
                        // which is what we care about.
                        byte[] shortOidArray = new byte[(long)len >> 1];
                        Array.Copy(short_oid.Id, shortOidArray, shortOidArray.Length);

                        int toReturn = odbBackend.ReadPrefix(shortOidArray, out oid, 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;
                            }

                            out_oid.Id = oid;
                            len_p = new UIntPtr((ulong)memoryStream.Capacity);
                            type_p = objectType.ToGitObjectType();

                            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;
            }
Пример #44
0
            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 = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend;

                if (odbBackend != null)
                {
                    int length;
                    ObjectType objectType;

                    try
                    {
                        int toReturn = odbBackend.ReadHeader(oid.Id, out length, out objectType);

                        if (0 == toReturn)
                        {
                            len_p = new UIntPtr((uint)length);
                            type_p = objectType.ToGitObjectType();
                        }

                        return toReturn;
                    }
                    catch (Exception ex)
                    {
                        Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
                    }
                }

                return (int)GitErrorCode.Error;
            }
Пример #45
0
 /// <summary>
 ///   Try to lookup an object by its sha or a reference canonical name and <see cref = "GitObjectType" />. If no matching object is found, null will be returned.
 /// </summary>
 /// <param name = "shaOrReferenceName">The sha or reference canonical name to lookup.</param>
 /// <param name = "type">The kind of <see cref = "GitObject" /> being looked up</param>
 /// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
 public GitObject Lookup(string shaOrReferenceName, GitObjectType type = GitObjectType.Any)
 {
     return Lookup(shaOrReferenceName, type, LookUpOptions.None);
 }
Пример #46
0
 /// <summary>
 ///   Requests that this backend write an object to the backing store. Returns a stream so that the caller can write
 ///   the data in chunks.
 /// </summary>
 public abstract int WriteStream(long length,
     GitObjectType objectType,
     out OdbBackendStream stream);
Пример #47
0
 /// <summary>
 ///   Requests that this backend read an object's header (length and object type) but not its contents.
 /// </summary>
 public abstract int ReadHeader(byte[] oid,
     out int length,
     out GitObjectType objectType);
Пример #48
0
 /// <summary>
 ///   Requests that this backend write an object to the backing store. The backend may need to compute the object ID
 ///   and return it to the caller.
 /// </summary>
 public abstract int Write(byte[] oid,
     Stream dataStream,
     long length,
     GitObjectType objectType,
     out byte[] finalOid);
Пример #49
0
 /// <summary>
 ///   Requests that this backend read an object. The object ID may not be complete (may be a prefix).
 /// </summary>
 public abstract int ReadPrefix(byte[] shortOid,
     out byte[] oid,
     out Stream data,
     out GitObjectType objectType);
Пример #50
0
            private unsafe static int ReadPrefix(
                out GitOid out_oid,
                out IntPtr buffer_p,
                out UIntPtr len_p,
                out GitObjectType type_p,
                IntPtr backend,
                ref GitOid short_oid,
                UIntPtr len)
            {
                out_oid = default(GitOid);
                buffer_p = IntPtr.Zero;
                len_p = UIntPtr.Zero;
                type_p = GitObjectType.Bad;

                OdbBackend odbBackend = MarshalOdbBackend(backend);
                if (odbBackend == null)
                {
                    return (int)GitErrorCode.Error;
                }

                UnmanagedMemoryStream memoryStream = null;

                try
                {
                    var shortSha = ObjectId.ToString(short_oid.Id, (int) len);

                    ObjectId oid;
                    ObjectType objectType;

                    int toReturn = odbBackend.ReadPrefix(shortSha, out oid, out memoryStream, out objectType);

                    if (toReturn != 0)
                    {
                        return toReturn;
                    }

                    if (memoryStream == null)
                    {
                        return (int)GitErrorCode.Error;
                    }

                    out_oid.Id = oid.RawId;
                    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 (memoryStream != null)
                    {
                        memoryStream.Dispose();
                    }
                }

                return (int)GitErrorCode.Ok;
            }
Пример #51
0
        /// <summary>
        ///   Try to lookup an object by its sha or a reference canonical name and <see cref = "GitObjectType" />. If no matching object is found, null will be returned.
        /// </summary>
        /// <param name = "shaOrReferenceName">The sha or reference canonical name to lookup.</param>
        /// <param name = "type">The kind of <see cref = "GitObject" /> being looked up</param>
        /// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
        public GitObject Lookup(string shaOrReferenceName, GitObjectType type = GitObjectType.Any)
        {
            ObjectId id;

            if (ObjectId.TryParse(shaOrReferenceName, out id))
            {
                return Lookup(id, type);
            }

            Reference reference = Refs[shaOrReferenceName];

            if (!IsReferencePeelable(reference))
            {
                return null;
            }

            return Lookup(reference.ResolveToDirectReference().TargetIdentifier, type);
        }
Пример #52
0
            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;
            }
Пример #53
0
 internal GitObjectMetadata(long size, GitObjectType type)
 {
     this.Size = size;
     this.type = type;
 }
Пример #54
0
            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;
                }
            }
Пример #55
0
        /// <summary>
        ///   Try to lookup an object by its sha or a reference canonical name and <see cref="GitObjectType"/>. If no matching object is found, null will be returned.
        /// </summary>
        /// <param name = "shaOrReferenceName">The sha or reference canonical name to lookup.</param>
        /// <param name = "type">The kind of <see cref="GitObject"/> being looked up</param>
        /// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
        public GitObject Lookup(string shaOrReferenceName, GitObjectType type = GitObjectType.Any)
        {
            ObjectId id = ObjectId.CreateFromMaybeSha(shaOrReferenceName);
            if (id != null)
            {
                return Lookup(id, type);
            }

            var reference = Refs[shaOrReferenceName];

            if (!IsReferencePeelable(reference))
            {
                return null;
            }

            return Lookup(reference.ResolveToDirectReference().TargetIdentifier, type);
        }
Пример #56
0
            private static int WriteStream(
                out IntPtr stream_out,
                IntPtr backend,
                UIntPtr len,
                GitObjectType type)
            {
                stream_out = IntPtr.Zero;

                long length = ConverToLong(len);

                OdbBackend odbBackend = MarshalOdbBackend(backend);
                if (odbBackend == null)
                {
                    return (int)GitErrorCode.Error;
                }

                ObjectType objectType = type.ToObjectType();

                try
                {
                    OdbBackendStream stream;
                    int toReturn = odbBackend.WriteStream(length, objectType, 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;
                }
            }
Пример #57
0
            private static unsafe 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;
                    ObjectType 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.ToGitObjectType();

                            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;
            }
Пример #58
0
            private static unsafe int ReadPrefix(
                out GitOid out_oid,
                out IntPtr buffer_p,
                out UIntPtr len_p,
                out GitObjectType type_p,
                IntPtr backend,
                ref GitOid short_oid,
                UIntPtr len)
            {
                out_oid = default(GitOid);
                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
                {
                    // The length of short_oid is described in characters (40 per full ID) vs. bytes (20)
                    // which is what we care about.
                    var oidLen = (int)len;

                    // Ensure we allocate enough space to cope with odd-sized prefix
                    int arraySize = (oidLen + 1) >> 1;
                    var shortOidArray = new byte[arraySize];
                    Array.Copy(short_oid.Id, shortOidArray, arraySize);

                    byte[] oid;
                    ObjectType objectType;

                    int toReturn = odbBackend.ReadPrefix(shortOidArray, oidLen, out 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;
                    }

                    out_oid.Id = oid;
                    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 (null != dataStream)
                    {
                        dataStream.Dispose();
                    }
                }

                return (int)GitErrorCode.Ok;
            }