Skip to main content

Jinja2 filter extension for detecting if a directory is a git repository

Project description

Git Directory Extension

Jinja2 filter extension for detecting if a directory is an (empty) git repository.

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

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 }}"
  # `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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jinja2_git_dir-0.4.1.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jinja2_git_dir-0.4.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file jinja2_git_dir-0.4.1.tar.gz.

File metadata

  • Download URL: jinja2_git_dir-0.4.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for jinja2_git_dir-0.4.1.tar.gz
Algorithm Hash digest
SHA256 f406151d213217a067d52a2b8d205fb0f28c53009ef5735251e1f647d1e396db
MD5 77e1455d71ff15688f36d4558b08da11
BLAKE2b-256 e632e9119b52c2cfed12432981a378d5733590f9057f95eedf60ec7b27e6d9c8

See more details on using hashes here.

File details

Details for the file jinja2_git_dir-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for jinja2_git_dir-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cc39cc7f3bd3fccfa8deb5589f4dfd89fb76c231ad15d1d28503b64327c58db5
MD5 9a31a89781670b98803b42106cf2ebd5
BLAKE2b-256 dde865e146686525c6281183301d09fd2891c04d44201b22e463852e63aad81d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page