Beispiel #1
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]);
        }
        /// <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));
            }
        }
Beispiel #3
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));
        }
Beispiel #4
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)));
        }
        /// <summary>
        ///   Creates a direct or symbolic reference with the specified name and target
        /// </summary>
        /// <param name = "name">The name of the reference to create.</param>
        /// <param name = "target">The target which can be either a sha or the canonical name of another reference.</param>
        /// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing reference, false otherwise.</param>
        /// <returns>A new <see cref = "Reference" />.</returns>
        public Reference Create(string name, string target, bool allowOverwrite = false)
        {
            Ensure.ArgumentNotNullOrEmptyString(name, "name");
            Ensure.ArgumentNotNullOrEmptyString(target, "target");

            ObjectId id;
            Func <string, bool, ReferenceSafeHandle> referenceCreator;

            if (ObjectId.TryParse(target, out id))
            {
                referenceCreator = (n, o) => CreateDirectReference(n, id, o);
            }
            else
            {
                referenceCreator = (n, o) => CreateSymbolicReference(n, target, o);
            }

            using (ReferenceSafeHandle handle = referenceCreator(name, allowOverwrite))
            {
                return(Reference.BuildFromPtr <Reference>(handle, repo));
            }
        }
Beispiel #6
0
        /// <summary>
        ///   Creates a direct or symbolic reference with the specified name and target
        /// </summary>
        /// <param name = "name">The name of the reference to create.</param>
        /// <param name = "target">The target which can be either a sha or the canonical name of another reference.</param>
        /// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing reference, false otherwise.</param>
        /// <returns>A new <see cref = "Reference" />.</returns>
        public Reference Create(string name, string target, bool allowOverwrite = false)
        {
            Ensure.ArgumentNotNullOrEmptyString(name, "name");
            Ensure.ArgumentNotNullOrEmptyString(target, "target");

            ObjectId id;

            IntPtr reference;
            int    res;

            if (ObjectId.TryParse(target, out id))
            {
                res = CreateDirectReference(name, id, allowOverwrite, out reference);
            }
            else
            {
                res = CreateSymbolicReference(name, target, allowOverwrite, out reference);
            }

            Ensure.Success(res);

            return(Reference.BuildFromPtr <Reference>(reference, repo));
        }