1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
1
# Copyright (C) 2005, 2006 Canonical Ltd
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
18
# XXX: Can we do any better about making interrupted commits change
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?)
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.
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.
62
# TODO: If commit fails, leave the message in a file somewhere.
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
85
92
class NullCommitReporter(object):
114
121
def snapshot_change(self, change, path):
115
122
if change == 'unchanged':
117
if change == 'added' and path == '':
119
124
note("%s %s", change, path)
121
126
def completed(self, revno, rev_id):
172
177
working_tree=None,
176
message_callback=None,
178
181
"""Commit working copy as a new revision.
180
message -- the commit message (it or message_callback is required)
183
branch -- the deprecated branch to commit to. New callers should pass in
186
message -- the commit message, a mandatory parameter
182
188
timestamp -- if not None, seconds-since-epoch for a
183
189
postdated/predated commit.
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.
204
208
mutter('preparing to commit')
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)
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().")
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
220
raise BzrError("The message or message_callback keyword"
221
" parameter is required for commit().")
222
raise BzrError("The message keyword parameter is required for commit().")
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
238
self.verbose = verbose
241
232
if reporter is None and self.reporter is None:
242
233
self.reporter = NullCommitReporter()
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()
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
274
# ghost parents never appear in revision history.
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)
277
262
# raise an exception as soon as we find a single unknown.
278
263
for unknown in self.work_tree.unknowns():
281
266
if self.config is None:
282
267
self.config = self.branch.get_config()
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()
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
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)
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)
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()
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,
320
self.master_branch.append_revision(self.rev_id)
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)
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
350
self.reporter.completed(new_revno, self.rev_id)
351
# old style commit hooks - should be deprecated ? (obsoleted in
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,
360
340
'rev_id':self.rev_id})
361
# new style commit hooks:
362
if not self.bound_branch:
363
hook_master = self.branch
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
372
old_revid = self.parents[0]
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,
378
341
self._emit_progress_update()
381
344
return self.rev_id
383
def _any_real_changes(self):
384
"""Are there real changes between new_inventory and basis?
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
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()
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)
408
if not ie_equal_no_revision(new_root_ie, basis_root_ie):
411
for new_ie, basis_ie in zip(new_entries, basis_entries):
412
if new_ie != basis_ie:
415
# No actual changes present
418
346
def _check_pointless(self):
419
347
if self.allow_pointless:
424
352
# work around the fact that a newly-initted tree does differ from its
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
430
354
if len(self.builder.new_inventory) != len(self.basis_inv):
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):
436
359
raise PointlessCommit()
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)
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
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
614
self.work_tree.branch.repository.bzrdir.root_transport.base):
615
sub_tree.branch.repository = \
616
self.work_tree.branch.repository
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:
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:
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)):