~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: John Arbash Meinel
  • Date: 2007-02-13 13:38:16 UTC
  • mto: This revision was merged to the branch mainline in revision 2294.
  • Revision ID: john@arbash-meinel.com-20070213133816-r6swt6ibvtms473f
remove get_cached_utf8 checks which were slowing things down.

Show diffs side-by-side

added added

removed removed

Lines of Context:
81
81
 
82
82
    base
83
83
        Base directory/url of the branch.
 
84
 
 
85
    hooks: An instance of BranchHooks.
84
86
    """
85
87
    # this is really an instance variable - FIXME move it there
86
88
    # - RBC 20060112
358
360
        """Given a revision id, return its revno"""
359
361
        if revision_id is None:
360
362
            return 0
 
363
        revision_id = osutils.safe_revision_id(revision_id)
361
364
        history = self.revision_history()
362
365
        try:
363
366
            return history.index(revision_id) + 1
543
546
        """
544
547
        new_history = self.revision_history()
545
548
        if revision_id is not None:
 
549
            revision_id = osutils.safe_revision_id(revision_id)
546
550
            try:
547
551
                new_history = new_history[:new_history.index(revision_id) + 1]
548
552
            except ValueError:
716
720
        return self.get_format_string().rstrip()
717
721
 
718
722
 
 
723
class BranchHooks(dict):
 
724
    """A dictionary mapping hook name to a list of callables for branch hooks.
 
725
    
 
726
    e.g. ['set_rh'] Is the list of items to be called when the
 
727
    set_revision_history function is invoked.
 
728
    """
 
729
 
 
730
    def __init__(self):
 
731
        """Create the default hooks.
 
732
 
 
733
        These are all empty initially, because by default nothing should get
 
734
        notified.
 
735
        """
 
736
        dict.__init__(self)
 
737
        # invoked whenever the revision history has been set
 
738
        # with set_revision_history. The api signature is
 
739
        # (branch, revision_history), and the branch will
 
740
        # be write-locked. Introduced in 0.15.
 
741
        self['set_rh'] = []
 
742
 
 
743
    def install_hook(self, hook_name, a_callable):
 
744
        """Install a_callable in to the hook hook_name.
 
745
 
 
746
        :param hook_name: A hook name. See the __init__ method of BranchHooks
 
747
            for the complete list of hooks.
 
748
        :param a_callable: The callable to be invoked when the hook triggers.
 
749
            The exact signature will depend on the hook - see the __init__ 
 
750
            method of BranchHooks for details on each hook.
 
751
        """
 
752
        try:
 
753
            self[hook_name].append(a_callable)
 
754
        except KeyError:
 
755
            raise errors.UnknownHook('branch', hook_name)
 
756
 
 
757
 
 
758
# install the default hooks into the Branch class.
 
759
Branch.hooks = BranchHooks()
 
760
 
 
761
 
719
762
class BzrBranchFormat4(BranchFormat):
720
763
    """Bzr branch format 4.
721
764
 
1086
1129
    @needs_write_lock
1087
1130
    def set_revision_history(self, rev_history):
1088
1131
        """See Branch.set_revision_history."""
1089
 
        self.control_files.put_utf8(
 
1132
        rev_history = [osutils.safe_revision_id(r) for r in rev_history]
 
1133
        self.control_files.put_bytes(
1090
1134
            'revision-history', '\n'.join(rev_history))
1091
1135
        transaction = self.get_transaction()
1092
1136
        history = transaction.map.find_revision_history()
1101
1145
            # this call is disabled because revision_history is 
1102
1146
            # not really an object yet, and the transaction is for objects.
1103
1147
            # transaction.register_clean(history)
 
1148
        for hook in Branch.hooks['set_rh']:
 
1149
            hook(self, rev_history)
1104
1150
 
1105
1151
    @needs_read_lock
1106
1152
    def revision_history(self):
1110
1156
        if history is not None:
1111
1157
            # mutter("cache hit for revision-history in %s", self)
1112
1158
            return list(history)
1113
 
        decode_utf8 = cache_utf8.decode
1114
 
        history = [decode_utf8(l.rstrip('\r\n')) for l in
 
1159
        get_cached_utf8 = cache_utf8.get_cached_utf8
 
1160
        history = [get_cached_utf8(l.rstrip('\r\n')) for l in
1115
1161
                self.control_files.get('revision-history').readlines()]
1116
1162
        transaction.map.add_revision_history(history)
1117
1163
        # this call is disabled because revision_history is 
1130
1176
        :param other_branch: The other branch that DivergedBranches should
1131
1177
            raise with respect to.
1132
1178
        """
 
1179
        revision_id = osutils.safe_revision_id(revision_id)
1133
1180
        # stop_revision must be a descendant of last_revision
1134
1181
        stop_graph = self.repository.get_revision_graph(revision_id)
1135
1182
        if last_rev is not None and last_rev not in stop_graph:
1158
1205
                if stop_revision is None:
1159
1206
                    # if there are no commits, we're done.
1160
1207
                    return
 
1208
            else:
 
1209
                stop_revision = osutils.safe_revision_id(stop_revision)
1161
1210
            # whats the current last revision, before we fetch [and change it
1162
1211
            # possibly]
1163
1212
            last_rev = self.last_revision()
1256
1305
                        "use bzrlib.urlutils.escape")
1257
1306
                    
1258
1307
            url = urlutils.relative_url(self.base, url)
1259
 
            self.control_files.put('parent', StringIO(url + '\n'))
 
1308
            self.control_files.put_bytes('parent', url + '\n')
1260
1309
 
1261
1310
    @deprecated_function(zero_nine)
1262
1311
    def tree_config(self):