| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -124,8 +124,8 @@ This tree contains three ``Blob`` objects and one ``Tree`` object. The trees | |||
| 124 | 124 | are subdirectories and the blobs are files. Trees below the root have | |
| 125 | 125 | additional attributes. | |
| 126 | 126 | ||
| 127 | - >>> contents = tree.contents[-2] | ||
| 128 | - <GitPython.Tree "e5445b9db4a9f08d5b4de4e29e61dffda2f386ba"> | ||
| 127 | + >>> contents = tree["lib"] | ||
| 128 | + <GitPython.Tree "c1c7214dde86f76bc3e18806ac1f47c38b2b7a3"> | ||
| 129 | 129 | ||
| 130 | 130 | >>> contents.name | |
| 131 | 131 | 'test' | |
@@ -134,7 +134,8 @@ additional attributes. | |||
| 134 | 134 | '040000' | |
| 135 | 135 | ||
| 136 | 136 | There is a convenience method that allows you to get a named sub-object | |
| 137 | - from a tree. | ||
| 137 | + from a tree with a syntax similar to how paths are written in an unix | ||
| 138 | + system. | ||
| 138 | 139 | ||
| 139 | 140 | >>> tree/"lib" | |
| 140 | 141 | <GitPython.Tree "c1c7214dde86f76bc3e18806ac1f47c38b2b7a30"> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,29 +14,33 @@ | |||
| 14 | 14 | class Blob(object): | |
| 15 | 15 | DEFAULT_MIME_TYPE = "text/plain" | |
| 16 | 16 | ||
| 17 | - def __init__(self, repo, **kwargs): | ||
| 17 | + def __init__(self, repo, id, mode=None, name=None): | ||
| 18 | 18 | """ | |
| 19 | 19 | Create an unbaked Blob containing just the specified attributes | |
| 20 | 20 | ||
| 21 | 21 | ``repo`` | |
| 22 | 22 | is the Repo | |
| 23 | 23 | ||
| 24 | - ``atts`` | ||
| 25 | - is a dict of instance variable data | ||
| 24 | + ``id`` | ||
| 25 | + is the git object id | ||
| 26 | + | ||
| 27 | + ``mode`` | ||
| 28 | + is the file mode | ||
| 29 | + | ||
| 30 | + ``name`` | ||
| 31 | + is the file name | ||
| 26 | 32 | ||
| 27 | 33 | Returns | |
| 28 | 34 | GitPython.Blob | |
| 29 | 35 | """ | |
| 30 | - self.id = None | ||
| 31 | - self.mode = None | ||
| 32 | - self.name = None | ||
| 36 | + self.repo = repo | ||
| 37 | + self.id = id | ||
| 38 | + self.mode = mode | ||
| 39 | + self.name = name | ||
| 40 | + | ||
| 33 | 41 | self._size = None | |
| 34 | 42 | self.data_stored = None | |
| 35 | 43 | ||
| 36 | - self.repo = repo | ||
| 37 | - for k, v in kwargs.items(): | ||
| 38 | - setattr(self, k, v) | ||
| 39 | - | ||
| 40 | 44 | @property | |
| 41 | 45 | def size(self): | |
| 42 | 46 | """ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,12 +9,13 @@ | |||
| 9 | 9 | ||
| 10 | 10 | from actor import Actor | |
| 11 | 11 | from lazy import LazyMixin | |
| 12 | - import tree | ||
| 12 | + from tree import Tree | ||
| 13 | 13 | import diff | |
| 14 | 14 | import stats | |
| 15 | 15 | ||
| 16 | 16 | class Commit(LazyMixin): | |
| 17 | - def __init__(self, repo, **kwargs): | ||
| 17 | + def __init__(self, repo, id, tree=None, author=None, authored_date=None, | ||
| 18 | + committer=None, committed_date=None, message=None, parents=None): | ||
| 18 | 19 | """ | |
| 19 | 20 | Instantiate a new Commit | |
| 20 | 21 | ||
@@ -42,29 +43,29 @@ def __init__(self, repo, **kwargs): | |||
| 42 | 43 | ``message`` | |
| 43 | 44 | is the first line of the commit message | |
| 44 | 45 | ||
| 46 | + ``parents`` | ||
| 47 | + is the list of the parents of the commit | ||
| 48 | + | ||
| 45 | 49 | Returns | |
| 46 | 50 | GitPython.Commit | |
| 47 | 51 | """ | |
| 48 | 52 | LazyMixin.__init__(self) | |
| 49 | 53 | ||
| 50 | 54 | self.repo = repo | |
| 51 | - self.id = None | ||
| 52 | - self.tree = None | ||
| 53 | - self.author = None | ||
| 54 | - self.authored_date = None | ||
| 55 | - self.committer = None | ||
| 56 | - self.committed_date = None | ||
| 57 | - self.message = None | ||
| 55 | + self.id = id | ||
| 58 | 56 | self.parents = None | |
| 59 | - | ||
| 60 | - for k, v in kwargs.items(): | ||
| 61 | - setattr(self, k, v) | ||
| 57 | + self.tree = None | ||
| 58 | + self.author = author | ||
| 59 | + self.authored_date = authored_date | ||
| 60 | + self.committer = committer | ||
| 61 | + self.committed_date = committed_date | ||
| 62 | + self.message = message | ||
| 62 | 63 | ||
| 63 | 64 | if self.id: | |
| 64 | - if 'parents' in kwargs: | ||
| 65 | - self.parents = map(lambda p: Commit(repo, id=p), kwargs['parents']) | ||
| 66 | - if 'tree' in kwargs: | ||
| 67 | - self.tree = tree.Tree(repo, id=kwargs['tree']) | ||
| 65 | + if parents is not None: | ||
| 66 | + self.parents = [Commit(repo, p) for p in parents] | ||
| 67 | + if tree is not None: | ||
| 68 | + self.tree = Tree(repo, id=tree) | ||
| 68 | 69 | ||
| 69 | 70 | def __bake__(self): | |
| 70 | 71 | temp = Commit.find_all(self.repo, self.id, max_count=1)[0] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -62,20 +62,19 @@ def __init__(self, path=None): | |||
| 62 | 62 | ||
| 63 | 63 | self.git = Git(self.wd) | |
| 64 | 64 | ||
| 65 | - @property | ||
| 66 | - def description(self): | ||
| 67 | - """ | ||
| 68 | - The project's description. Taken verbatim from GIT_REPO/description | ||
| 65 | + # Description property | ||
| 66 | + def _get_description(self): | ||
| 67 | + filename = os.path.join(self.path, 'description') | ||
| 68 | + return file(filename).read().rstrip() | ||
| 69 | 69 | ||
| 70 | - Returns | ||
| 71 | - str | ||
| 72 | - """ | ||
| 73 | - try: | ||
| 74 | - f = open(os.path.join(self.path, 'description')) | ||
| 75 | - result = f.read() | ||
| 76 | - return result.rstrip() | ||
| 77 | - finally: | ||
| 78 | - f.close() | ||
| 70 | + def _set_description(self, descr): | ||
| 71 | + filename = os.path.join(self.path, 'description') | ||
| 72 | + file(filename, 'w').write(descr+'\n') | ||
| 73 | + | ||
| 74 | + description = property(_get_description, _set_description, | ||
| 75 | + doc="the project's description") | ||
| 76 | + del _get_description | ||
| 77 | + del _set_description | ||
| 79 | 78 | ||
| 80 | 79 | @property | |
| 81 | 80 | def heads(self): | |
@@ -199,24 +198,22 @@ def commit_deltas_from(self, other_repo, ref = 'master', other_ref = 'master'): | |||
| 199 | 198 | diff_refs = list(set(other_repo_refs) - set(repo_refs)) | |
| 200 | 199 | return map(lambda ref: Commit.find_all(other_repo, ref, max_count=1)[0], diff_refs) | |
| 201 | 200 | ||
| 202 | - def tree(self, treeish = 'master', paths = []): | ||
| 201 | + def tree(self, treeish = 'master'): | ||
| 203 | 202 | """ | |
| 204 | 203 | The Tree object for the given treeish reference | |
| 205 | 204 | ||
| 206 | 205 | ``treeish`` | |
| 207 | 206 | is the reference (default 'master') | |
| 208 | - ``paths`` | ||
| 209 | - is an optional Array of directory paths to restrict the tree (default []) | ||
| 210 | 207 | ||
| 211 | 208 | Examples:: | |
| 212 | 209 | ||
| 213 | - repo.tree('master', ['lib/']) | ||
| 210 | + repo.tree('master') | ||
| 214 | 211 | ||
| 215 | 212 | ||
| 216 | 213 | Returns | |
| 217 | 214 | ``GitPython.Tree`` | |
| 218 | 215 | """ | |
| 219 | - return Tree.construct(self, treeish, paths) | ||
| 216 | + return Tree(self, id=treeish) | ||
| 220 | 217 | ||
| 221 | 218 | def blob(self, id): | |
| 222 | 219 | """ | |
@@ -377,25 +374,22 @@ def archive_tar_gz(self, treeish = 'master', prefix = None): | |||
| 377 | 374 | kwargs['prefix'] = prefix | |
| 378 | 375 | self.git.archive(treeish, "| gzip", **kwargs) | |
| 379 | 376 | ||
| 380 | - def enable_daemon_serve(self): | ||
| 381 | - """ | ||
| 382 | - Enable git-daemon serving of this repository by writing the | ||
| 383 | - git-daemon-export-ok file to its git directory | ||
| 384 | - | ||
| 385 | - Returns | ||
| 386 | - None | ||
| 387 | - """ | ||
| 388 | - touch(os.path.join(self.path, DAEMON_EXPORT_FILE)) | ||
| 389 | - | ||
| 390 | - def disable_daemon_serve(self): | ||
| 391 | - """ | ||
| 392 | - Disable git-daemon serving of this repository by ensuring there is no | ||
| 393 | - git-daemon-export-ok file in its git directory | ||
| 394 | - | ||
| 395 | - Returns | ||
| 396 | - None | ||
| 397 | - """ | ||
| 398 | - return os.remove(os.path.join(self.path, DAEMON_EXPORT_FILE)) | ||
| 377 | + def _get_daemon_export(self): | ||
| 378 | + filename = os.path.join(self.path, self.DAEMON_EXPORT_FILE) | ||
| 379 | + return os.path.exists(filename) | ||
| 380 | + | ||
| 381 | + def _set_daemon_export(self, value): | ||
| 382 | + filename = os.path.join(self.path, self.DAEMON_EXPORT_FILE) | ||
| 383 | + fileexists = os.path.exists(filename) | ||
| 384 | + if value and not fileexists: | ||
| 385 | + touch(filename) | ||
| 386 | + elif not value and fileexists: | ||
| 387 | + os.unlink(filename) | ||
| 388 | + | ||
| 389 | + daemon_export = property(_get_daemon_export, _set_daemon_export, | ||
| 390 | + doc="git-daemon export of this repository") | ||
| 391 | + del _get_daemon_export | ||
| 392 | + del _set_daemon_export | ||
| 399 | 393 | ||
| 400 | 394 | def _get_alternates(self): | |
| 401 | 395 | """ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,41 +9,29 @@ | |||
| 9 | 9 | import blob | |
| 10 | 10 | ||
| 11 | 11 | class Tree(LazyMixin): | |
| 12 | - def __init__(self, repo, **kwargs): | ||
| 12 | + def __init__(self, repo, id, mode=None, name=None): | ||
| 13 | 13 | LazyMixin.__init__(self) | |
| 14 | - self.repo = repo | ||
| 15 | - self.id = None | ||
| 16 | - self.mode = None | ||
| 17 | - self.name = None | ||
| 18 | - self.contents = None | ||
| 19 | - | ||
| 20 | - for k, v in kwargs.items(): | ||
| 21 | - setattr(self, k, v) | ||
| 22 | - | ||
| 23 | - def __bake__(self): | ||
| 24 | - temp = Tree.construct(self.repo, self.id) | ||
| 25 | - self.contents = temp.contents | ||
| 26 | - | ||
| 27 | - @classmethod | ||
| 28 | - def construct(cls, repo, treeish, paths = []): | ||
| 29 | - output = repo.git.ls_tree(treeish, *paths) | ||
| 30 | - return Tree(repo, id=treeish).construct_initialize(repo, treeish, output) | ||
| 31 | - | ||
| 32 | - def construct_initialize(self, repo, id, text): | ||
| 33 | 14 | self.repo = repo | |
| 34 | 15 | self.id = id | |
| 35 | - self.contents = [] | ||
| 36 | - self.__baked__ = False | ||
| 37 | - | ||
| 38 | - for line in text.splitlines(): | ||
| 39 | - self.contents.append(self.content_from_string(self.repo, line)) | ||
| 40 | - | ||
| 41 | - self.contents = [c for c in self.contents if c is not None] | ||
| 42 | - | ||
| 43 | - self.__bake_it__() | ||
| 44 | - return self | ||
| 16 | + self.mode = mode | ||
| 17 | + self.name = name | ||
| 18 | + self._contents = None | ||
| 45 | 19 | ||
| 46 | - def content_from_string(self, repo, text): | ||
| 20 | + def __bake__(self): | ||
| 21 | + # Ensure the treeish references directly a tree | ||
| 22 | + treeish = self.id | ||
| 23 | + if not treeish.endswith(':'): | ||
| 24 | + treeish = treeish + ':' | ||
| 25 | + | ||
| 26 | + # Read the tree contents. | ||
| 27 | + self._contents = {} | ||
| 28 | + for line in self.repo.git.ls_tree(self.id).splitlines(): | ||
| 29 | + obj = self.content_from_string(self.repo, line) | ||
| 30 | + if obj is not None: | ||
| 31 | + self._contents[obj.name] = obj | ||
| 32 | + | ||
| 33 | + @staticmethod | ||
| 34 | + def content_from_string(repo, text): | ||
| 47 | 35 | """ | |
| 48 | 36 | Parse a content item and create the appropriate object | |
| 49 | 37 | ||
@@ -84,12 +72,37 @@ def __div__(self, file): | |||
| 84 | 72 | Returns | |
| 85 | 73 | ``GitPython.Blob`` or ``GitPython.Tree`` or ``None`` if not found | |
| 86 | 74 | """ | |
| 87 | - contents = [c for c in self.contents if c.name == file] | ||
| 88 | - return contents and contents[0] or None | ||
| 75 | + return self.get(file) | ||
| 89 | 76 | ||
| 90 | 77 | @property | |
| 91 | 78 | def basename(self): | |
| 92 | 79 | os.path.basename(self.name) | |
| 93 | 80 | ||
| 94 | 81 | def __repr__(self): | |
| 95 | 82 | return '<GitPython.Tree "%s">' % self.id | |
| 83 | + | ||
| 84 | + # Implement the basics of the dict protocol: | ||
| 85 | + # directories/trees can be seen as object dicts. | ||
| 86 | + def __getitem__(self, key): | ||
| 87 | + return self._contents[key] | ||
| 88 | + | ||
| 89 | + def __iter__(self): | ||
| 90 | + return iter(self._contents) | ||
| 91 | + | ||
| 92 | + def __len__(self, keys): | ||
| 93 | + return len(self._contents) | ||
| 94 | + | ||
| 95 | + def __contains__(self, key): | ||
| 96 | + return key in self._contents | ||
| 97 | + | ||
| 98 | + def get(self, key): | ||
| 99 | + return self._contents.get(key) | ||
| 100 | + | ||
| 101 | + def items(self): | ||
| 102 | + return self._contents.items() | ||
| 103 | + | ||
| 104 | + def keys(self): | ||
| 105 | + return self._contents.keys() | ||
| 106 | + | ||
| 107 | + def values(self): | ||
| 108 | + return self._contents.values() | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,7 +25,9 @@ def test_new_should_raise_on_non_existant_path(self): | |||
| 25 | 25 | Repo("repos/foobar") | |
| 26 | 26 | ||
| 27 | 27 | def test_description(self): | |
| 28 | - assert_equal("Unnamed repository; edit this file to name it for gitweb.", self.repo.description) | ||
| 28 | + txt = "Test repository" | ||
| 29 | + self.repo.description = txt | ||
| 30 | + assert_equal(self.repo.description, txt) | ||
| 29 | 31 | ||
| 30 | 32 | def test_heads_should_return_array_of_head_objects(self): | |
| 31 | 33 | for head in self.repo.heads: | |
@@ -96,8 +98,8 @@ def test_tree(self, git): | |||
| 96 | 98 | ||
| 97 | 99 | tree = self.repo.tree('master') | |
| 98 | 100 | ||
| 99 | - assert_equal(4, len([c for c in tree.contents if isinstance(c, Blob)])) | ||
| 100 | - assert_equal(3, len([c for c in tree.contents if isinstance(c, Tree)])) | ||
| 101 | + assert_equal(4, len([c for c in tree.values() if isinstance(c, Blob)])) | ||
| 102 | + assert_equal(3, len([c for c in tree.values() if isinstance(c, Tree)])) | ||
| 101 | 103 | ||
| 102 | 104 | assert_true(git.called) | |
| 103 | 105 | assert_equal(git.call_args, (('ls_tree', 'master'), {})) | |
@@ -194,10 +196,12 @@ def test_archive_tar_gz(self): | |||
| 194 | 196 | ||
| 195 | 197 | @patch('git.utils', 'touch') | |
| 196 | 198 | def test_enable_daemon_serve(self, touch): | |
| 197 | - self.repo.enable_daemon_serve | ||
| 199 | + self.repo.daemon_serve = False | ||
| 200 | + assert_false(self.repo.daemon_serve) | ||
| 198 | 201 | ||
| 199 | 202 | def test_disable_daemon_serve(self): | |
| 200 | - self.repo.disable_daemon_serve | ||
| 203 | + self.repo.daemon_serve = True | ||
| 204 | + assert_true(self.repo.daemon_serve) | ||
| 201 | 205 | ||
| 202 | 206 | # @patch(os.path, 'exists') | |
| 203 | 207 | # @patch('__builtin__', 'open') | |
| Back | FazBrowse Home | New Git URL |
0 commit comments