git_command.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. from git_refs import HEAD
  24. import platform_utils
  25. from repo_trace import REPO_TRACE, IsTrace, Trace
  26. from wrapper import Wrapper
  27. GIT = 'git'
  28. MIN_GIT_VERSION = (1, 5, 4)
  29. GIT_DIR = 'GIT_DIR'
  30. LAST_GITDIR = None
  31. LAST_CWD = None
  32. _ssh_proxy_path = None
  33. _ssh_sock_path = None
  34. _ssh_clients = []
  35. def ssh_sock(create=True):
  36. global _ssh_sock_path
  37. if _ssh_sock_path is None:
  38. if not create:
  39. return None
  40. tmp_dir = '/tmp'
  41. if not os.path.exists(tmp_dir):
  42. tmp_dir = tempfile.gettempdir()
  43. _ssh_sock_path = os.path.join(
  44. tempfile.mkdtemp('', 'ssh-', tmp_dir),
  45. 'master-%r@%h:%p')
  46. return _ssh_sock_path
  47. def _ssh_proxy():
  48. global _ssh_proxy_path
  49. if _ssh_proxy_path is None:
  50. _ssh_proxy_path = os.path.join(
  51. os.path.dirname(__file__),
  52. 'git_ssh')
  53. return _ssh_proxy_path
  54. def _add_ssh_client(p):
  55. _ssh_clients.append(p)
  56. def _remove_ssh_client(p):
  57. try:
  58. _ssh_clients.remove(p)
  59. except ValueError:
  60. pass
  61. def terminate_ssh_clients():
  62. global _ssh_clients
  63. for p in _ssh_clients:
  64. try:
  65. os.kill(p.pid, SIGTERM)
  66. p.wait()
  67. except OSError:
  68. pass
  69. _ssh_clients = []
  70. _git_version = None
  71. class _GitCall(object):
  72. def version_tuple(self):
  73. global _git_version
  74. if _git_version is None:
  75. _git_version = Wrapper().ParseGitVersion()
  76. if _git_version is None:
  77. print('fatal: unable to detect git version', file=sys.stderr)
  78. sys.exit(1)
  79. return _git_version
  80. def __getattr__(self, name):
  81. name = name.replace('_','-')
  82. def fun(*cmdv):
  83. command = [name]
  84. command.extend(cmdv)
  85. return GitCommand(None, command).Wait() == 0
  86. return fun
  87. git = _GitCall()
  88. def RepoSourceVersion():
  89. """Return the version of the repo.git tree."""
  90. ver = getattr(RepoSourceVersion, 'version', None)
  91. # We avoid GitCommand so we don't run into circular deps -- GitCommand needs
  92. # to initialize version info we provide.
  93. if ver is None:
  94. env = GitCommand._GetBasicEnv()
  95. proj = os.path.dirname(os.path.abspath(__file__))
  96. env[GIT_DIR] = os.path.join(proj, '.git')
  97. p = subprocess.Popen([GIT, 'describe', HEAD], stdout=subprocess.PIPE,
  98. env=env)
  99. if p.wait() == 0:
  100. ver = p.stdout.read().strip().decode('utf-8')
  101. if ver.startswith('v'):
  102. ver = ver[1:]
  103. else:
  104. ver = 'unknown'
  105. setattr(RepoSourceVersion, 'version', ver)
  106. return ver
  107. class UserAgent(object):
  108. """Mange User-Agent settings when talking to external services
  109. We follow the style as documented here:
  110. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
  111. """
  112. _os = None
  113. _repo_ua = None
  114. @property
  115. def os(self):
  116. """The operating system name."""
  117. if self._os is None:
  118. os_name = sys.platform
  119. if os_name.lower().startswith('linux'):
  120. os_name = 'Linux'
  121. elif os_name == 'win32':
  122. os_name = 'Win32'
  123. elif os_name == 'cygwin':
  124. os_name = 'Cygwin'
  125. elif os_name == 'darwin':
  126. os_name = 'Darwin'
  127. self._os = os_name
  128. return self._os
  129. @property
  130. def repo(self):
  131. """The UA when connecting directly from repo."""
  132. if self._repo_ua is None:
  133. py_version = sys.version_info
  134. self._repo_ua = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % (
  135. RepoSourceVersion(),
  136. self.os,
  137. git.version_tuple().full,
  138. py_version.major, py_version.minor, py_version.micro)
  139. return self._repo_ua
  140. user_agent = UserAgent()
  141. def git_require(min_version, fail=False, msg=''):
  142. git_version = git.version_tuple()
  143. if min_version <= git_version:
  144. return True
  145. if fail:
  146. need = '.'.join(map(str, min_version))
  147. if msg:
  148. msg = ' for ' + msg
  149. print('fatal: git %s or later required%s' % (need, msg), file=sys.stderr)
  150. sys.exit(1)
  151. return False
  152. def _setenv(env, name, value):
  153. env[name] = value.encode()
  154. class GitCommand(object):
  155. def __init__(self,
  156. project,
  157. cmdv,
  158. bare = False,
  159. provide_stdin = False,
  160. capture_stdout = False,
  161. capture_stderr = False,
  162. disable_editor = False,
  163. ssh_proxy = False,
  164. cwd = None,
  165. gitdir = None):
  166. env = self._GetBasicEnv()
  167. # If we are not capturing std* then need to print it.
  168. self.tee = {'stdout': not capture_stdout, 'stderr': not capture_stderr}
  169. if disable_editor:
  170. _setenv(env, 'GIT_EDITOR', ':')
  171. if ssh_proxy:
  172. _setenv(env, 'REPO_SSH_SOCK', ssh_sock())
  173. _setenv(env, 'GIT_SSH', _ssh_proxy())
  174. _setenv(env, 'GIT_SSH_VARIANT', 'ssh')
  175. if 'http_proxy' in env and 'darwin' == sys.platform:
  176. s = "'http.proxy=%s'" % (env['http_proxy'],)
  177. p = env.get('GIT_CONFIG_PARAMETERS')
  178. if p is not None:
  179. s = p + ' ' + s
  180. _setenv(env, 'GIT_CONFIG_PARAMETERS', s)
  181. if 'GIT_ALLOW_PROTOCOL' not in env:
  182. _setenv(env, 'GIT_ALLOW_PROTOCOL',
  183. 'file:git:http:https:ssh:persistent-http:persistent-https:sso:rpc')
  184. if project:
  185. if not cwd:
  186. cwd = project.worktree
  187. if not gitdir:
  188. gitdir = project.gitdir
  189. command = [GIT]
  190. if bare:
  191. if gitdir:
  192. _setenv(env, GIT_DIR, gitdir)
  193. cwd = None
  194. command.append(cmdv[0])
  195. # Need to use the --progress flag for fetch/clone so output will be
  196. # displayed as by default git only does progress output if stderr is a TTY.
  197. if sys.stderr.isatty() and cmdv[0] in ('fetch', 'clone'):
  198. if '--progress' not in cmdv and '--quiet' not in cmdv:
  199. command.append('--progress')
  200. command.extend(cmdv[1:])
  201. if provide_stdin:
  202. stdin = subprocess.PIPE
  203. else:
  204. stdin = None
  205. stdout = subprocess.PIPE
  206. stderr = subprocess.PIPE
  207. if IsTrace():
  208. global LAST_CWD
  209. global LAST_GITDIR
  210. dbg = ''
  211. if cwd and LAST_CWD != cwd:
  212. if LAST_GITDIR or LAST_CWD:
  213. dbg += '\n'
  214. dbg += ': cd %s\n' % cwd
  215. LAST_CWD = cwd
  216. if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]:
  217. if LAST_GITDIR or LAST_CWD:
  218. dbg += '\n'
  219. dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR]
  220. LAST_GITDIR = env[GIT_DIR]
  221. dbg += ': '
  222. dbg += ' '.join(command)
  223. if stdin == subprocess.PIPE:
  224. dbg += ' 0<|'
  225. if stdout == subprocess.PIPE:
  226. dbg += ' 1>|'
  227. if stderr == subprocess.PIPE:
  228. dbg += ' 2>|'
  229. Trace('%s', dbg)
  230. try:
  231. p = subprocess.Popen(command,
  232. cwd = cwd,
  233. env = env,
  234. stdin = stdin,
  235. stdout = stdout,
  236. stderr = stderr)
  237. except Exception as e:
  238. raise GitError('%s: %s' % (command[1], e))
  239. if ssh_proxy:
  240. _add_ssh_client(p)
  241. self.process = p
  242. self.stdin = p.stdin
  243. @staticmethod
  244. def _GetBasicEnv():
  245. """Return a basic env for running git under.
  246. This is guaranteed to be side-effect free.
  247. """
  248. env = os.environ.copy()
  249. for key in (REPO_TRACE,
  250. GIT_DIR,
  251. 'GIT_ALTERNATE_OBJECT_DIRECTORIES',
  252. 'GIT_OBJECT_DIRECTORY',
  253. 'GIT_WORK_TREE',
  254. 'GIT_GRAFT_FILE',
  255. 'GIT_INDEX_FILE'):
  256. env.pop(key, None)
  257. return env
  258. def Wait(self):
  259. try:
  260. p = self.process
  261. rc = self._CaptureOutput()
  262. finally:
  263. _remove_ssh_client(p)
  264. return rc
  265. def _CaptureOutput(self):
  266. p = self.process
  267. s_in = platform_utils.FileDescriptorStreams.create()
  268. s_in.add(p.stdout, sys.stdout, 'stdout')
  269. s_in.add(p.stderr, sys.stderr, 'stderr')
  270. self.stdout = ''
  271. self.stderr = ''
  272. while not s_in.is_done:
  273. in_ready = s_in.select()
  274. for s in in_ready:
  275. buf = s.read()
  276. if not buf:
  277. s_in.remove(s)
  278. continue
  279. if not hasattr(buf, 'encode'):
  280. buf = buf.decode()
  281. if s.std_name == 'stdout':
  282. self.stdout += buf
  283. else:
  284. self.stderr += buf
  285. if self.tee[s.std_name]:
  286. s.dest.write(buf)
  287. s.dest.flush()
  288. return p.wait()