diff options
author | Yobmod <yobmod@gmail.com> | 2021-08-02 22:52:10 +0100 |
---|---|---|
committer | Yobmod <yobmod@gmail.com> | 2021-08-02 22:52:10 +0100 |
commit | bc9bcf51ef68385895d8cdbc76098d6b493cd1b6 (patch) | |
tree | 581e20a60da9cbc01315eadc6054a0efd96bfdea /git/repo | |
parent | 1aaa7048ddecb4509e1c279e28de5ef71477e71f (diff) | |
download | gitpython-bc9bcf51ef68385895d8cdbc76098d6b493cd1b6.tar.gz |
Test Dataclass in repo.base.blame() 5
Diffstat (limited to 'git/repo')
-rw-r--r-- | git/repo/base.py | 67 |
1 files changed, 37 insertions, 30 deletions
diff --git a/git/repo/base.py b/git/repo/base.py index 2bfc4677..a0aee322 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -7,6 +7,7 @@ from __future__ import annotations import logging import os import re +from dataclasses import dataclass import shlex import warnings from gitdb.db.loose import LooseObjectDB @@ -41,10 +42,10 @@ import gitdb from git.types import TBD, PathLike, Lit_config_levels, Commit_ish, Tree_ish, assert_never from typing import (Any, BinaryIO, Callable, Dict, Iterator, List, Mapping, Optional, Sequence, - TextIO, Tuple, Type, Union, + TextIO, Tuple, Type, TypedDict, Union, NamedTuple, cast, TYPE_CHECKING) -from git.types import ConfigLevels_Tup, TypedDict +from git.types import ConfigLevels_Tup if TYPE_CHECKING: from git.util import IterableList @@ -889,7 +890,7 @@ class Repo(object): commits: Dict[str, Commit] = {} blames: List[List[Commit | List[str | bytes] | None]] = [] - class InfoTD(TypedDict, total=False): + class InfoTC(TypedDict, total=False): sha: str id: str filename: str @@ -901,7 +902,21 @@ class Repo(object): committer_email: str committer_date: int - info: InfoTD = {} + @dataclass + class InfoDC(Dict[str, Union[str, int]]): + sha: str = '' + id: str = '' + filename: str = '' + summary: str = '' + author: str = '' + author_email: str = '' + author_date: int = 0 + committer: str = '' + committer_email: str = '' + committer_date: int = 0 + + # info: InfoTD = {} + info = InfoDC() keepends = True for line_bytes in data.splitlines(keepends): @@ -909,7 +924,7 @@ class Repo(object): line_str = line_bytes.rstrip().decode(defenc) except UnicodeDecodeError: firstpart = '' - parts = [] + parts = [''] is_binary = True else: # As we don't have an idea when the binary data ends, as it could contain multiple newlines @@ -928,10 +943,10 @@ class Repo(object): # another line of blame with the same data digits = parts[-1].split(" ") if len(digits) == 3: - info = {'id': firstpart} + info.id = firstpart blames.append([None, []]) - elif info['id'] != firstpart: - info = {'id': firstpart} + elif info.id != firstpart: + info.id = firstpart blames.append([commits.get(firstpart), []]) # END blame data initialization else: @@ -947,20 +962,12 @@ class Repo(object): # committer-time 1192271832 # committer-tz -0700 - IGNORED BY US role = m.group(0) - if role == 'author': - if firstpart.endswith('-mail'): - info["author_email"] = parts[-1] - elif firstpart.endswith('-time'): - info["author_date"] = int(parts[-1]) - elif role == firstpart: - info["author"] = parts[-1] - elif role == 'committer': - if firstpart.endswith('-mail'): - info["committer_email"] = parts[-1] - elif firstpart.endswith('-time'): - info["committer_date"] = int(parts[-1]) - elif role == firstpart: - info["committer"] = parts[-1] + if firstpart.endswith('-mail'): + info[f"{role}_email"] = parts[-1] + elif firstpart.endswith('-time'): + info[f"{role}_date"] = int(parts[-1]) + elif role == firstpart: + info[role] = parts[-1] # END distinguish mail,time,name else: # handle @@ -973,33 +980,33 @@ class Repo(object): info['summary'] = parts[-1] elif firstpart == '': if info: - sha = info['id'] + sha = info.id c = commits.get(sha) if c is None: c = Commit(self, hex_to_bin(sha), - author=Actor._from_string(info['author'] + ' ' + info['author_email']), - authored_date=info['author_date'], + author=Actor._from_string(info.author + ' ' + info.author_email), + authored_date=info.author_date, committer=Actor._from_string( - info['committer'] + ' ' + info['committer_email']), - committed_date=info['committer_date']) + info.committer + ' ' + info.committer_email), + committed_date=info.committer_date) commits[sha] = c blames[-1][0] = c # END if commit objects needs initial creation - if blames[-1][1] is not None: if not is_binary: if line_str and line_str[0] == '\t': line_str = line_str[1:] + blames[-1][1].append(line_str) else: - blames[-1][1].append(line_bytes) # NOTE: We are actually parsing lines out of binary data, which can lead to the # binary being split up along the newline separator. We will append this to the # blame we are currently looking at, even though it should be concatenated with # the last line we have seen. - info = {'id': sha} + blames[-1][1].append(line_bytes) # end handle line contents + info.id = sha # END if we collected commit info # END distinguish filename,summary,rest # END distinguish author|committer vs filename,summary,rest |