git_command.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. import platform_utils
  23. from trace import REPO_TRACE, IsTrace, Trace
  24. from wrapper import Wrapper
  25. GIT = 'git'
  26. MIN_GIT_VERSION = (1, 5, 4)
  27. GIT_DIR = 'GIT_DIR'
  28. LAST_GITDIR = None
  29. LAST_CWD = None
  30. _ssh_proxy_path = None
  31. _ssh_sock_path = None
  32. _ssh_clients = []
  33. def ssh_sock(create=True):
  34. global _ssh_sock_path
  35. if _ssh_sock_path is None:
  36. if not create:
  37. return None
  38. tmp_dir = '/tmp'
  39. if not os.path.exists(tmp_dir):
  40. tmp_dir = tempfile.gettempdir()
  41. _ssh_sock_path = os.path.join(
  42. tempfile.mkdtemp('', 'ssh-', tmp_dir),
  43. 'master-%r@%h:%p')
  44. return _ssh_sock_path
  45. def _ssh_proxy():
  46. global _ssh_proxy_path
  47. if _ssh_proxy_path is None:
  48. _ssh_proxy_path = os.path.join(
  49. os.path.dirname(__file__),
  50. 'git_ssh')
  51. return _ssh_proxy_path
  52. def _add_ssh_client(p):
  53. _ssh_clients.append(p)
  54. def _remove_ssh_client(p):
  55. try:
  56. _ssh_clients.remove(p)
  57. except ValueError:
  58. pass
  59. def terminate_ssh_clients():
  60. global _ssh_clients
  61. for p in _ssh_clients:
  62. try:
  63. os.kill(p.pid, SIGTERM)
  64. p.wait()
  65. except OSError:
  66. pass
  67. _ssh_clients = []
  68. _git_version = None
  69. class _GitCall(object):
  70. def version(self):
  71. p = GitCommand(None, ['--version'], capture_stdout=True)
  72. if p.Wait() == 0:
  73. if hasattr(p.stdout, 'decode'):
  74. return p.stdout.decode('utf-8')
  75. else:
  76. return p.stdout
  77. return None
  78. def version_tuple(self):
  79. global _git_version
  80. if _git_version is None:
  81. ver_str = git.version()
  82. _git_version = Wrapper().ParseGitVersion(ver_str)
  83. if _git_version is None:
  84. print('fatal: "%s" unsupported' % ver_str, file=sys.stderr)
  85. sys.exit(1)
  86. return _git_version
  87. def __getattr__(self, name):
  88. name = name.replace('_','-')
  89. def fun(*cmdv):
  90. command = [name]
  91. command.extend(cmdv)
  92. return GitCommand(None, command).Wait() == 0
  93. return fun
  94. git = _GitCall()
  95. def git_require(min_version, fail=False):
  96. git_version = git.version_tuple()
  97. if min_version <= git_version:
  98. return True
  99. if fail:
  100. need = '.'.join(map(str, min_version))
  101. print('fatal: git %s or later required' % need, file=sys.stderr)
  102. sys.exit(1)
  103. return False
  104. def _setenv(env, name, value):
  105. env[name] = value.encode()
  106. class GitCommand(object):
  107. def __init__(self,
  108. project,
  109. cmdv,
  110. bare = False,
  111. provide_stdin = False,
  112. capture_stdout = False,
  113. capture_stderr = False,
  114. disable_editor = False,
  115. ssh_proxy = False,
  116. cwd = None,
  117. gitdir = None):
  118. env = os.environ.copy()
  119. for key in [REPO_TRACE,
  120. GIT_DIR,
  121. 'GIT_ALTERNATE_OBJECT_DIRECTORIES',
  122. 'GIT_OBJECT_DIRECTORY',
  123. 'GIT_WORK_TREE',
  124. 'GIT_GRAFT_FILE',
  125. 'GIT_INDEX_FILE']:
  126. if key in env:
  127. del env[key]
  128. # If we are not capturing std* then need to print it.
  129. self.tee = {'stdout': not capture_stdout, 'stderr': not capture_stderr}
  130. if disable_editor:
  131. _setenv(env, 'GIT_EDITOR', ':')
  132. if ssh_proxy:
  133. _setenv(env, 'REPO_SSH_SOCK', ssh_sock())
  134. _setenv(env, 'GIT_SSH', _ssh_proxy())
  135. _setenv(env, 'GIT_SSH_VARIANT', 'ssh')
  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 'GIT_ALLOW_PROTOCOL' not in env:
  143. _setenv(env, 'GIT_ALLOW_PROTOCOL',
  144. 'file:git:http:https:ssh:persistent-http:persistent-https:sso:rpc')
  145. if project:
  146. if not cwd:
  147. cwd = project.worktree
  148. if not gitdir:
  149. gitdir = project.gitdir
  150. command = [GIT]
  151. if bare:
  152. if gitdir:
  153. _setenv(env, GIT_DIR, gitdir)
  154. cwd = None
  155. command.append(cmdv[0])
  156. # Need to use the --progress flag for fetch/clone so output will be
  157. # displayed as by default git only does progress output if stderr is a TTY.
  158. if sys.stderr.isatty() and cmdv[0] in ('fetch', 'clone'):
  159. if '--progress' not in cmdv and '--quiet' not in cmdv:
  160. command.append('--progress')
  161. command.extend(cmdv[1:])
  162. if provide_stdin:
  163. stdin = subprocess.PIPE
  164. else:
  165. stdin = None
  166. stdout = subprocess.PIPE
  167. stderr = subprocess.PIPE
  168. if IsTrace():
  169. global LAST_CWD
  170. global LAST_GITDIR
  171. dbg = ''
  172. if cwd and LAST_CWD != cwd:
  173. if LAST_GITDIR or LAST_CWD:
  174. dbg += '\n'
  175. dbg += ': cd %s\n' % cwd
  176. LAST_CWD = cwd
  177. if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]:
  178. if LAST_GITDIR or LAST_CWD:
  179. dbg += '\n'
  180. dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR]
  181. LAST_GITDIR = env[GIT_DIR]
  182. dbg += ': '
  183. dbg += ' '.join(command)
  184. if stdin == subprocess.PIPE:
  185. dbg += ' 0<|'
  186. if stdout == subprocess.PIPE:
  187. dbg += ' 1>|'
  188. if stderr == subprocess.PIPE:
  189. dbg += ' 2>|'
  190. Trace('%s', dbg)
  191. try:
  192. p = subprocess.Popen(command,
  193. cwd = cwd,
  194. env = env,
  195. stdin = stdin,
  196. stdout = stdout,
  197. stderr = stderr)
  198. except Exception as e:
  199. raise GitError('%s: %s' % (command[1], e))
  200. if ssh_proxy:
  201. _add_ssh_client(p)
  202. self.process = p
  203. self.stdin = p.stdin
  204. def Wait(self):
  205. try:
  206. p = self.process
  207. rc = self._CaptureOutput()
  208. finally:
  209. _remove_ssh_client(p)
  210. return rc
  211. def _CaptureOutput(self):
  212. p = self.process
  213. s_in = platform_utils.FileDescriptorStreams.create()
  214. s_in.add(p.stdout, sys.stdout, 'stdout')
  215. s_in.add(p.stderr, sys.stderr, 'stderr')
  216. self.stdout = ''
  217. self.stderr = ''
  218. while not s_in.is_done:
  219. in_ready = s_in.select()
  220. for s in in_ready:
  221. buf = s.read()
  222. if not buf:
  223. s_in.remove(s)
  224. continue
  225. if not hasattr(buf, 'encode'):
  226. buf = buf.decode()
  227. if s.std_name == 'stdout':
  228. self.stdout += buf
  229. else:
  230. self.stderr += buf
  231. if self.tee[s.std_name]:
  232. s.dest.write(buf)
  233. s.dest.flush()
  234. return p.wait()