| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import itertools | 5 import itertools |
| 6 import re | 6 import re |
| 7 | 7 |
| 8 from recipe_engine import recipe_api | 8 from recipe_engine import recipe_api |
| 9 | 9 |
| 10 class GitApi(recipe_api.RecipeApi): | 10 class GitApi(recipe_api.RecipeApi): |
| 11 _GIT_HASH_RE = re.compile('[0-9a-f]{40}', re.IGNORECASE) | 11 _GIT_HASH_RE = re.compile('[0-9a-f]{40}', re.IGNORECASE) |
| 12 | 12 |
| 13 def __init__(self, *args, **kwargs): |
| 14 super(GitApi, self).__init__(*args, **kwargs) |
| 15 self.initialized_win_git = False |
| 16 |
| 13 def __call__(self, *args, **kwargs): | 17 def __call__(self, *args, **kwargs): |
| 14 """Return a git command step.""" | 18 """Return a git command step.""" |
| 15 name = kwargs.pop('name', 'git '+args[0]) | 19 name = kwargs.pop('name', 'git '+args[0]) |
| 16 infra_step = kwargs.pop('infra_step', True) | 20 infra_step = kwargs.pop('infra_step', True) |
| 17 if 'cwd' not in kwargs: | 21 if 'cwd' not in kwargs: |
| 18 kwargs.setdefault('cwd', self.m.path['checkout']) | 22 kwargs.setdefault('cwd', self.m.path['checkout']) |
| 19 git_cmd = ['git'] | 23 git_cmd = ['git'] |
| 20 if self.m.platform.is_win: | 24 if self.m.platform.is_win: |
| 21 git_cmd = [self.m.path['depot_tools'].join('git.bat')] | 25 self.ensure_win_git_tooling() |
| 26 git_cmd = [self.package_resource('git.bat')] |
| 22 options = kwargs.pop('git_config_options', {}) | 27 options = kwargs.pop('git_config_options', {}) |
| 23 for k, v in sorted(options.iteritems()): | 28 for k, v in sorted(options.iteritems()): |
| 24 git_cmd.extend(['-c', '%s=%s' % (k, v)]) | 29 git_cmd.extend(['-c', '%s=%s' % (k, v)]) |
| 25 can_fail_build = kwargs.pop('can_fail_build', True) | 30 can_fail_build = kwargs.pop('can_fail_build', True) |
| 26 try: | 31 try: |
| 27 return self.m.step(name, git_cmd + list(args), infra_step=infra_step, | 32 return self.m.step(name, git_cmd + list(args), infra_step=infra_step, |
| 28 **kwargs) | 33 **kwargs) |
| 29 except self.m.step.StepFailure as f: | 34 except self.m.step.StepFailure as f: |
| 30 if can_fail_build: | 35 if can_fail_build: |
| 31 raise | 36 raise |
| 32 else: | 37 else: |
| 33 return f.result | 38 return f.result |
| 34 | 39 |
| 40 def ensure_win_git_tooling(self): |
| 41 """Ensures that depot_tools/git.bat actually exists.""" |
| 42 if not self.m.platform.is_win or self.initialized_win_git: |
| 43 return |
| 44 self.m.step( |
| 45 'ensure git tooling on windows', |
| 46 [self.package_resource('bootstrap', 'win', 'win_tools.bat')], |
| 47 infra_step=True, |
| 48 cwd=self.package_resource()) |
| 49 self.initialized_win_git = True |
| 50 |
| 35 def fetch_tags(self, remote_name=None, **kwargs): | 51 def fetch_tags(self, remote_name=None, **kwargs): |
| 36 """Fetches all tags from the remote.""" | 52 """Fetches all tags from the remote.""" |
| 37 kwargs.setdefault('name', 'git fetch tags') | 53 kwargs.setdefault('name', 'git fetch tags') |
| 38 remote_name = remote_name or 'origin' | 54 remote_name = remote_name or 'origin' |
| 39 return self('fetch', remote_name, '--tags', **kwargs) | 55 return self('fetch', remote_name, '--tags', **kwargs) |
| 40 | 56 |
| 41 def cat_file_at_commit(self, file_path, commit_hash, remote_name=None, | 57 def cat_file_at_commit(self, file_path, commit_hash, remote_name=None, |
| 42 **kwargs): | 58 **kwargs): |
| 43 """Outputs the contents of a file at a given revision.""" | 59 """Outputs the contents of a file at a given revision.""" |
| 44 self.fetch_tags(remote_name=remote_name, **kwargs) | 60 self.fetch_tags(remote_name=remote_name, **kwargs) |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 self.m.path['checkout'] = dir_path | 181 self.m.path['checkout'] = dir_path |
| 166 | 182 |
| 167 git_setup_args = ['--path', dir_path, '--url', url] | 183 git_setup_args = ['--path', dir_path, '--url', url] |
| 168 | 184 |
| 169 if remote_name: | 185 if remote_name: |
| 170 git_setup_args += ['--remote', remote_name] | 186 git_setup_args += ['--remote', remote_name] |
| 171 else: | 187 else: |
| 172 remote_name = 'origin' | 188 remote_name = 'origin' |
| 173 | 189 |
| 174 if self.m.platform.is_win: | 190 if self.m.platform.is_win: |
| 175 git_setup_args += ['--git_cmd_path', | 191 self.ensure_win_git_tooling() |
| 176 self.m.path['depot_tools'].join('git.bat')] | 192 git_setup_args += ['--git_cmd_path', self.package_resource('git.bat')] |
| 177 | 193 |
| 178 step_suffix = '' if step_suffix is None else ' (%s)' % step_suffix | 194 step_suffix = '' if step_suffix is None else ' (%s)' % step_suffix |
| 179 self.m.python( | 195 self.m.python( |
| 180 'git setup%s' % step_suffix, | 196 'git setup%s' % step_suffix, |
| 181 self.resource('git_setup.py'), | 197 self.resource('git_setup.py'), |
| 182 git_setup_args) | 198 git_setup_args) |
| 183 | 199 |
| 184 # There are five kinds of refs we can be handed: | 200 # There are five kinds of refs we can be handed: |
| 185 # 0) None. In this case, we default to properties['branch']. | 201 # 0) None. In this case, we default to properties['branch']. |
| 186 # 1) A 40-character SHA1 hash. | 202 # 1) A 40-character SHA1 hash. |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 | 367 |
| 352 Args: | 368 Args: |
| 353 bundle_path (Path): The path of the output bundle. | 369 bundle_path (Path): The path of the output bundle. |
| 354 refs (list): The list of refs to include in the bundle. If None, all | 370 refs (list): The list of refs to include in the bundle. If None, all |
| 355 refs in the Git checkout will be bundled. | 371 refs in the Git checkout will be bundled. |
| 356 kwargs: Forwarded to '__call__'. | 372 kwargs: Forwarded to '__call__'. |
| 357 """ | 373 """ |
| 358 if not rev_list_args: | 374 if not rev_list_args: |
| 359 rev_list_args = ['--all'] | 375 rev_list_args = ['--all'] |
| 360 self('bundle', 'create', bundle_path, *rev_list_args, **kwargs) | 376 self('bundle', 'create', bundle_path, *rev_list_args, **kwargs) |
| OLD | NEW |