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 | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 17 | from trace import Trace |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 18 | |
| 19 | HEAD = 'HEAD' |
| 20 | R_HEADS = 'refs/heads/' |
| 21 | R_TAGS = 'refs/tags/' |
| 22 | R_PUB = 'refs/published/' |
| 23 | R_M = 'refs/remotes/m/' |
| 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 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 69 | for name, mtime in self._mtime.items(): |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 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 = {} |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 92 | for name, dest in scan.items(): |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 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: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 111 | line = str(line) |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 112 | if line[0] == '#': |
| 113 | continue |
| 114 | if line[0] == '^': |
| 115 | continue |
| 116 | |
| 117 | line = line[:-1] |
| 118 | p = line.split(' ') |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 119 | ref_id = p[0] |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 120 | name = p[1] |
| 121 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 122 | self._phyref[name] = ref_id |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 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') |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 142 | except IOError: |
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) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 148 | ref_id = fd.readline() |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 149 | except (IOError, OSError): |
Shawn O. Pearce | cc14fa9 | 2011-11-29 12:32:56 -0800 | [diff] [blame] | 150 | return |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 151 | finally: |
| 152 | fd.close() |
| 153 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 154 | try: |
| 155 | ref_id = ref_id.decode() |
| 156 | except AttributeError: |
| 157 | pass |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 158 | if not ref_id: |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 159 | return |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 160 | ref_id = ref_id[:-1] |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 161 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 162 | if ref_id.startswith('ref: '): |
| 163 | self._symref[name] = ref_id[5:] |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 164 | else: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 165 | self._phyref[name] = ref_id |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 166 | self._mtime[name] = mtime |