6 # Provides access to a global GitCola instance
9 """Return the GitCola singleton"""
17 class GitCola(git
.Git
):
19 Subclass git.Git to provide custom behaviors.
21 GitPython throws exceptions by default.
22 We suppress exceptions in favor of return values.
26 git
.Git
.__init
__(self
)
27 self
.load_worktree(os
.getcwd())
29 def load_worktree(self
, path
):
39 curdir
= self
._git
_dir
43 if self
._is
_git
_dir
(os
.path
.join(curdir
, '.git')):
46 # Handle bare repositories
47 if (len(os
.path
.basename(curdir
)) > 4
48 and curdir
.endswith('.git')):
50 if 'GIT_WORK_TREE' in os
.environ
:
51 self
._worktree
= os
.getenv('GIT_WORK_TREE')
52 if not self
._worktree
or not os
.path
.isdir(self
._worktree
):
54 gitparent
= os
.path
.join(os
.path
.abspath(self
._git
_dir
), '..')
55 self
._worktree
= os
.path
.abspath(gitparent
)
56 self
.set_cwd(self
._worktree
)
60 return self
._git
_dir
and self
._is
_git
_dir
(self
._git
_dir
)
65 if 'GIT_DIR' in os
.environ
:
66 self
._git
_dir
= os
.getenv('GIT_DIR')
68 curpath
= os
.path
.abspath(self
._git
_dir
)
70 curpath
= os
.path
.abspath(os
.getcwd())
71 # Search for a .git directory
73 if self
._is
_git
_dir
(curpath
):
74 self
._git
_dir
= curpath
76 gitpath
= os
.path
.join(curpath
, '.git')
77 if self
._is
_git
_dir
(gitpath
):
78 self
._git
_dir
= gitpath
80 curpath
, dummy
= os
.path
.split(curpath
)
85 def _is_git_dir(self
, d
):
86 """From git's setup.c:is_git_directory()."""
88 and os
.path
.isdir(os
.path
.join(d
, 'objects'))
89 and os
.path
.isdir(os
.path
.join(d
, 'refs'))):
90 headref
= os
.path
.join(d
, 'HEAD')
91 return (os
.path
.isfile(headref
)
92 or (os
.path
.islink(headref
)
93 and os
.readlink(headref
).startswith('refs')))