/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-11-16 18:26:22 UTC
  • mfrom: (7167.1.4 run-flake8)
  • Revision ID: breezy.the.bot@gmail.com-20181116182622-qw3gan3hz78a2imw
Add a flake8 test.

Merged from https://code.launchpad.net/~jelmer/brz/run-flake8/+merge/358902

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