git_command.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. def _setenv(env, name, value):
  100. env[name] = value.encode()
  101. class GitCommand(object):
  102. def __init__(self,
  103. project,
  104. cmdv,
  105. bare = False,
  106. provide_stdin = False,
  107. capture_stdout = False,
  108. capture_stderr = False,
  109. disable_editor = False,
  110. ssh_proxy = False,
  111. cwd = None,
  112. gitdir = None):
  113. env = os.environ.copy()
  114. for e in [REPO_TRACE,
  115. GIT_DIR,
  116. 'GIT_ALTERNATE_OBJECT_DIRECTORIES',
  117. 'GIT_OBJECT_DIRECTORY',
  118. 'GIT_WORK_TREE',
  119. 'GIT_GRAFT_FILE',
  120. 'GIT_INDEX_FILE']:
  121. if e in env:
  122. del env[e]
  123. if disable_editor:
  124. _setenv(env, 'GIT_EDITOR', ':')
  125. if ssh_proxy:
  126. _setenv(env, 'REPO_SSH_SOCK', ssh_sock())
  127. _setenv(env, 'GIT_SSH', _ssh_proxy())
  128. if project:
  129. if not cwd:
  130. cwd = project.worktree
  131. if not gitdir:
  132. gitdir = project.gitdir
  133. command = [GIT]
  134. if bare:
  135. if gitdir:
  136. _setenv(env, GIT_DIR, gitdir)
  137. cwd = None
  138. command.extend(cmdv)
  139. if provide_stdin:
  140. stdin = subprocess.PIPE
  141. else:
  142. stdin = None
  143. if capture_stdout:
  144. stdout = subprocess.PIPE
  145. else:
  146. stdout = None
  147. if capture_stderr:
  148. stderr = subprocess.PIPE
  149. else:
  150. stderr = None
  151. if IsTrace():
  152. global LAST_CWD
  153. global LAST_GITDIR
  154. dbg = ''
  155. if cwd and LAST_CWD != cwd:
  156. if LAST_GITDIR or LAST_CWD:
  157. dbg += '\n'
  158. dbg += ': cd %s\n' % cwd
  159. LAST_CWD = cwd
  160. if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]:
  161. if LAST_GITDIR or LAST_CWD:
  162. dbg += '\n'
  163. dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR]
  164. LAST_GITDIR = env[GIT_DIR]
  165. dbg += ': '
  166. dbg += ' '.join(command)
  167. if stdin == subprocess.PIPE:
  168. dbg += ' 0<|'
  169. if stdout == subprocess.PIPE:
  170. dbg += ' 1>|'
  171. if stderr == subprocess.PIPE:
  172. dbg += ' 2>|'
  173. Trace('%s', dbg)
  174. try:
  175. p = subprocess.Popen(command,
  176. cwd = cwd,
  177. env = env,
  178. stdin = stdin,
  179. stdout = stdout,
  180. stderr = stderr)
  181. except Exception, e:
  182. raise GitError('%s: %s' % (command[1], e))
  183. if ssh_proxy:
  184. _add_ssh_client(p)
  185. self.process = p
  186. self.stdin = p.stdin
  187. def Wait(self):
  188. p = self.process
  189. if p.stdin:
  190. p.stdin.close()
  191. self.stdin = None
  192. if p.stdout:
  193. self.stdout = p.stdout.read()
  194. p.stdout.close()
  195. else:
  196. p.stdout = None
  197. if p.stderr:
  198. self.stderr = p.stderr.read()
  199. p.stderr.close()
  200. else:
  201. p.stderr = None
  202. try:
  203. rc = p.wait()
  204. finally:
  205. _remove_ssh_client(p)
  206. return rc