/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

Implement to_files() for git merge directives.

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
    )
33
31
from bzrlib import (
34
32
    errors,
35
33
    ignores,
36
 
    inventory,
37
34
    lockable_files,
38
35
    lockdir,
39
36
    osutils,
40
37
    transport,
41
38
    tree,
42
 
    urlutils,
43
39
    workingtree,
44
40
    )
45
41
from bzrlib.decorators import (
46
42
    needs_read_lock,
47
 
    needs_write_lock,
48
43
    )
49
44
 
50
45
 
51
46
from bzrlib.plugins.git.inventory import (
52
47
    GitIndexInventory,
53
48
    )
 
49
from bzrlib.plugins.git.tree import (
 
50
    changes_from_git_changes,
 
51
    tree_delta_from_git_changes,
 
52
    )
54
53
 
55
54
 
56
55
IGNORE_FILENAME = ".gitignore"
67
66
        self._branch = branch
68
67
        self._transport = bzrdir.transport
69
68
 
70
 
        self.controldir = urlutils.join(self.repository._git._controldir, 'bzr')
 
69
        self.controldir = self.bzrdir.transport.local_abspath('bzr')
71
70
 
72
71
        try:
73
72
            os.makedirs(self.controldir)
82
81
        self.views = self._make_views()
83
82
        self._detect_case_handling()
84
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
 
85
96
    def unlock(self):
86
97
        # non-implementation specific cleanup
87
98
        self._cleanup()
108
119
                except (errors.NoSuchFile, IOError):
109
120
                    # TODO: Rather than come up with something here, use the old index
110
121
                    file = StringIO()
111
 
                    stat_val = (0, 0, 0, 0, stat.S_IFREG | 0644, 0, 0, 0, 0, 0)
 
122
                    from posix import stat_result
 
123
                    stat_val = stat_result((stat.S_IFREG | 0644, 0, 0, 0, 0, 0, 0, 0, 0, 0))
112
124
                blob.set_raw_string(file.read())
113
125
            elif entry.kind == "symlink":
114
126
                blob = Blob()
117
129
                except (errors.NoSuchFile, OSError):
118
130
                    # TODO: Rather than come up with something here, use the 
119
131
                    # old index
120
 
                    stat_val = (0, 0, 0, 0, stat.S_IFLNK, 0, 0, 0, 0, 0)
 
132
                    from posix import stat_result
 
133
                    stat_val = stat_result((stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
121
134
                blob.set_raw_string(entry.symlink_target)
 
135
            else:
 
136
                raise AssertionError("unknown kind '%s'" % entry.kind)
122
137
            # Add object to the repository if it didn't exist yet
123
138
            if not blob.id in self.repository._git.object_store:
124
139
                self.repository._git.object_store.add_object(blob)
165
180
    def get_file_sha1(self, file_id, path=None, stat_value=None):
166
181
        if not path:
167
182
            path = self._inventory.id2path(file_id)
168
 
        return osutils.sha_file_by_name(self.abspath(path).encode(osutils._fs_enc))
169
 
 
170
 
    def iter_changes(self, from_tree, include_unchanged=False,
171
 
                     specific_files=None, pb=None, extra_trees=None,
172
 
                     require_versioned=True, want_unversioned=False):
173
 
 
174
 
        intertree = tree.InterTree.get(from_tree, self)
175
 
        return intertree.iter_changes(include_unchanged, specific_files, pb,
176
 
            extra_trees, require_versioned, want_unversioned=want_unversioned)
 
183
        try:
 
184
            return osutils.sha_file_by_name(self.abspath(path).encode(osutils._fs_enc))
 
185
        except OSError, (num, msg):
 
186
            if num in (errno.EISDIR, errno.ENOENT):
 
187
                return None
 
188
            raise
 
189
 
 
190
    def revision_tree(self, revid):
 
191
        return self.repository.revision_tree(revid)
 
192
 
 
193
    @needs_read_lock
 
194
    def conflicts(self):
 
195
        # FIXME:
 
196
        return []
177
197
 
178
198
 
179
199
class GitWorkingTreeFormat(workingtree.WorkingTreeFormat):
180
200
 
 
201
    @property
 
202
    def _matchingbzrdir(self):
 
203
        from bzrlib.plugins.git import LocalGitBzrDirFormat
 
204
        return LocalGitBzrDirFormat()
 
205
 
181
206
    def get_format_description(self):
182
207
        return "Git Working Tree"
 
208
 
 
209
 
 
210
class InterIndexGitTree(tree.InterTree):
 
211
    """InterTree that works between a Git revision tree and an index."""
 
212
 
 
213
    def __init__(self, source, target):
 
214
        super(InterIndexGitTree, self).__init__(source, target)
 
215
        self._index = target.index
 
216
 
 
217
    @classmethod
 
218
    def is_compatible(cls, source, target):
 
219
        from bzrlib.plugins.git.repository import GitRevisionTree
 
220
        return (isinstance(source, GitRevisionTree) and 
 
221
                isinstance(target, GitWorkingTree))
 
222
 
 
223
    def compare(self, want_unchanged=False, specific_files=None,
 
224
                extra_trees=None, require_versioned=False, include_root=False,
 
225
                want_unversioned=False):
 
226
        changes = self._index.changes_from_tree(
 
227
            self.source._repository._git.object_store, self.source.tree, 
 
228
            want_unchanged=want_unchanged)
 
229
        ret = tree_delta_from_git_changes(changes, self.target.mapping, 
 
230
            specific_file=specific_files, require_versioned=require_versioned)
 
231
        if want_unversioned:
 
232
            for e in self.target.extras():
 
233
                ret.unversioned.append((e, None, osutils.file_kind(self.target.abspath(e))))
 
234
        return ret
 
235
 
 
236
    def iter_changes(self, include_unchanged=False, specific_files=None,
 
237
        pb=None, extra_trees=[], require_versioned=True, want_unversioned=False):
 
238
        changes = self._index.changes_from_tree(
 
239
            self.source._repository._git.object_store, self.source.tree, 
 
240
            want_unchanged=include_unchanged)
 
241
        # FIXME: Handle want_unversioned
 
242
        return changes_from_git_changes(changes, self.target.mapping, 
 
243
            specific_file=specific_files)
 
244
 
 
245
tree.InterTree.register_optimiser(InterIndexGitTree)