Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions mkdocs_git_authors_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from mkdocs_git_authors_plugin.exclude import exclude
from mkdocs_git_authors_plugin.git.command import GitCommandError
from mkdocs_git_authors_plugin.git.repo import Repo
from mkdocs_git_authors_plugin.util import working_directory


logger = logging.getLogger("mkdocs.plugins")

Expand Down Expand Up @@ -57,7 +59,8 @@ def on_config(self, config: MkDocsConfig, **kwargs) -> Union[MkDocsConfig, None]
assert self.config.authorship_threshold_percent <= 100

try:
self._repo = Repo()
with working_directory(config.get("docs_dir",".")):
self._repo = Repo()
self._fallback = False
self.repo().set_config(self.config)
raise_ci_warnings(path=self.repo()._root)
Expand Down Expand Up @@ -118,16 +121,16 @@ def on_files(
if exclude(file.src_path, excluded_pages):
continue

if not hasattr(file, 'src_dir') or file.src_dir is None:
logger.debug(
f"[git-authors-plugin] Unable to find path for file {file.src_path}. "
"Generated, in-memory files won't have a git history."
)
elif hasattr(file, "generated_by") and file.generated_by:
if hasattr(file, "generated_by") and file.generated_by:
logger.debug(
f"[git-authors-plugin] The file {file.src_path} was generated by {file.generated_by}. "
"Generated, dynamic files won't have a git history."
)
elif not hasattr(file, 'src_dir') or file.src_dir is None:
logger.debug(
f"[git-authors-plugin] Unable to find path for file {file.src_path}. "
"Generated, in-memory files won't have a git history."
)
elif path := file.abs_src_path:
if path.endswith(".md"):
_ = self.repo().page(path)
Expand Down
27 changes: 27 additions & 0 deletions mkdocs_git_authors_plugin/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import Any, Dict, List
import os
from contextlib import contextmanager
from typing import Any, Generator
from pathlib import Path

from mkdocs_git_authors_plugin.config import GitAuthorsPluginConfig

Expand Down Expand Up @@ -148,3 +152,26 @@ def page_authors(authors: List, path: str) -> List[Dict[str, Any]]:
}
for author in authors
]



@contextmanager
def working_directory(path) -> Generator[None, Any, None]:
"""
Temporarily change working directory.
A context manager which changes the working directory to the given
path, and then changes it back to its previous value on exit.
Usage:
```python
# Do something in original directory
with working_directory('/my/new/path'):
# Do something in new directory
# Back to old directory
```
"""
prev_cwd = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(prev_cwd)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ authors = [
]
keywords = ["mkdocs", "git", "contributors", "committers", "authors", "plugin"]
urls = {Homepage = "https://github.com/timvink/mkdocs-git-authors-plugin"}
version = "0.9.2"
version = "0.9.3"
requires-python = ">=3.8"
dependencies = ["mkdocs>=1.0"]
classifiers = [
Expand Down
7 changes: 7 additions & 0 deletions tests/custom_docs_dir/documentation/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# homepage

this is the homepage

Page authors: {{ git_page_authors }}

Site authors: {{ git_site_authors }}
8 changes: 8 additions & 0 deletions tests/custom_docs_dir/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
site_name: test gitauthors_plugin
use_directory_urls: true

docs_dir: documentation

plugins:
- search
- git-authors
52 changes: 30 additions & 22 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import re
import shutil
import sys
from contextlib import contextmanager
from packaging.version import Version
from typing import Any, Generator

import git as gitpython
import mkdocs
Expand All @@ -31,6 +29,8 @@
from git import Repo
from mkdocs.__main__ import build_command

from mkdocs_git_authors_plugin.util import working_directory

SITES_THAT_SHOULD_SUCCEED = [
"mkdocs.yml",
"mkdocs_complete_material.yml",
Expand All @@ -45,26 +45,6 @@
]


@contextmanager
def working_directory(path) -> Generator[None, Any, None]:
"""
Temporarily change working directory.
A context manager which changes the working directory to the given
path, and then changes it back to its previous value on exit.
Usage:
```python
# Do something in original directory
with working_directory('/my/new/path'):
# Do something in new directory
# Back to old directory
```
"""
prev_cwd = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(prev_cwd)


def build_docs_setup(mkdocs_path, output_path) -> Result:
Expand Down Expand Up @@ -373,3 +353,31 @@ def test_mkapi_v1(tmp_path) -> None:
r'<a href="\$api:src/mkdocs_git_authors_plugin.*" class="nav-link">API</a>',
contents,
)

def test_custom_docs_dir(tmp_path):

testproject_path = tmp_path / "testproject"
shutil.copytree("tests/custom_docs_dir", testproject_path)


# init git inside the docs directory
with working_directory(str(testproject_path / "documentation")):
# setup git history
repo = Repo.init(".")
assert not repo.bare
repo.git.add(all=True)
repo.index.commit("first commit")

with working_directory(str(testproject_path)):
result = build_docs_setup("mkdocs.yml", str(tmp_path / "site"))

assert (
result.exit_code == 0
), f"'mkdocs build' command failed. Error: {result.stdout}"

index_file = tmp_path / "site" / "index.html"
assert index_file.exists(), f"{index_file} does not exist"

contents = index_file.read_text()
assert re.search("<span class='git-page-authors", contents)