Skip to content

feat: add gituser and gitemail filters for git config access #5

@wgordon17

Description

@wgordon17

Summary

Add two new Jinja2 filters to GitDirectoryExtension that expose git config user.name and git config user.email.

Motivation

Copier project templates need access to the git user's name and email for:

  • Pre-filling copyright holder in LICENSE files
  • Pre-filling author information in configuration (e.g., cocogitto cog.toml)
  • Providing smart defaults for template questions

Currently, Copier's sandboxed Jinja2 environment cannot access git config values. Since jinja2-git-dir already runs subprocess.run(["git", ...]) for gitdir, emptygit, and gitdefaultbranch filters, adding gituser and gitemail follows the same pattern.

Proposed API

{{ 'Fallback Name' | gituser }}    {# returns git config user.name, or 'Fallback Name' if unset #}
{{ 'fallback@example.com' | gitemail }}  {# returns git config user.email, or fallback #}

The input string serves as the fallback value (consistent with the existing filter pattern where input is transformed/returned).

Implementation

Two new functions using the existing _run_git_command_at_path helper:

def _git_user(fallback: str) -> str:
    result = _run_git_command_at_path(".", ["config", "user.name"])
    return result.strip() if result and result.strip() else fallback

def _git_email(fallback: str) -> str:
    result = _run_git_command_at_path(".", ["config", "user.email"])
    return result.strip() if result and result.strip() else fallback

Plus registration in GitDirectoryExtension.__init__:

environment.filters["gituser"] = _git_user
environment.filters["gitemail"] = _git_email

Naming: gituser not gitauthor

The filter is named gituser (not gitauthor) because it reads git config user.name, which is the configured user identity -- not the commit author. In git's data model, "author" (GIT_AUTHOR_NAME) refers to who originally wrote a particular commit, while "user" (user.name in git config) is the persistent identity configuration. The distinction matters in rebase, cherry-pick, and --author override scenarios where author and committer can differ from the configured user. Since this filter reads the config setting (not commit metadata), gituser is the accurate name.

Prior Art

The copier-uv template by pawamoy implements identical functionality via copier-templates-extensions, confirming this is a common need in the Copier ecosystem.

Use Case in copier.yaml

copyright_holder:
  type: str
  help: "Full name for copyright/license?"
  default: "{{ 'Default Name' | gituser }}"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions