|
1 | 1 | # standard library
|
2 | 2 | import logging
|
| 3 | +import os |
3 | 4 | import time
|
4 | 5 | from datetime import datetime
|
5 | 6 |
|
|
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 |
| - logging.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 |
| - logging.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 path not in self.repo_cache: |
| 23 | + self.repo_cache[path] = Repo(path, search_parent_directories=True).git |
| 24 | + # Checks if user is running builds on CI |
| 25 | + # and raise appropriate warnings |
| 26 | + raise_ci_warnings(self.repo_cache[path]) |
35 | 27 |
|
36 |
| - # Checks if user is running builds on CI |
37 |
| - # and raise appropriate warnings |
38 |
| - raise_ci_warnings(self.repo) |
| 28 | + return self.repo_cache[path] |
39 | 29 |
|
40 | 30 | @staticmethod
|
41 | 31 | def _date_formats(
|
@@ -94,7 +84,8 @@ def get_revision_date_for_file(
|
94 | 84 | if not self.fallback_enabled:
|
95 | 85 | # Retrieve author date in UNIX format (%at)
|
96 | 86 | # 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") |
| 87 | + realpath = os.path.realpath(path) |
| 88 | + unix_timestamp = self._get_repo(realpath).log(realpath, n=1, date="short", format="%at") |
98 | 89 |
|
99 | 90 | except GitCommandError as err:
|
100 | 91 | if fallback_to_build_date:
|
|
0 commit comments