diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2009-10-24 00:08:33 +0200 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2009-10-24 00:08:33 +0200 |
commit | f9cec00938d9059882bb8eabdaf2f775943e00e5 (patch) | |
tree | 049935780c32f2c27b9b301bc0277cad19222783 | |
parent | b999cae064fb6ac11a61a39856e074341baeefde (diff) | |
download | gitpython-f9cec00938d9059882bb8eabdaf2f775943e00e5.tar.gz |
index.commit: implemented initial version, but in fact some more changes are required to have a nice API. Tests are not yet fully done either
-rw-r--r-- | TODO | 11 | ||||
-rw-r--r-- | lib/git/index.py | 44 | ||||
-rw-r--r-- | test/git/test_index.py | 12 |
3 files changed, 58 insertions, 9 deletions
@@ -31,6 +31,12 @@ Object it should be renamed to 'sha'. There was a time when references where allowed as well, but now objects will be 'baked' to the actual sha to assure comparisons work. +Commit +------ +* message is stipped during parsing, which is wrong unless we parse from + rev-list output. In fact we don't know that, and can't really tell either. + Currently we strip away white space that might actually belong to the message + Config ------ * Expand .get* methods of GitConfigParser to support default value. If it is not None, @@ -71,6 +77,11 @@ Index to remove whole directories easily. This could be implemented using git-update-index if this becomes an issue, but then we had to do all the globbing and directory removal ourselves +* commit: advance head = False - tree object should get the base commit wrapping + that index uses after writing itself as tree. Perhaps it would even be better + to have a Commit.create method from a tree or from an index. Allowing the + latter would be quite flexible and would fit into the system as refs have + create methods as well Refs ----- diff --git a/lib/git/index.py b/lib/git/index.py index 65e658b0..86bdfd39 100644 --- a/lib/git/index.py +++ b/lib/git/index.py @@ -17,7 +17,7 @@ import sys import stat import git.diff as diff -from git.objects import Blob, Tree, Object +from git.objects import Blob, Tree, Object, Commit from git.utils import SHA1Writer, LazyMixin, ConcurrentWriteOperation @@ -711,25 +711,51 @@ class IndexFile(LazyMixin, diff.Diffable): return [ p[4:-1] for p in removed_paths ] @default_index - def commit(self, message=None, parent_commits=None, **kwargs): + def commit(self, message, parent_commits=None): """ Commit the current index, creating a commit object. ``message`` - 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 None or empty, the current head commit will be the parent of the + 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 - ``**kwargs`` - Additional keyword arguments passed to git-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 """ - raise NotImplementedError("todo") + 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,"w") + fp.write(str(message)) + fp.close() + fp = open(tmp_file_path,"r") + 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) + return Commit(self.repo, commit_sha) + finally: + fp.close() + os.remove(tmp_file_path) @clear_cache @default_index diff --git a/test/git/test_index.py b/test/git/test_index.py index 2f8fed32..0a5f4c35 100644 --- a/test/git/test_index.py +++ b/test/git/test_index.py @@ -236,6 +236,18 @@ class TestTree(TestBase): assert len(deleted_files) > 1 self.failUnlessRaises(ValueError, index.remove, ["/doesnt/exists"]) + # test committing + # commit changed index + cur_commit = rw_repo.head.commit + commit_message = "commit default head" + new_commit = index.commit(commit_message) + assert new_commit.message == commit_message + assert new_commit.parents[0] == cur_commit + + self.fail("commit with no parents") + self.fail("commit multiple parents") + + # re-add all files in lib self.fail( "add, commit, working tree handling" ) |