A DirectReference points directly to a GitObject
상속: System.Reference
예제 #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);
        }
예제 #2
0
        private ObjectSafeWrapper RetrieveHeadCommitPtr(Reference head)
        {
            DirectReference oidRef = head.ResolveToDirectReference();

            if (oidRef == null)
            {
                return(new ObjectSafeWrapper(null, repo));
            }

            return(new ObjectSafeWrapper(new ObjectId(oidRef.TargetIdentifier), repo));
        }
예제 #3
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);
        }
예제 #4
0
        /// <summary>
        ///   Create a new local branch with the specified name
        /// </summary>
        /// <param name = "name">The name of the branch.</param>
        /// <param name = "target">The target sha or branch name.</param>
        /// <returns></returns>
        public Branch Create(string name, string target)
        {
            ObjectId id;

            if (!ObjectId.TryParse(target, out id))
            {
                DirectReference reference = repo.Refs[NormalizeToCanonicalName(target)].ResolveToDirectReference();
                target = reference.TargetIdentifier;
            }

            repo.Refs.Create(NormalizeToCanonicalName(name), target);
            return(this[name]);
        }
예제 #5
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);
        }
예제 #6
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);
        }