git_command.py 5.9 KB

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