diff --git a/pygit2/repository.py b/pygit2/repository.py index b5e59e87..660ac4fa 100644 --- a/pygit2/repository.py +++ b/pygit2/repository.py @@ -81,7 +81,7 @@ from .remotes import RemoteCollection from .submodules import SubmoduleCollection from .transaction import ReferenceTransaction -from .utils import StrArray, to_bytes +from .utils import StrArray, maybe_string, to_bytes if TYPE_CHECKING: from pygit2._libgit2.ffi import ( @@ -1278,7 +1278,7 @@ def stash( Example:: >>> repo = pygit2.Repository('.') - >>> repo.stash(repo.default_signature(), 'WIP: stashing') + >>> repo.stash(repo.default_signature, 'WIP: stashing') """ opts = ffi.new('git_stash_save_options *') @@ -1347,7 +1347,7 @@ def stash_apply( Example:: >>> repo = pygit2.Repository('.') - >>> repo.stash(repo.default_signature(), 'WIP: stashing') + >>> repo.stash(repo.default_signature, 'WIP: stashing') >>> repo.stash_apply(strategy=CheckoutStrategy.ALLOW_CONFLICTS) """ with git_stash_apply_options( @@ -1578,16 +1578,16 @@ def get_attr( # Identity for reference operations # @property - def ident(self): + def ident(self) -> tuple[Optional[str], Optional[str]]: cname = ffi.new('char **') cemail = ffi.new('char **') err = C.git_repository_ident(cname, cemail, self._repo) check_error(err) - return (ffi.string(cname).decode('utf-8'), ffi.string(cemail).decode('utf-8')) + return (maybe_string(cname[0]), maybe_string(cemail[0])) - def set_ident(self, name: str, email: str) -> None: + def set_ident(self, name: Optional[str], email: Optional[str]) -> None: """Set the identity to be used for reference operations. Updates to some references also append data to their diff --git a/test/test_repository.py b/test/test_repository.py index 68913ff5..446aaae7 100644 --- a/test/test_repository.py +++ b/test/test_repository.py @@ -641,6 +641,19 @@ def test_default_signature(testrepo: Repository) -> None: assert 'rjh@example.com' == sig.email +def test_ident_get_set(testrepo: Repository) -> None: + # By default, reflog identity should be unset. + assert testrepo.ident == (None, None) + + cname = 'C O Mitter' + cemail = 'committer@example.com' + testrepo.set_ident(cname, cemail) + assert testrepo.ident == (cname, cemail) + + testrepo.set_ident(None, None) + assert testrepo.ident == (None, None) + + def test_new_repo(tmp_path: Path) -> None: repo = init_repository(tmp_path, False)