/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 bzrlib/dirstate.py

Ensure Format4 working trees start with a dirstate.

Show diffs side-by-side

added added

removed removed

Lines of Context:
65
65
        for parent_id in parent_ids:
66
66
            parent_trees.append(tree.branch.repository.revision_tree(parent_id))
67
67
 
 
68
        lines.append(result._get_parents_line(parent_ids))
68
69
        # FIXME: is this utf8 safe?
69
 
        lines.append('\0'.join([str(num_parents)] + parent_ids))
70
70
 
71
71
        to_minikind = DirState._kind_to_minikind
72
72
        to_yesno = {True:'y', False: 'n'}
152
152
            for entry in to_remove:
153
153
                block.remove(entry)
154
154
 
 
155
        result.lines = result._get_output_lines(lines)
 
156
        return result
 
157
 
 
158
    def get_lines(self):
 
159
        """Serialise the entire dirstate to a sequence of lines."""
 
160
        return self.lines
 
161
 
 
162
    def _get_parents_line(self, parent_ids):
 
163
        """Create a line for the state file for parents information."""
 
164
        return '\0'.join([str(len(parent_ids))] + parent_ids)
 
165
        
 
166
    def get_parent_ids(self):
 
167
        """Return a list of the parent tree ids for the directory state."""
 
168
        self._read_header()
 
169
        return self._parents
 
170
 
 
171
    @staticmethod
 
172
    def initialize(path):
 
173
        """Create a new dirstate on path."""
 
174
        result = DirState()
 
175
        lines = []
 
176
        lines.append(result._get_parents_line([]))
 
177
        result.lines = result._get_output_lines(lines)
 
178
        state_file = open(path, 'wb+')
 
179
        try:
 
180
            state_file.write(''.join(result.lines))
 
181
        finally:
 
182
            state_file.close()
 
183
        return result
 
184
 
 
185
    def _get_output_lines(self, lines):
 
186
        """format lines for final output.
 
187
        
 
188
        :param lines: A sequece of lines containing the parents list and the
 
189
            path lines.
 
190
        """
155
191
        output_lines = ['#bzr dirstate flat format 1\n']
156
 
 
157
192
        lines.append('') # a final newline
158
193
        inventory_text = '\0\n\0'.join(lines)
159
194
        output_lines.append('adler32: %s\n' % (zlib.adler32(inventory_text),))
161
196
        num_entries = len(lines)-2
162
197
        output_lines.append('num_entries: %s\n' % (num_entries,))
163
198
        output_lines.append(inventory_text)
 
199
        return output_lines
164
200
 
165
 
        result.lines = output_lines
 
201
    @staticmethod
 
202
    def on_file(path):
 
203
        """Construct a DirState on the file at path path."""
 
204
        result = DirState()
 
205
        result.state_file = open(path, 'rb')
166
206
        return result
167
207
 
168
 
    def get_lines(self):
169
 
        """Serialise the entire dirstate to a sequence of lines."""
170
 
        return self.lines
171
 
        return [
172
 
            '#bzr dirstate flat format 1\n',
173
 
            'adler32: -2\n',
174
 
            'num_entries: 1\n',
175
 
            '0\x00\n',
176
 
            '\x00\x00\x00d\x00TREE_ROOT\x004096\x00AAAQAETIF65EyBeuAAADAQAQQxsAAEHt\x00\x00\n',
177
 
            '\x00',
178
 
            ]
 
208
    def _read_all(self):
 
209
        """Read the entire state."""
 
210
        self._read_header()
 
211
        
 
212
    def _read_header(self):
 
213
        """This reads in the metadata header, and the parent ids.
 
214
 
 
215
        After reading in, the file should be positioned at the null
 
216
        just before the start of the first record in the file.
 
217
 
 
218
        :return: (expected adler checksum, number of entries, parent list)
 
219
        """
 
220
        self._read_prelude()
 
221
        parent_line = self.state_file.readline()
 
222
        info = parent_line.split('\0')
 
223
        num_parents = int(info[0])
 
224
        assert num_parents == len(info)-2, 'incorrect parent info line'
 
225
 
 
226
        self._parents = [p.decode('utf8') for p in info[1:-1]]
 
227
 
 
228
    def _read_prelude(self):
 
229
        """Read in the prelude header of the dirstate file
 
230
 
 
231
        This only reads in the stuff that is not connected to the adler
 
232
        checksum. The position will be correct to read in the rest of
 
233
        the file and check the checksum after this point.
 
234
        The next entry in the file should be the number of parents,
 
235
        and their ids. Followed by a newline.
 
236
        """
 
237
        header = self.state_file.readline()
 
238
        assert header == '#bzr dirstate flat format 1\n', \
 
239
            'invalid header line: %r' % (header,)
 
240
        adler_line = self.state_file.readline()
 
241
        assert adler_line.startswith('adler32: '), 'missing adler32 checksum'
 
242
        self.adler_expected = int(adler_line[len('adler32: '):-1])
 
243
        num_entries_line = self.state_file.readline()
 
244
        assert num_entries_line.startswith('num_entries: '), 'missing num_entries line'
 
245
        self.num_entries = int(num_entries_line[len('num_entries: '):-1])
 
246
    
179
247
 
180
248
def pack_stat(st, _encode=base64.encodestring, _pack=struct.pack):
181
249
    """Convert stat values into a packed representation."""