Skip to content

Commit 93ca65c

Browse files
committed
support retrieve first commit timestamp with follow mode
1 parent 04ea4c2 commit 93ca65c

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

mkdocs_git_revision_date_localized_plugin/plugin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class GitRevisionDateLocalizedPlugin(BasePlugin):
4141
("timezone", config_options.Type(str, default="UTC")),
4242
("exclude", config_options.Type(list, default=[])),
4343
("enable_creation_date", config_options.Type(bool, default=False)),
44+
("creation_date_with_follow", config_options.Type(bool, default=False)),
4445
("enabled", config_options.Type(bool, default=True)),
4546
)
4647

@@ -255,6 +256,7 @@ def on_page_markdown(
255256
first_revision_timestamp = self.util.get_git_commit_timestamp(
256257
path=page.file.abs_src_path,
257258
is_first_commit=True,
259+
follow_mode=self.config.get("creation_date_with_follow")
258260
)
259261

260262
# Creation date formats

mkdocs_git_revision_date_localized_plugin/util.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ def _date_formats(
8585
def get_git_commit_timestamp(
8686
self,
8787
path: str,
88-
is_first_commit: bool = False
88+
is_first_commit: bool = False,
89+
follow_mode: bool = False
8990
) -> int:
9091
"""
9192
Get a list of commit dates in unix timestamp, starts with the most recent commit.
@@ -95,6 +96,7 @@ def get_git_commit_timestamp(
9596
else, get that of the most recent commit.
9697
path (str): Location of a markdown file that is part of a Git repository.
9798
is_first_commit (bool): retrieve commit timestamp when file was created.
99+
follow_mode (bool): retrieve first commit timestamp with follow mode.
98100
99101
Returns:
100102
int: commit date in unix timestamp, starts with the most recent commit.
@@ -112,7 +114,7 @@ def get_git_commit_timestamp(
112114
if is_first_commit:
113115
# diff_filter="A" will select the commit that created the file
114116
commit_timestamp = git.log(
115-
realpath, date="unix", format="%at", diff_filter="A", no_show_signature=True
117+
realpath, date="unix", format="%at", diff_filter="A", no_show_signature=True, follow=follow_mode
116118
)
117119
# A file can be created multiple times, through a file renamed.
118120
# Commits are ordered with most recent commit first
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Page for testing creation date follow mode
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+
creation_date_with_follow: True

tests/test_builds.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ def setup_commit_history(testproject_path):
155155
repo.git.add("docs/page_with_tag.md")
156156
repo.git.commit(message="update homepage", author=author, date="1642911026") # Sun Jan 23 2022 04:10:26 GMT+0000
157157

158+
bf_file_name = os.path.join(testproject_path, "docs/page_with_renamed.md")
159+
af_file_name = os.path.join(testproject_path, "docs/subfolder/page_with_renamed.md")
160+
# Since git.mv would actually remove the file, move page_with_renamed.md back to docs if it has been moved
161+
if os.path.exists(af_file_name):
162+
os.replace(af_file_name, bf_file_name)
163+
repo.git.add("docs/page_with_renamed.md")
164+
repo.git.commit(message="page_with_renamed.md before renamed", author=author, date="1655229469") # Tue Jun 14 2022 17:57:49 GMT+0000
165+
repo.git.mv("docs/page_with_renamed.md", "docs/subfolder/page_with_renamed.md")
166+
repo.git.commit(message="page_with_renamed.md after renamed", author=author, date="1655229515") # Tue Jun 14 2022 17:58:35 GMT+0000
167+
158168
repo.git.add("docs/first_page.md")
159169
repo.git.commit(message="first page", author=author, date="1500854705") # Mon Jul 24 2017 00:05:05 GMT+0000
160170
file_name = os.path.join(testproject_path, "docs/first_page.md")
@@ -254,6 +264,20 @@ def validate_build(testproject_path, plugin_config: dict = {}):
254264
searches = [x in contents for x in date_formats.values()]
255265
assert any(searches), "No correct creation date formats output was found"
256266

267+
if plugin_config.get("creation_date_with_follow"):
268+
commit_timestamp=repo.get_git_commit_timestamp(
269+
path=str(testproject_path / "docs/subfolder/page_with_renamed.md"),
270+
is_first_commit=True,
271+
follow_mode=True
272+
)
273+
assert commit_timestamp == 1655229469
274+
else:
275+
commit_timestamp=repo.get_git_commit_timestamp(
276+
path=str(testproject_path / "docs/subfolder/page_with_renamed.md"),
277+
is_first_commit=True,
278+
follow_mode=False
279+
)
280+
assert commit_timestamp == 1655229515
257281

258282
def validate_mkdocs_file(temp_path: str, mkdocs_yml_file: str):
259283
"""
@@ -289,6 +313,7 @@ def validate_mkdocs_file(temp_path: str, mkdocs_yml_file: str):
289313
'basic_project/mkdocs_with_override.yml',
290314
'basic_project/mkdocs_theme_language.yml',
291315
'basic_project/mkdocs_creation_date.yml',
316+
'basic_project/mkdocs_creation_date_with_follow.yml',
292317
'basic_project/mkdocs_theme_locale_disabled.yml',
293318
'basic_project/mkdocs_timeago_locale.yml',
294319
'basic_project/mkdocs_timeago.yml',
@@ -355,10 +380,10 @@ def test_tags_are_replaced(tmp_path, mkdocs_file):
355380
pytest.skip("Not necessary to test the JS library")
356381

357382
# Make sure count_commits() works
358-
# We created 8 commits in setup_commit_history()
383+
# We created 10 commits in setup_commit_history()
359384
with working_directory(testproject_path):
360385
u = Util()
361-
assert commit_count(u._get_repo("docs/page_with_tag.md")) == 8
386+
assert commit_count(u._get_repo("docs/page_with_tag.md")) == 10
362387

363388

364389
# the revision date was in 'setup_commit_history' was set to 1642911026 (Sun Jan 23 2022 04:10:26 GMT+0000)

0 commit comments

Comments
 (0)