def __init__(self, gitdir=None): self.gitdir = gitdir if not self.gitdir: self.gitdir = tempfile.mkdtemp() Repo.create(self.gitdir) self.repo = Repo(self.gitdir) self.fetch_objects = self.repo.fetch_objects self.get_refs = self.repo.get_refs
def create_repo(repo_name, description=None, initial_commit=False): """ Create a repository at settings.GIT_REPOS_DIR + repo_name + '.git' :param repo_name: Name of the new repo. :param description: Optional description. :param initial_commit: Whether to do a initial commit after creation. """ repo_bare_path = get_repo_path(repo_name) os.makedirs(repo_bare_path) repo = Repo.create(repo_bare_path) # Write the description to the file for the time being: with open(os.path.join(repo_bare_path, 'description'), 'w') as f: if description: f.write(description) else: f.write(repo_name) if initial_commit: # Since we are creating a bare repo, we have to clone the repository # and then do the commit and then push and then remove the clone. readme = Blob.from_string('# {}'.format(repo_name)) tree = Tree() tree.add('README.md', 0100644, readme.id) repo.do_commit( message='Initial commit', tree=tree.id, committer='Djangit' ) object_store = repo.object_store object_store.add_object(readme) object_store.add_object(tree) return repo