/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

Update docs.

Show diffs side-by-side

added added

removed removed

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