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 | |
Zac Livingston | 9ead97b | 2017-06-13 08:29:04 -0600 | [diff] [blame] | 19 | HEAD = 'HEAD' |
| 20 | R_CHANGES = 'refs/changes/' |
| 21 | R_HEADS = 'refs/heads/' |
| 22 | R_TAGS = 'refs/tags/' |
| 23 | R_PUB = 'refs/published/' |
| 24 | R_M = 'refs/remotes/m/' |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 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 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 70 | for name, mtime in self._mtime.items(): |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 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 = {} |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 93 | for name, dest in scan.items(): |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 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: |
Chirayu Desai | 0eb35cb | 2013-11-19 18:46:29 +0530 | [diff] [blame] | 104 | fd = open(path, 'r') |
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: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 112 | line = str(line) |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 113 | if line[0] == '#': |
| 114 | continue |
| 115 | if line[0] == '^': |
| 116 | continue |
| 117 | |
| 118 | line = line[:-1] |
| 119 | p = line.split(' ') |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 120 | ref_id = p[0] |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 121 | name = p[1] |
| 122 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 123 | self._phyref[name] = ref_id |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 124 | finally: |
| 125 | fd.close() |
| 126 | self._mtime['packed-refs'] = mtime |
| 127 | |
| 128 | def _ReadLoose(self, prefix): |
| 129 | base = os.path.join(self._gitdir, prefix) |
| 130 | for name in os.listdir(base): |
| 131 | p = os.path.join(base, name) |
| 132 | if os.path.isdir(p): |
| 133 | self._mtime[prefix] = os.path.getmtime(base) |
| 134 | self._ReadLoose(prefix + name + '/') |
| 135 | elif name.endswith('.lock'): |
| 136 | pass |
| 137 | else: |
| 138 | self._ReadLoose1(p, prefix + name) |
| 139 | |
| 140 | def _ReadLoose1(self, path, name): |
| 141 | try: |
Renaud Paquay | 2a4be94 | 2016-11-01 13:48:15 -0700 | [diff] [blame] | 142 | fd = open(path) |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 143 | except IOError: |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 144 | return |
Shawn O. Pearce | cc14fa9 | 2011-11-29 12:32:56 -0800 | [diff] [blame] | 145 | |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 146 | try: |
Shawn O. Pearce | cc14fa9 | 2011-11-29 12:32:56 -0800 | [diff] [blame] | 147 | try: |
| 148 | mtime = os.path.getmtime(path) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 149 | ref_id = fd.readline() |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 150 | except (IOError, OSError): |
Shawn O. Pearce | cc14fa9 | 2011-11-29 12:32:56 -0800 | [diff] [blame] | 151 | return |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 152 | finally: |
| 153 | fd.close() |
| 154 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 155 | try: |
| 156 | ref_id = ref_id.decode() |
| 157 | except AttributeError: |
| 158 | pass |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 159 | if not ref_id: |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 160 | return |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 161 | ref_id = ref_id[:-1] |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 162 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 163 | if ref_id.startswith('ref: '): |
| 164 | self._symref[name] = ref_id[5:] |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 165 | else: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 166 | self._phyref[name] = ref_id |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 167 | self._mtime[name] = mtime |