git_refs.py 3.4 KB

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