283
284
:param lossy: When committing to a foreign VCS, ignore any
284
285
data that can not be natively represented.
286
with ExitStack() as stack:
287
self.revprops = revprops or {}
288
# XXX: Can be set on __init__ or passed in - this is a bit ugly.
289
self.config_stack = config or self.config_stack
290
mutter('preparing to commit')
292
if working_tree is None:
293
raise BzrError("working_tree must be passed into commit().")
295
self.work_tree = working_tree
296
self.branch = self.work_tree.branch
297
if getattr(self.work_tree, 'requires_rich_root', lambda: False)():
298
if not self.branch.repository.supports_rich_root():
299
raise errors.RootNotRich()
300
if message_callback is None:
301
if message is not None:
302
if isinstance(message, bytes):
303
message = message.decode(get_user_encoding())
305
def message_callback(x):
308
raise BzrError("The message or message_callback keyword"
309
" parameter is required for commit().")
311
self.bound_branch = None
312
self.any_entries_deleted = False
313
if exclude is not None:
314
self.exclude = sorted(
315
minimum_path_selection(exclude))
319
self.master_branch = None
320
self.recursive = recursive
322
# self.specific_files is None to indicate no filter, or any iterable to
323
# indicate a filter - [] means no files at all, as per iter_changes.
324
if specific_files is not None:
325
self.specific_files = sorted(
326
minimum_path_selection(specific_files))
328
self.specific_files = None
330
self.allow_pointless = allow_pointless
331
self.message_callback = message_callback
332
self.timestamp = timestamp
333
self.timezone = timezone
334
self.committer = committer
336
self.verbose = verbose
338
stack.enter_context(self.work_tree.lock_write())
339
self.parents = self.work_tree.get_parent_ids()
340
self.pb = ui.ui_factory.nested_progress_bar()
341
stack.callback(self.pb.finished)
342
self.basis_revid = self.work_tree.last_revision()
343
self.basis_tree = self.work_tree.basis_tree()
344
stack.enter_context(self.basis_tree.lock_read())
345
# Cannot commit with conflicts present.
346
if len(self.work_tree.conflicts()) > 0:
347
raise ConflictsInTree
349
# Setup the bound branch variables as needed.
350
self._check_bound_branch(stack, possible_master_transports)
351
if self.config_stack is None:
352
self.config_stack = self.work_tree.get_config_stack()
354
# Check that the working tree is up to date
355
old_revno, old_revid, new_revno = self._check_out_of_date_tree()
357
# Complete configuration setup
358
if reporter is not None:
359
self.reporter = reporter
360
elif self.reporter is None:
361
self.reporter = self._select_reporter()
363
# Setup the progress bar. As the number of files that need to be
364
# committed in unknown, progress is reported as stages.
365
# We keep track of entries separately though and include that
366
# information in the progress bar during the relevant stages.
367
self.pb_stage_name = ""
368
self.pb_stage_count = 0
369
self.pb_stage_total = 5
287
operation = OperationWithCleanups(self._commit)
288
self.revprops = revprops or {}
289
# XXX: Can be set on __init__ or passed in - this is a bit ugly.
290
self.config_stack = config or self.config_stack
291
return operation.run(
296
specific_files=specific_files,
298
allow_pointless=allow_pointless,
301
working_tree=working_tree,
304
message_callback=message_callback,
307
possible_master_transports=possible_master_transports,
310
def _commit(self, operation, message, timestamp, timezone, committer,
311
specific_files, rev_id, allow_pointless, strict, verbose,
312
working_tree, local, reporter, message_callback, recursive,
313
exclude, possible_master_transports, lossy):
314
mutter('preparing to commit')
316
if working_tree is None:
317
raise BzrError("working_tree must be passed into commit().")
319
self.work_tree = working_tree
320
self.branch = self.work_tree.branch
321
if getattr(self.work_tree, 'requires_rich_root', lambda: False)():
322
if not self.branch.repository.supports_rich_root():
323
raise errors.RootNotRich()
324
if message_callback is None:
325
if message is not None:
326
if isinstance(message, bytes):
327
message = message.decode(get_user_encoding())
328
message_callback = lambda x: message
330
raise BzrError("The message or message_callback keyword"
331
" parameter is required for commit().")
333
self.bound_branch = None
334
self.any_entries_deleted = False
335
if exclude is not None:
336
self.exclude = sorted(
337
minimum_path_selection(exclude))
341
self.master_branch = None
342
self.recursive = recursive
344
# self.specific_files is None to indicate no filter, or any iterable to
345
# indicate a filter - [] means no files at all, as per iter_changes.
346
if specific_files is not None:
347
self.specific_files = sorted(
348
minimum_path_selection(specific_files))
350
self.specific_files = None
352
self.allow_pointless = allow_pointless
353
self.message_callback = message_callback
354
self.timestamp = timestamp
355
self.timezone = timezone
356
self.committer = committer
358
self.verbose = verbose
360
self.work_tree.lock_write()
361
operation.add_cleanup(self.work_tree.unlock)
362
self.parents = self.work_tree.get_parent_ids()
363
self.pb = ui.ui_factory.nested_progress_bar()
364
operation.add_cleanup(self.pb.finished)
365
self.basis_revid = self.work_tree.last_revision()
366
self.basis_tree = self.work_tree.basis_tree()
367
self.basis_tree.lock_read()
368
operation.add_cleanup(self.basis_tree.unlock)
369
# Cannot commit with conflicts present.
370
if len(self.work_tree.conflicts()) > 0:
371
raise ConflictsInTree
373
# Setup the bound branch variables as needed.
374
self._check_bound_branch(operation, possible_master_transports)
376
# Check that the working tree is up to date
377
old_revno, old_revid, new_revno = self._check_out_of_date_tree()
379
# Complete configuration setup
380
if reporter is not None:
381
self.reporter = reporter
382
elif self.reporter is None:
383
self.reporter = self._select_reporter()
384
if self.config_stack is None:
385
self.config_stack = self.work_tree.get_config_stack()
387
# Setup the progress bar. As the number of files that need to be
388
# committed in unknown, progress is reported as stages.
389
# We keep track of entries separately though and include that
390
# information in the progress bar during the relevant stages.
391
self.pb_stage_name = ""
392
self.pb_stage_count = 0
393
self.pb_stage_total = 5
394
if self.bound_branch:
395
# 2 extra stages: "Uploading data to master branch" and "Merging
396
# tags to master branch"
397
self.pb_stage_total += 2
398
self.pb.show_pct = False
399
self.pb.show_spinner = False
400
self.pb.show_eta = False
401
self.pb.show_count = True
402
self.pb.show_bar = True
404
# After a merge, a selected file commit is not supported.
405
# See 'bzr help merge' for an explanation as to why.
406
if len(self.parents) > 1 and self.specific_files is not None:
407
raise CannotCommitSelectedFileMerge(self.specific_files)
408
# Excludes are a form of selected file commit.
409
if len(self.parents) > 1 and self.exclude:
410
raise CannotCommitSelectedFileMerge(self.exclude)
412
# Collect the changes
413
self._set_progress_stage("Collecting changes", counter=True)
415
self.builder = self.branch.get_commit_builder(self.parents,
416
self.config_stack, timestamp, timezone, committer, self.revprops,
419
if self.builder.updates_branch and self.bound_branch:
421
raise AssertionError(
422
"bound branches not supported for commit builders "
423
"that update the branch")
426
# find the location being committed to
370
427
if self.bound_branch:
371
# 2 extra stages: "Uploading data to master branch" and "Merging
372
# tags to master branch"
373
self.pb_stage_total += 2
374
self.pb.show_pct = False
375
self.pb.show_spinner = False
376
self.pb.show_eta = False
377
self.pb.show_count = True
378
self.pb.show_bar = True
380
# After a merge, a selected file commit is not supported.
381
# See 'bzr help merge' for an explanation as to why.
382
if len(self.parents) > 1 and self.specific_files is not None:
383
raise CannotCommitSelectedFileMerge(self.specific_files)
384
# Excludes are a form of selected file commit.
385
if len(self.parents) > 1 and self.exclude:
386
raise CannotCommitSelectedFileMerge(self.exclude)
388
# Collect the changes
389
self._set_progress_stage("Collecting changes", counter=True)
391
self.builder = self.branch.get_commit_builder(
392
self.parents, self.config_stack, timestamp, timezone, committer,
393
self.revprops, rev_id, lossy=lossy)
395
if self.builder.updates_branch and self.bound_branch:
397
raise AssertionError(
398
"bound branches not supported for commit builders "
399
"that update the branch")
402
# find the location being committed to
403
if self.bound_branch:
404
master_location = self.master_branch.base
406
master_location = self.branch.base
408
# report the start of the commit
409
self.reporter.started(new_revno, self.rev_id, master_location)
411
self._update_builder_with_changes()
412
self._check_pointless()
414
# TODO: Now the new inventory is known, check for conflicts.
415
# ADHB 2006-08-08: If this is done, populate_new_inv should not add
416
# weave lines, because nothing should be recorded until it is known
417
# that commit will succeed.
418
self._set_progress_stage("Saving data locally")
419
self.builder.finish_inventory()
421
# Prompt the user for a commit message if none provided
422
message = message_callback(self)
423
self.message = message
425
# Add revision data to the local branch
426
self.rev_id = self.builder.commit(self.message)
429
mutter("aborting commit write group because of exception:")
430
trace.log_exception_quietly()
434
self._update_branches(old_revno, old_revid, new_revno)
436
# Make the working tree be up to date with the branch. This
437
# includes automatic changes scheduled to be made to the tree, such
438
# as updating its basis and unversioning paths that were missing.
439
self.work_tree.unversion(self.deleted_paths)
440
self._set_progress_stage("Updating the working tree")
441
self.work_tree.update_basis_by_delta(
442
self.rev_id, self.builder.get_basis_delta())
443
self.reporter.completed(new_revno, self.rev_id)
444
self._process_post_hooks(old_revno, new_revno)
428
master_location = self.master_branch.base
430
master_location = self.branch.base
432
# report the start of the commit
433
self.reporter.started(new_revno, self.rev_id, master_location)
435
self._update_builder_with_changes()
436
self._check_pointless()
438
# TODO: Now the new inventory is known, check for conflicts.
439
# ADHB 2006-08-08: If this is done, populate_new_inv should not add
440
# weave lines, because nothing should be recorded until it is known
441
# that commit will succeed.
442
self._set_progress_stage("Saving data locally")
443
self.builder.finish_inventory()
445
# Prompt the user for a commit message if none provided
446
message = message_callback(self)
447
self.message = message
449
# Add revision data to the local branch
450
self.rev_id = self.builder.commit(self.message)
452
except Exception as e:
453
mutter("aborting commit write group because of exception:")
454
trace.log_exception_quietly()
458
self._update_branches(old_revno, old_revid, new_revno)
460
# Make the working tree be up to date with the branch. This
461
# includes automatic changes scheduled to be made to the tree, such
462
# as updating its basis and unversioning paths that were missing.
463
self.work_tree.unversion(
464
{self.work_tree.id2path(file_id) for file_id in self.deleted_ids},
466
self._set_progress_stage("Updating the working tree")
467
self.work_tree.update_basis_by_delta(self.rev_id,
468
self.builder.get_basis_delta())
469
self.reporter.completed(new_revno, self.rev_id)
470
self._process_post_hooks(old_revno, new_revno)
447
473
def _update_branches(self, old_revno, old_revid, new_revno):
448
474
"""Update the master and local branch to the new revision.