~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to read_changeset.py

  • Committer: aaron.bentley at utoronto
  • Date: 2005-07-01 00:48:12 UTC
  • mto: (0.5.85) (1185.82.1 bzr-w-changeset)
  • mto: This revision was merged to the branch mainline in revision 1738.
  • Revision ID: aaron.bentley@utoronto.ca-20050701004812-449cf077546a6637
Got get_file working for new files

Show diffs side-by-side

added added

removed removed

Lines of Context:
440
440
        self._new_id[new_path] = new_id
441
441
        self._new_id_r[new_id] = new_path
442
442
 
443
 
    def note_patch(new_path, patch):
444
 
        self._patches[new_path] = patch
 
443
    def note_patch(self, new_path, patch):
 
444
        self.patches[new_path] = patch
445
445
 
446
446
    def old_path(self, new_path):
447
447
        import os.path
509
509
            patch_original = self.base_tree.get_file(file_id)
510
510
        else:
511
511
            patch_original = None
512
 
        file_patch = patches.get(self.id2path(id))
 
512
        file_patch = self.patches.get(self.id2path(file_id))
513
513
        if file_patch is None:
514
 
            return patch_base
 
514
            return patch_original
515
515
        return patched_file(file_patch, patch_original)
516
516
 
517
517
def patched_file(file_patch, original):
519
519
    from tempfile import mkdtemp
520
520
    from shutil import rmtree
521
521
    from StringIO import StringIO
522
 
    from osutils import pumpfile
 
522
    from bzrlib.osutils import pumpfile
 
523
    import os.path
523
524
    temp_dir = mkdtemp()
524
525
    try:
525
 
        if original is None:
526
 
            original_path = "/dev/null"
527
 
        else:
528
 
            original_path = os.path.join(temp_dir, "originalfile")
529
 
            temp_original = file(original_path, "wb")
 
526
        original_path = os.path.join(temp_dir, "originalfile")
 
527
        temp_original = file(original_path, "wb")
 
528
        if original is not None:
530
529
            pumpfile(original, temp_original)
531
 
            temp_original.close()
 
530
        temp_original.close()
532
531
        patched_path = os.path.join(temp_dir, "patchfile")
533
532
        patch(file_patch, original_path, patched_path)
534
533
        result = StringIO()
535
 
        temp_patched = file(patched_file, "rb")
 
534
        temp_patched = file(patched_path, "rb")
536
535
        pumpfile(temp_patched, result)
537
536
        temp_patched.close()
538
537
        result.seek(0,0)
545
544
def test():
546
545
    import unittest
547
546
    from StringIO import StringIO
 
547
    from bzrlib.diff import internal_diff
548
548
    class MockTree(object):
549
549
        def __init__(self):
550
550
            object.__init__(self)
566
566
        def id2path(self, file_id):
567
567
            return self.paths.get(file_id)
568
568
 
 
569
        def has_id(self, file_id):
 
570
            return self.id2path(file_id) is not None
 
571
 
569
572
        def get_file(file_id):
570
573
            result = StringIO()
571
574
            result.write(self.contents[file_id])
652
655
            assert ctree.path2id("grandparent/alt_parent/file") == "c"
653
656
            assert ctree.path2id("grandparent/parent/file") == None
654
657
 
 
658
        def unified_diff(self, old, new):
 
659
            out = StringIO()
 
660
            internal_diff("old", old, "new", new, out)
 
661
            out.seek(0,0)
 
662
            return out.read()
 
663
 
655
664
        def testAdds(self):
656
665
            """Ensure that inventory adds work"""
657
666
            ctree = self.make_tree_1()[0]
660
669
            assert ctree.id2path("e") is None
661
670
            assert ctree.path2id("grandparent/parent/file") is None
662
671
            ctree.note_id("e", "grandparent/parent/file")
 
672
            add_patch = self.unified_diff([], ["Extra cheese"])
 
673
            ctree.note_patch("grandparent/parent/file", add_patch)
 
674
 
663
675
            assert ctree.id2path("e") == "grandparent/parent/file"
664
676
            assert ctree.path2id("grandparent/parent/file") == "e"
665
 
            
 
677
            assert ctree.get_file("e").read() == "Extra cheese"
666
678
        
667
679
    patchesTestSuite = unittest.makeSuite(CTreeTester,'test')
668
680
    runner = unittest.TextTestRunner()