diff options
author | Govind Salinas <blix@sophiasuchtig.com> | 2008-06-06 22:49:15 -0500 |
---|---|---|
committer | David Aguilar <davvid@gmail.com> | 2008-06-12 01:26:07 -0700 |
commit | c5083752d5d02d6c46bc2f18ba53a28d119af5b9 (patch) | |
tree | 6d55b650713f27e2b1e83ccb2b557cc5e0cca017 /lib/git/cmd.py | |
parent | 8a0eee3989abd3a5d6d47d0298bd056a954d379b (diff) | |
download | gitpython-c5083752d5d02d6c46bc2f18ba53a28d119af5b9.tar.gz |
Determine git_dir and git_work_tree in python.
Calling git to find the git_dir and work_tree is very costly.
This patch uses the same mechanisim to find the git_dir as native
git does without shelling out.
Signed-off-by: Govind Salinas <blix@sophiasuchtig.com>
Diffstat (limited to 'lib/git/cmd.py')
-rw-r--r-- | lib/git/cmd.py | 81 |
1 files changed, 50 insertions, 31 deletions
diff --git a/lib/git/cmd.py b/lib/git/cmd.py index cf0f066d..55e81e78 100644 --- a/lib/git/cmd.py +++ b/lib/git/cmd.py @@ -15,40 +15,59 @@ class Git(MethodMissingMixin): def __init__(self, git_dir=None): super(Git, self).__init__() if git_dir: - self.find_git_dir(git_dir) + self._location = os.path.abspath(git_dir) else: - self.find_git_dir(os.getcwd()) - - def find_git_dir(self, path): - """Find the best value for self.git_dir. - For bare repositories, this is the path to the bare repository. - For repositories with work trees, this is the work tree path. - - When barerepo.git is passed in, self.git_dir = barerepo.git - When worktree/.git is passed in, self.git_dir = worktree - When worktree is passed in, self.git_dir = worktree - """ - - path = os.path.abspath(path) - self.git_dir = path - - cdup = self.execute(["git", "rev-parse", "--show-cdup"]) - if cdup: - path = os.path.abspath(os.path.join(self.git_dir, cdup)) - else: - is_bare_repository =\ - self.rev_parse(is_bare_repository=True) == "true" - is_inside_git_dir =\ - self.rev_parse(is_inside_git_dir=True) == "true" - - if not is_bare_repository and is_inside_git_dir: - path = os.path.dirname(self.git_dir) - - self.git_dir = path + self._location = os.getcwd() + self.refresh() + + def refresh(self): + self._git_dir = None + self._is_in_repo = not not self.get_git_dir() + self._work_tree = None + + def _is_git_dir(self, d): + """ This is taken from the git setup.c:is_git_directory + function.""" + + if os.path.isdir(d) and \ + os.path.isdir(os.path.join(d, 'objects')) and \ + os.path.isdir(os.path.join(d, 'refs')): + headref = os.path.join(d, 'HEAD') + return os.path.isfile(headref) or \ + (os.path.islink(headref) and + os.readlink(headref).startswith('refs')) + return False + + def get_git_dir(self): + if not self._git_dir: + self._git_dir = os.getenv('GIT_DIR') + if self._git_dir and self._is_git_dir(self._git_dir): + return self._git_dir + curpath = self._location + while curpath: + if self._is_git_dir(curpath): + self._git_dir = curpath + break + gitpath = os.path.join(curpath, '.git') + if self._is_git_dir(gitpath): + self._git_dir = gitpath + break + curpath, dummy = os.path.split(curpath) + if not dummy: + break + return self._git_dir + + def get_work_tree(self): + if not self._work_tree: + self._work_tree = os.getenv('GIT_WORK_TREE') + if not self._work_tree or not os.path.isdir(self._work_tree): + self._work_tree = os.path.abspath( + os.path.join(self._git_dir, '..')) + return self._work_tree @property def get_dir(self): - return self.git_dir + return self._git_dir def execute(self, command, istream=None, @@ -96,7 +115,7 @@ class Git(MethodMissingMixin): # Start the process proc = subprocess.Popen(command, - cwd=self.git_dir, + cwd=self._git_dir, stdin=istream, stderr=stderr, stdout=subprocess.PIPE |