Skip to content

Commit c53dea5

Browse files
committed
Fix merge conflict
2 parents ccbb02b + 71d3920 commit c53dea5

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

Diff for: mkdocs_git_revision_date_localized_plugin/plugin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def on_config(self, config: config_options.Config, **kwargs) -> dict:
3434
Returns:
3535
dict: global configuration object
3636
"""
37-
self.util = Util(path=config["docs_dir"], config=self.config)
37+
self.util = Util(config=self.config)
3838

3939
# Get locale settings - might be added in future mkdocs versions
4040
# see: https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/issues/24

Diff for: mkdocs_git_revision_date_localized_plugin/util.py

+29-24
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,34 @@
11
import logging
2+
import os
23
import time
34
from datetime import datetime
45

56
from mkdocs_git_revision_date_localized_plugin.ci import raise_ci_warnings
67

78
from babel.dates import format_date, get_timezone
8-
from git import Repo, GitCommandError, GitCommandNotFound
9+
from git import Repo, GitCommandError, GitCommandNotFound, InvalidGitRepositoryError, NoSuchPathError
910

1011
logger = logging.getLogger("mkdocs.plugins")
1112

1213

1314
class Util:
14-
def __init__(self, path: str = ".", config={}):
15+
def __init__(self, config={}):
1516

1617
self.fallback_enabled = False
18+
self.config = config
19+
self.repo_cache = {}
1720

18-
try:
19-
git_repo = Repo(path, search_parent_directories=True)
20-
self.repo = git_repo.git
21-
except:
22-
if config.get("fallback_to_build_date"):
23-
self.fallback_enabled = True
24-
logger.warning(
25-
"[git-revision-date-localized-plugin] Unable to find a git directory and/or git is not installed."
26-
" Option 'fallback_to_build_date' set to 'true': Falling back to build date"
27-
)
28-
return None
29-
else:
30-
logger.error(
31-
"[git-revision-date-localized-plugin] Unable to find a git directory and/or git is not installed."
32-
" To ignore this error, set option 'fallback_to_build_date: true'"
33-
)
34-
raise
21+
def _get_repo(self, path: str):
22+
if not os.path.isdir(path):
23+
path = os.path.dirname(path)
24+
25+
if path not in self.repo_cache:
26+
self.repo_cache[path] = Repo(path, search_parent_directories=True).git
27+
# Checks if user is running builds on CI
28+
# and raise appropriate warnings
29+
raise_ci_warnings(self.repo_cache[path])
3530

36-
# Checks if user is running builds on CI
37-
# and raise appropriate warnings
38-
raise_ci_warnings(self.repo)
31+
return self.repo_cache[path]
3932

4033
@staticmethod
4134
def _date_formats(
@@ -94,8 +87,20 @@ def get_revision_date_for_file(
9487
if not self.fallback_enabled:
9588
# Retrieve author date in UNIX format (%at)
9689
# https://git-scm.com/docs/git-log#Documentation/git-log.txt-ematem
97-
unix_timestamp = self.repo.log(path, n=1, date="short", format="%at")
98-
90+
realpath = os.path.realpath(path)
91+
unix_timestamp = self._get_repo(realpath).log(realpath, n=1, date="short", format="%at")
92+
except (InvalidGitRepositoryError, NoSuchPathError) as err:
93+
if fallback_to_build_date:
94+
logger.warning(
95+
"[git-revision-date-localized-plugin] Unable to find a git directory and/or git is not installed."
96+
" Option 'fallback_to_build_date' set to 'true': Falling back to build date"
97+
)
98+
else:
99+
logger.error(
100+
"[git-revision-date-localized-plugin] Unable to find a git directory and/or git is not installed."
101+
" To ignore this error, set option 'fallback_to_build_date: true'"
102+
)
103+
raise err
99104
except GitCommandError as err:
100105
if fallback_to_build_date:
101106
logger.warning(

0 commit comments

Comments
 (0)