6 class GitCola(git
.Git
):
8 Subclass git.Git to provide custom behaviors.
10 GitPython throws exceptions by default.
11 We suppress exceptions in favor of return values.
14 git
.Git
.__init
__(self
)
15 self
.load_worktree(os
.getcwd())
17 def load_worktree(self
, path
):
27 curdir
= self
._git
_dir
31 if self
._is
_git
_dir
(os
.path
.join(curdir
, '.git')):
34 # Handle bare repositories
35 if (len(os
.path
.basename(curdir
)) > 4
36 and curdir
.endswith('.git')):
38 if 'GIT_WORK_TREE' in os
.environ
:
39 self
._worktree
= os
.getenv('GIT_WORK_TREE')
40 if not self
._worktree
or not os
.path
.isdir(self
._worktree
):
42 gitparent
= os
.path
.join(os
.path
.abspath(self
._git
_dir
), '..')
43 self
._worktree
= os
.path
.abspath(gitparent
)
44 self
.set_cwd(self
._worktree
)
48 return self
._git
_dir
and self
._is
_git
_dir
(self
._git
_dir
)
53 if 'GIT_DIR' in os
.environ
:
54 self
._git
_dir
= os
.getenv('GIT_DIR')
56 curpath
= os
.path
.abspath(self
._git
_dir
)
58 curpath
= os
.path
.abspath(os
.getcwd())
59 # Search for a .git directory
61 if self
._is
_git
_dir
(curpath
):
62 self
._git
_dir
= curpath
64 gitpath
= os
.path
.join(curpath
, '.git')
65 if self
._is
_git
_dir
(gitpath
):
66 self
._git
_dir
= gitpath
68 curpath
, dummy
= os
.path
.split(curpath
)
73 def _is_git_dir(self
, d
):
74 """ This is taken from the git setup.c:is_git_directory
77 and os
.path
.isdir(os
.path
.join(d
, 'objects'))
78 and os
.path
.isdir(os
.path
.join(d
, 'refs'))):
79 headref
= os
.path
.join(d
, 'HEAD')
80 return (os
.path
.isfile(headref
)
81 or (os
.path
.islink(headref
)
82 and os
.readlink(headref
).startswith('refs')))