Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 1 | # |
| 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 | |
| 16 | import os |
Shawn O. Pearce | b81ac9e | 2009-04-17 20:44:26 -0700 | [diff] [blame] | 17 | import sys |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 18 | from trace import Trace |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 19 | |
| 20 | HEAD = 'HEAD' |
| 21 | R_HEADS = 'refs/heads/' |
| 22 | R_TAGS = 'refs/tags/' |
| 23 | R_PUB = 'refs/published/' |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 24 | |
| 25 | |
| 26 | class 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. Pearce | 0f3dd23 | 2009-04-17 20:32:44 -0700 | [diff] [blame] | 35 | self._EnsureLoaded() |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 36 | return self._phyref |
| 37 | |
| 38 | def get(self, name): |
| 39 | try: |
| 40 | return self.all[name] |
| 41 | except KeyError: |
| 42 | return '' |
| 43 | |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 44 | 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. Pearce | 0f3dd23 | 2009-04-17 20:32:44 -0700 | [diff] [blame] | 55 | 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. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 66 | def _NeedUpdate(self): |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 67 | Trace(': scan refs %s', self._gitdir) |
| 68 | |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 69 | 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. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 78 | Trace(': load refs %s', self._gitdir) |
| 79 | |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 80 | 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. Pearce | 76ca9f8 | 2009-04-18 14:48:03 -0700 | [diff] [blame] | 103 | fd = open(path, 'rb') |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 104 | 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. Pearce | 76ca9f8 | 2009-04-18 14:48:03 -0700 | [diff] [blame] | 140 | fd = open(path, 'rb') |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 141 | 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 |