/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/commit.py

  • Committer: John Arbash Meinel
  • Date: 2006-09-13 02:09:37 UTC
  • mto: This revision was merged to the branch mainline in revision 2004.
  • Revision ID: john@arbash-meinel.com-20060913020937-2df2f49f9a28ec43
Update HACKING and docstrings

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
 
 
18
# XXX: Can we do any better about making interrupted commits change
 
19
# nothing?  
 
20
 
 
21
# TODO: Separate 'prepare' phase where we find a list of potentially
 
22
# committed files.  We then can then pause the commit to prompt for a
 
23
# commit message, knowing the summary will be the same as what's
 
24
# actually used for the commit.  (But perhaps simpler to simply get
 
25
# the tree status, then use that for a selective commit?)
 
26
 
18
27
# The newly committed revision is going to have a shape corresponding
19
28
# to that of the working inventory.  Files that are not in the
20
29
# working tree and that were in the predecessor are reported as
46
55
# merges from, then it should still be reported as newly added
47
56
# relative to the basis revision.
48
57
 
49
 
# TODO: Change the parameter 'rev_id' to 'revision_id' to be consistent with
50
 
# the rest of the code; add a deprecation of the old name.
 
58
# TODO: Do checks that the tree can be committed *before* running the 
 
59
# editor; this should include checks for a pointless commit and for 
 
60
# unknown or missing files.
 
61
 
 
62
# TODO: If commit fails, leave the message in a file somewhere.
 
63
 
51
64
 
52
65
import os
53
66
import re
56
69
 
57
70
from cStringIO import StringIO
58
71
 
59
 
from bzrlib import (
60
 
    errors,
61
 
    inventory,
62
 
    tree,
63
 
    )
64
 
from bzrlib.branch import Branch
65
72
import bzrlib.config
 
73
import bzrlib.errors as errors
66
74
from bzrlib.errors import (BzrError, PointlessCommit,
67
75
                           ConflictsInTree,
68
76
                           StrictCommitFailed
73
81
from bzrlib.testament import Testament
74
82
from bzrlib.trace import mutter, note, warning
75
83
from bzrlib.xml5 import serializer_v5
76
 
from bzrlib.inventory import Inventory, InventoryEntry
 
84
from bzrlib.inventory import Inventory, ROOT_ID, InventoryEntry
77
85
from bzrlib import symbol_versioning
78
86
from bzrlib.symbol_versioning import (deprecated_passed,
79
87
        deprecated_function,
80
88
        DEPRECATED_PARAMETER)
81
89
from bzrlib.workingtree import WorkingTree
82
 
import bzrlib.ui
83
90
 
84
91
 
85
92
class NullCommitReporter(object):
114
121
    def snapshot_change(self, change, path):
115
122
        if change == 'unchanged':
116
123
            return
117
 
        if change == 'added' and path == '':
118
 
            return
119
124
        note("%s %s", change, path)
120
125
 
121
126
    def completed(self, revno, rev_id):
159
164
            self.config = None
160
165
        
161
166
    def commit(self,
162
 
               message=None,
 
167
               branch=DEPRECATED_PARAMETER, message=None,
163
168
               timestamp=None,
164
169
               timezone=None,
165
170
               committer=None,
172
177
               working_tree=None,
173
178
               local=False,
174
179
               reporter=None,
175
 
               config=None,
176
 
               message_callback=None,
177
 
               recursive='down'):
 
180
               config=None):
178
181
        """Commit working copy as a new revision.
179
182
 
180
 
        message -- the commit message (it or message_callback is required)
 
183
        branch -- the deprecated branch to commit to. New callers should pass in 
 
184
                  working_tree instead
 
185
 
 
186
        message -- the commit message, a mandatory parameter
181
187
 
182
188
        timestamp -- if not None, seconds-since-epoch for a
183
189
             postdated/predated commit.
198
204
 
199
205
        revprops -- Properties for new revision
200
206
        :param local: Perform a local only commit.
201
 
        :param recursive: If set to 'down', commit in any subtrees that have
202
 
            pending changes of any sort during this commit.
203
207
        """
204
208
        mutter('preparing to commit')
205
209
 
206
 
        if working_tree is None:
207
 
            raise BzrError("working_tree must be passed into commit().")
 
210
        if deprecated_passed(branch):
 
211
            symbol_versioning.warn("Commit.commit (branch, ...): The branch parameter is "
 
212
                 "deprecated as of bzr 0.8. Please use working_tree= instead.",
 
213
                 DeprecationWarning, stacklevel=2)
 
214
            self.branch = branch
 
215
            self.work_tree = self.branch.bzrdir.open_workingtree()
 
216
        elif working_tree is None:
 
217
            raise BzrError("One of branch and working_tree must be passed into commit().")
208
218
        else:
209
219
            self.work_tree = working_tree
210
220
            self.branch = self.work_tree.branch
211
 
            if getattr(self.work_tree, 'requires_rich_root', lambda: False)():
212
 
                if not self.branch.repository.supports_rich_root():
213
 
                    raise errors.RootNotRich()
214
 
        if message_callback is None:
215
 
            if message is not None:
216
 
                if isinstance(message, str):
217
 
                    message = message.decode(bzrlib.user_encoding)
218
 
                message_callback = lambda x: message
219
 
            else:
220
 
                raise BzrError("The message or message_callback keyword"
221
 
                               " parameter is required for commit().")
 
221
        if message is None:
 
222
            raise BzrError("The message keyword parameter is required for commit().")
222
223
 
223
224
        self.bound_branch = None
224
225
        self.local = local
227
228
        self.rev_id = None
228
229
        self.specific_files = specific_files
229
230
        self.allow_pointless = allow_pointless
230
 
        self.recursive = recursive
231
 
        self.revprops = revprops
232
 
        self.message_callback = message_callback
233
 
        self.timestamp = timestamp
234
 
        self.timezone = timezone
235
 
        self.committer = committer
236
 
        self.specific_files = specific_files
237
 
        self.strict = strict
238
 
        self.verbose = verbose
239
 
        self.local = local
240
231
 
241
232
        if reporter is None and self.reporter is None:
242
233
            self.reporter = NullCommitReporter()
245
236
 
246
237
        self.work_tree.lock_write()
247
238
        self.pb = bzrlib.ui.ui_factory.nested_progress_bar()
248
 
        self.basis_tree = self.work_tree.basis_tree()
249
 
        self.basis_tree.lock_read()
250
239
        try:
251
240
            # Cannot commit with conflicts present.
252
241
            if len(self.work_tree.conflicts())>0:
263
252
                # this is so that we still consier the master branch
264
253
                # - in a checkout scenario the tree may have no
265
254
                # parents but the branch may do.
266
 
                first_tree_parent = bzrlib.revision.NULL_REVISION
267
 
            old_revno, master_last = self.master_branch.last_revision_info()
268
 
            if master_last != first_tree_parent:
269
 
                if master_last != bzrlib.revision.NULL_REVISION:
270
 
                    raise errors.OutOfDateTree(self.work_tree)
271
 
            if self.branch.repository.has_revision(first_tree_parent):
272
 
                new_revno = old_revno + 1
273
 
            else:
274
 
                # ghost parents never appear in revision history.
275
 
                new_revno = 1
 
255
                first_tree_parent = None
 
256
            master_last = self.master_branch.last_revision()
 
257
            if (master_last is not None and
 
258
                master_last != first_tree_parent):
 
259
                raise errors.OutOfDateTree(self.work_tree)
 
260
    
276
261
            if strict:
277
262
                # raise an exception as soon as we find a single unknown.
278
263
                for unknown in self.work_tree.unknowns():
280
265
                   
281
266
            if self.config is None:
282
267
                self.config = self.branch.get_config()
 
268
      
 
269
            if isinstance(message, str):
 
270
                message = message.decode(bzrlib.user_encoding)
 
271
            assert isinstance(message, unicode), type(message)
 
272
            self.message = message
 
273
            self._escape_commit_message()
283
274
 
284
275
            self.work_inv = self.work_tree.inventory
 
276
            self.basis_tree = self.work_tree.basis_tree()
285
277
            self.basis_inv = self.basis_tree.inventory
286
 
            if specific_files is not None:
287
 
                # Ensure specified files are versioned
288
 
                # (We don't actually need the ids here)
289
 
                # XXX: Dont we have filter_unversioned to do this more
290
 
                # cheaply?
291
 
                tree.find_ids_across_trees(specific_files,
292
 
                                           [self.basis_tree, self.work_tree])
293
278
            # one to finish, one for rev and inventory, and one for each
294
279
            # inventory entry, and the same for the new inventory.
295
280
            # note that this estimate is too long when we do a partial tree
303
288
                raise NotImplementedError('selected-file commit of merges is not supported yet: files %r',
304
289
                        self.specific_files)
305
290
            
306
 
            self.builder = self.branch.get_commit_builder(self.parents,
 
291
            self.builder = self.branch.get_commit_builder(self.parents, 
307
292
                self.config, timestamp, timezone, committer, revprops, rev_id)
308
293
            
309
294
            self._remove_deleted()
320
305
            # that commit will succeed.
321
306
            self.builder.finish_inventory()
322
307
            self._emit_progress_update()
323
 
            message = message_callback(self)
324
 
            assert isinstance(message, unicode), type(message)
325
 
            self.message = message
326
 
            self._escape_commit_message()
327
 
 
328
308
            self.rev_id = self.builder.commit(self.message)
329
309
            self._emit_progress_update()
330
310
            # revision data is in the local branch now.
337
317
                # now the master has the revision data
338
318
                # 'commit' to the master first so a timeout here causes the local
339
319
                # branch to be out of date
340
 
                self.master_branch.set_last_revision_info(new_revno,
341
 
                                                          self.rev_id)
 
320
                self.master_branch.append_revision(self.rev_id)
342
321
 
343
322
            # and now do the commit locally.
344
 
            self.branch.set_last_revision_info(new_revno, self.rev_id)
 
323
            self.branch.append_revision(self.rev_id)
345
324
 
346
 
            rev_tree = self.builder.revision_tree()
347
 
            self.work_tree.set_parent_trees([(self.rev_id, rev_tree)])
 
325
            # if the builder gave us the revisiontree it created back, we
 
326
            # could use it straight away here.
 
327
            # TODO: implement this.
 
328
            self.work_tree.set_parent_trees([(self.rev_id,
 
329
                self.branch.repository.revision_tree(self.rev_id))])
348
330
            # now the work tree is up to date with the branch
349
331
            
350
 
            self.reporter.completed(new_revno, self.rev_id)
351
 
            # old style commit hooks - should be deprecated ? (obsoleted in
352
 
            # 0.15)
 
332
            self.reporter.completed(self.branch.revno(), self.rev_id)
353
333
            if self.config.post_commit() is not None:
354
334
                hooks = self.config.post_commit().split(' ')
355
335
                # this would be nicer with twisted.python.reflect.namedAny
358
338
                                  {'branch':self.branch,
359
339
                                   'bzrlib':bzrlib,
360
340
                                   'rev_id':self.rev_id})
361
 
            # new style commit hooks:
362
 
            if not self.bound_branch:
363
 
                hook_master = self.branch
364
 
                hook_local = None
365
 
            else:
366
 
                hook_master = self.master_branch
367
 
                hook_local = self.branch
368
 
            # With bound branches, when the master is behind the local branch,
369
 
            # the 'old_revno' and old_revid values here are incorrect.
370
 
            # XXX: FIXME ^. RBC 20060206
371
 
            if self.parents:
372
 
                old_revid = self.parents[0]
373
 
            else:
374
 
                old_revid = bzrlib.revision.NULL_REVISION
375
 
            for hook in Branch.hooks['post_commit']:
376
 
                hook(hook_local, hook_master, old_revno, old_revid, new_revno,
377
 
                    self.rev_id)
378
341
            self._emit_progress_update()
379
342
        finally:
380
343
            self._cleanup()
381
344
        return self.rev_id
382
345
 
383
 
    def _any_real_changes(self):
384
 
        """Are there real changes between new_inventory and basis?
385
 
 
386
 
        For trees without rich roots, inv.root.revision changes every commit.
387
 
        But if that is the only change, we want to treat it as though there
388
 
        are *no* changes.
389
 
        """
390
 
        new_entries = self.builder.new_inventory.iter_entries()
391
 
        basis_entries = self.basis_inv.iter_entries()
392
 
        new_path, new_root_ie = new_entries.next()
393
 
        basis_path, basis_root_ie = basis_entries.next()
394
 
 
395
 
        # This is a copy of InventoryEntry.__eq__ only leaving out .revision
396
 
        def ie_equal_no_revision(this, other):
397
 
            return ((this.file_id == other.file_id)
398
 
                    and (this.name == other.name)
399
 
                    and (this.symlink_target == other.symlink_target)
400
 
                    and (this.text_sha1 == other.text_sha1)
401
 
                    and (this.text_size == other.text_size)
402
 
                    and (this.text_id == other.text_id)
403
 
                    and (this.parent_id == other.parent_id)
404
 
                    and (this.kind == other.kind)
405
 
                    and (this.executable == other.executable)
406
 
                    and (this.reference_revision == other.reference_revision)
407
 
                    )
408
 
        if not ie_equal_no_revision(new_root_ie, basis_root_ie):
409
 
            return True
410
 
 
411
 
        for new_ie, basis_ie in zip(new_entries, basis_entries):
412
 
            if new_ie != basis_ie:
413
 
                return True
414
 
 
415
 
        # No actual changes present
416
 
        return False
417
 
 
418
346
    def _check_pointless(self):
419
347
        if self.allow_pointless:
420
348
            return
423
351
            return
424
352
        # work around the fact that a newly-initted tree does differ from its
425
353
        # basis
426
 
        if len(self.basis_inv) == 0 and len(self.builder.new_inventory) == 1:
427
 
            raise PointlessCommit()
428
 
        # Shortcut, if the number of entries changes, then we obviously have
429
 
        # a change
430
354
        if len(self.builder.new_inventory) != len(self.basis_inv):
431
355
            return
432
 
        # If length == 1, then we only have the root entry. Which means
433
 
        # that there is no real difference (only the root could be different)
434
 
        if (len(self.builder.new_inventory) != 1 and self._any_real_changes()):
 
356
        if (len(self.builder.new_inventory) != 1 and
 
357
            self.builder.new_inventory != self.basis_inv):
435
358
            return
436
359
        raise PointlessCommit()
437
360
 
465
388
        #       to local.
466
389
        
467
390
        # Make sure the local branch is identical to the master
468
 
        master_info = self.master_branch.last_revision_info()
469
 
        local_info = self.branch.last_revision_info()
470
 
        if local_info != master_info:
 
391
        master_rh = self.master_branch.revision_history()
 
392
        local_rh = self.branch.revision_history()
 
393
        if local_rh != master_rh:
471
394
            raise errors.BoundBranchOutOfDate(self.branch,
472
395
                    self.master_branch)
473
396
 
480
403
    def _cleanup(self):
481
404
        """Cleanup any open locks, progress bars etc."""
482
405
        cleanups = [self._cleanup_bound_branch,
483
 
                    self.basis_tree.unlock,
484
406
                    self.work_tree.unlock,
485
407
                    self.pb.finished]
486
408
        found_exception = None
536
458
        # TODO: Make sure that this list doesn't contain duplicate 
537
459
        # entries and the order is preserved when doing this.
538
460
        self.parents = self.work_tree.get_parent_ids()
539
 
        self.parent_invs = [self.basis_inv]
540
 
        for revision in self.parents[1:]:
 
461
        self.parent_invs = []
 
462
        for revision in self.parents:
541
463
            if self.branch.repository.has_revision(revision):
542
464
                mutter('commit parent revision {%s}', revision)
543
465
                inventory = self.branch.repository.get_inventory(revision)
586
508
        # in bugs like #46635.  Any reason not to use/enhance Tree.changes_from?
587
509
        # ADHB 11-07-2006
588
510
        mutter("Selecting files for commit with filter %s", self.specific_files)
589
 
        assert self.work_inv.root is not None
590
511
        entries = self.work_inv.iter_entries()
591
512
        if not self.builder.record_root_entry:
592
513
            symbol_versioning.warn('CommitBuilders should support recording'
598
519
        for path, new_ie in entries:
599
520
            self._emit_progress_update()
600
521
            file_id = new_ie.file_id
601
 
            try:
602
 
                kind = self.work_tree.kind(file_id)
603
 
                if kind == 'tree-reference' and self.recursive == 'down':
604
 
                    # nested tree: commit in it
605
 
                    sub_tree = WorkingTree.open(self.work_tree.abspath(path))
606
 
                    # FIXME: be more comprehensive here:
607
 
                    # this works when both trees are in --trees repository,
608
 
                    # but when both are bound to a different repository,
609
 
                    # it fails; a better way of approaching this is to 
610
 
                    # finally implement the explicit-caches approach design
611
 
                    # a while back - RBC 20070306.
612
 
                    if (sub_tree.branch.repository.bzrdir.root_transport.base
613
 
                        ==
614
 
                        self.work_tree.branch.repository.bzrdir.root_transport.base):
615
 
                        sub_tree.branch.repository = \
616
 
                            self.work_tree.branch.repository
617
 
                    try:
618
 
                        sub_tree.commit(message=None, revprops=self.revprops,
619
 
                            recursive=self.recursive,
620
 
                            message_callback=self.message_callback,
621
 
                            timestamp=self.timestamp, timezone=self.timezone,
622
 
                            committer=self.committer,
623
 
                            allow_pointless=self.allow_pointless,
624
 
                            strict=self.strict, verbose=self.verbose,
625
 
                            local=self.local, reporter=self.reporter)
626
 
                    except errors.PointlessCommit:
627
 
                        pass
628
 
                if kind != new_ie.kind:
629
 
                    new_ie = inventory.make_entry(kind, new_ie.name,
630
 
                                                  new_ie.parent_id, file_id)
631
 
            except errors.NoSuchFile:
632
 
                pass
633
522
            # mutter('check %s {%s}', path, file_id)
634
523
            if (not self.specific_files or 
635
524
                is_inside_or_parent_of_any(self.specific_files, path)):
643
532
                else:
644
533
                    # this entry is new and not being committed
645
534
                    continue
 
535
 
646
536
            self.builder.record_entry_contents(ie, self.parent_invs, 
647
537
                path, self.work_tree)
648
538
            # describe the nature of the change that has occurred relative to