/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

Merge from bzr.ab.integration

Show diffs side-by-side

added added

removed removed

Lines of Context:
76
76
from bzrlib.osutils import (local_time_offset,
77
77
                            rand_bytes, compact_date,
78
78
                            kind_marker, is_inside_any, quotefn,
79
 
                            sha_string, sha_strings, sha_file, isdir, isfile,
 
79
                            sha_file, isdir, isfile,
80
80
                            split_lines)
81
81
import bzrlib.config
82
82
import bzrlib.errors as errors
85
85
                           ConflictsInTree,
86
86
                           StrictCommitFailed
87
87
                           )
88
 
import bzrlib.gpg as gpg
89
88
from bzrlib.revision import Revision
90
89
from bzrlib.testament import Testament
91
90
from bzrlib.trace import mutter, note, warning
92
91
from bzrlib.xml5 import serializer_v5
93
92
from bzrlib.inventory import Inventory, ROOT_ID
94
93
from bzrlib.symbol_versioning import *
95
 
from bzrlib.weave import Weave
96
 
from bzrlib.weavefile import read_weave, write_weave_v5
97
94
from bzrlib.workingtree import WorkingTree
98
95
 
99
96
 
181
178
               strict=False,
182
179
               verbose=False,
183
180
               revprops=None,
184
 
               working_tree=None):
 
181
               working_tree=None,
 
182
               local=False):
185
183
        """Commit working copy as a new revision.
186
184
 
187
185
        branch -- the deprecated branch to commit to. New callers should pass in 
207
205
            contains unknown files.
208
206
 
209
207
        revprops -- Properties for new revision
 
208
        :param local: Perform a local only commit.
210
209
        """
211
210
        mutter('preparing to commit')
212
211
 
225
224
            raise BzrError("The message keyword parameter is required for commit().")
226
225
 
227
226
        self.weave_store = self.branch.repository.weave_store
 
227
        self.bound_branch = None
 
228
        self.local = local
 
229
        self.master_branch = None
228
230
        self.rev_id = rev_id
229
231
        self.specific_files = specific_files
230
232
        self.allow_pointless = allow_pointless
231
 
        self.revprops = {'branch-nick': self.branch.nick}
232
 
        if revprops:
 
233
        self.revprops = {}
 
234
        if revprops is not None:
233
235
            self.revprops.update(revprops)
234
236
 
235
 
        # check for out of date working trees
236
 
        if self.work_tree.last_revision() != self.branch.last_revision():
237
 
            raise errors.OutOfDateTree(self.work_tree)
238
 
 
239
 
        if strict:
240
 
            # raise an exception as soon as we find a single unknown.
241
 
            for unknown in self.work_tree.unknowns():
242
 
                raise StrictCommitFailed()
243
 
 
244
 
        if timestamp is None:
245
 
            self.timestamp = time.time()
246
 
        else:
247
 
            self.timestamp = long(timestamp)
248
 
            
249
 
        if self.config is None:
250
 
            self.config = bzrlib.config.BranchConfig(self.branch)
251
 
 
252
 
        if rev_id is None:
253
 
            self.rev_id = _gen_revision_id(self.config, self.timestamp)
254
 
        else:
255
 
            self.rev_id = rev_id
256
 
 
257
 
        if committer is None:
258
 
            self.committer = self.config.username()
259
 
        else:
260
 
            assert isinstance(committer, basestring), type(committer)
261
 
            self.committer = committer
262
 
 
263
 
        if timezone is None:
264
 
            self.timezone = local_time_offset()
265
 
        else:
266
 
            self.timezone = int(timezone)
267
 
 
268
 
        if isinstance(message, str):
269
 
            message = message.decode(bzrlib.user_encoding)
270
 
        assert isinstance(message, unicode), type(message)
271
 
        self.message = message
272
 
        self._escape_commit_message()
273
 
 
274
 
        self.branch.lock_write()
 
237
        self.work_tree.lock_write()
275
238
        try:
 
239
            # setup the bound branch variables as needed.
 
240
            self._check_bound_branch()
 
241
 
 
242
            # check for out of date working trees
 
243
            # if we are bound, then self.branch is the master branch and this
 
244
            # test is thus all we need.
 
245
            if self.work_tree.last_revision() != self.master_branch.last_revision():
 
246
                raise errors.OutOfDateTree(self.work_tree)
 
247
    
 
248
            if strict:
 
249
                # raise an exception as soon as we find a single unknown.
 
250
                for unknown in self.work_tree.unknowns():
 
251
                    raise StrictCommitFailed()
 
252
    
 
253
            if timestamp is None:
 
254
                self.timestamp = time.time()
 
255
            else:
 
256
                self.timestamp = long(timestamp)
 
257
                
 
258
            if self.config is None:
 
259
                self.config = bzrlib.config.BranchConfig(self.branch)
 
260
    
 
261
            if rev_id is None:
 
262
                self.rev_id = _gen_revision_id(self.config, self.timestamp)
 
263
            else:
 
264
                self.rev_id = rev_id
 
265
    
 
266
            if committer is None:
 
267
                self.committer = self.config.username()
 
268
            else:
 
269
                assert isinstance(committer, basestring), type(committer)
 
270
                self.committer = committer
 
271
    
 
272
            if timezone is None:
 
273
                self.timezone = local_time_offset()
 
274
            else:
 
275
                self.timezone = int(timezone)
 
276
    
 
277
            if isinstance(message, str):
 
278
                message = message.decode(bzrlib.user_encoding)
 
279
            assert isinstance(message, unicode), type(message)
 
280
            self.message = message
 
281
            self._escape_commit_message()
 
282
 
276
283
            self.work_inv = self.work_tree.inventory
277
284
            self.basis_tree = self.work_tree.basis_tree()
278
285
            self.basis_inv = self.basis_tree.inventory
295
302
            if len(list(self.work_tree.iter_conflicts()))>0:
296
303
                raise ConflictsInTree
297
304
 
298
 
            self._record_inventory()
 
305
            self.inv_sha1 = self.branch.repository.add_inventory(
 
306
                self.rev_id,
 
307
                self.new_inv,
 
308
                self.present_parents
 
309
                )
299
310
            self._make_revision()
 
311
            # revision data is in the local branch now.
 
312
            
 
313
            # upload revision data to the master.
 
314
            # this will propogate merged revisions too if needed.
 
315
            if self.bound_branch:
 
316
                self.master_branch.repository.fetch(self.branch.repository,
 
317
                                                    revision_id=self.rev_id)
 
318
                # now the master has the revision data
 
319
                # 'commit' to the master first so a timeout here causes the local
 
320
                # branch to be out of date
 
321
                self.master_branch.append_revision(self.rev_id)
 
322
 
 
323
            # and now do the commit locally.
 
324
            self.branch.append_revision(self.rev_id)
 
325
 
300
326
            self.work_tree.set_pending_merges([])
301
 
            self.branch.append_revision(self.rev_id)
302
327
            if len(self.parents):
303
328
                precursor = self.parents[0]
304
329
            else:
305
330
                precursor = None
306
331
            self.work_tree.set_last_revision(self.rev_id, precursor)
 
332
            # now the work tree is up to date with the branch
 
333
            
307
334
            self.reporter.completed(self.branch.revno()+1, self.rev_id)
308
335
            if self.config.post_commit() is not None:
309
336
                hooks = self.config.post_commit().split(' ')
314
341
                                   'bzrlib':bzrlib,
315
342
                                   'rev_id':self.rev_id})
316
343
        finally:
317
 
            self.branch.unlock()
318
 
 
319
 
    def _record_inventory(self):
320
 
        """Store the inventory for the new revision."""
321
 
        inv_text = serializer_v5.write_inventory_to_string(self.new_inv)
322
 
        self.inv_sha1 = sha_string(inv_text)
323
 
        s = self.branch.repository.control_weaves
324
 
        s.add_text('inventory', self.rev_id,
325
 
                   split_lines(inv_text), self.present_parents,
326
 
                   self.branch.get_transaction())
 
344
            self._cleanup_bound_branch()
 
345
            self.work_tree.unlock()
 
346
 
 
347
    def _check_bound_branch(self):
 
348
        """Check to see if the local branch is bound.
 
349
 
 
350
        If it is bound, then most of the commit will actually be
 
351
        done using the remote branch as the target branch.
 
352
        Only at the end will the local branch be updated.
 
353
        """
 
354
        if self.local and not self.branch.get_bound_location():
 
355
            raise errors.LocalRequiresBoundBranch()
 
356
 
 
357
        if not self.local:
 
358
            self.master_branch = self.branch.get_master_branch()
 
359
 
 
360
        if not self.master_branch:
 
361
            # make this branch the reference branch for out of date checks.
 
362
            self.master_branch = self.branch
 
363
            return
 
364
 
 
365
        # If the master branch is bound, we must fail
 
366
        master_bound_location = self.master_branch.get_bound_location()
 
367
        if master_bound_location:
 
368
            raise errors.CommitToDoubleBoundBranch(self.branch,
 
369
                    self.master_branch, master_bound_location)
 
370
 
 
371
        # TODO: jam 20051230 We could automatically push local
 
372
        #       commits to the remote branch if they would fit.
 
373
        #       But for now, just require remote to be identical
 
374
        #       to local.
 
375
        
 
376
        # Make sure the local branch is identical to the master
 
377
        master_rh = self.master_branch.revision_history()
 
378
        local_rh = self.branch.revision_history()
 
379
        if local_rh != master_rh:
 
380
            raise errors.BoundBranchOutOfDate(self.branch,
 
381
                    self.master_branch)
 
382
 
 
383
        # Now things are ready to change the master branch
 
384
        # so grab the lock
 
385
        self.bound_branch = self.branch
 
386
        self.master_branch.lock_write()
 
387
####        
 
388
####        # Check to see if we have any pending merges. If we do
 
389
####        # those need to be pushed into the master branch
 
390
####        pending_merges = self.work_tree.pending_merges()
 
391
####        if pending_merges:
 
392
####            for revision_id in pending_merges:
 
393
####                self.master_branch.repository.fetch(self.bound_branch.repository,
 
394
####                                                    revision_id=revision_id)
 
395
 
 
396
    def _cleanup_bound_branch(self):
 
397
        """Executed at the end of a try/finally to cleanup a bound branch.
 
398
 
 
399
        If the branch wasn't bound, this is a no-op.
 
400
        If it was, it resents self.branch to the local branch, instead
 
401
        of being the master.
 
402
        """
 
403
        if not self.bound_branch:
 
404
            return
 
405
        self.master_branch.unlock()
327
406
 
328
407
    def _escape_commit_message(self):
329
408
        """Replace xml-incompatible control characters."""
366
445
            
367
446
    def _make_revision(self):
368
447
        """Record a new revision object for this commit."""
369
 
        self.rev = Revision(timestamp=self.timestamp,
370
 
                            timezone=self.timezone,
371
 
                            committer=self.committer,
372
 
                            message=self.message,
373
 
                            inventory_sha1=self.inv_sha1,
374
 
                            revision_id=self.rev_id,
375
 
                            properties=self.revprops)
376
 
        self.rev.parent_ids = self.parents
377
 
        rev_tmp = StringIO()
378
 
        serializer_v5.write_revision(self.rev, rev_tmp)
379
 
        rev_tmp.seek(0)
380
 
        if self.config.signature_needed():
381
 
            plaintext = Testament(self.rev, self.new_inv).as_short_text()
382
 
            self.branch.repository.store_revision_signature(
383
 
                gpg.GPGStrategy(self.config), plaintext, self.rev_id)
384
 
        self.branch.repository.revision_store.add(rev_tmp, self.rev_id)
385
 
        mutter('new revision_id is {%s}', self.rev_id)
 
448
        rev = Revision(timestamp=self.timestamp,
 
449
                       timezone=self.timezone,
 
450
                       committer=self.committer,
 
451
                       message=self.message,
 
452
                       inventory_sha1=self.inv_sha1,
 
453
                       revision_id=self.rev_id,
 
454
                       properties=self.revprops)
 
455
        rev.parent_ids = self.parents
 
456
        self.branch.repository.add_revision(self.rev_id, rev, self.new_inv, self.config)
386
457
 
387
458
    def _remove_deleted(self):
388
459
        """Remove deleted files from the working inventories.
422
493
        for path, ie in self.new_inv.iter_entries():
423
494
            previous_entries = ie.find_previous_heads(
424
495
                self.parent_invs, 
425
 
                self.weave_store.get_weave_prelude_or_empty(ie.file_id,
 
496
                self.weave_store.get_weave_or_empty(ie.file_id,
426
497
                    self.branch.get_transaction()))
427
498
            if ie.revision is None:
428
499
                change = ie.snapshot(self.rev_id, path, previous_entries,