~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to read_changeset.py

  • Committer: Aaron Bentley
  • Date: 2005-07-27 18:46:33 UTC
  • mto: (1185.82.1 bzr-w-changeset)
  • mto: This revision was merged to the branch mainline in revision 1738.
  • Revision ID: abentley@panoramicfeedback.com-20050727184633-8b8601908bcdb90a
Switched to native patch application, added tests for terminating newlines

Show diffs side-by-side

added added

removed removed

Lines of Context:
809
809
        file_patch = self.patches.get(self.id2path(file_id))
810
810
        if file_patch is None:
811
811
            return patch_original
 
812
 
 
813
        assert not file_patch.startswith('\\'), \
 
814
            'Malformed patch for %s, %r' % (file_id, file_patch)
812
815
        return patched_file(file_patch, patch_original)
813
816
 
814
817
    def get_kind(self, file_id):
840
843
            if ie.text_size is None:
841
844
                return ie.text_size, ie.text_sha1
842
845
            return int(ie.text_size), ie.text_sha1
843
 
        content = self.get_file(file_id).read()
 
846
        fileobj = self.get_file(file_id)
 
847
        content = fileobj.read()
844
848
        return len(content), sha_string(content)
845
849
 
846
850
 
919
923
        paths.sort()
920
924
        return paths
921
925
 
922
 
 
923
926
def patched_file(file_patch, original):
924
 
    from bzrlib.patch import patch
925
 
    from tempfile import mkdtemp
926
 
    from shutil import rmtree
927
 
    from StringIO import StringIO
928
 
    from bzrlib.osutils import pumpfile
929
 
    import os.path
930
 
    temp_dir = mkdtemp()
931
 
    try:
932
 
        original_path = os.path.join(temp_dir, "originalfile")
933
 
        temp_original = file(original_path, "wb")
934
 
        if original is not None:
935
 
            pumpfile(original, temp_original)
936
 
        temp_original.close()
937
 
        patched_path = os.path.join(temp_dir, "patchfile")
938
 
        assert patch(file_patch, original_path, patched_path) == 0
939
 
        result = StringIO()
940
 
        temp_patched = file(patched_path, "rb")
941
 
        pumpfile(temp_patched, result)
942
 
        temp_patched.close()
943
 
        result.seek(0,0)
944
 
 
945
 
    finally:
946
 
        rmtree(temp_dir)
947
 
 
948
 
    return result
949
 
 
 
927
    """Produce a file-like object with the patched version of a text"""
 
928
    from patches import iter_patched
 
929
    from iterablefile import IterableFile
 
930
    if file_patch == "":
 
931
        return IterableFile(())
 
932
    return IterableFile(iter_patched(original, file_patch.splitlines(True)))
 
933