Skip to main content

Git Directory Extension

Jinja2 filter extension for detecting if a directory is an (empty) git repository and determining the default branch.

Usage

gitdir

Detects if the path being filtered by gitdir is the top level git repository directory.

So, if my path is /git/path/here and there exists /git/path/here/.git, then {{ '/git/path/here' | gitdir }} will be true.

However, if my path is /git/path/here and there exists /git/path/.git, then {{ '/git/path/here' | gitdir }} will be false.

This is because the top level directory is /git/path, not the tested path of /git/path/here.

Examples:

  • Detect if git_path is a git directory
    {{ git_path | gitdir }}
  • Assert that git_path is a git directory
    {{ git_path | gitdir is true }}
  • Assert that git_path is NOT a git directory
    {{ git_path | gitdir is false }}
  • Using gitdir in a conditional
    {% if (git_path | gitdir) %}{{ git_path }} is a git directory{% else %}no git directory at {{ git_path }}{% endif %}

emptygit

Detects if the path being filtered by emptygit contains exactly 0 commits across all references. This will work for subdirectories within a git directory.

So, if my path is /git/path/here and there have been no commits, then {{ '/git/path/here' | emptygit }} will be true.

However, if my path is /git/path/here and there have been any commits anywhere, then {{ '/git/path/here' | emptygit }} will be false.

Examples:

  • Detect if git_path is an empty git directory
    {{ git_path | emptygit }}
  • Assert that git_path is an empty git directory
    {{ git_path | emptygit is true }}
  • Assert that git_path is NOT an empty git directory
    {{ git_path | emptygit is false }}
  • Using emptygit in a conditional
    {% if (git_path | emptygit) %}{{ git_path }} has commits{% else %}{{ git_path }} has NO commits{% endif %}

gitdefaultbranch

Returns the default branch name for the git repository at the given path. Uses a multi-layered fallback cascade to determine the default branch:

  1. git symbolic-ref refs/remotes/upstream/HEAD (local, fast)
  2. git symbolic-ref refs/remotes/origin/HEAD (local, fast)
  3. git ls-remote --symref upstream HEAD (network, read-only)
  4. git ls-remote --symref origin HEAD (network, read-only)
  5. git config init.defaultBranch (local config)
  6. Hardcoded fallback: main

Returns an empty string if the path is not a git repository.

Examples:

  • Get the default branch name {{ git_path | gitdefaultbranch }}
  • Using gitdefaultbranch in a conditional {% if (git_path | gitdefaultbranch) %}default branch: {{ git_path | gitdefaultbranch }}{% endif %}

Copier

This can be utilized within a Copier copier.yaml file for determining if the destination path is already initialized as a git directory.

Example:

This will configure a Copier _task to run git init but only if the destination path isn't already a git directory.

_jinja_extensions:
    - jinja2_git_dir.GitDirectoryExtension
_tasks:
  - command: "git init"
    when: "{{ _copier_conf.dst_path | realpath | gitdir is false }}"
  # ORDERING: `emptygit is false` test must come first, otherwise both tasks trigger
  - command: "git commit -am 'template update applied'"
    when: "{{ _copier_conf.dst_path | realpath | emptygit is false }}"
  - command: "git commit -am 'initial commit'"
    when: "{{ _copier_conf.dst_path | realpath | emptygit is true }}"

Development

Release files for jinja2-git-dir 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jinja2-git-dir 0.5.0
File Size Uploaded
jinja2_git_dir-0.5.0.tar.gz 4.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jinja2-git-dir 0.5.0
File Interpreter ABI Platform
jinja2_git_dir-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.1 kB

Release files / jinja2_git_dir-0.5.0.tar.gz

Download URL jinja2_git_dir-0.5.0.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
04b0129059a58fa7572f95ad626ce152349fee0f86eb0be30c3f6c7b7b4ea529
BLAKE2b-256 checksum
How to use checksums
25d5456e8a84d64ca8e29432909e3932a8f5a34fc65e0be7e852d4573154f335
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 7, 2026.

Transparency log

Release files / jinja2_git_dir-0.5.0-py3-none-any.whl

Download URL jinja2_git_dir-0.5.0-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2186011255f1ee9763dd46165abcc2fecbbfc4fbde0e6c63b00ab3efca253a51
BLAKE2b-256 checksum
How to use checksums
bfdb5559919247aacd3b7c6d5dfb30ce749b4693f444ba4fc19a358c1d1c76bc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 7, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page