~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commit.py

  • Committer: Robert Collins
  • Date: 2006-08-01 04:54:43 UTC
  • mfrom: (1864.6.1 plugin-imports)
  • mto: This revision was merged to the branch mainline in revision 1901.
  • Revision ID: robertc@robertcollins.net-20060801045443-b99468f4250e01c4
Merge Johns bug 51810 fixup.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
 
18
18
# XXX: Can we do any better about making interrupted commits change
19
 
# nothing?  
 
19
# nothing?  Perhaps the best approach is to integrate commit of
 
20
# AtomicFiles with releasing the lock on the Branch.
20
21
 
21
22
# TODO: Separate 'prepare' phase where we find a list of potentially
22
23
# committed files.  We then can then pause the commit to prompt for a
61
62
 
62
63
# TODO: If commit fails, leave the message in a file somewhere.
63
64
 
64
 
# TODO: Change the parameter 'rev_id' to 'revision_id' to be consistent with
65
 
# the rest of the code; add a deprecation of the old name.
66
65
 
67
66
import os
68
67
import re
69
68
import sys
70
69
import time
 
70
import warnings
71
71
 
72
72
from cStringIO import StringIO
73
73
 
 
74
from bzrlib.atomicfile import AtomicFile
74
75
import bzrlib.config
75
76
import bzrlib.errors as errors
76
77
from bzrlib.errors import (BzrError, PointlessCommit,
84
85
from bzrlib.trace import mutter, note, warning
85
86
from bzrlib.xml5 import serializer_v5
86
87
from bzrlib.inventory import Inventory, ROOT_ID, InventoryEntry
87
 
from bzrlib import symbol_versioning
88
88
from bzrlib.symbol_versioning import (deprecated_passed,
89
89
        deprecated_function,
 
90
        zero_seven,
90
91
        DEPRECATED_PARAMETER)
91
92
from bzrlib.workingtree import WorkingTree
92
93
 
93
94
 
 
95
@deprecated_function(zero_seven)
 
96
def commit(*args, **kwargs):
 
97
    """Commit a new revision to a branch.
 
98
 
 
99
    Function-style interface for convenience of old callers.
 
100
 
 
101
    New code should use the Commit class instead.
 
102
    """
 
103
    ## XXX: Remove this in favor of WorkingTree.commit?
 
104
    Commit().commit(*args, **kwargs)
 
105
 
 
106
 
94
107
class NullCommitReporter(object):
95
108
    """I report on progress of a commit."""
96
109
 
210
223
        mutter('preparing to commit')
211
224
 
212
225
        if deprecated_passed(branch):
213
 
            symbol_versioning.warn("Commit.commit (branch, ...): The branch parameter is "
 
226
            warnings.warn("Commit.commit (branch, ...): The branch parameter is "
214
227
                 "deprecated as of bzr 0.8. Please use working_tree= instead.",
215
228
                 DeprecationWarning, stacklevel=2)
216
229
            self.branch = branch
247
260
            self._check_bound_branch()
248
261
 
249
262
            # check for out of date working trees
250
 
            try:
251
 
                first_tree_parent = self.work_tree.get_parent_ids()[0]
252
 
            except IndexError:
253
 
                # if there are no parents, treat our parent as 'None'
254
 
                # this is so that we still consier the master branch
255
 
                # - in a checkout scenario the tree may have no
256
 
                # parents but the branch may do.
257
 
                first_tree_parent = None
258
 
            master_last = self.master_branch.last_revision()
259
 
            if (master_last is not None and
260
 
                master_last != first_tree_parent):
 
263
            # if we are bound, then self.branch is the master branch and this
 
264
            # test is thus all we need.
 
265
            if self.work_tree.last_revision() != self.master_branch.last_revision():
261
266
                raise errors.OutOfDateTree(self.work_tree)
262
267
    
263
268
            if strict:
289
294
            if len(self.parents) > 1 and self.specific_files:
290
295
                raise NotImplementedError('selected-file commit of merges is not supported yet: files %r',
291
296
                        self.specific_files)
292
 
            
 
297
            self._check_parents_present()
293
298
            self.builder = self.branch.get_commit_builder(self.parents, 
294
299
                self.config, timestamp, timezone, committer, revprops, rev_id)
295
300
            
297
302
            self._populate_new_inv()
298
303
            self._report_deletes()
299
304
 
300
 
            self._check_pointless()
 
305
            if not (self.allow_pointless
 
306
                    or len(self.parents) > 1
 
307
                    or self.builder.new_inventory != self.basis_inv):
 
308
                raise PointlessCommit()
301
309
 
302
310
            self._emit_progress_update()
303
 
            # TODO: Now the new inventory is known, check for conflicts and
304
 
            # prompt the user for a commit message.
305
 
            # ADHB 2006-08-08: If this is done, populate_new_inv should not add
306
 
            # weave lines, because nothing should be recorded until it is known
307
 
            # that commit will succeed.
 
311
            # TODO: Now the new inventory is known, check for conflicts and prompt the 
 
312
            # user for a commit message.
308
313
            self.builder.finish_inventory()
309
314
            self._emit_progress_update()
310
315
            self.rev_id = self.builder.commit(self.message)
324
329
            # and now do the commit locally.
325
330
            self.branch.append_revision(self.rev_id)
326
331
 
327
 
            # if the builder gave us the revisiontree it created back, we
328
 
            # could use it straight away here.
329
 
            # TODO: implement this.
330
 
            self.work_tree.set_parent_trees([(self.rev_id,
331
 
                self.branch.repository.revision_tree(self.rev_id))])
 
332
            self.work_tree.set_pending_merges([])
 
333
            self.work_tree.set_last_revision(self.rev_id)
332
334
            # now the work tree is up to date with the branch
333
335
            
334
336
            self.reporter.completed(self.branch.revno(), self.rev_id)
345
347
            self._cleanup()
346
348
        return self.rev_id
347
349
 
348
 
    def _check_pointless(self):
349
 
        if self.allow_pointless:
350
 
            return
351
 
        # A merge with no effect on files
352
 
        if len(self.parents) > 1:
353
 
            return
354
 
        # work around the fact that a newly-initted tree does differ from its
355
 
        # basis
356
 
        if len(self.builder.new_inventory) != len(self.basis_inv):
357
 
            return
358
 
        if (len(self.builder.new_inventory) != 1 and
359
 
            self.builder.new_inventory != self.basis_inv):
360
 
            return
361
 
        raise PointlessCommit()
362
 
 
363
350
    def _check_bound_branch(self):
364
351
        """Check to see if the local branch is bound.
365
352
 
463
450
        self.parent_invs = []
464
451
        for revision in self.parents:
465
452
            if self.branch.repository.has_revision(revision):
466
 
                mutter('commit parent revision {%s}', revision)
467
453
                inventory = self.branch.repository.get_inventory(revision)
468
454
                self.parent_invs.append(inventory)
469
 
            else:
470
 
                mutter('commit parent ghost revision {%s}', revision)
471
455
 
 
456
    def _check_parents_present(self):
 
457
        for parent_id in self.parents:
 
458
            mutter('commit parent revision {%s}', parent_id)
 
459
            if not self.branch.repository.has_revision(parent_id):
 
460
                if parent_id == self.branch.last_revision():
 
461
                    warning("parent is missing %r", parent_id)
 
462
                    raise BzrCheckError("branch %s is missing revision {%s}"
 
463
                            % (self.branch, parent_id))
 
464
            
472
465
    def _remove_deleted(self):
473
466
        """Remove deleted files from the working inventories.
474
467
 
482
475
        """
483
476
        specific = self.specific_files
484
477
        deleted_ids = []
485
 
        deleted_paths = set()
486
478
        for path, ie in self.work_inv.iter_entries():
487
 
            if is_inside_any(deleted_paths, path):
488
 
                # The tree will delete the required ids recursively.
489
 
                continue
490
479
            if specific and not is_inside_any(specific, path):
491
480
                continue
492
481
            if not self.work_tree.has_filename(path):
493
 
                deleted_paths.add(path)
494
482
                self.reporter.missing(path)
495
 
                deleted_ids.append(ie.file_id)
496
 
        self.work_tree.unversion(deleted_ids)
 
483
                deleted_ids.append((path, ie.file_id))
 
484
        if deleted_ids:
 
485
            deleted_ids.sort(reverse=True)
 
486
            for path, file_id in deleted_ids:
 
487
                del self.work_inv[file_id]
 
488
            self.work_tree._write_inventory(self.work_inv)
497
489
 
498
490
    def _populate_new_inv(self):
499
491
        """Build revision inventory.
510
502
        # in bugs like #46635.  Any reason not to use/enhance Tree.changes_from?
511
503
        # ADHB 11-07-2006
512
504
        mutter("Selecting files for commit with filter %s", self.specific_files)
 
505
        # at this point we dont copy the root entry:
513
506
        entries = self.work_inv.iter_entries()
514
 
        if not self.builder.record_root_entry:
515
 
            symbol_versioning.warn('CommitBuilders should support recording'
516
 
                ' the root entry as of bzr 0.10.', DeprecationWarning, 
517
 
                stacklevel=1)
518
 
            self.builder.new_inventory.add(self.basis_inv.root.copy())
519
 
            entries.next()
520
 
            self._emit_progress_update()
 
507
        entries.next()
 
508
        self._emit_progress_update()
521
509
        for path, new_ie in entries:
522
510
            self._emit_progress_update()
523
511
            file_id = new_ie.file_id