2017-09-12 05:36:13 +00:00
|
|
|
import git as gitpython
|
2017-09-02 23:19:34 +00:00
|
|
|
import pygit2
|
|
|
|
|
2017-09-23 08:04:40 +00:00
|
|
|
# it's fucking weird to be using two different git libraries, i know.
|
|
|
|
# gitpython:
|
|
|
|
# - couldn't figure out how to check repo status
|
|
|
|
# pygit2:
|
|
|
|
# - had trouble with committing after a merge
|
|
|
|
# - can't push to local repos
|
|
|
|
#
|
|
|
|
# i want to standardize on gitpython, but gotta figure out the repo status and
|
|
|
|
# also do the cloning
|
|
|
|
|
2017-09-25 06:42:26 +00:00
|
|
|
def reset_from_origin(repo_path:str) -> None:
|
|
|
|
repo = gitpython.Repo(repo_path)
|
|
|
|
repo.remotes['origin'].fetch()
|
|
|
|
repo.git.reset('--hard', 'origin/master')
|
|
|
|
|
|
|
|
|
2017-09-02 23:19:34 +00:00
|
|
|
def create_repo(to_clone, local_path, author_name, author_email):
|
2017-09-23 08:04:40 +00:00
|
|
|
# TODO port to GitPython
|
2017-09-02 23:19:34 +00:00
|
|
|
repo = pygit2.clone_repository(to_clone, local_path)
|
|
|
|
repo.config['user.name'] = author_name
|
|
|
|
repo.config['user.email'] = author_email
|
|
|
|
|
2017-09-25 06:54:45 +00:00
|
|
|
def dirty(repo_path:str):
|
|
|
|
repo = gitpython.Repo(repo_path)
|
|
|
|
status = repo.git.status()
|
|
|
|
return 'nothing to commit, working directory clean' not in status
|
2017-09-02 23:19:34 +00:00
|
|
|
|
|
|
|
def make_commit(repo_path, author_name, author_email):
|
2017-09-12 05:36:13 +00:00
|
|
|
"""Given a path to a repository, adds everything and commits it. If there
|
|
|
|
are no unstaged changes, does nothing."""
|
2017-09-23 08:04:40 +00:00
|
|
|
if not dirty(repo_path):
|
|
|
|
return
|
|
|
|
repo = gitpython.Repo(repo_path)
|
|
|
|
index = repo.index
|
|
|
|
|
|
|
|
index.add([path for (path, _), __ in index.entries.items()])
|
|
|
|
|
|
|
|
actor = gitpython.Actor(author_name, author_email)
|
|
|
|
index.commit('wiki update', author=actor, committer=actor)
|
|
|
|
|
2017-09-12 05:36:13 +00:00
|
|
|
def push_all(repo_path):
|
|
|
|
repo = gitpython.Repo(repo_path)
|
|
|
|
repo.remotes['origin'].push()
|
|
|
|
|
|
|
|
def pull_from_origin(repo_path):
|
|
|
|
repo = gitpython.Repo(repo_path)
|
|
|
|
repo.remotes['origin'].pull()
|