/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-09-15 00:32:05 UTC
  • mfrom: (7096.3.9 per-tree-git-revtree)
  • Revision ID: breezy.the.bot@gmail.com-20180915003205-dmcqqujf6z368wgf
Run per_tree tests against GitRevisionTree.

Merged from https://code.launchpad.net/~jelmer/brz/per-tree-git-revtree/+merge/354530

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
from __future__ import absolute_import
21
21
 
 
22
from collections import deque
22
23
import errno
23
24
from io import BytesIO
24
25
import os
289
290
        try:
290
291
            revid = self.get_file_revision(path, file_id)
291
292
        except KeyError:
292
 
            raise _mod_tree.FileTimestampUnavailable(path)
 
293
            raise errors.NoSuchFile(path)
293
294
        try:
294
295
            rev = self._repository.get_revision(revid)
295
296
        except errors.NoSuchRevision:
311
312
    def path2id(self, path):
312
313
        if self.mapping.is_special_file(path):
313
314
            return None
 
315
        if not self.is_versioned(path):
 
316
            return None
314
317
        return self._fileid_map.lookup_file_id(osutils.safe_unicode(path))
315
318
 
316
319
    def all_file_ids(self):
317
 
        return set(self._fileid_map.all_file_ids())
 
320
        return {self.path2id(path) for path in self.all_versioned_paths()}
318
321
 
319
322
    def all_versioned_paths(self):
320
 
        ret = set()
 
323
        ret = {u''}
321
324
        todo = [(self.store, b'', self.tree)]
322
325
        while todo:
323
326
            (store, path, tree_id) = todo.pop()
326
329
            tree = store[tree_id]
327
330
            for name, mode, hexsha in tree.items():
328
331
                subpath = posixpath.join(path, name)
 
332
                ret.add(subpath.decode('utf-8'))
329
333
                if stat.S_ISDIR(mode):
330
334
                    todo.append((store, subpath, hexsha))
331
 
                else:
332
 
                    ret.add(subpath.decode('utf-8'))
333
335
        return ret
334
336
 
335
337
    def get_root_id(self):
393
395
        if mode is None: # Root
394
396
            root_ie = self._get_dir_ie(b"", None)
395
397
        else:
396
 
            parent_path = posixpath.dirname(from_dir.encode("utf-8"))
 
398
            parent_path = posixpath.dirname(from_dir)
397
399
            parent_id = self._fileid_map.lookup_file_id(parent_path)
398
400
            if mode_kind(mode) == 'directory':
399
401
                root_ie = self._get_dir_ie(from_dir.encode("utf-8"), parent_id)
400
402
            else:
401
403
                root_ie = self._get_file_ie(store, from_dir.encode("utf-8"),
402
404
                    posixpath.basename(from_dir), mode, hexsha)
403
 
        if from_dir != "" or include_root:
 
405
        if include_root:
404
406
            yield (from_dir, "V", root_ie.kind, root_ie.file_id, root_ie)
405
407
        todo = []
406
408
        if root_ie.kind == 'directory':
407
 
            todo.append((store, from_dir.encode("utf-8"), hexsha, root_ie.file_id))
 
409
            todo.append((store, from_dir.encode("utf-8"), b"", hexsha, root_ie.file_id))
408
410
        while todo:
409
 
            (store, path, hexsha, parent_id) = todo.pop()
 
411
            (store, path, relpath, hexsha, parent_id) = todo.pop()
410
412
            tree = store[hexsha]
411
413
            for name, mode, hexsha in tree.iteritems():
412
414
                if self.mapping.is_special_file(name):
413
415
                    continue
414
416
                child_path = posixpath.join(path, name)
 
417
                child_relpath = posixpath.join(relpath, name)
415
418
                if stat.S_ISDIR(mode):
416
419
                    ie = self._get_dir_ie(child_path, parent_id)
417
420
                    if recursive:
418
 
                        todo.append((store, child_path, hexsha, ie.file_id))
 
421
                        todo.append((store, child_path, child_relpath, hexsha, ie.file_id))
419
422
                else:
420
423
                    ie = self._get_file_ie(store, child_path, name, mode, hexsha, parent_id)
421
 
                yield child_path.decode('utf-8'), "V", ie.kind, ie.file_id, ie
 
424
                yield child_relpath.decode('utf-8'), "V", ie.kind, ie.file_id, ie
422
425
 
423
426
    def _get_file_ie(self, store, path, name, mode, hexsha, parent_id):
424
427
        if not isinstance(path, bytes):
449
452
    def iter_child_entries(self, path, file_id=None):
450
453
        (store, mode, tree_sha) = self._lookup_path(path)
451
454
 
452
 
        if not stat.S_ISDIR(mode):
 
455
        if mode is not None and not stat.S_ISDIR(mode):
453
456
            return
454
457
 
455
458
        encoded_path = path.encode('utf-8')
476
479
                specific_files = None
477
480
            else:
478
481
                specific_files = set([p.encode('utf-8') for p in specific_files])
479
 
        todo = [(self.store, b"", self.tree, None)]
 
482
        todo = deque([(self.store, b"", self.tree, self.get_root_id())])
 
483
        if specific_files is None or u"" in specific_files:
 
484
            yield u"", self._get_dir_ie(b"", None)
480
485
        while todo:
481
 
            store, path, tree_sha, parent_id = todo.pop()
482
 
            ie = self._get_dir_ie(path, parent_id)
483
 
            if specific_files is None or path in specific_files:
484
 
                yield path.decode("utf-8"), ie
 
486
            store, path, tree_sha, parent_id = todo.popleft()
485
487
            tree = store[tree_sha]
 
488
            extradirs = []
486
489
            for name, mode, hexsha in tree.iteritems():
487
490
                if self.mapping.is_special_file(name):
488
491
                    continue
489
492
                child_path = posixpath.join(path, name)
 
493
                child_path_decoded = child_path.decode('utf-8')
490
494
                if stat.S_ISDIR(mode):
491
495
                    if (specific_files is None or
492
496
                        any(filter(lambda p: p.startswith(child_path), specific_files))):
493
 
                        todo.append((store, child_path, hexsha, ie.file_id))
494
 
                elif specific_files is None or child_path in specific_files:
495
 
                    yield (child_path.decode("utf-8"),
496
 
                            self._get_file_ie(store, child_path, name, mode, hexsha,
497
 
                           ie.file_id))
 
497
                        extradirs.append(
 
498
                            (store, child_path, hexsha, self.path2id(child_path_decoded)))
 
499
                if specific_files is None or child_path in specific_files:
 
500
                    if stat.S_ISDIR(mode):
 
501
                        yield (child_path_decoded,
 
502
                               self._get_dir_ie(child_path, parent_id))
 
503
                    else:
 
504
                        yield (child_path_decoded,
 
505
                               self._get_file_ie(store, child_path, name, mode,
 
506
                                   hexsha, parent_id))
 
507
            todo.extendleft(reversed(extradirs))
 
508
 
 
509
    def iter_references(self):
 
510
        if self.supports_tree_reference():
 
511
            for path, entry in self.iter_entries_by_dir():
 
512
                if entry.kind == 'tree-reference':
 
513
                    yield path, self.mapping.generate_file_id(b'')
498
514
 
499
515
    def get_revision_id(self):
500
516
        """See RevisionTree.get_revision_id."""
509
525
        (store, mode, hexsha) = self._lookup_path(path)
510
526
        return ("GIT", hexsha)
511
527
 
 
528
    def get_file_size(self, path, file_id=None):
 
529
        (store, mode, hexsha) = self._lookup_path(path)
 
530
        if stat.S_ISREG(mode):
 
531
            return len(store[hexsha].data)
 
532
        return None
 
533
 
512
534
    def get_file_text(self, path, file_id=None):
513
535
        """See RevisionTree.get_file_text."""
514
536
        (store, mode, hexsha) = self._lookup_path(path)
551
573
            contents = store[hexsha].data
552
574
            return (kind, len(contents), executable, osutils.sha_string(contents))
553
575
        elif kind == 'symlink':
554
 
            return (kind, None, None, store[hexsha].data)
 
576
            return (kind, None, None, store[hexsha].data.decode('utf-8'))
555
577
        elif kind == 'tree-reference':
556
578
            nested_repo = self._get_nested_repository(path)
557
579
            return (kind, None, None,
605
627
                           for key, line in annotator.annotate_flat(this_key)]
606
628
            return annotations
607
629
 
 
630
    def _get_rules_searcher(self, default_searcher):
 
631
        return default_searcher
 
632
 
 
633
    def walkdirs(self, prefix=u""):
 
634
        (store, mode, hexsha) = self._lookup_path(prefix)
 
635
        todo = deque([(store, prefix.encode('utf-8'), hexsha, self.path2id(prefix))])
 
636
        while todo:
 
637
            store, path, tree_sha, parent_id = todo.popleft()
 
638
            path_decoded = path.decode('utf-8')
 
639
            tree = store[tree_sha]
 
640
            children = []
 
641
            for name, mode, hexsha in tree.iteritems():
 
642
                if self.mapping.is_special_file(name):
 
643
                    continue
 
644
                child_path = posixpath.join(path, name)
 
645
                file_id = self.path2id(child_path.decode('utf-8'))
 
646
                if stat.S_ISDIR(mode):
 
647
                    todo.append((store, child_path, hexsha, file_id))
 
648
                children.append(
 
649
                    (child_path.decode('utf-8'), name.decode('utf-8'),
 
650
                        mode_kind(mode), None,
 
651
                        file_id, mode_kind(mode)))
 
652
            yield (path_decoded, parent_id), children
 
653
 
608
654
 
609
655
def tree_delta_from_git_changes(changes, mapping,
610
656
        fileid_maps, specific_files=None,