/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to shamap.py

More docstrings, prefer migrating git.db to migrating git.tdb.

Show diffs side-by-side

added added

removed removed

Lines of Context:
143
143
class BzrGitCacheFormat(object):
144
144
 
145
145
    def get_format_string(self):
 
146
        """Return a single-line unique format string for this cache format."""
146
147
        raise NotImplementedError(self.get_format_string)
147
148
 
148
149
    def open(self, transport):
 
150
        """Open this format on a transport."""
149
151
        raise NotImplementedError(self.open)
150
152
 
151
153
    def initialize(self, transport):
152
154
        transport.put_bytes('format', self.get_format_string())
153
155
 
154
156
    @classmethod
155
 
    def from_repository(self, repository):
 
157
    def from_transport(self, transport):
 
158
        """Open a cache file present on a transport, or initialize one.
 
159
 
 
160
        :param transport: Transport to use
 
161
        :return: A BzrGitCache instance
 
162
        """
 
163
        try:
 
164
            format_name = transport.get_bytes('format')
 
165
            format = formats.get(format_name)
 
166
        except bzrlib.errors.NoSuchFile:
 
167
            format = formats.get('default')
 
168
            format.initialize(transport)
 
169
        return format.open(transport)
 
170
 
 
171
    @classmethod
 
172
    def from_repository(cls, repository):
 
173
        """Open a cache file for a repository.
 
174
 
 
175
        This will use the repository's transport to store the cache file, or
 
176
        use the users global cache directory if the repository has no 
 
177
        transport associated with it.
 
178
 
 
179
        :param repository: Repository to open the cache for
 
180
        :return: A `BzrGitCache`
 
181
        """
156
182
        repo_transport = getattr(repository, "_transport", None)
157
183
        if repo_transport is not None:
158
184
            # Even if we don't write to this repo, we should be able 
165
191
            transport = repo_transport.clone('git')
166
192
        else:
167
193
            transport = get_remote_cache_transport()
168
 
        try:
169
 
            format_name = transport.get_bytes('format')
170
 
            format = formats.get(format_name)
171
 
        except bzrlib.errors.NoSuchFile:
172
 
            format = formats.get('default')
173
 
            format.initialize(transport)
174
 
        return format.open(transport)
 
194
        return cls.from_transport(transport)
175
195
 
176
196
 
177
197
class CacheUpdater(object):
550
570
 
551
571
 
552
572
def migrate_ancient_formats(repo_transport):
553
 
    if repo_transport.has("git.tdb"):
 
573
    # Prefer migrating git.db over git.tdb, since the latter may not 
 
574
    # be openable on some platforms.
 
575
    if repo_transport.has("git.db"):
 
576
        SqliteGitCacheFormat().initialize(repo_transport.clone("git"))
 
577
        repo_transport.rename("git.db", "git/idmap.db")
 
578
    elif repo_transport.has("git.tdb"):
554
579
        TdbGitCacheFormat().initialize(repo_transport.clone("git"))
555
580
        repo_transport.rename("git.tdb", "git/idmap.tdb")
556
 
    elif repo_transport.has("git.db"):
557
 
        SqliteGitCacheFormat().initialize(repo_transport.clone("git"))
558
 
        repo_transport.rename("git.db", "git/idmap.db")
559
581
 
560
582
 
561
583
def remove_readonly_transport_decorator(transport):
565
587
 
566
588
 
567
589
def from_repository(repository):
 
590
    """Open a cache file for a repository.
 
591
 
 
592
    If the repository is remote and there is no transport available from it
 
593
    this will use a local file in the users cache directory
 
594
    (typically ~/.cache/bazaar/git/)
 
595
 
 
596
    :param repository: A repository object
 
597
    """
568
598
    repo_transport = getattr(repository, "_transport", None)
569
599
    if repo_transport is not None:
570
600
        # Migrate older cache formats