Skip to content

Commit 6285841

Browse files
committed
Replace readonly_constant_property decorator
The custom readonly_constant_property decorator was only used within the Directory class hierarchy. And it no longer seems to warrant its complexity. The git_dir readonly_constant_property becomes a regular _get_git_dir() method. It is only ever called once, at Directory setup time, to ensure that the command is being run in the context of a git repository. The __topdir_path readonly_constant_property is removed. The cd_to_topdir() method now calls `git rev-parse --show-cdup` on its own and changes directory, if necessary. This potentially saves a superfluous os.chdir() call. The is_inside_git_dir readonly_constant_property was unused and is removed. Signed-off-by: Peter Grayson <[email protected]>
1 parent 0baa8ce commit 6285841

File tree

1 file changed

+18
-46
lines changed

1 file changed

+18
-46
lines changed

stgit/commands/common.py

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -590,18 +590,6 @@ def parse_patch(patch_data, contains_diff):
590590
return (descr, authname, authemail, authdate, diff)
591591

592592

593-
def readonly_constant_property(f):
594-
"""Decorator that converts a function that computes a value to an
595-
attribute that returns the value. The value is computed only once,
596-
the first time it is accessed."""
597-
def new_f(self):
598-
n = '__' + f.__name__
599-
if not hasattr(self, n):
600-
setattr(self, n, f(self))
601-
return getattr(self, n)
602-
return property(new_f)
603-
604-
605593
def run_commit_msg_hook(repo, cd, editor_is_used=True):
606594
"""Run the commit-msg hook (if any) on a commit.
607595
@@ -662,42 +650,25 @@ def __init__(self, needs_current_series=True, log=True):
662650
self.needs_current_series = needs_current_series
663651
self.log = log
664652

665-
@readonly_constant_property
666-
def git_dir(self):
667-
try:
668-
return Run('git', 'rev-parse', '--git-dir'
669-
).discard_stderr().output_one_line()
670-
except RunException:
671-
raise DirectoryException('No git repository found')
672-
673-
@readonly_constant_property
674-
def __topdir_path(self):
653+
def _get_git_dir(self):
675654
try:
676-
lines = Run('git', 'rev-parse', '--show-cdup'
677-
).discard_stderr().output_lines()
678-
if len(lines) == 0:
679-
return '.'
680-
elif len(lines) == 1:
681-
return lines[0]
682-
else:
683-
raise RunException('Too much output')
655+
return Run(
656+
'git', 'rev-parse', '--git-dir'
657+
).discard_stderr().output_one_line()
684658
except RunException:
685-
raise DirectoryException('No git repository found')
659+
return None
686660

687-
@readonly_constant_property
688-
def is_inside_git_dir(self):
689-
return {'true': True, 'false': False}[
690-
Run('git', 'rev-parse', '--is-inside-git-dir').output_one_line()
691-
]
692-
693-
@readonly_constant_property
694-
def is_inside_worktree(self):
695-
return {'true': True, 'false': False}[
696-
Run('git', 'rev-parse', '--is-inside-work-tree').output_one_line()
697-
]
661+
def _is_inside_worktree(self):
662+
return Run(
663+
'git', 'rev-parse', '--is-inside-work-tree'
664+
).output_one_line() == 'true'
698665

699666
def cd_to_topdir(self):
700-
os.chdir(self.__topdir_path)
667+
worktree_top = Run(
668+
'git', 'rev-parse', '--show-cdup'
669+
).discard_stderr().raw_output().rstrip()
670+
if worktree_top:
671+
os.chdir(worktree_top)
701672

702673
def write_log(self, msg):
703674
if self.log:
@@ -716,14 +687,15 @@ def setup(self):
716687

717688
class DirectoryHasRepository(_Directory):
718689
def setup(self):
719-
self.git_dir # might throw an exception
690+
if not self._get_git_dir():
691+
raise DirectoryException('No git repository found')
720692
compat_log_external_mods()
721693

722694

723695
class DirectoryInWorktree(DirectoryHasRepository):
724696
def setup(self):
725697
DirectoryHasRepository.setup(self)
726-
if not self.is_inside_worktree:
698+
if not self._is_inside_worktree():
727699
raise DirectoryException('Not inside a git worktree')
728700

729701

@@ -749,7 +721,7 @@ def setup(self):
749721
class DirectoryInWorktreeLib(DirectoryHasRepositoryLib):
750722
def setup(self):
751723
DirectoryHasRepositoryLib.setup(self)
752-
if not self.is_inside_worktree:
724+
if not self._is_inside_worktree():
753725
raise DirectoryException('Not inside a git worktree')
754726

755727

0 commit comments

Comments
 (0)