~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/testament.py

  • Committer: Aaron Bentley
  • Date: 2006-08-08 06:47:16 UTC
  • mto: (1910.2.43 format-bumps)
  • mto: This revision was merged to the branch mainline in revision 1922.
  • Revision ID: aaron.bentley@utoronto.ca-20060808064716-75bc465292c2708f
Ensure root entry always has a revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
82
82
    Testaments can be 
83
83
 
84
84
      - produced from a revision
85
 
      - writen to a stream
 
85
      - written to a stream
86
86
      - loaded from a stream
87
87
      - compared to a revision
88
88
    """
89
89
 
 
90
    long_header = 'bazaar-ng testament version 1\n'
 
91
    short_header = 'bazaar-ng testament short form 1\n'
 
92
 
90
93
    @classmethod
91
 
    def from_revision(cls, branch, revision_id):
 
94
    def from_revision(cls, repository, revision_id):
92
95
        """Produce a new testament from a historical revision"""
93
 
        rev = branch.get_revision(revision_id)
94
 
        inventory = branch.get_inventory(revision_id)
 
96
        rev = repository.get_revision(revision_id)
 
97
        inventory = repository.get_inventory(revision_id)
95
98
        return cls(rev, inventory)
96
99
 
97
100
    def __init__(self, rev, inventory):
114
117
        hashed in that encoding.
115
118
        """
116
119
        r = []
117
 
        def a(s):
118
 
            r.append(s)
119
 
        a('bazaar-ng testament version 1\n')
 
120
        a = r.append
 
121
        a(self.long_header)
120
122
        a('revision-id: %s\n' % self.revision_id)
121
123
        a('committer: %s\n' % self.committer)
122
124
        a('timestamp: %d\n' % self.timestamp)
130
132
        for l in self.message.splitlines():
131
133
            a('  %s\n' % l)
132
134
        a('inventory:\n')
133
 
        for path, ie in self.inventory.iter_entries():
 
135
        entries = self.inventory.iter_entries()
 
136
        entries.next()
 
137
        for path, ie in entries:
134
138
            a(self._entry_to_line(path, ie))
135
139
        r.extend(self._revprops_to_lines())
136
140
        if __debug__:
137
141
            for l in r:
138
142
                assert isinstance(l, basestring), \
139
143
                    '%r of type %s is not a plain string' % (l, type(l))
140
 
        return r
 
144
        return [line.encode('utf-8') for line in r]
141
145
 
142
146
    def _escape_path(self, path):
143
147
        assert not contains_linebreaks(path)
157
161
            assert ie.symlink_target
158
162
            l += ' ' + self._escape_path(ie.symlink_target)
159
163
        l += '\n'
160
 
        return l
 
164
        return l.decode('utf-8')
161
165
 
162
166
    def as_text(self):
163
167
        return ''.join(self.as_text_lines())
164
168
 
165
169
    def as_short_text(self):
166
170
        """Return short digest-based testament."""
167
 
        s = sha()
168
 
        map(s.update, self.as_text_lines())
169
 
        return ('bazaar-ng testament short form 1\n'
 
171
        return (self.short_header + 
170
172
                'revision-id: %s\n'
171
173
                'sha1: %s\n'
172
 
                % (self.revision_id, s.hexdigest()))
 
174
                % (self.revision_id, self.as_sha1()))
173
175
 
174
176
    def _revprops_to_lines(self):
175
177
        """Pack up revision properties."""
185
187
                    line = line.encode('utf-8')
186
188
                r.append('    %s\n' % line)
187
189
        return r
 
190
 
 
191
    def as_sha1(self):
 
192
        s = sha()
 
193
        map(s.update, self.as_text_lines())
 
194
        return s.hexdigest()
 
195
 
 
196
 
 
197
class StrictTestament(Testament):
 
198
    """This testament format is for use as a checksum in changesets"""
 
199
 
 
200
    long_header = 'bazaar-ng testament version 2.1\n'
 
201
    short_header = 'bazaar-ng testament short form 2.1\n'
 
202
    def _entry_to_line(self, path, ie):
 
203
        l = Testament._entry_to_line(self, path, ie)[:-1]
 
204
        l += ' ' + ie.revision.decode('utf-8')
 
205
        l += {True: ' yes\n', False: ' no\n'}[ie.executable]
 
206
        return l