Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 1 | # Copyright (C) 2009 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import os |
Mike Frysinger | 8a11f6f | 2019-08-27 00:26:15 -0400 | [diff] [blame] | 16 | from repo_trace import Trace |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 17 | import platform_utils |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 18 | |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 19 | HEAD = 'HEAD' |
Zac Livingston | 9ead97b | 2017-06-13 08:29:04 -0600 | [diff] [blame] | 20 | R_CHANGES = 'refs/changes/' |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 21 | R_HEADS = 'refs/heads/' |
| 22 | R_TAGS = 'refs/tags/' |
| 23 | R_PUB = 'refs/published/' |
Mike Frysinger | 21b7fbe | 2020-02-26 23:53:36 -0500 | [diff] [blame] | 24 | R_WORKTREE = 'refs/worktree/' |
| 25 | R_WORKTREE_M = R_WORKTREE + 'm/' |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 26 | R_M = 'refs/remotes/m/' |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 27 | |
| 28 | |
| 29 | class GitRefs(object): |
| 30 | def __init__(self, gitdir): |
| 31 | self._gitdir = gitdir |
| 32 | self._phyref = None |
| 33 | self._symref = None |
| 34 | self._mtime = {} |
| 35 | |
| 36 | @property |
| 37 | def all(self): |
Shawn O. Pearce | 0f3dd23 | 2009-04-17 20:32:44 -0700 | [diff] [blame] | 38 | self._EnsureLoaded() |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 39 | return self._phyref |
| 40 | |
| 41 | def get(self, name): |
| 42 | try: |
| 43 | return self.all[name] |
| 44 | except KeyError: |
| 45 | return '' |
| 46 | |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 47 | def deleted(self, name): |
| 48 | if self._phyref is not None: |
| 49 | if name in self._phyref: |
| 50 | del self._phyref[name] |
| 51 | |
| 52 | if name in self._symref: |
| 53 | del self._symref[name] |
| 54 | |
| 55 | if name in self._mtime: |
| 56 | del self._mtime[name] |
| 57 | |
Shawn O. Pearce | 0f3dd23 | 2009-04-17 20:32:44 -0700 | [diff] [blame] | 58 | def symref(self, name): |
| 59 | try: |
| 60 | self._EnsureLoaded() |
| 61 | return self._symref[name] |
| 62 | except KeyError: |
| 63 | return '' |
| 64 | |
| 65 | def _EnsureLoaded(self): |
| 66 | if self._phyref is None or self._NeedUpdate(): |
| 67 | self._LoadAll() |
| 68 | |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 69 | def _NeedUpdate(self): |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 70 | with Trace(': scan refs %s', self._gitdir): |
| 71 | for name, mtime in self._mtime.items(): |
| 72 | try: |
| 73 | if mtime != os.path.getmtime(os.path.join(self._gitdir, name)): |
| 74 | return True |
| 75 | except OSError: |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 76 | return True |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 77 | return False |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 78 | |
| 79 | def _LoadAll(self): |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 80 | with Trace(': load refs %s', self._gitdir): |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 81 | |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 82 | self._phyref = {} |
| 83 | self._symref = {} |
| 84 | self._mtime = {} |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 85 | |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 86 | self._ReadPackedRefs() |
| 87 | self._ReadLoose('refs/') |
| 88 | self._ReadLoose1(os.path.join(self._gitdir, HEAD), HEAD) |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 89 | |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 90 | scan = self._symref |
| 91 | attempts = 0 |
| 92 | while scan and attempts < 5: |
| 93 | scan_next = {} |
| 94 | for name, dest in scan.items(): |
| 95 | if dest in self._phyref: |
| 96 | self._phyref[name] = self._phyref[dest] |
| 97 | else: |
| 98 | scan_next[name] = dest |
| 99 | scan = scan_next |
| 100 | attempts += 1 |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 101 | |
| 102 | def _ReadPackedRefs(self): |
| 103 | path = os.path.join(self._gitdir, 'packed-refs') |
| 104 | try: |
Chirayu Desai | 0eb35cb | 2013-11-19 18:46:29 +0530 | [diff] [blame] | 105 | fd = open(path, 'r') |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 106 | mtime = os.path.getmtime(path) |
| 107 | except IOError: |
| 108 | return |
| 109 | except OSError: |
| 110 | return |
| 111 | try: |
| 112 | for line in fd: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 113 | line = str(line) |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 114 | if line[0] == '#': |
| 115 | continue |
| 116 | if line[0] == '^': |
| 117 | continue |
| 118 | |
| 119 | line = line[:-1] |
| 120 | p = line.split(' ') |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 121 | ref_id = p[0] |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 122 | name = p[1] |
| 123 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 124 | self._phyref[name] = ref_id |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 125 | finally: |
| 126 | fd.close() |
| 127 | self._mtime['packed-refs'] = mtime |
| 128 | |
| 129 | def _ReadLoose(self, prefix): |
| 130 | base = os.path.join(self._gitdir, prefix) |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 131 | for name in platform_utils.listdir(base): |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 132 | p = os.path.join(base, name) |
Mike Frysinger | 6d1faa1 | 2021-02-27 21:27:04 -0500 | [diff] [blame] | 133 | # We don't implement the full ref validation algorithm, just the simple |
| 134 | # rules that would show up in local filesystems. |
| 135 | # https://git-scm.com/docs/git-check-ref-format |
| 136 | if name.startswith('.') or name.endswith('.lock'): |
| 137 | pass |
| 138 | elif platform_utils.isdir(p): |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 139 | self._mtime[prefix] = os.path.getmtime(base) |
| 140 | self._ReadLoose(prefix + name + '/') |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 141 | else: |
| 142 | self._ReadLoose1(p, prefix + name) |
| 143 | |
| 144 | def _ReadLoose1(self, path, name): |
| 145 | try: |
Mike Frysinger | 3164d40 | 2019-11-11 05:40:22 -0500 | [diff] [blame] | 146 | with open(path) as fd: |
Shawn O. Pearce | cc14fa9 | 2011-11-29 12:32:56 -0800 | [diff] [blame] | 147 | mtime = os.path.getmtime(path) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 148 | ref_id = fd.readline() |
Mike Frysinger | 6d1faa1 | 2021-02-27 21:27:04 -0500 | [diff] [blame] | 149 | except (OSError, UnicodeError): |
Mike Frysinger | 3164d40 | 2019-11-11 05:40:22 -0500 | [diff] [blame] | 150 | return |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 151 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 152 | try: |
| 153 | ref_id = ref_id.decode() |
| 154 | except AttributeError: |
| 155 | pass |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 156 | if not ref_id: |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 157 | return |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 158 | ref_id = ref_id[:-1] |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 159 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 160 | if ref_id.startswith('ref: '): |
| 161 | self._symref[name] = ref_id[5:] |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 162 | else: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 163 | self._phyref[name] = ref_id |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 164 | self._mtime[name] = mtime |