steveblock@chromium.org | 9356704 | 2012-02-15 01:02:26 +0000 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 4 | |
maruel@chromium.org | d5800f1 | 2009-11-12 20:03:43 +0000 | [diff] [blame] | 5 | """Gclient-specific SCM-specific operations.""" |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 6 | |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
borenet@google.com | b225621 | 2014-05-07 20:57:28 +0000 | [diff] [blame] | 9 | import errno |
maruel@chromium.org | 754960e | 2009-09-21 12:31:05 +0000 | [diff] [blame] | 10 | import logging |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 11 | import os |
maruel@chromium.org | ee4071d | 2009-12-22 22:25:37 +0000 | [diff] [blame] | 12 | import posixpath |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 13 | import re |
maruel@chromium.org | 9054173 | 2011-04-01 17:54:18 +0000 | [diff] [blame] | 14 | import sys |
ilevy@chromium.org | 3534aa5 | 2013-07-20 01:58:08 +0000 | [diff] [blame] | 15 | import tempfile |
zty@chromium.org | 6279e8a | 2014-02-13 01:45:25 +0000 | [diff] [blame] | 16 | import traceback |
hinoka@google.com | 2f2ca14 | 2014-01-07 03:59:18 +0000 | [diff] [blame] | 17 | import urlparse |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 18 | |
hinoka@google.com | 2f2ca14 | 2014-01-07 03:59:18 +0000 | [diff] [blame] | 19 | import download_from_google_storage |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 20 | import gclient_utils |
szager@chromium.org | 848fd49 | 2014-04-09 19:06:44 +0000 | [diff] [blame] | 21 | import git_cache |
maruel@chromium.org | 31cb48a | 2011-04-04 18:01:36 +0000 | [diff] [blame] | 22 | import scm |
borenet@google.com | b225621 | 2014-05-07 20:57:28 +0000 | [diff] [blame] | 23 | import shutil |
maruel@chromium.org | 31cb48a | 2011-04-04 18:01:36 +0000 | [diff] [blame] | 24 | import subprocess2 |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 25 | |
| 26 | |
szager@chromium.org | 71cbb50 | 2013-04-19 23:30:15 +0000 | [diff] [blame] | 27 | THIS_FILE_PATH = os.path.abspath(__file__) |
| 28 | |
hinoka@google.com | 2f2ca14 | 2014-01-07 03:59:18 +0000 | [diff] [blame] | 29 | GSUTIL_DEFAULT_PATH = os.path.join( |
hinoka@chromium.org | b091aa5 | 2014-12-20 01:47:31 +0000 | [diff] [blame] | 30 | os.path.dirname(os.path.abspath(__file__)), 'gsutil.py') |
hinoka@google.com | 2f2ca14 | 2014-01-07 03:59:18 +0000 | [diff] [blame] | 31 | |
maruel@chromium.org | 79d6237 | 2015-06-01 18:50:55 +0000 | [diff] [blame] | 32 | |
smut | ae7ea31 | 2016-07-18 11:59:41 -0700 | [diff] [blame] | 33 | class NoUsableRevError(gclient_utils.Error): |
| 34 | """Raised if requested revision isn't found in checkout.""" |
| 35 | |
| 36 | |
haitao.feng@intel.com | 306080c | 2012-05-04 13:11:29 +0000 | [diff] [blame] | 37 | class DiffFiltererWrapper(object): |
| 38 | """Simple base class which tracks which file is being diffed and |
maruel@chromium.org | ee4071d | 2009-12-22 22:25:37 +0000 | [diff] [blame] | 39 | replaces instances of its file name in the original and |
agable | 41e3a6c | 2016-10-20 11:36:56 -0700 | [diff] [blame] | 40 | working copy lines of the git diff output.""" |
haitao.feng@intel.com | 306080c | 2012-05-04 13:11:29 +0000 | [diff] [blame] | 41 | index_string = None |
maruel@chromium.org | ee4071d | 2009-12-22 22:25:37 +0000 | [diff] [blame] | 42 | original_prefix = "--- " |
| 43 | working_prefix = "+++ " |
| 44 | |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 45 | def __init__(self, relpath, print_func): |
maruel@chromium.org | ee4071d | 2009-12-22 22:25:37 +0000 | [diff] [blame] | 46 | # Note that we always use '/' as the path separator to be |
agable | 41e3a6c | 2016-10-20 11:36:56 -0700 | [diff] [blame] | 47 | # consistent with cygwin-style output on Windows |
maruel@chromium.org | ee4071d | 2009-12-22 22:25:37 +0000 | [diff] [blame] | 48 | self._relpath = relpath.replace("\\", "/") |
haitao.feng@intel.com | 306080c | 2012-05-04 13:11:29 +0000 | [diff] [blame] | 49 | self._current_file = None |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 50 | self._print_func = print_func |
maruel@chromium.org | ee4071d | 2009-12-22 22:25:37 +0000 | [diff] [blame] | 51 | |
maruel@chromium.org | 6e29d57 | 2010-06-04 17:32:20 +0000 | [diff] [blame] | 52 | def SetCurrentFile(self, current_file): |
| 53 | self._current_file = current_file |
haitao.feng@intel.com | 306080c | 2012-05-04 13:11:29 +0000 | [diff] [blame] | 54 | |
iannucci@chromium.org | 3830a67 | 2013-02-19 20:15:14 +0000 | [diff] [blame] | 55 | @property |
| 56 | def _replacement_file(self): |
haitao.feng@intel.com | 306080c | 2012-05-04 13:11:29 +0000 | [diff] [blame] | 57 | return posixpath.join(self._relpath, self._current_file) |
maruel@chromium.org | ee4071d | 2009-12-22 22:25:37 +0000 | [diff] [blame] | 58 | |
maruel@chromium.org | f5d37bf | 2010-09-02 00:50:34 +0000 | [diff] [blame] | 59 | def _Replace(self, line): |
| 60 | return line.replace(self._current_file, self._replacement_file) |
maruel@chromium.org | ee4071d | 2009-12-22 22:25:37 +0000 | [diff] [blame] | 61 | |
| 62 | def Filter(self, line): |
| 63 | if (line.startswith(self.index_string)): |
| 64 | self.SetCurrentFile(line[len(self.index_string):]) |
maruel@chromium.org | f5d37bf | 2010-09-02 00:50:34 +0000 | [diff] [blame] | 65 | line = self._Replace(line) |
maruel@chromium.org | ee4071d | 2009-12-22 22:25:37 +0000 | [diff] [blame] | 66 | else: |
| 67 | if (line.startswith(self.original_prefix) or |
| 68 | line.startswith(self.working_prefix)): |
maruel@chromium.org | f5d37bf | 2010-09-02 00:50:34 +0000 | [diff] [blame] | 69 | line = self._Replace(line) |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 70 | self._print_func(line) |
maruel@chromium.org | ee4071d | 2009-12-22 22:25:37 +0000 | [diff] [blame] | 71 | |
| 72 | |
haitao.feng@intel.com | 306080c | 2012-05-04 13:11:29 +0000 | [diff] [blame] | 73 | class GitDiffFilterer(DiffFiltererWrapper): |
| 74 | index_string = "diff --git " |
| 75 | |
| 76 | def SetCurrentFile(self, current_file): |
| 77 | # Get filename by parsing "a/<filename> b/<filename>" |
| 78 | self._current_file = current_file[:(len(current_file)/2)][2:] |
| 79 | |
| 80 | def _Replace(self, line): |
| 81 | return re.sub("[a|b]/" + self._current_file, self._replacement_file, line) |
| 82 | |
| 83 | |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 84 | ### SCM abstraction layer |
| 85 | |
msb@chromium.org | cb5442b | 2009-09-22 16:51:24 +0000 | [diff] [blame] | 86 | # Factory Method for SCM wrapper creation |
| 87 | |
maruel@chromium.org | 9eda411 | 2010-06-11 18:56:10 +0000 | [diff] [blame] | 88 | def GetScmName(url): |
agable | 41e3a6c | 2016-10-20 11:36:56 -0700 | [diff] [blame] | 89 | if not url: |
| 90 | return None |
| 91 | url, _ = gclient_utils.SplitUrlRevision(url) |
| 92 | if url.endswith('.git'): |
| 93 | return 'git' |
| 94 | protocol = url.split('://')[0] |
| 95 | if protocol in ( |
| 96 | 'file', 'git', 'git+http', 'git+https', 'http', 'https', 'ssh', 'sso'): |
| 97 | return 'git' |
maruel@chromium.org | 9eda411 | 2010-06-11 18:56:10 +0000 | [diff] [blame] | 98 | return None |
| 99 | |
| 100 | |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 101 | def CreateSCM(url, root_dir=None, relpath=None, out_fh=None, out_cb=None): |
maruel@chromium.org | 9eda411 | 2010-06-11 18:56:10 +0000 | [diff] [blame] | 102 | SCM_MAP = { |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 103 | 'git' : GitWrapper, |
msb@chromium.org | cb5442b | 2009-09-22 16:51:24 +0000 | [diff] [blame] | 104 | } |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 105 | |
maruel@chromium.org | 9eda411 | 2010-06-11 18:56:10 +0000 | [diff] [blame] | 106 | scm_name = GetScmName(url) |
| 107 | if not scm_name in SCM_MAP: |
| 108 | raise gclient_utils.Error('No SCM found for url %s' % url) |
mukai@chromium.org | 9e3e82c | 2012-04-18 12:55:43 +0000 | [diff] [blame] | 109 | scm_class = SCM_MAP[scm_name] |
| 110 | if not scm_class.BinaryExists(): |
| 111 | raise gclient_utils.Error('%s command not found' % scm_name) |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 112 | return scm_class(url, root_dir, relpath, out_fh, out_cb) |
msb@chromium.org | cb5442b | 2009-09-22 16:51:24 +0000 | [diff] [blame] | 113 | |
| 114 | |
| 115 | # SCMWrapper base class |
| 116 | |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 117 | class SCMWrapper(object): |
| 118 | """Add necessary glue between all the supported SCM. |
| 119 | |
msb@chromium.org | d650421 | 2010-01-13 17:34:31 +0000 | [diff] [blame] | 120 | This is the abstraction layer to bind to different SCM. |
| 121 | """ |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 122 | |
| 123 | def __init__(self, url=None, root_dir=None, relpath=None, out_fh=None, |
| 124 | out_cb=None): |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 125 | self.url = url |
maruel@chromium.org | 5e73b0c | 2009-09-18 19:47:48 +0000 | [diff] [blame] | 126 | self._root_dir = root_dir |
| 127 | if self._root_dir: |
| 128 | self._root_dir = self._root_dir.replace('/', os.sep) |
| 129 | self.relpath = relpath |
| 130 | if self.relpath: |
| 131 | self.relpath = self.relpath.replace('/', os.sep) |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 132 | if self.relpath and self._root_dir: |
| 133 | self.checkout_path = os.path.join(self._root_dir, self.relpath) |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 134 | if out_fh is None: |
| 135 | out_fh = sys.stdout |
| 136 | self.out_fh = out_fh |
| 137 | self.out_cb = out_cb |
| 138 | |
| 139 | def Print(self, *args, **kwargs): |
| 140 | kwargs.setdefault('file', self.out_fh) |
| 141 | if kwargs.pop('timestamp', True): |
| 142 | self.out_fh.write('[%s] ' % gclient_utils.Elapsed()) |
| 143 | print(*args, **kwargs) |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 144 | |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 145 | def RunCommand(self, command, options, args, file_list=None): |
agable | debf6c8 | 2016-12-21 12:50:12 -0800 | [diff] [blame] | 146 | commands = ['update', 'updatesingle', 'revert', |
tony@chromium.org | 4b5b177 | 2010-04-08 01:52:56 +0000 | [diff] [blame] | 147 | 'revinfo', 'status', 'diff', 'pack', 'runhooks'] |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 148 | |
| 149 | if not command in commands: |
| 150 | raise gclient_utils.Error('Unknown command %s' % command) |
| 151 | |
msb@chromium.org | cb5442b | 2009-09-22 16:51:24 +0000 | [diff] [blame] | 152 | if not command in dir(self): |
maruel@chromium.org | ee4071d | 2009-12-22 22:25:37 +0000 | [diff] [blame] | 153 | raise gclient_utils.Error('Command %s not implemented in %s wrapper' % ( |
maruel@chromium.org | 9eda411 | 2010-06-11 18:56:10 +0000 | [diff] [blame] | 154 | command, self.__class__.__name__)) |
msb@chromium.org | cb5442b | 2009-09-22 16:51:24 +0000 | [diff] [blame] | 155 | |
| 156 | return getattr(self, command)(options, args, file_list) |
| 157 | |
hinoka@chromium.org | fa2b9b4 | 2014-08-22 18:08:53 +0000 | [diff] [blame] | 158 | @staticmethod |
| 159 | def _get_first_remote_url(checkout_path): |
| 160 | log = scm.GIT.Capture( |
| 161 | ['config', '--local', '--get-regexp', r'remote.*.url'], |
| 162 | cwd=checkout_path) |
| 163 | # Get the second token of the first line of the log. |
| 164 | return log.splitlines()[0].split(' ', 1)[1] |
| 165 | |
levarum@chromium.org | 27a6f9a | 2016-05-28 00:21:49 +0000 | [diff] [blame] | 166 | def GetCacheMirror(self): |
| 167 | if (getattr(self, 'cache_dir', None)): |
| 168 | url, _ = gclient_utils.SplitUrlRevision(self.url) |
| 169 | return git_cache.Mirror(url) |
| 170 | return None |
| 171 | |
smut@google.com | d33eab3 | 2014-07-07 19:35:18 +0000 | [diff] [blame] | 172 | def GetActualRemoteURL(self, options): |
borenet@google.com | 88d1008 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 173 | """Attempt to determine the remote URL for this SCMWrapper.""" |
smut@google.com | d33eab3 | 2014-07-07 19:35:18 +0000 | [diff] [blame] | 174 | # Git |
borenet@google.com | bda475e | 2014-03-24 19:04:45 +0000 | [diff] [blame] | 175 | if os.path.exists(os.path.join(self.checkout_path, '.git')): |
hinoka@chromium.org | fa2b9b4 | 2014-08-22 18:08:53 +0000 | [diff] [blame] | 176 | actual_remote_url = self._get_first_remote_url(self.checkout_path) |
borenet@google.com | 4e9be26 | 2014-04-08 19:40:30 +0000 | [diff] [blame] | 177 | |
levarum@chromium.org | 27a6f9a | 2016-05-28 00:21:49 +0000 | [diff] [blame] | 178 | mirror = self.GetCacheMirror() |
| 179 | # If the cache is used, obtain the actual remote URL from there. |
| 180 | if (mirror and mirror.exists() and |
| 181 | mirror.mirror_path.replace('\\', '/') == |
| 182 | actual_remote_url.replace('\\', '/')): |
| 183 | actual_remote_url = self._get_first_remote_url(mirror.mirror_path) |
borenet@google.com | 4e9be26 | 2014-04-08 19:40:30 +0000 | [diff] [blame] | 184 | return actual_remote_url |
borenet@google.com | 88d1008 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 185 | return None |
| 186 | |
borenet@google.com | 4e9be26 | 2014-04-08 19:40:30 +0000 | [diff] [blame] | 187 | def DoesRemoteURLMatch(self, options): |
borenet@google.com | 88d1008 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 188 | """Determine whether the remote URL of this checkout is the expected URL.""" |
| 189 | if not os.path.exists(self.checkout_path): |
| 190 | # A checkout which doesn't exist can't be broken. |
| 191 | return True |
| 192 | |
smut@google.com | d33eab3 | 2014-07-07 19:35:18 +0000 | [diff] [blame] | 193 | actual_remote_url = self.GetActualRemoteURL(options) |
borenet@google.com | 88d1008 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 194 | if actual_remote_url: |
borenet@google.com | 8156c9f | 2014-04-01 16:41:36 +0000 | [diff] [blame] | 195 | return (gclient_utils.SplitUrlRevision(actual_remote_url)[0].rstrip('/') |
| 196 | == gclient_utils.SplitUrlRevision(self.url)[0].rstrip('/')) |
borenet@google.com | 88d1008 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 197 | else: |
| 198 | # This may occur if the self.checkout_path exists but does not contain a |
agable | 41e3a6c | 2016-10-20 11:36:56 -0700 | [diff] [blame] | 199 | # valid git checkout. |
borenet@google.com | 88d1008 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 200 | return False |
| 201 | |
borenet@google.com | b09097a | 2014-04-09 19:09:08 +0000 | [diff] [blame] | 202 | def _DeleteOrMove(self, force): |
| 203 | """Delete the checkout directory or move it out of the way. |
| 204 | |
| 205 | Args: |
| 206 | force: bool; if True, delete the directory. Otherwise, just move it. |
| 207 | """ |
borenet@google.com | b225621 | 2014-05-07 20:57:28 +0000 | [diff] [blame] | 208 | if force and os.environ.get('CHROME_HEADLESS') == '1': |
| 209 | self.Print('_____ Conflicting directory found in %s. Removing.' |
| 210 | % self.checkout_path) |
| 211 | gclient_utils.AddWarning('Conflicting directory %s deleted.' |
| 212 | % self.checkout_path) |
| 213 | gclient_utils.rmtree(self.checkout_path) |
| 214 | else: |
| 215 | bad_scm_dir = os.path.join(self._root_dir, '_bad_scm', |
| 216 | os.path.dirname(self.relpath)) |
| 217 | |
| 218 | try: |
| 219 | os.makedirs(bad_scm_dir) |
| 220 | except OSError as e: |
| 221 | if e.errno != errno.EEXIST: |
| 222 | raise |
| 223 | |
| 224 | dest_path = tempfile.mkdtemp( |
| 225 | prefix=os.path.basename(self.relpath), |
| 226 | dir=bad_scm_dir) |
| 227 | self.Print('_____ Conflicting directory found in %s. Moving to %s.' |
| 228 | % (self.checkout_path, dest_path)) |
| 229 | gclient_utils.AddWarning('Conflicting directory %s moved to %s.' |
| 230 | % (self.checkout_path, dest_path)) |
| 231 | shutil.move(self.checkout_path, dest_path) |
borenet@google.com | b09097a | 2014-04-09 19:09:08 +0000 | [diff] [blame] | 232 | |
msb@chromium.org | cb5442b | 2009-09-22 16:51:24 +0000 | [diff] [blame] | 233 | |
maruel@chromium.org | 55e724e | 2010-03-11 19:36:49 +0000 | [diff] [blame] | 234 | class GitWrapper(SCMWrapper): |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 235 | """Wrapper for Git""" |
iannucci@chromium.org | 2702bcd | 2013-09-24 19:10:07 +0000 | [diff] [blame] | 236 | name = 'git' |
maruel@chromium.org | 1a60dca | 2013-11-26 14:06:26 +0000 | [diff] [blame] | 237 | remote = 'origin' |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 238 | |
iannucci@chromium.org | 53456aa | 2013-07-03 19:38:34 +0000 | [diff] [blame] | 239 | cache_dir = None |
iannucci@chromium.org | 53456aa | 2013-07-03 19:38:34 +0000 | [diff] [blame] | 240 | |
szager@chromium.org | 848fd49 | 2014-04-09 19:06:44 +0000 | [diff] [blame] | 241 | def __init__(self, url=None, *args): |
igorgatis@gmail.com | 4e07567 | 2011-11-21 16:35:08 +0000 | [diff] [blame] | 242 | """Removes 'git+' fake prefix from git URL.""" |
| 243 | if url.startswith('git+http://') or url.startswith('git+https://'): |
| 244 | url = url[4:] |
szager@chromium.org | 848fd49 | 2014-04-09 19:06:44 +0000 | [diff] [blame] | 245 | SCMWrapper.__init__(self, url, *args) |
| 246 | filter_kwargs = { 'time_throttle': 1, 'out_fh': self.out_fh } |
| 247 | if self.out_cb: |
| 248 | filter_kwargs['predicate'] = self.out_cb |
| 249 | self.filter = gclient_utils.GitFilter(**filter_kwargs) |
igorgatis@gmail.com | 4e07567 | 2011-11-21 16:35:08 +0000 | [diff] [blame] | 250 | |
mukai@chromium.org | 9e3e82c | 2012-04-18 12:55:43 +0000 | [diff] [blame] | 251 | @staticmethod |
| 252 | def BinaryExists(): |
| 253 | """Returns true if the command exists.""" |
| 254 | try: |
| 255 | # We assume git is newer than 1.7. See: crbug.com/114483 |
| 256 | result, version = scm.GIT.AssertVersion('1.7') |
| 257 | if not result: |
| 258 | raise gclient_utils.Error('Git version is older than 1.7: %s' % version) |
| 259 | return result |
| 260 | except OSError: |
| 261 | return False |
| 262 | |
xusydoc@chromium.org | 885a960 | 2013-05-31 09:54:40 +0000 | [diff] [blame] | 263 | def GetCheckoutRoot(self): |
| 264 | return scm.GIT.GetCheckoutRoot(self.checkout_path) |
| 265 | |
iannucci@chromium.org | 396e1a6 | 2013-07-03 19:41:04 +0000 | [diff] [blame] | 266 | def GetRevisionDate(self, _revision): |
floitsch@google.com | eaab784 | 2011-04-28 09:07:58 +0000 | [diff] [blame] | 267 | """Returns the given revision's date in ISO-8601 format (which contains the |
| 268 | time zone).""" |
| 269 | # TODO(floitsch): get the time-stamp of the given revision and not just the |
| 270 | # time-stamp of the currently checked out revision. |
| 271 | return self._Capture(['log', '-n', '1', '--format=%ai']) |
| 272 | |
iannucci@chromium.org | 396e1a6 | 2013-07-03 19:41:04 +0000 | [diff] [blame] | 273 | def diff(self, options, _args, _file_list): |
raphael.kubo.da.costa | 05c8359 | 2016-08-04 08:32:41 -0700 | [diff] [blame] | 274 | try: |
| 275 | merge_base = [self._Capture(['merge-base', 'HEAD', self.remote])] |
| 276 | except subprocess2.CalledProcessError: |
| 277 | merge_base = [] |
| 278 | self._Run(['diff'] + merge_base, options) |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 279 | |
iannucci@chromium.org | 396e1a6 | 2013-07-03 19:41:04 +0000 | [diff] [blame] | 280 | def pack(self, _options, _args, _file_list): |
maruel@chromium.org | ee4071d | 2009-12-22 22:25:37 +0000 | [diff] [blame] | 281 | """Generates a patch file which can be applied to the root of the |
msb@chromium.org | d650421 | 2010-01-13 17:34:31 +0000 | [diff] [blame] | 282 | repository. |
| 283 | |
| 284 | The patch file is generated from a diff of the merge base of HEAD and |
| 285 | its upstream branch. |
| 286 | """ |
raphael.kubo.da.costa | 05c8359 | 2016-08-04 08:32:41 -0700 | [diff] [blame] | 287 | try: |
| 288 | merge_base = [self._Capture(['merge-base', 'HEAD', self.remote])] |
| 289 | except subprocess2.CalledProcessError: |
| 290 | merge_base = [] |
maruel@chromium.org | 17d0179 | 2010-09-01 18:07:10 +0000 | [diff] [blame] | 291 | gclient_utils.CheckCallAndFilter( |
raphael.kubo.da.costa | 05c8359 | 2016-08-04 08:32:41 -0700 | [diff] [blame] | 292 | ['git', 'diff'] + merge_base, |
maruel@chromium.org | 8469bf9 | 2010-09-03 19:03:15 +0000 | [diff] [blame] | 293 | cwd=self.checkout_path, |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 294 | filter_fn=GitDiffFilterer(self.relpath, print_func=self.Print).Filter) |
maruel@chromium.org | ee4071d | 2009-12-22 22:25:37 +0000 | [diff] [blame] | 295 | |
Robert Iannucci | c41d8b9 | 2017-02-16 17:07:37 -0800 | [diff] [blame] | 296 | def _Scrub(self, target, options): |
| 297 | """Scrubs out all changes in the local repo, back to the state of target.""" |
mmoss@chromium.org | 50fd47f | 2014-02-13 01:03:19 +0000 | [diff] [blame] | 298 | quiet = [] |
| 299 | if not options.verbose: |
| 300 | quiet = ['--quiet'] |
Robert Iannucci | c41d8b9 | 2017-02-16 17:07:37 -0800 | [diff] [blame] | 301 | self._Run(['reset', '--hard', target] + quiet, options) |
| 302 | if options.force and options.delete_unversioned_trees: |
| 303 | # where `target` is a commit that contains both upper and lower case |
| 304 | # versions of the same file on a case insensitive filesystem, we are |
| 305 | # actually in a broken state here. The index will have both 'a' and 'A', |
| 306 | # but only one of them will exist on the disk. To progress, we delete |
| 307 | # everything that status thinks is modified. |
Robert Iannucci | a7a9ceb | 2017-02-16 17:38:06 -0800 | [diff] [blame] | 308 | output = self._Capture(['status', '--porcelain'], strip=False) |
| 309 | for line in output.splitlines(): |
Robert Iannucci | c41d8b9 | 2017-02-16 17:07:37 -0800 | [diff] [blame] | 310 | # --porcelain (v1) looks like: |
| 311 | # XY filename |
| 312 | try: |
| 313 | filename = line[3:] |
| 314 | self.Print('_____ Deleting residual after reset: %r.' % filename) |
| 315 | gclient_utils.rm_file_or_tree( |
Robert Iannucci | a7a9ceb | 2017-02-16 17:38:06 -0800 | [diff] [blame] | 316 | os.path.join(self.checkout_path, filename)) |
Robert Iannucci | c41d8b9 | 2017-02-16 17:07:37 -0800 | [diff] [blame] | 317 | except OSError: |
| 318 | pass |
| 319 | |
| 320 | def _FetchAndReset(self, revision, file_list, options): |
| 321 | """Equivalent to git fetch; git reset.""" |
mmoss@chromium.org | 50fd47f | 2014-02-13 01:03:19 +0000 | [diff] [blame] | 322 | self._UpdateBranchHeads(options, fetch=False) |
| 323 | |
dnj@chromium.org | 680f217 | 2014-06-25 00:39:32 +0000 | [diff] [blame] | 324 | self._Fetch(options, prune=True, quiet=options.verbose) |
Robert Iannucci | c41d8b9 | 2017-02-16 17:07:37 -0800 | [diff] [blame] | 325 | self._Scrub(revision, options) |
mmoss@chromium.org | 50fd47f | 2014-02-13 01:03:19 +0000 | [diff] [blame] | 326 | if file_list is not None: |
| 327 | files = self._Capture(['ls-files']).splitlines() |
| 328 | file_list.extend([os.path.join(self.checkout_path, f) for f in files]) |
| 329 | |
szager@chromium.org | 8a13970 | 2014-06-20 15:55:01 +0000 | [diff] [blame] | 330 | def _DisableHooks(self): |
| 331 | hook_dir = os.path.join(self.checkout_path, '.git', 'hooks') |
| 332 | if not os.path.isdir(hook_dir): |
| 333 | return |
| 334 | for f in os.listdir(hook_dir): |
| 335 | if not f.endswith('.sample') and not f.endswith('.disabled'): |
primiano@chromium.org | 4126556 | 2015-04-08 09:14:46 +0000 | [diff] [blame] | 336 | disabled_hook_path = os.path.join(hook_dir, f + '.disabled') |
| 337 | if os.path.exists(disabled_hook_path): |
| 338 | os.remove(disabled_hook_path) |
| 339 | os.rename(os.path.join(hook_dir, f), disabled_hook_path) |
szager@chromium.org | 8a13970 | 2014-06-20 15:55:01 +0000 | [diff] [blame] | 340 | |
iannucci@chromium.org | 30a0798 | 2016-04-07 21:35:19 +0000 | [diff] [blame] | 341 | def _maybe_break_locks(self, options): |
| 342 | """This removes all .lock files from this repo's .git directory, if the |
| 343 | user passed the --break_repo_locks command line flag. |
| 344 | |
| 345 | In particular, this will cleanup index.lock files, as well as ref lock |
| 346 | files. |
| 347 | """ |
| 348 | if options.break_repo_locks: |
| 349 | git_dir = os.path.join(self.checkout_path, '.git') |
| 350 | for path, _, filenames in os.walk(git_dir): |
| 351 | for filename in filenames: |
| 352 | if filename.endswith('.lock'): |
| 353 | to_break = os.path.join(path, filename) |
| 354 | self.Print('breaking lock: %s' % (to_break,)) |
| 355 | try: |
| 356 | os.remove(to_break) |
| 357 | except OSError as ex: |
| 358 | self.Print('FAILED to break lock: %s: %s' % (to_break, ex)) |
| 359 | raise |
| 360 | |
| 361 | |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 362 | def update(self, options, args, file_list): |
| 363 | """Runs git to update or transparently checkout the working copy. |
| 364 | |
| 365 | All updated files will be appended to file_list. |
| 366 | |
| 367 | Raises: |
| 368 | Error: if can't get URL for relative path. |
| 369 | """ |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 370 | if args: |
| 371 | raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args)) |
| 372 | |
nasser@codeaurora.org | ece406f | 2010-02-23 17:29:15 +0000 | [diff] [blame] | 373 | self._CheckMinVersion("1.6.6") |
msb@chromium.org | 923a037 | 2009-12-11 20:42:43 +0000 | [diff] [blame] | 374 | |
maruel@chromium.org | 1a60dca | 2013-11-26 14:06:26 +0000 | [diff] [blame] | 375 | # If a dependency is not pinned, track the default remote branch. |
smut@google.com | d33eab3 | 2014-07-07 19:35:18 +0000 | [diff] [blame] | 376 | default_rev = 'refs/remotes/%s/master' % self.remote |
nasser@codeaurora.org | 7080e94 | 2010-03-15 15:06:16 +0000 | [diff] [blame] | 377 | url, deps_revision = gclient_utils.SplitUrlRevision(self.url) |
nasser@codeaurora.org | 7080e94 | 2010-03-15 15:06:16 +0000 | [diff] [blame] | 378 | revision = deps_revision |
cmp@chromium.org | eb2756d | 2011-09-20 20:17:51 +0000 | [diff] [blame] | 379 | managed = True |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 380 | if options.revision: |
msb@chromium.org | ac915bb | 2009-11-13 17:03:01 +0000 | [diff] [blame] | 381 | # Override the revision number. |
| 382 | revision = str(options.revision) |
cmp@chromium.org | eb2756d | 2011-09-20 20:17:51 +0000 | [diff] [blame] | 383 | if revision == 'unmanaged': |
romain.pokrzywka@gmail.com | 483a0ba | 2014-05-30 00:06:07 +0000 | [diff] [blame] | 384 | # Check again for a revision in case an initial ref was specified |
| 385 | # in the url, for example bla.git@refs/heads/custombranch |
| 386 | revision = deps_revision |
cmp@chromium.org | eb2756d | 2011-09-20 20:17:51 +0000 | [diff] [blame] | 387 | managed = False |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 388 | if not revision: |
| 389 | revision = default_rev |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 390 | |
szager@chromium.org | 8a13970 | 2014-06-20 15:55:01 +0000 | [diff] [blame] | 391 | if managed: |
| 392 | self._DisableHooks() |
| 393 | |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 394 | printed_path = False |
| 395 | verbose = [] |
msb@chromium.org | b1a22bf | 2009-11-07 02:33:50 +0000 | [diff] [blame] | 396 | if options.verbose: |
agable | 83faed0 | 2016-10-24 14:37:10 -0700 | [diff] [blame] | 397 | self.Print('_____ %s at %s' % (self.relpath, revision), timestamp=False) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 398 | verbose = ['--verbose'] |
| 399 | printed_path = True |
| 400 | |
mmoss@chromium.org | 6e7202b | 2014-09-09 18:23:39 +0000 | [diff] [blame] | 401 | remote_ref = scm.GIT.RefToRemoteRef(revision, self.remote) |
| 402 | if remote_ref: |
smut@google.com | d33eab3 | 2014-07-07 19:35:18 +0000 | [diff] [blame] | 403 | # Rewrite remote refs to their local equivalents. |
mmoss@chromium.org | 6e7202b | 2014-09-09 18:23:39 +0000 | [diff] [blame] | 404 | revision = ''.join(remote_ref) |
| 405 | rev_type = "branch" |
| 406 | elif revision.startswith('refs/'): |
| 407 | # Local branch? We probably don't want to support, since DEPS should |
| 408 | # always specify branches as they are in the upstream repo. |
smut@google.com | d33eab3 | 2014-07-07 19:35:18 +0000 | [diff] [blame] | 409 | rev_type = "branch" |
| 410 | else: |
| 411 | # hash is also a tag, only make a distinction at checkout |
| 412 | rev_type = "hash" |
| 413 | |
szager@chromium.org | b0a13a2 | 2014-06-18 00:52:25 +0000 | [diff] [blame] | 414 | mirror = self._GetMirror(url, options) |
| 415 | if mirror: |
| 416 | url = mirror.mirror_path |
| 417 | |
primiano@chromium.org | 1c12738 | 2015-02-17 11:15:40 +0000 | [diff] [blame] | 418 | # If we are going to introduce a new project, there is a possibility that |
| 419 | # we are syncing back to a state where the project was originally a |
| 420 | # sub-project rolled by DEPS (realistic case: crossing the Blink merge point |
| 421 | # syncing backwards, when Blink was a DEPS entry and not part of src.git). |
| 422 | # In such case, we might have a backup of the former .git folder, which can |
| 423 | # be used to avoid re-fetching the entire repo again (useful for bisects). |
| 424 | backup_dir = self.GetGitBackupDirPath() |
| 425 | target_dir = os.path.join(self.checkout_path, '.git') |
| 426 | if os.path.exists(backup_dir) and not os.path.exists(target_dir): |
| 427 | gclient_utils.safe_makedirs(self.checkout_path) |
| 428 | os.rename(backup_dir, target_dir) |
| 429 | # Reset to a clean state |
Robert Iannucci | c41d8b9 | 2017-02-16 17:07:37 -0800 | [diff] [blame] | 430 | self._Scrub('HEAD', options) |
primiano@chromium.org | 1c12738 | 2015-02-17 11:15:40 +0000 | [diff] [blame] | 431 | |
szager@chromium.org | 6c2b49d | 2014-02-26 23:57:38 +0000 | [diff] [blame] | 432 | if (not os.path.exists(self.checkout_path) or |
| 433 | (os.path.isdir(self.checkout_path) and |
| 434 | not os.path.exists(os.path.join(self.checkout_path, '.git')))): |
szager@chromium.org | b0a13a2 | 2014-06-18 00:52:25 +0000 | [diff] [blame] | 435 | if mirror: |
| 436 | self._UpdateMirror(mirror, options) |
borenet@google.com | 90fe58b | 2014-05-01 18:22:00 +0000 | [diff] [blame] | 437 | try: |
smut@google.com | d33eab3 | 2014-07-07 19:35:18 +0000 | [diff] [blame] | 438 | self._Clone(revision, url, options) |
borenet@google.com | 90fe58b | 2014-05-01 18:22:00 +0000 | [diff] [blame] | 439 | except subprocess2.CalledProcessError: |
| 440 | self._DeleteOrMove(options.force) |
smut@google.com | d33eab3 | 2014-07-07 19:35:18 +0000 | [diff] [blame] | 441 | self._Clone(revision, url, options) |
iannucci@chromium.org | 396e1a6 | 2013-07-03 19:41:04 +0000 | [diff] [blame] | 442 | if file_list is not None: |
| 443 | files = self._Capture(['ls-files']).splitlines() |
| 444 | file_list.extend([os.path.join(self.checkout_path, f) for f in files]) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 445 | if not verbose: |
| 446 | # Make the output a little prettier. It's nice to have some whitespace |
| 447 | # between projects when cloning. |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 448 | self.Print('') |
iannucci@chromium.org | 2702bcd | 2013-09-24 19:10:07 +0000 | [diff] [blame] | 449 | return self._Capture(['rev-parse', '--verify', 'HEAD']) |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 450 | |
szager@chromium.org | 3dc5cb7 | 2014-06-17 15:06:05 +0000 | [diff] [blame] | 451 | if not managed: |
| 452 | self._UpdateBranchHeads(options, fetch=False) |
| 453 | self.Print('________ unmanaged solution; skipping %s' % self.relpath) |
| 454 | return self._Capture(['rev-parse', '--verify', 'HEAD']) |
| 455 | |
iannucci@chromium.org | 30a0798 | 2016-04-07 21:35:19 +0000 | [diff] [blame] | 456 | self._maybe_break_locks(options) |
| 457 | |
szager@chromium.org | b0a13a2 | 2014-06-18 00:52:25 +0000 | [diff] [blame] | 458 | if mirror: |
| 459 | self._UpdateMirror(mirror, options) |
| 460 | |
szager@chromium.org | 6c2b49d | 2014-02-26 23:57:38 +0000 | [diff] [blame] | 461 | # See if the url has changed (the unittests use git://foo for the url, let |
| 462 | # that through). |
| 463 | current_url = self._Capture(['config', 'remote.%s.url' % self.remote]) |
| 464 | return_early = False |
| 465 | # TODO(maruel): Delete url != 'git://foo' since it's just to make the |
| 466 | # unit test pass. (and update the comment above) |
| 467 | # Skip url auto-correction if remote.origin.gclient-auto-fix-url is set. |
| 468 | # This allows devs to use experimental repos which have a different url |
| 469 | # but whose branch(s) are the same as official repos. |
borenet@google.com | b09097a | 2014-04-09 19:09:08 +0000 | [diff] [blame] | 470 | if (current_url.rstrip('/') != url.rstrip('/') and |
szager@chromium.org | 6c2b49d | 2014-02-26 23:57:38 +0000 | [diff] [blame] | 471 | url != 'git://foo' and |
| 472 | subprocess2.capture( |
| 473 | ['git', 'config', 'remote.%s.gclient-auto-fix-url' % self.remote], |
| 474 | cwd=self.checkout_path).strip() != 'False'): |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 475 | self.Print('_____ switching %s to a new upstream' % self.relpath) |
iannucci@chromium.org | 7851421 | 2014-08-20 23:08:00 +0000 | [diff] [blame] | 476 | if not (options.force or options.reset): |
| 477 | # Make sure it's clean |
agable | 83faed0 | 2016-10-24 14:37:10 -0700 | [diff] [blame] | 478 | self._CheckClean(revision) |
szager@chromium.org | 6c2b49d | 2014-02-26 23:57:38 +0000 | [diff] [blame] | 479 | # Switch over to the new upstream |
| 480 | self._Run(['remote', 'set-url', self.remote, url], options) |
szager@chromium.org | 8dd3546 | 2015-06-08 22:56:05 +0000 | [diff] [blame] | 481 | if mirror: |
| 482 | with open(os.path.join( |
| 483 | self.checkout_path, '.git', 'objects', 'info', 'alternates'), |
| 484 | 'w') as fh: |
| 485 | fh.write(os.path.join(url, 'objects')) |
tandrii@chromium.org | c438c14 | 2015-08-24 22:55:55 +0000 | [diff] [blame] | 486 | self._EnsureValidHeadObjectOrCheckout(revision, options, url) |
smut@google.com | d33eab3 | 2014-07-07 19:35:18 +0000 | [diff] [blame] | 487 | self._FetchAndReset(revision, file_list, options) |
tandrii@chromium.org | c438c14 | 2015-08-24 22:55:55 +0000 | [diff] [blame] | 488 | |
szager@chromium.org | 6c2b49d | 2014-02-26 23:57:38 +0000 | [diff] [blame] | 489 | return_early = True |
tandrii@chromium.org | c438c14 | 2015-08-24 22:55:55 +0000 | [diff] [blame] | 490 | else: |
| 491 | self._EnsureValidHeadObjectOrCheckout(revision, options, url) |
szager@chromium.org | 6c2b49d | 2014-02-26 23:57:38 +0000 | [diff] [blame] | 492 | |
mmoss@chromium.org | 50fd47f | 2014-02-13 01:03:19 +0000 | [diff] [blame] | 493 | if return_early: |
| 494 | return self._Capture(['rev-parse', '--verify', 'HEAD']) |
| 495 | |
msb@chromium.org | 5bde485 | 2009-12-14 16:47:12 +0000 | [diff] [blame] | 496 | cur_branch = self._GetCurrentBranch() |
| 497 | |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 498 | # Cases: |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 499 | # 0) HEAD is detached. Probably from our initial clone. |
| 500 | # - make sure HEAD is contained by a named ref, then update. |
| 501 | # Cases 1-4. HEAD is a branch. |
agable | 41e3a6c | 2016-10-20 11:36:56 -0700 | [diff] [blame] | 502 | # 1) current branch is not tracking a remote branch |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 503 | # - try to rebase onto the new hash or branch |
| 504 | # 2) current branch is tracking a remote branch with local committed |
| 505 | # changes, but the DEPS file switched to point to a hash |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 506 | # - rebase those changes on top of the hash |
mmoss@chromium.org | 6e7202b | 2014-09-09 18:23:39 +0000 | [diff] [blame] | 507 | # 3) current branch is tracking a remote branch w/or w/out changes, and |
| 508 | # no DEPS switch |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 509 | # - see if we can FF, if not, prompt the user for rebase, merge, or stop |
mmoss@chromium.org | 6e7202b | 2014-09-09 18:23:39 +0000 | [diff] [blame] | 510 | # 4) current branch is tracking a remote branch, but DEPS switches to a |
| 511 | # different remote branch, and |
| 512 | # a) current branch has no local changes, and --force: |
| 513 | # - checkout new branch |
| 514 | # b) current branch has local changes, and --force and --reset: |
| 515 | # - checkout new branch |
| 516 | # c) otherwise exit |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 517 | |
maruel@chromium.org | 81e012c | 2010-04-29 16:07:24 +0000 | [diff] [blame] | 518 | # GetUpstreamBranch returns something like 'refs/remotes/origin/master' for |
| 519 | # a tracking branch |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 520 | # or 'master' if not a tracking branch (it's based on a specific rev/hash) |
| 521 | # or it returns None if it couldn't find an upstream |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 522 | if cur_branch is None: |
| 523 | upstream_branch = None |
| 524 | current_type = "detached" |
| 525 | logging.debug("Detached HEAD") |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 526 | else: |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 527 | upstream_branch = scm.GIT.GetUpstreamBranch(self.checkout_path) |
| 528 | if not upstream_branch or not upstream_branch.startswith('refs/remotes'): |
| 529 | current_type = "hash" |
| 530 | logging.debug("Current branch is not tracking an upstream (remote)" |
| 531 | " branch.") |
| 532 | elif upstream_branch.startswith('refs/remotes'): |
| 533 | current_type = "branch" |
| 534 | else: |
| 535 | raise gclient_utils.Error('Invalid Upstream: %s' % upstream_branch) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 536 | |
smut@google.com | d33eab3 | 2014-07-07 19:35:18 +0000 | [diff] [blame] | 537 | if not scm.GIT.IsValidRevision(self.checkout_path, revision, sha_only=True): |
bauerb@chromium.org | cbd20a4 | 2012-06-27 13:49:27 +0000 | [diff] [blame] | 538 | # Update the remotes first so we have all the refs. |
ilevy@chromium.org | a41249c | 2013-07-03 00:09:12 +0000 | [diff] [blame] | 539 | remote_output = scm.GIT.Capture(['remote'] + verbose + ['update'], |
bauerb@chromium.org | cbd20a4 | 2012-06-27 13:49:27 +0000 | [diff] [blame] | 540 | cwd=self.checkout_path) |
bauerb@chromium.org | cbd20a4 | 2012-06-27 13:49:27 +0000 | [diff] [blame] | 541 | if verbose: |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 542 | self.Print(remote_output) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 543 | |
mmoss@chromium.org | 37ac0e3 | 2015-08-18 18:14:38 +0000 | [diff] [blame] | 544 | self._UpdateBranchHeads(options, fetch=True) |
mmoss@chromium.org | e409df6 | 2013-04-16 17:28:57 +0000 | [diff] [blame] | 545 | |
Paweł Hajdan, Jr | 63b8c2a | 2017-09-05 17:59:08 +0200 | [diff] [blame] | 546 | revision = self._AutoFetchRef(options, revision) |
| 547 | |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 548 | # This is a big hammer, debatable if it should even be here... |
davemoore@chromium.org | 793796d | 2010-02-19 17:27:41 +0000 | [diff] [blame] | 549 | if options.force or options.reset: |
iannucci@chromium.org | d4fffee | 2013-06-28 00:35:26 +0000 | [diff] [blame] | 550 | target = 'HEAD' |
| 551 | if options.upstream and upstream_branch: |
| 552 | target = upstream_branch |
Robert Iannucci | c41d8b9 | 2017-02-16 17:07:37 -0800 | [diff] [blame] | 553 | self._Scrub(target, options) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 554 | |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 555 | if current_type == 'detached': |
| 556 | # case 0 |
Robert Iannucci | c41d8b9 | 2017-02-16 17:07:37 -0800 | [diff] [blame] | 557 | # We just did a Scrub, this is as clean as it's going to get. In |
| 558 | # particular if HEAD is a commit that contains two versions of the same |
| 559 | # file on a case-insensitive filesystem (e.g. 'a' and 'A'), there's no way |
| 560 | # to actually "Clean" the checkout; that commit is uncheckoutable on this |
| 561 | # system. The best we can do is carry forward to the checkout step. |
| 562 | if not (options.force or options.reset): |
| 563 | self._CheckClean(revision) |
agable | 83faed0 | 2016-10-24 14:37:10 -0700 | [diff] [blame] | 564 | self._CheckDetachedHead(revision, options) |
smut@google.com | d33eab3 | 2014-07-07 19:35:18 +0000 | [diff] [blame] | 565 | if self._Capture(['rev-list', '-n', '1', 'HEAD']) == revision: |
szager@chromium.org | 848fd49 | 2014-04-09 19:06:44 +0000 | [diff] [blame] | 566 | self.Print('Up-to-date; skipping checkout.') |
| 567 | else: |
vadimsh@chromium.org | 2b7d3ed | 2014-06-20 18:15:37 +0000 | [diff] [blame] | 568 | # 'git checkout' may need to overwrite existing untracked files. Allow |
| 569 | # it only when nuclear options are enabled. |
dnj@chromium.org | bb424c0 | 2014-06-23 22:42:51 +0000 | [diff] [blame] | 570 | self._Checkout( |
| 571 | options, |
| 572 | revision, |
smut@google.com | 34b4e98 | 2016-05-16 19:06:07 +0000 | [diff] [blame] | 573 | force=(options.force and options.delete_unversioned_trees), |
dnj@chromium.org | bb424c0 | 2014-06-23 22:42:51 +0000 | [diff] [blame] | 574 | quiet=True, |
| 575 | ) |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 576 | if not printed_path: |
agable | 83faed0 | 2016-10-24 14:37:10 -0700 | [diff] [blame] | 577 | self.Print('_____ %s at %s' % (self.relpath, revision), timestamp=False) |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 578 | elif current_type == 'hash': |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 579 | # case 1 |
agable | 41e3a6c | 2016-10-20 11:36:56 -0700 | [diff] [blame] | 580 | # Can't find a merge-base since we don't know our upstream. That makes |
| 581 | # this command VERY likely to produce a rebase failure. For now we |
| 582 | # assume origin is our upstream since that's what the old behavior was. |
| 583 | upstream_branch = self.remote |
| 584 | if options.revision or deps_revision: |
| 585 | upstream_branch = revision |
agable | 1a8439a | 2016-10-24 16:36:14 -0700 | [diff] [blame] | 586 | self._AttemptRebase(upstream_branch, file_list, options, |
agable | 41e3a6c | 2016-10-20 11:36:56 -0700 | [diff] [blame] | 587 | printed_path=printed_path, merge=options.merge) |
| 588 | printed_path = True |
smut@google.com | d33eab3 | 2014-07-07 19:35:18 +0000 | [diff] [blame] | 589 | elif rev_type == 'hash': |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 590 | # case 2 |
agable | 1a8439a | 2016-10-24 16:36:14 -0700 | [diff] [blame] | 591 | self._AttemptRebase(upstream_branch, file_list, options, |
smut@google.com | d33eab3 | 2014-07-07 19:35:18 +0000 | [diff] [blame] | 592 | newbase=revision, printed_path=printed_path, |
bauerb@chromium.org | 30c46d6 | 2014-01-23 12:11:56 +0000 | [diff] [blame] | 593 | merge=options.merge) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 594 | printed_path = True |
mmoss@chromium.org | 6e7202b | 2014-09-09 18:23:39 +0000 | [diff] [blame] | 595 | elif remote_ref and ''.join(remote_ref) != upstream_branch: |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 596 | # case 4 |
mmoss@chromium.org | 6e7202b | 2014-09-09 18:23:39 +0000 | [diff] [blame] | 597 | new_base = ''.join(remote_ref) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 598 | if not printed_path: |
agable | 83faed0 | 2016-10-24 14:37:10 -0700 | [diff] [blame] | 599 | self.Print('_____ %s at %s' % (self.relpath, revision), timestamp=False) |
mmoss@chromium.org | 6e7202b | 2014-09-09 18:23:39 +0000 | [diff] [blame] | 600 | switch_error = ("Could not switch upstream branch from %s to %s\n" |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 601 | % (upstream_branch, new_base) + |
mmoss@chromium.org | 6e7202b | 2014-09-09 18:23:39 +0000 | [diff] [blame] | 602 | "Please use --force or merge or rebase manually:\n" + |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 603 | "cd %s; git rebase %s\n" % (self.checkout_path, new_base) + |
| 604 | "OR git checkout -b <some new branch> %s" % new_base) |
mmoss@chromium.org | 6e7202b | 2014-09-09 18:23:39 +0000 | [diff] [blame] | 605 | force_switch = False |
| 606 | if options.force: |
| 607 | try: |
agable | 83faed0 | 2016-10-24 14:37:10 -0700 | [diff] [blame] | 608 | self._CheckClean(revision) |
mmoss@chromium.org | 6e7202b | 2014-09-09 18:23:39 +0000 | [diff] [blame] | 609 | # case 4a |
| 610 | force_switch = True |
| 611 | except gclient_utils.Error as e: |
| 612 | if options.reset: |
| 613 | # case 4b |
| 614 | force_switch = True |
| 615 | else: |
| 616 | switch_error = '%s\n%s' % (e.message, switch_error) |
| 617 | if force_switch: |
| 618 | self.Print("Switching upstream branch from %s to %s" % |
| 619 | (upstream_branch, new_base)) |
| 620 | switch_branch = 'gclient_' + remote_ref[1] |
| 621 | self._Capture(['branch', '-f', switch_branch, new_base]) |
| 622 | self._Checkout(options, switch_branch, force=True, quiet=True) |
| 623 | else: |
| 624 | # case 4c |
| 625 | raise gclient_utils.Error(switch_error) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 626 | else: |
| 627 | # case 3 - the default case |
agable | 1a8439a | 2016-10-24 16:36:14 -0700 | [diff] [blame] | 628 | rebase_files = self._Capture( |
| 629 | ['diff', upstream_branch, '--name-only']).split() |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 630 | if verbose: |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 631 | self.Print('Trying fast-forward merge to branch : %s' % upstream_branch) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 632 | try: |
bauerb@chromium.org | 2aad1b2 | 2011-07-22 12:00:41 +0000 | [diff] [blame] | 633 | merge_args = ['merge'] |
bauerb@chromium.org | 30c46d6 | 2014-01-23 12:11:56 +0000 | [diff] [blame] | 634 | if options.merge: |
| 635 | merge_args.append('--ff') |
| 636 | else: |
bauerb@chromium.org | 2aad1b2 | 2011-07-22 12:00:41 +0000 | [diff] [blame] | 637 | merge_args.append('--ff-only') |
| 638 | merge_args.append(upstream_branch) |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 639 | merge_output = self._Capture(merge_args) |
bratell@opera.com | 18fa454 | 2013-05-21 13:30:46 +0000 | [diff] [blame] | 640 | except subprocess2.CalledProcessError as e: |
agable | 1a8439a | 2016-10-24 16:36:14 -0700 | [diff] [blame] | 641 | rebase_files = [] |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 642 | if re.match('fatal: Not possible to fast-forward, aborting.', e.stderr): |
| 643 | if not printed_path: |
agable | 83faed0 | 2016-10-24 14:37:10 -0700 | [diff] [blame] | 644 | self.Print('_____ %s at %s' % (self.relpath, revision), |
| 645 | timestamp=False) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 646 | printed_path = True |
| 647 | while True: |
dnj@chromium.org | 5b23e87 | 2015-02-20 21:25:57 +0000 | [diff] [blame] | 648 | if not options.auto_rebase: |
| 649 | try: |
| 650 | action = self._AskForData( |
| 651 | 'Cannot %s, attempt to rebase? ' |
| 652 | '(y)es / (q)uit / (s)kip : ' % |
| 653 | ('merge' if options.merge else 'fast-forward merge'), |
| 654 | options) |
| 655 | except ValueError: |
| 656 | raise gclient_utils.Error('Invalid Character') |
| 657 | if options.auto_rebase or re.match(r'yes|y', action, re.I): |
agable | 1a8439a | 2016-10-24 16:36:14 -0700 | [diff] [blame] | 658 | self._AttemptRebase(upstream_branch, rebase_files, options, |
bauerb@chromium.org | 30c46d6 | 2014-01-23 12:11:56 +0000 | [diff] [blame] | 659 | printed_path=printed_path, merge=False) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 660 | printed_path = True |
| 661 | break |
| 662 | elif re.match(r'quit|q', action, re.I): |
| 663 | raise gclient_utils.Error("Can't fast-forward, please merge or " |
| 664 | "rebase manually.\n" |
| 665 | "cd %s && git " % self.checkout_path |
| 666 | + "rebase %s" % upstream_branch) |
| 667 | elif re.match(r'skip|s', action, re.I): |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 668 | self.Print('Skipping %s' % self.relpath) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 669 | return |
| 670 | else: |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 671 | self.Print('Input not recognized') |
smut@google.com | 27c9c8a | 2014-09-11 19:57:55 +0000 | [diff] [blame] | 672 | elif re.match("error: Your local changes to '.*' would be " |
| 673 | "overwritten by merge. Aborting.\nPlease, commit your " |
| 674 | "changes or stash them before you can merge.\n", |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 675 | e.stderr): |
| 676 | if not printed_path: |
agable | 83faed0 | 2016-10-24 14:37:10 -0700 | [diff] [blame] | 677 | self.Print('_____ %s at %s' % (self.relpath, revision), |
| 678 | timestamp=False) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 679 | printed_path = True |
| 680 | raise gclient_utils.Error(e.stderr) |
| 681 | else: |
| 682 | # Some other problem happened with the merge |
| 683 | logging.error("Error during fast-forward merge in %s!" % self.relpath) |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 684 | self.Print(e.stderr) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 685 | raise |
| 686 | else: |
| 687 | # Fast-forward merge was successful |
| 688 | if not re.match('Already up-to-date.', merge_output) or verbose: |
| 689 | if not printed_path: |
agable | 83faed0 | 2016-10-24 14:37:10 -0700 | [diff] [blame] | 690 | self.Print('_____ %s at %s' % (self.relpath, revision), |
| 691 | timestamp=False) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 692 | printed_path = True |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 693 | self.Print(merge_output.strip()) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 694 | if not verbose: |
| 695 | # Make the output a little prettier. It's nice to have some |
| 696 | # whitespace between projects when syncing. |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 697 | self.Print('') |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 698 | |
agable | c3937b9 | 2016-10-25 10:13:03 -0700 | [diff] [blame] | 699 | if file_list is not None: |
| 700 | file_list.extend( |
| 701 | [os.path.join(self.checkout_path, f) for f in rebase_files]) |
msb@chromium.org | 5bde485 | 2009-12-14 16:47:12 +0000 | [diff] [blame] | 702 | |
| 703 | # If the rebase generated a conflict, abort and ask user to fix |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 704 | if self._IsRebasing(): |
agable | 83faed0 | 2016-10-24 14:37:10 -0700 | [diff] [blame] | 705 | raise gclient_utils.Error('\n____ %s at %s\n' |
msb@chromium.org | 5bde485 | 2009-12-14 16:47:12 +0000 | [diff] [blame] | 706 | '\nConflict while rebasing this branch.\n' |
| 707 | 'Fix the conflict and run gclient again.\n' |
| 708 | 'See man git-rebase for details.\n' |
agable | 83faed0 | 2016-10-24 14:37:10 -0700 | [diff] [blame] | 709 | % (self.relpath, revision)) |
msb@chromium.org | 5bde485 | 2009-12-14 16:47:12 +0000 | [diff] [blame] | 710 | |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 711 | if verbose: |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 712 | self.Print('Checked out revision %s' % self.revinfo(options, (), None), |
| 713 | timestamp=False) |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 714 | |
steveblock@chromium.org | 98e6945 | 2012-02-16 16:36:43 +0000 | [diff] [blame] | 715 | # If --reset and --delete_unversioned_trees are specified, remove any |
| 716 | # untracked directories. |
| 717 | if options.reset and options.delete_unversioned_trees: |
| 718 | # GIT.CaptureStatus() uses 'dit diff' to compare to a specific SHA1 (the |
| 719 | # merge-base by default), so doesn't include untracked files. So we use |
| 720 | # 'git ls-files --directory --others --exclude-standard' here directly. |
| 721 | paths = scm.GIT.Capture( |
| 722 | ['ls-files', '--directory', '--others', '--exclude-standard'], |
| 723 | self.checkout_path) |
| 724 | for path in (p for p in paths.splitlines() if p.endswith('/')): |
| 725 | full_path = os.path.join(self.checkout_path, path) |
| 726 | if not os.path.islink(full_path): |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 727 | self.Print('_____ removing unversioned directory %s' % path) |
digit@chromium.org | dc112ac | 2013-04-24 13:00:19 +0000 | [diff] [blame] | 728 | gclient_utils.rmtree(full_path) |
steveblock@chromium.org | 98e6945 | 2012-02-16 16:36:43 +0000 | [diff] [blame] | 729 | |
iannucci@chromium.org | 2702bcd | 2013-09-24 19:10:07 +0000 | [diff] [blame] | 730 | return self._Capture(['rev-parse', '--verify', 'HEAD']) |
| 731 | |
steveblock@chromium.org | 98e6945 | 2012-02-16 16:36:43 +0000 | [diff] [blame] | 732 | |
iannucci@chromium.org | 396e1a6 | 2013-07-03 19:41:04 +0000 | [diff] [blame] | 733 | def revert(self, options, _args, file_list): |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 734 | """Reverts local modifications. |
| 735 | |
| 736 | All reverted files will be appended to file_list. |
| 737 | """ |
maruel@chromium.org | 8469bf9 | 2010-09-03 19:03:15 +0000 | [diff] [blame] | 738 | if not os.path.isdir(self.checkout_path): |
msb@chromium.org | 260c653 | 2009-10-28 03:22:35 +0000 | [diff] [blame] | 739 | # revert won't work if the directory doesn't exist. It needs to |
| 740 | # checkout instead. |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 741 | self.Print('_____ %s is missing, synching instead' % self.relpath) |
msb@chromium.org | 260c653 | 2009-10-28 03:22:35 +0000 | [diff] [blame] | 742 | # Don't reuse the args. |
| 743 | return self.update(options, [], file_list) |
nasser@codeaurora.org | b2b4631 | 2010-04-30 20:58:03 +0000 | [diff] [blame] | 744 | |
| 745 | default_rev = "refs/heads/master" |
iannucci@chromium.org | d4fffee | 2013-06-28 00:35:26 +0000 | [diff] [blame] | 746 | if options.upstream: |
| 747 | if self._GetCurrentBranch(): |
| 748 | upstream_branch = scm.GIT.GetUpstreamBranch(self.checkout_path) |
| 749 | default_rev = upstream_branch or default_rev |
maruel@chromium.org | 6e29d57 | 2010-06-04 17:32:20 +0000 | [diff] [blame] | 750 | _, deps_revision = gclient_utils.SplitUrlRevision(self.url) |
nasser@codeaurora.org | b2b4631 | 2010-04-30 20:58:03 +0000 | [diff] [blame] | 751 | if not deps_revision: |
| 752 | deps_revision = default_rev |
smut@google.com | d33eab3 | 2014-07-07 19:35:18 +0000 | [diff] [blame] | 753 | if deps_revision.startswith('refs/heads/'): |
| 754 | deps_revision = deps_revision.replace('refs/heads/', self.remote + '/') |
smut | ae7ea31 | 2016-07-18 11:59:41 -0700 | [diff] [blame] | 755 | try: |
| 756 | deps_revision = self.GetUsableRev(deps_revision, options) |
| 757 | except NoUsableRevError as e: |
| 758 | # If the DEPS entry's url and hash changed, try to update the origin. |
| 759 | # See also http://crbug.com/520067. |
| 760 | logging.warn( |
| 761 | 'Couldn\'t find usable revision, will retrying to update instead: %s', |
| 762 | e.message) |
| 763 | return self.update(options, [], file_list) |
nasser@codeaurora.org | b2b4631 | 2010-04-30 20:58:03 +0000 | [diff] [blame] | 764 | |
iannucci@chromium.org | 396e1a6 | 2013-07-03 19:41:04 +0000 | [diff] [blame] | 765 | if file_list is not None: |
| 766 | files = self._Capture(['diff', deps_revision, '--name-only']).split() |
| 767 | |
Robert Iannucci | c41d8b9 | 2017-02-16 17:07:37 -0800 | [diff] [blame] | 768 | self._Scrub(deps_revision, options) |
lliabraa@chromium.org | ade83db | 2012-09-27 14:06:49 +0000 | [diff] [blame] | 769 | self._Run(['clean', '-f', '-d'], options) |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 770 | |
iannucci@chromium.org | 396e1a6 | 2013-07-03 19:41:04 +0000 | [diff] [blame] | 771 | if file_list is not None: |
| 772 | file_list.extend([os.path.join(self.checkout_path, f) for f in files]) |
| 773 | |
| 774 | def revinfo(self, _options, _args, _file_list): |
maruel@chromium.org | 6cafa13 | 2010-09-07 14:17:26 +0000 | [diff] [blame] | 775 | """Returns revision""" |
| 776 | return self._Capture(['rev-parse', 'HEAD']) |
msb@chromium.org | 0f28206 | 2009-11-06 20:14:02 +0000 | [diff] [blame] | 777 | |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 778 | def runhooks(self, options, args, file_list): |
| 779 | self.status(options, args, file_list) |
| 780 | |
iannucci@chromium.org | 396e1a6 | 2013-07-03 19:41:04 +0000 | [diff] [blame] | 781 | def status(self, options, _args, file_list): |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 782 | """Display status information.""" |
| 783 | if not os.path.isdir(self.checkout_path): |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 784 | self.Print('________ couldn\'t run status in %s:\n' |
| 785 | 'The directory does not exist.' % self.checkout_path) |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 786 | else: |
raphael.kubo.da.costa | 05c8359 | 2016-08-04 08:32:41 -0700 | [diff] [blame] | 787 | try: |
| 788 | merge_base = [self._Capture(['merge-base', 'HEAD', self.remote])] |
| 789 | except subprocess2.CalledProcessError: |
| 790 | merge_base = [] |
| 791 | self._Run(['diff', '--name-status'] + merge_base, options, |
agable | 41e3a6c | 2016-10-20 11:36:56 -0700 | [diff] [blame] | 792 | stdout=self.out_fh, always=options.verbose) |
iannucci@chromium.org | 396e1a6 | 2013-07-03 19:41:04 +0000 | [diff] [blame] | 793 | if file_list is not None: |
raphael.kubo.da.costa | 05c8359 | 2016-08-04 08:32:41 -0700 | [diff] [blame] | 794 | files = self._Capture(['diff', '--name-only'] + merge_base).split() |
iannucci@chromium.org | 396e1a6 | 2013-07-03 19:41:04 +0000 | [diff] [blame] | 795 | file_list.extend([os.path.join(self.checkout_path, f) for f in files]) |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 796 | |
smut | ae7ea31 | 2016-07-18 11:59:41 -0700 | [diff] [blame] | 797 | def GetUsableRev(self, rev, options): |
agable | 41e3a6c | 2016-10-20 11:36:56 -0700 | [diff] [blame] | 798 | """Finds a useful revision for this repository.""" |
smut | ae7ea31 | 2016-07-18 11:59:41 -0700 | [diff] [blame] | 799 | sha1 = None |
| 800 | if not os.path.isdir(self.checkout_path): |
| 801 | raise NoUsableRevError( |
agable | a98a6cd | 2016-11-15 14:30:10 -0800 | [diff] [blame] | 802 | 'This is not a git repo, so we cannot get a usable rev.') |
agable | 41e3a6c | 2016-10-20 11:36:56 -0700 | [diff] [blame] | 803 | |
| 804 | if scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=rev): |
| 805 | sha1 = rev |
smut | ae7ea31 | 2016-07-18 11:59:41 -0700 | [diff] [blame] | 806 | else: |
agable | 41e3a6c | 2016-10-20 11:36:56 -0700 | [diff] [blame] | 807 | # May exist in origin, but we don't have it yet, so fetch and look |
| 808 | # again. |
| 809 | self._Fetch(options) |
smut | ae7ea31 | 2016-07-18 11:59:41 -0700 | [diff] [blame] | 810 | if scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=rev): |
| 811 | sha1 = rev |
smut | ae7ea31 | 2016-07-18 11:59:41 -0700 | [diff] [blame] | 812 | |
| 813 | if not sha1: |
| 814 | raise NoUsableRevError( |
agable | a98a6cd | 2016-11-15 14:30:10 -0800 | [diff] [blame] | 815 | 'Hash %s does not appear to be a valid hash in this repo.' % rev) |
smut | ae7ea31 | 2016-07-18 11:59:41 -0700 | [diff] [blame] | 816 | |
| 817 | return sha1 |
| 818 | |
msb@chromium.org | e6f7835 | 2010-01-13 17:05:33 +0000 | [diff] [blame] | 819 | def FullUrlForRelativeUrl(self, url): |
| 820 | # Strip from last '/' |
| 821 | # Equivalent to unix basename |
| 822 | base_url = self.url |
| 823 | return base_url[:base_url.rfind('/')] + url |
| 824 | |
primiano@chromium.org | 1c12738 | 2015-02-17 11:15:40 +0000 | [diff] [blame] | 825 | def GetGitBackupDirPath(self): |
| 826 | """Returns the path where the .git folder for the current project can be |
| 827 | staged/restored. Use case: subproject moved from DEPS <-> outer project.""" |
| 828 | return os.path.join(self._root_dir, |
| 829 | 'old_' + self.relpath.replace(os.sep, '_')) + '.git' |
| 830 | |
szager@chromium.org | b0a13a2 | 2014-06-18 00:52:25 +0000 | [diff] [blame] | 831 | def _GetMirror(self, url, options): |
| 832 | """Get a git_cache.Mirror object for the argument url.""" |
| 833 | if not git_cache.Mirror.GetCachePath(): |
| 834 | return None |
hinoka@google.com | b1b5457 | 2014-04-16 22:29:23 +0000 | [diff] [blame] | 835 | mirror_kwargs = { |
| 836 | 'print_func': self.filter, |
| 837 | 'refs': [] |
| 838 | } |
hinoka@google.com | b1b5457 | 2014-04-16 22:29:23 +0000 | [diff] [blame] | 839 | if hasattr(options, 'with_branch_heads') and options.with_branch_heads: |
| 840 | mirror_kwargs['refs'].append('refs/branch-heads/*') |
szager@chromium.org | 8d3348f | 2014-08-19 22:49:16 +0000 | [diff] [blame] | 841 | if hasattr(options, 'with_tags') and options.with_tags: |
| 842 | mirror_kwargs['refs'].append('refs/tags/*') |
szager@chromium.org | b0a13a2 | 2014-06-18 00:52:25 +0000 | [diff] [blame] | 843 | return git_cache.Mirror(url, **mirror_kwargs) |
| 844 | |
| 845 | @staticmethod |
| 846 | def _UpdateMirror(mirror, options): |
| 847 | """Update a git mirror by fetching the latest commits from the remote.""" |
szager@chromium.org | 3ec84f6 | 2014-08-22 21:00:22 +0000 | [diff] [blame] | 848 | if getattr(options, 'shallow', False): |
hinoka@chromium.org | 46b8741 | 2014-05-15 00:42:05 +0000 | [diff] [blame] | 849 | # HACK(hinoka): These repositories should be super shallow. |
szager@chromium.org | b0a13a2 | 2014-06-18 00:52:25 +0000 | [diff] [blame] | 850 | if 'flash' in mirror.url: |
hinoka@chromium.org | 46b8741 | 2014-05-15 00:42:05 +0000 | [diff] [blame] | 851 | depth = 10 |
| 852 | else: |
| 853 | depth = 10000 |
| 854 | else: |
| 855 | depth = None |
e.hakkinen@samsung.com | e8bc1aa | 2015-04-08 08:00:37 +0000 | [diff] [blame] | 856 | mirror.populate(verbose=options.verbose, |
| 857 | bootstrap=not getattr(options, 'no_bootstrap', False), |
Vadim Shtayura | 08049e2 | 2017-10-11 00:14:52 +0000 | [diff] [blame] | 858 | depth=depth, |
| 859 | ignore_lock=getattr(options, 'ignore_locks', False), |
| 860 | lock_timeout=getattr(options, 'lock_timeout', 0)) |
| 861 | mirror.unlock() |
iannucci@chromium.org | 53456aa | 2013-07-03 19:38:34 +0000 | [diff] [blame] | 862 | |
maruel@chromium.org | f5d37bf | 2010-09-02 00:50:34 +0000 | [diff] [blame] | 863 | def _Clone(self, revision, url, options): |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 864 | """Clone a git repository from the given URL. |
| 865 | |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 866 | Once we've cloned the repo, we checkout a working branch if the specified |
| 867 | revision is a branch head. If it is a tag or a specific commit, then we |
| 868 | leave HEAD detached as it makes future updates simpler -- in this case the |
| 869 | user should first create a new branch or switch to an existing branch before |
| 870 | making changes in the repo.""" |
maruel@chromium.org | f5d37bf | 2010-09-02 00:50:34 +0000 | [diff] [blame] | 871 | if not options.verbose: |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 872 | # git clone doesn't seem to insert a newline properly before printing |
| 873 | # to stdout |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 874 | self.Print('') |
szager@chromium.org | f1d73eb | 2014-04-21 17:07:04 +0000 | [diff] [blame] | 875 | cfg = gclient_utils.DefaultIndexPackConfig(url) |
szager@chromium.org | 8a13970 | 2014-06-20 15:55:01 +0000 | [diff] [blame] | 876 | clone_cmd = cfg + ['clone', '--no-checkout', '--progress'] |
iannucci@chromium.org | 53456aa | 2013-07-03 19:38:34 +0000 | [diff] [blame] | 877 | if self.cache_dir: |
| 878 | clone_cmd.append('--shared') |
maruel@chromium.org | f5d37bf | 2010-09-02 00:50:34 +0000 | [diff] [blame] | 879 | if options.verbose: |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 880 | clone_cmd.append('--verbose') |
ilevy@chromium.org | 3534aa5 | 2013-07-20 01:58:08 +0000 | [diff] [blame] | 881 | clone_cmd.append(url) |
nsylvain@chromium.org | 328c3c7 | 2011-06-01 20:50:27 +0000 | [diff] [blame] | 882 | # If the parent directory does not exist, Git clone on Windows will not |
| 883 | # create it, so we need to do it manually. |
| 884 | parent_dir = os.path.dirname(self.checkout_path) |
ilevy@chromium.org | 3534aa5 | 2013-07-20 01:58:08 +0000 | [diff] [blame] | 885 | gclient_utils.safe_makedirs(parent_dir) |
primiano@chromium.org | 5439ea5 | 2014-08-06 17:18:18 +0000 | [diff] [blame] | 886 | |
| 887 | template_dir = None |
| 888 | if hasattr(options, 'no_history') and options.no_history: |
| 889 | if gclient_utils.IsGitSha(revision): |
| 890 | # In the case of a subproject, the pinned sha is not necessarily the |
| 891 | # head of the remote branch (so we can't just use --depth=N). Instead, |
| 892 | # we tell git to fetch all the remote objects from SHA..HEAD by means of |
| 893 | # a template git dir which has a 'shallow' file pointing to the sha. |
| 894 | template_dir = tempfile.mkdtemp( |
| 895 | prefix='_gclient_gittmp_%s' % os.path.basename(self.checkout_path), |
| 896 | dir=parent_dir) |
| 897 | self._Run(['init', '--bare', template_dir], options, cwd=self._root_dir) |
| 898 | with open(os.path.join(template_dir, 'shallow'), 'w') as template_file: |
| 899 | template_file.write(revision) |
| 900 | clone_cmd.append('--template=' + template_dir) |
| 901 | else: |
| 902 | # Otherwise, we're just interested in the HEAD. Just use --depth. |
| 903 | clone_cmd.append('--depth=1') |
| 904 | |
ilevy@chromium.org | 3534aa5 | 2013-07-20 01:58:08 +0000 | [diff] [blame] | 905 | tmp_dir = tempfile.mkdtemp( |
| 906 | prefix='_gclient_%s_' % os.path.basename(self.checkout_path), |
| 907 | dir=parent_dir) |
| 908 | try: |
| 909 | clone_cmd.append(tmp_dir) |
agable@chromium.org | fd5b638 | 2013-10-25 20:54:34 +0000 | [diff] [blame] | 910 | self._Run(clone_cmd, options, cwd=self._root_dir, retry=True) |
ilevy@chromium.org | 3534aa5 | 2013-07-20 01:58:08 +0000 | [diff] [blame] | 911 | gclient_utils.safe_makedirs(self.checkout_path) |
cyrille@nnamrak.org | ef509e4 | 2013-09-20 13:19:08 +0000 | [diff] [blame] | 912 | gclient_utils.safe_rename(os.path.join(tmp_dir, '.git'), |
| 913 | os.path.join(self.checkout_path, '.git')) |
zty@chromium.org | 6279e8a | 2014-02-13 01:45:25 +0000 | [diff] [blame] | 914 | except: |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 915 | traceback.print_exc(file=self.out_fh) |
zty@chromium.org | 6279e8a | 2014-02-13 01:45:25 +0000 | [diff] [blame] | 916 | raise |
ilevy@chromium.org | 3534aa5 | 2013-07-20 01:58:08 +0000 | [diff] [blame] | 917 | finally: |
| 918 | if os.listdir(tmp_dir): |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 919 | self.Print('_____ removing non-empty tmp dir %s' % tmp_dir) |
ilevy@chromium.org | 3534aa5 | 2013-07-20 01:58:08 +0000 | [diff] [blame] | 920 | gclient_utils.rmtree(tmp_dir) |
primiano@chromium.org | 5439ea5 | 2014-08-06 17:18:18 +0000 | [diff] [blame] | 921 | if template_dir: |
| 922 | gclient_utils.rmtree(template_dir) |
mmoss@chromium.org | 1a6bec0 | 2014-06-02 21:53:29 +0000 | [diff] [blame] | 923 | self._UpdateBranchHeads(options, fetch=True) |
Paweł Hajdan, Jr | 63b8c2a | 2017-09-05 17:59:08 +0200 | [diff] [blame] | 924 | revision = self._AutoFetchRef(options, revision) |
mmoss@chromium.org | 6e7202b | 2014-09-09 18:23:39 +0000 | [diff] [blame] | 925 | remote_ref = scm.GIT.RefToRemoteRef(revision, self.remote) |
| 926 | self._Checkout(options, ''.join(remote_ref or revision), quiet=True) |
romain.pokrzywka@gmail.com | 483a0ba | 2014-05-30 00:06:07 +0000 | [diff] [blame] | 927 | if self._GetCurrentBranch() is None: |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 928 | # Squelch git's very verbose detached HEAD warning and use our own |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 929 | self.Print( |
smut@google.com | d33eab3 | 2014-07-07 19:35:18 +0000 | [diff] [blame] | 930 | ('Checked out %s to a detached HEAD. Before making any commits\n' |
| 931 | 'in this repo, you should use \'git checkout <branch>\' to switch to\n' |
| 932 | 'an existing branch or use \'git checkout %s -b <branch>\' to\n' |
| 933 | 'create a new branch for your work.') % (revision, self.remote)) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 934 | |
szager@chromium.org | 6cd41b6 | 2014-04-21 23:55:22 +0000 | [diff] [blame] | 935 | def _AskForData(self, prompt, options): |
bauerb@chromium.org | 30c46d6 | 2014-01-23 12:11:56 +0000 | [diff] [blame] | 936 | if options.jobs > 1: |
szager@chromium.org | 6cd41b6 | 2014-04-21 23:55:22 +0000 | [diff] [blame] | 937 | self.Print(prompt) |
bauerb@chromium.org | 30c46d6 | 2014-01-23 12:11:56 +0000 | [diff] [blame] | 938 | raise gclient_utils.Error("Background task requires input. Rerun " |
| 939 | "gclient with --jobs=1 so that\n" |
| 940 | "interaction is possible.") |
| 941 | try: |
| 942 | return raw_input(prompt) |
| 943 | except KeyboardInterrupt: |
| 944 | # Hide the exception. |
| 945 | sys.exit(1) |
| 946 | |
| 947 | |
maruel@chromium.org | f5d37bf | 2010-09-02 00:50:34 +0000 | [diff] [blame] | 948 | def _AttemptRebase(self, upstream, files, options, newbase=None, |
bauerb@chromium.org | 30c46d6 | 2014-01-23 12:11:56 +0000 | [diff] [blame] | 949 | branch=None, printed_path=False, merge=False): |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 950 | """Attempt to rebase onto either upstream or, if specified, newbase.""" |
iannucci@chromium.org | 396e1a6 | 2013-07-03 19:41:04 +0000 | [diff] [blame] | 951 | if files is not None: |
| 952 | files.extend(self._Capture(['diff', upstream, '--name-only']).split()) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 953 | revision = upstream |
| 954 | if newbase: |
| 955 | revision = newbase |
bauerb@chromium.org | 30c46d6 | 2014-01-23 12:11:56 +0000 | [diff] [blame] | 956 | action = 'merge' if merge else 'rebase' |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 957 | if not printed_path: |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 958 | self.Print('_____ %s : Attempting %s onto %s...' % ( |
bauerb@chromium.org | 30c46d6 | 2014-01-23 12:11:56 +0000 | [diff] [blame] | 959 | self.relpath, action, revision)) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 960 | printed_path = True |
| 961 | else: |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 962 | self.Print('Attempting %s onto %s...' % (action, revision)) |
bauerb@chromium.org | 30c46d6 | 2014-01-23 12:11:56 +0000 | [diff] [blame] | 963 | |
| 964 | if merge: |
| 965 | merge_output = self._Capture(['merge', revision]) |
| 966 | if options.verbose: |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 967 | self.Print(merge_output) |
bauerb@chromium.org | 30c46d6 | 2014-01-23 12:11:56 +0000 | [diff] [blame] | 968 | return |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 969 | |
| 970 | # Build the rebase command here using the args |
| 971 | # git rebase [options] [--onto <newbase>] <upstream> [<branch>] |
| 972 | rebase_cmd = ['rebase'] |
maruel@chromium.org | f5d37bf | 2010-09-02 00:50:34 +0000 | [diff] [blame] | 973 | if options.verbose: |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 974 | rebase_cmd.append('--verbose') |
| 975 | if newbase: |
| 976 | rebase_cmd.extend(['--onto', newbase]) |
| 977 | rebase_cmd.append(upstream) |
| 978 | if branch: |
| 979 | rebase_cmd.append(branch) |
| 980 | |
| 981 | try: |
maruel@chromium.org | ad80e3b | 2010-09-09 14:18:28 +0000 | [diff] [blame] | 982 | rebase_output = scm.GIT.Capture(rebase_cmd, cwd=self.checkout_path) |
maruel@chromium.org | bffad37 | 2011-09-08 17:54:22 +0000 | [diff] [blame] | 983 | except subprocess2.CalledProcessError, e: |
maruel@chromium.org | ad80e3b | 2010-09-09 14:18:28 +0000 | [diff] [blame] | 984 | if (re.match(r'cannot rebase: you have unstaged changes', e.stderr) or |
| 985 | re.match(r'cannot rebase: your index contains uncommitted changes', |
| 986 | e.stderr)): |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 987 | while True: |
bauerb@chromium.org | 30c46d6 | 2014-01-23 12:11:56 +0000 | [diff] [blame] | 988 | rebase_action = self._AskForData( |
maruel@chromium.org | 9054173 | 2011-04-01 17:54:18 +0000 | [diff] [blame] | 989 | 'Cannot rebase because of unstaged changes.\n' |
| 990 | '\'git reset --hard HEAD\' ?\n' |
| 991 | 'WARNING: destroys any uncommitted work in your current branch!' |
bratell@opera.com | 18fa454 | 2013-05-21 13:30:46 +0000 | [diff] [blame] | 992 | ' (y)es / (q)uit / (s)how : ', options) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 993 | if re.match(r'yes|y', rebase_action, re.I): |
Robert Iannucci | c41d8b9 | 2017-02-16 17:07:37 -0800 | [diff] [blame] | 994 | self._Scrub('HEAD', options) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 995 | # Should this be recursive? |
maruel@chromium.org | ad80e3b | 2010-09-09 14:18:28 +0000 | [diff] [blame] | 996 | rebase_output = scm.GIT.Capture(rebase_cmd, cwd=self.checkout_path) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 997 | break |
| 998 | elif re.match(r'quit|q', rebase_action, re.I): |
| 999 | raise gclient_utils.Error("Please merge or rebase manually\n" |
| 1000 | "cd %s && git " % self.checkout_path |
| 1001 | + "%s" % ' '.join(rebase_cmd)) |
| 1002 | elif re.match(r'show|s', rebase_action, re.I): |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 1003 | self.Print('%s' % e.stderr.strip()) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 1004 | continue |
| 1005 | else: |
| 1006 | gclient_utils.Error("Input not recognized") |
| 1007 | continue |
| 1008 | elif re.search(r'^CONFLICT', e.stdout, re.M): |
| 1009 | raise gclient_utils.Error("Conflict while rebasing this branch.\n" |
| 1010 | "Fix the conflict and run gclient again.\n" |
| 1011 | "See 'man git-rebase' for details.\n") |
| 1012 | else: |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 1013 | self.Print(e.stdout.strip()) |
| 1014 | self.Print('Rebase produced error output:\n%s' % e.stderr.strip()) |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 1015 | raise gclient_utils.Error("Unrecognized error, please merge or rebase " |
| 1016 | "manually.\ncd %s && git " % |
| 1017 | self.checkout_path |
| 1018 | + "%s" % ' '.join(rebase_cmd)) |
| 1019 | |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 1020 | self.Print(rebase_output.strip()) |
maruel@chromium.org | f5d37bf | 2010-09-02 00:50:34 +0000 | [diff] [blame] | 1021 | if not options.verbose: |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 1022 | # Make the output a little prettier. It's nice to have some |
| 1023 | # whitespace between projects when syncing. |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 1024 | self.Print('') |
nasser@codeaurora.org | d90ba3f | 2010-02-23 14:42:57 +0000 | [diff] [blame] | 1025 | |
maruel@chromium.org | 6e29d57 | 2010-06-04 17:32:20 +0000 | [diff] [blame] | 1026 | @staticmethod |
| 1027 | def _CheckMinVersion(min_version): |
maruel@chromium.org | d0f854a | 2010-03-11 19:35:53 +0000 | [diff] [blame] | 1028 | (ok, current_version) = scm.GIT.AssertVersion(min_version) |
| 1029 | if not ok: |
| 1030 | raise gclient_utils.Error('git version %s < minimum required %s' % |
| 1031 | (current_version, min_version)) |
msb@chromium.org | 923a037 | 2009-12-11 20:42:43 +0000 | [diff] [blame] | 1032 | |
tandrii@chromium.org | c438c14 | 2015-08-24 22:55:55 +0000 | [diff] [blame] | 1033 | def _EnsureValidHeadObjectOrCheckout(self, revision, options, url): |
| 1034 | # Special case handling if all 3 conditions are met: |
| 1035 | # * the mirros have recently changed, but deps destination remains same, |
| 1036 | # * the git histories of mirrors are conflicting. |
| 1037 | # * git cache is used |
| 1038 | # This manifests itself in current checkout having invalid HEAD commit on |
| 1039 | # most git operations. Since git cache is used, just deleted the .git |
| 1040 | # folder, and re-create it by cloning. |
| 1041 | try: |
| 1042 | self._Capture(['rev-list', '-n', '1', 'HEAD']) |
| 1043 | except subprocess2.CalledProcessError as e: |
| 1044 | if ('fatal: bad object HEAD' in e.stderr |
| 1045 | and self.cache_dir and self.cache_dir in url): |
| 1046 | self.Print(( |
| 1047 | 'Likely due to DEPS change with git cache_dir, ' |
| 1048 | 'the current commit points to no longer existing object.\n' |
| 1049 | '%s' % e) |
| 1050 | ) |
| 1051 | self._DeleteOrMove(options.force) |
| 1052 | self._Clone(revision, url, options) |
| 1053 | else: |
| 1054 | raise |
| 1055 | |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 1056 | def _IsRebasing(self): |
| 1057 | # Check for any of REBASE-i/REBASE-m/REBASE/AM. Unfortunately git doesn't |
| 1058 | # have a plumbing command to determine whether a rebase is in progress, so |
| 1059 | # for now emualate (more-or-less) git-rebase.sh / git-completion.bash |
| 1060 | g = os.path.join(self.checkout_path, '.git') |
| 1061 | return ( |
| 1062 | os.path.isdir(os.path.join(g, "rebase-merge")) or |
| 1063 | os.path.isdir(os.path.join(g, "rebase-apply"))) |
| 1064 | |
Robert Iannucci | c41d8b9 | 2017-02-16 17:07:37 -0800 | [diff] [blame] | 1065 | def _CheckClean(self, revision, fixup=False): |
iannucci@chromium.org | d9b318c | 2015-12-04 20:03:08 +0000 | [diff] [blame] | 1066 | lockfile = os.path.join(self.checkout_path, ".git", "index.lock") |
| 1067 | if os.path.exists(lockfile): |
| 1068 | raise gclient_utils.Error( |
agable | 83faed0 | 2016-10-24 14:37:10 -0700 | [diff] [blame] | 1069 | '\n____ %s at %s\n' |
iannucci@chromium.org | d9b318c | 2015-12-04 20:03:08 +0000 | [diff] [blame] | 1070 | '\tYour repo is locked, possibly due to a concurrent git process.\n' |
| 1071 | '\tIf no git executable is running, then clean up %r and try again.\n' |
agable | 83faed0 | 2016-10-24 14:37:10 -0700 | [diff] [blame] | 1072 | % (self.relpath, revision, lockfile)) |
iannucci@chromium.org | d9b318c | 2015-12-04 20:03:08 +0000 | [diff] [blame] | 1073 | |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 1074 | # Make sure the tree is clean; see git-rebase.sh for reference |
| 1075 | try: |
| 1076 | scm.GIT.Capture(['update-index', '--ignore-submodules', '--refresh'], |
maruel@chromium.org | ad80e3b | 2010-09-09 14:18:28 +0000 | [diff] [blame] | 1077 | cwd=self.checkout_path) |
maruel@chromium.org | bffad37 | 2011-09-08 17:54:22 +0000 | [diff] [blame] | 1078 | except subprocess2.CalledProcessError: |
agable | 83faed0 | 2016-10-24 14:37:10 -0700 | [diff] [blame] | 1079 | raise gclient_utils.Error('\n____ %s at %s\n' |
maruel@chromium.org | 6e29d57 | 2010-06-04 17:32:20 +0000 | [diff] [blame] | 1080 | '\tYou have unstaged changes.\n' |
| 1081 | '\tPlease commit, stash, or reset.\n' |
agable | 83faed0 | 2016-10-24 14:37:10 -0700 | [diff] [blame] | 1082 | % (self.relpath, revision)) |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 1083 | try: |
| 1084 | scm.GIT.Capture(['diff-index', '--cached', '--name-status', '-r', |
smut@google.com | 27c9c8a | 2014-09-11 19:57:55 +0000 | [diff] [blame] | 1085 | '--ignore-submodules', 'HEAD', '--'], |
maruel@chromium.org | ad80e3b | 2010-09-09 14:18:28 +0000 | [diff] [blame] | 1086 | cwd=self.checkout_path) |
maruel@chromium.org | bffad37 | 2011-09-08 17:54:22 +0000 | [diff] [blame] | 1087 | except subprocess2.CalledProcessError: |
agable | 83faed0 | 2016-10-24 14:37:10 -0700 | [diff] [blame] | 1088 | raise gclient_utils.Error('\n____ %s at %s\n' |
maruel@chromium.org | 6e29d57 | 2010-06-04 17:32:20 +0000 | [diff] [blame] | 1089 | '\tYour index contains uncommitted changes\n' |
| 1090 | '\tPlease commit, stash, or reset.\n' |
agable | 83faed0 | 2016-10-24 14:37:10 -0700 | [diff] [blame] | 1091 | % (self.relpath, revision)) |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 1092 | |
agable | 83faed0 | 2016-10-24 14:37:10 -0700 | [diff] [blame] | 1093 | def _CheckDetachedHead(self, revision, _options): |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 1094 | # HEAD is detached. Make sure it is safe to move away from (i.e., it is |
| 1095 | # reference by a commit). If not, error out -- most likely a rebase is |
| 1096 | # in progress, try to detect so we can give a better error. |
| 1097 | try: |
maruel@chromium.org | ad80e3b | 2010-09-09 14:18:28 +0000 | [diff] [blame] | 1098 | scm.GIT.Capture(['name-rev', '--no-undefined', 'HEAD'], |
| 1099 | cwd=self.checkout_path) |
maruel@chromium.org | bffad37 | 2011-09-08 17:54:22 +0000 | [diff] [blame] | 1100 | except subprocess2.CalledProcessError: |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 1101 | # Commit is not contained by any rev. See if the user is rebasing: |
| 1102 | if self._IsRebasing(): |
| 1103 | # Punt to the user |
agable | 83faed0 | 2016-10-24 14:37:10 -0700 | [diff] [blame] | 1104 | raise gclient_utils.Error('\n____ %s at %s\n' |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 1105 | '\tAlready in a conflict, i.e. (no branch).\n' |
| 1106 | '\tFix the conflict and run gclient again.\n' |
| 1107 | '\tOr to abort run:\n\t\tgit-rebase --abort\n' |
| 1108 | '\tSee man git-rebase for details.\n' |
agable | 83faed0 | 2016-10-24 14:37:10 -0700 | [diff] [blame] | 1109 | % (self.relpath, revision)) |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 1110 | # Let's just save off the commit so we can proceed. |
maruel@chromium.org | 6cafa13 | 2010-09-07 14:17:26 +0000 | [diff] [blame] | 1111 | name = ('saved-by-gclient-' + |
| 1112 | self._Capture(['rev-parse', '--short', 'HEAD'])) |
mmoss@chromium.org | 77bd736 | 2013-09-25 23:46:14 +0000 | [diff] [blame] | 1113 | self._Capture(['branch', '-f', name]) |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 1114 | self.Print('_____ found an unreferenced commit and saved it as \'%s\'' % |
maruel@chromium.org | f5d37bf | 2010-09-02 00:50:34 +0000 | [diff] [blame] | 1115 | name) |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 1116 | |
msb@chromium.org | 5bde485 | 2009-12-14 16:47:12 +0000 | [diff] [blame] | 1117 | def _GetCurrentBranch(self): |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 1118 | # Returns name of current branch or None for detached HEAD |
maruel@chromium.org | 6cafa13 | 2010-09-07 14:17:26 +0000 | [diff] [blame] | 1119 | branch = self._Capture(['rev-parse', '--abbrev-ref=strict', 'HEAD']) |
msb@chromium.org | 786fb68 | 2010-06-02 15:16:23 +0000 | [diff] [blame] | 1120 | if branch == 'HEAD': |
msb@chromium.org | 5bde485 | 2009-12-14 16:47:12 +0000 | [diff] [blame] | 1121 | return None |
| 1122 | return branch |
| 1123 | |
borenet@google.com | c3e09d2 | 2014-04-10 13:58:18 +0000 | [diff] [blame] | 1124 | def _Capture(self, args, **kwargs): |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 1125 | kwargs.setdefault('cwd', self.checkout_path) |
| 1126 | kwargs.setdefault('stderr', subprocess2.PIPE) |
Robert Iannucci | a7a9ceb | 2017-02-16 17:38:06 -0800 | [diff] [blame] | 1127 | strip = kwargs.pop('strip', True) |
szager@chromium.org | 6d8115d | 2014-04-23 20:59:23 +0000 | [diff] [blame] | 1128 | env = scm.GIT.ApplyEnvVars(kwargs) |
Robert Iannucci | a7a9ceb | 2017-02-16 17:38:06 -0800 | [diff] [blame] | 1129 | ret = subprocess2.check_output(['git'] + args, env=env, **kwargs) |
| 1130 | if strip: |
| 1131 | ret = ret.strip() |
| 1132 | return ret |
maruel@chromium.org | 6cafa13 | 2010-09-07 14:17:26 +0000 | [diff] [blame] | 1133 | |
dnj@chromium.org | bb424c0 | 2014-06-23 22:42:51 +0000 | [diff] [blame] | 1134 | def _Checkout(self, options, ref, force=False, quiet=None): |
| 1135 | """Performs a 'git-checkout' operation. |
| 1136 | |
| 1137 | Args: |
| 1138 | options: The configured option set |
| 1139 | ref: (str) The branch/commit to checkout |
| 1140 | quiet: (bool/None) Whether or not the checkout shoud pass '--quiet'; if |
| 1141 | 'None', the behavior is inferred from 'options.verbose'. |
| 1142 | Returns: (str) The output of the checkout operation |
| 1143 | """ |
| 1144 | if quiet is None: |
| 1145 | quiet = (not options.verbose) |
| 1146 | checkout_args = ['checkout'] |
| 1147 | if force: |
| 1148 | checkout_args.append('--force') |
| 1149 | if quiet: |
| 1150 | checkout_args.append('--quiet') |
| 1151 | checkout_args.append(ref) |
| 1152 | return self._Capture(checkout_args) |
| 1153 | |
Paweł Hajdan, Jr | 63b8c2a | 2017-09-05 17:59:08 +0200 | [diff] [blame] | 1154 | def _Fetch(self, options, remote=None, prune=False, quiet=False, |
| 1155 | refspec=None): |
dnj@chromium.org | 680f217 | 2014-06-25 00:39:32 +0000 | [diff] [blame] | 1156 | cfg = gclient_utils.DefaultIndexPackConfig(self.url) |
| 1157 | fetch_cmd = cfg + [ |
| 1158 | 'fetch', |
| 1159 | remote or self.remote, |
| 1160 | ] |
Paweł Hajdan, Jr | 63b8c2a | 2017-09-05 17:59:08 +0200 | [diff] [blame] | 1161 | if refspec: |
| 1162 | fetch_cmd.append(refspec) |
dnj@chromium.org | 680f217 | 2014-06-25 00:39:32 +0000 | [diff] [blame] | 1163 | |
| 1164 | if prune: |
| 1165 | fetch_cmd.append('--prune') |
| 1166 | if options.verbose: |
| 1167 | fetch_cmd.append('--verbose') |
| 1168 | elif quiet: |
| 1169 | fetch_cmd.append('--quiet') |
tandrii | 64103db | 2016-10-11 05:30:05 -0700 | [diff] [blame] | 1170 | self._Run(fetch_cmd, options, show_header=options.verbose, retry=True) |
dnj@chromium.org | 680f217 | 2014-06-25 00:39:32 +0000 | [diff] [blame] | 1171 | |
| 1172 | # Return the revision that was fetched; this will be stored in 'FETCH_HEAD' |
| 1173 | return self._Capture(['rev-parse', '--verify', 'FETCH_HEAD']) |
| 1174 | |
mmoss@chromium.org | e409df6 | 2013-04-16 17:28:57 +0000 | [diff] [blame] | 1175 | def _UpdateBranchHeads(self, options, fetch=False): |
szager@chromium.org | 8d3348f | 2014-08-19 22:49:16 +0000 | [diff] [blame] | 1176 | """Adds, and optionally fetches, "branch-heads" and "tags" refspecs |
| 1177 | if requested.""" |
| 1178 | need_fetch = fetch |
mmoss@chromium.org | e409df6 | 2013-04-16 17:28:57 +0000 | [diff] [blame] | 1179 | if hasattr(options, 'with_branch_heads') and options.with_branch_heads: |
maruel@chromium.org | 1a60dca | 2013-11-26 14:06:26 +0000 | [diff] [blame] | 1180 | config_cmd = ['config', 'remote.%s.fetch' % self.remote, |
szager@chromium.org | f2d7d6b | 2013-10-17 20:41:43 +0000 | [diff] [blame] | 1181 | '+refs/branch-heads/*:refs/remotes/branch-heads/*', |
| 1182 | '^\\+refs/branch-heads/\\*:.*$'] |
| 1183 | self._Run(config_cmd, options) |
szager@chromium.org | 8d3348f | 2014-08-19 22:49:16 +0000 | [diff] [blame] | 1184 | need_fetch = True |
| 1185 | if hasattr(options, 'with_tags') and options.with_tags: |
| 1186 | config_cmd = ['config', 'remote.%s.fetch' % self.remote, |
| 1187 | '+refs/tags/*:refs/tags/*', |
| 1188 | '^\\+refs/tags/\\*:.*$'] |
| 1189 | self._Run(config_cmd, options) |
| 1190 | need_fetch = True |
| 1191 | if fetch and need_fetch: |
bpastene | 2a3e991 | 2016-09-07 16:22:25 -0700 | [diff] [blame] | 1192 | self._Fetch(options, prune=options.force) |
mmoss@chromium.org | e409df6 | 2013-04-16 17:28:57 +0000 | [diff] [blame] | 1193 | |
Paweł Hajdan, Jr | 63b8c2a | 2017-09-05 17:59:08 +0200 | [diff] [blame] | 1194 | def _AutoFetchRef(self, options, revision): |
| 1195 | """Attempts to fetch |revision| if not available in local repo. |
| 1196 | |
| 1197 | Returns possibly updated revision.""" |
| 1198 | try: |
| 1199 | self._Capture(['rev-parse', revision]) |
| 1200 | except subprocess2.CalledProcessError: |
| 1201 | self._Fetch(options, refspec=revision) |
| 1202 | revision = self._Capture(['rev-parse', 'FETCH_HEAD']) |
| 1203 | return revision |
| 1204 | |
dnj@chromium.org | 680f217 | 2014-06-25 00:39:32 +0000 | [diff] [blame] | 1205 | def _Run(self, args, options, show_header=True, **kwargs): |
Quinten Yearsley | b2cc4a9 | 2016-12-15 13:53:26 -0800 | [diff] [blame] | 1206 | # Disable 'unused options' warning | pylint: disable=unused-argument |
sbc@chromium.org | 2cd0b8e | 2014-09-22 21:17:59 +0000 | [diff] [blame] | 1207 | kwargs.setdefault('cwd', self.checkout_path) |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 1208 | kwargs.setdefault('stdout', self.out_fh) |
szager@chromium.org | 848fd49 | 2014-04-09 19:06:44 +0000 | [diff] [blame] | 1209 | kwargs['filter_fn'] = self.filter |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 1210 | kwargs.setdefault('print_stdout', False) |
szager@chromium.org | 6d8115d | 2014-04-23 20:59:23 +0000 | [diff] [blame] | 1211 | env = scm.GIT.ApplyEnvVars(kwargs) |
agable@chromium.org | 772efaf | 2014-04-01 02:35:44 +0000 | [diff] [blame] | 1212 | cmd = ['git'] + args |
dnj@chromium.org | 680f217 | 2014-06-25 00:39:32 +0000 | [diff] [blame] | 1213 | if show_header: |
sbc@chromium.org | 2cd0b8e | 2014-09-22 21:17:59 +0000 | [diff] [blame] | 1214 | gclient_utils.CheckCallAndFilterAndHeader(cmd, env=env, **kwargs) |
| 1215 | else: |
| 1216 | gclient_utils.CheckCallAndFilter(cmd, env=env, **kwargs) |