Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/gitdb/db/ref.py: 19%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors
2#
3# This module is part of GitDB and is released under
4# the New BSD License: https://opensource.org/license/bsd-3-clause/
5from gitdb.db.base import (
6 CompoundDB,
7)
9__all__ = ('ReferenceDB', )
12class ReferenceDB(CompoundDB):
14 """A database consisting of database referred to in a file"""
16 # Configuration
17 # Specifies the object database to use for the paths found in the alternates
18 # file. If None, it defaults to the GitDB
19 ObjectDBCls = None
21 def __init__(self, ref_file):
22 super().__init__()
23 self._ref_file = ref_file
25 def _set_cache_(self, attr):
26 if attr == '_dbs':
27 self._dbs = list()
28 self._update_dbs_from_ref_file()
29 else:
30 super()._set_cache_(attr)
31 # END handle attrs
33 def _update_dbs_from_ref_file(self):
34 dbcls = self.ObjectDBCls
35 if dbcls is None:
36 # late import
37 from gitdb.db.git import GitDB
38 dbcls = GitDB
39 # END get db type
41 # try to get as many as possible, don't fail if some are unavailable
42 ref_paths = list()
43 try:
44 with open(self._ref_file, 'r', encoding="utf-8") as f:
45 ref_paths = [l.strip() for l in f]
46 except OSError:
47 pass
48 # END handle alternates
50 ref_paths_set = set(ref_paths)
51 cur_ref_paths_set = {db.root_path() for db in self._dbs}
53 # remove existing
54 for path in (cur_ref_paths_set - ref_paths_set):
55 for i, db in enumerate(self._dbs[:]):
56 if db.root_path() == path:
57 del(self._dbs[i])
58 continue
59 # END del matching db
60 # END for each path to remove
62 # add new
63 # sort them to maintain order
64 added_paths = sorted(ref_paths_set - cur_ref_paths_set, key=lambda p: ref_paths.index(p))
65 for path in added_paths:
66 try:
67 db = dbcls(path)
68 # force an update to verify path
69 if isinstance(db, CompoundDB):
70 db.databases()
71 # END verification
72 self._dbs.append(db)
73 except Exception:
74 # ignore invalid paths or issues
75 pass
76 # END for each path to add
78 def update_cache(self, force=False):
79 # re-read alternates and update databases
80 self._update_dbs_from_ref_file()
81 return super().update_cache(force)