/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 breezy/git/tree.py

  • Committer: Jelmer Vernooij
  • Date: 2018-09-30 00:33:53 UTC
  • mto: This revision was merged to the branch mainline in revision 7134.
  • Revision ID: jelmer@jelmer.uk-20180930003353-2z5sugalbxfxfiru
When opening working trees with .git files, open the right control transport.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
 
 
18
"""Git Trees."""
 
19
 
 
20
from __future__ import absolute_import
 
21
 
 
22
from collections import deque
 
23
import errno
 
24
from io import BytesIO
 
25
import os
 
26
 
 
27
from dulwich.index import (
 
28
    blob_from_path_and_stat,
 
29
    cleanup_mode,
 
30
    commit_tree,
 
31
    index_entry_from_stat,
 
32
    )
 
33
from dulwich.object_store import (
 
34
    tree_lookup_path,
 
35
    OverlayObjectStore,
 
36
    )
 
37
from dulwich.objects import (
 
38
    Blob,
 
39
    Tree,
 
40
    ZERO_SHA,
 
41
    S_IFGITLINK,
 
42
    S_ISGITLINK,
 
43
    )
 
44
import stat
 
45
import posixpath
 
46
 
 
47
from .. import (
 
48
    controldir as _mod_controldir,
 
49
    delta,
 
50
    errors,
 
51
    lock,
 
52
    mutabletree,
 
53
    osutils,
 
54
    revisiontree,
 
55
    trace,
 
56
    tree as _mod_tree,
 
57
    workingtree,
 
58
    )
 
59
from ..revision import (
 
60
    CURRENT_REVISION,
 
61
    NULL_REVISION,
 
62
    )
 
63
from ..sixish import (
 
64
    text_type,
 
65
    viewitems,
 
66
    )
 
67
 
 
68
from .mapping import (
 
69
    mode_is_executable,
 
70
    mode_kind,
 
71
    GitFileIdMap,
 
72
    default_mapping,
 
73
    )
 
74
 
 
75
 
 
76
class GitTreeDirectory(_mod_tree.TreeDirectory):
 
77
 
 
78
    __slots__ = ['file_id', 'name', 'parent_id', 'children']
 
79
 
 
80
    def __init__(self, file_id, name, parent_id):
 
81
        self.file_id = file_id
 
82
        self.name = name
 
83
        self.parent_id = parent_id
 
84
        # TODO(jelmer)
 
85
        self.children = {}
 
86
 
 
87
    @property
 
88
    def kind(self):
 
89
        return 'directory'
 
90
 
 
91
    @property
 
92
    def executable(self):
 
93
        return False
 
94
 
 
95
    def copy(self):
 
96
        return self.__class__(
 
97
            self.file_id, self.name, self.parent_id)
 
98
 
 
99
    def __repr__(self):
 
100
        return "%s(file_id=%r, name=%r, parent_id=%r)" % (
 
101
            self.__class__.__name__, self.file_id, self.name,
 
102
            self.parent_id)
 
103
 
 
104
    def __eq__(self, other):
 
105
        return (self.kind == other.kind and
 
106
                self.file_id == other.file_id and
 
107
                self.name == other.name and
 
108
                self.parent_id == other.parent_id)
 
109
 
 
110
 
 
111
class GitTreeFile(_mod_tree.TreeFile):
 
112
 
 
113
    __slots__ = ['file_id', 'name', 'parent_id', 'text_size', 'text_sha1',
 
114
                 'executable']
 
115
 
 
116
    def __init__(self, file_id, name, parent_id, text_size=None,
 
117
                 text_sha1=None, executable=None):
 
118
        self.file_id = file_id
 
119
        self.name = name
 
120
        self.parent_id = parent_id
 
121
        self.text_size = text_size
 
122
        self.text_sha1 = text_sha1
 
123
        self.executable = executable
 
124
 
 
125
    @property
 
126
    def kind(self):
 
127
        return 'file'
 
128
 
 
129
    def __eq__(self, other):
 
130
        return (self.kind == other.kind and
 
131
                self.file_id == other.file_id and
 
132
                self.name == other.name and
 
133
                self.parent_id == other.parent_id and
 
134
                self.text_sha1 == other.text_sha1 and
 
135
                self.text_size == other.text_size and
 
136
                self.executable == other.executable)
 
137
 
 
138
    def __repr__(self):
 
139
        return "%s(file_id=%r, name=%r, parent_id=%r, text_size=%r, text_sha1=%r, executable=%r)" % (
 
140
            type(self).__name__, self.file_id, self.name, self.parent_id,
 
141
            self.text_size, self.text_sha1, self.executable)
 
142
 
 
143
    def copy(self):
 
144
        ret = self.__class__(
 
145
                self.file_id, self.name, self.parent_id)
 
146
        ret.text_sha1 = self.text_sha1
 
147
        ret.text_size = self.text_size
 
148
        ret.executable = self.executable
 
149
        return ret
 
150
 
 
151
 
 
152
class GitTreeSymlink(_mod_tree.TreeLink):
 
153
 
 
154
    __slots__ = ['file_id', 'name', 'parent_id', 'symlink_target']
 
155
 
 
156
    def __init__(self, file_id, name, parent_id,
 
157
                 symlink_target=None):
 
158
        self.file_id = file_id
 
159
        self.name = name
 
160
        self.parent_id = parent_id
 
161
        self.symlink_target = symlink_target
 
162
 
 
163
    @property
 
164
    def kind(self):
 
165
        return 'symlink'
 
166
 
 
167
    @property
 
168
    def executable(self):
 
169
        return False
 
170
 
 
171
    @property
 
172
    def text_size(self):
 
173
        return None
 
174
 
 
175
    def __repr__(self):
 
176
        return "%s(file_id=%r, name=%r, parent_id=%r, symlink_target=%r)" % (
 
177
            type(self).__name__, self.file_id, self.name, self.parent_id,
 
178
            self.symlink_target)
 
179
 
 
180
    def __eq__(self, other):
 
181
        return (self.kind == other.kind and
 
182
                self.file_id == other.file_id and
 
183
                self.name == other.name and
 
184
                self.parent_id == other.parent_id and
 
185
                self.symlink_target == other.symlink_target)
 
186
 
 
187
    def copy(self):
 
188
        return self.__class__(
 
189
                self.file_id, self.name, self.parent_id,
 
190
                self.symlink_target)
 
191
 
 
192
 
 
193
class GitTreeSubmodule(_mod_tree.TreeLink):
 
194
 
 
195
    __slots__ = ['file_id', 'name', 'parent_id', 'reference_revision']
 
196
 
 
197
    def __init__(self, file_id, name, parent_id, reference_revision=None):
 
198
        self.file_id = file_id
 
199
        self.name = name
 
200
        self.parent_id = parent_id
 
201
        self.reference_revision = reference_revision
 
202
 
 
203
    @property
 
204
    def kind(self):
 
205
        return 'tree-reference'
 
206
 
 
207
    def __repr__(self):
 
208
        return "%s(file_id=%r, name=%r, parent_id=%r, reference_revision=%r)" % (
 
209
            type(self).__name__, self.file_id, self.name, self.parent_id,
 
210
            self.reference_revision)
 
211
 
 
212
    def __eq__(self, other):
 
213
        return (self.kind == other.kind and
 
214
                self.file_id == other.file_id and
 
215
                self.name == other.name and
 
216
                self.parent_id == other.parent_id and
 
217
                self.reference_revision == other.reference_revision)
 
218
 
 
219
    def copy(self):
 
220
        return self.__class__(
 
221
                self.file_id, self.name, self.parent_id,
 
222
                self.reference_revision)
 
223
 
 
224
 
 
225
entry_factory = {
 
226
    'directory': GitTreeDirectory,
 
227
    'file': GitTreeFile,
 
228
    'symlink': GitTreeSymlink,
 
229
    'tree-reference': GitTreeSubmodule,
 
230
    }
 
231
 
 
232
 
 
233
def ensure_normalized_path(path):
 
234
    """Check whether path is normalized.
 
235
 
 
236
    :raises InvalidNormalization: When path is not normalized, and cannot be
 
237
        accessed on this platform by the normalized path.
 
238
    :return: The NFC normalised version of path.
 
239
    """
 
240
    norm_path, can_access = osutils.normalized_filename(path)
 
241
    if norm_path != path:
 
242
        if can_access:
 
243
            return norm_path
 
244
        else:
 
245
            raise errors.InvalidNormalization(path)
 
246
    return path
 
247
 
 
248
 
 
249
class GitRevisionTree(revisiontree.RevisionTree):
 
250
    """Revision tree implementation based on Git objects."""
 
251
 
 
252
    def __init__(self, repository, revision_id):
 
253
        self._revision_id = revision_id
 
254
        self._repository = repository
 
255
        self.store = repository._git.object_store
 
256
        if not isinstance(revision_id, bytes):
 
257
            raise TypeError(revision_id)
 
258
        self.commit_id, self.mapping = repository.lookup_bzr_revision_id(revision_id)
 
259
        if revision_id == NULL_REVISION:
 
260
            self.tree = None
 
261
            self.mapping = default_mapping
 
262
            self._fileid_map = GitFileIdMap(
 
263
                {},
 
264
                default_mapping)
 
265
        else:
 
266
            try:
 
267
                commit = self.store[self.commit_id]
 
268
            except KeyError:
 
269
                raise errors.NoSuchRevision(repository, revision_id)
 
270
            self.tree = commit.tree
 
271
            self._fileid_map = self.mapping.get_fileid_map(self.store.__getitem__, self.tree)
 
272
 
 
273
    def _get_nested_repository(self, path):
 
274
        nested_repo_transport = self._repository.user_transport.clone(path)
 
275
        nested_controldir = _mod_controldir.ControlDir.open_from_transport(nested_repo_transport)
 
276
        return nested_controldir.find_repository()
 
277
 
 
278
    def supports_rename_tracking(self):
 
279
        return False
 
280
 
 
281
    def get_file_revision(self, path, file_id=None):
 
282
        change_scanner = self._repository._file_change_scanner
 
283
        if self.commit_id == ZERO_SHA:
 
284
            return NULL_REVISION
 
285
        (unused_path, commit_id) = change_scanner.find_last_change_revision(
 
286
            path.encode('utf-8'), self.commit_id)
 
287
        return self._repository.lookup_foreign_revision_id(commit_id, self.mapping)
 
288
 
 
289
    def get_file_mtime(self, path, file_id=None):
 
290
        try:
 
291
            revid = self.get_file_revision(path, file_id)
 
292
        except KeyError:
 
293
            raise errors.NoSuchFile(path)
 
294
        try:
 
295
            rev = self._repository.get_revision(revid)
 
296
        except errors.NoSuchRevision:
 
297
            raise _mod_tree.FileTimestampUnavailable(path)
 
298
        return rev.timestamp
 
299
 
 
300
    def id2path(self, file_id):
 
301
        try:
 
302
            path = self._fileid_map.lookup_path(file_id)
 
303
        except ValueError:
 
304
            raise errors.NoSuchId(self, file_id)
 
305
        if self.is_versioned(path):
 
306
            return path
 
307
        raise errors.NoSuchId(self, file_id)
 
308
 
 
309
    def is_versioned(self, path):
 
310
        return self.has_filename(path)
 
311
 
 
312
    def path2id(self, path):
 
313
        if self.mapping.is_special_file(path):
 
314
            return None
 
315
        if not self.is_versioned(path):
 
316
            return None
 
317
        return self._fileid_map.lookup_file_id(osutils.safe_unicode(path))
 
318
 
 
319
    def all_file_ids(self):
 
320
        return {self.path2id(path) for path in self.all_versioned_paths()}
 
321
 
 
322
    def all_versioned_paths(self):
 
323
        ret = {u''}
 
324
        todo = [(self.store, b'', self.tree)]
 
325
        while todo:
 
326
            (store, path, tree_id) = todo.pop()
 
327
            if tree_id is None:
 
328
                continue
 
329
            tree = store[tree_id]
 
330
            for name, mode, hexsha in tree.items():
 
331
                subpath = posixpath.join(path, name)
 
332
                ret.add(subpath.decode('utf-8'))
 
333
                if stat.S_ISDIR(mode):
 
334
                    todo.append((store, subpath, hexsha))
 
335
        return ret
 
336
 
 
337
    def get_root_id(self):
 
338
        if self.tree is None:
 
339
            return None
 
340
        return self.path2id("")
 
341
 
 
342
    def has_or_had_id(self, file_id):
 
343
        try:
 
344
            path = self.id2path(file_id)
 
345
        except errors.NoSuchId:
 
346
            return False
 
347
        return True
 
348
 
 
349
    def has_id(self, file_id):
 
350
        try:
 
351
            path = self.id2path(file_id)
 
352
        except errors.NoSuchId:
 
353
            return False
 
354
        return self.has_filename(path)
 
355
 
 
356
    def _lookup_path(self, path):
 
357
        if self.tree is None:
 
358
            raise errors.NoSuchFile(path)
 
359
        try:
 
360
            (mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree,
 
361
                path.encode('utf-8'))
 
362
        except KeyError:
 
363
            raise errors.NoSuchFile(self, path)
 
364
        else:
 
365
            return (self.store, mode, hexsha)
 
366
 
 
367
    def is_executable(self, path, file_id=None):
 
368
        (store, mode, hexsha) = self._lookup_path(path)
 
369
        if mode is None:
 
370
            # the tree root is a directory
 
371
            return False
 
372
        return mode_is_executable(mode)
 
373
 
 
374
    def kind(self, path, file_id=None):
 
375
        (store, mode, hexsha) = self._lookup_path(path)
 
376
        if mode is None:
 
377
            # the tree root is a directory
 
378
            return "directory"
 
379
        return mode_kind(mode)
 
380
 
 
381
    def has_filename(self, path):
 
382
        try:
 
383
            self._lookup_path(path)
 
384
        except errors.NoSuchFile:
 
385
            return False
 
386
        else:
 
387
            return True
 
388
 
 
389
    def list_files(self, include_root=False, from_dir=None, recursive=True):
 
390
        if self.tree is None:
 
391
            return
 
392
        if from_dir is None:
 
393
            from_dir = u""
 
394
        (store, mode, hexsha) = self._lookup_path(from_dir)
 
395
        if mode is None: # Root
 
396
            root_ie = self._get_dir_ie(b"", None)
 
397
        else:
 
398
            parent_path = posixpath.dirname(from_dir)
 
399
            parent_id = self._fileid_map.lookup_file_id(parent_path)
 
400
            if mode_kind(mode) == 'directory':
 
401
                root_ie = self._get_dir_ie(from_dir.encode("utf-8"), parent_id)
 
402
            else:
 
403
                root_ie = self._get_file_ie(store, from_dir.encode("utf-8"),
 
404
                    posixpath.basename(from_dir), mode, hexsha)
 
405
        if include_root:
 
406
            yield (from_dir, "V", root_ie.kind, root_ie.file_id, root_ie)
 
407
        todo = []
 
408
        if root_ie.kind == 'directory':
 
409
            todo.append((store, from_dir.encode("utf-8"), b"", hexsha, root_ie.file_id))
 
410
        while todo:
 
411
            (store, path, relpath, hexsha, parent_id) = todo.pop()
 
412
            tree = store[hexsha]
 
413
            for name, mode, hexsha in tree.iteritems():
 
414
                if self.mapping.is_special_file(name):
 
415
                    continue
 
416
                child_path = posixpath.join(path, name)
 
417
                child_relpath = posixpath.join(relpath, name)
 
418
                if stat.S_ISDIR(mode):
 
419
                    ie = self._get_dir_ie(child_path, parent_id)
 
420
                    if recursive:
 
421
                        todo.append((store, child_path, child_relpath, hexsha, ie.file_id))
 
422
                else:
 
423
                    ie = self._get_file_ie(store, child_path, name, mode, hexsha, parent_id)
 
424
                yield child_relpath.decode('utf-8'), "V", ie.kind, ie.file_id, ie
 
425
 
 
426
    def _get_file_ie(self, store, path, name, mode, hexsha, parent_id):
 
427
        if not isinstance(path, bytes):
 
428
            raise TypeError(path)
 
429
        if not isinstance(name, bytes):
 
430
            raise TypeError(name)
 
431
        kind = mode_kind(mode)
 
432
        path = path.decode('utf-8')
 
433
        name = name.decode("utf-8")
 
434
        file_id = self._fileid_map.lookup_file_id(path)
 
435
        ie = entry_factory[kind](file_id, name, parent_id)
 
436
        if kind == 'symlink':
 
437
            ie.symlink_target = store[hexsha].data.decode('utf-8')
 
438
        elif kind == 'tree-reference':
 
439
            ie.reference_revision = self.mapping.revision_id_foreign_to_bzr(hexsha)
 
440
        else:
 
441
            data = store[hexsha].data
 
442
            ie.text_sha1 = osutils.sha_string(data)
 
443
            ie.text_size = len(data)
 
444
            ie.executable = mode_is_executable(mode)
 
445
        return ie
 
446
 
 
447
    def _get_dir_ie(self, path, parent_id):
 
448
        path = path.decode('utf-8')
 
449
        file_id = self._fileid_map.lookup_file_id(path)
 
450
        return GitTreeDirectory(file_id, posixpath.basename(path), parent_id)
 
451
 
 
452
    def iter_child_entries(self, path, file_id=None):
 
453
        (store, mode, tree_sha) = self._lookup_path(path)
 
454
 
 
455
        if mode is not None and not stat.S_ISDIR(mode):
 
456
            return
 
457
 
 
458
        encoded_path = path.encode('utf-8')
 
459
        file_id = self.path2id(path)
 
460
        tree = store[tree_sha]
 
461
        for name, mode, hexsha in tree.iteritems():
 
462
            if self.mapping.is_special_file(name):
 
463
                continue
 
464
            child_path = posixpath.join(encoded_path, name)
 
465
            if stat.S_ISDIR(mode):
 
466
                yield self._get_dir_ie(child_path, file_id)
 
467
            else:
 
468
                yield self._get_file_ie(store, child_path, name, mode, hexsha,
 
469
                                        file_id)
 
470
 
 
471
    def iter_entries_by_dir(self, specific_files=None, yield_parents=False):
 
472
        if self.tree is None:
 
473
            return
 
474
        if yield_parents:
 
475
            # TODO(jelmer): Support yield parents
 
476
            raise NotImplementedError
 
477
        if specific_files is not None:
 
478
            if specific_files in ([""], []):
 
479
                specific_files = None
 
480
            else:
 
481
                specific_files = set([p.encode('utf-8') for p in specific_files])
 
482
        todo = deque([(self.store, b"", self.tree, self.get_root_id())])
 
483
        if specific_files is None or u"" in specific_files:
 
484
            yield u"", self._get_dir_ie(b"", None)
 
485
        while todo:
 
486
            store, path, tree_sha, parent_id = todo.popleft()
 
487
            tree = store[tree_sha]
 
488
            extradirs = []
 
489
            for name, mode, hexsha in tree.iteritems():
 
490
                if self.mapping.is_special_file(name):
 
491
                    continue
 
492
                child_path = posixpath.join(path, name)
 
493
                child_path_decoded = child_path.decode('utf-8')
 
494
                if stat.S_ISDIR(mode):
 
495
                    if (specific_files is None or
 
496
                        any(filter(lambda p: p.startswith(child_path), specific_files))):
 
497
                        extradirs.append(
 
498
                            (store, child_path, hexsha, self.path2id(child_path_decoded)))
 
499
                if specific_files is None or child_path in specific_files:
 
500
                    if stat.S_ISDIR(mode):
 
501
                        yield (child_path_decoded,
 
502
                               self._get_dir_ie(child_path, parent_id))
 
503
                    else:
 
504
                        yield (child_path_decoded,
 
505
                               self._get_file_ie(store, child_path, name, mode,
 
506
                                   hexsha, parent_id))
 
507
            todo.extendleft(reversed(extradirs))
 
508
 
 
509
    def iter_references(self):
 
510
        if self.supports_tree_reference():
 
511
            for path, entry in self.iter_entries_by_dir():
 
512
                if entry.kind == 'tree-reference':
 
513
                    yield path, self.mapping.generate_file_id(b'')
 
514
 
 
515
    def get_revision_id(self):
 
516
        """See RevisionTree.get_revision_id."""
 
517
        return self._revision_id
 
518
 
 
519
    def get_file_sha1(self, path, file_id=None, stat_value=None):
 
520
        if self.tree is None:
 
521
            raise errors.NoSuchFile(path)
 
522
        return osutils.sha_string(self.get_file_text(path, file_id))
 
523
 
 
524
    def get_file_verifier(self, path, file_id=None, stat_value=None):
 
525
        (store, mode, hexsha) = self._lookup_path(path)
 
526
        return ("GIT", hexsha)
 
527
 
 
528
    def get_file_size(self, path, file_id=None):
 
529
        (store, mode, hexsha) = self._lookup_path(path)
 
530
        if stat.S_ISREG(mode):
 
531
            return len(store[hexsha].data)
 
532
        return None
 
533
 
 
534
    def get_file_text(self, path, file_id=None):
 
535
        """See RevisionTree.get_file_text."""
 
536
        (store, mode, hexsha) = self._lookup_path(path)
 
537
        if stat.S_ISREG(mode):
 
538
            return store[hexsha].data
 
539
        else:
 
540
            return b""
 
541
 
 
542
    def get_symlink_target(self, path, file_id=None):
 
543
        """See RevisionTree.get_symlink_target."""
 
544
        (store, mode, hexsha) = self._lookup_path(path)
 
545
        if stat.S_ISLNK(mode):
 
546
            return store[hexsha].data.decode('utf-8')
 
547
        else:
 
548
            return None
 
549
 
 
550
    def get_reference_revision(self, path, file_id=None):
 
551
        """See RevisionTree.get_symlink_target."""
 
552
        (store, mode, hexsha) = self._lookup_path(path)
 
553
        if S_ISGITLINK(mode):
 
554
            nested_repo = self._get_nested_repository(path)
 
555
            return nested_repo.lookup_foreign_revision_id(hexsha)
 
556
        else:
 
557
            return None
 
558
 
 
559
    def _comparison_data(self, entry, path):
 
560
        if entry is None:
 
561
            return None, False, None
 
562
        return entry.kind, entry.executable, None
 
563
 
 
564
    def path_content_summary(self, path):
 
565
        """See Tree.path_content_summary."""
 
566
        try:
 
567
            (store, mode, hexsha) = self._lookup_path(path)
 
568
        except errors.NoSuchFile:
 
569
            return ('missing', None, None, None)
 
570
        kind = mode_kind(mode)
 
571
        if kind == 'file':
 
572
            executable = mode_is_executable(mode)
 
573
            contents = store[hexsha].data
 
574
            return (kind, len(contents), executable, osutils.sha_string(contents))
 
575
        elif kind == 'symlink':
 
576
            return (kind, None, None, store[hexsha].data.decode('utf-8'))
 
577
        elif kind == 'tree-reference':
 
578
            nested_repo = self._get_nested_repository(path)
 
579
            return (kind, None, None,
 
580
                    nested_repo.lookup_foreign_revision_id(hexsha))
 
581
        else:
 
582
            return (kind, None, None, None)
 
583
 
 
584
    def find_related_paths_across_trees(self, paths, trees=[],
 
585
            require_versioned=True):
 
586
        if paths is None:
 
587
            return None
 
588
        if require_versioned:
 
589
            trees = [self] + (trees if trees is not None else [])
 
590
            unversioned = set()
 
591
            for p in paths:
 
592
                for t in trees:
 
593
                    if t.is_versioned(p):
 
594
                        break
 
595
                else:
 
596
                    unversioned.add(p)
 
597
            if unversioned:
 
598
                raise errors.PathsNotVersionedError(unversioned)
 
599
        return filter(self.is_versioned, paths)
 
600
 
 
601
    def _iter_tree_contents(self, include_trees=False):
 
602
        if self.tree is None:
 
603
            return iter([])
 
604
        return self.store.iter_tree_contents(
 
605
                self.tree, include_trees=include_trees)
 
606
 
 
607
    def annotate_iter(self, path, file_id=None,
 
608
                      default_revision=CURRENT_REVISION):
 
609
        """Return an iterator of revision_id, line tuples.
 
610
 
 
611
        For working trees (and mutable trees in general), the special
 
612
        revision_id 'current:' will be used for lines that are new in this
 
613
        tree, e.g. uncommitted changes.
 
614
        :param file_id: The file to produce an annotated version from
 
615
        :param default_revision: For lines that don't match a basis, mark them
 
616
            with this revision id. Not all implementations will make use of
 
617
            this value.
 
618
        """
 
619
        with self.lock_read():
 
620
            # Now we have the parents of this content
 
621
            from breezy.annotate import Annotator
 
622
            from .annotate import AnnotateProvider
 
623
            annotator = Annotator(AnnotateProvider(
 
624
                self._repository._file_change_scanner))
 
625
            this_key = (path, self.get_file_revision(path))
 
626
            annotations = [(key[-1], line)
 
627
                           for key, line in annotator.annotate_flat(this_key)]
 
628
            return annotations
 
629
 
 
630
    def _get_rules_searcher(self, default_searcher):
 
631
        return default_searcher
 
632
 
 
633
    def walkdirs(self, prefix=u""):
 
634
        (store, mode, hexsha) = self._lookup_path(prefix)
 
635
        todo = deque([(store, prefix.encode('utf-8'), hexsha, self.path2id(prefix))])
 
636
        while todo:
 
637
            store, path, tree_sha, parent_id = todo.popleft()
 
638
            path_decoded = path.decode('utf-8')
 
639
            tree = store[tree_sha]
 
640
            children = []
 
641
            for name, mode, hexsha in tree.iteritems():
 
642
                if self.mapping.is_special_file(name):
 
643
                    continue
 
644
                child_path = posixpath.join(path, name)
 
645
                file_id = self.path2id(child_path.decode('utf-8'))
 
646
                if stat.S_ISDIR(mode):
 
647
                    todo.append((store, child_path, hexsha, file_id))
 
648
                children.append(
 
649
                    (child_path.decode('utf-8'), name.decode('utf-8'),
 
650
                        mode_kind(mode), None,
 
651
                        file_id, mode_kind(mode)))
 
652
            yield (path_decoded, parent_id), children
 
653
 
 
654
 
 
655
def tree_delta_from_git_changes(changes, mapping,
 
656
        fileid_maps, specific_files=None,
 
657
        require_versioned=False, include_root=False,
 
658
        target_extras=None):
 
659
    """Create a TreeDelta from two git trees.
 
660
 
 
661
    source and target are iterators over tuples with:
 
662
        (filename, sha, mode)
 
663
    """
 
664
    (old_fileid_map, new_fileid_map) = fileid_maps
 
665
    if target_extras is None:
 
666
        target_extras = set()
 
667
    ret = delta.TreeDelta()
 
668
    for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
 
669
        if newpath == b'' and not include_root:
 
670
            continue
 
671
        if oldpath is None:
 
672
            oldpath_encoded = None
 
673
        else:
 
674
            oldpath_decoded = oldpath.decode('utf-8')
 
675
        if newpath is None:
 
676
            newpath_decoded = None
 
677
        else:
 
678
            newpath_decoded = newpath.decode('utf-8')
 
679
        if not (specific_files is None or
 
680
                (oldpath is not None and osutils.is_inside_or_parent_of_any(specific_files, oldpath_decoded)) or
 
681
                (newpath is not None and osutils.is_inside_or_parent_of_any(specific_files, newpath_decoded))):
 
682
            continue
 
683
        if mapping.is_special_file(oldpath):
 
684
            oldpath = None
 
685
        if mapping.is_special_file(newpath):
 
686
            newpath = None
 
687
        if oldpath is None and newpath is None:
 
688
            continue
 
689
        if oldpath is None:
 
690
            if newpath in target_extras:
 
691
                ret.unversioned.append(
 
692
                    (osutils.normalized_filename(newpath)[0], None, mode_kind(newmode)))
 
693
            else:
 
694
                file_id = new_fileid_map.lookup_file_id(newpath_decoded)
 
695
                ret.added.append((newpath_decoded, file_id, mode_kind(newmode)))
 
696
        elif newpath is None or newmode == 0:
 
697
            file_id = old_fileid_map.lookup_file_id(oldpath_decoded)
 
698
            ret.removed.append((oldpath_decoded, file_id, mode_kind(oldmode)))
 
699
        elif oldpath != newpath:
 
700
            file_id = old_fileid_map.lookup_file_id(oldpath_decoded)
 
701
            ret.renamed.append(
 
702
                (oldpath_decoded, newpath.decode('utf-8'), file_id,
 
703
                mode_kind(newmode), (oldsha != newsha),
 
704
                (oldmode != newmode)))
 
705
        elif mode_kind(oldmode) != mode_kind(newmode):
 
706
            file_id = new_fileid_map.lookup_file_id(newpath_decoded)
 
707
            ret.kind_changed.append(
 
708
                (newpath_decoded, file_id, mode_kind(oldmode),
 
709
                mode_kind(newmode)))
 
710
        elif oldsha != newsha or oldmode != newmode:
 
711
            if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
 
712
                continue
 
713
            file_id = new_fileid_map.lookup_file_id(newpath_decoded)
 
714
            ret.modified.append(
 
715
                (newpath_decoded, file_id, mode_kind(newmode),
 
716
                (oldsha != newsha), (oldmode != newmode)))
 
717
        else:
 
718
            file_id = new_fileid_map.lookup_file_id(newpath_decoded)
 
719
            ret.unchanged.append((newpath_decoded, file_id, mode_kind(newmode)))
 
720
 
 
721
    return ret
 
722
 
 
723
 
 
724
def changes_from_git_changes(changes, mapping, specific_files=None, include_unchanged=False,
 
725
                             target_extras=None):
 
726
    """Create a iter_changes-like generator from a git stream.
 
727
 
 
728
    source and target are iterators over tuples with:
 
729
        (filename, sha, mode)
 
730
    """
 
731
    if target_extras is None:
 
732
        target_extras = set()
 
733
    for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
 
734
        if oldpath is not None:
 
735
            oldpath_decoded = oldpath.decode('utf-8')
 
736
        else:
 
737
            oldpath_decoded = None
 
738
        if newpath is not None:
 
739
            newpath_decoded = newpath.decode('utf-8')
 
740
        else:
 
741
            newpath_decoded = None
 
742
        if not (specific_files is None or
 
743
                (oldpath_decoded is not None and osutils.is_inside_or_parent_of_any(specific_files, oldpath_decoded)) or
 
744
                (newpath_decoded is not None and osutils.is_inside_or_parent_of_any(specific_files, newpath_decoded))):
 
745
            continue
 
746
        if oldpath is not None and mapping.is_special_file(oldpath):
 
747
            continue
 
748
        if newpath is not None and mapping.is_special_file(newpath):
 
749
            continue
 
750
        if oldpath_decoded is None:
 
751
            fileid = mapping.generate_file_id(newpath_decoded)
 
752
            oldexe = None
 
753
            oldkind = None
 
754
            oldname = None
 
755
            oldparent = None
 
756
            oldversioned = False
 
757
        else:
 
758
            oldversioned = True
 
759
            if oldmode:
 
760
                oldexe = mode_is_executable(oldmode)
 
761
                oldkind = mode_kind(oldmode)
 
762
            else:
 
763
                oldexe = False
 
764
                oldkind = None
 
765
            if oldpath_decoded == u'':
 
766
                oldparent = None
 
767
                oldname = u''
 
768
            else:
 
769
                (oldparentpath, oldname) = osutils.split(oldpath_decoded)
 
770
                oldparent = mapping.generate_file_id(oldparentpath)
 
771
            fileid = mapping.generate_file_id(oldpath_decoded)
 
772
        if newpath_decoded is None:
 
773
            newexe = None
 
774
            newkind = None
 
775
            newname = None
 
776
            newparent = None
 
777
            newversioned = False
 
778
        else:
 
779
            newversioned = (newpath_decoded not in target_extras)
 
780
            if newmode:
 
781
                newexe = mode_is_executable(newmode)
 
782
                newkind = mode_kind(newmode)
 
783
            else:
 
784
                newexe = False
 
785
                newkind = None
 
786
            if newpath_decoded == u'':
 
787
                newparent = None
 
788
                newname = u''
 
789
            else:
 
790
                newparentpath, newname = osutils.split(newpath_decoded)
 
791
                newparent = mapping.generate_file_id(newparentpath)
 
792
        if (not include_unchanged and
 
793
            oldkind == 'directory' and newkind == 'directory' and
 
794
            oldpath_decoded == newpath_decoded):
 
795
            continue
 
796
        yield (fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
 
797
             (oldversioned, newversioned),
 
798
             (oldparent, newparent), (oldname, newname),
 
799
             (oldkind, newkind), (oldexe, newexe))
 
800
 
 
801
 
 
802
class InterGitTrees(_mod_tree.InterTree):
 
803
    """InterTree that works between two git trees."""
 
804
 
 
805
    _matching_from_tree_format = None
 
806
    _matching_to_tree_format = None
 
807
    _test_mutable_trees_to_test_trees = None
 
808
 
 
809
    @classmethod
 
810
    def is_compatible(cls, source, target):
 
811
        return (isinstance(source, GitRevisionTree) and
 
812
                isinstance(target, GitRevisionTree))
 
813
 
 
814
    def compare(self, want_unchanged=False, specific_files=None,
 
815
                extra_trees=None, require_versioned=False, include_root=False,
 
816
                want_unversioned=False):
 
817
        with self.lock_read():
 
818
            changes, target_extras = self._iter_git_changes(
 
819
                    want_unchanged=want_unchanged,
 
820
                    require_versioned=require_versioned,
 
821
                    specific_files=specific_files,
 
822
                    extra_trees=extra_trees,
 
823
                    want_unversioned=want_unversioned)
 
824
            source_fileid_map = self.source._fileid_map
 
825
            target_fileid_map = self.target._fileid_map
 
826
            return tree_delta_from_git_changes(changes, self.target.mapping,
 
827
                (source_fileid_map, target_fileid_map),
 
828
                specific_files=specific_files, include_root=include_root,
 
829
                target_extras=target_extras)
 
830
 
 
831
    def iter_changes(self, include_unchanged=False, specific_files=None,
 
832
                     pb=None, extra_trees=[], require_versioned=True,
 
833
                     want_unversioned=False):
 
834
        with self.lock_read():
 
835
            changes, target_extras = self._iter_git_changes(
 
836
                    want_unchanged=include_unchanged,
 
837
                    require_versioned=require_versioned,
 
838
                    specific_files=specific_files,
 
839
                    extra_trees=extra_trees,
 
840
                    want_unversioned=want_unversioned)
 
841
            return changes_from_git_changes(
 
842
                    changes, self.target.mapping,
 
843
                    specific_files=specific_files,
 
844
                    include_unchanged=include_unchanged,
 
845
                    target_extras=target_extras)
 
846
 
 
847
    def _iter_git_changes(self, want_unchanged=False, specific_files=None,
 
848
            require_versioned=False, extra_trees=None,
 
849
            want_unversioned=False):
 
850
        raise NotImplementedError(self._iter_git_changes)
 
851
 
 
852
 
 
853
class InterGitRevisionTrees(InterGitTrees):
 
854
    """InterTree that works between two git revision trees."""
 
855
 
 
856
    _matching_from_tree_format = None
 
857
    _matching_to_tree_format = None
 
858
    _test_mutable_trees_to_test_trees = None
 
859
 
 
860
    @classmethod
 
861
    def is_compatible(cls, source, target):
 
862
        return (isinstance(source, GitRevisionTree) and
 
863
                isinstance(target, GitRevisionTree))
 
864
 
 
865
    def _iter_git_changes(self, want_unchanged=False, specific_files=None,
 
866
            require_versioned=True, extra_trees=None,
 
867
            want_unversioned=False):
 
868
        trees = [self.source]
 
869
        if extra_trees is not None:
 
870
            trees.extend(extra_trees)
 
871
        if specific_files is not None:
 
872
            specific_files = self.target.find_related_paths_across_trees(
 
873
                    specific_files, trees,
 
874
                    require_versioned=require_versioned)
 
875
 
 
876
        if self.source._repository._git.object_store != self.target._repository._git.object_store:
 
877
            store = OverlayObjectStore([self.source._repository._git.object_store,
 
878
                                        self.target._repository._git.object_store])
 
879
        else:
 
880
            store = self.source._repository._git.object_store
 
881
        return self.source._repository._git.object_store.tree_changes(
 
882
            self.source.tree, self.target.tree, want_unchanged=want_unchanged,
 
883
            include_trees=True, change_type_same=True), set()
 
884
 
 
885
 
 
886
_mod_tree.InterTree.register_optimiser(InterGitRevisionTrees)
 
887
 
 
888
 
 
889
class MutableGitIndexTree(mutabletree.MutableTree):
 
890
 
 
891
    def __init__(self):
 
892
        self._lock_mode = None
 
893
        self._lock_count = 0
 
894
        self._versioned_dirs = None
 
895
        self._index_dirty = False
 
896
 
 
897
    def is_versioned(self, path):
 
898
        with self.lock_read():
 
899
            path = path.rstrip('/').encode('utf-8')
 
900
            (index, subpath) = self._lookup_index(path)
 
901
            return (subpath in index or self._has_dir(path))
 
902
 
 
903
    def _has_dir(self, path):
 
904
        if not isinstance(path, bytes):
 
905
            raise TypeError(path)
 
906
        if path == b"":
 
907
            return True
 
908
        if self._versioned_dirs is None:
 
909
            self._load_dirs()
 
910
        return path in self._versioned_dirs
 
911
 
 
912
    def _load_dirs(self):
 
913
        if self._lock_mode is None:
 
914
            raise errors.ObjectNotLocked(self)
 
915
        self._versioned_dirs = set()
 
916
        # TODO(jelmer): Browse over all indexes
 
917
        for p, i in self._recurse_index_entries():
 
918
            self._ensure_versioned_dir(posixpath.dirname(p))
 
919
 
 
920
    def _ensure_versioned_dir(self, dirname):
 
921
        if not isinstance(dirname, bytes):
 
922
            raise TypeError(dirname)
 
923
        if dirname in self._versioned_dirs:
 
924
            return
 
925
        if dirname != b"":
 
926
            self._ensure_versioned_dir(posixpath.dirname(dirname))
 
927
        self._versioned_dirs.add(dirname)
 
928
 
 
929
    def path2id(self, path):
 
930
        with self.lock_read():
 
931
            path = path.rstrip('/')
 
932
            if self.is_versioned(path.rstrip('/')):
 
933
                return self._fileid_map.lookup_file_id(osutils.safe_unicode(path))
 
934
            return None
 
935
 
 
936
    def has_id(self, file_id):
 
937
        try:
 
938
            self.id2path(file_id)
 
939
        except errors.NoSuchId:
 
940
            return False
 
941
        else:
 
942
            return True
 
943
 
 
944
    def id2path(self, file_id):
 
945
        if file_id is None:
 
946
            return ''
 
947
        if type(file_id) is not bytes:
 
948
            raise TypeError(file_id)
 
949
        with self.lock_read():
 
950
            try:
 
951
                path = self._fileid_map.lookup_path(file_id)
 
952
            except ValueError:
 
953
                raise errors.NoSuchId(self, file_id)
 
954
            if self.is_versioned(path):
 
955
                return path
 
956
            raise errors.NoSuchId(self, file_id)
 
957
 
 
958
    def _set_root_id(self, file_id):
 
959
        self._fileid_map.set_file_id("", file_id)
 
960
 
 
961
    def get_root_id(self):
 
962
        return self.path2id(u"")
 
963
 
 
964
    def _add(self, files, ids, kinds):
 
965
        for (path, file_id, kind) in zip(files, ids, kinds):
 
966
            if file_id is not None:
 
967
                raise workingtree.SettingFileIdUnsupported()
 
968
            path, can_access = osutils.normalized_filename(path)
 
969
            if not can_access:
 
970
                raise errors.InvalidNormalization(path)
 
971
            self._index_add_entry(path, kind)
 
972
 
 
973
    def _read_submodule_head(self, path):
 
974
        raise NotImplementedError(self._read_submodule_head)
 
975
 
 
976
    def _lookup_index(self, encoded_path):
 
977
        if not isinstance(encoded_path, bytes):
 
978
            raise TypeError(encoded_path)
 
979
        # TODO(jelmer): Look in other indexes
 
980
        return self.index, encoded_path
 
981
 
 
982
    def _index_del_entry(self, index, path):
 
983
        del index[path]
 
984
        # TODO(jelmer): Keep track of dirty per index
 
985
        self._index_dirty = True
 
986
 
 
987
    def _index_add_entry(self, path, kind, flags=0, reference_revision=None):
 
988
        if kind == "directory":
 
989
            # Git indexes don't contain directories
 
990
            return
 
991
        if kind == "file":
 
992
            blob = Blob()
 
993
            try:
 
994
                file, stat_val = self.get_file_with_stat(path)
 
995
            except (errors.NoSuchFile, IOError):
 
996
                # TODO: Rather than come up with something here, use the old index
 
997
                file = BytesIO()
 
998
                stat_val = os.stat_result(
 
999
                    (stat.S_IFREG | 0o644, 0, 0, 0, 0, 0, 0, 0, 0, 0))
 
1000
            with file:
 
1001
                blob.set_raw_string(file.read())
 
1002
            # Add object to the repository if it didn't exist yet
 
1003
            if not blob.id in self.store:
 
1004
                self.store.add_object(blob)
 
1005
            hexsha = blob.id
 
1006
        elif kind == "symlink":
 
1007
            blob = Blob()
 
1008
            try:
 
1009
                stat_val = self._lstat(path)
 
1010
            except EnvironmentError:
 
1011
                # TODO: Rather than come up with something here, use the
 
1012
                # old index
 
1013
                stat_val = os.stat_result(
 
1014
                    (stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
 
1015
            blob.set_raw_string(
 
1016
                self.get_symlink_target(path).encode("utf-8"))
 
1017
            # Add object to the repository if it didn't exist yet
 
1018
            if not blob.id in self.store:
 
1019
                self.store.add_object(blob)
 
1020
            hexsha = blob.id
 
1021
        elif kind == "tree-reference":
 
1022
            if reference_revision is not None:
 
1023
                hexsha = self.branch.lookup_bzr_revision_id(reference_revision)[0]
 
1024
            else:
 
1025
                hexsha = self._read_submodule_head(path)
 
1026
                if hexsha is None:
 
1027
                    raise errors.NoCommits(path)
 
1028
            try:
 
1029
                stat_val = self._lstat(path)
 
1030
            except EnvironmentError:
 
1031
                stat_val = os.stat_result(
 
1032
                    (S_IFGITLINK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
 
1033
            stat_val = os.stat_result((S_IFGITLINK, ) + stat_val[1:])
 
1034
        else:
 
1035
            raise AssertionError("unknown kind '%s'" % kind)
 
1036
        # Add an entry to the index or update the existing entry
 
1037
        ensure_normalized_path(path)
 
1038
        encoded_path = path.encode("utf-8")
 
1039
        if b'\r' in encoded_path or b'\n' in encoded_path:
 
1040
            # TODO(jelmer): Why do we need to do this?
 
1041
            trace.mutter('ignoring path with invalid newline in it: %r', path)
 
1042
            return
 
1043
        (index, index_path) = self._lookup_index(encoded_path)
 
1044
        index[index_path] = index_entry_from_stat(stat_val, hexsha, flags)
 
1045
        self._index_dirty = True
 
1046
        if self._versioned_dirs is not None:
 
1047
            self._ensure_versioned_dir(index_path)
 
1048
 
 
1049
    def _recurse_index_entries(self, index=None, basepath=b""):
 
1050
        # Iterate over all index entries
 
1051
        with self.lock_read():
 
1052
            if index is None:
 
1053
                index = self.index
 
1054
            for path, value in index.items():
 
1055
                yield (posixpath.join(basepath, path), value)
 
1056
                (ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
 
1057
                if S_ISGITLINK(mode):
 
1058
                    pass # TODO(jelmer): dive into submodule
 
1059
 
 
1060
 
 
1061
    def iter_entries_by_dir(self, specific_files=None, yield_parents=False):
 
1062
        if yield_parents:
 
1063
            raise NotImplementedError(self.iter_entries_by_dir)
 
1064
        with self.lock_read():
 
1065
            if specific_files is not None:
 
1066
                specific_files = set(specific_files)
 
1067
            else:
 
1068
                specific_files = None
 
1069
            root_ie = self._get_dir_ie(u"", None)
 
1070
            ret = {}
 
1071
            if specific_files is None or u"" in specific_files:
 
1072
                ret[(u"", u"")] = root_ie
 
1073
            dir_ids = {u"": root_ie.file_id}
 
1074
            for path, value in self._recurse_index_entries():
 
1075
                if self.mapping.is_special_file(path):
 
1076
                    continue
 
1077
                path = path.decode("utf-8")
 
1078
                if specific_files is not None and not path in specific_files:
 
1079
                    continue
 
1080
                (parent, name) = posixpath.split(path)
 
1081
                try:
 
1082
                    file_ie = self._get_file_ie(name, path, value, None)
 
1083
                except errors.NoSuchFile:
 
1084
                    continue
 
1085
                if yield_parents or specific_files is None:
 
1086
                    for (dir_path, dir_ie) in self._add_missing_parent_ids(parent,
 
1087
                            dir_ids):
 
1088
                        ret[(posixpath.dirname(dir_path), dir_path)] = dir_ie
 
1089
                file_ie.parent_id = self.path2id(parent)
 
1090
                ret[(posixpath.dirname(path), path)] = file_ie
 
1091
            return ((path, ie) for ((_, path), ie) in sorted(viewitems(ret)))
 
1092
 
 
1093
    def iter_references(self):
 
1094
        # TODO(jelmer): Implement a more efficient version of this
 
1095
        for path, entry in self.iter_entries_by_dir():
 
1096
            if entry.kind == 'tree-reference':
 
1097
                yield path, self.mapping.generate_file_id(b'')
 
1098
 
 
1099
    def _get_dir_ie(self, path, parent_id):
 
1100
        file_id = self.path2id(path)
 
1101
        return GitTreeDirectory(file_id,
 
1102
            posixpath.basename(path).strip("/"), parent_id)
 
1103
 
 
1104
    def _get_file_ie(self, name, path, value, parent_id):
 
1105
        if not isinstance(name, text_type):
 
1106
            raise TypeError(name)
 
1107
        if not isinstance(path, text_type):
 
1108
            raise TypeError(path)
 
1109
        if not isinstance(value, tuple) or len(value) != 10:
 
1110
            raise TypeError(value)
 
1111
        (ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
 
1112
        file_id = self.path2id(path)
 
1113
        if not isinstance(file_id, bytes):
 
1114
            raise TypeError(file_id)
 
1115
        kind = mode_kind(mode)
 
1116
        ie = entry_factory[kind](file_id, name, parent_id)
 
1117
        if kind == 'symlink':
 
1118
            ie.symlink_target = self.get_symlink_target(path, file_id)
 
1119
        elif kind == 'tree-reference':
 
1120
            ie.reference_revision = self.get_reference_revision(path, file_id)
 
1121
        else:
 
1122
            try:
 
1123
                data = self.get_file_text(path, file_id)
 
1124
            except errors.NoSuchFile:
 
1125
                data = None
 
1126
            except IOError as e:
 
1127
                if e.errno != errno.ENOENT:
 
1128
                    raise
 
1129
                data = None
 
1130
            if data is None:
 
1131
                data = self.branch.repository._git.object_store[sha].data
 
1132
            ie.text_sha1 = osutils.sha_string(data)
 
1133
            ie.text_size = len(data)
 
1134
            ie.executable = bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
 
1135
        return ie
 
1136
 
 
1137
    def _add_missing_parent_ids(self, path, dir_ids):
 
1138
        if path in dir_ids:
 
1139
            return []
 
1140
        parent = posixpath.dirname(path).strip("/")
 
1141
        ret = self._add_missing_parent_ids(parent, dir_ids)
 
1142
        parent_id = dir_ids[parent]
 
1143
        ie = self._get_dir_ie(path, parent_id)
 
1144
        dir_ids[path] = ie.file_id
 
1145
        ret.append((path, ie))
 
1146
        return ret
 
1147
 
 
1148
    def _comparison_data(self, entry, path):
 
1149
        if entry is None:
 
1150
            return None, False, None
 
1151
        return entry.kind, entry.executable, None
 
1152
 
 
1153
    def _unversion_path(self, path):
 
1154
        if self._lock_mode is None:
 
1155
            raise errors.ObjectNotLocked(self)
 
1156
        encoded_path = path.encode("utf-8")
 
1157
        count = 0
 
1158
        (index, subpath) = self._lookup_index(encoded_path)
 
1159
        try:
 
1160
            self._index_del_entry(index, encoded_path)
 
1161
        except KeyError:
 
1162
            # A directory, perhaps?
 
1163
            # TODO(jelmer): Deletes that involve submodules?
 
1164
            for p in list(index):
 
1165
                if p.startswith(subpath+b"/"):
 
1166
                    count += 1
 
1167
                    self._index_del_entry(index, p)
 
1168
        else:
 
1169
            count = 1
 
1170
        self._versioned_dirs = None
 
1171
        return count
 
1172
 
 
1173
    def unversion(self, paths, file_ids=None):
 
1174
        with self.lock_tree_write():
 
1175
            for path in paths:
 
1176
                if self._unversion_path(path) == 0:
 
1177
                    raise errors.NoSuchFile(path)
 
1178
            self._versioned_dirs = None
 
1179
            self.flush()
 
1180
 
 
1181
    def flush(self):
 
1182
        pass
 
1183
 
 
1184
    def update_basis_by_delta(self, revid, delta):
 
1185
        # TODO(jelmer): This shouldn't be called, it's inventory specific.
 
1186
        for (old_path, new_path, file_id, ie) in delta:
 
1187
            if old_path is not None:
 
1188
                (index, old_subpath) = self._lookup_index(old_path.encode('utf-8'))
 
1189
                if old_subpath in index:
 
1190
                    self._index_del_entry(index, old_subpath)
 
1191
                    self._versioned_dirs = None
 
1192
            if new_path is not None and ie.kind != 'directory':
 
1193
                self._index_add_entry(new_path, ie.kind)
 
1194
        self.flush()
 
1195
        self._set_merges_from_parent_ids([])
 
1196
 
 
1197
    def move(self, from_paths, to_dir=None, after=None):
 
1198
        rename_tuples = []
 
1199
        with self.lock_tree_write():
 
1200
            to_abs = self.abspath(to_dir)
 
1201
            if not os.path.isdir(to_abs):
 
1202
                raise errors.BzrMoveFailedError('', to_dir,
 
1203
                    errors.NotADirectory(to_abs))
 
1204
 
 
1205
            for from_rel in from_paths:
 
1206
                from_tail = os.path.split(from_rel)[-1]
 
1207
                to_rel = os.path.join(to_dir, from_tail)
 
1208
                self.rename_one(from_rel, to_rel, after=after)
 
1209
                rename_tuples.append((from_rel, to_rel))
 
1210
            self.flush()
 
1211
            return rename_tuples
 
1212
 
 
1213
    def rename_one(self, from_rel, to_rel, after=None):
 
1214
        from_path = from_rel.encode("utf-8")
 
1215
        to_rel, can_access = osutils.normalized_filename(to_rel)
 
1216
        if not can_access:
 
1217
            raise errors.InvalidNormalization(to_rel)
 
1218
        to_path = to_rel.encode("utf-8")
 
1219
        with self.lock_tree_write():
 
1220
            if not after:
 
1221
                # Perhaps it's already moved?
 
1222
                after = (
 
1223
                    not self.has_filename(from_rel) and
 
1224
                    self.has_filename(to_rel) and
 
1225
                    not self.is_versioned(to_rel))
 
1226
            if after:
 
1227
                if not self.has_filename(to_rel):
 
1228
                    raise errors.BzrMoveFailedError(from_rel, to_rel,
 
1229
                        errors.NoSuchFile(to_rel))
 
1230
                if self.basis_tree().is_versioned(to_rel):
 
1231
                    raise errors.BzrMoveFailedError(from_rel, to_rel,
 
1232
                        errors.AlreadyVersionedError(to_rel))
 
1233
 
 
1234
                kind = self.kind(to_rel)
 
1235
            else:
 
1236
                try:
 
1237
                    to_kind = self.kind(to_rel)
 
1238
                except errors.NoSuchFile:
 
1239
                    exc_type = errors.BzrRenameFailedError
 
1240
                    to_kind = None
 
1241
                else:
 
1242
                    exc_type = errors.BzrMoveFailedError
 
1243
                if self.is_versioned(to_rel):
 
1244
                    raise exc_type(from_rel, to_rel,
 
1245
                        errors.AlreadyVersionedError(to_rel))
 
1246
                if not self.has_filename(from_rel):
 
1247
                    raise errors.BzrMoveFailedError(from_rel, to_rel,
 
1248
                        errors.NoSuchFile(from_rel))
 
1249
                kind = self.kind(from_rel)
 
1250
                if not self.is_versioned(from_rel) and kind != 'directory':
 
1251
                    raise exc_type(from_rel, to_rel,
 
1252
                        errors.NotVersionedError(from_rel))
 
1253
                if self.has_filename(to_rel):
 
1254
                    raise errors.RenameFailedFilesExist(
 
1255
                        from_rel, to_rel, errors.FileExists(to_rel))
 
1256
 
 
1257
                kind = self.kind(from_rel)
 
1258
 
 
1259
            if not after and kind != 'directory':
 
1260
                (index, from_subpath) = self._lookup_index(from_path)
 
1261
                if from_subpath not in index:
 
1262
                    # It's not a file
 
1263
                    raise errors.BzrMoveFailedError(from_rel, to_rel,
 
1264
                        errors.NotVersionedError(path=from_rel))
 
1265
 
 
1266
            if not after:
 
1267
                try:
 
1268
                    self._rename_one(from_rel, to_rel)
 
1269
                except OSError as e:
 
1270
                    if e.errno == errno.ENOENT:
 
1271
                        raise errors.BzrMoveFailedError(from_rel, to_rel,
 
1272
                            errors.NoSuchFile(to_rel))
 
1273
                    raise
 
1274
            if kind != 'directory':
 
1275
                (index, from_index_path) = self._lookup_index(from_path)
 
1276
                try:
 
1277
                    self._index_del_entry(index, from_path)
 
1278
                except KeyError:
 
1279
                    pass
 
1280
                self._index_add_entry(to_rel, kind)
 
1281
            else:
 
1282
                todo = [(p, i) for (p, i) in self._recurse_index_entries() if p.startswith(from_path+b'/')]
 
1283
                for child_path, child_value in todo:
 
1284
                    (child_to_index, child_to_index_path) = self._lookup_index(
 
1285
                            posixpath.join(to_path, posixpath.relpath(child_path, from_path)))
 
1286
                    child_to_index[child_to_index_path] = child_value
 
1287
                    # TODO(jelmer): Mark individual index as dirty
 
1288
                    self._index_dirty = True
 
1289
                    (child_from_index, child_from_index_path) = self._lookup_index(child_path)
 
1290
                    self._index_del_entry(child_from_index, child_from_index_path)
 
1291
 
 
1292
            self._versioned_dirs = None
 
1293
            self.flush()
 
1294
 
 
1295
    def find_related_paths_across_trees(self, paths, trees=[],
 
1296
            require_versioned=True):
 
1297
        if paths is None:
 
1298
            return None
 
1299
 
 
1300
        if require_versioned:
 
1301
            trees = [self] + (trees if trees is not None else [])
 
1302
            unversioned = set()
 
1303
            for p in paths:
 
1304
                for t in trees:
 
1305
                    if t.is_versioned(p):
 
1306
                        break
 
1307
                else:
 
1308
                    unversioned.add(p)
 
1309
            if unversioned:
 
1310
                raise errors.PathsNotVersionedError(unversioned)
 
1311
 
 
1312
        return filter(self.is_versioned, paths)
 
1313
 
 
1314
    def path_content_summary(self, path):
 
1315
        """See Tree.path_content_summary."""
 
1316
        try:
 
1317
            stat_result = self._lstat(path)
 
1318
        except OSError as e:
 
1319
            if getattr(e, 'errno', None) == errno.ENOENT:
 
1320
                # no file.
 
1321
                return ('missing', None, None, None)
 
1322
            # propagate other errors
 
1323
            raise
 
1324
        kind = mode_kind(stat_result.st_mode)
 
1325
        if kind == 'file':
 
1326
            return self._file_content_summary(path, stat_result)
 
1327
        elif kind == 'directory':
 
1328
            # perhaps it looks like a plain directory, but it's really a
 
1329
            # reference.
 
1330
            if self._directory_is_tree_reference(path):
 
1331
                kind = 'tree-reference'
 
1332
            return kind, None, None, None
 
1333
        elif kind == 'symlink':
 
1334
            target = osutils.readlink(self.abspath(path))
 
1335
            return ('symlink', None, None, target)
 
1336
        else:
 
1337
            return (kind, None, None, None)
 
1338
 
 
1339
    def kind(self, relpath, file_id=None):
 
1340
        kind = osutils.file_kind(self.abspath(relpath))
 
1341
        if kind == 'directory':
 
1342
            (index, index_path) = self._lookup_index(relpath.encode('utf-8'))
 
1343
            if index is None:
 
1344
                return kind
 
1345
            try:
 
1346
                mode = index[index_path].mode
 
1347
            except KeyError:
 
1348
                return kind
 
1349
            else:
 
1350
                if S_ISGITLINK(mode):
 
1351
                    return 'tree-reference'
 
1352
                return 'directory'
 
1353
        else:
 
1354
            return kind
 
1355
 
 
1356
    def _live_entry(self, relpath):
 
1357
        raise NotImplementedError(self._live_entry)
 
1358
 
 
1359
 
 
1360
class InterIndexGitTree(InterGitTrees):
 
1361
    """InterTree that works between a Git revision tree and an index."""
 
1362
 
 
1363
    def __init__(self, source, target):
 
1364
        super(InterIndexGitTree, self).__init__(source, target)
 
1365
        self._index = target.index
 
1366
 
 
1367
    @classmethod
 
1368
    def is_compatible(cls, source, target):
 
1369
        return (isinstance(source, GitRevisionTree) and
 
1370
                isinstance(target, MutableGitIndexTree))
 
1371
 
 
1372
    def _iter_git_changes(self, want_unchanged=False, specific_files=None,
 
1373
            require_versioned=False, extra_trees=None,
 
1374
            want_unversioned=False):
 
1375
        trees = [self.source]
 
1376
        if extra_trees is not None:
 
1377
            trees.extend(extra_trees)
 
1378
        if specific_files is not None:
 
1379
            specific_files = self.target.find_related_paths_across_trees(
 
1380
                    specific_files, trees,
 
1381
                    require_versioned=require_versioned)
 
1382
        # TODO(jelmer): Restrict to specific_files, for performance reasons.
 
1383
        with self.lock_read():
 
1384
            return changes_between_git_tree_and_working_copy(
 
1385
                self.source.store, self.source.tree,
 
1386
                self.target, want_unchanged=want_unchanged,
 
1387
                want_unversioned=want_unversioned)
 
1388
 
 
1389
 
 
1390
_mod_tree.InterTree.register_optimiser(InterIndexGitTree)
 
1391
 
 
1392
 
 
1393
def changes_between_git_tree_and_working_copy(store, from_tree_sha, target,
 
1394
        want_unchanged=False, want_unversioned=False):
 
1395
    """Determine the changes between a git tree and a working tree with index.
 
1396
 
 
1397
    """
 
1398
    extras = set()
 
1399
    blobs = {}
 
1400
    # Report dirified directories to commit_tree first, so that they can be
 
1401
    # replaced with non-empty directories if they have contents.
 
1402
    dirified = []
 
1403
    for path, index_entry in target._recurse_index_entries():
 
1404
        try:
 
1405
            live_entry = target._live_entry(path)
 
1406
        except EnvironmentError as e:
 
1407
            if e.errno == errno.ENOENT:
 
1408
                # Entry was removed; keep it listed, but mark it as gone.
 
1409
                blobs[path] = (ZERO_SHA, 0)
 
1410
            elif e.errno == errno.EISDIR:
 
1411
                # Entry was turned into a directory
 
1412
                dirified.append((path, Tree().id, stat.S_IFDIR))
 
1413
                store.add_object(Tree())
 
1414
            else:
 
1415
                raise
 
1416
        else:
 
1417
            blobs[path] = (live_entry.sha, cleanup_mode(live_entry.mode))
 
1418
    if want_unversioned:
 
1419
        for e in target.extras():
 
1420
            st = target._lstat(e)
 
1421
            try:
 
1422
                np, accessible = osutils.normalized_filename(e)
 
1423
            except UnicodeDecodeError:
 
1424
                raise errors.BadFilenameEncoding(
 
1425
                    e, osutils._fs_enc)
 
1426
            if stat.S_ISDIR(st.st_mode):
 
1427
                blob = Tree()
 
1428
            else:
 
1429
                blob = blob_from_path_and_stat(target.abspath(e).encode(osutils._fs_enc), st)
 
1430
            store.add_object(blob)
 
1431
            np = np.encode('utf-8')
 
1432
            blobs[np] = (blob.id, cleanup_mode(st.st_mode))
 
1433
            extras.add(np)
 
1434
    to_tree_sha = commit_tree(store, dirified + [(p, s, m) for (p, (s, m)) in blobs.items()])
 
1435
    return store.tree_changes(
 
1436
        from_tree_sha, to_tree_sha, include_trees=True,
 
1437
        want_unchanged=want_unchanged, change_type_same=True), extras