summaryrefslogtreecommitdiff
path: root/git/index/util.py
diff options
context:
space:
mode:
Diffstat (limited to 'git/index/util.py')
-rw-r--r--git/index/util.py32
1 files changed, 20 insertions, 12 deletions
diff --git a/git/index/util.py b/git/index/util.py
index 4f8af553..bfc7fadd 100644
--- a/git/index/util.py
+++ b/git/index/util.py
@@ -11,7 +11,7 @@ import os.path as osp
# typing ----------------------------------------------------------------------
-from typing import (Any, Callable, TYPE_CHECKING)
+from typing import Any, Callable, TYPE_CHECKING
from git.types import PathLike, _T
@@ -21,24 +21,26 @@ if TYPE_CHECKING:
# ---------------------------------------------------------------------------------
-__all__ = ('TemporaryFileSwap', 'post_clear_cache', 'default_index', 'git_working_dir')
+__all__ = ("TemporaryFileSwap", "post_clear_cache", "default_index", "git_working_dir")
-#{ Aliases
+# { Aliases
pack = struct.pack
unpack = struct.unpack
-#} END aliases
+# } END aliases
+
class TemporaryFileSwap(object):
"""Utility class moving a file to a temporary location within the same directory
and moving it back on to where on object deletion."""
+
__slots__ = ("file_path", "tmp_file_path")
def __init__(self, file_path: PathLike) -> None:
self.file_path = file_path
- self.tmp_file_path = str(self.file_path) + tempfile.mktemp('', '', '')
+ self.tmp_file_path = str(self.file_path) + tempfile.mktemp("", "", "")
# it may be that the source does not exist
try:
os.rename(self.file_path, self.tmp_file_path)
@@ -53,7 +55,8 @@ class TemporaryFileSwap(object):
# END temp file exists
-#{ Decorators
+# { Decorators
+
def post_clear_cache(func: Callable[..., _T]) -> Callable[..., _T]:
"""Decorator for functions that alter the index using the git command. This would
@@ -66,10 +69,11 @@ def post_clear_cache(func: Callable[..., _T]) -> Callable[..., _T]:
"""
@wraps(func)
- def post_clear_cache_if_not_raised(self: 'IndexFile', *args: Any, **kwargs: Any) -> _T:
+ def post_clear_cache_if_not_raised(self: "IndexFile", *args: Any, **kwargs: Any) -> _T:
rval = func(self, *args, **kwargs)
self._delete_entries_cache()
return rval
+
# END wrapper method
return post_clear_cache_if_not_raised
@@ -78,14 +82,16 @@ def post_clear_cache(func: Callable[..., _T]) -> Callable[..., _T]:
def default_index(func: Callable[..., _T]) -> Callable[..., _T]:
"""Decorator assuring the wrapped method may only run if we are the default
repository index. This is as we rely on git commands that operate
- on that index only. """
+ on that index only."""
@wraps(func)
- def check_default_index(self: 'IndexFile', *args: Any, **kwargs: Any) -> _T:
+ def check_default_index(self: "IndexFile", *args: Any, **kwargs: Any) -> _T:
if self._file_path != self._index_path():
raise AssertionError(
- "Cannot call %r on indices that do not represent the default git index" % func.__name__)
+ "Cannot call %r on indices that do not represent the default git index" % func.__name__
+ )
return func(self, *args, **kwargs)
+
# END wrapper method
return check_default_index
@@ -96,7 +102,7 @@ def git_working_dir(func: Callable[..., _T]) -> Callable[..., _T]:
repository in order to assure relative paths are handled correctly"""
@wraps(func)
- def set_git_working_dir(self: 'IndexFile', *args: Any, **kwargs: Any) -> _T:
+ def set_git_working_dir(self: "IndexFile", *args: Any, **kwargs: Any) -> _T:
cur_wd = os.getcwd()
os.chdir(str(self.repo.working_tree_dir))
try:
@@ -104,8 +110,10 @@ def git_working_dir(func: Callable[..., _T]) -> Callable[..., _T]:
finally:
os.chdir(cur_wd)
# END handle working dir
+
# END wrapper
return set_git_working_dir
-#} END decorators
+
+# } END decorators