diff options
author | nulltoken <emeric.fermas@gmail.com> | 2012-02-07 17:57:51 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2012-02-08 22:01:28 +0400 |
commit | 2ba6961327473478ea4862f6bb373f2c0f751bee (patch) | |
tree | ec7dd3730f51219575701e64e49f90bb050355d4 /LibGit2Sharp/CommitCollection.cs | |
parent | fb6083042a11c64e9d48a97062c1930e74059ce6 (diff) |
Make Repository.Commit() able to amend the current tip of the Head
Diffstat (limited to 'LibGit2Sharp/CommitCollection.cs')
-rw-r--r-- | LibGit2Sharp/CommitCollection.cs | 42 |
1 files changed, 21 insertions, 21 deletions
diff --git a/LibGit2Sharp/CommitCollection.cs b/LibGit2Sharp/CommitCollection.cs index db62d31c..27f62276 100644 --- a/LibGit2Sharp/CommitCollection.cs +++ b/LibGit2Sharp/CommitCollection.cs @@ -129,55 +129,55 @@ namespace LibGit2Sharp /// <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="amendPreviousCommit">True to amend the current <see cref="Commit"/> pointed at by <see cref="Repository.Head"/>, false otherwise.</param> /// <returns>The generated <see cref = "Commit" />.</returns> - public Commit Create(string message, Signature author, Signature committer) + public Commit Create(string message, Signature author, Signature committer, bool amendPreviousCommit) { Ensure.ArgumentNotNull(message, "message"); Ensure.ArgumentNotNull(author, "author"); Ensure.ArgumentNotNull(committer, "committer"); + if (amendPreviousCommit && repo.Info.IsEmpty) + { + throw new LibGit2Exception("Can not amend anything. The Head doesn't point at any commit."); + } + 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"]; + var parentIds = RetrieveParentIdsOfTheCommitBeingCreated(repo, amendPreviousCommit); GitOid commitOid; using (var treePtr = new ObjectSafeWrapper(new ObjectId(treeOid), repo)) - using (ObjectSafeWrapper headPtr = RetrieveHeadCommitPtr(head)) + using (var parentObjectPtrs = new DisposableEnumerable<ObjectSafeWrapper>(parentIds.Select(id => new ObjectSafeWrapper(id, repo)))) using (SignatureSafeHandle authorHandle = author.BuildHandle()) using (SignatureSafeHandle committerHandle = committer.BuildHandle()) { - IntPtr[] parentPtrs = BuildArrayFrom(headPtr); - res = NativeMethods.git_commit_create(out commitOid, repo.Handle, head.CanonicalName, authorHandle, - committerHandle, encoding, message, treePtr.ObjectPtr, parentPtrs.Count(), parentPtrs); + string encoding = null; //TODO: Handle the encoding of the commit to be created + + IntPtr[] parentsPtrs = parentObjectPtrs.Select(o => o.ObjectPtr ).ToArray(); + res = NativeMethods.git_commit_create(out commitOid, repo.Handle, repo.Refs["HEAD"].CanonicalName, authorHandle, + committerHandle, encoding, message, treePtr.ObjectPtr, parentObjectPtrs.Count(), parentsPtrs); + Ensure.Success(res); } - Ensure.Success(res); return repo.Lookup<Commit>(new ObjectId(commitOid)); } - private static IntPtr[] BuildArrayFrom(ObjectSafeWrapper headPtr) + private static IEnumerable<ObjectId> RetrieveParentIdsOfTheCommitBeingCreated(Repository repo, bool amendPreviousCommit) { - if (headPtr.ObjectPtr == IntPtr.Zero) + if (amendPreviousCommit) { - return new IntPtr[] { }; + return repo.Head.Tip.Parents.Select(c => c.Id); } - return new[] { headPtr.ObjectPtr }; - } - - private ObjectSafeWrapper RetrieveHeadCommitPtr(Reference head) - { - DirectReference oidRef = head.ResolveToDirectReference(); - if (oidRef == null) + if (repo.Info.IsEmpty) { - return new ObjectSafeWrapper(null, repo); + return Enumerable.Empty<ObjectId>(); } - return new ObjectSafeWrapper(new ObjectId(oidRef.TargetIdentifier), repo); + return new[] { repo.Head.Tip.Id }; } private class CommitEnumerator : IEnumerator<Commit> |