Rev 90: Basic support for opening working trees. in http://people.samba.org/bzr/jelmer/bzr-git/trunk

Jelmer Vernooij jelmer at samba.org
Sat Jul 26 21:56:51 BST 2008


At http://people.samba.org/bzr/jelmer/bzr-git/trunk

------------------------------------------------------------
revno: 90
revision-id: jelmer at samba.org-20080726205651-0ug01npe1r2ff6d4
parent: jelmer at samba.org-20080726191345-j3rrrr86bq9tb0i7
committer: Jelmer Vernooij <jelmer at samba.org>
branch nick: trunk
timestamp: Sat 2008-07-26 22:56:51 +0200
message:
  Basic support for opening working trees.
added:
  git_workingtree.py             git_workingtree.py-20080726200434-agakdygzl62md449-1
modified:
  git_dir.py                     git_dir.py-20071108234408-ygidvy5hviixghsd-1
  git_repository.py              git_repository.py-20071108234408-ygidvy5hviixghsd-2
  tests/test_blackbox.py         test_blackbox.py-20071228193717-18el6d8x4qddug3w-1
  tests/test_git_dir.py          test_git_dir.py-20071108234408-ygidvy5hviixghsd-4
=== modified file 'git_dir.py'
--- a/git_dir.py	2008-07-26 18:12:00 +0000
+++ b/git_dir.py	2008-07-26 20:56:51 +0000
@@ -16,6 +16,8 @@
 
 """An adapter between a Git control dir and a Bazaar BzrDir"""
 
+import git
+
 from bzrlib.lazy_import import lazy_import
 from bzrlib import (
     bzrdir,
@@ -28,6 +30,7 @@
     errors,
     git_branch,
     git_repository,
+    git_workingtree,
     )
 """)
 
@@ -63,10 +66,14 @@
 
     _gitrepository_class = git_repository.GitRepository
 
-    def __init__(self, transport, lockfiles, format):
+    def __init__(self, transport, lockfiles, gitrepo, format):
         self._format = format
         self.root_transport = transport
-        self.transport = transport.clone('.git')
+        self._git = gitrepo
+        if gitrepo.bare:
+            self.transport = transport
+        else:
+            self.transport = transport.clone('.git')
         self._lockfiles = lockfiles
 
     def get_branch_transport(self, branch_format):
@@ -97,8 +104,12 @@
         return self._gitrepository_class(self, self._lockfiles)
 
     def open_workingtree(self, recommend_upgrade=True):
-        loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
-        raise errors.bzr_errors.NoWorkingTree(loc)
+        if self._git.bare:
+            loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
+            raise errors.bzr_errors.NoWorkingTree(loc)
+        else:
+            return git_workingtree.GitWorkingTree(self, self.open_repository(), 
+                                                  self.open_branch())
 
     def cloning_metadir(self):
         return bzrdir.BzrDirFormat.get_default_format()
@@ -113,19 +124,21 @@
     def _known_formats(self):
         return set([GitBzrDirFormat()])
 
-    def open(self, transport, _create=False, _found=None):
+    def open(self, transport, _found=None):
         """Open this directory.
 
-        :param _create: create the git dir on the fly. private to GitDirFormat.
         """
         # we dont grok readonly - git isn't integrated with transport.
         url = transport.base
         if url.startswith('readonly+'):
             url = url[len('readonly+'):]
-        if not transport.has('.git'):
+
+        try:
+            gitrepo = git.repo.Repo(transport.local_abspath("."))
+        except errors.bzr_errors.NotLocalUrl:
             raise errors.bzr_errors.NotBranchError(path=transport.base)
         lockfiles = GitLockableFiles(GitLock())
-        return self._gitdir_class(transport, lockfiles, self)
+        return self._gitdir_class(transport, lockfiles, gitrepo, self)
 
     @classmethod
     def probe_transport(klass, transport):

=== modified file 'git_repository.py'
--- a/git_repository.py	2008-07-26 18:58:29 +0000
+++ b/git_repository.py	2008-07-26 20:56:51 +0000
@@ -49,7 +49,7 @@
         self.base = gitdir.root_transport.base
         self.bzrdir = gitdir
         self.control_files = lockfiles
-        self._git = git.repo.Repo(gitdir.root_transport.local_abspath("."))
+        self._git = gitdir._git
         cache_dir = cache.create_cache_dir()
         cachedir_transport = get_transport(cache_dir)
         cache_file = os.path.join(cache_dir, 'cache-%s' % ids.NAMESPACE)

=== added file 'git_workingtree.py'
--- a/git_workingtree.py	1970-01-01 00:00:00 +0000
+++ b/git_workingtree.py	2008-07-26 20:56:51 +0000
@@ -0,0 +1,72 @@
+# Copyright (C) 2008 Jelmer Vernooij <jelmer at 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; either version 2 of the License, or
+# (at your option) any later version.
+#
+# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+"""An adapter between a Git index and a Bazaar Working Tree"""
+
+import os
+
+from bzrlib import (
+    inventory,
+    lockable_files,
+    lockdir,
+    transport,
+    urlutils,
+    workingtree,
+    )
+
+class GitWorkingTree(workingtree.WorkingTree):
+    """A Git working tree."""
+
+    def __init__(self, bzrdir, repo, branch):
+        self.basedir = bzrdir.transport.base
+        self.bzrdir = bzrdir
+        self.repository = repo
+        self._branch = branch
+        self._transport = bzrdir.transport
+
+        self.controldir = urlutils.join(self.repository._git.path, 'bzr')
+
+        try:
+            os.makedirs(self.controldir)
+            os.makedirs(os.path.join(self.controldir, 'lock'))
+        except OSError:
+            pass
+
+        self._control_files = lockable_files.LockableFiles(
+            transport.get_transport(self.controldir), 'lock', lockdir.LockDir)
+
+        self._format = GitWorkingTreeFormat()
+
+    def lock_read(self):
+        pass
+
+    def unlock(self):
+        pass
+
+    def is_control_filename(self, path):
+        return os.path.basename(path) == ".git"
+
+    def _get_inventory(self):
+        return inventory.Inventory()
+
+    inventory = property(_get_inventory,
+                         doc="Inventory of this Tree")
+
+
+class GitWorkingTreeFormat(workingtree.WorkingTreeFormat):
+
+    def get_format_description(self):
+        return "Git Working Tree"

=== modified file 'tests/test_blackbox.py'
--- a/tests/test_blackbox.py	2008-07-26 19:13:45 +0000
+++ b/tests/test_blackbox.py	2008-07-26 20:56:51 +0000
@@ -41,7 +41,7 @@
         self.simple_commit()
         output, error = self.run_bzr(['info'])
         self.assertEqual(error, '')
-        self.assertTrue("Repository branch (format: git)" in output)
+        self.assertTrue("Repository tree (format: git)" in output)
 
     def test_branch(self):
         os.mkdir("gitbranch")
@@ -57,9 +57,9 @@
         self.assertEqual(error, 'Branched 1 revision(s).\n')
         self.assertEqual(output, '')
 
-    def test_ls(self):
+    def test_branch_ls(self):
         self.simple_commit()
-        output, error = self.run_bzr(['ls'])
+        output, error = self.run_bzr(['ls', '-r-1'])
         self.assertEqual(error, '')
         self.assertEqual(output, "a\n")
 
@@ -68,7 +68,7 @@
 
         output, error = self.run_bzr(['info', '-v'])
         self.assertEqual(error, '')
-        self.assertTrue("Repository branch (format: git)" in output)
+        self.assertTrue("Repository tree (format: git)" in output)
         self.assertTrue("control: Local Git Repository" in output)
         self.assertTrue("branch: Git Branch" in output)
         self.assertTrue("repository: Git Repository" in output)

=== modified file 'tests/test_git_dir.py'
--- a/tests/test_git_dir.py	2008-07-26 15:46:58 +0000
+++ b/tests/test_git_dir.py	2008-07-26 20:56:51 +0000
@@ -16,12 +16,10 @@
 
 """Test the GitDir class"""
 
-import subprocess
-
-from bzrlib import bzrdir
+from bzrlib import bzrdir, errors
 
 from bzrlib.plugins.git import tests
-from bzrlib.plugins.git import git_dir
+from bzrlib.plugins.git import git_dir, git_workingtree
 
 
 class TestGitDir(tests.TestCaseInTempDir):
@@ -34,6 +32,19 @@
         gd = bzrdir.BzrDir.open('.')
         self.assertIsInstance(gd, git_dir.GitDir)
 
+    def test_open_workingtree(self):
+        tests.run_git('init')
+
+        gd = bzrdir.BzrDir.open('.')
+        wt = gd.open_workingtree()
+        self.assertIsInstance(wt, git_workingtree.GitWorkingTree)
+
+    def test_open_workingtree_bare(self):
+        tests.run_git('--bare', 'init')
+
+        gd = bzrdir.BzrDir.open('.')
+        self.assertRaises(errors.NoWorkingTree, gd.open_workingtree)
+
 
 class TestGitDirFormat(tests.TestCaseInTempDir):
 




More information about the bazaar-commits mailing list