summaryrefslogtreecommitdiff
path: root/lib/git_python/git.py
diff options
context:
space:
mode:
authorDavid Aguilar <davvid@gmail.com>2008-05-29 17:53:30 -0700
committerDavid Aguilar <davvid@gmail.com>2008-05-29 17:53:30 -0700
commitcbbb6b349e8d0557e7e55e2d05819d6bdaed3769 (patch)
tree2ebb7aac2e26486ae352418e011c7240ffdc4c5c /lib/git_python/git.py
parentebee400cfa4a532018ee904c22c7e3e6619ca4de (diff)
downloadgitpython-cbbb6b349e8d0557e7e55e2d05819d6bdaed3769.tar.gz
git.py: make git_dir an optional parameter
git_dir is now optional in the constructor for git.Git. Git now falls back to os.getcwd() when git_dir is not specified. Furthermore, extra checks were added so that the git_dir variable always points to the root of the worktree for non-bare repositories. Signed-off-by: David Aguilar <davvid@gmail.com>
Diffstat (limited to 'lib/git_python/git.py')
-rw-r--r--lib/git_python/git.py34
1 files changed, 32 insertions, 2 deletions
diff --git a/lib/git_python/git.py b/lib/git_python/git.py
index 436df856..759c9127 100644
--- a/lib/git_python/git.py
+++ b/lib/git_python/git.py
@@ -12,9 +12,39 @@ class Git(MethodMissingMixin):
"""
The Git class manages communication with the Git binary
"""
- def __init__(self, git_dir):
+ def __init__(self, git_dir=None):
super(Git, self).__init__()
- self.git_dir = git_dir
+ if git_dir:
+ self.find_git_dir( 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
@property
def get_dir(self):