git_command.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #
  2. # Copyright (C) 2008 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. import sys
  17. import subprocess
  18. import tempfile
  19. from signal import SIGTERM
  20. from error import GitError
  21. from trace import REPO_TRACE, IsTrace, Trace
  22. GIT = 'git'
  23. MIN_GIT_VERSION = (1, 5, 4)
  24. GIT_DIR = 'GIT_DIR'
  25. LAST_GITDIR = None
  26. LAST_CWD = None
  27. _ssh_proxy_path = None
  28. _ssh_sock_path = None
  29. _ssh_clients = []
  30. def ssh_sock(create=True):
  31. global _ssh_sock_path
  32. if _ssh_sock_path is None:
  33. if not create:
  34. return None
  35. dir = '/tmp'
  36. if not os.path.exists(dir):
  37. dir = tempfile.gettempdir()
  38. _ssh_sock_path = os.path.join(
  39. tempfile.mkdtemp('', 'ssh-', dir),
  40. 'master-%r@%h:%p')
  41. return _ssh_sock_path
  42. def _ssh_proxy():
  43. global _ssh_proxy_path
  44. if _ssh_proxy_path is None:
  45. _ssh_proxy_path = os.path.join(
  46. os.path.dirname(__file__),
  47. 'git_ssh')
  48. return _ssh_proxy_path
  49. def _add_ssh_client(p):
  50. _ssh_clients.append(p)
  51. def _remove_ssh_client(p):
  52. try:
  53. _ssh_clients.remove(p)
  54. except ValueError:
  55. pass
  56. def terminate_ssh_clients():
  57. global _ssh_clients
  58. for p in _ssh_clients:
  59. try:
  60. os.kill(p.pid, SIGTERM)
  61. p.wait()
  62. except OSError:
  63. pass
  64. _ssh_clients = []
  65. class _GitCall(object):
  66. def version(self):
  67. p = GitCommand(None, ['--version'], capture_stdout=True)
  68. if p.Wait() == 0:
  69. return p.stdout
  70. return None
  71. def __getattr__(self, name):
  72. name = name.replace('_','-')
  73. def fun(*cmdv):
  74. command = [name]
  75. command.extend(cmdv)
  76. return GitCommand(None, command).Wait() == 0
  77. return fun
  78. git = _GitCall()
  79. _git_version = None
  80. def git_require(min_version, fail=False):
  81. global _git_version
  82. if _git_version is None:
  83. ver_str = git.version()
  84. if ver_str.startswith('git version '):
  85. _git_version = tuple(
  86. map(lambda x: int(x),
  87. ver_str[len('git version '):].strip().split('.')[0:3]
  88. ))
  89. else:
  90. print >>sys.stderr, 'fatal: "%s" unsupported' % ver_str
  91. sys.exit(1)
  92. if min_version <= _git_version:
  93. return True
  94. if fail:
  95. need = '.'.join(map(lambda x: str(x), min_version))
  96. print >>sys.stderr, 'fatal: git %s or later required' % need
  97. sys.exit(1)
  98. return False
  99. class GitCommand(object):
  100. def __init__(self,
  101. project,
  102. cmdv,
  103. bare = False,
  104. provide_stdin = False,
  105. capture_stdout = False,
  106. capture_stderr = False,
  107. disable_editor = False,
  108. ssh_proxy = False,
  109. cwd = None,
  110. gitdir = None):
  111. env = dict(os.environ)
  112. for e in [REPO_TRACE,
  113. GIT_DIR,
  114. 'GIT_ALTERNATE_OBJECT_DIRECTORIES',
  115. 'GIT_OBJECT_DIRECTORY',
  116. 'GIT_WORK_TREE',
  117. 'GIT_GRAFT_FILE',
  118. 'GIT_INDEX_FILE']:
  119. if e in env:
  120. del env[e]
  121. if disable_editor:
  122. env['GIT_EDITOR'] = ':'
  123. if ssh_proxy:
  124. env['REPO_SSH_SOCK'] = ssh_sock()
  125. env['GIT_SSH'] = _ssh_proxy()
  126. if project:
  127. if not cwd:
  128. cwd = project.worktree
  129. if not gitdir:
  130. gitdir = project.gitdir
  131. command = [GIT]
  132. if bare:
  133. if gitdir:
  134. env[GIT_DIR] = gitdir
  135. cwd = None
  136. command.extend(cmdv)
  137. if provide_stdin:
  138. stdin = subprocess.PIPE
  139. else:
  140. stdin = None
  141. if capture_stdout:
  142. stdout = subprocess.PIPE
  143. else:
  144. stdout = None
  145. if capture_stderr:
  146. stderr = subprocess.PIPE
  147. else:
  148. stderr = None
  149. if IsTrace():
  150. global LAST_CWD
  151. global LAST_GITDIR
  152. dbg = ''
  153. if cwd and LAST_CWD != cwd:
  154. if LAST_GITDIR or LAST_CWD:
  155. dbg += '\n'
  156. dbg += ': cd %s\n' % cwd
  157. LAST_CWD = cwd
  158. if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]:
  159. if LAST_GITDIR or LAST_CWD:
  160. dbg += '\n'
  161. dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR]
  162. LAST_GITDIR = env[GIT_DIR]
  163. dbg += ': '
  164. dbg += ' '.join(command)
  165. if stdin == subprocess.PIPE:
  166. dbg += ' 0<|'
  167. if stdout == subprocess.PIPE:
  168. dbg += ' 1>|'
  169. if stderr == subprocess.PIPE:
  170. dbg += ' 2>|'
  171. Trace('%s', dbg)
  172. try:
  173. p = subprocess.Popen(command,
  174. cwd = cwd,
  175. env = env,
  176. stdin = stdin,
  177. stdout = stdout,
  178. stderr = stderr)
  179. except Exception, e:
  180. raise GitError('%s: %s' % (command[1], e))
  181. if ssh_proxy:
  182. _add_ssh_client(p)
  183. self.process = p
  184. self.stdin = p.stdin
  185. def Wait(self):
  186. p = self.process
  187. if p.stdin:
  188. p.stdin.close()
  189. self.stdin = None
  190. if p.stdout:
  191. self.stdout = p.stdout.read()
  192. p.stdout.close()
  193. else:
  194. p.stdout = None
  195. if p.stderr:
  196. self.stderr = p.stderr.read()
  197. p.stderr.close()
  198. else:
  199. p.stderr = None
  200. try:
  201. rc = p.wait()
  202. finally:
  203. _remove_ssh_client(p)
  204. return rc