api.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. # Copyright 2013 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. import itertools
  5. import re
  6. from recipe_engine import recipe_api
  7. class GitApi(recipe_api.RecipeApi):
  8. _GIT_HASH_RE = re.compile('[0-9a-f]{40}', re.IGNORECASE)
  9. def __call__(self, *args, **kwargs):
  10. """Return a git command step."""
  11. name = kwargs.pop('name', 'git ' + args[0])
  12. infra_step = kwargs.pop('infra_step', True)
  13. git_cmd = ['git']
  14. options = kwargs.pop('git_config_options', {})
  15. for k, v in sorted(options.items()):
  16. git_cmd.extend(['-c', '%s=%s' % (k, v)])
  17. can_fail_build = kwargs.pop('can_fail_build', True)
  18. try:
  19. with self.m.context(cwd=(self.m.context.cwd or self.m.path['checkout'])):
  20. return self.m.step(name, git_cmd + list(args), infra_step=infra_step,
  21. **kwargs)
  22. except self.m.step.StepFailure as f:
  23. if can_fail_build:
  24. raise
  25. else:
  26. return f.result
  27. def fetch_tags(self, remote_name=None, **kwargs):
  28. """Fetches all tags from the remote."""
  29. kwargs.setdefault('name', 'git fetch tags')
  30. remote_name = remote_name or 'origin'
  31. return self('fetch', remote_name, '--tags', **kwargs)
  32. def cat_file_at_commit(self, file_path, commit_hash, remote_name=None,
  33. **kwargs):
  34. """Outputs the contents of a file at a given revision."""
  35. self.fetch_tags(remote_name=remote_name, **kwargs)
  36. kwargs.setdefault('name', 'git cat-file %s:%s' % (commit_hash, file_path))
  37. return self('cat-file', 'blob', '%s:%s' % (commit_hash, file_path),
  38. **kwargs)
  39. def count_objects(self, previous_result=None, can_fail_build=False, **kwargs):
  40. """Returns `git count-objects` result as a dict.
  41. Args:
  42. previous_result (dict): the result of previous count_objects call.
  43. If passed, delta is reported in the log and step text.
  44. can_fail_build (bool): if True, may fail the build and/or raise an
  45. exception. Defaults to False.
  46. Returns:
  47. A dict of count-object values, or None if count-object run failed.
  48. """
  49. if previous_result:
  50. assert isinstance(previous_result, dict)
  51. assert all(isinstance(v, long) for v in previous_result.values())
  52. assert 'size' in previous_result
  53. assert 'size-pack' in previous_result
  54. step_result = None
  55. try:
  56. step_result = self(
  57. 'count-objects', '-v', stdout=self.m.raw_io.output(),
  58. can_fail_build=can_fail_build, **kwargs)
  59. if not step_result.stdout:
  60. return None
  61. result = {}
  62. for line in step_result.stdout.splitlines():
  63. name, value = line.split(':', 1)
  64. result[name] = long(value.strip())
  65. def results_to_text(results):
  66. return [' %s: %s' % (k, v) for k, v in results.items()]
  67. step_result.presentation.logs['result'] = results_to_text(result)
  68. if previous_result:
  69. delta = {
  70. key: value - previous_result[key]
  71. for key, value in result.items()
  72. if key in previous_result}
  73. step_result.presentation.logs['delta'] = (
  74. ['before:'] + results_to_text(previous_result) +
  75. ['', 'after:'] + results_to_text(result) +
  76. ['', 'delta:'] + results_to_text(delta)
  77. )
  78. size_delta = (
  79. result['size'] + result['size-pack']
  80. - previous_result['size'] - previous_result['size-pack'])
  81. # size_delta is in KiB.
  82. step_result.presentation.step_text = (
  83. 'size delta: %+.2f MiB' % (size_delta / 1024.0))
  84. return result
  85. except Exception as ex:
  86. if step_result:
  87. step_result.presentation.logs['exception'] = ['%r' % ex]
  88. step_result.presentation.status = self.m.step.WARNING
  89. if can_fail_build:
  90. raise recipe_api.InfraFailure('count-objects failed: %s' % ex)
  91. return None
  92. def checkout(self, url, ref=None, dir_path=None, recursive=False,
  93. submodules=True, submodule_update_force=False,
  94. keep_paths=None, step_suffix=None,
  95. curl_trace_file=None, can_fail_build=True,
  96. set_got_revision=False, remote_name=None,
  97. display_fetch_size=None, file_name=None,
  98. submodule_update_recursive=True,
  99. use_git_cache=False, progress=True, tags=False):
  100. """Performs a full git checkout and returns sha1 of checked out revision.
  101. Args:
  102. url (str): url of remote repo to use as upstream
  103. ref (str): ref to fetch and check out
  104. dir_path (Path): optional directory to clone into
  105. recursive (bool): whether to recursively fetch submodules or not
  106. submodules (bool): whether to sync and update submodules or not
  107. submodule_update_force (bool): whether to update submodules with --force
  108. keep_paths (iterable of strings): paths to ignore during git-clean;
  109. paths are gitignore-style patterns relative to checkout_path.
  110. step_suffix (str): suffix to add to a each step name
  111. curl_trace_file (Path): if not None, dump GIT_CURL_VERBOSE=1 trace to that
  112. file. Useful for debugging git issue reproducible only on bots. It has
  113. a side effect of all stderr output of 'git fetch' going to that file.
  114. can_fail_build (bool): if False, ignore errors during fetch or checkout.
  115. set_got_revision (bool): if True, resolves HEAD and sets got_revision
  116. property.
  117. remote_name (str): name of the git remote to use
  118. display_fetch_size (bool): if True, run `git count-objects` before and
  119. after fetch and display delta. Adds two more steps. Defaults to False.
  120. file_name (str): optional path to a single file to checkout.
  121. submodule_update_recursive (bool): if True, updates submodules
  122. recursively.
  123. use_git_cache (bool): if True, git cache will be used for this checkout.
  124. WARNING, this is EXPERIMENTAL!!! This wasn't tested with:
  125. * submodules
  126. * since origin url is modified
  127. to a local path, may cause problem with scripts that do
  128. "git fetch origin" or "git push origin".
  129. * arbitrary refs such refs/whatever/not-fetched-by-default-to-cache
  130. progress (bool): whether to show progress for fetch or not
  131. tags (bool): Also fetch tags.
  132. Returns: If the checkout was successful, this returns the commit hash of
  133. the checked-out-repo. Otherwise this returns None.
  134. """
  135. retVal = None
  136. # TODO(robertocn): Break this function and refactor calls to it.
  137. # The problem is that there are way too many unrealated use cases for
  138. # it, and the function's signature is getting unwieldy and its body
  139. # unreadable.
  140. display_fetch_size = display_fetch_size or False
  141. if not dir_path:
  142. dir_path = url.rsplit('/', 1)[-1]
  143. if dir_path.endswith('.git'): # ex: https://host/foobar.git
  144. dir_path = dir_path[:-len('.git')]
  145. # ex: ssh://host:repo/foobar/.git
  146. dir_path = dir_path or dir_path.rsplit('/', 1)[-1]
  147. dir_path = self.m.path['start_dir'].join(dir_path)
  148. if 'checkout' not in self.m.path:
  149. self.m.path['checkout'] = dir_path
  150. git_setup_args = ['--path', dir_path, '--url', url]
  151. if remote_name:
  152. git_setup_args += ['--remote', remote_name]
  153. else:
  154. remote_name = 'origin'
  155. step_suffix = '' if step_suffix is None else ' (%s)' % step_suffix
  156. self.m.python(
  157. 'git setup%s' % step_suffix,
  158. self.resource('git_setup.py'),
  159. git_setup_args)
  160. # Some of the commands below require depot_tools to be in PATH.
  161. path = self.m.path.pathsep.join([
  162. str(self.repo_resource()), '%(PATH)s'])
  163. with self.m.context(cwd=dir_path):
  164. if use_git_cache:
  165. with self.m.context(env={'PATH': path}):
  166. self('cache', 'populate', '-c',
  167. self.m.infra_paths.default_git_cache_dir, url,
  168. name='populate cache',
  169. can_fail_build=can_fail_build)
  170. dir_cmd = self(
  171. 'cache', 'exists', '--quiet',
  172. '--cache-dir', self.m.infra_paths.default_git_cache_dir, url,
  173. can_fail_build=can_fail_build,
  174. stdout=self.m.raw_io.output(),
  175. step_test_data=lambda:
  176. self.m.raw_io.test_api.stream_output('mirror_dir'))
  177. mirror_dir = dir_cmd.stdout.strip()
  178. self('remote', 'set-url', 'origin', mirror_dir,
  179. can_fail_build=can_fail_build)
  180. # There are five kinds of refs we can be handed:
  181. # 0) None. In this case, we default to api.buildbucket.gitiles_commit.ref.
  182. # 1) A fully qualified branch name, e.g. 'refs/heads/master'.
  183. # Chop off 'refs/heads/' and now it matches case (4).
  184. # 2) A 40-character SHA1 hash.
  185. # 3) A fully-qualifed arbitrary ref, e.g. 'refs/foo/bar/baz'.
  186. # 4) A branch name, e.g. 'master'.
  187. # Note that 'FETCH_HEAD' can be many things (and therefore not a valid
  188. # checkout target) if many refs are fetched, but we only explicitly fetch
  189. # one ref here, so this is safe.
  190. if not ref: # Case 0.
  191. ref = self.m.buildbucket.gitiles_commit.ref or 'master'
  192. # If it's a fully-qualified branch name, trim the 'refs/heads/' prefix.
  193. if ref.startswith('refs/heads/'): # Case 1.
  194. ref = ref[len('refs/heads/'):]
  195. fetch_args = []
  196. if self._GIT_HASH_RE.match(ref): # Case 2.
  197. fetch_remote = remote_name
  198. fetch_ref = ''
  199. checkout_ref = ref
  200. else: # Cases 3 and 4.
  201. fetch_remote = remote_name
  202. fetch_ref = ref
  203. checkout_ref = 'FETCH_HEAD'
  204. fetch_args = [x for x in (fetch_remote, fetch_ref) if x]
  205. if recursive:
  206. fetch_args.append('--recurse-submodules')
  207. if progress:
  208. fetch_args.append('--progress')
  209. fetch_env = {'PATH': path}
  210. fetch_stderr = None
  211. if curl_trace_file:
  212. fetch_env['GIT_CURL_VERBOSE'] = '1'
  213. fetch_stderr = self.m.raw_io.output(leak_to=curl_trace_file)
  214. if tags:
  215. fetch_args.append('--tags')
  216. fetch_step_name = 'git fetch%s' % step_suffix
  217. if display_fetch_size:
  218. count_objects_before_fetch = self.count_objects(
  219. name='count-objects before %s' % fetch_step_name,
  220. step_test_data=lambda: self.m.raw_io.test_api.stream_output(
  221. self.test_api.count_objects_output(1000)))
  222. with self.m.context(env=fetch_env):
  223. self('fetch', *fetch_args,
  224. name=fetch_step_name,
  225. stderr=fetch_stderr,
  226. can_fail_build=can_fail_build)
  227. if display_fetch_size:
  228. self.count_objects(
  229. name='count-objects after %s' % fetch_step_name,
  230. previous_result=count_objects_before_fetch,
  231. step_test_data=lambda: self.m.raw_io.test_api.stream_output(
  232. self.test_api.count_objects_output(2000)))
  233. if file_name:
  234. self('checkout', '-f', checkout_ref, '--', file_name,
  235. name='git checkout%s' % step_suffix,
  236. can_fail_build=can_fail_build)
  237. else:
  238. self('checkout', '-f', checkout_ref,
  239. name='git checkout%s' % step_suffix,
  240. can_fail_build=can_fail_build)
  241. rev_parse_step = self('rev-parse', 'HEAD',
  242. name='read revision',
  243. stdout=self.m.raw_io.output(),
  244. can_fail_build=False,
  245. step_test_data=lambda:
  246. self.m.raw_io.test_api.stream_output('deadbeef'))
  247. if rev_parse_step.presentation.status == 'SUCCESS':
  248. sha = rev_parse_step.stdout.strip()
  249. retVal = sha
  250. rev_parse_step.presentation.step_text = "<br/>checked out %r<br/>" % sha
  251. if set_got_revision:
  252. rev_parse_step.presentation.properties['got_revision'] = sha
  253. clean_args = list(itertools.chain(
  254. *[('-e', path) for path in keep_paths or []]))
  255. self('clean', '-f', '-d', '-x', *clean_args,
  256. name='git clean%s' % step_suffix,
  257. can_fail_build=can_fail_build)
  258. if submodules:
  259. self('submodule', 'sync',
  260. name='submodule sync%s' % step_suffix,
  261. can_fail_build=can_fail_build)
  262. submodule_update = ['submodule', 'update', '--init']
  263. if submodule_update_recursive:
  264. submodule_update.append('--recursive')
  265. if submodule_update_force:
  266. submodule_update.append('--force')
  267. self(*submodule_update,
  268. name='submodule update%s' % step_suffix,
  269. can_fail_build=can_fail_build)
  270. return retVal
  271. def get_timestamp(self, commit='HEAD', test_data=None, **kwargs):
  272. """Find and return the timestamp of the given commit."""
  273. step_test_data = None
  274. if test_data is not None:
  275. step_test_data = lambda: self.m.raw_io.test_api.stream_output(test_data)
  276. return self('show', commit, '--format=%at', '-s',
  277. stdout=self.m.raw_io.output(),
  278. step_test_data=step_test_data).stdout.rstrip()
  279. def rebase(self, name_prefix, branch, dir_path, remote_name=None,
  280. **kwargs):
  281. """Run rebase HEAD onto branch
  282. Args:
  283. name_prefix (str): a prefix used for the step names
  284. branch (str): a branch name or a hash to rebase onto
  285. dir_path (Path): directory to clone into
  286. remote_name (str): the remote name to rebase from if not origin
  287. """
  288. remote_name = remote_name or 'origin'
  289. with self.m.context(cwd=dir_path):
  290. try:
  291. self('rebase', '%s/master' % remote_name,
  292. name="%s rebase" % name_prefix, **kwargs)
  293. except self.m.step.StepFailure:
  294. self('rebase', '--abort', name='%s rebase abort' % name_prefix,
  295. **kwargs)
  296. raise
  297. def config_get(self, prop_name, **kwargs):
  298. """Returns: (str) The Git config output, or None if no output was generated.
  299. Args:
  300. prop_name: (str) The name of the config property to query.
  301. kwargs: Forwarded to '__call__'.
  302. """
  303. kwargs['name'] = kwargs.get('name', 'git config %s' % (prop_name,))
  304. result = self('config', '--get', prop_name, stdout=self.m.raw_io.output(),
  305. **kwargs)
  306. value = result.stdout
  307. if value:
  308. value = value.strip()
  309. result.presentation.step_text = value
  310. return value
  311. def get_remote_url(self, remote_name=None, **kwargs):
  312. """Returns: (str) The URL of the remote Git repository, or None.
  313. Args:
  314. remote_name: (str) The name of the remote to query, defaults to 'origin'.
  315. kwargs: Forwarded to '__call__'.
  316. """
  317. remote_name = remote_name or 'origin'
  318. return self.config_get('remote.%s.url' % (remote_name,), **kwargs)
  319. def bundle_create(self, bundle_path, rev_list_args=None, **kwargs):
  320. """Run 'git bundle create' on a Git repository.
  321. Args:
  322. bundle_path (Path): The path of the output bundle.
  323. refs (list): The list of refs to include in the bundle. If None, all
  324. refs in the Git checkout will be bundled.
  325. kwargs: Forwarded to '__call__'.
  326. """
  327. if not rev_list_args:
  328. rev_list_args = ['--all']
  329. self('bundle', 'create', bundle_path, *rev_list_args, **kwargs)
  330. def new_branch(self, branch, name=None, upstream=None, **kwargs):
  331. """Runs git new-branch on a Git repository, to be used before git cl upload.
  332. Args:
  333. branch (str): new branch name, which must not yet exist.
  334. name (str): step name.
  335. upstream (str): to origin/master.
  336. kwargs: Forwarded to '__call__'.
  337. """
  338. env = self.m.context.env
  339. env['PATH'] = self.m.path.pathsep.join([
  340. str(self.repo_resource()), '%(PATH)s'])
  341. args = ['new-branch', branch]
  342. if upstream:
  343. args.extend(['--upstream', upstream])
  344. if not name:
  345. name = 'git new-branch %s' % branch
  346. with self.m.context(env=env):
  347. return self(*args, name=name, **kwargs)