~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/inventory.py

  • Committer: Robert Collins
  • Date: 2005-09-30 16:38:34 UTC
  • mto: This revision was merged to the branch mainline in revision 1397.
  • Revision ID: robertc@robertcollins.net-20050930163834-6b74beb609162e00
move inventory entry centric snapshot taking logic to inventory.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
115
115
                 'text_id', 'parent_id', 'children',
116
116
                 'revision', 'symlink_target']
117
117
 
 
118
    def _add_text_to_weave(self, new_lines, parents, weave_store):
 
119
        weave_store.add_text(self.file_id, self.revision, new_lines, parents)
 
120
 
118
121
    def __init__(self, file_id, name, kind, parent_id, text_id=None):
119
122
        """Create an InventoryEntry
120
123
        
218
221
        # others are added
219
222
        return other
220
223
 
 
224
    def _get_snapshot_change(self, previous_entries):
 
225
        if len(previous_entries) > 1:
 
226
            return 'merged'
 
227
        elif len(previous_entries) == 0:
 
228
            return 'added'
 
229
        else:
 
230
            return 'modified/renamed/reparented'
221
231
 
222
232
    def __repr__(self):
223
233
        return ("%s(%r, %r, kind=%r, parent_id=%r)"
227
237
                   self.kind,
228
238
                   self.parent_id))
229
239
 
 
240
    def snapshot(self, revision, path, previous_entries, work_tree, 
 
241
                 weave_store):
 
242
        """Make a snapshot of this entry.
 
243
        
 
244
        This means that all its fields are populated, that it has its
 
245
        text stored in the text store or weave.
 
246
        """
 
247
        mutter('new parents of %s are %r', path, previous_entries)
 
248
        self._read_tree_state(path, work_tree)
 
249
        if len(previous_entries) == 1:
 
250
            # cannot be unchanged unless there is only one parent file rev.
 
251
            parent_ie = previous_entries.values()[0]
 
252
            if self._unchanged(path, parent_ie, work_tree):
 
253
                mutter("found unchanged entry")
 
254
                self.revision = parent_ie.revision
 
255
                return "unchanged"
 
256
        mutter('new revision for {%s}', self.file_id)
 
257
        self.revision = revision
 
258
        change = self._get_snapshot_change(previous_entries)
 
259
        if self.kind != 'file':
 
260
            return change
 
261
        self._snapshot_text(previous_entries, work_tree, weave_store)
 
262
        return change
 
263
 
 
264
    def _snapshot_text(self, file_parents, work_tree, weave_store): 
 
265
        mutter('storing file {%s} in revision {%s}',
 
266
               self.file_id, self.revision)
 
267
        # special case to avoid diffing on renames or 
 
268
        # reparenting
 
269
        if (len(file_parents) == 1
 
270
            and self.text_sha1 == file_parents.values()[0].text_sha1
 
271
            and self.text_size == file_parents.values()[0].text_size):
 
272
            previous_ie = file_parents.values()[0]
 
273
            weave_store.add_identical_text(
 
274
                self.file_id, previous_ie.revision, 
 
275
                self.revision, file_parents)
 
276
        else:
 
277
            new_lines = work_tree.get_file(self.file_id).readlines()
 
278
            self._add_text_to_weave(new_lines, file_parents, weave_store)
 
279
            self.text_sha1 = sha_strings(new_lines)
 
280
            self.text_size = sum(map(len, new_lines))
 
281
 
230
282
    def __eq__(self, other):
231
283
        if not isinstance(other, InventoryEntry):
232
284
            return NotImplemented
247
299
    def __hash__(self):
248
300
        raise ValueError('not hashable')
249
301
 
250
 
    def unchanged(self, previous_ie, work_tree):
 
302
    def _unchanged(self, path, previous_ie, work_tree):
251
303
        compatible = True
252
304
        # different inv parent
253
305
        if previous_ie.parent_id != self.parent_id:
255
307
        # renamed
256
308
        elif previous_ie.name != self.name:
257
309
            compatible = False
258
 
        # changed link target
259
 
        elif (hasattr(self,'symlink_target')
260
 
              and self.symlink_target != previous_ie.symlink_target):
261
 
            compatible = False
262
 
        if self.kind != 'file':
263
 
            return compatible
264
 
        self.text_sha1 = work_tree.get_file_sha1(self.file_id)
265
 
        if self.text_sha1 != previous_ie.text_sha1:
266
 
            compatible = False
267
 
        else:
268
 
            # FIXME: 20050930 probe for the text size when getting sha1
269
 
            self.text_size = previous_ie.text_size
 
310
        if self.kind == 'symlink':
 
311
            if self.symlink_target != previous_ie.symlink_target:
 
312
                compatible = False
 
313
        if self.kind == 'file':
 
314
            if self.text_sha1 != previous_ie.text_sha1:
 
315
                compatible = False
 
316
            else:
 
317
                # FIXME: 20050930 probe for the text size when getting sha1
 
318
                # in _read_tree_state
 
319
                self.text_size = previous_ie.text_size
270
320
        return compatible
271
321
 
 
322
    def _read_tree_state(self, path, work_tree):
 
323
        if self.kind == 'symlink':
 
324
            self.read_symlink_target(work_tree.abspath(path))
 
325
        if self.kind == 'file':
 
326
            self.text_sha1 = work_tree.get_file_sha1(self.file_id)
 
327
 
272
328
 
273
329
class RootEntry(InventoryEntry):
274
330
    def __init__(self, file_id):