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,
81
81
import bzrlib.config
82
82
import bzrlib.errors as errors
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
225
224
raise BzrError("The message keyword parameter is required for commit().")
227
226
self.weave_store = self.branch.repository.weave_store
227
self.bound_branch = None
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}
234
if revprops is not None:
233
235
self.revprops.update(revprops)
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)
240
# raise an exception as soon as we find a single unknown.
241
for unknown in self.work_tree.unknowns():
242
raise StrictCommitFailed()
244
if timestamp is None:
245
self.timestamp = time.time()
247
self.timestamp = long(timestamp)
249
if self.config is None:
250
self.config = bzrlib.config.BranchConfig(self.branch)
253
self.rev_id = _gen_revision_id(self.config, self.timestamp)
257
if committer is None:
258
self.committer = self.config.username()
260
assert isinstance(committer, basestring), type(committer)
261
self.committer = committer
264
self.timezone = local_time_offset()
266
self.timezone = int(timezone)
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()
274
self.branch.lock_write()
237
self.work_tree.lock_write()
239
# setup the bound branch variables as needed.
240
self._check_bound_branch()
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)
249
# raise an exception as soon as we find a single unknown.
250
for unknown in self.work_tree.unknowns():
251
raise StrictCommitFailed()
253
if timestamp is None:
254
self.timestamp = time.time()
256
self.timestamp = long(timestamp)
258
if self.config is None:
259
self.config = bzrlib.config.BranchConfig(self.branch)
262
self.rev_id = _gen_revision_id(self.config, self.timestamp)
266
if committer is None:
267
self.committer = self.config.username()
269
assert isinstance(committer, basestring), type(committer)
270
self.committer = committer
273
self.timezone = local_time_offset()
275
self.timezone = int(timezone)
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()
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
298
self._record_inventory()
305
self.inv_sha1 = self.branch.repository.add_inventory(
299
310
self._make_revision()
311
# revision data is in the local branch now.
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)
323
# and now do the commit locally.
324
self.branch.append_revision(self.rev_id)
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]
306
331
self.work_tree.set_last_revision(self.rev_id, precursor)
332
# now the work tree is up to date with the branch
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(' ')
315
342
'rev_id':self.rev_id})
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()
347
def _check_bound_branch(self):
348
"""Check to see if the local branch is bound.
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.
354
if self.local and not self.branch.get_bound_location():
355
raise errors.LocalRequiresBoundBranch()
358
self.master_branch = self.branch.get_master_branch()
360
if not self.master_branch:
361
# make this branch the reference branch for out of date checks.
362
self.master_branch = self.branch
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)
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
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,
383
# Now things are ready to change the master branch
385
self.bound_branch = self.branch
386
self.master_branch.lock_write()
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)
396
def _cleanup_bound_branch(self):
397
"""Executed at the end of a try/finally to cleanup a bound branch.
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
403
if not self.bound_branch:
405
self.master_branch.unlock()
328
407
def _escape_commit_message(self):
329
408
"""Replace xml-incompatible control characters."""
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
378
serializer_v5.write_revision(self.rev, rev_tmp)
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)
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,