qiBuild documentation

qisrc.git – Git related tools

A Pythonic git API


class qisrc.git.Git(repo)

The Git represent a git tree

call(*args, **kwargs)

Call a git command

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


Calls git ls-files and returns a list


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


return the current branch

clone(*args, **kwargs)

Wrapper for git clone


Update submodule, cloning them if necessary


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


Check if the worktree is a valid git tree.


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


Return the output of status or None if it failed.


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.

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

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


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


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

Return None:if it’s not a git work tree.

Tell if the given path is a submodule