blob: 300d2b308981684112bbc1e28ba74a5c2eaac5e4 [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
David Pursehouse54a4e602020-02-12 14:31:05 +090019HEAD = 'HEAD'
Zac Livingston9ead97b2017-06-13 08:29:04 -060020R_CHANGES = 'refs/changes/'
David Pursehouse54a4e602020-02-12 14:31:05 +090021R_HEADS = 'refs/heads/'
22R_TAGS = 'refs/tags/'
23R_PUB = 'refs/published/'
Mike Frysinger21b7fbe2020-02-26 23:53:36 -050024R_WORKTREE = 'refs/worktree/'
25R_WORKTREE_M = R_WORKTREE + 'm/'
David Pursehouse54a4e602020-02-12 14:31:05 +090026R_M = 'refs/remotes/m/'
Shawn O. Pearced237b692009-04-17 18:49:50 -070027
28
29class GitRefs(object):
30 def __init__(self, gitdir):
31 self._gitdir = gitdir
32 self._phyref = None
33 self._symref = None
34 self._mtime = {}
35
36 @property
37 def all(self):
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -070038 self._EnsureLoaded()
Shawn O. Pearced237b692009-04-17 18:49:50 -070039 return self._phyref
40
41 def get(self, name):
42 try:
43 return self.all[name]
44 except KeyError:
45 return ''
46
Shawn O. Pearcefbcde472009-04-17 20:58:02 -070047 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
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -070058 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
Shawn O. Pearced237b692009-04-17 18:49:50 -070069 def _NeedUpdate(self):
Joanna Wanga6c52f52022-11-03 16:51:19 -040070 with Trace(': scan refs %s', self._gitdir):
71 for name, mtime in self._mtime.items():
72 try:
73 if mtime != os.path.getmtime(os.path.join(self._gitdir, name)):
74 return True
75 except OSError:
Shawn O. Pearced237b692009-04-17 18:49:50 -070076 return True
Joanna Wanga6c52f52022-11-03 16:51:19 -040077 return False
Shawn O. Pearced237b692009-04-17 18:49:50 -070078
79 def _LoadAll(self):
Joanna Wanga6c52f52022-11-03 16:51:19 -040080 with Trace(': load refs %s', self._gitdir):
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070081
Joanna Wanga6c52f52022-11-03 16:51:19 -040082 self._phyref = {}
83 self._symref = {}
84 self._mtime = {}
Shawn O. Pearced237b692009-04-17 18:49:50 -070085
Joanna Wanga6c52f52022-11-03 16:51:19 -040086 self._ReadPackedRefs()
87 self._ReadLoose('refs/')
88 self._ReadLoose1(os.path.join(self._gitdir, HEAD), HEAD)
Shawn O. Pearced237b692009-04-17 18:49:50 -070089
Joanna Wanga6c52f52022-11-03 16:51:19 -040090 scan = self._symref
91 attempts = 0
92 while scan and attempts < 5:
93 scan_next = {}
94 for name, dest in scan.items():
95 if dest in self._phyref:
96 self._phyref[name] = self._phyref[dest]
97 else:
98 scan_next[name] = dest
99 scan = scan_next
100 attempts += 1
Shawn O. Pearced237b692009-04-17 18:49:50 -0700101
102 def _ReadPackedRefs(self):
103 path = os.path.join(self._gitdir, 'packed-refs')
104 try:
Chirayu Desai0eb35cb2013-11-19 18:46:29 +0530105 fd = open(path, 'r')
Shawn O. Pearced237b692009-04-17 18:49:50 -0700106 mtime = os.path.getmtime(path)
107 except IOError:
108 return
109 except OSError:
110 return
111 try:
112 for line in fd:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530113 line = str(line)
Shawn O. Pearced237b692009-04-17 18:49:50 -0700114 if line[0] == '#':
115 continue
116 if line[0] == '^':
117 continue
118
119 line = line[:-1]
120 p = line.split(' ')
David Pursehouse8a68ff92012-09-24 12:15:13 +0900121 ref_id = p[0]
Shawn O. Pearced237b692009-04-17 18:49:50 -0700122 name = p[1]
123
David Pursehouse8a68ff92012-09-24 12:15:13 +0900124 self._phyref[name] = ref_id
Shawn O. Pearced237b692009-04-17 18:49:50 -0700125 finally:
126 fd.close()
127 self._mtime['packed-refs'] = mtime
128
129 def _ReadLoose(self, prefix):
130 base = os.path.join(self._gitdir, prefix)
Renaud Paquaybed8b622018-09-27 10:46:58 -0700131 for name in platform_utils.listdir(base):
Shawn O. Pearced237b692009-04-17 18:49:50 -0700132 p = os.path.join(base, name)
Mike Frysinger6d1faa12021-02-27 21:27:04 -0500133 # We don't implement the full ref validation algorithm, just the simple
134 # rules that would show up in local filesystems.
135 # https://git-scm.com/docs/git-check-ref-format
136 if name.startswith('.') or name.endswith('.lock'):
137 pass
138 elif platform_utils.isdir(p):
Shawn O. Pearced237b692009-04-17 18:49:50 -0700139 self._mtime[prefix] = os.path.getmtime(base)
140 self._ReadLoose(prefix + name + '/')
Shawn O. Pearced237b692009-04-17 18:49:50 -0700141 else:
142 self._ReadLoose1(p, prefix + name)
143
144 def _ReadLoose1(self, path, name):
145 try:
Mike Frysinger3164d402019-11-11 05:40:22 -0500146 with open(path) as fd:
Shawn O. Pearcecc14fa92011-11-29 12:32:56 -0800147 mtime = os.path.getmtime(path)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900148 ref_id = fd.readline()
Mike Frysinger6d1faa12021-02-27 21:27:04 -0500149 except (OSError, UnicodeError):
Mike Frysinger3164d402019-11-11 05:40:22 -0500150 return
Shawn O. Pearced237b692009-04-17 18:49:50 -0700151
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530152 try:
153 ref_id = ref_id.decode()
154 except AttributeError:
155 pass
David Pursehouse8a68ff92012-09-24 12:15:13 +0900156 if not ref_id:
Shawn O. Pearced237b692009-04-17 18:49:50 -0700157 return
David Pursehouse8a68ff92012-09-24 12:15:13 +0900158 ref_id = ref_id[:-1]
Shawn O. Pearced237b692009-04-17 18:49:50 -0700159
David Pursehouse8a68ff92012-09-24 12:15:13 +0900160 if ref_id.startswith('ref: '):
161 self._symref[name] = ref_id[5:]
Shawn O. Pearced237b692009-04-17 18:49:50 -0700162 else:
David Pursehouse8a68ff92012-09-24 12:15:13 +0900163 self._phyref[name] = ref_id
Shawn O. Pearced237b692009-04-17 18:49:50 -0700164 self._mtime[name] = mtime