Git#

Helper functions for working with Git repositories

cpg_utils.git.check_if_commit_is_on_remote(commit: str) bool[source]#

Returns ‘True’ if the commit is available on a remote branch. This relies on the current environment to be up-to-date. It asks if the local environment knows a remote branch with the commit.

Parameters:

commit

cpg_utils.git.get_git_branch_name() str | None[source]#

Returns the current branch name.

cpg_utils.git.get_git_commit_ref_of_current_repository() str[source]#

Returns the commit SHA at the current HEAD

cpg_utils.git.get_git_default_remote() str[source]#

Returns the git remote of ‘origin’, e.g. populationgenomics/cpg-utils

cpg_utils.git.get_git_repo_root() str[source]#

Returns the git repository directory root, e.g. /Users/foo/repos/cpg-utils

cpg_utils.git.get_organisation_name_from_current_directory() str[source]#

Gets the organisation name from the default remote

cpg_utils.git.get_organisation_name_from_remote(remote_name: str) str[source]#

Takes the GitHub repo path and obtains the source organisation based on its remote URL e.g.: >>> get_organisation_name_from_remote( ‘git@github.com:populationgenomics/cpg-utils.git’ ) ‘populationgenomics’

>>> get_organisation_name_from_remote(        'https://github.com/populationgenomics/cpg-utils.git'    )
'populationgenomics'
Parameters:

remote_name

Return type:

the organisation name

cpg_utils.git.get_output_of_command(command: list[str], description: str) str[source]#

subprocess.check_output wrapper that returns string output and raises detailed exceptions on error.

Parameters:
  • command (list of strings creating a full command)

  • description (meaningful message for error logging)

Return type:

stripped output string if command was successful

cpg_utils.git.get_relative_path_from_git_root() str[source]#

If we’re in a subdirectory, get the relative path from the git root to the current directory. Relpath returns “.” if cwd is a git root.

cpg_utils.git.get_relative_script_path_from_git_root(script_name: str) str[source]#

If we’re in a subdirectory, get the relative path from the git root to the current directory, and append the script path. For example, the relative path to this script (from git root) is:

cpg-utils/git.py

Parameters:

script_name

Return type:

fully qualified script path from repo root

cpg_utils.git.get_repo_name_from_current_directory() str[source]#

Gets the repo name from the default remote

cpg_utils.git.get_repo_name_from_remote(remote_name: str) str[source]#

Get the name of a GitHub repo from a supported organisation based on its remote URL e.g.: >>> get_repo_name_from_remote( ‘git@github.com:populationgenomics/cpg-utils.git’ ) ‘cpg-utils’ >>> get_repo_name_from_remote( ‘populationgenomics/cpg-utils.git’ ) ‘cpg-utils’

removed check for the authorised organisation(s) here - we receive a full repository path, so we trust users will not attempt to run potentially harmful code

cpg_utils.git.guess_script_github_url_from(*, repo: str | None, commit: str | None, cwd: str | None, script: list[str], organisation: str = 'populationgenomics') str | None[source]#

Guess the GitHub URL of the script from the given arguments.

cpg_utils.git.guess_script_name_from_script_argument(script: list[str]) str | None[source]#

Guess the script name from the first argument of the script. If the first argument is an executable, try the second param

>>> guess_script_name_from_script_argument(['python', 'main.py'])
'main.py'
>>> guess_script_name_from_script_argument(['./main.sh'])
'main.sh'
>>> guess_script_name_from_script_argument(['main.sh'])
'main.sh'
>>> guess_script_name_from_script_argument(['./test/path/main.sh', 'arg1', 'arg2'])
'test/path/main.sh'
>>> guess_script_name_from_script_argument(['gcloud', 'cp' 'test']) is None
True