summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Thiel <byronimo@gmail.com>2009-11-24 16:15:40 +0100
committerSebastian Thiel <byronimo@gmail.com>2009-11-24 16:15:40 +0100
commit279d162f54b5156c442c878dee2450f8137d0fe3 (patch)
tree0b89fb29494137ca04c582fa80b89e6a5b4f787c
parent1194dc4322e15a816bfa7731a9487f67ba1a02aa (diff)
downloadgitpython-279d162f54b5156c442c878dee2450f8137d0fe3.tar.gz
commit: added create_from_tree method to untie commit creation from actually using the current index. This makes it more flexible. For convenience, the index.commit method is still available, it delgates all the work
-rw-r--r--lib/git/index.py53
-rw-r--r--lib/git/objects/commit.py65
2 files changed, 69 insertions, 49 deletions
diff --git a/lib/git/index.py b/lib/git/index.py
index 930f2c75..b3dd12fd 100644
--- a/lib/git/index.py
+++ b/lib/git/index.py
@@ -995,58 +995,13 @@ class IndexFile(LazyMixin, diff.Diffable):
"""
Commit the current index, creating a commit object.
- ``message``
- Commit message. It may be an empty string if no message is provided.
- It will be converted to a string in any case.
-
- ``parent_commits``
- Optional Commit objects to use as parents for the new commit.
- If empty list, the commit will have no parents at all and become
- a root commit.
- If None , the current head commit will be the parent of the
- new commit object
-
- ``head``
- If True, the HEAD will be advanced to the new commit automatically.
- Else the HEAD will remain pointing on the previous commit. This could
- lead to undesired results when diffing files.
-
+ For more information on the arguments, see tree.commit.
+
Returns
Commit object representing the new commit
-
- Note:
- Additional information about hte committer and Author are taken from the
- environment or from the git configuration, see git-commit-tree for
- more information
"""
- parents = parent_commits
- if parent_commits is None:
- parent_commits = [ self.repo.head.commit ]
-
- parent_args = [ ("-p", str(commit)) for commit in parent_commits ]
-
- # create message stream
- tmp_file_path = tempfile.mktemp()
- fp = open(tmp_file_path,"wb")
- fp.write(str(message))
- fp.close()
- fp = open(tmp_file_path,"rb")
- fp.seek(0)
-
- try:
- # write the current index as tree
- tree_sha = self.repo.git.write_tree()
- commit_sha = self.repo.git.commit_tree(tree_sha, parent_args, istream=fp)
- new_commit = Commit(self.repo, commit_sha)
-
- if head:
- self.repo.head.commit = new_commit
- # END advance head handling
-
- return new_commit
- finally:
- fp.close()
- os.remove(tmp_file_path)
+ tree_sha = self.repo.git.write_tree()
+ return Commit.create_from_tree(self.repo, tree_sha, message, parent_commits, head)
@classmethod
def _flush_stdin_and_wait(cls, proc):
diff --git a/lib/git/objects/commit.py b/lib/git/objects/commit.py
index 4ec806fb..c7824ca6 100644
--- a/lib/git/objects/commit.py
+++ b/lib/git/objects/commit.py
@@ -10,6 +10,8 @@ import git.stats as stats
from tree import Tree
import base
import utils
+import tempfile
+import os
class Commit(base.Object, Iterable, diff.Diffable):
"""
@@ -270,6 +272,69 @@ class Commit(base.Object, Iterable, diff.Diffable):
# END for each line in stream
+ @classmethod
+ def create_from_tree(cls, repo, tree, message, parent_commits=None, head=True ):
+ """
+ Commit the given tree, creating a commit object.
+
+ ``repo``
+ is the Repo
+
+ ``tree``
+ Sha of a tree or a tree object to become the tree of the new commit
+
+ ``message``
+ Commit message. It may be an empty string if no message is provided.
+ It will be converted to a string in any case.
+
+ ``parent_commits``
+ Optional Commit objects to use as parents for the new commit.
+ If empty list, the commit will have no parents at all and become
+ a root commit.
+ If None , the current head commit will be the parent of the
+ new commit object
+
+ ``head``
+ If True, the HEAD will be advanced to the new commit automatically.
+ Else the HEAD will remain pointing on the previous commit. This could
+ lead to undesired results when diffing files.
+
+ Returns
+ Commit object representing the new commit
+
+ Note:
+ Additional information about hte committer and Author are taken from the
+ environment or from the git configuration, see git-commit-tree for
+ more information
+ """
+ parents = parent_commits
+ if parent_commits is None:
+ parent_commits = [ repo.head.commit ]
+
+ parent_args = [ ("-p", str(commit)) for commit in parent_commits ]
+
+ # create message stream
+ tmp_file_path = tempfile.mktemp()
+ fp = open(tmp_file_path,"wb")
+ fp.write(str(message))
+ fp.close()
+ fp = open(tmp_file_path,"rb")
+ fp.seek(0)
+
+ try:
+ # write the current index as tree
+ commit_sha = repo.git.commit_tree(tree, parent_args, istream=fp)
+ new_commit = cls(repo, commit_sha)
+
+ if head:
+ repo.head.commit = new_commit
+ # END advance head handling
+
+ return new_commit
+ finally:
+ fp.close()
+ os.remove(tmp_file_path)
+
def __str__(self):
""" Convert commit to string which is SHA1 """
return self.sha