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/' |
| 24 | R_M = 'refs/remotes/m/' |
| 25 | |
| 26 | |
| 27 | class 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. Pearce | 0f3dd23 | 2009-04-17 20:32:44 -0700 | [diff] [blame] | 36 | self._EnsureLoaded() |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 37 | return self._phyref |
| 38 | |
| 39 | def get(self, name): |
| 40 | try: |
| 41 | return self.all[name] |
| 42 | except KeyError: |
| 43 | return '' |
| 44 | |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 45 | 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. Pearce | 0f3dd23 | 2009-04-17 20:32:44 -0700 | [diff] [blame] | 56 | 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. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 67 | def _NeedUpdate(self): |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 68 | Trace(': scan refs %s', self._gitdir) |
| 69 | |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 70 | 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. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 79 | Trace(': load refs %s', self._gitdir) |
| 80 | |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 81 | 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: |
Shawn O. Pearce | 76ca9f8 | 2009-04-18 14:48:03 -0700 | [diff] [blame] | 104 | fd = open(path, 'rb') |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 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: |
Shawn O. Pearce | 76ca9f8 | 2009-04-18 14:48:03 -0700 | [diff] [blame] | 141 | fd = open(path, 'rb') |
Shawn O. Pearce | cc14fa9 | 2011-11-29 12:32:56 -0800 | [diff] [blame] | 142 | except: |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 143 | return |
Shawn O. Pearce | cc14fa9 | 2011-11-29 12:32:56 -0800 | [diff] [blame] | 144 | |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 145 | try: |
Shawn O. Pearce | cc14fa9 | 2011-11-29 12:32:56 -0800 | [diff] [blame] | 146 | try: |
| 147 | mtime = os.path.getmtime(path) |
| 148 | id = fd.readline() |
| 149 | except: |
| 150 | return |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 151 | finally: |
| 152 | fd.close() |
| 153 | |
| 154 | if not id: |
| 155 | return |
| 156 | id = id[:-1] |
| 157 | |
| 158 | if id.startswith('ref: '): |
| 159 | self._symref[name] = id[5:] |
| 160 | else: |
| 161 | self._phyref[name] = id |
| 162 | self._mtime[name] = mtime |