Skip to content

Commit

Permalink
support retrieve first commit timestamp with follow mode
Browse files Browse the repository at this point in the history
  • Loading branch information
blueswen committed Jun 15, 2022
1 parent 04ea4c2 commit 93ca65c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
2 changes: 2 additions & 0 deletions mkdocs_git_revision_date_localized_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class GitRevisionDateLocalizedPlugin(BasePlugin):
("timezone", config_options.Type(str, default="UTC")),
("exclude", config_options.Type(list, default=[])),
("enable_creation_date", config_options.Type(bool, default=False)),
("creation_date_with_follow", config_options.Type(bool, default=False)),
("enabled", config_options.Type(bool, default=True)),
)

Expand Down Expand Up @@ -255,6 +256,7 @@ def on_page_markdown(
first_revision_timestamp = self.util.get_git_commit_timestamp(
path=page.file.abs_src_path,
is_first_commit=True,
follow_mode=self.config.get("creation_date_with_follow")
)

# Creation date formats
Expand Down
6 changes: 4 additions & 2 deletions mkdocs_git_revision_date_localized_plugin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ def _date_formats(
def get_git_commit_timestamp(
self,
path: str,
is_first_commit: bool = False
is_first_commit: bool = False,
follow_mode: bool = False
) -> int:
"""
Get a list of commit dates in unix timestamp, starts with the most recent commit.
Expand All @@ -95,6 +96,7 @@ def get_git_commit_timestamp(
else, get that of the most recent commit.
path (str): Location of a markdown file that is part of a Git repository.
is_first_commit (bool): retrieve commit timestamp when file was created.
follow_mode (bool): retrieve first commit timestamp with follow mode.
Returns:
int: commit date in unix timestamp, starts with the most recent commit.
Expand All @@ -112,7 +114,7 @@ def get_git_commit_timestamp(
if is_first_commit:
# diff_filter="A" will select the commit that created the file
commit_timestamp = git.log(
realpath, date="unix", format="%at", diff_filter="A", no_show_signature=True
realpath, date="unix", format="%at", diff_filter="A", no_show_signature=True, follow=follow_mode
)
# A file can be created multiple times, through a file renamed.
# Commits are ordered with most recent commit first
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/basic_project/docs/page_with_renamed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Page for testing creation date follow mode
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
site_name: test gitrevisiondatelocalized_plugin
use_directory_urls: true

plugins:
- search
- git-revision-date-localized:
enable_creation_date: True
creation_date_with_follow: True
29 changes: 27 additions & 2 deletions tests/test_builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ def setup_commit_history(testproject_path):
repo.git.add("docs/page_with_tag.md")
repo.git.commit(message="update homepage", author=author, date="1642911026") # Sun Jan 23 2022 04:10:26 GMT+0000

bf_file_name = os.path.join(testproject_path, "docs/page_with_renamed.md")
af_file_name = os.path.join(testproject_path, "docs/subfolder/page_with_renamed.md")
# Since git.mv would actually remove the file, move page_with_renamed.md back to docs if it has been moved
if os.path.exists(af_file_name):
os.replace(af_file_name, bf_file_name)
repo.git.add("docs/page_with_renamed.md")
repo.git.commit(message="page_with_renamed.md before renamed", author=author, date="1655229469") # Tue Jun 14 2022 17:57:49 GMT+0000
repo.git.mv("docs/page_with_renamed.md", "docs/subfolder/page_with_renamed.md")
repo.git.commit(message="page_with_renamed.md after renamed", author=author, date="1655229515") # Tue Jun 14 2022 17:58:35 GMT+0000

repo.git.add("docs/first_page.md")
repo.git.commit(message="first page", author=author, date="1500854705") # Mon Jul 24 2017 00:05:05 GMT+0000
file_name = os.path.join(testproject_path, "docs/first_page.md")
Expand Down Expand Up @@ -254,6 +264,20 @@ def validate_build(testproject_path, plugin_config: dict = {}):
searches = [x in contents for x in date_formats.values()]
assert any(searches), "No correct creation date formats output was found"

if plugin_config.get("creation_date_with_follow"):
commit_timestamp=repo.get_git_commit_timestamp(
path=str(testproject_path / "docs/subfolder/page_with_renamed.md"),
is_first_commit=True,
follow_mode=True
)
assert commit_timestamp == 1655229469
else:
commit_timestamp=repo.get_git_commit_timestamp(
path=str(testproject_path / "docs/subfolder/page_with_renamed.md"),
is_first_commit=True,
follow_mode=False
)
assert commit_timestamp == 1655229515

def validate_mkdocs_file(temp_path: str, mkdocs_yml_file: str):
"""
Expand Down Expand Up @@ -289,6 +313,7 @@ def validate_mkdocs_file(temp_path: str, mkdocs_yml_file: str):
'basic_project/mkdocs_with_override.yml',
'basic_project/mkdocs_theme_language.yml',
'basic_project/mkdocs_creation_date.yml',
'basic_project/mkdocs_creation_date_with_follow.yml',
'basic_project/mkdocs_theme_locale_disabled.yml',
'basic_project/mkdocs_timeago_locale.yml',
'basic_project/mkdocs_timeago.yml',
Expand Down Expand Up @@ -355,10 +380,10 @@ def test_tags_are_replaced(tmp_path, mkdocs_file):
pytest.skip("Not necessary to test the JS library")

# Make sure count_commits() works
# We created 8 commits in setup_commit_history()
# We created 10 commits in setup_commit_history()
with working_directory(testproject_path):
u = Util()
assert commit_count(u._get_repo("docs/page_with_tag.md")) == 8
assert commit_count(u._get_repo("docs/page_with_tag.md")) == 10


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

0 comments on commit 93ca65c

Please sign in to comment.