~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/testament.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-02-21 17:41:02 UTC
  • mfrom: (1185.50.85 bzr-jam-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20060221174102-aa6bd4464296c614
Mac OSX raises EPERM when you try to unlink a directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
69
69
# TODO: Perhaps these should just be different formats in which inventories/
70
70
# revisions can be serialized.
71
71
 
 
72
from copy import copy
72
73
from cStringIO import StringIO
73
74
import string
74
75
from sha import sha
81
82
    Testaments can be 
82
83
 
83
84
      - produced from a revision
84
 
      - writen to a stream
 
85
      - written to a stream
85
86
      - loaded from a stream
86
87
      - compared to a revision
87
88
    """
88
89
 
89
90
    @classmethod
90
 
    def from_revision(cls, branch, revision_id):
 
91
    def from_revision(cls, repository, revision_id):
91
92
        """Produce a new testament from a historical revision"""
92
 
        t = cls()
93
 
        rev = branch.get_revision(revision_id)
94
 
        t.revision_id = str(revision_id)
95
 
        t.committer = rev.committer
96
 
        t.timezone = rev.timezone or 0
97
 
        t.timestamp = rev.timestamp
98
 
        t.message = rev.message
99
 
        t.parent_ids = rev.parent_ids[:]
100
 
        t.inventory = branch.get_inventory(revision_id)
101
 
        assert not contains_whitespace(t.revision_id)
102
 
        assert not contains_linebreaks(t.committer)
103
 
        return t
 
93
        rev = repository.get_revision(revision_id)
 
94
        inventory = repository.get_inventory(revision_id)
 
95
        return cls(rev, inventory)
 
96
 
 
97
    def __init__(self, rev, inventory):
 
98
        """Create a new testament for rev using inventory."""
 
99
        self.revision_id = str(rev.revision_id)
 
100
        self.committer = rev.committer
 
101
        self.timezone = rev.timezone or 0
 
102
        self.timestamp = rev.timestamp
 
103
        self.message = rev.message
 
104
        self.parent_ids = rev.parent_ids[:]
 
105
        self.inventory = inventory
 
106
        self.revprops = copy(rev.properties)
 
107
        assert not contains_whitespace(self.revision_id)
 
108
        assert not contains_linebreaks(self.committer)
104
109
 
105
110
    def as_text_lines(self):
106
111
        """Yield text form as a sequence of lines.
109
114
        hashed in that encoding.
110
115
        """
111
116
        r = []
112
 
        def a(s):
113
 
            r.append(s)
 
117
        a = r.append
114
118
        a('bazaar-ng testament version 1\n')
115
119
        a('revision-id: %s\n' % self.revision_id)
116
120
        a('committer: %s\n' % self.committer)
127
131
        a('inventory:\n')
128
132
        for path, ie in self.inventory.iter_entries():
129
133
            a(self._entry_to_line(path, ie))
 
134
        r.extend(self._revprops_to_lines())
130
135
        if __debug__:
131
136
            for l in r:
132
 
                assert isinstance(l, str), \
 
137
                assert isinstance(l, basestring), \
133
138
                    '%r of type %s is not a plain string' % (l, type(l))
134
 
        return r
 
139
        return [line.encode('utf-8') for line in r]
135
140
 
136
141
    def _escape_path(self, path):
137
142
        assert not contains_linebreaks(path)
165
170
                'sha1: %s\n'
166
171
                % (self.revision_id, s.hexdigest()))
167
172
 
 
173
    def _revprops_to_lines(self):
 
174
        """Pack up revision properties."""
 
175
        if not self.revprops:
 
176
            return []
 
177
        r = ['properties:\n']
 
178
        for name, value in sorted(self.revprops.items()):
 
179
            assert isinstance(name, str)
 
180
            assert not contains_whitespace(name)
 
181
            r.append('  %s:\n' % name)
 
182
            for line in value.splitlines():
 
183
                if not isinstance(line, str):
 
184
                    line = line.encode('utf-8')
 
185
                r.append('    %s\n' % line)
 
186
        return r