Skip to content

Commit db89b20

Browse files
authored
fix merge
1 parent e777f78 commit db89b20

File tree

6 files changed

+120
-4
lines changed

6 files changed

+120
-4
lines changed

docs/options.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ You can customize the plugin by setting options in `mkdocs.yml`. For example:
1818
enable_git_follow: true
1919
enabled: true
2020
strict: true
21+
ignored_commits_file: .git-blame-ignore-revs
2122
```
2223
2324
## `type`
@@ -155,3 +156,18 @@ Default is `true`. When enabled, the logs will show warnings when something is w
155156
- git-revision-date-localized:
156157
strict: true
157158
```
159+
160+
## `ignored_commits_file`
161+
162+
Default is `None`. When specified, contains a file that contains a list of commit hashes to ignore
163+
when determining the most recent updated time. The format of the file is the same as the format of
164+
git `blame.ignoreRevsFile`. This can be useful to ignore formatting updates or other mass changes to the documents.
165+
166+
167+
=== ":octicons-file-code-16: mkdocs.yml"
168+
169+
```yaml
170+
plugins:
171+
- git-revision-date-localized:
172+
ignored_commits_file: .git-blame-ignore-revs
173+
```

src/mkdocs_git_revision_date_localized_plugin/plugin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class GitRevisionDateLocalizedPlugin(BasePlugin):
4444
("enabled", config_options.Type(bool, default=True)),
4545
("strict", config_options.Type(bool, default=True)),
4646
("enable_git_follow", config_options.Type(bool, default=True))
47+
("ignored_commits_file", config_options.Type(str, default=None)),
4748
)
4849

4950
def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:

src/mkdocs_git_revision_date_localized_plugin/util.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
NoSuchPathError,
1717
)
1818

19-
from typing import Dict
19+
from typing import Any, Dict, List
2020

2121
logger = logging.getLogger("mkdocs.plugins")
2222

@@ -32,6 +32,9 @@ def __init__(self, config={}):
3232
self.config = config
3333
self.repo_cache = {}
3434

35+
ignore_commits_file = self.config.get("ignored_commits_file")
36+
self.ignored_commits = self.parse_git_ignore_revs(ignore_commits_file) if ignore_commits_file else []
37+
3538
def _get_repo(self, path: str) -> Git:
3639
if not os.path.isdir(path):
3740
path = os.path.dirname(path)
@@ -82,6 +85,7 @@ def get_git_commit_timestamp(
8285

8386
follow_option=self.config.get('enable_git_follow')
8487

88+
# Ignored commits are only considered for the most recent update, not for creation
8589
if is_first_commit:
8690
# diff_filter="A" will select the commit that created the file
8791
commit_timestamp = git.log(
@@ -100,6 +104,22 @@ def get_git_commit_timestamp(
100104
ignore_all_space=True, ignore_blank_lines=True
101105
)
102106

107+
# Retrieve the history for the file in the format <hash> <timestamp>
108+
# The maximum number of commits we will ever need to examine is 1 more than the number of ignored commits.
109+
lines = git.log(
110+
realpath, date="unix", format="%H %at", n=len(self.ignored_commits)+1, no_show_signature=True,
111+
).split("\n")
112+
113+
# process the commits for the file in reverse-chronological order. Ignore any commit that is on the
114+
# ignored list. If the line is empty, we've reached the end and need to use the fallback behavior
115+
for line in lines:
116+
if not line:
117+
commit_timestamp = ""
118+
break
119+
commit, commit_timestamp = line.split(" ")
120+
if not any(commit.startswith(x) for x in self.ignored_commits):
121+
break
122+
103123
except (InvalidGitRepositoryError, NoSuchPathError) as err:
104124
if self.config.get('fallback_to_build_date'):
105125
log(
@@ -193,3 +213,19 @@ def add_spans(date_formats: Dict[str, str]) -> Dict[str, str]:
193213
% (date_type, datetime_string, date_string)
194214
)
195215
return date_formats
216+
217+
@staticmethod
218+
def parse_git_ignore_revs(filename: str) -> List[str]:
219+
"""
220+
Parses a file that is the same format as git's blame.ignoreRevsFile and return the list of commit hashes.
221+
222+
Whitespace, blanklines and comments starting with # are all ignored.
223+
"""
224+
result = []
225+
with open(filename, "rt") as f:
226+
for line in f:
227+
line = line.split("#", 1)[0].strip()
228+
if not line:
229+
continue
230+
result.append(line)
231+
return result
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
site_name: test gitrevisiondatelocalized_plugin
2+
use_directory_urls: true
3+
4+
plugins:
5+
- search
6+
- git-revision-date-localized:
7+
enable_creation_date: True
8+
ignored_commits_file: ignored-commits.txt

tests/test_builds.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,15 @@ def setup_commit_history(testproject_path):
160160
repo.git.commit(message="add homepage", author=author, date="1500854705") # Mon Jul 24 2017 00:05:05 GMT+0000
161161

162162
file_name = os.path.join(testproject_path, "docs/page_with_tag.md")
163+
with open(file_name, "a") as the_file:
164+
the_file.write("test\n")
165+
repo.git.add("docs/page_with_tag.md")
166+
repo.git.commit(message="update homepage #1", author=author, date="1525475836") # Fri May 04 2018 23:17:16 GMT+0000
167+
163168
with open(file_name, "a") as the_file:
164169
the_file.write("awa\n")
165170
repo.git.add("docs/page_with_tag.md")
166-
repo.git.commit(message="update homepage", author=author, date="1642911026") # Sun Jan 23 2022 04:10:26 GMT+0000
171+
repo.git.commit(message="update homepage #2", author=author, date="1642911026") # Sun Jan 23 2022 04:10:26 GMT+0000
167172

168173
if os.path.exists("docs/page_with_renamed.md"):
169174
bf_file_name = os.path.join(testproject_path, "docs/page_with_renamed.md")
@@ -373,10 +378,10 @@ def test_tags_are_replaced(tmp_path, mkdocs_file):
373378
pytest.skip("Not necessary to test the JS library")
374379

375380
# Make sure count_commits() works
376-
# We created 10 commits in setup_commit_history()
381+
# We created 11 commits in setup_commit_history()
377382
with working_directory(testproject_path):
378383
u = Util()
379-
assert commit_count(u._get_repo("docs/page_with_tag.md")) == 10
384+
assert commit_count(u._get_repo("docs/page_with_tag.md")) == 11
380385

381386

382387
# the revision date was in 'setup_commit_history' was set to 1642911026 (Sun Jan 23 2022 04:10:26 GMT+0000)
@@ -667,3 +672,37 @@ def test_mkdocs_genfiles_plugin(tmp_path):
667672
validate_build(
668673
testproject_path, plugin_config
669674
)
675+
676+
677+
def test_ignored_commits(tmp_path):
678+
testproject_path = setup_clean_mkdocs_folder(
679+
"tests/fixtures/basic_project/mkdocs_ignored_commits.yml", tmp_path
680+
)
681+
repo = setup_commit_history(testproject_path)
682+
683+
# First test that the middle commit doesn't show up by default
684+
# January 23, 2022 is the date of the most recent commit
685+
with open(str(testproject_path / "ignored-commits.txt"), "wt") as fp:
686+
fp.write("")
687+
688+
result = build_docs_setup(testproject_path)
689+
assert result.exit_code == 0
690+
691+
page_with_tag = testproject_path / "site/page_with_tag/index.html"
692+
contents = page_with_tag.read_text(encoding="utf8")
693+
assert "January 23, 2022" in contents
694+
695+
# Now mark the most recent change to page_with_tag as ignored
696+
# May 4, 2018 is the date of the second most recent commit
697+
hash = repo.git.log("docs/page_with_tag.md", format="%H", n=1)
698+
699+
with open(str(testproject_path / "ignored-commits.txt"), "wt") as fp:
700+
fp.write(hash)
701+
702+
# should not raise warning
703+
result = build_docs_setup(testproject_path)
704+
assert result.exit_code == 0
705+
706+
page_with_tag = testproject_path / "site/page_with_tag/index.html"
707+
contents = page_with_tag.read_text(encoding="utf8")
708+
assert "May 4, 2018" in contents

tests/test_parse_git_ignore_revs.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from mkdocs_git_revision_date_localized_plugin.util import Util
2+
import pytest
3+
import tempfile
4+
5+
TEST_PARAMS = [
6+
(b"abc123\n", ["abc123"]),
7+
(b"abc123 # comments are ignored\n", ["abc123"]),
8+
(b"\n\n\n\n\nabc123\n\n\n\n\n", ["abc123"]),
9+
]
10+
11+
@pytest.mark.parametrize("test_input,expected", TEST_PARAMS)
12+
def test_parse_git_ignore_revs(test_input, expected):
13+
with tempfile.NamedTemporaryFile() as fp:
14+
fp.write(test_input)
15+
fp.flush()
16+
assert Util.parse_git_ignore_revs(fp.name) == expected

0 commit comments

Comments
 (0)