/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: Martin Pool
  • Date: 2006-03-06 11:20:10 UTC
  • mfrom: (1593 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1611.
  • Revision ID: mbp@sourcefrog.net-20060306112010-17c0170dde5d1eea
[merge] large merge to sync with bzr.dev

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