소스 검색

Import changes from my bzr-git branch, to make dulwich before more like python-git.

Jelmer Vernooij 16 년 전
부모
커밋
dbe7eb6431
5개의 변경된 파일81개의 추가작업 그리고 16개의 파일을 삭제
  1. 1 0
      dulwich/__init__.py
  2. 22 0
      dulwich/commit.py
  3. 46 4
      dulwich/repo.py
  4. 4 4
      dulwich/tests/test_objects.py
  5. 8 8
      dulwich/tests/test_repository.py

+ 1 - 0
dulwich/__init__.py

@@ -16,3 +16,4 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 # MA  02110-1301, USA.
 
+import repo

+ 22 - 0
dulwich/commit.py

@@ -0,0 +1,22 @@
+# commit.py -- commit for python-git
+# Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
+# 
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; version 2
+# of the License.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA  02110-1301, USA.
+
+class Commit(object):
+
+    def __init__(self, id):
+        self.id = id

+ 46 - 4
dulwich/repository.py → dulwich/repo.py

@@ -1,5 +1,6 @@
-# repository.py -- For dealing wih git repositories.
+# repo.py -- For dealing wih git repositories.
 # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
+# Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
 # 
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -18,6 +19,7 @@
 
 import os
 
+from commit import Commit
 from errors import MissingCommitError
 from objects import (ShaFile,
                      Commit,
@@ -28,7 +30,15 @@ from objects import (ShaFile,
 objectdir = 'objects'
 symref = 'ref: '
 
-class Repository(object):
+
+class Tag(object):
+
+    def __init__(self, name, ref):
+        self.name = name
+        self.commit = Commit(ref)
+
+
+class Repo(object):
 
   ref_locs = ['', 'refs', 'refs/tags', 'refs/heads', 'refs/remotes']
 
@@ -40,6 +50,8 @@ class Repository(object):
     else:
       self.bare = True
       self._basedir = root
+    self.path = controldir
+    self.tags = [Tag(name, ref) for name, ref in self.get_tags().items()]
 
   def basedir(self):
     return self._basedir
@@ -67,6 +79,18 @@ class Repository(object):
       if os.path.exists(file):
         return self._get_ref(file)
 
+  def get_tags(self):
+    ret = {}
+    for name in os.listdir(os.path.join(self.basedir(), 'refs', 'tags')):
+      ret[name] = self._get_ref(os.path.join(self.basedir(), 'refs', 'tags', name))
+    return ret
+
+  def heads(self):
+    ret = {}
+    for name in os.listdir(os.path.join(self.basedir(), 'refs', 'heads')):
+      ret[name] = self._get_ref(os.path.join(self.basedir(), 'refs', 'heads', name))
+    return ret
+
   def head(self):
     return self.ref('HEAD')
 
@@ -83,7 +107,7 @@ class Repository(object):
   def get_object(self, sha):
     return self._get_object(sha, ShaFile)
 
-  def get_commit(self, sha):
+  def commit(self, sha):
     return self._get_object(sha, Commit)
 
   def get_tree(self, sha):
@@ -109,7 +133,7 @@ class Repository(object):
     history = []
     while pending_commits != []:
       head = pending_commits.pop(0)
-      commit = self.get_commit(head)
+      commit = self.commit(head)
       if commit is None:
         raise MissingCommitError(head)
       if commit in history:
@@ -125,3 +149,21 @@ class Repository(object):
     history.reverse()
     return history
 
+  @classmethod
+  def init_bare(cls, path, mkdir=True):
+      for d in [["objects"], 
+                ["objects", "info"], 
+                ["objects", "pack"],
+                ["branches"],
+                ["refs"],
+                ["refs", "tags"],
+                ["refs", "heads"],
+                ["hooks"],
+                ["info"]]:
+          os.mkdir(os.path.join(path, *d))
+      open(os.path.join(path, 'HEAD'), 'w').write("ref: refs/heads/master\n")
+      open(os.path.join(path, 'description'), 'w').write("Unnamed repository")
+      open(os.path.join(path, 'info', 'excludes'), 'w').write("")
+
+  create = init_bare
+

+ 4 - 4
dulwich/tests/test_objects.py

@@ -43,7 +43,7 @@ class BlobReadTests(unittest.TestCase):
   def get_tree(self, sha):
     return self.get_sha_file(Tree, 'trees', sha)
 
-  def get_commit(self, sha):
+  def commit(self, sha):
     return self.get_sha_file(Commit, 'commits', sha)
 
   def test_decompress_simple_blob(self):
@@ -81,7 +81,7 @@ class BlobReadTests(unittest.TestCase):
 
   def test_read_commit_from_file(self):
     sha = '60dacdc733de308bb77bb76ce0fb0f9b44c9769e'
-    c = self.get_commit(sha)
+    c = self.commit(sha)
     self.assertEqual(c.tree(), tree_sha)
     self.assertEqual(c.parents(), ['0d89f20333fbb1d2f3a94da77f4981373d8f4310'])
     self.assertEqual(c.author(),
@@ -93,7 +93,7 @@ class BlobReadTests(unittest.TestCase):
 
   def test_read_commit_no_parents(self):
     sha = '0d89f20333fbb1d2f3a94da77f4981373d8f4310'
-    c = self.get_commit(sha)
+    c = self.commit(sha)
     self.assertEqual(c.tree(), '90182552c4a85a45ec2a835cadc3451bebdfe870')
     self.assertEqual(c.parents(), [])
     self.assertEqual(c.author(),
@@ -105,7 +105,7 @@ class BlobReadTests(unittest.TestCase):
 
   def test_read_commit_two_parents(self):
     sha = '5dac377bdded4c9aeb8dff595f0faeebcc8498cc'
-    c = self.get_commit(sha)
+    c = self.commit(sha)
     self.assertEqual(c.tree(), 'd80c186a03f423a81b39df39dc87fd269736ca86')
     self.assertEqual(c.parents(), ['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
                                    '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'])

+ 8 - 8
dulwich/tests/test_repository.py

@@ -20,14 +20,14 @@ import os
 import unittest
 
 from dulwich import errors
-from dulwich.repository import Repository
+from dulwich.repo import Repo
 
 missing_sha = 'b91fa4d900g17e99b433218e988c4eb4a3e9a097'
 
 class RepositoryTests(unittest.TestCase):
 
   def open_repo(self, name):
-    return Repository(os.path.join(os.path.dirname(__file__),
+    return Repo(os.path.join(os.path.dirname(__file__),
                       'data/repos', name, '.git'))
 
   def test_simple_props(self):
@@ -55,19 +55,19 @@ class RepositoryTests(unittest.TestCase):
     obj = r.get_object(missing_sha)
     self.assertEqual(obj, None)
 
-  def test_get_commit(self):
+  def test_commit(self):
     r = self.open_repo('a')
-    obj = r.get_commit(r.head())
+    obj = r.commit(r.head())
     self.assertEqual(obj._type, 'commit')
 
-  def test_get_commit_not_commit(self):
+  def test_commit_not_commit(self):
     r = self.open_repo('a')
     self.assertRaises(errors.NotCommitError,
-                      r.get_commit, '4f2e6529203aa6d44b5af6e3292c837ceda003f9')
+                      r.commit, '4f2e6529203aa6d44b5af6e3292c837ceda003f9')
 
   def test_get_tree(self):
     r = self.open_repo('a')
-    commit = r.get_commit(r.head())
+    commit = r.commit(r.head())
     tree = r.get_tree(commit.tree())
     self.assertEqual(tree._type, 'tree')
     self.assertEqual(tree.sha().hexdigest(), commit.tree())
@@ -78,7 +78,7 @@ class RepositoryTests(unittest.TestCase):
 
   def test_get_blob(self):
     r = self.open_repo('a')
-    commit = r.get_commit(r.head())
+    commit = r.commit(r.head())
     tree = r.get_tree(commit.tree())
     blob_sha = tree.entries()[0][2]
     blob = r.get_blob(blob_sha)