Skip to content

Commit ced54d5

Browse files
ldeluigitimvink
authored andcommitted
Fix option implementation
1 parent 0f3f836 commit ced54d5

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

docs/options.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ plugins:
88
show_contribution: true
99
show_line_count: true
1010
count_empty_lines: true
11+
fallback_to_empty: false
1112
```
1213
1314
## `show_contribution`
@@ -28,3 +29,7 @@ If this option is set to `true` (default: `false`) the number of lines per autho
2829
## `count_empty_lines`
2930

3031
If this option is set to `true` (default: `false`) empty lines will count towards an authors' contribution.
32+
33+
## `fallback_to_empty`
34+
35+
If this option is set to `true` (default: `false`) the plugin will work even outside of a proper Git environment, prompting a warning when it's the case, and resulting in empty author list.

mkdocs_git_authors_plugin/plugin.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from mkdocs_git_authors_plugin.git.command import GitCommandError
22
import re
3+
import logging
34
from mkdocs.config import config_options
45
from mkdocs.plugins import BasePlugin
56

@@ -19,14 +20,8 @@ class GitAuthorsPlugin(BasePlugin):
1920
)
2021

2122
def __init__(self):
22-
try:
23-
self._repo = Repo()
24-
self._fallback = False
25-
except GitCommandError:
26-
if self.config["fallback_to_empty"]:
27-
self._fallback = True
28-
else:
29-
raise
23+
self._repo = None
24+
self._fallback = False
3025

3126
def on_config(self, config, **kwargs):
3227
"""
@@ -47,12 +42,20 @@ def on_config(self, config, **kwargs):
4742
Returns:
4843
(updated) configuration object
4944
"""
50-
if self._fallback:
51-
return
52-
53-
self.repo().set_config(self.config)
54-
55-
raise_ci_warnings(path = self.repo()._root)
45+
try:
46+
self._repo = Repo()
47+
self._fallback = False
48+
self.repo().set_config(self.config)
49+
raise_ci_warnings(path = self.repo()._root)
50+
except GitCommandError:
51+
if self.config["fallback_to_empty"]:
52+
self._fallback = True
53+
logging.warning(
54+
"[git-authors-plugin] Unable to find a git directory and/or git is not installed."
55+
" Option 'fallback_to_empty' set to 'true': Falling back to empty authors list"
56+
)
57+
else:
58+
raise
5659

5760
def on_files(self, files, config, **kwargs):
5861
"""
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
site_name: test gitauthors_plugin
2+
use_directory_urls: true
3+
4+
plugins:
5+
- search
6+
- git-authors:
7+
fallback_to_empty: true

tests/test_basic.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,15 @@ def test_fallback(tmp_path):
127127
"tests/basic_setup/docs", str(testproject_path / "website" / "docs")
128128
)
129129
shutil.copyfile(
130-
"tests/basic_setup/mkdocs_w_contribution.yml",
130+
"tests/basic_setup/mkdocs_fallback.yml",
131131
str(testproject_path / "website" / "mkdocs.yml"),
132132
)
133133

134134
cwd = os.getcwd()
135135
os.chdir(str(testproject_path))
136136

137+
print(str(testproject_path))
138+
137139
result = build_docs_setup(
138140
str(testproject_path / "website/mkdocs.yml"), str(testproject_path / "site")
139141
)

0 commit comments

Comments
 (0)