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 |
| 17 | |
| 18 | HEAD = 'HEAD' |
| 19 | R_HEADS = 'refs/heads/' |
| 20 | R_TAGS = 'refs/tags/' |
| 21 | R_PUB = 'refs/published/' |
| 22 | R_M = 'refs/remotes/m/' |
| 23 | |
| 24 | |
| 25 | class GitRefs(object): |
| 26 | def __init__(self, gitdir): |
| 27 | self._gitdir = gitdir |
| 28 | self._phyref = None |
| 29 | self._symref = None |
| 30 | self._mtime = {} |
| 31 | |
| 32 | @property |
| 33 | def all(self): |
| 34 | if self._phyref is None or self._NeedUpdate(): |
| 35 | self._LoadAll() |
| 36 | return self._phyref |
| 37 | |
| 38 | def get(self, name): |
| 39 | try: |
| 40 | return self.all[name] |
| 41 | except KeyError: |
| 42 | return '' |
| 43 | |
| 44 | def _NeedUpdate(self): |
| 45 | for name, mtime in self._mtime.iteritems(): |
| 46 | try: |
| 47 | if mtime != os.path.getmtime(os.path.join(self._gitdir, name)): |
| 48 | return True |
| 49 | except OSError: |
| 50 | return True |
| 51 | return False |
| 52 | |
| 53 | def _LoadAll(self): |
| 54 | self._phyref = {} |
| 55 | self._symref = {} |
| 56 | self._mtime = {} |
| 57 | |
| 58 | self._ReadPackedRefs() |
| 59 | self._ReadLoose('refs/') |
| 60 | self._ReadLoose1(os.path.join(self._gitdir, HEAD), HEAD) |
| 61 | |
| 62 | scan = self._symref |
| 63 | attempts = 0 |
| 64 | while scan and attempts < 5: |
| 65 | scan_next = {} |
| 66 | for name, dest in scan.iteritems(): |
| 67 | if dest in self._phyref: |
| 68 | self._phyref[name] = self._phyref[dest] |
| 69 | else: |
| 70 | scan_next[name] = dest |
| 71 | scan = scan_next |
| 72 | attempts += 1 |
| 73 | |
| 74 | def _ReadPackedRefs(self): |
| 75 | path = os.path.join(self._gitdir, 'packed-refs') |
| 76 | try: |
| 77 | fd = open(path, 'r') |
| 78 | mtime = os.path.getmtime(path) |
| 79 | except IOError: |
| 80 | return |
| 81 | except OSError: |
| 82 | return |
| 83 | try: |
| 84 | for line in fd: |
| 85 | if line[0] == '#': |
| 86 | continue |
| 87 | if line[0] == '^': |
| 88 | continue |
| 89 | |
| 90 | line = line[:-1] |
| 91 | p = line.split(' ') |
| 92 | id = p[0] |
| 93 | name = p[1] |
| 94 | |
| 95 | self._phyref[name] = id |
| 96 | finally: |
| 97 | fd.close() |
| 98 | self._mtime['packed-refs'] = mtime |
| 99 | |
| 100 | def _ReadLoose(self, prefix): |
| 101 | base = os.path.join(self._gitdir, prefix) |
| 102 | for name in os.listdir(base): |
| 103 | p = os.path.join(base, name) |
| 104 | if os.path.isdir(p): |
| 105 | self._mtime[prefix] = os.path.getmtime(base) |
| 106 | self._ReadLoose(prefix + name + '/') |
| 107 | elif name.endswith('.lock'): |
| 108 | pass |
| 109 | else: |
| 110 | self._ReadLoose1(p, prefix + name) |
| 111 | |
| 112 | def _ReadLoose1(self, path, name): |
| 113 | try: |
| 114 | fd = open(path, 'r') |
| 115 | mtime = os.path.getmtime(path) |
| 116 | except OSError: |
| 117 | return |
| 118 | except IOError: |
| 119 | return |
| 120 | try: |
| 121 | id = fd.readline() |
| 122 | finally: |
| 123 | fd.close() |
| 124 | |
| 125 | if not id: |
| 126 | return |
| 127 | id = id[:-1] |
| 128 | |
| 129 | if id.startswith('ref: '): |
| 130 | self._symref[name] = id[5:] |
| 131 | else: |
| 132 | self._phyref[name] = id |
| 133 | self._mtime[name] = mtime |