72
from binascii import hexlify
73
72
from cStringIO import StringIO
75
74
from bzrlib.atomicfile import AtomicFile
76
from bzrlib.osutils import (local_time_offset,
77
rand_bytes, compact_date,
78
kind_marker, is_inside_any, quotefn,
79
sha_file, isdir, isfile,
81
75
import bzrlib.config
82
76
import bzrlib.errors as errors
83
77
from bzrlib.errors import (BzrError, PointlessCommit,
88
from bzrlib.revision import Revision
81
from bzrlib.osutils import (kind_marker, isdir,isfile, is_inside_any,
82
is_inside_or_parent_of_any,
83
quotefn, sha_file, split_lines)
89
84
from bzrlib.testament import Testament
90
85
from bzrlib.trace import mutter, note, warning
91
86
from bzrlib.xml5 import serializer_v5
92
87
from bzrlib.inventory import Inventory, ROOT_ID, InventoryEntry
93
from bzrlib.symbol_versioning import *
88
from bzrlib.symbol_versioning import (deprecated_passed,
94
92
from bzrlib.workingtree import WorkingTree
275
269
# raise an exception as soon as we find a single unknown.
276
270
for unknown in self.work_tree.unknowns():
277
271
raise StrictCommitFailed()
279
if timestamp is None:
280
self.timestamp = time.time()
282
self.timestamp = long(timestamp)
284
273
if self.config is None:
285
274
self.config = bzrlib.config.BranchConfig(self.branch)
288
self.rev_id = _gen_revision_id(self.config, self.timestamp)
292
if committer is None:
293
self.committer = self.config.username()
295
assert isinstance(committer, basestring), type(committer)
296
self.committer = committer
299
self.timezone = local_time_offset()
301
self.timezone = int(timezone)
303
276
if isinstance(message, str):
304
277
message = message.decode(bzrlib.user_encoding)
305
278
assert isinstance(message, unicode), type(message)
322
295
raise NotImplementedError('selected-file commit of merges is not supported yet: files %r',
323
296
self.specific_files)
324
297
self._check_parents_present()
298
self.builder = self.branch.get_commit_builder(self.parents,
299
self.config, timestamp, timezone, committer, revprops, rev_id)
326
301
self._remove_deleted()
327
302
self._populate_new_inv()
328
self._store_snapshot()
329
303
self._report_deletes()
331
305
if not (self.allow_pointless
332
306
or len(self.parents) > 1
333
or self.new_inv != self.basis_inv):
307
or self.builder.new_inventory != self.basis_inv):
334
308
raise PointlessCommit()
336
310
self._emit_progress_update()
337
self.inv_sha1 = self.branch.repository.add_inventory(
342
self._emit_progress_update()
343
self._make_revision()
311
# TODO: Now the new inventory is known, check for conflicts and prompt the
312
# user for a commit message.
313
self.builder.finish_inventory()
314
self._emit_progress_update()
315
self.rev_id = self.builder.commit(self.message)
316
self._emit_progress_update()
344
317
# revision data is in the local branch now.
346
319
# upload revision data to the master.
347
# this will propogate merged revisions too if needed.
320
# this will propagate merged revisions too if needed.
348
321
if self.bound_branch:
349
322
self.master_branch.repository.fetch(self.branch.repository,
350
323
revision_id=self.rev_id)
479
445
def _gather_parents(self):
480
446
"""Record the parents of a merge for merge detection."""
481
pending_merges = self.work_tree.pending_merges()
447
# TODO: Make sure that this list doesn't contain duplicate
448
# entries and the order is preserved when doing this.
449
self.parents = self.work_tree.get_parent_ids()
483
450
self.parent_invs = []
484
self.present_parents = []
485
precursor_id = self.branch.last_revision()
487
self.parents.append(precursor_id)
488
self.parents += pending_merges
489
451
for revision in self.parents:
490
452
if self.branch.repository.has_revision(revision):
491
453
inventory = self.branch.repository.get_inventory(revision)
492
454
self.parent_invs.append(inventory)
493
self.present_parents.append(revision)
495
456
def _check_parents_present(self):
496
457
for parent_id in self.parents:
498
459
if not self.branch.repository.has_revision(parent_id):
499
460
if parent_id == self.branch.last_revision():
500
461
warning("parent is missing %r", parent_id)
501
raise HistoryMissing(self.branch, 'revision', parent_id)
503
mutter("commit will ghost revision %r", parent_id)
462
raise BzrCheckError("branch %s is missing revision {%s}"
463
% (self.branch, parent_id))
505
def _make_revision(self):
506
"""Record a new revision object for this commit."""
507
rev = Revision(timestamp=self.timestamp,
508
timezone=self.timezone,
509
committer=self.committer,
510
message=self.message,
511
inventory_sha1=self.inv_sha1,
512
revision_id=self.rev_id,
513
properties=self.revprops)
514
rev.parent_ids = self.parents
515
self.branch.repository.add_revision(self.rev_id, rev, self.new_inv, self.config)
517
465
def _remove_deleted(self):
518
466
"""Remove deleted files from the working inventories.
539
487
del self.work_inv[file_id]
540
488
self.work_tree._write_inventory(self.work_inv)
542
def _store_snapshot(self):
543
"""Pass over inventory and record a snapshot.
545
Entries get a new revision when they are modified in
546
any way, which includes a merge with a new set of
547
parents that have the same entry.
549
# XXX: Need to think more here about when the user has
550
# made a specific decision on a particular value -- c.f.
553
# iter_entries does not visit the ROOT_ID node so we need to call
554
# self._emit_progress_update once by hand.
555
self._emit_progress_update()
556
for path, ie in self.new_inv.iter_entries():
557
self._emit_progress_update()
558
previous_entries = ie.find_previous_heads(
561
self.branch.repository.get_transaction())
562
if ie.revision is None:
563
# we are creating a new revision for ie in the history store
565
ie.snapshot(self.rev_id, path, previous_entries,
566
self.work_tree, self.weave_store,
567
self.branch.repository.get_transaction())
568
# describe the nature of the change that has occured relative to
569
# the basis inventory.
570
if (self.basis_inv.has_id(ie.file_id)):
571
basis_ie = self.basis_inv[ie.file_id]
574
change = ie.describe_change(basis_ie, ie)
575
if change in (InventoryEntry.RENAMED,
576
InventoryEntry.MODIFIED_AND_RENAMED):
577
old_path = self.basis_inv.id2path(ie.file_id)
578
self.reporter.renamed(change, old_path, path)
580
self.reporter.snapshot_change(change, path)
582
490
def _populate_new_inv(self):
583
491
"""Build revision inventory.
590
498
revision set to their prior value.
592
500
mutter("Selecting files for commit with filter %s", self.specific_files)
593
self.new_inv = Inventory(revision_id=self.rev_id)
594
501
# iter_entries does not visit the ROOT_ID node so we need to call
595
502
# self._emit_progress_update once by hand.
596
503
self._emit_progress_update()
597
504
for path, new_ie in self.work_inv.iter_entries():
598
505
self._emit_progress_update()
599
506
file_id = new_ie.file_id
600
mutter('check %s {%s}', path, new_ie.file_id)
601
if self.specific_files:
602
if not is_inside_any(self.specific_files, path):
603
mutter('%s not selected for commit', path)
604
self._carry_entry(file_id)
507
mutter('check %s {%s}', path, file_id)
508
if (not self.specific_files or
509
is_inside_or_parent_of_any(self.specific_files, path)):
510
mutter('%s selected for commit', path)
514
mutter('%s not selected for commit', path)
515
if self.basis_inv.has_id(file_id):
516
ie = self.basis_inv[file_id].copy()
518
# this entry is new and not being committed
607
# this is selected, ensure its parents are too.
608
parent_id = new_ie.parent_id
609
while parent_id != ROOT_ID:
610
if not self.new_inv.has_id(parent_id):
611
ie = self._select_entry(self.work_inv[parent_id])
612
mutter('%s selected for commit because of %s',
613
self.new_inv.id2path(parent_id), path)
615
ie = self.new_inv[parent_id]
616
if ie.revision is not None:
618
mutter('%s selected for commit because of %s',
619
self.new_inv.id2path(parent_id), path)
620
parent_id = ie.parent_id
621
mutter('%s selected for commit', path)
622
self._select_entry(new_ie)
521
self.builder.record_entry_contents(ie, self.parent_invs,
522
path, self.work_tree)
523
# describe the nature of the change that has occurred relative to
524
# the basis inventory.
525
if (self.basis_inv.has_id(ie.file_id)):
526
basis_ie = self.basis_inv[ie.file_id]
529
change = ie.describe_change(basis_ie, ie)
530
if change in (InventoryEntry.RENAMED,
531
InventoryEntry.MODIFIED_AND_RENAMED):
532
old_path = self.basis_inv.id2path(ie.file_id)
533
self.reporter.renamed(change, old_path, path)
535
self.reporter.snapshot_change(change, path)
624
537
def _emit_progress_update(self):
625
538
"""Emit an update to the progress bar."""
626
539
self.pb.update("Committing", self.pb_count, self.pb_total)
627
540
self.pb_count += 1
629
def _select_entry(self, new_ie):
630
"""Make new_ie be considered for committing."""
636
def _carry_entry(self, file_id):
637
"""Carry the file unchanged from the basis revision."""
638
if self.basis_inv.has_id(file_id):
639
self.new_inv.add(self.basis_inv[file_id].copy())
641
# this entry is new and not being committed
644
542
def _report_deletes(self):
645
543
for path, ie in self.basis_inv.iter_entries():
646
if ie.file_id not in self.new_inv:
544
if ie.file_id not in self.builder.new_inventory:
647
545
self.reporter.deleted(path)
649
def _gen_revision_id(config, when):
650
"""Return new revision-id."""
651
s = '%s-%s-' % (config.user_email(), compact_date(when))
652
s += hexlify(rand_bytes(8))