| OLD | NEW |
| 1 # Copyright (c) 2009, 2010, 2011 Google Inc. All rights reserved. | 1 # Copyright (c) 2009, 2010, 2011 Google Inc. All rights reserved. |
| 2 # Copyright (c) 2009 Apple Inc. All rights reserved. | 2 # Copyright (c) 2009 Apple Inc. All rights reserved. |
| 3 # | 3 # |
| 4 # Redistribution and use in source and binary forms, with or without | 4 # Redistribution and use in source and binary forms, with or without |
| 5 # modification, are permitted provided that the following conditions are | 5 # modification, are permitted provided that the following conditions are |
| 6 # met: | 6 # met: |
| 7 # | 7 # |
| 8 # * Redistributions of source code must retain the above copyright | 8 # * Redistributions of source code must retain the above copyright |
| 9 # notice, this list of conditions and the following disclaimer. | 9 # notice, this list of conditions and the following disclaimer. |
| 10 # * Redistributions in binary form must reproduce the above | 10 # * Redistributions in binary form must reproduce the above |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 | 39 |
| 40 | 40 |
| 41 class Git(object): | 41 class Git(object): |
| 42 # Unless otherwise specified, methods are expected to return paths relative | 42 # Unless otherwise specified, methods are expected to return paths relative |
| 43 # to self.checkout_root. | 43 # to self.checkout_root. |
| 44 | 44 |
| 45 # Git doesn't appear to document error codes, but seems to return | 45 # Git doesn't appear to document error codes, but seems to return |
| 46 # 1 or 128, mostly. | 46 # 1 or 128, mostly. |
| 47 ERROR_FILE_IS_MISSING = 128 | 47 ERROR_FILE_IS_MISSING = 128 |
| 48 | 48 |
| 49 executable_name = 'git' | 49 def __init__(self, cwd=None, executive=None, filesystem=None, platform=None)
: |
| 50 | |
| 51 def __init__(self, cwd=None, executive=None, filesystem=None): | |
| 52 self._executive = executive or Executive() | 50 self._executive = executive or Executive() |
| 53 self._filesystem = filesystem or FileSystem() | 51 self._filesystem = filesystem or FileSystem() |
| 52 self._executable_name = self.find_executable_name(self._executive, platf
orm) |
| 54 | 53 |
| 55 self.cwd = cwd or self._filesystem.abspath(self._filesystem.getcwd()) | 54 self.cwd = cwd or self._filesystem.abspath(self._filesystem.getcwd()) |
| 56 if not Git.in_working_directory(self.cwd, executive=self._executive): | 55 if not self.in_working_directory(self.cwd): |
| 57 module_directory = self._filesystem.abspath( | 56 module_directory = self._filesystem.abspath( |
| 58 self._filesystem.dirname(self._filesystem.path_to_module(self.__
module__))) | 57 self._filesystem.dirname(self._filesystem.path_to_module(self.__
module__))) |
| 59 _log.info('The current directory (%s) is not in a git repo, trying d
irectory %s.', | 58 _log.info('The current directory (%s) is not in a git repo, trying d
irectory %s.', |
| 60 cwd, module_directory) | 59 cwd, module_directory) |
| 61 if Git.in_working_directory(module_directory, executive=self._execut
ive): | 60 if self.in_working_directory(module_directory): |
| 62 self.cwd = module_directory | 61 self.cwd = module_directory |
| 63 _log.error('Failed to find Git repo for %s or %s', cwd, module_direc
tory) | 62 _log.error('Failed to find Git repo for %s or %s', cwd, module_direc
tory) |
| 64 | 63 |
| 65 self._init_executable_name() | |
| 66 self.checkout_root = self.find_checkout_root(self.cwd) | 64 self.checkout_root = self.find_checkout_root(self.cwd) |
| 67 | 65 |
| 68 def _init_executable_name(self): | 66 @staticmethod |
| 69 """Sets the executable name on Windows. | 67 def find_executable_name(executive, platform): |
| 68 """Finds the git executable name which may be different on Windows. |
| 70 | 69 |
| 71 The Win port uses the depot_tools package, which contains a number | 70 The Win port uses the depot_tools package, which contains a number |
| 72 of development tools, including Python and git. Instead of using a | 71 of development tools, including Python and git. Instead of using a |
| 73 real git executable, depot_tools indirects via a batch file, called | 72 real git executable, depot_tools indirects via a batch file, called |
| 74 "git.bat". This batch file is used because it allows depot_tools to | 73 "git.bat". This batch file is used because it allows depot_tools to |
| 75 auto-update the real git executable, which is contained in a | 74 auto-update the real git executable, which is contained in a |
| 76 subdirectory. | 75 subdirectory. |
| 77 | 76 |
| 78 FIXME: This is a hack and should be resolved in a different way if | 77 FIXME: This is a hack and should be resolved in a different way if |
| 79 possible. | 78 possible. |
| 80 """ | 79 """ |
| 80 if not platform or not platform.is_win(): |
| 81 return 'git' |
| 81 try: | 82 try: |
| 82 self._executive.run_command(['git', 'help']) | 83 executive.run_command(['git', 'help']) |
| 84 return 'git' |
| 83 except OSError: | 85 except OSError: |
| 84 try: | 86 _log.debug('Using "git.bat" as git executable.') |
| 85 self._executive.run_command(['git.bat', 'help']) | 87 return 'git.bat' |
| 86 _log.debug('Engaging git.bat Windows hack.') | |
| 87 self.executable_name = 'git.bat' | |
| 88 except OSError: | |
| 89 _log.debug('Failed to engage git.bat Windows hack.') | |
| 90 | 88 |
| 91 def run(self, command_args, cwd=None, stdin=None, decode_output=True, return
_exit_code=False): | 89 def run(self, command_args, cwd=None, stdin=None, decode_output=True, return
_exit_code=False): |
| 92 """Invokes git with the given args.""" | 90 """Invokes git with the given args.""" |
| 93 full_command_args = [self.executable_name] + command_args | 91 full_command_args = [self._executable_name] + command_args |
| 94 cwd = cwd or self.checkout_root | 92 cwd = cwd or self.checkout_root |
| 95 return self._executive.run_command( | 93 return self._executive.run_command( |
| 96 full_command_args, | 94 full_command_args, |
| 97 cwd=cwd, | 95 cwd=cwd, |
| 98 input=stdin, | 96 input=stdin, |
| 99 return_exit_code=return_exit_code, | 97 return_exit_code=return_exit_code, |
| 100 decode_output=decode_output) | 98 decode_output=decode_output) |
| 101 | 99 |
| 102 def absolute_path(self, repository_relative_path): | 100 def absolute_path(self, repository_relative_path): |
| 103 """Converts repository-relative paths to absolute paths.""" | 101 """Converts repository-relative paths to absolute paths.""" |
| 104 return self._filesystem.join(self.checkout_root, repository_relative_pat
h) | 102 return self._filesystem.join(self.checkout_root, repository_relative_pat
h) |
| 105 | 103 |
| 106 @classmethod | 104 def in_working_directory(self, path): |
| 107 def in_working_directory(cls, path, executive=None): | |
| 108 try: | 105 try: |
| 109 executive = executive or Executive() | 106 self._executive.run_command( |
| 110 return executive.run_command( | 107 [self._executable_name, 'rev-parse', '--is-inside-work-tree'], |
| 111 [cls.executable_name, 'rev-parse', '--is-inside-work-tree'], | |
| 112 cwd=path, error_handler=Executive.ignore_error).rstrip() == 'tru
e' | 108 cwd=path, error_handler=Executive.ignore_error).rstrip() == 'tru
e' |
| 113 except OSError: | 109 except OSError: |
| 114 # The Windows bots seem to throw a WindowsError when git isn't insta
lled. | 110 # The Windows bots seem to throw a WindowsError when git isn't insta
lled. |
| 115 # TODO(qyearsley): This might be because the git executable name | 111 # TODO(qyearsley): Check if this is still necessary and remove if po
ssible. |
| 116 # isn't initialized yet; maybe this would be fixed by using the | |
| 117 # _init_executable_name hack above. | |
| 118 _log.warn('Got OSError when running Git.in_working_directory.') | 112 _log.warn('Got OSError when running Git.in_working_directory.') |
| 119 return False | 113 return False |
| 120 | 114 |
| 121 def find_checkout_root(self, path): | 115 def find_checkout_root(self, path): |
| 122 # "git rev-parse --show-cdup" would be another way to get to the root | 116 # "git rev-parse --show-cdup" would be another way to get to the root |
| 123 checkout_root = self.run(['rev-parse', '--show-toplevel'], cwd=(path or
'./')).strip() | 117 checkout_root = self.run(['rev-parse', '--show-toplevel'], cwd=(path or
'./')).strip() |
| 124 if not self._filesystem.isabs(checkout_root): # Sometimes git returns r
elative paths | 118 if not self._filesystem.isabs(checkout_root): # Sometimes git returns r
elative paths |
| 125 checkout_root = self._filesystem.join(path, checkout_root) | 119 checkout_root = self._filesystem.join(path, checkout_root) |
| 126 return checkout_root | 120 return checkout_root |
| 127 | 121 |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 match = re.search(r"^\s*(?P<branch_name>\S+)\s+merges with remote master
$", origin_info, re.MULTILINE) | 390 match = re.search(r"^\s*(?P<branch_name>\S+)\s+merges with remote master
$", origin_info, re.MULTILINE) |
| 397 if not match: | 391 if not match: |
| 398 raise ScriptError(message='Unable to find local branch tracking orig
in/master.') | 392 raise ScriptError(message='Unable to find local branch tracking orig
in/master.') |
| 399 branch = str(match.group('branch_name')) | 393 branch = str(match.group('branch_name')) |
| 400 return self._branch_from_ref(self.run(['rev-parse', '--symbolic-full-nam
e', branch]).strip()) | 394 return self._branch_from_ref(self.run(['rev-parse', '--symbolic-full-nam
e', branch]).strip()) |
| 401 | 395 |
| 402 def ensure_cleanly_tracking_remote_master(self): | 396 def ensure_cleanly_tracking_remote_master(self): |
| 403 self._discard_working_directory_changes() | 397 self._discard_working_directory_changes() |
| 404 self.run(['checkout', '-q', self._branch_tracking_remote_master()]) | 398 self.run(['checkout', '-q', self._branch_tracking_remote_master()]) |
| 405 self._discard_local_commits() | 399 self._discard_local_commits() |
| OLD | NEW |