blob: be8d271b25c9632def6bba67efe74c89aa33199a [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
18from git_command 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/'
24R_M = 'refs/remotes/m/'
25
26
27class GitRefs(object):
28 def __init__(self, gitdir):
29 self._gitdir = gitdir
30 self._phyref = None
31 self._symref = None
32 self._mtime = {}
33
34 @property
35 def all(self):
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -070036 self._EnsureLoaded()
Shawn O. Pearced237b692009-04-17 18:49:50 -070037 return self._phyref
38
39 def get(self, name):
40 try:
41 return self.all[name]
42 except KeyError:
43 return ''
44
Shawn O. Pearcefbcde472009-04-17 20:58:02 -070045 def deleted(self, name):
46 if self._phyref is not None:
47 if name in self._phyref:
48 del self._phyref[name]
49
50 if name in self._symref:
51 del self._symref[name]
52
53 if name in self._mtime:
54 del self._mtime[name]
55
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -070056 def symref(self, name):
57 try:
58 self._EnsureLoaded()
59 return self._symref[name]
60 except KeyError:
61 return ''
62
63 def _EnsureLoaded(self):
64 if self._phyref is None or self._NeedUpdate():
65 self._LoadAll()
66
Shawn O. Pearced237b692009-04-17 18:49:50 -070067 def _NeedUpdate(self):
Shawn O. Pearceb81ac9e2009-04-17 20:44:26 -070068 if TRACE:
69 print >>sys.stderr, ': scan refs %s' % self._gitdir
Shawn O. Pearced237b692009-04-17 18:49:50 -070070 for name, mtime in self._mtime.iteritems():
71 try:
72 if mtime != os.path.getmtime(os.path.join(self._gitdir, name)):
73 return True
74 except OSError:
75 return True
76 return False
77
78 def _LoadAll(self):
Shawn O. Pearceb81ac9e2009-04-17 20:44:26 -070079 if TRACE:
80 print >>sys.stderr, ': load refs %s' % self._gitdir
Shawn O. Pearced237b692009-04-17 18:49:50 -070081 self._phyref = {}
82 self._symref = {}
83 self._mtime = {}
84
85 self._ReadPackedRefs()
86 self._ReadLoose('refs/')
87 self._ReadLoose1(os.path.join(self._gitdir, HEAD), HEAD)
88
89 scan = self._symref
90 attempts = 0
91 while scan and attempts < 5:
92 scan_next = {}
93 for name, dest in scan.iteritems():
94 if dest in self._phyref:
95 self._phyref[name] = self._phyref[dest]
96 else:
97 scan_next[name] = dest
98 scan = scan_next
99 attempts += 1
100
101 def _ReadPackedRefs(self):
102 path = os.path.join(self._gitdir, 'packed-refs')
103 try:
104 fd = open(path, 'r')
105 mtime = os.path.getmtime(path)
106 except IOError:
107 return
108 except OSError:
109 return
110 try:
111 for line in fd:
112 if line[0] == '#':
113 continue
114 if line[0] == '^':
115 continue
116
117 line = line[:-1]
118 p = line.split(' ')
119 id = p[0]
120 name = p[1]
121
122 self._phyref[name] = id
123 finally:
124 fd.close()
125 self._mtime['packed-refs'] = mtime
126
127 def _ReadLoose(self, prefix):
128 base = os.path.join(self._gitdir, prefix)
129 for name in os.listdir(base):
130 p = os.path.join(base, name)
131 if os.path.isdir(p):
132 self._mtime[prefix] = os.path.getmtime(base)
133 self._ReadLoose(prefix + name + '/')
134 elif name.endswith('.lock'):
135 pass
136 else:
137 self._ReadLoose1(p, prefix + name)
138
139 def _ReadLoose1(self, path, name):
140 try:
141 fd = open(path, 'r')
142 mtime = os.path.getmtime(path)
143 except OSError:
144 return
145 except IOError:
146 return
147 try:
148 id = fd.readline()
149 finally:
150 fd.close()
151
152 if not id:
153 return
154 id = id[:-1]
155
156 if id.startswith('ref: '):
157 self._symref[name] = id[5:]
158 else:
159 self._phyref[name] = id
160 self._mtime[name] = mtime