Git Wrapper

Zoo tools wrapper around gitpython with a strong focus zoo package release workflows.

exception DirtyGitRepoException

Bases: Exception

Raised when the current branch has uncommitted changes.

exception IncorrectCurrentBranchException

Bases: Exception

Raised when the current branch isn’t the primary master branch.

exception GitCommandError

Bases: Exception

Raised when the current branch isn’t the primary master branch.

exception InvalidGitRepositoryError

Bases: Exception

Raised when the current branch isn’t the primary master branch.

class GitRepo(repoPath=None)

Bases: object

Wrapper around the gitpython Repo class to provide Zoo related operations.

Parameters

repoPath (str) – The physical disk or URL to the Git repository

classmethod clone(repoPath, destination, **kwargs)

Clones the given git URL to the destination path.

Parameters
  • repoPath (str) – the Git VCS to clone

  • destination (str) – The folder path to clone to

  • kwargs (dict) – see Repo.clone_from

Returns

GitRepo

Return type

GitRepo

checkout(name)

Checks out a branch or tag based on the name.

Parameters

name (str) – The branch or tag name to checkout ie. master

Returns

The branch instance which was checked out. see git.Head class.

Return type

git.Head

assertRepo()

Checks whether this repo is either dirty ie. uncommitted changes in which case a DirtyGitRepoException will be raised. or the current branch isn’t master in which case IncorrectCurrentBranchException is raised.

Returns

True when everything checks out to be fine.

Return type

bool

Raises
  • DirtyGitRepoException – In the case we’re the current branch has uncommitted changes

  • IncorrectCurrentBranchException – In the case that the current branch isn’t master which is required when releasing our packages.

  • AssertionError – if the repository is bare

archive(fileName)

Archive the current repo state as a zip into a temp location.

Note

Relies heavily on gitpython to do the packing.

Parameters

fileName (str) – The file base name to use before the extension.

Returns

The output archive file path which was created in the OS temp folder.

Return type

str

tags()

Returns a sorted list based on the commit date of all tags on the repository

Return type

iterable[git.TagReference]

latestTag()

Returns the latest tag based on the commit date.

Return type

git.TagReference

commit(msg)

Commits All current changes to the branch with the given message.

Parameters

msg (str) – The commit message

createTag(name, message)

Creates and returns a new tag locally. use GitRepo.pushTag() to push to origin.

Parameters
  • name (str) – The new tag name ie. 1.0.0

  • message (str) – The Message for the new tag.

Return type

git.TagReference

pushChanges()

Pushes any uncommitted changes to origin.

pullTags()

Does a fetch –tags on the current branch

pushTag(tag)

Pushes a single tag to origin.

Parameters

tag (git.TagReference) – The tag to push.

hasChangesSinceLastTag()

Checks to see if there’s been any commits since the latest tag.

Return type

bool

commitMessagesSinceTag(firstTag, secondTag=None)

finds and returns all commits between to tags if secondTag is None then the HEAD will be used.

Parameters
  • firstTag (gitTagReference or str) – The first/ older tag to to search from.

  • secondTag (gitTagReference or str or None) – The Tag to search to if None then HEAD will be used.

Returns

A generator where each element is commit object containing the date,author and message.

Return type

iterable[Commit]

Note

Ignores certain commit messages like “Merge branch”, “auto stash”, “Create new version Tag”(which is done by our release tool). Exits early if theres a fatal reply by git.log

latestCommitMsg()

Returns the latest commit message string.

Return type

str

class Commit

Bases: dict

Wrapper dict for a single git commit which contains the date, author and message.

property date
property author
property message
tagToChangeLog(repo, previousTag, toTag, version=None, change=None)

Given the previous tag and the future tag(toTag) a changelog object is generated based on the vcs commit log, removing any commits which need to be ignored.

Parameters
  • repo (GitRepo) – The GitRepo to operate on.

  • previousTag (git.TagReference) – The tag to start filtering from ie. 1.2.0 while toTag would be 1.2.1

  • toTag (git.TagReference) – The latest tag to filter commits too.

  • change (changelog.Changelog or None) – The changelog instance to update or None which will create a new instance.

  • version (str) – The Version to use for the commit messages. only used

Returns

Either the same instance provided by change or a newly created instance.

Return type

changelog.Changelog