qisrc.git – Git related tools

A Pythonic git API

qisrc.git.Git

class qisrc.git.Git(repo)

The Git represent a git tree

call(*args, **kwargs)

Call a git command

Parameters:
  • args – The arguments of the command. For instance [“frobnicate”, “–spam=eggs”]
  • kwargs
    Will be passed to subprocess.check_call()
    command, with the following changes:
    • if cwd is not given it will be self.repo instead
    • if env is not given it will be read from the config file
    • if raises is False, no exception will be raised if command fails, and a (retcode, output) tuple will be returned.
transaction(*args, **kwds)

Start a series of git commands

get_config(name)

Get a git config value. Return None if not found

set_config(name, value)

Set a new config value. Will be created if it does not exist

ls_files()

Calls git ls-files and returns a list

get_current_ref(ref='HEAD')

return the current ref git symbolic-ref HEAD else: git name-rev –name-only –always HEAD

get_current_branch()

return the current branch

clone(*args, **kwargs)

Wrapper for git clone

update_submodules(raises=True)

Update submodule, cloning them if necessary

get_local_branches()

Get the list of the local branches in a dict master -> tracking branch

is_valid()

Check if the worktree is a valid git tree.

require_clean_worktree()

Taken from /usr/lib/git-core/git-sh-setup return a tuple (bool, message) so that you can be more verbose in case the worktree is not clean

get_status(untracked=True)

Return the output of status or None if it failed.

is_clean(untracked=True)

Returns true if working dir is clean. (i.e. no untracked files, no unstaged changes)

Parameters:untracked – will return True even if there are untracked files.
is_empty()

Returns true if there are no commits yet (between git init and git commit

set_remote(name, url)

Set a new remote with the given name and url

set_tracking_branch(branch, remote_name, remote_branch=None)

Update the configuration of a branch to track a given remote branch

Parameters:
  • branch – the branch to set configuration for
  • remote_name – the name of the remove (‘origin’ in most cases)
  • remote_branch – the name of the remote to track. If not given will be the same of the branch name
sync_branch(branch, fetch_first=True)

git pull –rebase on steroids:

  • do not try anything if the worktree is not clean
  • update submodules and detect broken submodules configs
  • if no development occurred (master == origin/master), reset the local branch next to the remote (don’t try to rebase, maybe there was a push -f)
  • if on the correct branch, rebase it
Return a tuple (status, message), where status can be:
  • None: sync was skipped, but there was no error
  • False: sync failed
  • True: sync succeeded
is_ff(local_sha1, remote_sha1)

Check local_sha1 is fast-forward with remote_sha1. Return True / False or None in case of error with merge-base.

get_ref_sha1(ref)

Return the sha1 from a ref. None if not found.

sync_branch_devel(master_branch, fetch_first=True)

Make sure master stays compatible with your development branch Checks if your local master branch can be fast-forwarded to remote Update master’s HEAD if it’s the case

get_log(before_ref, after_ref)

Return a list of commits between two refspecs, in natural order (most recent commits last)

Each commit is a dict containing, ‘sha1’ and ‘message’

FIXME: parse author and date ?

safe_checkout(branch, remote, force=False)

Checkout or create the branch next to the matching remote branch.

Return either (True, None) if all went well, or (False, error) in case of error

Other functions in this module

qisrc.git.get_repo_root(path)

Return the root dir of a git worktree given a path.

Return None:if it’s not a git work tree.
qisrc.git.is_submodule(path)

Tell if the given path is a submodule