git_command.py 10 KB

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