Skip to content

Commit 4d0ab69

Browse files
authored
fix windows issue
1 parent 9126650 commit 4d0ab69

File tree

5 files changed

+36
-20
lines changed

5 files changed

+36
-20
lines changed

docs/options.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ Default is `true`. When enabled, the logs will show warnings when something is w
159159

160160
## `ignored_commits_file`
161161

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.
162+
Default is `None`. You can specify a file path (relative to your `mkdocs.yml` directory) that contains a list of commit hashes to ignore
163+
when determining the revision date. The format of the file is the same as the format of
164+
git `blame.ignoreRevsFile`. This can be useful to ignore specific commits that apply formatting updates or other mass changes to the documents.
165165

166166

167167
=== ":octicons-file-code-16: mkdocs.yml"

src/mkdocs_git_revision_date_localized_plugin/plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
6767

6868
assert self.config['type'] in ["date","datetime","iso_date","iso_datetime","timeago","custom"]
6969

70-
self.util = Util(config=self.config)
70+
self.util = Util(config=self.config, mkdocs_dir=os.path.abspath(os.path.dirname(config.get('config_file_path'))))
7171

7272
# Save last commit timestamp for entire site
7373
self.last_site_revision_timestamp = self.util.get_git_commit_timestamp(

src/mkdocs_git_revision_date_localized_plugin/util.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ class Util:
2727
This helps find git and calculate relevant dates.
2828
"""
2929

30-
def __init__(self, config={}):
30+
def __init__(self, config: Dict, mkdocs_dir: str):
3131
"""Initialize utility class."""
3232
self.config = config
3333
self.repo_cache = {}
3434

3535
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 []
36+
ignore_commits_filepath = os.path.join(mkdocs_dir, ignore_commits_file) if ignore_commits_file else None
37+
self.ignored_commits = self.parse_git_ignore_revs(ignore_commits_filepath) if ignore_commits_file else []
3738

3839
def _get_repo(self, path: str) -> Git:
3940
if not os.path.isdir(path):
@@ -66,6 +67,7 @@ def get_git_commit_timestamp(
6667
int: commit date in unix timestamp, starts with the most recent commit.
6768
"""
6869
commit_timestamp = ""
70+
n_ignored_commits = 0
6971

7072
# Determine the logging level
7173
# Only log warnings when plugin is set to strict.
@@ -111,14 +113,17 @@ def get_git_commit_timestamp(
111113
).split("\n")
112114

113115
# 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
116+
# ignored list. If the line is empty, we've reached the end and need to use the fallback behavior.
115117
for line in lines:
116118
if not line:
117119
commit_timestamp = ""
118120
break
119121
commit, commit_timestamp = line.split(" ")
120122
if not any(commit.startswith(x) for x in self.ignored_commits):
121123
break
124+
else:
125+
n_ignored_commits += 1
126+
122127

123128
except (InvalidGitRepositoryError, NoSuchPathError) as err:
124129
if self.config.get('fallback_to_build_date'):
@@ -165,10 +170,13 @@ def get_git_commit_timestamp(
165170
# create timestamp
166171
if commit_timestamp == "":
167172
commit_timestamp = time.time()
168-
log(
173+
msg = (
169174
"[git-revision-date-localized-plugin] '%s' has no git logs, using current timestamp"
170175
% path
171176
)
177+
if n_ignored_commits:
178+
msg += f" (ignored {n_ignored_commits} commits)"
179+
log(msg)
172180

173181
return int(commit_timestamp)
174182

@@ -222,10 +230,17 @@ def parse_git_ignore_revs(filename: str) -> List[str]:
222230
Whitespace, blanklines and comments starting with # are all ignored.
223231
"""
224232
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)
233+
234+
try:
235+
with open(filename, "rt", encoding='utf-8') as f:
236+
for line in f:
237+
line = line.split("#", 1)[0].strip()
238+
if not line:
239+
continue
240+
result.append(line)
241+
except FileNotFoundError:
242+
logger.error(f"File not found: {filename}")
243+
except Exception as e:
244+
logger.error(f"An error occurred while reading the file {filename}: {e}")
245+
231246
return result
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
site/

tests/test_builds.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def validate_build(testproject_path, plugin_config: dict = {}):
254254
contents = page_with_tag.read_text(encoding="utf8")
255255
assert re.search(r"renders as\:\s[<span>|\w].+", contents)
256256

257-
repo = Util(config=plugin_config)
257+
repo = Util(config=plugin_config, mkdocs_dir=testproject_path)
258258
date_formats = repo.get_date_formats_for_timestamp(
259259
commit_timestamp=repo.get_git_commit_timestamp(
260260
path=str(testproject_path / "docs/page_with_tag.md"),
@@ -380,7 +380,7 @@ def test_tags_are_replaced(tmp_path, mkdocs_file):
380380
# Make sure count_commits() works
381381
# We created 11 commits in setup_commit_history()
382382
with working_directory(testproject_path):
383-
u = Util()
383+
u = Util(config={}, mkdocs_dir=os.getcwd())
384384
assert commit_count(u._get_repo("docs/page_with_tag.md")) == 11
385385

386386

@@ -682,7 +682,7 @@ def test_ignored_commits(tmp_path):
682682

683683
# First test that the middle commit doesn't show up by default
684684
# January 23, 2022 is the date of the most recent commit
685-
with open(str(testproject_path / "ignored-commits.txt"), "wt") as fp:
685+
with open(str(testproject_path / "ignored-commits.txt"), "wt", encoding="utf-8") as fp:
686686
fp.write("")
687687

688688
result = build_docs_setup(testproject_path)
@@ -694,10 +694,10 @@ def test_ignored_commits(tmp_path):
694694

695695
# Now mark the most recent change to page_with_tag as ignored
696696
# 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)
697+
commit_hash = repo.git.log("docs/page_with_tag.md", format="%H", n=1)
698698

699-
with open(str(testproject_path / "ignored-commits.txt"), "wt") as fp:
700-
fp.write(hash)
699+
with open(str(testproject_path / "ignored-commits.txt"), "wt", encoding="utf-8") as fp:
700+
fp.write(commit_hash)
701701

702702
# should not raise warning
703703
result = build_docs_setup(testproject_path)

0 commit comments

Comments
 (0)