/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 workingtree.py

  • Committer: Jelmer Vernooij
  • Date: 2010-06-28 22:30:34 UTC
  • mto: (0.200.953 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20100628223034-vylrgdyakmqoupl6
use transport repo objects even for local access.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
from cStringIO import (
22
22
    StringIO,
23
23
    )
24
 
from dulwich.index import (
25
 
    Index,
26
 
    )
 
24
import errno
27
25
from dulwich.objects import (
28
26
    Blob,
29
27
    )
 
28
from dulwich.protocol import (
 
29
    ZERO_SHA,
 
30
    )
30
31
import os
31
32
import stat
32
33
 
38
39
    lockdir,
39
40
    osutils,
40
41
    transport,
41
 
    urlutils,
 
42
    tree,
42
43
    workingtree,
43
44
    )
44
45
from bzrlib.decorators import (
45
46
    needs_read_lock,
46
 
    needs_write_lock,
47
47
    )
48
48
 
49
49
 
50
50
from bzrlib.plugins.git.inventory import (
51
51
    GitIndexInventory,
52
52
    )
 
53
from bzrlib.plugins.git.tree import (
 
54
    changes_from_git_changes,
 
55
    tree_delta_from_git_changes,
 
56
    )
53
57
 
54
58
 
55
59
IGNORE_FILENAME = ".gitignore"
58
62
class GitWorkingTree(workingtree.WorkingTree):
59
63
    """A Git working tree."""
60
64
 
61
 
    def __init__(self, bzrdir, repo, branch):
 
65
    def __init__(self, bzrdir, repo, branch, index):
62
66
        self.basedir = bzrdir.root_transport.local_abspath('.')
63
67
        self.bzrdir = bzrdir
64
68
        self.repository = repo
66
70
        self._branch = branch
67
71
        self._transport = bzrdir.transport
68
72
 
69
 
        self.controldir = urlutils.join(self.repository._git._controldir, 'bzr')
 
73
        self.controldir = self.bzrdir.transport.local_abspath('bzr')
70
74
 
71
75
        try:
72
76
            os.makedirs(self.controldir)
77
81
        self._control_files = lockable_files.LockableFiles(
78
82
            transport.get_transport(self.controldir), 'lock', lockdir.LockDir)
79
83
        self._format = GitWorkingTreeFormat()
80
 
        self.index = self.repository._git.open_index()
 
84
        self.index = index
81
85
        self.views = self._make_views()
82
86
        self._detect_case_handling()
83
87
 
 
88
    def extras(self):
 
89
        """Yield all unversioned files in this WorkingTree.
 
90
        """
 
91
        for (dirpath, dirnames, filenames) in os.walk(self.basedir):
 
92
            if self.bzrdir.is_control_filename(dirpath[len(self.basedir):].strip("/")):
 
93
                continue
 
94
            for filename in filenames:
 
95
                relpath = os.path.join(dirpath[len(self.basedir):].strip("/"), filename)
 
96
                if not relpath in self.index:
 
97
                    yield relpath
 
98
 
 
99
 
84
100
    def unlock(self):
85
101
        # non-implementation specific cleanup
86
102
        self._cleanup()
107
123
                except (errors.NoSuchFile, IOError):
108
124
                    # TODO: Rather than come up with something here, use the old index
109
125
                    file = StringIO()
110
 
                    stat_val = (0, 0, 0, 0, stat.S_IFREG | 0644, 0, 0, 0, 0, 0)
111
 
                blob._text = file.read()
 
126
                    from posix import stat_result
 
127
                    stat_val = stat_result((stat.S_IFREG | 0644, 0, 0, 0, 0, 0, 0, 0, 0, 0))
 
128
                blob.set_raw_string(file.read())
112
129
            elif entry.kind == "symlink":
113
130
                blob = Blob()
114
 
                stat_val = os.stat(self.abspath(path))
115
 
                blob._text = entry.symlink_target
 
131
                try:
 
132
                    stat_val = os.lstat(self.abspath(path))
 
133
                except (errors.NoSuchFile, OSError):
 
134
                    # TODO: Rather than come up with something here, use the 
 
135
                    # old index
 
136
                    from posix import stat_result
 
137
                    stat_val = stat_result((stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
 
138
                blob.set_raw_string(entry.symlink_target)
 
139
            else:
 
140
                raise AssertionError("unknown kind '%s'" % entry.kind)
116
141
            # Add object to the repository if it didn't exist yet
117
142
            if not blob.id in self.repository._git.object_store:
118
143
                self.repository._git.object_store.add_object(blob)
119
144
            # Add an entry to the index or update the existing entry
120
 
            (mode, ino, dev, links, uid, gid, size, atime, mtime, ctime) = stat_val
121
 
            flags = 0
122
 
            self.index[path.encode("utf-8")] = (ctime, mtime, ino, dev, mode, uid, gid, size, blob.id, flags)
 
145
            flags = 0 # FIXME
 
146
            self.index[path.encode("utf-8")] = (stat_val.st_ctime, stat_val.st_mtime, stat_val.st_dev, stat_val.st_ino, stat_val.st_mode, stat_val.st_uid, stat_val.st_gid, stat_val.st_size, blob.id, flags)
123
147
 
124
148
    def flush(self):
125
149
        # TODO: Maybe this should only write on dirty ?
146
170
        self._ignoreset = ignore_globs
147
171
        return ignore_globs
148
172
 
 
173
    def set_last_revision(self, revid):
 
174
        self._change_last_revision(revid)
 
175
 
149
176
    def _reset_data(self):
150
177
        self._inventory_is_modified = False
151
 
        basis_inv = self.repository.get_inventory(self.mapping.revision_id_foreign_to_bzr(self.repository._git.head()))
152
 
        result = GitIndexInventory(basis_inv, self.mapping, self.index)
 
178
        try:
 
179
            head = self.repository._git.head()
 
180
        except KeyError, name:
 
181
            raise errors.NotBranchError("branch %s at %s" % (name, self.repository.base))
 
182
        basis_inv = self.repository.get_inventory(self.branch.lookup_foreign_revision_id(head))
 
183
        store = self.repository._git.object_store
 
184
        if head == ZERO_SHA:
 
185
            fileid_map = {}
 
186
            result = inventory.Inventory(root_id=None)
 
187
        else:
 
188
            fileid_map = self.mapping.get_fileid_map(store.__getitem__,
 
189
                store[head].tree)
 
190
            result = GitIndexInventory(basis_inv, fileid_map, self.index, store)
153
191
        self._set_inventory(result, dirty=False)
154
192
 
155
193
    @needs_read_lock
156
194
    def get_file_sha1(self, file_id, path=None, stat_value=None):
157
195
        if not path:
158
196
            path = self._inventory.id2path(file_id)
159
 
        return osutils.sha_file_by_name(self.abspath(path).encode(osutils._fs_enc))
 
197
        try:
 
198
            return osutils.sha_file_by_name(self.abspath(path).encode(osutils._fs_enc))
 
199
        except OSError, (num, msg):
 
200
            if num in (errno.EISDIR, errno.ENOENT):
 
201
                return None
 
202
            raise
 
203
 
 
204
    def revision_tree(self, revid):
 
205
        return self.repository.revision_tree(revid)
 
206
 
 
207
    @needs_read_lock
 
208
    def conflicts(self):
 
209
        # FIXME:
 
210
        return []
160
211
 
161
212
 
162
213
class GitWorkingTreeFormat(workingtree.WorkingTreeFormat):
163
214
 
 
215
    @property
 
216
    def _matchingbzrdir(self):
 
217
        from bzrlib.plugins.git import LocalGitBzrDirFormat
 
218
        return LocalGitBzrDirFormat()
 
219
 
164
220
    def get_format_description(self):
165
221
        return "Git Working Tree"
 
222
 
 
223
 
 
224
class InterIndexGitTree(tree.InterTree):
 
225
    """InterTree that works between a Git revision tree and an index."""
 
226
 
 
227
    def __init__(self, source, target):
 
228
        super(InterIndexGitTree, self).__init__(source, target)
 
229
        self._index = target.index
 
230
 
 
231
    @classmethod
 
232
    def is_compatible(cls, source, target):
 
233
        from bzrlib.plugins.git.repository import GitRevisionTree
 
234
        return (isinstance(source, GitRevisionTree) and 
 
235
                isinstance(target, GitWorkingTree))
 
236
 
 
237
    def compare(self, want_unchanged=False, specific_files=None,
 
238
                extra_trees=None, require_versioned=False, include_root=False,
 
239
                want_unversioned=False):
 
240
        changes = self._index.changes_from_tree(
 
241
            self.source._repository._git.object_store, self.source.tree, 
 
242
            want_unchanged=want_unchanged)
 
243
        source_fileid_map = self.source.mapping.get_fileid_map(
 
244
            self.source._repository._git.object_store.__getitem__,
 
245
            self.source.tree)
 
246
        if self.target.mapping.BZR_FILE_IDS_FILE is not None:
 
247
            try:
 
248
                file_id = self.target.path2id(
 
249
                    self.target.mapping.BZR_FILE_IDS_FILE)
 
250
            except errors.NoSuchId:
 
251
                target_fileid_map = {}
 
252
            else:
 
253
                target_fileid_map = self.import_fileid_map(Blob.from_string(self.target.file_text(file_id)))
 
254
        else:
 
255
            target_fileid_map = {}
 
256
        ret = tree_delta_from_git_changes(changes, self.target.mapping, 
 
257
            (source_fileid_map, target_fileid_map),
 
258
            specific_file=specific_files, require_versioned=require_versioned)
 
259
        if want_unversioned:
 
260
            for e in self.target.extras():
 
261
                ret.unversioned.append((e, None, osutils.file_kind(self.target.abspath(e))))
 
262
        return ret
 
263
 
 
264
    def iter_changes(self, include_unchanged=False, specific_files=None,
 
265
        pb=None, extra_trees=[], require_versioned=True, want_unversioned=False):
 
266
        changes = self._index.changes_from_tree(
 
267
            self.source._repository._git.object_store, self.source.tree, 
 
268
            want_unchanged=include_unchanged)
 
269
        # FIXME: Handle want_unversioned
 
270
        return changes_from_git_changes(changes, self.target.mapping, 
 
271
            specific_file=specific_files)
 
272
 
 
273
tree.InterTree.register_optimiser(InterIndexGitTree)