diff options
author | Florian Apolloner <florian@apolloner.eu> | 2008-06-25 00:41:41 +0200 |
---|---|---|
committer | Florian Apolloner <florian@apolloner.eu> | 2008-06-25 00:43:07 +0200 |
commit | 990d1fe06e8c2db48a895aaa7e5e5eda8b330a5c (patch) | |
tree | a529af9a05481053a9286a346be7612fdee15df6 /lib/git | |
parent | 7fda0ec787de5159534ebc8b81824920d9613575 (diff) | |
download | gitpython-990d1fe06e8c2db48a895aaa7e5e5eda8b330a5c.tar.gz |
fixed http://groups.google.com/group/git-python/browse_thread/thread/b8f3580abf31f9db?hl=en# and passed Git a working_tree again (sort of).
Diffstat (limited to 'lib/git')
-rw-r--r-- | lib/git/cmd.py | 65 | ||||
-rw-r--r-- | lib/git/repo.py | 56 | ||||
-rw-r--r-- | lib/git/stats.py | 4 | ||||
-rw-r--r-- | lib/git/utils.py | 17 |
4 files changed, 52 insertions, 90 deletions
diff --git a/lib/git/cmd.py b/lib/git/cmd.py index 4cadeeb3..4fa22fd9 100644 --- a/lib/git/cmd.py +++ b/lib/git/cmd.py @@ -15,68 +15,13 @@ class Git(MethodMissingMixin): """ The Git class manages communication with the Git binary """ - def __init__(self, git_dir=None, bare_repo=False): + def __init__(self, git_dir): super(Git, self).__init__() - if git_dir: - self._location = os.path.abspath(git_dir) - else: - self._location = os.getcwd() - self._is_bare_repo = bare_repo - self.refresh() - - def refresh(self): - self._git_dir = None - self._is_in_repo = not not self.get_git_dir() - self._work_tree = None - self._cwd = self._git_dir - if self._git_dir and not self._is_bare_repo: - self._cwd = self.get_work_tree() - - 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 self._is_bare_repo: - return None - 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 + self.git_dir = git_dir @property def get_dir(self): - return self._git_dir + return self.git_dir def execute(self, command, istream=None, @@ -118,10 +63,10 @@ class Git(MethodMissingMixin): print ' '.join(command) # Allow the user to have the command executed in their working dir. - if with_keep_cwd: + if with_keep_cwd or self.git_dir is None: cwd = os.getcwd() else: - cwd=self._cwd + cwd=self.git_dir # Start the process proc = subprocess.Popen(command, diff --git a/lib/git/repo.py b/lib/git/repo.py index 7621c67b..f1a5a8ee 100644 --- a/lib/git/repo.py +++ b/lib/git/repo.py @@ -1,7 +1,7 @@ import os import re from errors import InvalidGitRepositoryError, NoSuchPathError -from utils import touch +from utils import touch, is_git_dir from cmd import Git from head import Head from blob import Blob @@ -27,25 +27,33 @@ class Repo(object): Returns ``GitPython.Repo`` """ - path = os.path.expanduser(path) - if not os.path.exists(path): - raise NoSuchPathError(path) - - self.git = Git(path) - self.path = self.git.get_git_dir() - if not self.path: - raise InvalidGitRepositoryError(path) - epath = self.git.get_work_tree() - - if os.path.exists(os.path.join(epath, '.git')): - self.bare = False - elif os.path.exists(epath) and epath.endswith('.git'): - self.bare = True - elif os.path.exists(epath): - raise InvalidGitRepositoryError(epath) - else: + + epath = os.path.abspath(os.path.expanduser(path or os.getcwd())) + + if not os.path.exists(epath): raise NoSuchPathError(epath) + self.path = None + curpath = epath + while curpath: + if is_git_dir(curpath): + self.bare = True + self.path, self.wd = curpath + break + gitpath = os.path.join(curpath, '.git') + if is_git_dir(gitpath): + self.bare = False + self.path = gitpath + self.wd = curpath + break + curpath, dummy = os.path.split(curpath) + if not dummy: + break + + if self.path is None: + raise InvalidGitRepositoryError(epath) + + self.git = Git(self.wd) @property def description(self): @@ -281,7 +289,7 @@ class Repo(object): if mkdir and not os.path.exists(path): os.makedirs(path, 0755) - git = Git(path, bare_repo=True) + git = Git(path) output = git.init(**kwargs) return Repo(path) create = init_bare @@ -370,10 +378,7 @@ class Repo(object): Returns None """ - if self.bare: - touch(os.path.join(self.path, DAEMON_EXPORT_FILE)) - else: - touch(os.path.join(self.path, '.git', DAEMON_EXPORT_FILE)) + touch(os.path.join(self.path, DAEMON_EXPORT_FILE)) def disable_daemon_serve(self): """ @@ -383,10 +388,7 @@ class Repo(object): Returns None """ - if self.bare: - return os.remove(os.path.join(self.path, DAEMON_EXPORT_FILE)) - else: - return os.remove(os.path.join(self.path, '.git', DAEMON_EXPORT_FILE)) + return os.remove(os.path.join(self.path, DAEMON_EXPORT_FILE)) def _get_alternates(self): """ diff --git a/lib/git/stats.py b/lib/git/stats.py index 95dc875e..0fb6828c 100644 --- a/lib/git/stats.py +++ b/lib/git/stats.py @@ -9,8 +9,8 @@ class Stats(object): hsh = {'total': {'insertions': 0, 'deletions': 0, 'lines': 0, 'files': 0}, 'files': {}} for line in text.splitlines(): (insertions, deletions, filename) = line.split("\t") - hsh['total']['insertions'] += int(insertions) - hsh['total']['deletions'] += int(deletions) + hsh['total']['insertions'] += insertions != '-' and int(insertions) or 0 + hsh['total']['deletions'] += deleteions != '-' and int(deletions) or 0 hsh['total']['lines'] = (hsh['total']['deletions'] + hsh['total']['insertions']) hsh['total']['files'] += 1 hsh['files'][filename.strip()] = {'insertions': int(insertions), 'deletions': int(deletions)} diff --git a/lib/git/utils.py b/lib/git/utils.py index c2140ba0..656e783c 100644 --- a/lib/git/utils.py +++ b/lib/git/utils.py @@ -1,5 +1,20 @@ +import os + def dashify(string): return string.replace('_', '-') def touch(filename): - open(filename, "a").close() + os.utime(filename) + +def is_git_dir(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 |