~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commit.py

  • Committer: John Arbash Meinel
  • Date: 2006-06-18 02:21:57 UTC
  • mfrom: (1787 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1794.
  • Revision ID: john@arbash-meinel.com-20060618022157-6e33aa9b67c25e4f
[merge] bzr.dev 1787

Show diffs side-by-side

added added

removed removed

Lines of Context:
69
69
import time
70
70
import pdb
71
71
 
72
 
from binascii import hexlify
73
72
from cStringIO import StringIO
74
73
 
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,
80
 
                            split_lines)
81
75
import bzrlib.config
82
76
import bzrlib.errors as errors
83
77
from bzrlib.errors import (BzrError, PointlessCommit,
85
79
                           ConflictsInTree,
86
80
                           StrictCommitFailed
87
81
                           )
88
 
from bzrlib.revision import Revision
 
82
from bzrlib.osutils import (kind_marker, isdir,isfile, is_inside_any, 
 
83
                            is_inside_or_parent_of_any,
 
84
                            quotefn, sha_file, split_lines)
89
85
from bzrlib.testament import Testament
90
86
from bzrlib.trace import mutter, note, warning
91
87
from bzrlib.xml5 import serializer_v5
102
98
 
103
99
    New code should use the Commit class instead.
104
100
    """
105
 
    ## XXX: Remove this in favor of Branch.commit?
 
101
    ## XXX: Remove this in favor of WorkingTree.commit?
106
102
    Commit().commit(*args, **kwargs)
107
103
 
108
104
 
238
234
        if message is None:
239
235
            raise BzrError("The message keyword parameter is required for commit().")
240
236
 
241
 
        self.weave_store = self.branch.repository.weave_store
242
237
        self.bound_branch = None
243
238
        self.local = local
244
239
        self.master_branch = None
245
240
        self.master_locked = False
246
 
        self.rev_id = rev_id
 
241
        self.rev_id = None
247
242
        self.specific_files = specific_files
248
243
        self.allow_pointless = allow_pointless
249
 
        self.revprops = {}
250
 
        if revprops is not None:
251
 
            self.revprops.update(revprops)
252
244
 
253
245
        if reporter is None and self.reporter is None:
254
246
            self.reporter = NullCommitReporter()
275
267
                # raise an exception as soon as we find a single unknown.
276
268
                for unknown in self.work_tree.unknowns():
277
269
                    raise StrictCommitFailed()
278
 
    
279
 
            if timestamp is None:
280
 
                self.timestamp = time.time()
281
 
            else:
282
 
                self.timestamp = long(timestamp)
283
 
                
 
270
                   
284
271
            if self.config is None:
285
272
                self.config = bzrlib.config.BranchConfig(self.branch)
286
 
    
287
 
            if rev_id is None:
288
 
                self.rev_id = _gen_revision_id(self.config, self.timestamp)
289
 
            else:
290
 
                self.rev_id = rev_id
291
 
    
292
 
            if committer is None:
293
 
                self.committer = self.config.username()
294
 
            else:
295
 
                assert isinstance(committer, basestring), type(committer)
296
 
                self.committer = committer
297
 
    
298
 
            if timezone is None:
299
 
                self.timezone = local_time_offset()
300
 
            else:
301
 
                self.timezone = int(timezone)
302
 
    
 
273
      
303
274
            if isinstance(message, str):
304
275
                message = message.decode(bzrlib.user_encoding)
305
276
            assert isinstance(message, unicode), type(message)
314
285
            # note that this estimate is too long when we do a partial tree
315
286
            # commit which excludes some new files from being considered.
316
287
            # The estimate is corrected when we populate the new inv.
317
 
            self.pb_total = len(self.basis_inv) + len(self.work_inv) + 3 - 1
 
288
            self.pb_total = len(self.work_inv) + 5
318
289
            self.pb_count = 0
319
290
 
320
291
            self._gather_parents()
322
293
                raise NotImplementedError('selected-file commit of merges is not supported yet: files %r',
323
294
                        self.specific_files)
324
295
            self._check_parents_present()
 
296
            self.builder = self.branch.get_commit_builder(self.parents, 
 
297
                self.config, timestamp, timezone, committer, revprops, rev_id)
325
298
            
326
299
            self._remove_deleted()
327
300
            self._populate_new_inv()
328
 
            self._store_snapshot()
329
301
            self._report_deletes()
330
302
 
331
303
            if not (self.allow_pointless
332
304
                    or len(self.parents) > 1
333
 
                    or self.new_inv != self.basis_inv):
 
305
                    or self.builder.new_inventory != self.basis_inv):
334
306
                raise PointlessCommit()
335
307
 
336
308
            self._emit_progress_update()
337
 
            self.inv_sha1 = self.branch.repository.add_inventory(
338
 
                self.rev_id,
339
 
                self.new_inv,
340
 
                self.present_parents
341
 
                )
342
 
            self._emit_progress_update()
343
 
            self._make_revision()
 
309
            # TODO: Now the new inventory is known, check for conflicts and prompt the 
 
310
            # user for a commit message.
 
311
            self.builder.finish_inventory()
 
312
            self._emit_progress_update()
 
313
            self.rev_id = self.builder.commit(self.message)
 
314
            self._emit_progress_update()
344
315
            # revision data is in the local branch now.
345
316
            
346
317
            # upload revision data to the master.
347
 
            # this will propogate merged revisions too if needed.
 
318
            # this will propagate merged revisions too if needed.
348
319
            if self.bound_branch:
349
320
                self.master_branch.repository.fetch(self.branch.repository,
350
321
                                                    revision_id=self.rev_id)
372
343
            self._emit_progress_update()
373
344
        finally:
374
345
            self._cleanup()
 
346
        return self.rev_id
375
347
 
376
348
    def _check_bound_branch(self):
377
349
        """Check to see if the local branch is bound.
414
386
        self.bound_branch = self.branch
415
387
        self.master_branch.lock_write()
416
388
        self.master_locked = True
417
 
####        
418
 
####        # Check to see if we have any pending merges. If we do
419
 
####        # those need to be pushed into the master branch
420
 
####        pending_merges = self.work_tree.pending_merges()
421
 
####        if pending_merges:
422
 
####            for revision_id in pending_merges:
423
 
####                self.master_branch.repository.fetch(self.bound_branch.repository,
424
 
####                                                    revision_id=revision_id)
425
389
 
426
390
    def _cleanup(self):
427
391
        """Cleanup any open locks, progress bars etc."""
439
403
            except Exception, e:
440
404
                found_exception = e
441
405
        if found_exception is not None: 
442
 
            # dont do a plan raise, because the last exception may have been
 
406
            # don't do a plan raise, because the last exception may have been
443
407
            # trashed, e is our sure-to-work exception even though it loses the
444
408
            # full traceback. XXX: RBC 20060421 perhaps we could check the
445
409
            # exc_info and if its the same one do a plain raise otherwise 
478
442
 
479
443
    def _gather_parents(self):
480
444
        """Record the parents of a merge for merge detection."""
481
 
        pending_merges = self.work_tree.pending_merges()
482
 
        self.parents = []
 
445
        # TODO: Make sure that this list doesn't contain duplicate 
 
446
        # entries and the order is preserved when doing this.
 
447
        self.parents = self.work_tree.get_parent_ids()
483
448
        self.parent_invs = []
484
 
        self.present_parents = []
485
 
        precursor_id = self.branch.last_revision()
486
 
        if precursor_id:
487
 
            self.parents.append(precursor_id)
488
 
        self.parents += pending_merges
489
449
        for revision in self.parents:
490
450
            if self.branch.repository.has_revision(revision):
491
451
                inventory = self.branch.repository.get_inventory(revision)
492
452
                self.parent_invs.append(inventory)
493
 
                self.present_parents.append(revision)
494
453
 
495
454
    def _check_parents_present(self):
496
455
        for parent_id in self.parents:
502
461
                else:
503
462
                    mutter("commit will ghost revision %r", parent_id)
504
463
            
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)
516
 
 
517
464
    def _remove_deleted(self):
518
465
        """Remove deleted files from the working inventories.
519
466
 
539
486
                del self.work_inv[file_id]
540
487
            self.work_tree._write_inventory(self.work_inv)
541
488
 
542
 
    def _store_snapshot(self):
543
 
        """Pass over inventory and record a snapshot.
544
 
 
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. 
548
 
        """
549
 
        # XXX: Need to think more here about when the user has
550
 
        # made a specific decision on a particular value -- c.f.
551
 
        # mark-merge.  
552
 
 
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(
559
 
                self.parent_invs,
560
 
                self.weave_store,
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
564
 
                # and inventory.
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]
572
 
            else:
573
 
                basis_ie = None
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)
579
 
            else:
580
 
                self.reporter.snapshot_change(change, path)
581
 
 
582
489
    def _populate_new_inv(self):
583
490
        """Build revision inventory.
584
491
 
590
497
        revision set to their prior value.
591
498
        """
592
499
        mutter("Selecting files for commit with filter %s", self.specific_files)
593
 
        self.new_inv = Inventory(revision_id=self.rev_id)
594
500
        # iter_entries does not visit the ROOT_ID node so we need to call
595
501
        # self._emit_progress_update once by hand.
596
502
        self._emit_progress_update()
597
503
        for path, new_ie in self.work_inv.iter_entries():
598
504
            self._emit_progress_update()
599
505
            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)
 
506
            mutter('check %s {%s}', path, file_id)
 
507
            if (not self.specific_files or 
 
508
                is_inside_or_parent_of_any(self.specific_files, path)):
 
509
                    mutter('%s selected for commit', path)
 
510
                    ie = new_ie.copy()
 
511
                    ie.revision = None
 
512
            else:
 
513
                mutter('%s not selected for commit', path)
 
514
                if self.basis_inv.has_id(file_id):
 
515
                    ie = self.basis_inv[file_id].copy()
 
516
                else:
 
517
                    # this entry is new and not being committed
605
518
                    continue
606
 
                else:
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)
614
519
 
615
 
                        ie = self.new_inv[parent_id]
616
 
                        if ie.revision is not None:
617
 
                            ie.revision = 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)
 
520
            self.builder.record_entry_contents(ie, self.parent_invs, 
 
521
                path, self.work_tree)
 
522
            # describe the nature of the change that has occurred relative to
 
523
            # the basis inventory.
 
524
            if (self.basis_inv.has_id(ie.file_id)):
 
525
                basis_ie = self.basis_inv[ie.file_id]
 
526
            else:
 
527
                basis_ie = None
 
528
            change = ie.describe_change(basis_ie, ie)
 
529
            if change in (InventoryEntry.RENAMED, 
 
530
                InventoryEntry.MODIFIED_AND_RENAMED):
 
531
                old_path = self.basis_inv.id2path(ie.file_id)
 
532
                self.reporter.renamed(change, old_path, path)
 
533
            else:
 
534
                self.reporter.snapshot_change(change, path)
623
535
 
624
536
    def _emit_progress_update(self):
625
537
        """Emit an update to the progress bar."""
626
538
        self.pb.update("Committing", self.pb_count, self.pb_total)
627
539
        self.pb_count += 1
628
540
 
629
 
    def _select_entry(self, new_ie):
630
 
        """Make new_ie be considered for committing."""
631
 
        ie = new_ie.copy()
632
 
        ie.revision = None
633
 
        self.new_inv.add(ie)
634
 
        return ie
635
 
 
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())
640
 
        else:
641
 
            # this entry is new and not being committed
642
 
            self.pb_total -= 1
643
 
 
644
541
    def _report_deletes(self):
645
542
        for path, ie in self.basis_inv.iter_entries():
646
 
            if ie.file_id not in self.new_inv:
 
543
            if ie.file_id not in self.builder.new_inventory:
647
544
                self.reporter.deleted(path)
648
545
 
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))
653
 
    return s
 
546