git_command.py 5.7 KB

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