diff --git a/mkdocs_git_authors_plugin/plugin.py b/mkdocs_git_authors_plugin/plugin.py index d218b8b..b6e2dc2 100644 --- a/mkdocs_git_authors_plugin/plugin.py +++ b/mkdocs_git_authors_plugin/plugin.py @@ -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") @@ -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) @@ -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) diff --git a/mkdocs_git_authors_plugin/util.py b/mkdocs_git_authors_plugin/util.py index 46732ba..54eb1c3 100644 --- a/mkdocs_git_authors_plugin/util.py +++ b/mkdocs_git_authors_plugin/util.py @@ -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 @@ -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) diff --git a/pyproject.toml b/pyproject.toml index d6a5a02..963ac66 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 = [ diff --git a/tests/custom_docs_dir/documentation/index.md b/tests/custom_docs_dir/documentation/index.md new file mode 100644 index 0000000..cce1a6a --- /dev/null +++ b/tests/custom_docs_dir/documentation/index.md @@ -0,0 +1,7 @@ +# homepage + +this is the homepage + +Page authors: {{ git_page_authors }} + +Site authors: {{ git_site_authors }} diff --git a/tests/custom_docs_dir/mkdocs.yml b/tests/custom_docs_dir/mkdocs.yml new file mode 100644 index 0000000..1bebdf4 --- /dev/null +++ b/tests/custom_docs_dir/mkdocs.yml @@ -0,0 +1,8 @@ +site_name: test gitauthors_plugin +use_directory_urls: true + +docs_dir: documentation + +plugins: + - search + - git-authors \ No newline at end of file diff --git a/tests/test_basic.py b/tests/test_basic.py index be0076d..4c8ccba 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -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 @@ -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", @@ -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: @@ -373,3 +353,31 @@ def test_mkapi_v1(tmp_path) -> None: r'API', 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("