Exemple #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 GitException("Unable to build a new reference from a type '{0}'.", type);
            }

            return(reference as T);
        }
Exemple #2
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_symbolic_target(handle);

                var targetRef = repo.Refs[targetIdentifier];
                reference = new SymbolicReference(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(String.Format(CultureInfo.InvariantCulture, "Unable to build a new reference from a type '{0}'.", type));
            }

            return(reference as T);
        }
        public static IList<string> ListAllReferenceNames(RepositorySafeHandle repo, GitReferenceType types)
        {
            UnSafeNativeMethods.git_strarray strArray;
            var res = UnSafeNativeMethods.git_reference_listall(out strArray, repo, types);
            Ensure.Success(res);

            return BuildListOf(strArray);
        }
        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);
        }
        /// <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));
            }
        }
Exemple #6
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);
        }
        public GitReference(string fullName, string commitHash)
        {
            string[] nameParts = fullName.Split('/');
            string   shortName = nameParts[nameParts.Length - 1];

            this.RemoteName = null;
            GitReferenceType type = GitReferenceType.Other;

            if (nameParts.Length > 1)
            {
                switch (nameParts[1])
                {
                case "heads":
                    type = GitReferenceType.Head;
                    break;

                case "remotes":
                    if (nameParts.Length == 4)
                    {
                        // Pull "origin" from "refs/remotes/origin/HEAD"
                        this.RemoteName = nameParts[2];
                    }
                    type = GitReferenceType.Remote;
                    break;

                case "tags":
                    type = GitReferenceType.Tag;
                    break;

                case "stash":
                    type = GitReferenceType.Stash;
                    break;

                default:
                    break;
                }
            }

            this.ShortName  = shortName;
            this.FullName   = fullName;
            this.CommitHash = commitHash;
            this.Type       = type;
        }
Exemple #8
0
        public static IList <string> ListAllReferenceNames(RepositorySafeHandle repo, GitReferenceType types)
        {
            UnSafeNativeMethods.git_strarray strArray;
            int res = UnSafeNativeMethods.git_reference_list(out strArray, repo, types);

            Ensure.Success(res);

            return(BuildListOf(strArray));
        }
 public static extern int git_reference_list(out git_strarray array, RepositorySafeHandle repo, GitReferenceType flags);
Exemple #10
0
 internal static extern int git_reference_foreach_glob(
     RepositorySafeHandle repo,
     [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string glob,
     GitReferenceType flags,
     ref_glob_callback callback,
     IntPtr payload);
 public static extern int git_reference_listall(out git_strarray array, RepositorySafeHandle repo, GitReferenceType flags);