/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: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

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