~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commit.py

  • Committer: Martin Pool
  • Date: 2005-09-15 06:35:58 UTC
  • Revision ID: mbp@sourcefrog.net-20050915063558-f3b5bae25543c922
- add assertion

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
# TODO: Update hashcache before and after - or does the WorkingTree
45
45
# look after that?
46
46
 
47
 
# This code requires all merge parents to be present in the branch.
48
 
# We could relax this but for the sake of simplicity the constraint is
49
 
# here for now.  It's not totally clear to me how we'd know which file
50
 
# need new text versions if some parents are absent.  -- mbp 20050915
51
 
 
52
 
# TODO: Rather than mashing together the ancestry and storing it back,
53
 
# perhaps the weave should have single method which does it all in one
54
 
# go, avoiding a lot of redundant work.
55
 
 
56
 
# TODO: Perhaps give a warning if one of the revisions marked as
57
 
# merged is already in the ancestry, and then don't record it as a
58
 
# distinct parent.
59
 
 
60
 
# TODO: If the file is newly merged but unchanged from the version it
61
 
# merges from, then it should still be reported as newly added
62
 
# relative to the basis revision.
63
47
 
64
48
 
65
49
import os
75
59
                            kind_marker, is_inside_any, quotefn,
76
60
                            sha_string, sha_strings, sha_file, isdir, isfile,
77
61
                            split_lines)
78
 
from bzrlib.branch import gen_file_id
79
 
from bzrlib.errors import (BzrError, PointlessCommit,
80
 
                           HistoryMissing,
81
 
                           )
82
 
from bzrlib.revision import Revision
83
 
from bzrlib.trace import mutter, note, warning
 
62
from bzrlib.branch import gen_file_id, INVENTORY_FILEID, ANCESTRY_FILEID
 
63
from bzrlib.errors import BzrError, PointlessCommit
 
64
from bzrlib.revision import Revision, RevisionReference
 
65
from bzrlib.trace import mutter, note
84
66
from bzrlib.xml5 import serializer_v5
85
67
from bzrlib.inventory import Inventory
86
68
from bzrlib.weave import Weave
95
77
 
96
78
    New code should use the Commit class instead.
97
79
    """
98
 
    ## XXX: Remove this in favor of Branch.commit?
99
80
    Commit().commit(*args, **kwargs)
100
81
 
101
82
 
149
130
               committer=None,
150
131
               specific_files=None,
151
132
               rev_id=None,
152
 
               allow_pointless=True,
153
 
               verbose=False):
 
133
               allow_pointless=True):
154
134
        """Commit working copy as a new revision.
155
135
 
156
136
        timestamp -- if not None, seconds-since-epoch for a
167
147
        allow_pointless -- If true (default), commit even if nothing
168
148
            has changed and no merges are recorded.
169
149
        """
170
 
        mutter('preparing to commit')
 
150
        self.any_changes = False
171
151
 
172
152
        self.branch = branch
173
153
        self.weave_store = branch.weave_store
207
187
            self.basis_inv = self.basis_tree.inventory
208
188
 
209
189
            self._gather_parents()
210
 
            if len(self.parents) > 1 and self.specific_files:
211
 
                raise NotImplementedError('selected-file commit of merges is not supported yet')
212
 
            self._check_parents_present()
213
 
            
 
190
 
214
191
            self._remove_deleted()
215
192
            self.new_inv = Inventory()
216
 
            self._store_entries()
 
193
            self._store_files()
217
194
            self._report_deletes()
218
 
            self._set_name_versions()
219
195
 
220
196
            if not (self.allow_pointless
221
 
                    or len(self.parents) > 1
 
197
                    or len(self.parents) != 1
222
198
                    or self.new_inv != self.basis_inv):
223
199
                raise PointlessCommit()
224
200
 
238
214
        """Store the inventory for the new revision."""
239
215
        inv_text = serializer_v5.write_inventory_to_string(self.new_inv)
240
216
        self.inv_sha1 = sha_string(inv_text)
241
 
        s = self.branch.control_weaves
242
 
        s.add_text('inventory', self.rev_id,
243
 
                   split_lines(inv_text), self.parents)
 
217
        self.weave_store.add_text(INVENTORY_FILEID, self.rev_id,
 
218
                                         split_lines(inv_text), self.parents)
244
219
 
245
220
 
246
221
    def _record_ancestry(self):
247
 
        """Append merged revision ancestry to the ancestry file.
248
 
 
249
 
        This should be the merged ancestry of all parents, plus the
250
 
        new revision id."""
251
 
        s = self.branch.control_weaves
252
 
        w = s.get_weave_or_empty('ancestry')
253
 
        lines = self._make_ancestry(w)
254
 
        w.add(self.rev_id, self.parents, lines)
255
 
        s.put_weave('ancestry', w)
256
 
 
257
 
 
258
 
    def _make_ancestry(self, ancestry_weave):
259
 
        """Return merged ancestry lines.
260
 
 
261
 
        The lines are revision-ids followed by newlines."""
262
 
        parent_ancestries = [ancestry_weave.get(p) for p in self.parents]
263
 
        new_lines = merge_ancestry_lines(self.rev_id, parent_ancestries)
264
 
        mutter('merged ancestry of {%s}:\n%s', self.rev_id, ''.join(new_lines))
265
 
        return new_lines
 
222
        """Append merged revision ancestry to the ancestry file."""
 
223
        w = self.weave_store.get_weave_or_empty(ANCESTRY_FILEID)
 
224
        if self.parents:
 
225
            lines = w.get(w.lookup(self.parents[0]))
 
226
        else:
 
227
            lines = []
 
228
        lines.append(self.rev_id + '\n')
 
229
        parent_idxs = map(w.lookup, self.parents)
 
230
        w.add(self.rev_id, parent_idxs, lines)
 
231
        self.weave_store.put_weave(ANCESTRY_FILEID, w)
266
232
 
267
233
 
268
234
    def _gather_parents(self):
269
235
        pending_merges = self.branch.pending_merges()
270
236
        self.parents = []
271
 
        self.parent_trees = []
272
237
        precursor_id = self.branch.last_revision()
273
238
        if precursor_id:
274
239
            self.parents.append(precursor_id)
275
 
            self.parent_trees.append(self.basis_tree)
276
240
        self.parents += pending_merges
277
 
        self.parent_trees.extend(map(self.branch.revision_tree, pending_merges))
278
 
 
279
 
 
280
 
    def _check_parents_present(self):
281
 
        for parent_id in self.parents:
282
 
            mutter('commit parent revision {%s}', parent_id)
283
 
            if not self.branch.has_revision(parent_id):
284
 
                warning("can't commit a merge from an absent parent")
285
 
                raise HistoryMissing(self.branch, 'revision', parent_id)
286
 
 
287
 
            
 
241
        self.parent_trees = map(self.branch.revision_tree, self.parents)
 
242
 
 
243
 
288
244
    def _make_revision(self):
289
245
        """Record a new revision object for this commit."""
290
246
        self.rev = Revision(timestamp=self.timestamp,
293
249
                            message=self.message,
294
250
                            inventory_sha1=self.inv_sha1,
295
251
                            revision_id=self.rev_id)
296
 
        self.rev.parent_ids = self.parents
 
252
        self.rev.parents = map(RevisionReference, self.parents)
297
253
        rev_tmp = StringIO()
298
254
        serializer_v5.write_revision(self.rev, rev_tmp)
299
255
        rev_tmp.seek(0)
300
 
        self.branch.revision_store.add(rev_tmp, self.rev_id, compressed=False)
 
256
        self.branch.revision_store.add(rev_tmp, self.rev_id)
301
257
        mutter('new revision_id is {%s}', self.rev_id)
302
258
 
303
259
 
329
285
    def _find_file_parents(self, file_id):
330
286
        """Return the text versions and hashes for all file parents.
331
287
 
332
 
        Returned as a map from text version to inventory entry.
 
288
        Returned as a map from text version to text sha1.
333
289
 
334
290
        This is a set containing the file versions in all parents
335
291
        revisions containing the file.  If the file is new, the set
341
297
                assert ie.kind == 'file'
342
298
                assert ie.file_id == file_id
343
299
                if ie.text_version in r:
344
 
                    assert r[ie.text_version] == ie
345
 
                else:
346
 
                    r[ie.text_version] = ie
347
 
        return r
348
 
 
349
 
 
350
 
    def _set_name_versions(self):
351
 
        """Pass over inventory and mark new entry version as needed.
352
 
 
353
 
        Files get a new name version when they are new, have a
354
 
        different parent, or a different name from in the
355
 
        basis inventory, or if the file is in a different place
356
 
        to any of the parents."""
357
 
        # XXX: Need to think more here about when the user has
358
 
        # made a specific decision on a particular value -- c.f.
359
 
        # mark-merge.  
360
 
        for path, ie in self.new_inv.iter_entries():
361
 
            old_version = None
362
 
            file_id = ie.file_id
363
 
            for parent_tree in self.parent_trees:
364
 
                parent_inv = parent_tree.inventory
365
 
                if file_id not in parent_inv:
366
 
                    continue
367
 
                parent_ie = parent_inv[file_id]
368
 
                if parent_ie.parent_id != ie.parent_id:
369
 
                    old_version = None
370
 
                    break
371
 
                elif parent_ie.name != ie.name:
372
 
                    old_version = None
373
 
                    break
374
 
                elif old_version is None:
375
 
                    old_version = parent_ie.name_version
376
 
                elif old_version != parent_ie.name_version:
377
 
                    old_version = None
378
 
                    break
379
 
                else:
380
 
                    pass                # so far so good
381
 
            if old_version is None:
382
 
                mutter('new name_version for {%s}', file_id)
383
 
                ie.name_version = self.rev_id
384
 
            else:
385
 
                mutter('name_version for {%s} inherited as {%s}',
386
 
                       file_id, old_version)
387
 
                ie.name_version = old_version
388
 
 
389
 
 
390
 
    def _store_entries(self):
391
 
        """Build revision inventory and store modified files.
392
 
 
393
 
        This is called with new_inv a new empty inventory.  Depending on
394
 
        which files are selected for commit, and which ones have
395
 
        been modified or merged, new inventory entries are built
396
 
        based on the working and parent inventories.
397
 
 
398
 
        As a side-effect this stores new text versions for committed
399
 
        files with text changes or merges.
400
 
 
401
 
        Each entry can have one of several things happen:
402
 
 
403
 
        carry_file -- carried from the previous version (if not
404
 
            selected for commit)
405
 
 
406
 
        commit_nonfile -- no text to worry about
407
 
 
408
 
        commit_old_text -- same text, may have moved
409
 
 
410
 
        commit_file -- new text version
411
 
        """
 
300
                    assert r[ie.text_version] == ie.text_sha1
 
301
                else:
 
302
                    r[ie.text_version] = ie.text_sha1
 
303
        return r            
 
304
 
 
305
 
 
306
    def _store_files(self):
 
307
        """Store new texts of modified/added files.
 
308
 
 
309
        This is called with new_inv set to a copy of the working
 
310
        inventory, with deleted/removed files already cut out.  So
 
311
        this code only needs to deal with setting text versions, and
 
312
        possibly recording new file texts."""
412
313
        for path, new_ie in self.work_inv.iter_entries():
413
314
            file_id = new_ie.file_id
414
315
            mutter('check %s {%s}', path, new_ie.file_id)
420
321
            if new_ie.kind != 'file':
421
322
                self._commit_nonfile(file_id)
422
323
                continue
423
 
            
424
324
            file_parents = self._find_file_parents(file_id)
 
325
            wc_sha1 = self.work_tree.get_file_sha1(file_id)
 
326
            if (len(file_parents) == 1
 
327
                and file_parents.values()[0] == wc_sha1):
 
328
                # not changed or merged
 
329
                self._carry_file(file_id)
 
330
                continue
 
331
 
425
332
            mutter('parents of %s are %r', path, file_parents)
426
 
            if len(file_parents) == 1:
427
 
                parent_ie = file_parents.values()[0]
428
 
                wc_sha1 = self.work_tree.get_file_sha1(file_id)
429
 
                if parent_ie.text_sha1 == wc_sha1:
430
 
                    # text not changed or merged
431
 
                    self._commit_old_text(file_id, parent_ie)
432
 
                    continue
 
333
 
433
334
            # file is either new, or a file merge; need to record
434
335
            # a new version
435
336
            if len(file_parents) > 1:
446
347
 
447
348
 
448
349
    def _carry_file(self, file_id):
449
 
        """Carry the file unchanged from the basis revision."""
 
350
        """Keep a file in the same state as in the basis."""
450
351
        if self.basis_inv.has_id(file_id):
451
352
            self.new_inv.add(self.basis_inv[file_id].copy())
452
353
 
453
354
 
454
 
    def _commit_old_text(self, file_id, parent_ie):
455
 
        """Keep the same text as last time, but possibly a different name."""
456
 
        ie = self.work_inv[file_id].copy()
457
 
        ie.text_version = parent_ie.text_version
458
 
        ie.text_size = parent_ie.text_size
459
 
        ie.text_sha1 = parent_ie.text_sha1
460
 
        self.new_inv.add(ie)
461
 
 
462
 
 
463
355
    def _report_deletes(self):
464
356
        for file_id in self.basis_inv:
465
357
            if file_id not in self.new_inv:
478
370
 
479
371
 
480
372
    def _add_text_to_weave(self, file_id, new_lines, parents):
 
373
        if file_id.startswith('__'):
 
374
            raise ValueError('illegal file-id %r for text file' % file_id)
481
375
        self.weave_store.add_text(file_id, self.rev_id, new_lines, parents)
482
376
 
483
377
 
490
384
 
491
385
 
492
386
    
493
 
def merge_ancestry_lines(rev_id, ancestries):
494
 
    """Return merged ancestry lines.
495
 
 
496
 
    rev_id -- id of the new revision
497
 
    
498
 
    ancestries -- a sequence of ancestries for parent revisions,
499
 
        as newline-terminated line lists.
500
 
    """
501
 
    if len(ancestries) == 0:
502
 
        return [rev_id + '\n']
503
 
    seen = set(ancestries[0])
504
 
    ancs = ancestries[0][:]    
505
 
    for parent_ancestry in ancestries[1:]:
506
 
        for line in parent_ancestry:
507
 
            assert line[-1] == '\n'
508
 
            if line not in seen:
509
 
                ancs.append(line)
510
 
                seen.add(line)
511
 
    r = rev_id + '\n'
512
 
    assert r not in seen
513
 
    ancs.append(r)
514
 
    return ancs
515