From 93ca65c2969390a14e3a65ba4e6394fe8d89e3d0 Mon Sep 17 00:00:00 2001 From: blueswen Date: Wed, 15 Jun 2022 23:44:40 +0800 Subject: [PATCH 1/2] support retrieve first commit timestamp with follow mode --- .../plugin.py | 2 ++ .../util.py | 6 ++-- .../basic_project/docs/page_with_renamed.md | 1 + .../mkdocs_creation_date_with_follow.yml | 8 +++++ tests/test_builds.py | 29 +++++++++++++++++-- 5 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/basic_project/docs/page_with_renamed.md create mode 100644 tests/fixtures/basic_project/mkdocs_creation_date_with_follow.yml diff --git a/mkdocs_git_revision_date_localized_plugin/plugin.py b/mkdocs_git_revision_date_localized_plugin/plugin.py index 9d609fc..592080c 100644 --- a/mkdocs_git_revision_date_localized_plugin/plugin.py +++ b/mkdocs_git_revision_date_localized_plugin/plugin.py @@ -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)), ) @@ -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 diff --git a/mkdocs_git_revision_date_localized_plugin/util.py b/mkdocs_git_revision_date_localized_plugin/util.py index 50605fa..b10e2fb 100644 --- a/mkdocs_git_revision_date_localized_plugin/util.py +++ b/mkdocs_git_revision_date_localized_plugin/util.py @@ -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. @@ -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. @@ -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 diff --git a/tests/fixtures/basic_project/docs/page_with_renamed.md b/tests/fixtures/basic_project/docs/page_with_renamed.md new file mode 100644 index 0000000..2e4b2f2 --- /dev/null +++ b/tests/fixtures/basic_project/docs/page_with_renamed.md @@ -0,0 +1 @@ +# Page for testing creation date follow mode diff --git a/tests/fixtures/basic_project/mkdocs_creation_date_with_follow.yml b/tests/fixtures/basic_project/mkdocs_creation_date_with_follow.yml new file mode 100644 index 0000000..563647c --- /dev/null +++ b/tests/fixtures/basic_project/mkdocs_creation_date_with_follow.yml @@ -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 diff --git a/tests/test_builds.py b/tests/test_builds.py index fe338ff..4231760 100644 --- a/tests/test_builds.py +++ b/tests/test_builds.py @@ -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") @@ -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): """ @@ -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', @@ -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) From 45a94a9dcbc0bf8155b1ddcc8cd8faf74f034afd Mon Sep 17 00:00:00 2001 From: blueswen Date: Tue, 28 Jun 2022 01:08:38 +0800 Subject: [PATCH 2/2] remove creation_date_with_follow options, alway retrieve first commit with follow mode --- .../plugin.py | 2 -- .../util.py | 6 ++---- .../mkdocs_creation_date_with_follow.yml | 8 -------- tests/test_builds.py | 20 +++++-------------- 4 files changed, 7 insertions(+), 29 deletions(-) delete mode 100644 tests/fixtures/basic_project/mkdocs_creation_date_with_follow.yml diff --git a/mkdocs_git_revision_date_localized_plugin/plugin.py b/mkdocs_git_revision_date_localized_plugin/plugin.py index 592080c..9d609fc 100644 --- a/mkdocs_git_revision_date_localized_plugin/plugin.py +++ b/mkdocs_git_revision_date_localized_plugin/plugin.py @@ -41,7 +41,6 @@ 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)), ) @@ -256,7 +255,6 @@ 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 diff --git a/mkdocs_git_revision_date_localized_plugin/util.py b/mkdocs_git_revision_date_localized_plugin/util.py index b10e2fb..7ed255f 100644 --- a/mkdocs_git_revision_date_localized_plugin/util.py +++ b/mkdocs_git_revision_date_localized_plugin/util.py @@ -85,8 +85,7 @@ def _date_formats( def get_git_commit_timestamp( self, path: str, - is_first_commit: bool = False, - follow_mode: bool = False + is_first_commit: bool = False ) -> int: """ Get a list of commit dates in unix timestamp, starts with the most recent commit. @@ -96,7 +95,6 @@ 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. @@ -114,7 +112,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, follow=follow_mode + realpath, date="unix", format="%at", diff_filter="A", no_show_signature=True, follow=True ) # A file can be created multiple times, through a file renamed. # Commits are ordered with most recent commit first diff --git a/tests/fixtures/basic_project/mkdocs_creation_date_with_follow.yml b/tests/fixtures/basic_project/mkdocs_creation_date_with_follow.yml deleted file mode 100644 index 563647c..0000000 --- a/tests/fixtures/basic_project/mkdocs_creation_date_with_follow.yml +++ /dev/null @@ -1,8 +0,0 @@ -site_name: test gitrevisiondatelocalized_plugin -use_directory_urls: true - -plugins: - - search - - git-revision-date-localized: - enable_creation_date: True - creation_date_with_follow: True diff --git a/tests/test_builds.py b/tests/test_builds.py index 4231760..3f0f8ea 100644 --- a/tests/test_builds.py +++ b/tests/test_builds.py @@ -264,20 +264,11 @@ 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 + commit_timestamp=repo.get_git_commit_timestamp( + path=str(testproject_path / "docs/subfolder/page_with_renamed.md"), + is_first_commit=True + ) + assert commit_timestamp == 1655229469 def validate_mkdocs_file(temp_path: str, mkdocs_yml_file: str): """ @@ -313,7 +304,6 @@ 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',