Git Operations

Manage git repositories and configuration.

Facts used in these operations: files.Directory, files.File, git.GitBranch, git.GitConfig, git.GitTrackingBranch.

git.bare_repo

Create bare git repositories.

git.bare_repo(path, user=None, group=None, present=True)
  • path: path to the folder
  • present: whether the bare repository should exist
  • user: chown files to this user after
  • group: chown files to this group after

Example:

git.bare_repo(
    name="Create bare repo",
    path="/home/git/test.git",
)

git.config

Manage git config for a repository or globally.

git.config(key, value, multi_value=False, repo=None)
  • key: the key of the config to ensure
  • value: the value this key should have
  • multi_value: Add the value rather than set it for settings that can have multiple values
  • repo: specify the git repo path to edit local config (defaults to global)

Example:

git.config(
    name="Ensure user name is set for a repo",
    key="user.name",
    value="Anon E. Mouse",
    repo="/usr/local/src/pyinfra",
)

git.repo

Clone/pull git repositories.

git.repo(
    src, dest, branch=None, pull=True, rebase=False, user=None, group=None, ssh_keyscan=False,
    update_submodules=False, recursive_submodules=False,
)
  • src: the git source URL
  • dest: directory to clone to
  • branch: branch to pull/checkout
  • pull: pull any changes for the branch
  • rebase: when pulling, use --rebase
  • user: chown files to this user after
  • group: chown files to this group after
  • ssh_keyscan: keyscan the remote host if not in known_hosts before clone/pull
  • update_submodules: update any git submodules
  • recursive_submodules: update git submodules recursively

Example:

git.repo(
    name="Clone repo",
    src="https://github.com/Fizzadar/pyinfra.git",
    dest="/usr/local/src/pyinfra",
)

git.worktree

Manage git worktrees.

git.worktree(
    worktree, repo=None, detached=False, new_branch=None, commitish=None, pull=True,
    rebase=False, from_remote_branch=None, present=True, assume_repo_exists=False,
    force=False, user=None, group=None,
)
  • worktree: git working tree directory
  • repo: git main repository directory
  • detached: create a working tree with a detached HEAD
  • new_branch: local branch name created at the same time than the worktree
  • commitish: from which git commit, branch, … the worktree is created
  • pull: pull any changes from a remote branch if set
  • rebase: when pulling, use --rebase
  • from_remote_branch: a 2-tuple (remote, branch) that identifies a remote branch
  • present: whether the working tree should exist
  • assume_repo_exists: whether to assume the main repo exists
  • force: remove unclean working tree if should not exist
  • user: chown files to this user after
  • group: chown files to this group after

Examples:

git.worktree(
    name="Create a worktree from the current repo `HEAD`",
    repo="/usr/local/src/pyinfra/master",
    worktree="/usr/local/src/pyinfra/hotfix"
)

git.worktree(
    name="Create a worktree from the commit `4e091aa0`",
    repo="/usr/local/src/pyinfra/master",
    worktree="/usr/local/src/pyinfra/hotfix",
    commitish="4e091aa0"
)

git.worktree(
    name="Create a worktree with a new local branch `v1.0`",
    repo="/usr/local/src/pyinfra/master",
    worktree="/usr/local/src/pyinfra/hotfix",
    new_branch="v1.0",
)

git.worktree(
    name="Create a worktree from the commit 4e091aa0 with the new local branch `v1.0`",
    repo="/usr/local/src/pyinfra/master",
    worktree="/usr/local/src/pyinfra/hotfix",
    new_branch="v1.0",
    commitish="4e091aa0"
)

git.worktree(
    name="Create a worktree with a detached `HEAD`",
    repo="/usr/local/src/pyinfra/master",
    worktree="/usr/local/src/pyinfra/hotfix",
    detached=True,
)

git.worktree(
    name="Create a worktree with a detached `HEAD` from commit `4e091aa0`",
    repo="/usr/local/src/pyinfra/master",
    worktree="/usr/local/src/pyinfra/hotfix",
    commitish="4e091aa0",
    detached=True,
)

git.worktree(
    name="Create a worktree from the existing local branch `v1.0`",
    repo="/usr/local/src/pyinfra/master",
    worktree="/usr/local/src/pyinfra/hotfix",
    commitish="v1.0"
)

git.worktree(
    name="Create a worktree with a new branch `v1.0` that tracks `origin/v1.0`",
    repo="/usr/local/src/pyinfra/master",
    worktree="/usr/local/src/pyinfra/hotfix",
    new_branch="v1.0",
    commitish="v1.0"
)

git.worktree(
    name="Pull an existing worktree already linked to a tracking branch",
    repo="/usr/local/src/pyinfra/master",
    worktree="/usr/local/src/pyinfra/hotfix"
)

git.worktree(
    name="Pull an existing worktree from a specific remote branch",
    repo="/usr/local/src/pyinfra/master",
    worktree="/usr/local/src/pyinfra/hotfix",
    from_remote_branch=("origin", "master")
)

git.worktree(
    name="Remove a worktree",
    worktree="/usr/local/src/pyinfra/hotfix",
    present=False,
)

git.worktree(
    name="Remove an unclean worktree",
    worktree="/usr/local/src/pyinfra/hotfix",
    present=False,
    force=True,
)