| |   |
| 15 | 15 | """ |
| 16 | 16 | The Git class manages communication with the Git binary |
| 17 | 17 | """ |
| 18 | | def __init__(self, git_dir=None, bare_repo=False): |
| 18 | def __init__(self, git_dir): |
| 19 | 19 | super(Git, self).__init__() |
| 20 | | if git_dir: |
| 21 | | self._location = os.path.abspath(git_dir) |
| 22 | | else: |
| 23 | | self._location = os.getcwd() |
| 24 | | self._is_bare_repo = bare_repo |
| 25 | | self.refresh() |
| 26 | | |
| 27 | | def refresh(self): |
| 28 | | self._git_dir = None |
| 29 | | self._is_in_repo = not not self.get_git_dir() |
| 30 | | self._work_tree = None |
| 31 | | self._cwd = self._git_dir |
| 32 | | if self._git_dir and not self._is_bare_repo: |
| 33 | | self._cwd = self.get_work_tree() |
| 34 | | |
| 35 | | def _is_git_dir(self, d): |
| 36 | | """ This is taken from the git setup.c:is_git_directory |
| 37 | | function.""" |
| 38 | | |
| 39 | | if os.path.isdir(d) and \ |
| 40 | | os.path.isdir(os.path.join(d, 'objects')) and \ |
| 41 | | os.path.isdir(os.path.join(d, 'refs')): |
| 42 | | headref = os.path.join(d, 'HEAD') |
| 43 | | return os.path.isfile(headref) or \ |
| 44 | | (os.path.islink(headref) and |
| 45 | | os.readlink(headref).startswith('refs')) |
| 46 | | return False |
| 47 | | |
| 48 | | def get_git_dir(self): |
| 49 | | if not self._git_dir: |
| 50 | | self._git_dir = os.getenv('GIT_DIR') |
| 51 | | if self._git_dir and self._is_git_dir(self._git_dir): |
| 52 | | return self._git_dir |
| 53 | | curpath = self._location |
| 54 | | while curpath: |
| 55 | | if self._is_git_dir(curpath): |
| 56 | | self._git_dir = curpath |
| 57 | | break |
| 58 | | gitpath = os.path.join(curpath, '.git') |
| 59 | | if self._is_git_dir(gitpath): |
| 60 | | self._git_dir = gitpath |
| 61 | | break |
| 62 | | curpath, dummy = os.path.split(curpath) |
| 63 | | if not dummy: |
| 64 | | break |
| 65 | | return self._git_dir |
| 66 | | |
| 67 | | def get_work_tree(self): |
| 68 | | if self._is_bare_repo: |
| 69 | | return None |
| 70 | | if not self._work_tree: |
| 71 | | self._work_tree = os.getenv('GIT_WORK_TREE') |
| 72 | | if not self._work_tree or not os.path.isdir(self._work_tree): |
| 73 | | self._work_tree = os.path.abspath( |
| 74 | | os.path.join(self._git_dir, '..')) |
| 75 | | return self._work_tree |
| 20 | self.git_dir = git_dir |
| 76 | 21 | |
| 77 | 22 | @property |
| 78 | 23 | def get_dir(self): |
| 79 | | return self._git_dir |
| 24 | return self.git_dir |
| 80 | 25 | |
| 81 | 26 | def execute(self, command, |
| 82 | 27 | istream=None, |
| … | … | |
| 63 | 63 | print ' '.join(command) |
| 64 | 64 | |
| 65 | 65 | # Allow the user to have the command executed in their working dir. |
| 66 | | if with_keep_cwd: |
| 66 | if with_keep_cwd or self.git_dir is None: |
| 67 | 67 | cwd = os.getcwd() |
| 68 | 68 | else: |
| 69 | | cwd=self._cwd |
| 69 | cwd=self.git_dir |
| 70 | 70 | |
| 71 | 71 | # Start the process |
| 72 | 72 | proc = subprocess.Popen(command, |
| toggle raw diff |
--- 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, |
| |   |
| 1 | 1 | import os |
| 2 | 2 | import re |
| 3 | 3 | from errors import InvalidGitRepositoryError, NoSuchPathError |
| 4 | | from utils import touch |
| 4 | from utils import touch, is_git_dir |
| 5 | 5 | from cmd import Git |
| 6 | 6 | from head import Head |
| 7 | 7 | from blob import Blob |
| … | … | |
| 27 | 27 | Returns |
| 28 | 28 | ``GitPython.Repo`` |
| 29 | 29 | """ |
| 30 | | path = os.path.expanduser(path) |
| 31 | | if not os.path.exists(path): |
| 32 | | raise NoSuchPathError(path) |
| 33 | | |
| 34 | | self.git = Git(path) |
| 35 | | self.path = self.git.get_git_dir() |
| 36 | | if not self.path: |
| 37 | | raise InvalidGitRepositoryError(path) |
| 38 | | epath = self.git.get_work_tree() |
| 39 | | |
| 40 | | if os.path.exists(os.path.join(epath, '.git')): |
| 41 | | self.bare = False |
| 42 | | elif os.path.exists(epath) and epath.endswith('.git'): |
| 43 | | self.bare = True |
| 44 | | elif os.path.exists(epath): |
| 45 | | raise InvalidGitRepositoryError(epath) |
| 46 | | else: |
| 30 | |
| 31 | epath = os.path.abspath(os.path.expanduser(path or os.getcwd())) |
| 32 | |
| 33 | if not os.path.exists(epath): |
| 47 | 34 | raise NoSuchPathError(epath) |
| 48 | 35 | |
| 36 | self.path = None |
| 37 | curpath = epath |
| 38 | while curpath: |
| 39 | if is_git_dir(curpath): |
| 40 | self.bare = True |
| 41 | self.path, self.wd = curpath |
| 42 | break |
| 43 | gitpath = os.path.join(curpath, '.git') |
| 44 | if is_git_dir(gitpath): |
| 45 | self.bare = False |
| 46 | self.path = gitpath |
| 47 | self.wd = curpath |
| 48 | break |
| 49 | curpath, dummy = os.path.split(curpath) |
| 50 | if not dummy: |
| 51 | break |
| 52 | |
| 53 | if self.path is None: |
| 54 | raise InvalidGitRepositoryError(epath) |
| 55 | |
| 56 | self.git = Git(self.wd) |
| 49 | 57 | |
| 50 | 58 | @property |
| 51 | 59 | def description(self): |
| … | … | |
| 289 | 289 | if mkdir and not os.path.exists(path): |
| 290 | 290 | os.makedirs(path, 0755) |
| 291 | 291 | |
| 292 | | git = Git(path, bare_repo=True) |
| 292 | git = Git(path) |
| 293 | 293 | output = git.init(**kwargs) |
| 294 | 294 | return Repo(path) |
| 295 | 295 | create = init_bare |
| … | … | |
| 378 | 378 | Returns |
| 379 | 379 | None |
| 380 | 380 | """ |
| 381 | | if self.bare: |
| 382 | | touch(os.path.join(self.path, DAEMON_EXPORT_FILE)) |
| 383 | | else: |
| 384 | | touch(os.path.join(self.path, '.git', DAEMON_EXPORT_FILE)) |
| 381 | touch(os.path.join(self.path, DAEMON_EXPORT_FILE)) |
| 385 | 382 | |
| 386 | 383 | def disable_daemon_serve(self): |
| 387 | 384 | """ |
| … | … | |
| 388 | 388 | Returns |
| 389 | 389 | None |
| 390 | 390 | """ |
| 391 | | if self.bare: |
| 392 | | return os.remove(os.path.join(self.path, DAEMON_EXPORT_FILE)) |
| 393 | | else: |
| 394 | | return os.remove(os.path.join(self.path, '.git', DAEMON_EXPORT_FILE)) |
| 391 | return os.remove(os.path.join(self.path, DAEMON_EXPORT_FILE)) |
| 395 | 392 | |
| 396 | 393 | def _get_alternates(self): |
| 397 | 394 | """ |
| toggle raw diff |
--- 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):
""" |
| |   |
| 9 | 9 | hsh = {'total': {'insertions': 0, 'deletions': 0, 'lines': 0, 'files': 0}, 'files': {}} |
| 10 | 10 | for line in text.splitlines(): |
| 11 | 11 | (insertions, deletions, filename) = line.split("\t") |
| 12 | | hsh['total']['insertions'] += int(insertions) |
| 13 | | hsh['total']['deletions'] += int(deletions) |
| 12 | hsh['total']['insertions'] += insertions != '-' and int(insertions) or 0 |
| 13 | hsh['total']['deletions'] += deleteions != '-' and int(deletions) or 0 |
| 14 | 14 | hsh['total']['lines'] = (hsh['total']['deletions'] + hsh['total']['insertions']) |
| 15 | 15 | hsh['total']['files'] += 1 |
| 16 | 16 | hsh['files'][filename.strip()] = {'insertions': int(insertions), 'deletions': int(deletions)} |
| toggle raw diff |
--- 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)} |
| |   |
| 1 | import os |
| 2 | |
| 1 | 3 | def dashify(string): |
| 2 | 4 | return string.replace('_', '-') |
| 3 | 5 | |
| 4 | 6 | def touch(filename): |
| 5 | | open(filename, "a").close() |
| 7 | os.utime(filename) |
| 8 | |
| 9 | def is_git_dir(d): |
| 10 | """ This is taken from the git setup.c:is_git_directory |
| 11 | function.""" |
| 12 | |
| 13 | if os.path.isdir(d) and \ |
| 14 | os.path.isdir(os.path.join(d, 'objects')) and \ |
| 15 | os.path.isdir(os.path.join(d, 'refs')): |
| 16 | headref = os.path.join(d, 'HEAD') |
| 17 | return os.path.isfile(headref) or \ |
| 18 | (os.path.islink(headref) and |
| 19 | os.readlink(headref).startswith('refs')) |
| 20 | return False |
| toggle raw diff |
--- 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 |