|
1 | 1 | import logging
|
| 2 | +import os |
2 | 3 | import time
|
3 | 4 | from datetime import datetime
|
4 | 5 |
|
5 | 6 | from mkdocs_git_revision_date_localized_plugin.ci import raise_ci_warnings
|
6 | 7 |
|
7 | 8 | from babel.dates import format_date, get_timezone
|
8 |
| -from git import Repo, GitCommandError, GitCommandNotFound |
| 9 | +from git import Repo, GitCommandError, GitCommandNotFound, InvalidGitRepositoryError, NoSuchPathError |
9 | 10 |
|
10 | 11 | logger = logging.getLogger("mkdocs.plugins")
|
11 | 12 |
|
12 | 13 |
|
13 | 14 | class Util:
|
14 |
| - def __init__(self, path: str = ".", config={}): |
| 15 | + def __init__(self, config={}): |
15 | 16 |
|
16 | 17 | self.fallback_enabled = False
|
| 18 | + self.config = config |
| 19 | + self.repo_cache = {} |
17 | 20 |
|
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]) |
35 | 30 |
|
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] |
39 | 32 |
|
40 | 33 | @staticmethod
|
41 | 34 | def _date_formats(
|
@@ -94,8 +87,20 @@ def get_revision_date_for_file(
|
94 | 87 | if not self.fallback_enabled:
|
95 | 88 | # Retrieve author date in UNIX format (%at)
|
96 | 89 | # 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 |
99 | 104 | except GitCommandError as err:
|
100 | 105 | if fallback_to_build_date:
|
101 | 106 | logger.warning(
|
|
0 commit comments