git_command.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. # -*- coding:utf-8 -*-
  2. #
  3. # Copyright (C) 2008 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from __future__ import print_function
  17. import os
  18. import sys
  19. import subprocess
  20. import tempfile
  21. from signal import SIGTERM
  22. from error import GitError
  23. import platform_utils
  24. from repo_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 _GitCall(object):
  71. def version_tuple(self):
  72. global _git_version
  73. if _git_version is None:
  74. _git_version = Wrapper().ParseGitVersion()
  75. if _git_version is None:
  76. print('fatal: unable to detect git version', file=sys.stderr)
  77. sys.exit(1)
  78. return _git_version
  79. def __getattr__(self, name):
  80. name = name.replace('_','-')
  81. def fun(*cmdv):
  82. command = [name]
  83. command.extend(cmdv)
  84. return GitCommand(None, command).Wait() == 0
  85. return fun
  86. git = _GitCall()
  87. _user_agent = None
  88. def RepoUserAgent():
  89. """Return a User-Agent string suitable for HTTP-like services.
  90. We follow the style as documented here:
  91. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
  92. """
  93. global _user_agent
  94. if _user_agent is None:
  95. py_version = sys.version_info
  96. os_name = sys.platform
  97. if os_name == 'linux2':
  98. os_name = 'Linux'
  99. elif os_name == 'win32':
  100. os_name = 'Win32'
  101. elif os_name == 'cygwin':
  102. os_name = 'Cygwin'
  103. elif os_name == 'darwin':
  104. os_name = 'Darwin'
  105. p = GitCommand(
  106. None, ['describe', 'HEAD'],
  107. cwd=os.path.dirname(__file__),
  108. capture_stdout=True)
  109. if p.Wait() == 0:
  110. repo_version = p.stdout
  111. if repo_version and repo_version[-1] == '\n':
  112. repo_version = repo_version[0:-1]
  113. if repo_version and repo_version[0] == 'v':
  114. repo_version = repo_version[1:]
  115. else:
  116. repo_version = 'unknown'
  117. _user_agent = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % (
  118. repo_version,
  119. os_name,
  120. git.version_tuple().full,
  121. py_version.major, py_version.minor, py_version.micro)
  122. return _user_agent
  123. def git_require(min_version, fail=False, msg=''):
  124. git_version = git.version_tuple()
  125. if min_version <= git_version:
  126. return True
  127. if fail:
  128. need = '.'.join(map(str, min_version))
  129. if msg:
  130. msg = ' for ' + msg
  131. print('fatal: git %s or later required%s' % (need, msg), file=sys.stderr)
  132. sys.exit(1)
  133. return False
  134. def _setenv(env, name, value):
  135. env[name] = value.encode()
  136. class GitCommand(object):
  137. def __init__(self,
  138. project,
  139. cmdv,
  140. bare = False,
  141. provide_stdin = False,
  142. capture_stdout = False,
  143. capture_stderr = False,
  144. disable_editor = False,
  145. ssh_proxy = False,
  146. cwd = None,
  147. gitdir = None):
  148. env = os.environ.copy()
  149. for key in [REPO_TRACE,
  150. GIT_DIR,
  151. 'GIT_ALTERNATE_OBJECT_DIRECTORIES',
  152. 'GIT_OBJECT_DIRECTORY',
  153. 'GIT_WORK_TREE',
  154. 'GIT_GRAFT_FILE',
  155. 'GIT_INDEX_FILE']:
  156. if key in env:
  157. del env[key]
  158. # If we are not capturing std* then need to print it.
  159. self.tee = {'stdout': not capture_stdout, 'stderr': not capture_stderr}
  160. if disable_editor:
  161. _setenv(env, 'GIT_EDITOR', ':')
  162. if ssh_proxy:
  163. _setenv(env, 'REPO_SSH_SOCK', ssh_sock())
  164. _setenv(env, 'GIT_SSH', _ssh_proxy())
  165. _setenv(env, 'GIT_SSH_VARIANT', 'ssh')
  166. if 'http_proxy' in env and 'darwin' == sys.platform:
  167. s = "'http.proxy=%s'" % (env['http_proxy'],)
  168. p = env.get('GIT_CONFIG_PARAMETERS')
  169. if p is not None:
  170. s = p + ' ' + s
  171. _setenv(env, 'GIT_CONFIG_PARAMETERS', s)
  172. if 'GIT_ALLOW_PROTOCOL' not in env:
  173. _setenv(env, 'GIT_ALLOW_PROTOCOL',
  174. 'file:git:http:https:ssh:persistent-http:persistent-https:sso:rpc')
  175. if project:
  176. if not cwd:
  177. cwd = project.worktree
  178. if not gitdir:
  179. gitdir = project.gitdir
  180. command = [GIT]
  181. if bare:
  182. if gitdir:
  183. _setenv(env, GIT_DIR, gitdir)
  184. cwd = None
  185. command.append(cmdv[0])
  186. # Need to use the --progress flag for fetch/clone so output will be
  187. # displayed as by default git only does progress output if stderr is a TTY.
  188. if sys.stderr.isatty() and cmdv[0] in ('fetch', 'clone'):
  189. if '--progress' not in cmdv and '--quiet' not in cmdv:
  190. command.append('--progress')
  191. command.extend(cmdv[1:])
  192. if provide_stdin:
  193. stdin = subprocess.PIPE
  194. else:
  195. stdin = None
  196. stdout = subprocess.PIPE
  197. stderr = subprocess.PIPE
  198. if IsTrace():
  199. global LAST_CWD
  200. global LAST_GITDIR
  201. dbg = ''
  202. if cwd and LAST_CWD != cwd:
  203. if LAST_GITDIR or LAST_CWD:
  204. dbg += '\n'
  205. dbg += ': cd %s\n' % cwd
  206. LAST_CWD = cwd
  207. if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]:
  208. if LAST_GITDIR or LAST_CWD:
  209. dbg += '\n'
  210. dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR]
  211. LAST_GITDIR = env[GIT_DIR]
  212. dbg += ': '
  213. dbg += ' '.join(command)
  214. if stdin == subprocess.PIPE:
  215. dbg += ' 0<|'
  216. if stdout == subprocess.PIPE:
  217. dbg += ' 1>|'
  218. if stderr == subprocess.PIPE:
  219. dbg += ' 2>|'
  220. Trace('%s', dbg)
  221. try:
  222. p = subprocess.Popen(command,
  223. cwd = cwd,
  224. env = env,
  225. stdin = stdin,
  226. stdout = stdout,
  227. stderr = stderr)
  228. except Exception as e:
  229. raise GitError('%s: %s' % (command[1], e))
  230. if ssh_proxy:
  231. _add_ssh_client(p)
  232. self.process = p
  233. self.stdin = p.stdin
  234. def Wait(self):
  235. try:
  236. p = self.process
  237. rc = self._CaptureOutput()
  238. finally:
  239. _remove_ssh_client(p)
  240. return rc
  241. def _CaptureOutput(self):
  242. p = self.process
  243. s_in = platform_utils.FileDescriptorStreams.create()
  244. s_in.add(p.stdout, sys.stdout, 'stdout')
  245. s_in.add(p.stderr, sys.stderr, 'stderr')
  246. self.stdout = ''
  247. self.stderr = ''
  248. while not s_in.is_done:
  249. in_ready = s_in.select()
  250. for s in in_ready:
  251. buf = s.read()
  252. if not buf:
  253. s_in.remove(s)
  254. continue
  255. if not hasattr(buf, 'encode'):
  256. buf = buf.decode()
  257. if s.std_name == 'stdout':
  258. self.stdout += buf
  259. else:
  260. self.stderr += buf
  261. if self.tee[s.std_name]:
  262. s.dest.write(buf)
  263. s.dest.flush()
  264. return p.wait()