diff options
Diffstat (limited to 'lib/git')
-rw-r--r-- | lib/git/blob.py | 24 | ||||
-rw-r--r-- | lib/git/commit.py | 33 | ||||
-rw-r--r-- | lib/git/repo.py | 68 | ||||
-rw-r--r-- | lib/git/tree.py | 79 |
4 files changed, 108 insertions, 96 deletions
diff --git a/lib/git/blob.py b/lib/git/blob.py index 0b4b19c2..80d237d7 100644 --- a/lib/git/blob.py +++ b/lib/git/blob.py @@ -14,29 +14,33 @@ from commit import Commit class Blob(object): DEFAULT_MIME_TYPE = "text/plain" - def __init__(self, repo, **kwargs): + def __init__(self, repo, id, mode=None, name=None): """ Create an unbaked Blob containing just the specified attributes ``repo`` is the Repo - ``atts`` - is a dict of instance variable data + ``id`` + is the git object id + + ``mode`` + is the file mode + + ``name`` + is the file name Returns GitPython.Blob """ - self.id = None - self.mode = None - self.name = None + self.repo = repo + self.id = id + self.mode = mode + self.name = name + self._size = None self.data_stored = None - self.repo = repo - for k, v in kwargs.items(): - setattr(self, k, v) - @property def size(self): """ diff --git a/lib/git/commit.py b/lib/git/commit.py index 18e36c00..e92293e4 100644 --- a/lib/git/commit.py +++ b/lib/git/commit.py @@ -9,12 +9,13 @@ import time from actor import Actor from lazy import LazyMixin -import tree +from tree import Tree import diff import stats class Commit(LazyMixin): - def __init__(self, repo, **kwargs): + def __init__(self, repo, id, tree=None, author=None, authored_date=None, + committer=None, committed_date=None, message=None, parents=None): """ Instantiate a new Commit @@ -42,29 +43,29 @@ class Commit(LazyMixin): ``message`` is the first line of the commit message + ``parents`` + is the list of the parents of the commit + Returns GitPython.Commit """ LazyMixin.__init__(self) self.repo = repo - self.id = None - self.tree = None - self.author = None - self.authored_date = None - self.committer = None - self.committed_date = None - self.message = None + self.id = id self.parents = None - - for k, v in kwargs.items(): - setattr(self, k, v) + self.tree = None + self.author = author + self.authored_date = authored_date + self.committer = committer + self.committed_date = committed_date + self.message = message if self.id: - if 'parents' in kwargs: - self.parents = map(lambda p: Commit(repo, id=p), kwargs['parents']) - if 'tree' in kwargs: - self.tree = tree.Tree(repo, id=kwargs['tree']) + if parents is not None: + self.parents = [Commit(repo, p) for p in parents] + if tree is not None: + self.tree = Tree(repo, id=tree) def __bake__(self): temp = Commit.find_all(self.repo, self.id, max_count=1)[0] diff --git a/lib/git/repo.py b/lib/git/repo.py index 55f73f66..fdfb7928 100644 --- a/lib/git/repo.py +++ b/lib/git/repo.py @@ -62,20 +62,19 @@ class Repo(object): self.git = Git(self.wd) - @property - def description(self): - """ - The project's description. Taken verbatim from GIT_REPO/description + # Description property + def _get_description(self): + filename = os.path.join(self.path, 'description') + return file(filename).read().rstrip() - Returns - str - """ - try: - f = open(os.path.join(self.path, 'description')) - result = f.read() - return result.rstrip() - finally: - f.close() + def _set_description(self, descr): + filename = os.path.join(self.path, 'description') + file(filename, 'w').write(descr+'\n') + + description = property(_get_description, _set_description, + doc="the project's description") + del _get_description + del _set_description @property def heads(self): @@ -199,24 +198,22 @@ class Repo(object): diff_refs = list(set(other_repo_refs) - set(repo_refs)) return map(lambda ref: Commit.find_all(other_repo, ref, max_count=1)[0], diff_refs) - def tree(self, treeish = 'master', paths = []): + def tree(self, treeish = 'master'): """ The Tree object for the given treeish reference ``treeish`` is the reference (default 'master') - ``paths`` - is an optional Array of directory paths to restrict the tree (default []) Examples:: - repo.tree('master', ['lib/']) + repo.tree('master') Returns ``GitPython.Tree`` """ - return Tree.construct(self, treeish, paths) + return Tree(self, id=treeish) def blob(self, id): """ @@ -377,25 +374,22 @@ class Repo(object): kwargs['prefix'] = prefix self.git.archive(treeish, "| gzip", **kwargs) - def enable_daemon_serve(self): - """ - Enable git-daemon serving of this repository by writing the - git-daemon-export-ok file to its git directory - - Returns - None - """ - touch(os.path.join(self.path, DAEMON_EXPORT_FILE)) - - def disable_daemon_serve(self): - """ - Disable git-daemon serving of this repository by ensuring there is no - git-daemon-export-ok file in its git directory - - Returns - None - """ - return os.remove(os.path.join(self.path, DAEMON_EXPORT_FILE)) + def _get_daemon_export(self): + filename = os.path.join(self.path, self.DAEMON_EXPORT_FILE) + return os.path.exists(filename) + + def _set_daemon_export(self, value): + filename = os.path.join(self.path, self.DAEMON_EXPORT_FILE) + fileexists = os.path.exists(filename) + if value and not fileexists: + touch(filename) + elif not value and fileexists: + os.unlink(filename) + + daemon_export = property(_get_daemon_export, _set_daemon_export, + doc="git-daemon export of this repository") + del _get_daemon_export + del _set_daemon_export def _get_alternates(self): """ diff --git a/lib/git/tree.py b/lib/git/tree.py index f1aa0b3b..dbd78ac4 100644 --- a/lib/git/tree.py +++ b/lib/git/tree.py @@ -9,41 +9,29 @@ from lazy import LazyMixin import blob class Tree(LazyMixin): - def __init__(self, repo, **kwargs): + def __init__(self, repo, id, mode=None, name=None): LazyMixin.__init__(self) self.repo = repo - self.id = None - self.mode = None - self.name = None - self.contents = None - - for k, v in kwargs.items(): - setattr(self, k, v) - - def __bake__(self): - temp = Tree.construct(self.repo, self.id) - self.contents = temp.contents - - @classmethod - def construct(cls, repo, treeish, paths = []): - output = repo.git.ls_tree(treeish, *paths) - return Tree(repo, id=treeish).construct_initialize(repo, treeish, output) - - def construct_initialize(self, repo, id, text): - self.repo = repo self.id = id - self.contents = [] - self.__baked__ = False - - for line in text.splitlines(): - self.contents.append(self.content_from_string(self.repo, line)) - - self.contents = [c for c in self.contents if c is not None] - - self.__bake_it__() - return self + self.mode = mode + self.name = name + self._contents = None - def content_from_string(self, repo, text): + def __bake__(self): + # Ensure the treeish references directly a tree + treeish = self.id + if not treeish.endswith(':'): + treeish = treeish + ':' + + # Read the tree contents. + self._contents = {} + for line in self.repo.git.ls_tree(self.id).splitlines(): + obj = self.content_from_string(self.repo, line) + if obj is not None: + self._contents[obj.name] = obj + + @staticmethod + def content_from_string(repo, text): """ Parse a content item and create the appropriate object @@ -84,8 +72,7 @@ class Tree(LazyMixin): Returns ``GitPython.Blob`` or ``GitPython.Tree`` or ``None`` if not found """ - contents = [c for c in self.contents if c.name == file] - return contents and contents[0] or None + return self.get(file) @property def basename(self): @@ -93,3 +80,29 @@ class Tree(LazyMixin): def __repr__(self): return '<GitPython.Tree "%s">' % self.id + + # Implement the basics of the dict protocol: + # directories/trees can be seen as object dicts. + def __getitem__(self, key): + return self._contents[key] + + def __iter__(self): + return iter(self._contents) + + def __len__(self, keys): + return len(self._contents) + + def __contains__(self, key): + return key in self._contents + + def get(self, key): + return self._contents.get(key) + + def items(self): + return self._contents.items() + + def keys(self): + return self._contents.keys() + + def values(self): + return self._contents.values() |