diff options
author | nulltoken <emeric.fermas@gmail.com> | 2012-04-21 23:37:24 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2012-04-28 05:32:19 +0400 |
commit | 2401133ef6f365d3aae26a020f2754584852e558 (patch) | |
tree | ea01f38dbcd56ca75304e04d1aafcbe11fa0ef11 /LibGit2Sharp/ObjectDatabase.cs | |
parent | a3d5b885eeb3d1fac1bf69c95196e7d8ef167e28 (diff) |
Add Repository.ObjectDatabase.CreateCommit()
Partially fixes #127.
Diffstat (limited to 'LibGit2Sharp/ObjectDatabase.cs')
-rw-r--r-- | LibGit2Sharp/ObjectDatabase.cs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/LibGit2Sharp/ObjectDatabase.cs b/LibGit2Sharp/ObjectDatabase.cs index 1a430440..dfaf565d 100644 --- a/LibGit2Sharp/ObjectDatabase.cs +++ b/LibGit2Sharp/ObjectDatabase.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using LibGit2Sharp.Core; using LibGit2Sharp.Core.Handles; @@ -59,5 +61,46 @@ namespace LibGit2Sharp { return treeDefinition.Build(repo); } + + /// <summary> + /// Inserts a <see cref = "Commit"/> into the object database, referencing an existing <see cref = "Tree"/>. + /// </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> + /// <param name = "tree">The <see cref = "Tree"/> of the <see cref = "Commit"/> to be created.</param> + /// <param name = "parents">The parents of the <see cref = "Commit"/> to be created.</param> + /// <returns>The created <see cref = "Commit"/>.</returns> + public Commit CreateCommit(string message, Signature author, Signature committer, Tree tree, IEnumerable<Commit> parents) + { + return CreateCommit(message, author, committer, tree, parents, null); + } + + 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)); + } } } |