blob: aca1f90d447fd940d2c645914a1d8e53abdc0691 [file] [log] [blame]
Shawn O. Pearced237b692009-04-17 18:49:50 -07001# Copyright (C) 2009 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import os
Mike Frysinger8a11f6f2019-08-27 00:26:15 -040016from repo_trace import Trace
Renaud Paquaybed8b622018-09-27 10:46:58 -070017import platform_utils
Shawn O. Pearced237b692009-04-17 18:49:50 -070018
Gavin Makea2e3302023-03-11 06:46:20 +000019HEAD = "HEAD"
20R_CHANGES = "refs/changes/"
21R_HEADS = "refs/heads/"
22R_TAGS = "refs/tags/"
23R_PUB = "refs/published/"
24R_WORKTREE = "refs/worktree/"
25R_WORKTREE_M = R_WORKTREE + "m/"
26R_M = "refs/remotes/m/"
Shawn O. Pearced237b692009-04-17 18:49:50 -070027
28
29class GitRefs(object):
Gavin Makea2e3302023-03-11 06:46:20 +000030 def __init__(self, gitdir):
31 self._gitdir = gitdir
32 self._phyref = None
33 self._symref = None
34 self._mtime = {}
Shawn O. Pearced237b692009-04-17 18:49:50 -070035
Gavin Makea2e3302023-03-11 06:46:20 +000036 @property
37 def all(self):
38 self._EnsureLoaded()
39 return self._phyref
Shawn O. Pearced237b692009-04-17 18:49:50 -070040
Gavin Makea2e3302023-03-11 06:46:20 +000041 def get(self, name):
Joanna Wanga6c52f52022-11-03 16:51:19 -040042 try:
Gavin Makea2e3302023-03-11 06:46:20 +000043 return self.all[name]
44 except KeyError:
45 return ""
46
47 def deleted(self, name):
48 if self._phyref is not None:
49 if name in self._phyref:
50 del self._phyref[name]
51
52 if name in self._symref:
53 del self._symref[name]
54
55 if name in self._mtime:
56 del self._mtime[name]
57
58 def symref(self, name):
59 try:
60 self._EnsureLoaded()
61 return self._symref[name]
62 except KeyError:
63 return ""
64
65 def _EnsureLoaded(self):
66 if self._phyref is None or self._NeedUpdate():
67 self._LoadAll()
68
69 def _NeedUpdate(self):
70 with Trace(": scan refs %s", self._gitdir):
71 for name, mtime in self._mtime.items():
72 try:
73 if mtime != os.path.getmtime(
74 os.path.join(self._gitdir, name)
75 ):
76 return True
77 except OSError:
78 return True
79 return False
80
81 def _LoadAll(self):
82 with Trace(": load refs %s", self._gitdir):
83 self._phyref = {}
84 self._symref = {}
85 self._mtime = {}
86
87 self._ReadPackedRefs()
88 self._ReadLoose("refs/")
89 self._ReadLoose1(os.path.join(self._gitdir, HEAD), HEAD)
90
91 scan = self._symref
92 attempts = 0
93 while scan and attempts < 5:
94 scan_next = {}
95 for name, dest in scan.items():
96 if dest in self._phyref:
97 self._phyref[name] = self._phyref[dest]
98 else:
99 scan_next[name] = dest
100 scan = scan_next
101 attempts += 1
102
103 def _ReadPackedRefs(self):
104 path = os.path.join(self._gitdir, "packed-refs")
105 try:
106 fd = open(path, "r")
107 mtime = os.path.getmtime(path)
108 except IOError:
109 return
Joanna Wanga6c52f52022-11-03 16:51:19 -0400110 except OSError:
Gavin Makea2e3302023-03-11 06:46:20 +0000111 return
112 try:
113 for line in fd:
114 line = str(line)
115 if line[0] == "#":
116 continue
117 if line[0] == "^":
118 continue
Shawn O. Pearced237b692009-04-17 18:49:50 -0700119
Gavin Makea2e3302023-03-11 06:46:20 +0000120 line = line[:-1]
121 p = line.split(" ")
122 ref_id = p[0]
123 name = p[1]
Shawn O. Pearcead3193a2009-04-18 09:54:51 -0700124
Gavin Makea2e3302023-03-11 06:46:20 +0000125 self._phyref[name] = ref_id
126 finally:
127 fd.close()
128 self._mtime["packed-refs"] = mtime
Shawn O. Pearced237b692009-04-17 18:49:50 -0700129
Gavin Makea2e3302023-03-11 06:46:20 +0000130 def _ReadLoose(self, prefix):
131 base = os.path.join(self._gitdir, prefix)
132 for name in platform_utils.listdir(base):
133 p = os.path.join(base, name)
134 # We don't implement the full ref validation algorithm, just the
135 # simple rules that would show up in local filesystems.
136 # https://git-scm.com/docs/git-check-ref-format
137 if name.startswith(".") or name.endswith(".lock"):
138 pass
139 elif platform_utils.isdir(p):
140 self._mtime[prefix] = os.path.getmtime(base)
141 self._ReadLoose(prefix + name + "/")
142 else:
143 self._ReadLoose1(p, prefix + name)
Shawn O. Pearced237b692009-04-17 18:49:50 -0700144
Gavin Makea2e3302023-03-11 06:46:20 +0000145 def _ReadLoose1(self, path, name):
146 try:
147 with open(path) as fd:
148 mtime = os.path.getmtime(path)
149 ref_id = fd.readline()
150 except (OSError, UnicodeError):
151 return
Shawn O. Pearced237b692009-04-17 18:49:50 -0700152
Gavin Makea2e3302023-03-11 06:46:20 +0000153 try:
154 ref_id = ref_id.decode()
155 except AttributeError:
156 pass
157 if not ref_id:
158 return
159 ref_id = ref_id[:-1]
Shawn O. Pearced237b692009-04-17 18:49:50 -0700160
Gavin Makea2e3302023-03-11 06:46:20 +0000161 if ref_id.startswith("ref: "):
162 self._symref[name] = ref_id[5:]
163 else:
164 self._phyref[name] = ref_id
165 self._mtime[name] = mtime