Skip to content

Commit ed0d2f5

Browse files
committed
Add warnings on build environments, #44
1 parent e204cf4 commit ed0d2f5

File tree

5 files changed

+147
-0
lines changed

5 files changed

+147
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ plugins:
3737
3838
> If you have no `plugins` entry in your config file yet, you'll likely also want to add the `search` plugin. MkDocs enables it by default if there is no `plugins` entry set.
3939

40+
### Note when using build environments
41+
42+
This plugin needs access to the last commit that touched a specific file to be able to retrieve the date. By default many build environments only retrieve the last commit, which means you might need to:
43+
<details>
44+
<summary>Change your CI settings</summary>
45+
46+
- github actions: set `fetch_depth` to `0` ([docs](https://github.com/actions/checkout))
47+
- gitlab runners: set `GIT_DEPTH` to `1000` ([docs](https://docs.gitlab.com/ee/user/project/pipelines/settings.html#git-shallow-clone))
48+
- bitbucket pipelines: set `clone: depth: full` ([docs](https://support.atlassian.com/bitbucket-cloud/docs/configure-bitbucket-pipelinesyml/))
49+
</details>
50+
51+
4052
## Documentation
4153

4254
See https://timvink.github.io/mkdocs-git-authors-plugin/

docs/index.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,14 @@ plugins:
1919
```
2020
2121
> If you have no `plugins` entry in your config file yet, you'll likely also want to add the `search` plugin. MkDocs enables it by default if there is no `plugins` entry set.
22+
23+
!!! warning "Using build environments"
24+
25+
This plugin needs access to the last commit that touched a specific file to be able to retrieve the date. By default many build environments only retrieve the last commit, which means you might need to:
26+
27+
**Change your CI settings**
28+
29+
- github actions: set `fetch_depth` to `0` ([docs](https://github.com/actions/checkout))
30+
- gitlab runners: set `GIT_DEPTH` to `1000` ([docs](https://docs.gitlab.com/ee/user/project/pipelines/settings.html#git-shallow-clone))
31+
- bitbucket pipelines: set `clone: depth: full` ([docs](https://support.atlassian.com/bitbucket-cloud/docs/configure-bitbucket-pipelinesyml/))
32+

mkdocs.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
site_name: "git-authors Plugin"
22
site_description: "Plugin to add git info to MkDocs sites"
3+
repo_url: https://github.com/timvink/mkdocs-git-authors-plugin
4+
site_url: https://timvink.github.io/mkdocs-git-authors-plugin/
5+
site_author: Tim Vink
6+
copyright: Copyright &copy; 2020 Maintained by <a href="https://github.com/timvink">Tim Vink</a>.
7+
38
use_directory_urls: false
49

510
# Theme
611
theme:
712
name: material
813
custom_dir: docs/overrides
14+
palette:
15+
scheme: preference
16+
primary: blue
17+
accent: blue
918

1019
# Plugins
1120
plugins:
@@ -26,3 +35,5 @@ markdown_extensions:
2635
- markdown.extensions.codehilite
2736
- pymdownx.escapeall
2837
- pymdownx.superfences
38+
- admonition
39+
- pymdownx.details

mkdocs_git_authors_plugin/ci.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
"""
2+
Helper functions related to continuous integration (CI).
3+
4+
This is because often CI runners do not have access to full git history.
5+
6+
Taken from https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/blob/master/mkdocs_git_revision_date_localized_plugin/ci.py
7+
"""
8+
9+
import os
10+
import logging
11+
12+
from pathlib import Path
13+
from mkdocs_git_authors_plugin.git.command import GitCommand
14+
15+
16+
def raise_ci_warnings(path: str) -> None:
17+
"""
18+
Raise warnings when users use plugin on CI build runners.
19+
20+
Args:
21+
path (str): path to the root of the git repo
22+
"""
23+
with Path(path):
24+
if not is_shallow_clone():
25+
return None
26+
27+
n_commits = commit_count()
28+
29+
30+
# Gitlab Runners
31+
if os.getenv("GITLAB_CI") is not None and n_commits < 50:
32+
# Default is GIT_DEPTH of 50 for gitlab
33+
logging.warning(
34+
"""
35+
[git-authors] Running on a GitLab runner might lead to wrong
36+
Git revision dates due to a shallow git fetch depth.
37+
38+
Make sure to set GIT_DEPTH to 1000 in your .gitlab-ci.yml file
39+
(see https://docs.gitlab.com/ee/user/project/pipelines/settings.html#git-shallow-clone).
40+
"""
41+
)
42+
43+
# Github Actions
44+
if os.getenv("GITHUB_ACTIONS") is not None and n_commits == 1:
45+
# Default is fetch-depth of 1 for github actions
46+
logging.warning(
47+
"""
48+
[git-authors] Running on GitHub Actions might lead to wrong
49+
Git revision dates due to a shallow git fetch depth.
50+
51+
Try setting fetch-depth to 0 in your GitHub Action
52+
(see https://github.com/actions/checkout).
53+
"""
54+
)
55+
56+
# Bitbucket pipelines
57+
if os.getenv("CI") is not None and n_commits < 50:
58+
# Default is fetch-depth of 50 for bitbucket pipelines
59+
logging.warning(
60+
"""
61+
[git-authors] Running on bitbucket pipelines might lead to wrong
62+
Git revision dates due to a shallow git fetch depth.
63+
64+
Try setting "clone: depth" to "full" in your pipeline
65+
(see https://support.atlassian.com/bitbucket-cloud/docs/configure-bitbucket-pipelinesyml/
66+
and search 'depth').
67+
"""
68+
)
69+
70+
# Azure Devops Pipeline
71+
# Does not limit fetch-depth by default
72+
if int(os.getenv("Agent.Source.Git.ShallowFetchDepth", 10e99)) < n_commits:
73+
logging.warning(
74+
"""
75+
[git-authors] Running on Azure pipelines with limited
76+
fetch-depth might lead to wrong git revision dates due to a shallow git fetch depth.
77+
78+
Remove any Shallow Fetch settings
79+
(see https://docs.microsoft.com/en-us/azure/devops/pipelines/repos/pipeline-options-for-git?view=azure-devops#shallow-fetch).
80+
"""
81+
)
82+
83+
84+
def commit_count() -> int:
85+
"""
86+
Determine the number of commits in a repository.
87+
88+
Returns:
89+
count (int): Number of commits.
90+
"""
91+
gc = GitCommand('rev-list',['--count','HEAD'])
92+
gc.run()
93+
n_commits = int(gc._stdout[0])
94+
assert n_commits >= 0
95+
return n_commits
96+
97+
98+
def is_shallow_clone() -> bool:
99+
"""
100+
Determine if repository is a shallow clone.
101+
102+
References & Context:
103+
https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/issues/10
104+
https://stackoverflow.com/a/37203240/5525118
105+
106+
Returns:
107+
bool: If a repo is shallow clone
108+
"""
109+
return os.path.exists(".git/shallow")

mkdocs_git_authors_plugin/plugin.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import re
22
from mkdocs.config import config_options
33
from mkdocs.plugins import BasePlugin
4+
45
from . import util
56
from .git.repo import Repo
7+
from mkdocs_git_authors_plugin.ci import raise_ci_warnings
68

79

810
class GitAuthorsPlugin(BasePlugin):
@@ -38,6 +40,8 @@ def on_config(self, config, **kwargs):
3840
"""
3941
self.repo().set_config(self.config)
4042

43+
raise_ci_warnings(path = self.repo()._root)
44+
4145
def on_files(self, files, config, **kwargs):
4246
"""
4347
Preprocess all markdown pages in the project.

0 commit comments

Comments
 (0)