From 1719c9c60daf445f7da8f5f1874f493ae300eb9f Mon Sep 17 00:00:00 2001 From: Peter Grayson Date: Fri, 23 Apr 2021 12:03:09 -0400 Subject: [PATCH] Add Worktree.default() method Worktree thus now takes responsibility for interpreting GIT_WORK_TREE instead of Repository. Signed-off-by: Peter Grayson --- stgit/lib/git/iw.py | 12 ++++++++++++ stgit/lib/git/repository.py | 9 +-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/stgit/lib/git/iw.py b/stgit/lib/git/iw.py index f8260b7..e0023e1 100644 --- a/stgit/lib/git/iw.py +++ b/stgit/lib/git/iw.py @@ -173,6 +173,18 @@ class Worktree: def __init__(self, directory): self.directory = directory + @classmethod + def default(cls): + path = environ_get('GIT_WORK_TREE', None) + if not path: + lines = Run('git', 'rev-parse', '--show-cdup').output_lines() + if lines: + assert len(lines) == 1 + path = lines[0] + else: + path = '.' + return cls(path) + @property def env(self): return {'GIT_WORK_TREE': '.'} diff --git a/stgit/lib/git/repository.py b/stgit/lib/git/repository.py index a2be77a..d19cd5b 100644 --- a/stgit/lib/git/repository.py +++ b/stgit/lib/git/repository.py @@ -5,7 +5,6 @@ import re import signal from stgit import utils -from stgit.compat import environ_get from stgit.exception import StgException from stgit.lib.objcache import ObjectCache from stgit.run import Run, RunException @@ -289,13 +288,7 @@ class Repository: def default_worktree(self): """A :class:`Worktree` representing the default work tree.""" if self._default_worktree is None: - path = environ_get('GIT_WORK_TREE', None) - if not path: - o = Run('git', 'rev-parse', '--show-cdup').output_lines() - o = o or ['.'] - assert len(o) == 1 - path = o[0] - self._default_worktree = Worktree(path) + self._default_worktree = Worktree.default() return self._default_worktree @property -- 2.11.4.GIT