git_refs.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 _NeedUpdate(self):
  38. for name, mtime in self._mtime.iteritems():
  39. try:
  40. if mtime != os.path.getmtime(os.path.join(self._gitdir, name)):
  41. return True
  42. except OSError:
  43. return True
  44. return False
  45. def _LoadAll(self):
  46. self._phyref = {}
  47. self._symref = {}
  48. self._mtime = {}
  49. self._ReadPackedRefs()
  50. self._ReadLoose('refs/')
  51. self._ReadLoose1(os.path.join(self._gitdir, HEAD), HEAD)
  52. scan = self._symref
  53. attempts = 0
  54. while scan and attempts < 5:
  55. scan_next = {}
  56. for name, dest in scan.iteritems():
  57. if dest in self._phyref:
  58. self._phyref[name] = self._phyref[dest]
  59. else:
  60. scan_next[name] = dest
  61. scan = scan_next
  62. attempts += 1
  63. def _ReadPackedRefs(self):
  64. path = os.path.join(self._gitdir, 'packed-refs')
  65. try:
  66. fd = open(path, 'r')
  67. mtime = os.path.getmtime(path)
  68. except IOError:
  69. return
  70. except OSError:
  71. return
  72. try:
  73. for line in fd:
  74. if line[0] == '#':
  75. continue
  76. if line[0] == '^':
  77. continue
  78. line = line[:-1]
  79. p = line.split(' ')
  80. id = p[0]
  81. name = p[1]
  82. self._phyref[name] = id
  83. finally:
  84. fd.close()
  85. self._mtime['packed-refs'] = mtime
  86. def _ReadLoose(self, prefix):
  87. base = os.path.join(self._gitdir, prefix)
  88. for name in os.listdir(base):
  89. p = os.path.join(base, name)
  90. if os.path.isdir(p):
  91. self._mtime[prefix] = os.path.getmtime(base)
  92. self._ReadLoose(prefix + name + '/')
  93. elif name.endswith('.lock'):
  94. pass
  95. else:
  96. self._ReadLoose1(p, prefix + name)
  97. def _ReadLoose1(self, path, name):
  98. try:
  99. fd = open(path, 'r')
  100. mtime = os.path.getmtime(path)
  101. except OSError:
  102. return
  103. except IOError:
  104. return
  105. try:
  106. id = fd.readline()
  107. finally:
  108. fd.close()
  109. if not id:
  110. return
  111. id = id[:-1]
  112. if id.startswith('ref: '):
  113. self._symref[name] = id[5:]
  114. else:
  115. self._phyref[name] = id
  116. self._mtime[name] = mtime