Skip to content

load_git_metadata returns empty dict when docs_dir equals the git root #30

@isuftin

Description

@isuftin

Description

When docs_dir is set to the repository root (. or the same path as the git toplevel), the load_git_metadata() function returns an empty dictionary despite valid git history being available. This causes the plugin to fall back to site_author and filesystem timestamps instead of using git data.

Environment

  • mkdocs-document-dates: 3.8.5
  • mkdocs-materialx: 10.1.6
  • properdocs: 1.6.7
  • Python: 3.14
  • OS: Tested on macOS (native) and Alpine Linux (Docker container)

Reproduction

  1. Create a repository where markdown files live at the root (no docs/ subdirectory)
  2. Configure mkdocs with docs_dir pointing to the repo root:
docs_dir: !ENV [MKDOCS_DOCS_DIR, 'docs']
# At runtime, MKDOCS_DOCS_DIR is set to the repo root
  1. Enable the plugin:
plugins:
  - document-dates:
      position: top
      type: timeago
      show_created: true
      show_updated: true
      show_author: true
  1. Build the site. Authors display as site_author fallback and dates are filesystem timestamps, not git dates.

Root Cause

In utils.py, load_git_metadata() computes:

git_root = Path(subprocess.check_output(
    ['git', 'rev-parse', '--show-toplevel'],
    cwd=docs_dir_path, encoding='utf-8'
).strip())
rel_docs_path = docs_dir_path.relative_to(git_root).as_posix()

When docs_dir_path equals git_root, rel_docs_path resolves to '.'.

The git command that follows works correctly:

git -c core.quotepath=false log --reverse --no-merges --use-mailmap \
    --name-only -z --format='%aN%x1f%aE%x1f%at%x1f%B%x00' \
    --relative=. -- '*.md'

This returns valid output (verified: 72KB of commit data in our repo). However, the parsing logic that processes the output appears to fail when rel_docs_path is '.', resulting in an empty dates_cache dictionary.

Verification

from pathlib import Path
from mkdocs_document_dates.utils import load_git_metadata

# Returns empty dict when run from repo root
data = load_git_metadata(Path('.'))
print(data)  # {}

# But the underlying git command returns valid data
import subprocess
cmd = ['git', '-c', 'core.quotepath=false', 'log', '--reverse', '--no-merges',
       '--use-mailmap', '--name-only', '-z', '--format=%aN%x1f%aE%x1f%at%x1f%B%x00',
       '--relative=.', '--', '*.md']
result = subprocess.run(cmd, capture_output=True, encoding='utf-8')
print(f"stdout length: {len(result.stdout)}")  # 72656 bytes

Expected Behavior

load_git_metadata() should correctly parse git log output and return date/author data for all markdown files when docs_dir is the repository root.

Workaround

None currently. Using git-revision-date-localized plugin as an alternative for dates, but it lacks native template integration with materialx.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions