blob: b24a0b4e8ee74dcce32d2e98bfd939e38efe3d81 [file] [log] [blame]
Shawn O. Pearced237b692009-04-17 18:49:50 -07001#
2# Copyright (C) 2009 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import os
Shawn O. Pearceb81ac9e2009-04-17 20:44:26 -070017import sys
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070018from trace import Trace
Shawn O. Pearced237b692009-04-17 18:49:50 -070019
20HEAD = 'HEAD'
21R_HEADS = 'refs/heads/'
22R_TAGS = 'refs/tags/'
23R_PUB = 'refs/published/'
Shawn O. Pearced237b692009-04-17 18:49:50 -070024
25
26class GitRefs(object):
27 def __init__(self, gitdir):
28 self._gitdir = gitdir
29 self._phyref = None
30 self._symref = None
31 self._mtime = {}
32
33 @property
34 def all(self):
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -070035 self._EnsureLoaded()
Shawn O. Pearced237b692009-04-17 18:49:50 -070036 return self._phyref
37
38 def get(self, name):
39 try:
40 return self.all[name]
41 except KeyError:
42 return ''
43
Shawn O. Pearcefbcde472009-04-17 20:58:02 -070044 def deleted(self, name):
45 if self._phyref is not None:
46 if name in self._phyref:
47 del self._phyref[name]
48
49 if name in self._symref:
50 del self._symref[name]
51
52 if name in self._mtime:
53 del self._mtime[name]
54
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -070055 def symref(self, name):
56 try:
57 self._EnsureLoaded()
58 return self._symref[name]
59 except KeyError:
60 return ''
61
62 def _EnsureLoaded(self):
63 if self._phyref is None or self._NeedUpdate():
64 self._LoadAll()
65
Shawn O. Pearced237b692009-04-17 18:49:50 -070066 def _NeedUpdate(self):
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070067 Trace(': scan refs %s', self._gitdir)
68
Shawn O. Pearced237b692009-04-17 18:49:50 -070069 for name, mtime in self._mtime.iteritems():
70 try:
71 if mtime != os.path.getmtime(os.path.join(self._gitdir, name)):
72 return True
73 except OSError:
74 return True
75 return False
76
77 def _LoadAll(self):
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070078 Trace(': load refs %s', self._gitdir)
79
Shawn O. Pearced237b692009-04-17 18:49:50 -070080 self._phyref = {}
81 self._symref = {}
82 self._mtime = {}
83
84 self._ReadPackedRefs()
85 self._ReadLoose('refs/')
86 self._ReadLoose1(os.path.join(self._gitdir, HEAD), HEAD)
87
88 scan = self._symref
89 attempts = 0
90 while scan and attempts < 5:
91 scan_next = {}
92 for name, dest in scan.iteritems():
93 if dest in self._phyref:
94 self._phyref[name] = self._phyref[dest]
95 else:
96 scan_next[name] = dest
97 scan = scan_next
98 attempts += 1
99
100 def _ReadPackedRefs(self):
101 path = os.path.join(self._gitdir, 'packed-refs')
102 try:
Shawn O. Pearce76ca9f82009-04-18 14:48:03 -0700103 fd = open(path, 'rb')
Shawn O. Pearced237b692009-04-17 18:49:50 -0700104 mtime = os.path.getmtime(path)
105 except IOError:
106 return
107 except OSError:
108 return
109 try:
110 for line in fd:
111 if line[0] == '#':
112 continue
113 if line[0] == '^':
114 continue
115
116 line = line[:-1]
117 p = line.split(' ')
118 id = p[0]
119 name = p[1]
120
121 self._phyref[name] = id
122 finally:
123 fd.close()
124 self._mtime['packed-refs'] = mtime
125
126 def _ReadLoose(self, prefix):
127 base = os.path.join(self._gitdir, prefix)
128 for name in os.listdir(base):
129 p = os.path.join(base, name)
130 if os.path.isdir(p):
131 self._mtime[prefix] = os.path.getmtime(base)
132 self._ReadLoose(prefix + name + '/')
133 elif name.endswith('.lock'):
134 pass
135 else:
136 self._ReadLoose1(p, prefix + name)
137
138 def _ReadLoose1(self, path, name):
139 try:
Shawn O. Pearce76ca9f82009-04-18 14:48:03 -0700140 fd = open(path, 'rb')
Shawn O. Pearced237b692009-04-17 18:49:50 -0700141 mtime = os.path.getmtime(path)
142 except OSError:
143 return
144 except IOError:
145 return
146 try:
147 id = fd.readline()
148 finally:
149 fd.close()
150
151 if not id:
152 return
153 id = id[:-1]
154
155 if id.startswith('ref: '):
156 self._symref[name] = id[5:]
157 else:
158 self._phyref[name] = id
159 self._mtime[name] = mtime