|
38 | 38 | import os
|
39 | 39 | import sys
|
40 | 40 | import re
|
| 41 | +from shutil import copy2, rmtree # for WildRepo |
41 | 42 |
|
42 | 43 | DefaultDBType = GitDB
|
43 | 44 | if sys.version_info[1] < 5: # python 2.4 compatiblity
|
@@ -766,3 +767,44 @@ def archive(self, ostream, treeish=None, prefix=None, **kwargs):
|
766 | 767 |
|
767 | 768 | def __repr__(self):
|
768 | 769 | return '<git.Repo "%s">' % self.git_dir
|
| 770 | + |
| 771 | +class WildRepo(Repo): |
| 772 | + """ |
| 773 | + WildRepo is an extension of the GitPython Repo class. It extends the |
| 774 | + functionality to allow for dynamic repo configurations (e.g., repo |
| 775 | + templates with std files and/or git hooks) in 'wild repos'; dynamically |
| 776 | + created gitolite repos. |
| 777 | + """ |
| 778 | + def __init__(self, path=None): |
| 779 | + super(WildRepo, self).__init__(path) |
| 780 | + |
| 781 | + def add(self, path): |
| 782 | + """ |
| 783 | + Extends Repo.index.add by removing the requirement that the file to be |
| 784 | + added is already in the working directory. Instead it accepts a path |
| 785 | + and will make a copy of the file within the repository and then add |
| 786 | + it. |
| 787 | + """ |
| 788 | + copy2(path, self.working_dir) |
| 789 | + name = os.path.basename(path) |
| 790 | + return self.index.add(items={name}) |
| 791 | + |
| 792 | + def commit(self, message=None): |
| 793 | + """ |
| 794 | + A wrapper around Repo.index.commit. Doesn't currently add any |
| 795 | + additional functionality. |
| 796 | + """ |
| 797 | + if message is not None: |
| 798 | + return self.index.commit(message) |
| 799 | + else: |
| 800 | + return ValueError("Commit message can not be None.") |
| 801 | + |
| 802 | + def add_hook(self, path, hook): |
| 803 | + """ |
| 804 | + Allows dynamic addition of Git hooks to wild repositories when they |
| 805 | + are created. The path to the hook is provided and it is symlinked into |
| 806 | + %working_dir%/hooks/. |
| 807 | + """ |
| 808 | + hooks_path = "%s/hooks/%s" % (self.working_dir, hook) |
| 809 | + os.symlink(path, hooks_path) |
| 810 | + |
0 commit comments