git_wrapper package

Submodules

git_wrapper.branch module

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)[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)
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]

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
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]

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

git_wrapper.commit module

This module acts as an interface for acting on git commits

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

Bases: object

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

git_wrapper.exceptions module

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 patch or diff 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.

git_wrapper.remote module

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')[source]

Refresh the specified remote

Parameters:remote (str) – Remote to fetch
fetch_all()[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.

names()[source]

Returns a list of remotes for a given repo

Return list:A list of utf-8 encoded remote names

git_wrapper.repo module

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

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

commit

Return object to act on the repo’s commits

destroy_and_reclone()[source]

Deletes the current directory and reclone the repository.

git

Returns the git command for a given repo

remote

Return object to act on the repo’s remotes

repo

Returns the git repo for a given path

Return git.Repo:
 A reference to the internal git.Repo object
tag

Return object to act on the repo’s tags

git_wrapper.tag module

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
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

Module contents

Top-level package for git_wrapper.