Skip to content

Commit 927324d

Browse files
fpozzobontimvink
authored andcommitted
Optimise logs
Usage of logs slows down significantly generation of the docs, adding a cache mechanism
1 parent 370ee36 commit 927324d

1 file changed

Lines changed: 27 additions & 16 deletions

File tree

  • src/mkdocs_git_authors_plugin/git

src/mkdocs_git_authors_plugin/git/page.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ def __init__(self, repo: Repo, path: Path, strict: bool) -> None:
3232
self._total_lines = 0
3333
self._authors: List[dict] = list()
3434
self._strict = strict
35+
# Cache Logs, indexed by 40 char SHA
36+
self._cached_logs = {}
3537

3638
try:
3739
self._process_git_blame()
@@ -238,18 +240,29 @@ def _process_git_log(self, sha, commit) -> None:
238240
--- (this method works through side effects)
239241
"""
240242

241-
args = []
242-
args.append("-1") # Only existing sha
243-
args.append(sha)
244-
cmd = GitCommand("log", args)
245-
cmd.run()
246-
247-
lines = cmd.stdout()
248-
249-
# in case of empty, non-committed files, raise error
250-
if len(lines) == 0:
251-
raise GitCommandError
252-
243+
co_authors = self._get_git_log(sha, commit)
244+
for co_author in co_authors:
245+
# Create the co-author
246+
if co_author not in self._authors:
247+
self._authors.append(co_author)
248+
co_author.add_lines(self, commit)
249+
250+
def _get_git_log(self, sha, commit):
251+
if self._cached_logs.get(sha) is None:
252+
args = ["-1", sha]
253+
cmd = GitCommand("log", args)
254+
cmd.run()
255+
256+
lines = cmd.stdout()
257+
258+
# in case of empty, non-committed files, raise error
259+
if len(lines) == 0:
260+
raise GitCommandError
261+
self._cached_logs[sha] = self._parse_git_log(lines, commit)
262+
return self._cached_logs.get(sha)
263+
264+
def _parse_git_log(self, lines, commit):
265+
parsed_logs = []
253266
ignore_authors = self.repo().config("ignore_authors")
254267
for line in lines:
255268
if line.startswith("Author: "):
@@ -264,10 +277,8 @@ def _process_git_log(self, sha, commit) -> None:
264277
co_author.email() not in ignore_authors
265278
and co_author.email() != commit.author().email()
266279
):
267-
# Create the co-author
268-
if co_author not in self._authors:
269-
self._authors.append(co_author)
270-
co_author.add_lines(self, commit)
280+
parsed_logs.append(co_author)
281+
return parsed_logs
271282

272283
def path(self) -> Path:
273284
"""

0 commit comments

Comments
 (0)