git_command.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. if 'http_proxy' in env and 'darwin' == sys.platform:
  136. s = "'http.proxy=%s'" % (env['http_proxy'],)
  137. p = env.get('GIT_CONFIG_PARAMETERS')
  138. if p is not None:
  139. s = p + ' ' + s
  140. _setenv(env, 'GIT_CONFIG_PARAMETERS', s)
  141. if 'GIT_ALLOW_PROTOCOL' not in env:
  142. _setenv(env, 'GIT_ALLOW_PROTOCOL',
  143. 'file:git:http:https:ssh:persistent-http:persistent-https:sso:rpc')
  144. if project:
  145. if not cwd:
  146. cwd = project.worktree
  147. if not gitdir:
  148. gitdir = project.gitdir
  149. command = [GIT]
  150. if bare:
  151. if gitdir:
  152. _setenv(env, GIT_DIR, gitdir)
  153. cwd = None
  154. command.append(cmdv[0])
  155. # Need to use the --progress flag for fetch/clone so output will be
  156. # displayed as by default git only does progress output if stderr is a TTY.
  157. if sys.stderr.isatty() and cmdv[0] in ('fetch', 'clone'):
  158. if '--progress' not in cmdv and '--quiet' not in cmdv:
  159. command.append('--progress')
  160. command.extend(cmdv[1:])
  161. if provide_stdin:
  162. stdin = subprocess.PIPE
  163. else:
  164. stdin = None
  165. stdout = subprocess.PIPE
  166. stderr = subprocess.PIPE
  167. if IsTrace():
  168. global LAST_CWD
  169. global LAST_GITDIR
  170. dbg = ''
  171. if cwd and LAST_CWD != cwd:
  172. if LAST_GITDIR or LAST_CWD:
  173. dbg += '\n'
  174. dbg += ': cd %s\n' % cwd
  175. LAST_CWD = cwd
  176. if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]:
  177. if LAST_GITDIR or LAST_CWD:
  178. dbg += '\n'
  179. dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR]
  180. LAST_GITDIR = env[GIT_DIR]
  181. dbg += ': '
  182. dbg += ' '.join(command)
  183. if stdin == subprocess.PIPE:
  184. dbg += ' 0<|'
  185. if stdout == subprocess.PIPE:
  186. dbg += ' 1>|'
  187. if stderr == subprocess.PIPE:
  188. dbg += ' 2>|'
  189. Trace('%s', dbg)
  190. try:
  191. p = subprocess.Popen(command,
  192. cwd = cwd,
  193. env = env,
  194. stdin = stdin,
  195. stdout = stdout,
  196. stderr = stderr)
  197. except Exception as e:
  198. raise GitError('%s: %s' % (command[1], e))
  199. if ssh_proxy:
  200. _add_ssh_client(p)
  201. self.process = p
  202. self.stdin = p.stdin
  203. def Wait(self):
  204. try:
  205. p = self.process
  206. rc = self._CaptureOutput()
  207. finally:
  208. _remove_ssh_client(p)
  209. return rc
  210. def _CaptureOutput(self):
  211. p = self.process
  212. s_in = platform_utils.FileDescriptorStreams.create()
  213. s_in.add(p.stdout, sys.stdout, 'stdout')
  214. s_in.add(p.stderr, sys.stderr, 'stderr')
  215. self.stdout = ''
  216. self.stderr = ''
  217. while not s_in.is_done:
  218. in_ready = s_in.select()
  219. for s in in_ready:
  220. buf = s.read()
  221. if not buf:
  222. s_in.remove(s)
  223. continue
  224. if not hasattr(buf, 'encode'):
  225. buf = buf.decode()
  226. if s.std_name == 'stdout':
  227. self.stdout += buf
  228. else:
  229. self.stderr += buf
  230. if self.tee[s.std_name]:
  231. s.dest.write(buf)
  232. s.dest.flush()
  233. return p.wait()