Beispiel #1
0
        internal static unsafe T BuildFromPtr <T>(git_reference *handle, Repository repo) where T : Reference
        {
            GitReferenceType type = Proxy.git_reference_type(handle);
            string           name = Proxy.git_reference_name(handle);

            Reference reference;

            switch (type)
            {
            case GitReferenceType.Symbolic:
                string targetIdentifier = Proxy.git_reference_symbolic_target(handle);

                var targetRef = repo.Refs[targetIdentifier];
                reference = new SymbolicReference(repo, name, targetIdentifier, targetRef);
                break;

            case GitReferenceType.Oid:
                ObjectId targetOid = Proxy.git_reference_target(handle);

                reference = new DirectReference(name, repo, targetOid);
                break;

            default:
                throw new LibGit2SharpException("Unable to build a new reference from a type '{0}'.", type);
            }

            return(reference as T);
        }
Beispiel #2
0
        private static T BuildFromPtr <T>(IntPtr ptr, Repository repo) where T : Reference
        {
            if (ptr == IntPtr.Zero)
            {
                return(default(T));
            }

            string           name = NativeMethods.git_reference_name(ptr).MarshallAsString();
            GitReferenceType type = NativeMethods.git_reference_type(ptr);

            Reference reference;

            switch (type)
            {
            case GitReferenceType.Symbolic:
                IntPtr resolveRef;
                var    targetIdentifier = NativeMethods.git_reference_target(ptr).MarshallAsString();
                int    res = NativeMethods.git_reference_resolve(out resolveRef, ptr);

                if (res == (int)GitErrorCode.GIT_ENOTFOUND)
                {
                    reference = new SymbolicReference {
                        CanonicalName = name, Target = null, TargetIdentifier = targetIdentifier
                    };
                    break;
                }

                Ensure.Success(res);

                var targetRef = BuildFromPtrAndRelease <DirectReference>(resolveRef, repo);
                reference = new SymbolicReference {
                    CanonicalName = name, Target = targetRef, TargetIdentifier = targetIdentifier
                };
                break;

            case GitReferenceType.Oid:
                IntPtr oidPtr    = NativeMethods.git_reference_oid(ptr);
                var    oid       = (GitOid)Marshal.PtrToStructure(oidPtr, typeof(GitOid));
                var    targetOid = new ObjectId(oid);

                var targetBuilder = new Lazy <GitObject>(() => repo.Lookup(targetOid));
                reference = new DirectReference(targetBuilder)
                {
                    CanonicalName = name, TargetIdentifier = targetOid.Sha
                };
                break;

            default:
                throw new LibGit2Exception(String.Format(CultureInfo.InvariantCulture, "Unable to build a new reference from a type '{0}'.", Enum.GetName(typeof(GitReferenceType), type)));
            }

            return(reference as T);
        }
Beispiel #3
0
        internal static T BuildFromPtr <T>(ReferenceSafeHandle handle, Repository repo) where T : Reference
        {
            GitReferenceType type = NativeMethods.git_reference_type(handle);
            string           name = NativeMethods.git_reference_name(handle);

            Reference reference;

            switch (type)
            {
            case GitReferenceType.Symbolic:
                string targetIdentifier = NativeMethods.git_reference_target(handle);

                using (ReferenceSafeHandle resolvedHandle = PeelToDirectReference(handle))
                {
                    if (resolvedHandle == null)
                    {
                        reference = new SymbolicReference {
                            CanonicalName = name, Target = null, TargetIdentifier = targetIdentifier
                        };
                        break;
                    }

                    var targetRef = BuildFromPtr <DirectReference>(resolvedHandle, repo);
                    reference = new SymbolicReference {
                        CanonicalName = name, Target = targetRef, TargetIdentifier = targetIdentifier
                    };
                    break;
                }

            case GitReferenceType.Oid:
                ObjectId targetOid = NativeMethods.git_reference_oid(handle).MarshalAsObjectId();

                var targetBuilder = new Lazy <GitObject>(() => repo.Lookup(targetOid));
                reference = new DirectReference(targetBuilder)
                {
                    CanonicalName = name, TargetIdentifier = targetOid.Sha
                };
                break;

            default:
                throw new LibGit2Exception(String.Format(CultureInfo.InvariantCulture, "Unable to build a new reference from a type '{0}'.", Enum.GetName(typeof(GitReferenceType), type)));
            }

            return(reference as T);
        }
Beispiel #4
0
        internal static T BuildFromPtr <T>(ReferenceSafeHandle handle, Repository repo) where T : Reference
        {
            GitReferenceType type = Proxy.git_reference_type(handle);
            string           name = Proxy.git_reference_name(handle);

            Reference reference;

            switch (type)
            {
            case GitReferenceType.Symbolic:
                string targetIdentifier = Proxy.git_reference_target(handle);

                using (ReferenceSafeHandle resolvedHandle = Proxy.git_reference_resolve(handle))
                {
                    if (resolvedHandle == null)
                    {
                        reference = new SymbolicReference(name, targetIdentifier, null);
                        break;
                    }

                    var targetRef = BuildFromPtr <DirectReference>(resolvedHandle, repo);
                    reference = new SymbolicReference(name, targetIdentifier, targetRef);
                    break;
                }

            case GitReferenceType.Oid:
                ObjectId targetOid = Proxy.git_reference_oid(handle);

                reference = new DirectReference(name, repo, targetOid);
                break;

            default:
                throw new LibGit2SharpException(String.Format(CultureInfo.InvariantCulture, "Unable to build a new reference from a type '{0}'.", type));
            }

            return(reference as T);
        }