@@ -29,7 +29,6 @@ class Util:
29
29
30
30
def __init__ (self , config = {}):
31
31
"""Initialize utility class."""
32
- self .fallback_enabled = False
33
32
self .config = config
34
33
self .repo_cache = {}
35
34
@@ -82,8 +81,7 @@ def _date_formats(
82
81
def get_git_commit_timestamp (
83
82
self ,
84
83
path : str ,
85
- is_first_commit : bool ,
86
- fallback_to_build_date : bool = False ,
84
+ is_first_commit : bool = False
87
85
) -> int :
88
86
"""
89
87
Get a list of commit dates in unix timestamp, starts with the most recent commit.
@@ -92,47 +90,50 @@ def get_git_commit_timestamp(
92
90
is_first_commit (bool): if true, get the timestamp of the first commit,
93
91
else, get that of the most recent commit.
94
92
path (str): Location of a markdown file that is part of a Git repository.
93
+ is_first_commit (bool): retrieve commit timestamp when file was created.
95
94
96
95
Returns:
97
- list : commit dates in unix timestamp, starts with the most recent commit.
96
+ int : commit date in unix timestamp, starts with the most recent commit.
98
97
"""
99
-
100
98
commit_timestamp = ""
101
99
102
100
# perform git log operation
103
101
try :
104
- if not self .fallback_enabled :
105
- # Retrieve author date in UNIX format (%at)
106
- # https://git-scm.com/docs/git-log#Documentation/git-log.txt-ematem
107
- realpath = os .path .realpath (path )
108
- git = self ._get_repo (realpath )
109
- if is_first_commit :
110
- commit_timestamp = git .log (
111
- realpath , date = "short" , format = "%at" , diff_filter = "A"
112
- )
113
- else :
114
- commit_timestamp = git .log (
115
- realpath , date = "short" , format = "%at" , n = 1
116
- )
102
+ # Retrieve author date in UNIX format (%at)
103
+ # https://git-scm.com/docs/git-log#Documentation/git-log.txt-ematem
104
+ # https://git-scm.com/docs/git-log#Documentation/git-log.txt---diff-filterACDMRTUXB82308203
105
+ realpath = os .path .realpath (path )
106
+ git = self ._get_repo (realpath )
107
+ if is_first_commit :
108
+ # diff_filter="A" will select the commit that created the file
109
+ commit_timestamp = git .log (
110
+ realpath , date = "short" , format = "%at" , diff_filter = "A"
111
+ )
112
+ else :
113
+ commit_timestamp = git .log (
114
+ realpath , date = "short" , format = "%at" , n = 1
115
+ )
117
116
except (InvalidGitRepositoryError , NoSuchPathError ) as err :
118
- if fallback_to_build_date :
117
+ if self . config . get ( ' fallback_to_build_date' ) :
119
118
logger .warning (
120
119
"[git-revision-date-localized-plugin] Unable to find a git directory and/or git is not installed."
121
120
" Option 'fallback_to_build_date' set to 'true': Falling back to build date"
122
121
)
122
+ commit_timestamp = time .time ()
123
123
else :
124
124
logger .error (
125
125
"[git-revision-date-localized-plugin] Unable to find a git directory and/or git is not installed."
126
126
" To ignore this error, set option 'fallback_to_build_date: true'"
127
127
)
128
128
raise err
129
129
except GitCommandError as err :
130
- if fallback_to_build_date :
130
+ if self . config . get ( ' fallback_to_build_date' ) :
131
131
logger .warning (
132
132
"[git-revision-date-localized-plugin] Unable to read git logs of '%s'. Is git log readable?"
133
133
" Option 'fallback_to_build_date' set to 'true': Falling back to build date"
134
134
% path
135
135
)
136
+ commit_timestamp = time .time ()
136
137
else :
137
138
logger .error (
138
139
"[git-revision-date-localized-plugin] Unable to read git logs of '%s'. "
@@ -141,11 +142,12 @@ def get_git_commit_timestamp(
141
142
)
142
143
raise err
143
144
except GitCommandNotFound as err :
144
- if fallback_to_build_date :
145
+ if self . config . get ( ' fallback_to_build_date' ) :
145
146
logger .warning (
146
147
"[git-revision-date-localized-plugin] Unable to perform command: 'git log'. Is git installed?"
147
148
" Option 'fallback_to_build_date' set to 'true': Falling back to build date"
148
149
)
150
+ commit_timestamp = time .time ()
149
151
else :
150
152
logger .error (
151
153
"[git-revision-date-localized-plugin] Unable to perform command 'git log'. Is git installed?"
@@ -156,19 +158,16 @@ def get_git_commit_timestamp(
156
158
# create timestamp
157
159
if commit_timestamp == "" :
158
160
commit_timestamp = time .time ()
159
- if not self .fallback_enabled :
160
- logger .warning (
161
- "[git-revision-date-localized-plugin] '%s' has no git logs, using current timestamp"
162
- % path
163
- )
161
+ logger .warning (
162
+ "[git-revision-date-localized-plugin] '%s' has no git logs, using current timestamp"
163
+ % path
164
+ )
164
165
165
166
return int (commit_timestamp )
166
167
167
- def get_revision_date_for_file (
168
+ def get_date_formats_for_timestamp (
168
169
self ,
169
170
commit_timestamp : int ,
170
- locale : str = "en" ,
171
- time_zone : str = "UTC" ,
172
171
) -> Dict [str , str ]:
173
172
"""
174
173
Determine localized date variants for a given file.
@@ -183,7 +182,9 @@ def get_revision_date_for_file(
183
182
"""
184
183
185
184
date_formats = self ._date_formats (
186
- unix_timestamp = commit_timestamp , time_zone = time_zone , locale = locale
185
+ unix_timestamp = commit_timestamp ,
186
+ time_zone = self .config .get ("time_zone" ),
187
+ locale = self .config .get ("locale" )
187
188
)
188
189
189
190
# Wrap in <span> for styling
@@ -195,33 +196,3 @@ def get_revision_date_for_file(
195
196
196
197
return date_formats
197
198
198
- def get_creation_date_for_file (
199
- self ,
200
- commit_timestamp : int ,
201
- locale : str = "en" ,
202
- time_zone : str = "UTC" ,
203
- ) -> Dict [str , str ]:
204
- """
205
- Determine localized date variants for a given file.
206
-
207
- Args:
208
- commit_timestamp (int): the first commit date in unix timestamp.
209
- locale (str, optional): Locale code of language to use. Defaults to 'en'.
210
- time_zone (str): Timezone database name (https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).
211
-
212
- Returns:
213
- dict: Localized date variants.
214
- """
215
-
216
- date_formats = self ._date_formats (
217
- unix_timestamp = commit_timestamp , time_zone = time_zone , locale = locale
218
- )
219
-
220
- # Wrap in <span> for styling
221
- for date_type , date_string in date_formats .items ():
222
- date_formats [date_type ] = (
223
- '<span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-%s">%s</span>'
224
- % (date_type , date_string )
225
- )
226
-
227
- return date_formats
0 commit comments