GitBranch

This module acts as an interface for acting on git branches

class git_wrapper.branch.GitBranch(git_repo, logger)[source]

Bases: object

abort_patch_apply()[source]

Abort applying a patch (git am).

abort_rebase()[source]

Aborts a rebase.

apply_diff(branch_name, diff_path, message, signoff=False)[source]

Apply a diff on top of the specified branch.

Parameters
  • branch_name (str) – The name of the branch or reference to apply the diff to

  • diff_path (str) – Path to the diff file

  • message (str) – Commit message

  • signoff (bool) – Whether to add signed-off-by to commit message

apply_patch(branch_name, path, keep_square_brackets=False)[source]

Apply a git patch file on top of the specified branch.

Parameters
  • branch_name (str) – The name of the branch or reference to apply the patch to

  • path (str) – Path to a git-formatted patch file (cf. git format-patch)

  • keep_square_brackets (bool) – Preserve non-[PATCH] brackets in commit subject

cherry_equivalent(upstream, head)[source]

Get patches that are in both upstream and head.

Parameters
  • upstream (str) – Branch name

  • head (str) – Branch name

cherry_on_head_only(upstream, head)[source]

Get new patches between upstream and head.

Parameters
  • upstream (str) – Branch name

  • head (str) – Branch name

create(name, start_ref, reset_if_exists=False, checkout=False)[source]

Create a local branch based on start_ref.

If the branch exists, do nothing or hard reset it if reset_if_exists is set.

Parameters
  • name (str) – New branch’s name

  • start_ref (str) – Reference (branch, commit, tag, …) to use as a starting point.

  • reset_if_exists (bool) – Whether to hard reset the branch to start_ref if the branch already exists.

  • checkout (bool) – Whether to checkout the new branch

Returns

True if a new branch was created, None otherwise

exists(name, remote=None)[source]

Checks if a branch exists locally or on the specified remote.

Parameters
  • name (str) – Name of the branch to find

  • remote (str) – Remote name to check for the branch, or None if local

hard_reset(branch='master', remote='origin', remote_branch='master', refresh=True)[source]

Perform a hard reset of a local branch to a remote branch.

Parameters
  • branch (str) – Local branch to reset

  • remote (str) – Remote use as base for the reset

  • remote_branch (str) – Remote branch to reset to

  • refresh (bool) – Whether to refresh the remote before resetting

hard_reset_to_ref(branch, ref, checkout=True)[source]

Perform a hard reset of a local branch to any reference.

Parameters
  • branch (str) – Local branch to reset

  • ref (str) – Reference (commit, tag, …) to reset to

  • checkout (bool) – Whether to checkout the new branch

log_diff(hash_from, hash_to, pattern='$full_message')[source]

DEPRECATED. Use GitRepo.log.log_diff instead.

rebase_to_hash(branch_name, hash_)[source]

Perform a rebase from a specific reference to another.

Parameters
  • branch_name (str) – The name of the branch to rebase on

  • hash (str) – The commit hash or reference to rebase to

remote_contains(remote_branch, hash_)[source]

Check if a commit hash is present on a remote branch

Parameters
  • remote_branch (str) – Remote branch to check

  • hash (str) – Commit hash to check if present

reverse_diff(diff_path)[source]

Reverse a diff that was applied to the workspace.

Parameters

diff_path (str) – Path to the diff file

short_log_diff(hash_from, hash_to)[source]

DEPRECATED. Use GitRepo.log.short_log_diff instead.

GitCommit

This module acts as an interface for acting on git commits

class git_wrapper.commit.GitCommit(git_repo, logger)[source]

Bases: object

abort_cherrypick()[source]

Aborts a cherrypick.

cherrypick(sha, branch_name)[source]

Apply given sha on given branch

Parameters
  • sha (str) – The SHA1 of the commit to cherry-pick

  • branch_name (str) – The branch to apply it to

commit(message, signoff=False)[source]

Create a commit for changes to tracked files in the repo. Equivalent to git commit -a -m <message>.

Parameters
  • message (str) – The commit message

  • signoff (bool) – Whether to add signed-off-by to commit message

describe(sha)[source]

Return tag and commit info for a given sha

Parameters

sha (str) – The SHA1 of the commit to describe

Return dict

A dict with tag and patch data

revert(hash_, message=None)[source]

Revert a specified commit.

Parameters
  • hash (str) – The commit hash or reference to rebase to

  • message (str) – Extra info to be included in commit message

same(reference_A, reference_B)[source]

Determine whether two references refer to the same commit.

Parameters
  • reference_A (str) – A commit ref (sha, tag, branch name, …)

  • reference_B (str) – A commit ref (sha, tag, branch name, …)

Return bool

True if the references point to the same commit, False if not

to_hexsha(ref)[source]

Return the commit hex sha for a given commit reference

Parameters

ref (str) – The tag, branch, etc referring to a commit

Return str

The commit’s hex sha for the given reference

Exceptions

exception git_wrapper.exceptions.AbortException[source]

Bases: git_wrapper.exceptions.GitWrapperException

Error occurred while attempting to abort a command.

exception git_wrapper.exceptions.ChangeNotAppliedException[source]

Bases: git_wrapper.exceptions.GitWrapperException

Error occurred while applying a changeset onto a repo.

exception git_wrapper.exceptions.CheckoutException[source]

Bases: git_wrapper.exceptions.GitWrapperException

Error occurred while switching branch.

exception git_wrapper.exceptions.CommitMessageMissingException[source]

Bases: git_wrapper.exceptions.GitWrapperException

Cannot create a commit without a commit message.

exception git_wrapper.exceptions.DescribeException[source]

Bases: git_wrapper.exceptions.GitWrapperException

Error occurred while running the describe command.

exception git_wrapper.exceptions.DirtyRepositoryException[source]

Bases: git_wrapper.exceptions.GitWrapperException

Repository workspace is dirty.

exception git_wrapper.exceptions.FileDoesntExistException[source]

Bases: git_wrapper.exceptions.GitWrapperException

File doesn’t exist.

exception git_wrapper.exceptions.GitWrapperException[source]

Bases: Exception

Superclass for this library’s exceptions

exception git_wrapper.exceptions.PushException[source]

Bases: git_wrapper.exceptions.GitWrapperException

Error occurred while pushing to remote.

exception git_wrapper.exceptions.RebaseException[source]

Bases: git_wrapper.exceptions.GitWrapperException

Error occurred during a rebase.

exception git_wrapper.exceptions.ReferenceNotFoundException[source]

Bases: git_wrapper.exceptions.GitWrapperException

Reference (commit, tag, branch, …) doesn’t exist.

exception git_wrapper.exceptions.RemoteException[source]

Bases: git_wrapper.exceptions.GitWrapperException

Error occurred while doing an operation on a remote.

exception git_wrapper.exceptions.RepoCreationException[source]

Bases: git_wrapper.exceptions.GitWrapperException

Error occurred while creating or cloning a repo.

exception git_wrapper.exceptions.ResetException[source]

Bases: git_wrapper.exceptions.GitWrapperException

Error occurred while resetting.

exception git_wrapper.exceptions.RevertException[source]

Bases: git_wrapper.exceptions.GitWrapperException

Error occurred while attempting to perform a revert.

exception git_wrapper.exceptions.TaggingException[source]

Bases: git_wrapper.exceptions.GitWrapperException

Error occurred while performing operation on tag.

GitLog

This module acts as an interface for acting on git logs

class git_wrapper.log.GitLog(git_repo, logger)[source]

Bases: object

grep_for_commits(branch, grep_for, reverse=False, path=None, log_format='format:%H')[source]

Returns a list of matching commits shas. :param str branch: which branch to grep on :param str grep_for: what to grep for :param bool reverse: whether to return in reversed order :param str path: path to limit the search to, optionally :param str log_format: log format output. Defaults to format:%H. Please refer to git-log documentation about PRETTY FORMATS :return: A list of resulting commit matching the pattern.

log_diff(hash_from, hash_to, pattern='$full_message')[source]

Return a list of strings for log entries between two hashes.

Any of the following placeholders may be used in the pattern:
  • $hash The full commit hash

  • $short_hash The short commit hash, similar to –abbrev-commit

  • $message The commit message

  • $summary First line of the commit message

  • $full_message Complete commit info with hash, author, message. Similar to default “git log” ouput

  • $author Commit author

  • $date Date the commit was authored

Parameters
  • hash_from (str) – A commit hash

  • hash_to (str) – A commit hash

  • pattern (str) – Formatter containing any of the placeholders above

Returns

list of strings

log_show_commit(commit_ref='HEAD', pattern='$full_message')[source]

Return a string representing the given commit.

Any of the following placeholders may be used in the pattern:
  • $hash The full commit hash

  • $short_hash The short commit hash, similar to –abbrev-commit

  • $message The commit message

  • $summary First line of the commit message

  • $full_message Complete commit info with hash, author, message. Similar to default “git log” ouput

  • $author Commit author

  • $date Date the commit was authored

Parameters
  • commit_ref (str) – A commit hash. Defaults to HEAD

  • pattern (str) – Formatter containing any of the placeholders above

Returns

A string

short_log_diff(hash_from, hash_to)[source]

Return a list of strings for log entries between two hashes.

Log entries will be returned in the “<short_hash> <summary>” format.

Parameters
  • hash_from (str) – A commit hash

  • hash_to (str) – A commit hash

GitRemote

This module acts as an interface for acting on git remotes

class git_wrapper.remote.GitRemote(git_repo, logger)[source]

Bases: object

add(name, url)[source]

Adds a remote to the given repo

Parameters
  • name (str) – The name for the remote

  • url (str) – The url to use for the remote

Return bool

True if the remote was added, False otherwise

fetch(remote='origin', prune=False, prune_tags=False)[source]

Refresh the specified remote.

Optionally, specify prune to be True to allow for the removal of local branches that don’t exist on the remote. If you also wish to remove local tags that don’t exist on the remote, specify prune_tags to be True.

Parameters
  • remote (str) – Remote to fetch

  • prune (bool) – True if you want to prune

  • prune_tags (bool) – True if you want to prune local tags (only works if prune is True)

fetch_all(prune=False, prune_tags=False)[source]

Refresh all the repo’s remotes.

All the remotes will be fetched even if one fails ; in this case a single exception containing the list of failed remotes is returned.

Optionally, specify prune to be True to allow for the removal of local branches that don’t exist on the remotes. If you also wish to remove local tags that don’t exist on the remotes, specify prune_tags to be True.

Parameters
  • prune (bool) – True if you want to prune

  • prune_tags (bool) – True if you want to prune local tags (only works if prune is True)

names()[source]

Returns a list of remotes for a given repo

Return list

A list of utf-8 encoded remote names

GitRepo

This module acts as an interface for common git tasks

class git_wrapper.repo.GitRepo(path='', repo=None, logger=None)[source]

Bases: object

Provides a wrapper to interact with a git repository

property branch

Return object to act on the repo’s branches

static clone(clone_from, clone_to, bare=False)[source]

Clone a repository.

Parameters
  • clone_from (str) – The url or path to clone the repo from

  • clone_to (str) – The local path to clone to

  • bare (bool) – Whether to create a bare repo

Return GitRepo

Returns the newly created repo object

property commit

Return object to act on the repo’s commits

destroy_and_reclone()[source]

Deletes the current directory and reclone the repository.

property git

Returns the git command for a given repo

property log

Return object to act on the repo’s logs

property remote

Return object to act on the repo’s remotes

property repo

Returns the git repo for a given path

Return git.Repo

A reference to the internal git.Repo object

property tag

Return object to act on the repo’s tags

GitTag

This module acts as an interface for acting on git tags

class git_wrapper.tag.GitTag(git_repo, logger)[source]

Bases: object

create(name, reference)[source]

Create a new tag to the target reference.

Parameters
  • name (str) – New tag’s name

  • reference (str) – What the tag should point to

delete(name)[source]

Delete tag from local repository

Parameters

name (str) – Tag name to delete

names()[source]

List git tags in the repository.

push(name, remote, dry_run=False)[source]

Push specified tag to specified remote

Parameters
  • name (str) – Tag name

  • remote (str) – Remote to push the tag to

  • dry_run (bool) – Whether to run the commands in dry-run mode