Skip to content

Commit aadcf07

Browse files
committed
support git folder to be inside documentation folder
1 parent 9d0d95a commit aadcf07

File tree

3 files changed

+61
-23
lines changed

3 files changed

+61
-23
lines changed

mkdocs_git_authors_plugin/plugin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from mkdocs_git_authors_plugin.exclude import exclude
1616
from mkdocs_git_authors_plugin.git.command import GitCommandError
1717
from mkdocs_git_authors_plugin.git.repo import Repo
18+
from mkdocs_git_authors_plugin.util import working_directory
19+
1820

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

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

5961
try:
60-
self._repo = Repo()
62+
with working_directory(config.get("docs_dir",".")):
63+
self._repo = Repo()
6164
self._fallback = False
6265
self.repo().set_config(self.config)
6366
raise_ci_warnings(path=self.repo()._root)

mkdocs_git_authors_plugin/util.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from datetime import datetime, timedelta, timezone
22
from pathlib import Path
33
from typing import Any, Dict, List
4+
import os
5+
from contextlib import contextmanager
6+
from typing import Any, Generator
7+
from pathlib import Path
48

59
from mkdocs_git_authors_plugin.config import GitAuthorsPluginConfig
610

@@ -148,3 +152,26 @@ def page_authors(authors: List, path: str) -> List[Dict[str, Any]]:
148152
}
149153
for author in authors
150154
]
155+
156+
157+
158+
@contextmanager
159+
def working_directory(path) -> Generator[None, Any, None]:
160+
"""
161+
Temporarily change working directory.
162+
A context manager which changes the working directory to the given
163+
path, and then changes it back to its previous value on exit.
164+
Usage:
165+
```python
166+
# Do something in original directory
167+
with working_directory('/my/new/path'):
168+
# Do something in new directory
169+
# Back to old directory
170+
```
171+
"""
172+
prev_cwd = os.getcwd()
173+
os.chdir(path)
174+
try:
175+
yield
176+
finally:
177+
os.chdir(prev_cwd)

tests/test_basic.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
import re
2121
import shutil
2222
import sys
23-
from contextlib import contextmanager
2423
from packaging.version import Version
25-
from typing import Any, Generator
2624

2725
import git as gitpython
2826
import mkdocs
@@ -31,6 +29,8 @@
3129
from git import Repo
3230
from mkdocs.__main__ import build_command
3331

32+
from mkdocs_git_authors_plugin.util import working_directory
33+
3434
SITES_THAT_SHOULD_SUCCEED = [
3535
"mkdocs.yml",
3636
"mkdocs_complete_material.yml",
@@ -45,26 +45,6 @@
4545
]
4646

4747

48-
@contextmanager
49-
def working_directory(path) -> Generator[None, Any, None]:
50-
"""
51-
Temporarily change working directory.
52-
A context manager which changes the working directory to the given
53-
path, and then changes it back to its previous value on exit.
54-
Usage:
55-
```python
56-
# Do something in original directory
57-
with working_directory('/my/new/path'):
58-
# Do something in new directory
59-
# Back to old directory
60-
```
61-
"""
62-
prev_cwd = os.getcwd()
63-
os.chdir(path)
64-
try:
65-
yield
66-
finally:
67-
os.chdir(prev_cwd)
6848

6949

7050
def build_docs_setup(mkdocs_path, output_path) -> Result:
@@ -373,3 +353,31 @@ def test_mkapi_v1(tmp_path) -> None:
373353
r'<a href="\$api:src/mkdocs_git_authors_plugin.*" class="nav-link">API</a>',
374354
contents,
375355
)
356+
357+
def test_custom_docs_dir(tmp_path):
358+
359+
testproject_path = tmp_path / "testproject"
360+
shutil.copytree("tests/custom_docs_dir", testproject_path)
361+
362+
363+
# init git inside the docs directory
364+
with working_directory(str(testproject_path / "documentation")):
365+
# setup git history
366+
repo = Repo.init(".")
367+
assert not repo.bare
368+
repo.git.add(all=True)
369+
repo.index.commit("first commit")
370+
371+
with working_directory(str(testproject_path)):
372+
result = build_docs_setup("mkdocs.yml", str(tmp_path / "site"))
373+
374+
assert (
375+
result.exit_code == 0
376+
), f"'mkdocs build' command failed. Error: {result.stdout}"
377+
378+
index_file = tmp_path / "site" / "index.html"
379+
assert index_file.exists(), f"{index_file} does not exist"
380+
381+
contents = index_file.read_text()
382+
assert re.search("<span class='git-page-authors", contents)
383+

0 commit comments

Comments
 (0)