6 # Provides access to a global GitCola instance
9 """Return the GitCola singleton"""
16 class GitCola(git
.Git
):
18 Subclass git.Git to provide search-for-git-dir
22 git
.Git
.__init
__(self
)
23 self
.load_worktree(os
.getcwd())
25 def load_worktree(self
, path
):
35 curdir
= self
._git
_dir
39 if self
._is
_git
_dir
(os
.path
.join(curdir
, '.git')):
42 # Handle bare repositories
43 if (len(os
.path
.basename(curdir
)) > 4
44 and curdir
.endswith('.git')):
46 if 'GIT_WORK_TREE' in os
.environ
:
47 self
._worktree
= os
.getenv('GIT_WORK_TREE')
48 if not self
._worktree
or not os
.path
.isdir(self
._worktree
):
50 gitparent
= os
.path
.join(os
.path
.abspath(self
._git
_dir
), '..')
51 self
._worktree
= os
.path
.abspath(gitparent
)
52 self
.set_cwd(self
._worktree
)
56 return self
._git
_dir
and self
._is
_git
_dir
(self
._git
_dir
)
58 def git_path(self
, *paths
):
59 return os
.path
.join(self
.git_dir(), *paths
)
64 if 'GIT_DIR' in os
.environ
:
65 self
._git
_dir
= os
.getenv('GIT_DIR')
67 curpath
= os
.path
.abspath(self
._git
_dir
)
69 curpath
= os
.path
.abspath(os
.getcwd())
70 # Search for a .git directory
72 if self
._is
_git
_dir
(curpath
):
73 self
._git
_dir
= curpath
75 gitpath
= os
.path
.join(curpath
, '.git')
76 if self
._is
_git
_dir
(gitpath
):
77 self
._git
_dir
= gitpath
79 curpath
, dummy
= os
.path
.split(curpath
)
84 def _is_git_dir(self
, d
):
85 """From git's setup.c:is_git_directory()."""
87 and os
.path
.isdir(os
.path
.join(d
, 'objects'))
88 and os
.path
.isdir(os
.path
.join(d
, 'refs'))):
89 headref
= os
.path
.join(d
, 'HEAD')
90 return (os
.path
.isfile(headref
)
91 or (os
.path
.islink(headref
)
92 and os
.readlink(headref
).startswith('refs')))