Beispiel #1
0
        private TreeEntry RetrieveFromPath(FilePath relativePath)
        {
            if (relativePath.IsNullOrEmpty())
            {
                return(null);
            }

            using (var obj = new ObjectSafeWrapper(Id, repo))
            {
                GitObjectSafeHandle objectPtr;

                int res = NativeMethods.git_tree_get_subtree(out objectPtr, obj.ObjectPtr, relativePath);

                if (res == (int)GitErrorCode.GIT_ENOTFOUND)
                {
                    return(null);
                }

                Ensure.Success(res);

                string posixPath = relativePath.Posix;
                string filename  = posixPath.Split('/').Last();

                TreeEntrySafeHandle handle = NativeMethods.git_tree_entry_byname(objectPtr, filename);
                objectPtr.SafeDispose();

                if (handle.IsInvalid)
                {
                    return(null);
                }

                string parentPath = posixPath.Substring(0, posixPath.Length - filename.Length);
                return(new TreeEntry(handle, Id, repo, path.Combine(parentPath)));
            }
        }
Beispiel #2
0
 private static DiffListSafeHandle BuildDiffListFromTreeAndComparer(Repository repo, ObjectId treeId, TreeComparisonHandleRetriever comparisonHandleRetriever)
 {
     using (var osw = new ObjectSafeWrapper(treeId, repo))
     {
         return(BuildDiffListFromComparer(osw.ObjectPtr, comparisonHandleRetriever));
     }
 }
Beispiel #3
0
        private TreeEntry RetrieveFromPath(string relativePath)
        {
            Ensure.ArgumentNotNullOrEmptyString(relativePath, "relativePath");

            using (var obj = new ObjectSafeWrapper(Id, repo))
            {
                IntPtr objectPtr;

                int res = NativeMethods.git_tree_get_subtree(out objectPtr, obj.ObjectPtr, relativePath);

                if (res == (int)GitErrorCode.GIT_ENOTFOUND)
                {
                    return(null);
                }

                Ensure.Success(res);

                IntPtr e = NativeMethods.git_tree_entry_byname(objectPtr, relativePath.Split('/').Last());

                if (e == IntPtr.Zero)
                {
                    return(null);
                }

                return(new TreeEntry(e, Id, repo));
            }
        }
Beispiel #4
0
 unsafe TreeEntry byIndex(ObjectSafeWrapper obj, uint i, ObjectId parentTreeId, Repository repo, string parentPath)
 {
     using (var entryHandle = Proxy.git_tree_entry_byindex(obj.ObjectPtr, i))
     {
         return(new TreeEntry(entryHandle, parentTreeId, repo, parentPath));
     }
 }
        /// <summary>
        ///   Stores the content of the <see cref = "Repository.Index" /> as a new <see cref = "Commit" /> into the repository.
        /// </summary>
        /// <param name = "message">The description of why a change was made to the repository.</param>
        /// <param name = "author">The <see cref = "Signature" /> of who made the change.</param>
        /// <param name = "committer">The <see cref = "Signature" /> of who added the change to the repository.</param>
        /// <returns>The generated <see cref = "Commit" />.</returns>
        public Commit Create(string message, Signature author, Signature committer)
        {
            Ensure.ArgumentNotNull(message, "message");
            Ensure.ArgumentNotNull(author, "author");
            Ensure.ArgumentNotNull(committer, "committer");

            GitOid treeOid;
            int    res      = NativeMethods.git_tree_create_fromindex(out treeOid, repo.Index.Handle);
            string encoding = null;

            Ensure.Success(res);

            Reference head = repo.Refs["HEAD"];

            GitOid commitOid;

            using (var treePtr = new ObjectSafeWrapper(new ObjectId(treeOid), repo))
                using (ObjectSafeWrapper headPtr = RetrieveHeadCommitPtr(head))
                {
                    IntPtr[] parentPtrs = BuildArrayFrom(headPtr);
                    res = NativeMethods.git_commit_create(out commitOid, repo.Handle, head.CanonicalName, author.Handle,
                                                          committer.Handle, encoding, message, treePtr.ObjectPtr, parentPtrs.Count(), parentPtrs);
                }
            Ensure.Success(res);

            return(repo.Lookup <Commit>(new ObjectId(commitOid)));
        }
Beispiel #6
0
 /// <summary>
 /// Replaces entries in the <see cref="Index"/> with entries from the specified <see cref="Tree"/>.
 /// <para>
 ///   This overwrites all existing state in the <see cref="Index"/>.
 /// </para>
 /// </summary>
 /// <param name="source">The <see cref="Tree"/> to read the entries from.</param>
 public virtual void Replace(Tree source)
 {
     using (var obj = new ObjectSafeWrapper(source.Id, repo.Handle))
     {
         Proxy.git_index_read_fromtree(this, obj.ObjectPtr);
     }
 }
Beispiel #7
0
        internal Commit CreateCommit(string message, Signature author, Signature committer, Tree tree, IEnumerable <Commit> parents, string referenceName)
        {
            Ensure.ArgumentNotNull(message, "message");
            Ensure.ArgumentNotNull(author, "author");
            Ensure.ArgumentNotNull(committer, "committer");
            Ensure.ArgumentNotNull(tree, "tree");
            Ensure.ArgumentNotNull(parents, "parents");

            IEnumerable <ObjectId> parentIds = parents.Select(p => p.Id);

            GitOid commitOid;

            using (var treePtr = new ObjectSafeWrapper(tree.Id, repo))
                using (var parentObjectPtrs = new DisposableEnumerable <ObjectSafeWrapper>(parentIds.Select(id => new ObjectSafeWrapper(id, repo))))
                    using (SignatureSafeHandle authorHandle = author.BuildHandle())
                        using (SignatureSafeHandle committerHandle = committer.BuildHandle())
                        {
                            string encoding = null; //TODO: Handle the encoding of the commit to be created

                            IntPtr[] parentsPtrs = parentObjectPtrs.Select(o => o.ObjectPtr.DangerousGetHandle()).ToArray();
                            int      res         = NativeMethods.git_commit_create(out commitOid, repo.Handle, referenceName, authorHandle,
                                                                                   committerHandle, encoding, message, treePtr.ObjectPtr, parentObjectPtrs.Count(), parentsPtrs);
                            Ensure.Success(res);
                        }

            return(repo.Lookup <Commit>(new ObjectId(commitOid)));
        }
Beispiel #8
0
        private static GitObjectSafeHandle GetParentCommitHandle(uint i, ObjectSafeWrapper obj)
        {
            GitObjectSafeHandle parentCommit;

            Ensure.Success(NativeMethods.git_commit_parent(out parentCommit, obj.ObjectPtr, i));
            return(parentCommit);
        }
 internal ContentChanges(Repository repo, Blob oldBlob, Blob newBlob, GitDiffOptions options)
 {
     using (var osw1 = new ObjectSafeWrapper(oldBlob.Id, repo))
         using (var osw2 = new ObjectSafeWrapper(newBlob.Id, repo))
         {
             Ensure.Success(NativeMethods.git_diff_blobs(osw1.ObjectPtr, osw2.ObjectPtr, options, IntPtr.Zero, FileCallback, HunkCallback, LineCallback));
         }
 }
        private static IntPtr[] BuildArrayFrom(ObjectSafeWrapper headPtr)
        {
            if (headPtr.ObjectPtr == IntPtr.Zero)
            {
                return(new IntPtr[] { });
            }

            return(new[] { headPtr.ObjectPtr });
        }
Beispiel #11
0
 internal void ReplaceContentWithTree(Tree tree)
 {
     using (var nativeTree = new ObjectSafeWrapper(tree.Id, repo))
     {
         int res = NativeMethods.git_index_read_tree(Handle, nativeTree.ObjectPtr);
         Ensure.Success(res);
         UpdatePhysicalIndex();
     }
 }
Beispiel #12
0
 /// <summary>
 /// Returns an enumerator that iterates through the collection.
 /// </summary>
 /// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns>
 public virtual IEnumerator <TreeEntry> GetEnumerator()
 {
     using (var obj = new ObjectSafeWrapper(Id, repo.Handle))
     {
         for (uint i = 0; i < Count; i++)
         {
             yield return(byIndex(obj, i, Id, repo, path));
         }
     }
 }
Beispiel #13
0
 /// <summary>
 /// Returns an enumerator that iterates through the collection.
 /// </summary>
 /// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns>
 public virtual IEnumerator <TreeEntry> GetEnumerator()
 {
     using (var obj = new ObjectSafeWrapper(Id, repo.Handle))
     {
         for (uint i = 0; i < Count; i++)
         {
             TreeEntrySafeHandle handle = Proxy.git_tree_entry_byindex(obj.ObjectPtr, i);
             yield return(new TreeEntry(handle, Id, repo, path));
         }
     }
 }
Beispiel #14
0
 /// <summary>
 ///   Returns an enumerator that iterates through the collection.
 /// </summary>
 /// <returns>An <see cref = "IEnumerator{T}" /> object that can be used to iterate through the collection.</returns>
 public IEnumerator <TreeEntry> GetEnumerator()
 {
     using (var obj = new ObjectSafeWrapper(Id, repo))
     {
         for (uint i = 0; i < Count; i++)
         {
             IntPtr e = NativeMethods.git_tree_entry_byindex(obj.ObjectPtr, i);
             yield return(new TreeEntry(e, Id, repo));
         }
     }
 }
Beispiel #15
0
        private DiffListSafeHandle BuildDiffListFromTrees(ObjectId oldTree, ObjectId newTree)
        {
            using (var osw1 = new ObjectSafeWrapper(oldTree, repo))
                using (var osw2 = new ObjectSafeWrapper(newTree, repo))
                {
                    DiffListSafeHandle diff;
                    GitDiffOptions     options = DefaultOptions;
                    Ensure.Success(NativeMethods.git_diff_tree_to_tree(repo.Handle, options, osw1.ObjectPtr, osw2.ObjectPtr, out diff));

                    return(diff);
                }
        }
Beispiel #16
0
        private IEnumerable <Commit> RetrieveParentsOfCommit(ObjectId oid)
        {
            using (var obj = new ObjectSafeWrapper(oid, repo))
            {
                uint parentsCount = NativeMethods.git_commit_parentcount(obj.ObjectPtr);

                for (uint i = 0; i < parentsCount; i++)
                {
                    IntPtr parentCommit;
                    Ensure.Success(NativeMethods.git_commit_parent(out parentCommit, obj.ObjectPtr, i));
                    yield return((Commit)CreateFromPtr(parentCommit, ObjectIdOf(parentCommit), repo));
                }
            }
        }
Beispiel #17
0
        /// <summary>
        ///   Create a new local branch with the specified name
        /// </summary>
        /// <param name = "name">The name of the branch.</param>
        /// <param name = "shaOrReferenceName">The target which can be sha or a canonical reference name.</param>
        /// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
        /// <returns></returns>
        public Branch Create(string name, string shaOrReferenceName, bool allowOverwrite = false)
        {
            Ensure.ArgumentNotNullOrEmptyString(name, "name");

            ObjectId commitId = repo.LookupCommit(shaOrReferenceName).Id;

            using (var osw = new ObjectSafeWrapper(commitId, repo))
            {
                GitOid oid;
                Ensure.Success(NativeMethods.git_branch_create(out oid, repo.Handle, name, osw.ObjectPtr, allowOverwrite));
            }

            return(this[ShortToLocalName(name)]);
        }
Beispiel #18
0
        private IEnumerable <Commit> RetrieveParentsOfCommit(ObjectId oid)
        {
            using (var obj = new ObjectSafeWrapper(oid, repo))
            {
                uint parentsCount = NativeMethods.git_commit_parentcount(obj.ObjectPtr);

                for (uint i = 0; i < parentsCount; i++)
                {
                    using (var parentCommit = GetParentCommitHandle(i, obj))
                    {
                        yield return(BuildFromPtr(parentCommit, ObjectIdOf(parentCommit), repo));
                    }
                }
            }
        }
Beispiel #19
0
            private ICollection <Commit> RetrieveParentsOfCommit(Repository repo, ObjectId commitId)
            {
                using (var obj = new ObjectSafeWrapper(commitId, repo.Handle))
                {
                    int parentsCount = _count.Value;
                    var parents      = new List <Commit>(parentsCount);

                    for (uint i = 0; i < parentsCount; i++)
                    {
                        ObjectId parentCommitId = Proxy.git_commit_parent_id(obj.ObjectPtr, i);
                        parents.Add(new Commit(repo, parentCommitId));
                    }

                    return(parents);
                }
            }
Beispiel #20
0
        public TreeEntry this[string name]
        {
            get
            {
                using (var obj = new ObjectSafeWrapper(Id, repo))
                {
                    IntPtr e = NativeMethods.git_tree_entry_byname(obj.ObjectPtr, name);

                    if (e == IntPtr.Zero)
                    {
                        return(null);
                    }

                    return(new TreeEntry(e, Id, repo));
                }
            }
        }
Beispiel #21
0
        /// <summary>
        ///   Creates a lightweight tag with the specified name.
        /// </summary>
        /// <param name = "name">The name.</param>
        /// <param name = "target">The target which can be sha or a canonical reference name.</param>
        /// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing tag, false otherwise.</param>
        /// <returns></returns>
        public Tag Create(string name, string target, bool allowOverwrite = false)
        {
            Ensure.ArgumentNotNullOrEmptyString(name, "name");
            Ensure.ArgumentNotNullOrEmptyString(target, "target");

            GitObject objectToTag = repo.Lookup(target, GitObjectType.Any, LookUpOptions.ThrowWhenNoGitObjectHasBeenFound);

            int res;

            using (var objectPtr = new ObjectSafeWrapper(objectToTag.Id, repo))
            {
                GitOid oid;
                res = NativeMethods.git_tag_create_lightweight(out oid, repo.Handle, name, objectPtr.ObjectPtr, allowOverwrite);
            }

            Ensure.Success(res);

            return(this[name]);
        }
Beispiel #22
0
        /// <summary>
        ///   Creates an annotated tag with the specified name.
        /// </summary>
        /// <param name = "name">The name.</param>
        /// <param name = "target">The target which can be sha or a canonical reference name.</param>
        /// <param name = "tagger">The tagger.</param>
        /// <param name = "message">The message.</param>
        /// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing tag, false otherwise.</param>
        /// <returns></returns>
        public Tag Create(string name, string target, Signature tagger, string message, bool allowOverwrite = false)
        {
            Ensure.ArgumentNotNullOrEmptyString(name, "name");
            Ensure.ArgumentNotNullOrEmptyString(target, "target");
            Ensure.ArgumentNotNull(tagger, "tagger");
            Ensure.ArgumentNotNull(message, "message");

            GitObject objectToTag = RetrieveObjectToTag(target);

            int res;

            using (var objectPtr = new ObjectSafeWrapper(objectToTag.Id, repo))
            {
                GitOid oid;
                res = NativeMethods.git_tag_create(out oid, repo.Handle, name, objectPtr.ObjectPtr, tagger.Handle, message, allowOverwrite);
            }

            Ensure.Success(res);

            return(this[name]);
        }
Beispiel #23
0
        /// <summary>
        ///   Find the best possible common ancestor given two <see cref = "Commit"/>s.
        /// </summary>
        /// <param name = "first">The first <see cref = "Commit"/>.</param>
        /// <param name = "second">The second <see cref = "Commit"/>.</param>
        /// <returns>The common ancestor or null if none found.</returns>
        public Commit FindCommonAncestor(Commit first, Commit second)
        {
            Ensure.ArgumentNotNull(first, "first");
            Ensure.ArgumentNotNull(second, "second");

            using (var osw1 = new ObjectSafeWrapper(first.Id, repo))
                using (var osw2 = new ObjectSafeWrapper(second.Id, repo))
                {
                    GitOid ret;
                    int    result = NativeMethods.git_merge_base(out ret, repo.Handle, osw1.ObjectPtr, osw2.ObjectPtr);

                    if (result == (int)GitErrorCode.GIT_ENOTFOUND)
                    {
                        return(null);
                    }

                    Ensure.Success(result);

                    return(repo.Lookup <Commit>(new ObjectId(ret)));
                }
        }