|
17 | 17 | import stat |
18 | 18 | import git.diff as diff |
19 | 19 |
|
20 | | -from git.objects import Blob, Tree, Object |
| 20 | +from git.objects import Blob, Tree, Object, Commit |
21 | 21 | from git.utils import SHA1Writer, LazyMixin, ConcurrentWriteOperation |
22 | 22 |
|
23 | 23 |
|
@@ -711,25 +711,51 @@ def remove(self, items, working_tree=False, **kwargs): |
711 | 711 | return [ p[4:-1] for p in removed_paths ] |
712 | 712 |
|
713 | 713 | @default_index |
714 | | - def commit(self, message=None, parent_commits=None, **kwargs): |
| 714 | + def commit(self, message, parent_commits=None): |
715 | 715 | """ |
716 | 716 | Commit the current index, creating a commit object. |
717 | 717 | |
718 | 718 | ``message`` |
719 | | - Commit message |
| 719 | + Commit message. It may be an empty string if no message is provided. |
| 720 | + It will be converted to a string in any case. |
720 | 721 | |
721 | 722 | ``parent_commits`` |
722 | | - Optional Commit objects to use as parents for the new commit. |
723 | | - If None or empty, the current head commit will be the parent of the |
| 723 | + Optional Commit objects to use as parents for the new commit. |
| 724 | + If empty list, the commit will have no parents at all and become |
| 725 | + a root commit. |
| 726 | + If None , the current head commit will be the parent of the |
724 | 727 | new commit object |
725 | 728 | |
726 | | - ``**kwargs`` |
727 | | - Additional keyword arguments passed to git-commit |
728 | | - |
729 | 729 | Returns |
730 | 730 | Commit object representing the new commit |
| 731 | + |
| 732 | + Note: |
| 733 | + Additional information about hte committer and Author are taken from the |
| 734 | + environment or from the git configuration, see git-commit-tree for |
| 735 | + more information |
731 | 736 | """ |
732 | | - raise NotImplementedError("todo") |
| 737 | + parents = parent_commits |
| 738 | + if parent_commits is None: |
| 739 | + parent_commits = [ self.repo.head.commit ] |
| 740 | + |
| 741 | + parent_args = [ ("-p", str(commit)) for commit in parent_commits ] |
| 742 | + |
| 743 | + # create message stream |
| 744 | + tmp_file_path = tempfile.mktemp() |
| 745 | + fp = open(tmp_file_path,"w") |
| 746 | + fp.write(str(message)) |
| 747 | + fp.close() |
| 748 | + fp = open(tmp_file_path,"r") |
| 749 | + fp.seek(0) |
| 750 | + |
| 751 | + try: |
| 752 | + # write the current index as tree |
| 753 | + tree_sha = self.repo.git.write_tree() |
| 754 | + commit_sha = self.repo.git.commit_tree(tree_sha, parent_args, istream=fp) |
| 755 | + return Commit(self.repo, commit_sha) |
| 756 | + finally: |
| 757 | + fp.close() |
| 758 | + os.remove(tmp_file_path) |
733 | 759 |
|
734 | 760 | @clear_cache |
735 | 761 | @default_index |
|
0 commit comments