Skip to content

Commit

Permalink
Merge pull request #110 from timvink/108-strict-mode
Browse files Browse the repository at this point in the history
Add new 'strict' option, closes #108
  • Loading branch information
timvink authored Mar 2, 2023
2 parents 7ddc74c + 7d89e01 commit 6150fad
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
16 changes: 16 additions & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ You can customize the plugin by setting options in `mkdocs.yml`. For example:
exclude:
- index.md
enabled: true
strict: true
```
## `type`
Expand Down Expand Up @@ -131,3 +132,18 @@ Which enables you to disable the plugin locally using:
export ENABLED_GIT_REVISION_DATE=false
mkdocs serve
```

## `strict`

Default is `true`. When enabled, the logs will show warnings when something is wrong but a fallback has been used. When disabled, the logger will use the INFO level instead.

- If you want to raise an error when a warning is logged, use [mkdocs strict mode](https://www.mkdocs.org/user-guide/configuration/#strict) (with `mkdocs build --strict`).
- If you are already using [mkdocs strict mode](https://www.mkdocs.org/user-guide/configuration/#strict), but do not care about these warnings, you can set `strict: false` to ensure no errors are raised.

=== ":octicons-file-code-16: mkdocs.yml"

```yaml
plugins:
- git-revision-date-localized:
strict: true
```
1 change: 1 addition & 0 deletions mkdocs_git_revision_date_localized_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class GitRevisionDateLocalizedPlugin(BasePlugin):
("exclude", config_options.Type(list, default=[])),
("enable_creation_date", config_options.Type(bool, default=False)),
("enabled", config_options.Type(bool, default=True)),
("strict", config_options.Type(bool, default=True)),
)

def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
Expand Down
20 changes: 14 additions & 6 deletions mkdocs_git_revision_date_localized_plugin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ def get_git_commit_timestamp(
"""
commit_timestamp = ""

# Determine the logging level
# Only log warnings when plugin is set to strict.
# That way, users turn those into errors using mkdocs build --strict
if self.config.get('strict'):
log = logger.warning
else:
log = logger.info

# perform git log operation
try:
# Retrieve author date in UNIX format (%at)
Expand All @@ -126,20 +134,20 @@ def get_git_commit_timestamp(
)
except (InvalidGitRepositoryError, NoSuchPathError) as err:
if self.config.get('fallback_to_build_date'):
logger.warning(
log(
"[git-revision-date-localized-plugin] Unable to find a git directory and/or git is not installed."
" Option 'fallback_to_build_date' set to 'true': Falling back to build date"
)
commit_timestamp = time.time()
else:
logger.error(
log(
"[git-revision-date-localized-plugin] Unable to find a git directory and/or git is not installed."
" To ignore this error, set option 'fallback_to_build_date: true'"
)
raise err
except GitCommandError as err:
if self.config.get('fallback_to_build_date'):
logger.warning(
log(
"[git-revision-date-localized-plugin] Unable to read git logs of '%s'. Is git log readable?"
" Option 'fallback_to_build_date' set to 'true': Falling back to build date"
% path
Expand All @@ -154,13 +162,13 @@ def get_git_commit_timestamp(
raise err
except GitCommandNotFound as err:
if self.config.get('fallback_to_build_date'):
logger.warning(
log(
"[git-revision-date-localized-plugin] Unable to perform command: 'git log'. Is git installed?"
" Option 'fallback_to_build_date' set to 'true': Falling back to build date"
)
commit_timestamp = time.time()
else:
logger.error(
log(
"[git-revision-date-localized-plugin] Unable to perform command 'git log'. Is git installed?"
" To ignore this error, set option 'fallback_to_build_date: true'"
)
Expand All @@ -169,7 +177,7 @@ def get_git_commit_timestamp(
# create timestamp
if commit_timestamp == "":
commit_timestamp = time.time()
logger.warning(
log(
"[git-revision-date-localized-plugin] '%s' has no git logs, using current timestamp"
% path
)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mkdocs>=1.0
GitPython
babel>=2.7.0
pytz
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name="mkdocs-git-revision-date-localized-plugin",
version="1.1.0",
version="1.2.0",
description="Mkdocs plugin that enables displaying the localized date of the last git modification of a markdown file.",
long_description=LONG_DESCRIPTION,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 6150fad

Please sign in to comment.