def set_remote(repo_path, url): run_command( cmd="git remote set-url origin {}".format(url), data=None, location=repo_path, chw=True, )
def git_fetch(repo_path: str, revision: str, flags: List[str], env=None): flags = flags or [] fetch_cmd = "git fetch {} origin".format(" ".join(flags)) if revision: fetch_cmd = "{} {}".format(fetch_cmd, revision) fetch_cmd = "{} --update-head-ok --force".format(fetch_cmd) run_command(cmd=fetch_cmd, data=None, location=repo_path, chw=True, env=env) run_command(cmd="git checkout FETCH_HEAD", data=None, location=repo_path, chw=True)
def checkout_revision(repo_path, revision): # pylint:disable=redefined-outer-name """Checkout to a specific revision. If commit is None then checkout to master. """ revision = revision or "master" run_command( cmd="git checkout {}".format(revision), data=None, location=repo_path, chw=True )
def commit(repo_path, user_email, user_name, message="updated"): run_command(cmd="git add -A", data=None, location=repo_path, chw=True) run_command( cmd='git -c user.email=<{}> -c user.name={} commit -m "{}"'.format( user_email, user_name, message), data=None, location=repo_path, chw=True, )
def git_commit(repo_path=".", user_email=None, user_name=None, message=None): message = message or "updated" run_command(cmd="git add -A", data=None, location=repo_path, chw=True) git_auth = "-c user.email=<{}> -c user.name={}".format(user_email, user_name) run_command( cmd='git {} commit -m "{}"'.format(git_auth, message), data=None, location=repo_path, chw=True, )
def checkout_commit(repo_path, commit_hash): # pylint:disable=redefined-outer-name """Checkout to a specific commit. If commit is None then checkout to master. """ commit_hash = commit_hash or "master" run_command( cmd="git checkout {}".format(commit_hash), data=None, location=repo_path, chw=True, )
def get_repo_name(path="."): repo = run_command(cmd="git rev-parse --show-toplevel", data=None, location=path, chw=True).split("\n")[0] return os.path.basename(repo)
def is_git_initialized(path="."): return bool( run_command( cmd="git rev-parse --is-inside-work-tree", data=None, location=path, chw=True, ).split("\n")[0])
def get_committed_files(repo_path, commit_hash): # pylint:disable=redefined-outer-name files_committed = run_command( cmd="git diff-tree --no-commit-id --name-only -r {}".format(commit_hash), data=None, location=repo_path, chw=True, ).split("\n") return [f for f in files_committed if f]
def get_remote(repo_path="."): current_remote = run_command( cmd="git config --get remote.origin.url", data=None, location=repo_path, chw=True, ) return current_remote.strip("\n")
def get_head(path="."): return run_command(cmd="git rev-parse HEAD", data=None, location=path, chw=True).split("\n")[0]
def get_commit(path="."): return run_command(cmd="git --no-pager log --pretty=oneline -1", data=None, location=path, chw=True).split(" ")[0]
def undo(repo_path): run_command(cmd="git reset --hard", data=None, location=repo_path, chw=True) run_command(cmd="git clean -fd", data=None, location=repo_path, chw=True)
def get_status(repo_path): return run_command(cmd="git status -s", data=None, location=repo_path, chw=True)
def is_dirty(path="."): return bool( run_command(cmd="git diff --stat", data=None, location=path, chw=True))
def get_branch_name(path="."): return run_command(cmd="git rev-parse --abbrev-ref HEAD", data=None, location=path, chw=True).split("\n")[0]
def git_init(repo_path): run_command( cmd="git init {}".format(repo_path), data=None, location=repo_path, chw=True )