143
143
class BzrGitCacheFormat(object):
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)
148
149
def open(self, transport):
150
"""Open this format on a transport."""
149
151
raise NotImplementedError(self.open)
151
153
def initialize(self, transport):
152
154
transport.put_bytes('format', self.get_format_string())
155
def from_repository(self, repository):
157
def from_transport(self, transport):
158
"""Open a cache file present on a transport, or initialize one.
160
:param transport: Transport to use
161
:return: A BzrGitCache instance
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)
172
def from_repository(cls, repository):
173
"""Open a cache file for a repository.
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.
179
:param repository: Repository to open the cache for
180
:return: A `BzrGitCache`
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')
167
193
transport = get_remote_cache_transport()
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)
177
197
class CacheUpdater(object):
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")
561
583
def remove_readonly_transport_decorator(transport):
567
589
def from_repository(repository):
590
"""Open a cache file for a repository.
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/)
596
:param repository: A repository object
568
598
repo_transport = getattr(repository, "_transport", None)
569
599
if repo_transport is not None:
570
600
# Migrate older cache formats