git_command.py 7.0 KB

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