/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to dir.py

Fix some places where we were way too much memory for repositories with a large number of entries in the inventory and a large number of revisions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
"""An adapter between a Git control dir and a Bazaar BzrDir"""
18
 
 
19
 
import os
20
 
 
21
 
import bzrlib
22
 
from bzrlib.lazy_import import lazy_import
 
17
"""An adapter between a Git control dir and a Bazaar BzrDir."""
 
18
 
23
19
from bzrlib import (
24
20
    bzrdir,
 
21
    errors as bzr_errors,
25
22
    lockable_files,
26
23
    urlutils,
27
24
    )
28
25
 
29
 
lazy_import(globals(), """
30
 
from bzrlib.lockable_files import TransportLock
 
26
LockWarner = getattr(lockable_files, "_LockWarner", None)
 
27
 
31
28
from bzrlib.plugins.git import (
32
 
    errors,
33
 
    branch,
34
 
    repository,
35
 
    workingtree,
 
29
    LocalGitBzrDirFormat,
 
30
    get_rich_root_format,
36
31
    )
37
 
""")
38
 
 
39
 
from bzrlib.plugins.git import LocalGitBzrDirFormat
40
 
 
41
32
 
42
33
 
43
34
class GitLock(object):
58
49
    def validate_token(self, token):
59
50
        pass
60
51
 
 
52
    def break_lock(self):
 
53
        pass
 
54
 
61
55
 
62
56
class GitLockableFiles(lockable_files.LockableFiles):
63
57
    """Git specific lockable files abstraction."""
66
60
        self._lock = lock
67
61
        self._transaction = None
68
62
        self._lock_mode = None
69
 
        self._lock_count = 0
70
63
        self._transport = transport
 
64
        if LockWarner is None:
 
65
            # Bzr 1.13
 
66
            self._lock_count = 0
 
67
        else:
 
68
            self._lock_warner = LockWarner(repr(self))
71
69
 
72
70
 
73
71
class GitDir(bzrdir.BzrDir):
77
75
        return True
78
76
 
79
77
    def cloning_metadir(self, stacked=False):
80
 
        return bzrlib.bzrdir.format_registry.make_bzrdir("1.9-rich-root")
 
78
        return get_rich_root_format(stacked)
81
79
 
82
80
 
83
81
class LocalGitDir(GitDir):
84
82
    """An adapter to the '.git' dir used by git."""
85
83
 
86
 
    _gitrepository_class = repository.LocalGitRepository
 
84
    def _get_gitrepository_class(self):
 
85
        from bzrlib.plugins.git.repository import LocalGitRepository
 
86
        return LocalGitRepository
 
87
 
 
88
    _gitrepository_class = property(_get_gitrepository_class)
87
89
 
88
90
    def __init__(self, transport, lockfiles, gitrepo, format):
89
91
        self._format = format
94
96
        else:
95
97
            self.transport = transport.clone('.git')
96
98
        self._lockfiles = lockfiles
 
99
        self._mode_check_done = None
 
100
 
 
101
    def is_control_filename(self, filename):
 
102
        return filename == '.git' or filename.startswith('.git/')
97
103
 
98
104
    def get_branch_transport(self, branch_format):
99
105
        if branch_format is None:
100
106
            return self.transport
101
107
        if isinstance(branch_format, LocalGitBzrDirFormat):
102
108
            return self.transport
103
 
        raise errors.bzr_errors.IncompatibleFormat(branch_format, self._format)
 
109
        raise bzr_errors.IncompatibleFormat(branch_format, self._format)
104
110
 
105
111
    get_repository_transport = get_branch_transport
106
112
    get_workingtree_transport = get_branch_transport
107
113
 
108
 
    def open_branch(self, ignored=None):
 
114
    def open_branch(self, ignore_fallbacks=None):
109
115
        """'create' a branch for this dir."""
110
116
        repo = self.open_repository()
111
 
        return branch.LocalGitBranch(self, repo, "HEAD", repo._git.head(), self._lockfiles)
 
117
        from bzrlib.plugins.git.branch import LocalGitBranch
 
118
        return LocalGitBranch(self, repo, "HEAD", self._lockfiles)
112
119
 
113
120
    def open_repository(self, shared=False):
114
121
        """'open' a repository for this dir."""
115
122
        return self._gitrepository_class(self, self._lockfiles)
116
123
 
117
124
    def open_workingtree(self, recommend_upgrade=True):
118
 
        if (not self._git.bare and 
119
 
            os.path.exists(os.path.join(self._git.controldir(), "index"))):
120
 
            return workingtree.GitWorkingTree(self, self.open_repository(), 
 
125
        if not self._git.bare and self._git.has_index():
 
126
            from bzrlib.plugins.git.workingtree import GitWorkingTree
 
127
            return GitWorkingTree(self, self.open_repository(), 
121
128
                                                  self.open_branch())
122
129
        loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
123
 
        raise errors.bzr_errors.NoWorkingTree(loc)
 
130
        raise bzr_errors.NoWorkingTree(loc)
124
131
 
125
132
    def create_repository(self, shared=False):
126
133
        return self.open_repository()
 
134
 
 
135
    def create_branch(self):
 
136
        return self.open_branch()
 
137
 
 
138
    def backup_bzrdir(self):
 
139
        if self._git.bare:
 
140
            self.root_transport.copy_tree(".git", ".git.backup")
 
141
            return (self.root_transport.abspath(".git"),
 
142
                    self.root_transport.abspath(".git.backup"))
 
143
        else:
 
144
            raise bzr_errors.BzrError("Unable to backup bare repositories")
 
145
 
 
146
    def create_workingtree(self, revision_id=None, from_branch=None,
 
147
        accelerator_tree=None, hardlink=False):
 
148
        if self._git.bare:
 
149
            raise bzr_errors.BzrError("Can't create working tree in a bare repo")
 
150
        from dulwich.index import write_index
 
151
        from dulwich.pack import SHA1Writer
 
152
        f = open(self.transport.local_abspath("index"), 'w+')
 
153
        try:
 
154
            f = SHA1Writer(f)
 
155
            write_index(f, [])
 
156
        finally:
 
157
            f.close()
 
158
        return self.open_workingtree()