The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # Copyright (C) 2008 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 15 | from __future__ import print_function |
Shawn O. Pearce | 438ee1c | 2008-11-03 09:59:36 -0800 | [diff] [blame] | 16 | import errno |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 17 | import filecmp |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 18 | import glob |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | import os |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 20 | import random |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | import re |
| 22 | import shutil |
| 23 | import stat |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 24 | import subprocess |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 25 | import sys |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 26 | import tarfile |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 27 | import tempfile |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 28 | import time |
Dave Borowitz | 137d013 | 2015-01-02 11:12:54 -0800 | [diff] [blame] | 29 | import traceback |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 30 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 31 | from color import Coloring |
Dave Borowitz | b42b474 | 2012-10-31 12:27:27 -0700 | [diff] [blame] | 32 | from git_command import GitCommand, git_require |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 33 | from git_config import GitConfig, IsId, GetSchemeFromUrl, GetUrlCookieFile, \ |
Ningning Xia | c2fbc78 | 2016-08-22 14:24:39 -0700 | [diff] [blame] | 34 | ID_RE, RefSpec |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 35 | from error import GitError, HookError, UploadError, DownloadError |
Ningning Xia | c2fbc78 | 2016-08-22 14:24:39 -0700 | [diff] [blame] | 36 | from error import CacheApplyError |
Shawn O. Pearce | 559b846 | 2009-03-02 12:56:08 -0800 | [diff] [blame] | 37 | from error import ManifestInvalidRevisionError |
Conley Owens | 75ee057 | 2012-11-15 17:33:11 -0800 | [diff] [blame] | 38 | from error import NoManifestException |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 39 | from trace import IsTrace, Trace |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 40 | |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 41 | from git_refs import GitRefs, HEAD, R_HEADS, R_TAGS, R_PUB, R_M |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 42 | |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 43 | from pyversion import is_python3 |
Mike Frysinger | 40252c2 | 2016-08-15 21:23:44 -0400 | [diff] [blame] | 44 | if is_python3(): |
| 45 | import urllib.parse |
| 46 | else: |
| 47 | import imp |
| 48 | import urlparse |
| 49 | urllib = imp.new_module('urllib') |
| 50 | urllib.parse = urlparse |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 51 | # pylint:disable=W0622 |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 52 | input = raw_input |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 53 | # pylint:enable=W0622 |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 54 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 55 | |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 56 | def _lwrite(path, content): |
| 57 | lock = '%s.lock' % path |
| 58 | |
Chirayu Desai | 303a82f | 2014-08-19 22:57:17 +0530 | [diff] [blame] | 59 | fd = open(lock, 'w') |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 60 | try: |
| 61 | fd.write(content) |
| 62 | finally: |
| 63 | fd.close() |
| 64 | |
| 65 | try: |
| 66 | os.rename(lock, path) |
| 67 | except OSError: |
| 68 | os.remove(lock) |
| 69 | raise |
| 70 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 71 | |
Shawn O. Pearce | 4824478 | 2009-04-16 08:25:57 -0700 | [diff] [blame] | 72 | def _error(fmt, *args): |
| 73 | msg = fmt % args |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 74 | print('error: %s' % msg, file=sys.stderr) |
Shawn O. Pearce | 4824478 | 2009-04-16 08:25:57 -0700 | [diff] [blame] | 75 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 76 | |
David Pursehouse | f33929d | 2015-08-24 14:39:14 +0900 | [diff] [blame] | 77 | def _warn(fmt, *args): |
| 78 | msg = fmt % args |
| 79 | print('warn: %s' % msg, file=sys.stderr) |
| 80 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 81 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 82 | def not_rev(r): |
| 83 | return '^' + r |
| 84 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 85 | |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 86 | def sq(r): |
| 87 | return "'" + r.replace("'", "'\''") + "'" |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 88 | |
Jonathan Nieder | 9371979 | 2015-03-17 11:29:58 -0700 | [diff] [blame] | 89 | _project_hook_list = None |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 90 | |
| 91 | |
Jonathan Nieder | 9371979 | 2015-03-17 11:29:58 -0700 | [diff] [blame] | 92 | def _ProjectHooks(): |
| 93 | """List the hooks present in the 'hooks' directory. |
| 94 | |
| 95 | These hooks are project hooks and are copied to the '.git/hooks' directory |
| 96 | of all subprojects. |
| 97 | |
| 98 | This function caches the list of hooks (based on the contents of the |
| 99 | 'repo/hooks' directory) on the first call. |
| 100 | |
| 101 | Returns: |
| 102 | A list of absolute paths to all of the files in the hooks directory. |
| 103 | """ |
| 104 | global _project_hook_list |
| 105 | if _project_hook_list is None: |
| 106 | d = os.path.realpath(os.path.abspath(os.path.dirname(__file__))) |
| 107 | d = os.path.join(d, 'hooks') |
| 108 | _project_hook_list = [os.path.join(d, x) for x in os.listdir(d)] |
| 109 | return _project_hook_list |
| 110 | |
| 111 | |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 112 | class DownloadedChange(object): |
| 113 | _commit_cache = None |
| 114 | |
| 115 | def __init__(self, project, base, change_id, ps_id, commit): |
| 116 | self.project = project |
| 117 | self.base = base |
| 118 | self.change_id = change_id |
| 119 | self.ps_id = ps_id |
| 120 | self.commit = commit |
| 121 | |
| 122 | @property |
| 123 | def commits(self): |
| 124 | if self._commit_cache is None: |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 125 | self._commit_cache = self.project.bare_git.rev_list('--abbrev=8', |
| 126 | '--abbrev-commit', |
| 127 | '--pretty=oneline', |
| 128 | '--reverse', |
| 129 | '--date-order', |
| 130 | not_rev(self.base), |
| 131 | self.commit, |
| 132 | '--') |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 133 | return self._commit_cache |
| 134 | |
| 135 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 136 | class ReviewableBranch(object): |
| 137 | _commit_cache = None |
| 138 | |
| 139 | def __init__(self, project, branch, base): |
| 140 | self.project = project |
| 141 | self.branch = branch |
| 142 | self.base = base |
| 143 | |
| 144 | @property |
| 145 | def name(self): |
| 146 | return self.branch.name |
| 147 | |
| 148 | @property |
| 149 | def commits(self): |
| 150 | if self._commit_cache is None: |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 151 | self._commit_cache = self.project.bare_git.rev_list('--abbrev=8', |
| 152 | '--abbrev-commit', |
| 153 | '--pretty=oneline', |
| 154 | '--reverse', |
| 155 | '--date-order', |
| 156 | not_rev(self.base), |
| 157 | R_HEADS + self.name, |
| 158 | '--') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 159 | return self._commit_cache |
| 160 | |
| 161 | @property |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 162 | def unabbrev_commits(self): |
| 163 | r = dict() |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 164 | for commit in self.project.bare_git.rev_list(not_rev(self.base), |
| 165 | R_HEADS + self.name, |
| 166 | '--'): |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 167 | r[commit[0:8]] = commit |
| 168 | return r |
| 169 | |
| 170 | @property |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 171 | def date(self): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 172 | return self.project.bare_git.log('--pretty=format:%cd', |
| 173 | '-n', '1', |
| 174 | R_HEADS + self.name, |
| 175 | '--') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 176 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 177 | def UploadForReview(self, people, |
| 178 | auto_topic=False, |
| 179 | draft=False, |
| 180 | dest_branch=None): |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 181 | self.project.UploadForReview(self.name, |
Shawn O. Pearce | a5ece0e | 2010-07-15 16:52:42 -0700 | [diff] [blame] | 182 | people, |
Brian Harring | 435370c | 2012-07-28 15:37:04 -0700 | [diff] [blame] | 183 | auto_topic=auto_topic, |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 184 | draft=draft, |
| 185 | dest_branch=dest_branch) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 186 | |
Ficus Kirkpatrick | bc7ef67 | 2009-05-04 12:45:11 -0700 | [diff] [blame] | 187 | def GetPublishedRefs(self): |
| 188 | refs = {} |
| 189 | output = self.project.bare_git.ls_remote( |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 190 | self.branch.remote.SshReviewUrl(self.project.UserEmail), |
| 191 | 'refs/changes/*') |
Ficus Kirkpatrick | bc7ef67 | 2009-05-04 12:45:11 -0700 | [diff] [blame] | 192 | for line in output.split('\n'): |
| 193 | try: |
| 194 | (sha, ref) = line.split() |
| 195 | refs[sha] = ref |
| 196 | except ValueError: |
| 197 | pass |
| 198 | |
| 199 | return refs |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 200 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 201 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 202 | class StatusColoring(Coloring): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 203 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 204 | def __init__(self, config): |
| 205 | Coloring.__init__(self, config, 'status') |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 206 | self.project = self.printer('header', attr='bold') |
| 207 | self.branch = self.printer('header', attr='bold') |
| 208 | self.nobranch = self.printer('nobranch', fg='red') |
| 209 | self.important = self.printer('important', fg='red') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 210 | |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 211 | self.added = self.printer('added', fg='green') |
| 212 | self.changed = self.printer('changed', fg='red') |
| 213 | self.untracked = self.printer('untracked', fg='red') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 214 | |
| 215 | |
| 216 | class DiffColoring(Coloring): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 217 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 218 | def __init__(self, config): |
| 219 | Coloring.__init__(self, config, 'diff') |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 220 | self.project = self.printer('header', attr='bold') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 221 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 222 | |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 223 | class _Annotation(object): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 224 | |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 225 | def __init__(self, name, value, keep): |
| 226 | self.name = name |
| 227 | self.value = value |
| 228 | self.keep = keep |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 229 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 230 | |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 231 | class _CopyFile(object): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 232 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 233 | def __init__(self, src, dest, abssrc, absdest): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 234 | self.src = src |
| 235 | self.dest = dest |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 236 | self.abs_src = abssrc |
| 237 | self.abs_dest = absdest |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 238 | |
| 239 | def _Copy(self): |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 240 | src = self.abs_src |
| 241 | dest = self.abs_dest |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 242 | # copy file if it does not exist or is out of date |
| 243 | if not os.path.exists(dest) or not filecmp.cmp(src, dest): |
| 244 | try: |
| 245 | # remove existing file first, since it might be read-only |
| 246 | if os.path.exists(dest): |
| 247 | os.remove(dest) |
Matthew Buckett | 2daf667 | 2009-07-11 09:43:47 -0400 | [diff] [blame] | 248 | else: |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 249 | dest_dir = os.path.dirname(dest) |
| 250 | if not os.path.isdir(dest_dir): |
| 251 | os.makedirs(dest_dir) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 252 | shutil.copy(src, dest) |
| 253 | # make the file read-only |
| 254 | mode = os.stat(dest)[stat.ST_MODE] |
| 255 | mode = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH) |
| 256 | os.chmod(dest, mode) |
| 257 | except IOError: |
Shawn O. Pearce | 4824478 | 2009-04-16 08:25:57 -0700 | [diff] [blame] | 258 | _error('Cannot copy file %s to %s', src, dest) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 259 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 260 | |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 261 | class _LinkFile(object): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 262 | |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 263 | def __init__(self, git_worktree, src, dest, relsrc, absdest): |
| 264 | self.git_worktree = git_worktree |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 265 | self.src = src |
| 266 | self.dest = dest |
Colin Cross | 0184dcc | 2015-05-05 00:24:54 -0700 | [diff] [blame] | 267 | self.src_rel_to_dest = relsrc |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 268 | self.abs_dest = absdest |
| 269 | |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 270 | def __linkIt(self, relSrc, absDest): |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 271 | # link file if it does not exist or is out of date |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 272 | if not os.path.islink(absDest) or (os.readlink(absDest) != relSrc): |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 273 | try: |
| 274 | # remove existing file first, since it might be read-only |
Dan Willemsen | e1e0bd1 | 2015-11-18 16:49:38 -0800 | [diff] [blame] | 275 | if os.path.lexists(absDest): |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 276 | os.remove(absDest) |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 277 | else: |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 278 | dest_dir = os.path.dirname(absDest) |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 279 | if not os.path.isdir(dest_dir): |
| 280 | os.makedirs(dest_dir) |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 281 | os.symlink(relSrc, absDest) |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 282 | except IOError: |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 283 | _error('Cannot link file %s to %s', relSrc, absDest) |
| 284 | |
| 285 | def _Link(self): |
| 286 | """Link the self.rel_src_to_dest and self.abs_dest. Handles wild cards |
| 287 | on the src linking all of the files in the source in to the destination |
| 288 | directory. |
| 289 | """ |
| 290 | # We use the absSrc to handle the situation where the current directory |
| 291 | # is not the root of the repo |
| 292 | absSrc = os.path.join(self.git_worktree, self.src) |
| 293 | if os.path.exists(absSrc): |
| 294 | # Entity exists so just a simple one to one link operation |
| 295 | self.__linkIt(self.src_rel_to_dest, self.abs_dest) |
| 296 | else: |
| 297 | # Entity doesn't exist assume there is a wild card |
| 298 | absDestDir = self.abs_dest |
| 299 | if os.path.exists(absDestDir) and not os.path.isdir(absDestDir): |
| 300 | _error('Link error: src with wildcard, %s must be a directory', |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 301 | absDestDir) |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 302 | else: |
| 303 | absSrcFiles = glob.glob(absSrc) |
| 304 | for absSrcFile in absSrcFiles: |
| 305 | # Create a releative path from source dir to destination dir |
| 306 | absSrcDir = os.path.dirname(absSrcFile) |
| 307 | relSrcDir = os.path.relpath(absSrcDir, absDestDir) |
| 308 | |
| 309 | # Get the source file name |
| 310 | srcFile = os.path.basename(absSrcFile) |
| 311 | |
| 312 | # Now form the final full paths to srcFile. They will be |
| 313 | # absolute for the desintaiton and relative for the srouce. |
| 314 | absDest = os.path.join(absDestDir, srcFile) |
| 315 | relSrc = os.path.join(relSrcDir, srcFile) |
| 316 | self.__linkIt(relSrc, absDest) |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 317 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 318 | |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 319 | class RemoteSpec(object): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 320 | |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 321 | def __init__(self, |
| 322 | name, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 323 | url=None, |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 324 | pushUrl=None, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 325 | review=None, |
Dan Willemsen | 96c2d65 | 2016-04-06 16:03:54 -0700 | [diff] [blame] | 326 | revision=None, |
| 327 | orig_name=None): |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 328 | self.name = name |
| 329 | self.url = url |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 330 | self.pushUrl = pushUrl |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 331 | self.review = review |
Anthony King | 36ea2fb | 2014-05-06 11:54:01 +0100 | [diff] [blame] | 332 | self.revision = revision |
Dan Willemsen | 96c2d65 | 2016-04-06 16:03:54 -0700 | [diff] [blame] | 333 | self.orig_name = orig_name |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 334 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 335 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 336 | class RepoHook(object): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 337 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 338 | """A RepoHook contains information about a script to run as a hook. |
| 339 | |
| 340 | Hooks are used to run a python script before running an upload (for instance, |
| 341 | to run presubmit checks). Eventually, we may have hooks for other actions. |
| 342 | |
| 343 | This shouldn't be confused with files in the 'repo/hooks' directory. Those |
| 344 | files are copied into each '.git/hooks' folder for each project. Repo-level |
| 345 | hooks are associated instead with repo actions. |
| 346 | |
| 347 | Hooks are always python. When a hook is run, we will load the hook into the |
| 348 | interpreter and execute its main() function. |
| 349 | """ |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 350 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 351 | def __init__(self, |
| 352 | hook_type, |
| 353 | hooks_project, |
| 354 | topdir, |
Mike Frysinger | 40252c2 | 2016-08-15 21:23:44 -0400 | [diff] [blame] | 355 | manifest_url, |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 356 | abort_if_user_denies=False): |
| 357 | """RepoHook constructor. |
| 358 | |
| 359 | Params: |
| 360 | hook_type: A string representing the type of hook. This is also used |
| 361 | to figure out the name of the file containing the hook. For |
| 362 | example: 'pre-upload'. |
| 363 | hooks_project: The project containing the repo hooks. If you have a |
| 364 | manifest, this is manifest.repo_hooks_project. OK if this is None, |
| 365 | which will make the hook a no-op. |
| 366 | topdir: Repo's top directory (the one containing the .repo directory). |
| 367 | Scripts will run with CWD as this directory. If you have a manifest, |
| 368 | this is manifest.topdir |
Mike Frysinger | 40252c2 | 2016-08-15 21:23:44 -0400 | [diff] [blame] | 369 | manifest_url: The URL to the manifest git repo. |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 370 | abort_if_user_denies: If True, we'll throw a HookError() if the user |
| 371 | doesn't allow us to run the hook. |
| 372 | """ |
| 373 | self._hook_type = hook_type |
| 374 | self._hooks_project = hooks_project |
Mike Frysinger | 40252c2 | 2016-08-15 21:23:44 -0400 | [diff] [blame] | 375 | self._manifest_url = manifest_url |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 376 | self._topdir = topdir |
| 377 | self._abort_if_user_denies = abort_if_user_denies |
| 378 | |
| 379 | # Store the full path to the script for convenience. |
| 380 | if self._hooks_project: |
| 381 | self._script_fullpath = os.path.join(self._hooks_project.worktree, |
| 382 | self._hook_type + '.py') |
| 383 | else: |
| 384 | self._script_fullpath = None |
| 385 | |
| 386 | def _GetHash(self): |
| 387 | """Return a hash of the contents of the hooks directory. |
| 388 | |
| 389 | We'll just use git to do this. This hash has the property that if anything |
| 390 | changes in the directory we will return a different has. |
| 391 | |
| 392 | SECURITY CONSIDERATION: |
| 393 | This hash only represents the contents of files in the hook directory, not |
| 394 | any other files imported or called by hooks. Changes to imported files |
| 395 | can change the script behavior without affecting the hash. |
| 396 | |
| 397 | Returns: |
| 398 | A string representing the hash. This will always be ASCII so that it can |
| 399 | be printed to the user easily. |
| 400 | """ |
| 401 | assert self._hooks_project, "Must have hooks to calculate their hash." |
| 402 | |
| 403 | # We will use the work_git object rather than just calling GetRevisionId(). |
| 404 | # That gives us a hash of the latest checked in version of the files that |
| 405 | # the user will actually be executing. Specifically, GetRevisionId() |
| 406 | # doesn't appear to change even if a user checks out a different version |
| 407 | # of the hooks repo (via git checkout) nor if a user commits their own revs. |
| 408 | # |
| 409 | # NOTE: Local (non-committed) changes will not be factored into this hash. |
| 410 | # I think this is OK, since we're really only worried about warning the user |
| 411 | # about upstream changes. |
| 412 | return self._hooks_project.work_git.rev_parse('HEAD') |
| 413 | |
| 414 | def _GetMustVerb(self): |
| 415 | """Return 'must' if the hook is required; 'should' if not.""" |
| 416 | if self._abort_if_user_denies: |
| 417 | return 'must' |
| 418 | else: |
| 419 | return 'should' |
| 420 | |
| 421 | def _CheckForHookApproval(self): |
| 422 | """Check to see whether this hook has been approved. |
| 423 | |
Mike Frysinger | 40252c2 | 2016-08-15 21:23:44 -0400 | [diff] [blame] | 424 | We'll accept approval of manifest URLs if they're using secure transports. |
| 425 | This way the user can say they trust the manifest hoster. For insecure |
| 426 | hosts, we fall back to checking the hash of the hooks repo. |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 427 | |
| 428 | Note that we ask permission for each individual hook even though we use |
| 429 | the hash of all hooks when detecting changes. We'd like the user to be |
| 430 | able to approve / deny each hook individually. We only use the hash of all |
| 431 | hooks because there is no other easy way to detect changes to local imports. |
| 432 | |
| 433 | Returns: |
| 434 | True if this hook is approved to run; False otherwise. |
| 435 | |
| 436 | Raises: |
| 437 | HookError: Raised if the user doesn't approve and abort_if_user_denies |
| 438 | was passed to the consturctor. |
| 439 | """ |
Mike Frysinger | 40252c2 | 2016-08-15 21:23:44 -0400 | [diff] [blame] | 440 | if self._ManifestUrlHasSecureScheme(): |
| 441 | return self._CheckForHookApprovalManifest() |
| 442 | else: |
| 443 | return self._CheckForHookApprovalHash() |
| 444 | |
| 445 | def _CheckForHookApprovalHelper(self, subkey, new_val, main_prompt, |
| 446 | changed_prompt): |
| 447 | """Check for approval for a particular attribute and hook. |
| 448 | |
| 449 | Args: |
| 450 | subkey: The git config key under [repo.hooks.<hook_type>] to store the |
| 451 | last approved string. |
| 452 | new_val: The new value to compare against the last approved one. |
| 453 | main_prompt: Message to display to the user to ask for approval. |
| 454 | changed_prompt: Message explaining why we're re-asking for approval. |
| 455 | |
| 456 | Returns: |
| 457 | True if this hook is approved to run; False otherwise. |
| 458 | |
| 459 | Raises: |
| 460 | HookError: Raised if the user doesn't approve and abort_if_user_denies |
| 461 | was passed to the consturctor. |
| 462 | """ |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 463 | hooks_config = self._hooks_project.config |
Mike Frysinger | 40252c2 | 2016-08-15 21:23:44 -0400 | [diff] [blame] | 464 | git_approval_key = 'repo.hooks.%s.%s' % (self._hook_type, subkey) |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 465 | |
Mike Frysinger | 40252c2 | 2016-08-15 21:23:44 -0400 | [diff] [blame] | 466 | # Get the last value that the user approved for this hook; may be None. |
| 467 | old_val = hooks_config.GetString(git_approval_key) |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 468 | |
Mike Frysinger | 40252c2 | 2016-08-15 21:23:44 -0400 | [diff] [blame] | 469 | if old_val is not None: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 470 | # User previously approved hook and asked not to be prompted again. |
Mike Frysinger | 40252c2 | 2016-08-15 21:23:44 -0400 | [diff] [blame] | 471 | if new_val == old_val: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 472 | # Approval matched. We're done. |
| 473 | return True |
| 474 | else: |
| 475 | # Give the user a reason why we're prompting, since they last told |
| 476 | # us to "never ask again". |
Mike Frysinger | 40252c2 | 2016-08-15 21:23:44 -0400 | [diff] [blame] | 477 | prompt = 'WARNING: %s\n\n' % (changed_prompt,) |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 478 | else: |
| 479 | prompt = '' |
| 480 | |
| 481 | # Prompt the user if we're not on a tty; on a tty we'll assume "no". |
| 482 | if sys.stdout.isatty(): |
Mike Frysinger | 40252c2 | 2016-08-15 21:23:44 -0400 | [diff] [blame] | 483 | prompt += main_prompt + ' (yes/always/NO)? ' |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 484 | response = input(prompt).lower() |
David Pursehouse | 98ffba1 | 2012-11-14 11:18:00 +0900 | [diff] [blame] | 485 | print() |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 486 | |
| 487 | # User is doing a one-time approval. |
| 488 | if response in ('y', 'yes'): |
| 489 | return True |
Mike Frysinger | 40252c2 | 2016-08-15 21:23:44 -0400 | [diff] [blame] | 490 | elif response == 'always': |
| 491 | hooks_config.SetString(git_approval_key, new_val) |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 492 | return True |
| 493 | |
| 494 | # For anything else, we'll assume no approval. |
| 495 | if self._abort_if_user_denies: |
| 496 | raise HookError('You must allow the %s hook or use --no-verify.' % |
| 497 | self._hook_type) |
| 498 | |
| 499 | return False |
| 500 | |
Mike Frysinger | 40252c2 | 2016-08-15 21:23:44 -0400 | [diff] [blame] | 501 | def _ManifestUrlHasSecureScheme(self): |
| 502 | """Check if the URI for the manifest is a secure transport.""" |
| 503 | secure_schemes = ('file', 'https', 'ssh', 'persistent-https', 'sso', 'rpc') |
| 504 | parse_results = urllib.parse.urlparse(self._manifest_url) |
| 505 | return parse_results.scheme in secure_schemes |
| 506 | |
| 507 | def _CheckForHookApprovalManifest(self): |
| 508 | """Check whether the user has approved this manifest host. |
| 509 | |
| 510 | Returns: |
| 511 | True if this hook is approved to run; False otherwise. |
| 512 | """ |
| 513 | return self._CheckForHookApprovalHelper( |
| 514 | 'approvedmanifest', |
| 515 | self._manifest_url, |
| 516 | 'Run hook scripts from %s' % (self._manifest_url,), |
| 517 | 'Manifest URL has changed since %s was allowed.' % (self._hook_type,)) |
| 518 | |
| 519 | def _CheckForHookApprovalHash(self): |
| 520 | """Check whether the user has approved the hooks repo. |
| 521 | |
| 522 | Returns: |
| 523 | True if this hook is approved to run; False otherwise. |
| 524 | """ |
| 525 | prompt = ('Repo %s run the script:\n' |
| 526 | ' %s\n' |
| 527 | '\n' |
Jonathan Nieder | 71e4cea | 2016-08-16 12:05:09 -0700 | [diff] [blame] | 528 | 'Do you want to allow this script to run') |
Mike Frysinger | 40252c2 | 2016-08-15 21:23:44 -0400 | [diff] [blame] | 529 | return self._CheckForHookApprovalHelper( |
| 530 | 'approvedhash', |
| 531 | self._GetHash(), |
Jonathan Nieder | 71e4cea | 2016-08-16 12:05:09 -0700 | [diff] [blame] | 532 | prompt % (self._GetMustVerb(), self._script_fullpath), |
Mike Frysinger | 40252c2 | 2016-08-15 21:23:44 -0400 | [diff] [blame] | 533 | 'Scripts have changed since %s was allowed.' % (self._hook_type,)) |
| 534 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 535 | def _ExecuteHook(self, **kwargs): |
| 536 | """Actually execute the given hook. |
| 537 | |
| 538 | This will run the hook's 'main' function in our python interpreter. |
| 539 | |
| 540 | Args: |
| 541 | kwargs: Keyword arguments to pass to the hook. These are often specific |
| 542 | to the hook type. For instance, pre-upload hooks will contain |
| 543 | a project_list. |
| 544 | """ |
| 545 | # Keep sys.path and CWD stashed away so that we can always restore them |
| 546 | # upon function exit. |
| 547 | orig_path = os.getcwd() |
| 548 | orig_syspath = sys.path |
| 549 | |
| 550 | try: |
| 551 | # Always run hooks with CWD as topdir. |
| 552 | os.chdir(self._topdir) |
| 553 | |
| 554 | # Put the hook dir as the first item of sys.path so hooks can do |
| 555 | # relative imports. We want to replace the repo dir as [0] so |
| 556 | # hooks can't import repo files. |
| 557 | sys.path = [os.path.dirname(self._script_fullpath)] + sys.path[1:] |
| 558 | |
| 559 | # Exec, storing global context in the context dict. We catch exceptions |
| 560 | # and convert to a HookError w/ just the failing traceback. |
Mike Frysinger | 4aa4b21 | 2016-03-04 15:03:00 -0500 | [diff] [blame] | 561 | context = {'__file__': self._script_fullpath} |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 562 | try: |
Anthony King | 70f6890 | 2014-05-05 21:15:34 +0100 | [diff] [blame] | 563 | exec(compile(open(self._script_fullpath).read(), |
| 564 | self._script_fullpath, 'exec'), context) |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 565 | except Exception: |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 566 | raise HookError('%s\nFailed to import %s hook; see traceback above.' % |
| 567 | (traceback.format_exc(), self._hook_type)) |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 568 | |
| 569 | # Running the script should have defined a main() function. |
| 570 | if 'main' not in context: |
| 571 | raise HookError('Missing main() in: "%s"' % self._script_fullpath) |
| 572 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 573 | # Add 'hook_should_take_kwargs' to the arguments to be passed to main. |
| 574 | # We don't actually want hooks to define their main with this argument-- |
| 575 | # it's there to remind them that their hook should always take **kwargs. |
| 576 | # For instance, a pre-upload hook should be defined like: |
| 577 | # def main(project_list, **kwargs): |
| 578 | # |
| 579 | # This allows us to later expand the API without breaking old hooks. |
| 580 | kwargs = kwargs.copy() |
| 581 | kwargs['hook_should_take_kwargs'] = True |
| 582 | |
| 583 | # Call the main function in the hook. If the hook should cause the |
| 584 | # build to fail, it will raise an Exception. We'll catch that convert |
| 585 | # to a HookError w/ just the failing traceback. |
| 586 | try: |
| 587 | context['main'](**kwargs) |
| 588 | except Exception: |
| 589 | raise HookError('%s\nFailed to run main() for %s hook; see traceback ' |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 590 | 'above.' % (traceback.format_exc(), |
| 591 | self._hook_type)) |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 592 | finally: |
| 593 | # Restore sys.path and CWD. |
| 594 | sys.path = orig_syspath |
| 595 | os.chdir(orig_path) |
| 596 | |
| 597 | def Run(self, user_allows_all_hooks, **kwargs): |
| 598 | """Run the hook. |
| 599 | |
| 600 | If the hook doesn't exist (because there is no hooks project or because |
| 601 | this particular hook is not enabled), this is a no-op. |
| 602 | |
| 603 | Args: |
| 604 | user_allows_all_hooks: If True, we will never prompt about running the |
| 605 | hook--we'll just assume it's OK to run it. |
| 606 | kwargs: Keyword arguments to pass to the hook. These are often specific |
| 607 | to the hook type. For instance, pre-upload hooks will contain |
| 608 | a project_list. |
| 609 | |
| 610 | Raises: |
| 611 | HookError: If there was a problem finding the hook or the user declined |
| 612 | to run a required hook (from _CheckForHookApproval). |
| 613 | """ |
| 614 | # No-op if there is no hooks project or if hook is disabled. |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 615 | if ((not self._hooks_project) or (self._hook_type not in |
| 616 | self._hooks_project.enabled_repo_hooks)): |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 617 | return |
| 618 | |
| 619 | # Bail with a nice error if we can't find the hook. |
| 620 | if not os.path.isfile(self._script_fullpath): |
| 621 | raise HookError('Couldn\'t find repo hook: "%s"' % self._script_fullpath) |
| 622 | |
| 623 | # Make sure the user is OK with running the hook. |
| 624 | if (not user_allows_all_hooks) and (not self._CheckForHookApproval()): |
| 625 | return |
| 626 | |
| 627 | # Run the hook with the same version of python we're using. |
| 628 | self._ExecuteHook(**kwargs) |
| 629 | |
| 630 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 631 | class Project(object): |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 632 | # These objects can be shared between several working trees. |
| 633 | shareable_files = ['description', 'info'] |
| 634 | shareable_dirs = ['hooks', 'objects', 'rr-cache', 'svn'] |
| 635 | # These objects can only be used by a single working tree. |
| 636 | working_tree_files = ['config', 'packed-refs', 'shallow'] |
| 637 | working_tree_dirs = ['logs', 'refs'] |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 638 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 639 | def __init__(self, |
| 640 | manifest, |
| 641 | name, |
| 642 | remote, |
| 643 | gitdir, |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 644 | objdir, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 645 | worktree, |
| 646 | relpath, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 647 | revisionExpr, |
Mike Pontillo | d315382 | 2012-02-28 11:53:24 -0800 | [diff] [blame] | 648 | revisionId, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 649 | rebase=True, |
| 650 | groups=None, |
| 651 | sync_c=False, |
| 652 | sync_s=False, |
| 653 | clone_depth=None, |
| 654 | upstream=None, |
| 655 | parent=None, |
| 656 | is_derived=False, |
David Pursehouse | b155354 | 2014-09-04 21:28:09 +0900 | [diff] [blame] | 657 | dest_branch=None, |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 658 | optimized_fetch=False, |
| 659 | old_revision=None): |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 660 | """Init a Project object. |
| 661 | |
| 662 | Args: |
| 663 | manifest: The XmlManifest object. |
| 664 | name: The `name` attribute of manifest.xml's project element. |
| 665 | remote: RemoteSpec object specifying its remote's properties. |
| 666 | gitdir: Absolute path of git directory. |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 667 | objdir: Absolute path of directory to store git objects. |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 668 | worktree: Absolute path of git working tree. |
| 669 | relpath: Relative path of git working tree to repo's top directory. |
| 670 | revisionExpr: The `revision` attribute of manifest.xml's project element. |
| 671 | revisionId: git commit id for checking out. |
| 672 | rebase: The `rebase` attribute of manifest.xml's project element. |
| 673 | groups: The `groups` attribute of manifest.xml's project element. |
| 674 | sync_c: The `sync-c` attribute of manifest.xml's project element. |
| 675 | sync_s: The `sync-s` attribute of manifest.xml's project element. |
| 676 | upstream: The `upstream` attribute of manifest.xml's project element. |
| 677 | parent: The parent Project object. |
| 678 | is_derived: False if the project was explicitly defined in the manifest; |
| 679 | True if the project is a discovered submodule. |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 680 | dest_branch: The branch to which to push changes for review by default. |
David Pursehouse | b155354 | 2014-09-04 21:28:09 +0900 | [diff] [blame] | 681 | optimized_fetch: If True, when a project is set to a sha1 revision, only |
| 682 | fetch from the remote if the sha1 is not present locally. |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 683 | old_revision: saved git commit id for open GITC projects. |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 684 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 685 | self.manifest = manifest |
| 686 | self.name = name |
| 687 | self.remote = remote |
Anthony Newnam | df14a70 | 2011-01-09 17:31:57 -0800 | [diff] [blame] | 688 | self.gitdir = gitdir.replace('\\', '/') |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 689 | self.objdir = objdir.replace('\\', '/') |
Shawn O. Pearce | 0ce6ca9 | 2011-01-10 13:26:01 -0800 | [diff] [blame] | 690 | if worktree: |
Mark E. Hamilton | f9fe3e1 | 2016-02-23 18:10:42 -0700 | [diff] [blame] | 691 | self.worktree = os.path.normpath(worktree.replace('\\', '/')) |
Shawn O. Pearce | 0ce6ca9 | 2011-01-10 13:26:01 -0800 | [diff] [blame] | 692 | else: |
| 693 | self.worktree = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 694 | self.relpath = relpath |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 695 | self.revisionExpr = revisionExpr |
| 696 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 697 | if revisionId is None \ |
| 698 | and revisionExpr \ |
| 699 | and IsId(revisionExpr): |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 700 | self.revisionId = revisionExpr |
| 701 | else: |
| 702 | self.revisionId = revisionId |
| 703 | |
Mike Pontillo | d315382 | 2012-02-28 11:53:24 -0800 | [diff] [blame] | 704 | self.rebase = rebase |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 705 | self.groups = groups |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 706 | self.sync_c = sync_c |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 707 | self.sync_s = sync_s |
David Pursehouse | ede7f12 | 2012-11-27 22:25:30 +0900 | [diff] [blame] | 708 | self.clone_depth = clone_depth |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 709 | self.upstream = upstream |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 710 | self.parent = parent |
| 711 | self.is_derived = is_derived |
David Pursehouse | b155354 | 2014-09-04 21:28:09 +0900 | [diff] [blame] | 712 | self.optimized_fetch = optimized_fetch |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 713 | self.subprojects = [] |
Mike Pontillo | d315382 | 2012-02-28 11:53:24 -0800 | [diff] [blame] | 714 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 715 | self.snapshots = {} |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 716 | self.copyfiles = [] |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 717 | self.linkfiles = [] |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 718 | self.annotations = [] |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 719 | self.config = GitConfig.ForRepository(gitdir=self.gitdir, |
| 720 | defaults=self.manifest.globalConfig) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 721 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 722 | if self.worktree: |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 723 | self.work_git = self._GitGetByExec(self, bare=False, gitdir=gitdir) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 724 | else: |
| 725 | self.work_git = None |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 726 | self.bare_git = self._GitGetByExec(self, bare=True, gitdir=gitdir) |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 727 | self.bare_ref = GitRefs(gitdir) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 728 | self.bare_objdir = self._GitGetByExec(self, bare=True, gitdir=objdir) |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 729 | self.dest_branch = dest_branch |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 730 | self.old_revision = old_revision |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 731 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 732 | # This will be filled in if a project is later identified to be the |
| 733 | # project containing repo hooks. |
| 734 | self.enabled_repo_hooks = [] |
| 735 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 736 | @property |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 737 | def Derived(self): |
| 738 | return self.is_derived |
| 739 | |
| 740 | @property |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 741 | def Exists(self): |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 742 | return os.path.isdir(self.gitdir) and os.path.isdir(self.objdir) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 743 | |
| 744 | @property |
| 745 | def CurrentBranch(self): |
| 746 | """Obtain the name of the currently checked out branch. |
| 747 | The branch name omits the 'refs/heads/' prefix. |
| 748 | None is returned if the project is on a detached HEAD. |
| 749 | """ |
Shawn O. Pearce | 5b23f24 | 2009-04-17 18:43:33 -0700 | [diff] [blame] | 750 | b = self.work_git.GetHead() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 751 | if b.startswith(R_HEADS): |
| 752 | return b[len(R_HEADS):] |
| 753 | return None |
| 754 | |
Shawn O. Pearce | 3d2cdd0 | 2009-04-18 15:26:10 -0700 | [diff] [blame] | 755 | def IsRebaseInProgress(self): |
| 756 | w = self.worktree |
| 757 | g = os.path.join(w, '.git') |
| 758 | return os.path.exists(os.path.join(g, 'rebase-apply')) \ |
| 759 | or os.path.exists(os.path.join(g, 'rebase-merge')) \ |
| 760 | or os.path.exists(os.path.join(w, '.dotest')) |
Julius Gustavsson | 0cb1b3f | 2010-06-17 17:55:02 +0200 | [diff] [blame] | 761 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 762 | def IsDirty(self, consider_untracked=True): |
| 763 | """Is the working directory modified in some way? |
| 764 | """ |
| 765 | self.work_git.update_index('-q', |
| 766 | '--unmerged', |
| 767 | '--ignore-missing', |
| 768 | '--refresh') |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 769 | if self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 770 | return True |
| 771 | if self.work_git.DiffZ('diff-files'): |
| 772 | return True |
| 773 | if consider_untracked and self.work_git.LsOthers(): |
| 774 | return True |
| 775 | return False |
| 776 | |
| 777 | _userident_name = None |
| 778 | _userident_email = None |
| 779 | |
| 780 | @property |
| 781 | def UserName(self): |
| 782 | """Obtain the user's personal name. |
| 783 | """ |
| 784 | if self._userident_name is None: |
| 785 | self._LoadUserIdentity() |
| 786 | return self._userident_name |
| 787 | |
| 788 | @property |
| 789 | def UserEmail(self): |
| 790 | """Obtain the user's email address. This is very likely |
| 791 | to be their Gerrit login. |
| 792 | """ |
| 793 | if self._userident_email is None: |
| 794 | self._LoadUserIdentity() |
| 795 | return self._userident_email |
| 796 | |
| 797 | def _LoadUserIdentity(self): |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 798 | u = self.bare_git.var('GIT_COMMITTER_IDENT') |
| 799 | m = re.compile("^(.*) <([^>]*)> ").match(u) |
| 800 | if m: |
| 801 | self._userident_name = m.group(1) |
| 802 | self._userident_email = m.group(2) |
| 803 | else: |
| 804 | self._userident_name = '' |
| 805 | self._userident_email = '' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 806 | |
| 807 | def GetRemote(self, name): |
| 808 | """Get the configuration for a single remote. |
| 809 | """ |
| 810 | return self.config.GetRemote(name) |
| 811 | |
| 812 | def GetBranch(self, name): |
| 813 | """Get the configuration for a single branch. |
| 814 | """ |
| 815 | return self.config.GetBranch(name) |
| 816 | |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 817 | def GetBranches(self): |
| 818 | """Get all existing local branches. |
| 819 | """ |
| 820 | current = self.CurrentBranch |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 821 | all_refs = self._allrefs |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 822 | heads = {} |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 823 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 824 | for name, ref_id in all_refs.items(): |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 825 | if name.startswith(R_HEADS): |
| 826 | name = name[len(R_HEADS):] |
| 827 | b = self.GetBranch(name) |
| 828 | b.current = name == current |
| 829 | b.published = None |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 830 | b.revision = ref_id |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 831 | heads[name] = b |
| 832 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 833 | for name, ref_id in all_refs.items(): |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 834 | if name.startswith(R_PUB): |
| 835 | name = name[len(R_PUB):] |
| 836 | b = heads.get(name) |
| 837 | if b: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 838 | b.published = ref_id |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 839 | |
| 840 | return heads |
| 841 | |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 842 | def MatchesGroups(self, manifest_groups): |
| 843 | """Returns true if the manifest groups specified at init should cause |
| 844 | this project to be synced. |
| 845 | Prefixing a manifest group with "-" inverts the meaning of a group. |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 846 | All projects are implicitly labelled with "all". |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 847 | |
| 848 | labels are resolved in order. In the example case of |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 849 | project_groups: "all,group1,group2" |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 850 | manifest_groups: "-group1,group2" |
| 851 | the project will be matched. |
David Holmer | 0a1c6a1 | 2012-11-14 19:19:00 -0500 | [diff] [blame] | 852 | |
| 853 | The special manifest group "default" will match any project that |
| 854 | does not have the special project group "notdefault" |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 855 | """ |
David Holmer | 0a1c6a1 | 2012-11-14 19:19:00 -0500 | [diff] [blame] | 856 | expanded_manifest_groups = manifest_groups or ['default'] |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 857 | expanded_project_groups = ['all'] + (self.groups or []) |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 858 | if 'notdefault' not in expanded_project_groups: |
David Holmer | 0a1c6a1 | 2012-11-14 19:19:00 -0500 | [diff] [blame] | 859 | expanded_project_groups += ['default'] |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 860 | |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 861 | matched = False |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 862 | for group in expanded_manifest_groups: |
| 863 | if group.startswith('-') and group[1:] in expanded_project_groups: |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 864 | matched = False |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 865 | elif group in expanded_project_groups: |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 866 | matched = True |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 867 | |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 868 | return matched |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 869 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 870 | # Status Display ## |
Vadim Bendebury | 14e134d | 2014-10-05 15:40:30 -0700 | [diff] [blame] | 871 | def UncommitedFiles(self, get_all=True): |
| 872 | """Returns a list of strings, uncommitted files in the git tree. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 873 | |
Vadim Bendebury | 14e134d | 2014-10-05 15:40:30 -0700 | [diff] [blame] | 874 | Args: |
| 875 | get_all: a boolean, if True - get information about all different |
| 876 | uncommitted files. If False - return as soon as any kind of |
| 877 | uncommitted files is detected. |
Anthony Newnam | cc50bac | 2010-04-08 10:28:59 -0500 | [diff] [blame] | 878 | """ |
Vadim Bendebury | 14e134d | 2014-10-05 15:40:30 -0700 | [diff] [blame] | 879 | details = [] |
Anthony Newnam | cc50bac | 2010-04-08 10:28:59 -0500 | [diff] [blame] | 880 | self.work_git.update_index('-q', |
| 881 | '--unmerged', |
| 882 | '--ignore-missing', |
| 883 | '--refresh') |
| 884 | if self.IsRebaseInProgress(): |
Vadim Bendebury | 14e134d | 2014-10-05 15:40:30 -0700 | [diff] [blame] | 885 | details.append("rebase in progress") |
| 886 | if not get_all: |
| 887 | return details |
Anthony Newnam | cc50bac | 2010-04-08 10:28:59 -0500 | [diff] [blame] | 888 | |
Vadim Bendebury | 14e134d | 2014-10-05 15:40:30 -0700 | [diff] [blame] | 889 | changes = self.work_git.DiffZ('diff-index', '--cached', HEAD).keys() |
| 890 | if changes: |
| 891 | details.extend(changes) |
| 892 | if not get_all: |
| 893 | return details |
Anthony Newnam | cc50bac | 2010-04-08 10:28:59 -0500 | [diff] [blame] | 894 | |
Vadim Bendebury | 14e134d | 2014-10-05 15:40:30 -0700 | [diff] [blame] | 895 | changes = self.work_git.DiffZ('diff-files').keys() |
| 896 | if changes: |
| 897 | details.extend(changes) |
| 898 | if not get_all: |
| 899 | return details |
Anthony Newnam | cc50bac | 2010-04-08 10:28:59 -0500 | [diff] [blame] | 900 | |
Vadim Bendebury | 14e134d | 2014-10-05 15:40:30 -0700 | [diff] [blame] | 901 | changes = self.work_git.LsOthers() |
| 902 | if changes: |
| 903 | details.extend(changes) |
Anthony Newnam | cc50bac | 2010-04-08 10:28:59 -0500 | [diff] [blame] | 904 | |
Vadim Bendebury | 14e134d | 2014-10-05 15:40:30 -0700 | [diff] [blame] | 905 | return details |
| 906 | |
| 907 | def HasChanges(self): |
| 908 | """Returns true if there are uncommitted changes. |
| 909 | """ |
| 910 | if self.UncommitedFiles(get_all=False): |
| 911 | return True |
| 912 | else: |
| 913 | return False |
Anthony Newnam | cc50bac | 2010-04-08 10:28:59 -0500 | [diff] [blame] | 914 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 915 | def PrintWorkTreeStatus(self, output_redir=None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 916 | """Prints the status of the repository to stdout. |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 917 | |
| 918 | Args: |
| 919 | output: If specified, redirect the output to this object. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 920 | """ |
| 921 | if not os.path.isdir(self.worktree): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 922 | if output_redir is None: |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 923 | output_redir = sys.stdout |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 924 | print(file=output_redir) |
| 925 | print('project %s/' % self.relpath, file=output_redir) |
| 926 | print(' missing (run "repo sync")', file=output_redir) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 927 | return |
| 928 | |
| 929 | self.work_git.update_index('-q', |
| 930 | '--unmerged', |
| 931 | '--ignore-missing', |
| 932 | '--refresh') |
Shawn O. Pearce | 3d2cdd0 | 2009-04-18 15:26:10 -0700 | [diff] [blame] | 933 | rb = self.IsRebaseInProgress() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 934 | di = self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD) |
| 935 | df = self.work_git.DiffZ('diff-files') |
| 936 | do = self.work_git.LsOthers() |
Ali Utku Selen | 76abcc1 | 2012-01-25 10:51:12 +0100 | [diff] [blame] | 937 | if not rb and not di and not df and not do and not self.CurrentBranch: |
Shawn O. Pearce | 161f445 | 2009-04-10 17:41:44 -0700 | [diff] [blame] | 938 | return 'CLEAN' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 939 | |
| 940 | out = StatusColoring(self.config) |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 941 | if output_redir is not None: |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 942 | out.redirect(output_redir) |
Jakub Vrana | 0402cd8 | 2014-09-09 15:39:15 -0700 | [diff] [blame] | 943 | out.project('project %-40s', self.relpath + '/ ') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 944 | |
| 945 | branch = self.CurrentBranch |
| 946 | if branch is None: |
| 947 | out.nobranch('(*** NO BRANCH ***)') |
| 948 | else: |
| 949 | out.branch('branch %s', branch) |
| 950 | out.nl() |
| 951 | |
Shawn O. Pearce | 3d2cdd0 | 2009-04-18 15:26:10 -0700 | [diff] [blame] | 952 | if rb: |
| 953 | out.important('prior sync failed; rebase still in progress') |
| 954 | out.nl() |
| 955 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 956 | paths = list() |
| 957 | paths.extend(di.keys()) |
| 958 | paths.extend(df.keys()) |
| 959 | paths.extend(do) |
| 960 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 961 | for p in sorted(set(paths)): |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 962 | try: |
| 963 | i = di[p] |
| 964 | except KeyError: |
| 965 | i = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 966 | |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 967 | try: |
| 968 | f = df[p] |
| 969 | except KeyError: |
| 970 | f = None |
Julius Gustavsson | 0cb1b3f | 2010-06-17 17:55:02 +0200 | [diff] [blame] | 971 | |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 972 | if i: |
| 973 | i_status = i.status.upper() |
| 974 | else: |
| 975 | i_status = '-' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 976 | |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 977 | if f: |
| 978 | f_status = f.status.lower() |
| 979 | else: |
| 980 | f_status = '-' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 981 | |
| 982 | if i and i.src_path: |
Shawn O. Pearce | fe08675 | 2009-03-03 13:49:48 -0800 | [diff] [blame] | 983 | line = ' %s%s\t%s => %s (%s%%)' % (i_status, f_status, |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 984 | i.src_path, p, i.level) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 985 | else: |
| 986 | line = ' %s%s\t%s' % (i_status, f_status, p) |
| 987 | |
| 988 | if i and not f: |
| 989 | out.added('%s', line) |
| 990 | elif (i and f) or (not i and f): |
| 991 | out.changed('%s', line) |
| 992 | elif not i and not f: |
| 993 | out.untracked('%s', line) |
| 994 | else: |
| 995 | out.write('%s', line) |
| 996 | out.nl() |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 997 | |
Shawn O. Pearce | 161f445 | 2009-04-10 17:41:44 -0700 | [diff] [blame] | 998 | return 'DIRTY' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 999 | |
pelya | d67872d | 2012-03-28 14:49:58 +0300 | [diff] [blame] | 1000 | def PrintWorkTreeDiff(self, absolute_paths=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1001 | """Prints the status of the repository to stdout. |
| 1002 | """ |
| 1003 | out = DiffColoring(self.config) |
| 1004 | cmd = ['diff'] |
| 1005 | if out.is_on: |
| 1006 | cmd.append('--color') |
| 1007 | cmd.append(HEAD) |
pelya | d67872d | 2012-03-28 14:49:58 +0300 | [diff] [blame] | 1008 | if absolute_paths: |
| 1009 | cmd.append('--src-prefix=a/%s/' % self.relpath) |
| 1010 | cmd.append('--dst-prefix=b/%s/' % self.relpath) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1011 | cmd.append('--') |
| 1012 | p = GitCommand(self, |
| 1013 | cmd, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1014 | capture_stdout=True, |
| 1015 | capture_stderr=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1016 | has_diff = False |
| 1017 | for line in p.process.stdout: |
| 1018 | if not has_diff: |
| 1019 | out.nl() |
| 1020 | out.project('project %s/' % self.relpath) |
| 1021 | out.nl() |
| 1022 | has_diff = True |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 1023 | print(line[:-1]) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1024 | p.Wait() |
| 1025 | |
| 1026 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1027 | # Publish / Upload ## |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1028 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1029 | def WasPublished(self, branch, all_refs=None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1030 | """Was the branch published (uploaded) for code review? |
| 1031 | If so, returns the SHA-1 hash of the last published |
| 1032 | state for the branch. |
| 1033 | """ |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1034 | key = R_PUB + branch |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1035 | if all_refs is None: |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1036 | try: |
| 1037 | return self.bare_git.rev_parse(key) |
| 1038 | except GitError: |
| 1039 | return None |
| 1040 | else: |
| 1041 | try: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1042 | return all_refs[key] |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1043 | except KeyError: |
| 1044 | return None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1045 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1046 | def CleanPublishedCache(self, all_refs=None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1047 | """Prunes any stale published refs. |
| 1048 | """ |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1049 | if all_refs is None: |
| 1050 | all_refs = self._allrefs |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1051 | heads = set() |
| 1052 | canrm = {} |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 1053 | for name, ref_id in all_refs.items(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1054 | if name.startswith(R_HEADS): |
| 1055 | heads.add(name) |
| 1056 | elif name.startswith(R_PUB): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1057 | canrm[name] = ref_id |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1058 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 1059 | for name, ref_id in canrm.items(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1060 | n = name[len(R_PUB):] |
| 1061 | if R_HEADS + n not in heads: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1062 | self.bare_git.DeleteRef(name, ref_id) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1063 | |
Mandeep Singh Baines | d6c93a2 | 2011-05-26 10:34:11 -0700 | [diff] [blame] | 1064 | def GetUploadableBranches(self, selected_branch=None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1065 | """List any branches which can be uploaded for review. |
| 1066 | """ |
| 1067 | heads = {} |
| 1068 | pubed = {} |
| 1069 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 1070 | for name, ref_id in self._allrefs.items(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1071 | if name.startswith(R_HEADS): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1072 | heads[name[len(R_HEADS):]] = ref_id |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1073 | elif name.startswith(R_PUB): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1074 | pubed[name[len(R_PUB):]] = ref_id |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1075 | |
| 1076 | ready = [] |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 1077 | for branch, ref_id in heads.items(): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1078 | if branch in pubed and pubed[branch] == ref_id: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1079 | continue |
Mandeep Singh Baines | d6c93a2 | 2011-05-26 10:34:11 -0700 | [diff] [blame] | 1080 | if selected_branch and branch != selected_branch: |
| 1081 | continue |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1082 | |
Shawn O. Pearce | 35f2596 | 2008-11-11 17:03:13 -0800 | [diff] [blame] | 1083 | rb = self.GetUploadableBranch(branch) |
| 1084 | if rb: |
| 1085 | ready.append(rb) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1086 | return ready |
| 1087 | |
Shawn O. Pearce | 35f2596 | 2008-11-11 17:03:13 -0800 | [diff] [blame] | 1088 | def GetUploadableBranch(self, branch_name): |
| 1089 | """Get a single uploadable branch, or None. |
| 1090 | """ |
| 1091 | branch = self.GetBranch(branch_name) |
| 1092 | base = branch.LocalMerge |
| 1093 | if branch.LocalMerge: |
| 1094 | rb = ReviewableBranch(self, branch, base) |
| 1095 | if rb.commits: |
| 1096 | return rb |
| 1097 | return None |
| 1098 | |
Shawn O. Pearce | a5ece0e | 2010-07-15 16:52:42 -0700 | [diff] [blame] | 1099 | def UploadForReview(self, branch=None, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1100 | people=([], []), |
Brian Harring | 435370c | 2012-07-28 15:37:04 -0700 | [diff] [blame] | 1101 | auto_topic=False, |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 1102 | draft=False, |
| 1103 | dest_branch=None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1104 | """Uploads the named branch for code review. |
| 1105 | """ |
| 1106 | if branch is None: |
| 1107 | branch = self.CurrentBranch |
| 1108 | if branch is None: |
| 1109 | raise GitError('not currently on a branch') |
| 1110 | |
| 1111 | branch = self.GetBranch(branch) |
| 1112 | if not branch.LocalMerge: |
| 1113 | raise GitError('branch %s does not track a remote' % branch.name) |
| 1114 | if not branch.remote.review: |
| 1115 | raise GitError('remote %s has no review url' % branch.remote.name) |
| 1116 | |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 1117 | if dest_branch is None: |
| 1118 | dest_branch = self.dest_branch |
| 1119 | if dest_branch is None: |
| 1120 | dest_branch = branch.merge |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1121 | if not dest_branch.startswith(R_HEADS): |
| 1122 | dest_branch = R_HEADS + dest_branch |
| 1123 | |
Shawn O. Pearce | 339ba9f | 2008-11-06 09:52:51 -0800 | [diff] [blame] | 1124 | if not branch.remote.projectname: |
| 1125 | branch.remote.projectname = self.name |
| 1126 | branch.remote.Save() |
| 1127 | |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 1128 | url = branch.remote.ReviewUrl(self.UserEmail) |
| 1129 | if url is None: |
| 1130 | raise UploadError('review not configured') |
| 1131 | cmd = ['push'] |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 1132 | |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 1133 | if url.startswith('ssh://'): |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 1134 | rp = ['gerrit receive-pack'] |
| 1135 | for e in people[0]: |
| 1136 | rp.append('--reviewer=%s' % sq(e)) |
| 1137 | for e in people[1]: |
| 1138 | rp.append('--cc=%s' % sq(e)) |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 1139 | cmd.append('--receive-pack=%s' % " ".join(rp)) |
Shawn O. Pearce | a5ece0e | 2010-07-15 16:52:42 -0700 | [diff] [blame] | 1140 | |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 1141 | cmd.append(url) |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 1142 | |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 1143 | if dest_branch.startswith(R_HEADS): |
| 1144 | dest_branch = dest_branch[len(R_HEADS):] |
Brian Harring | 435370c | 2012-07-28 15:37:04 -0700 | [diff] [blame] | 1145 | |
| 1146 | upload_type = 'for' |
| 1147 | if draft: |
| 1148 | upload_type = 'drafts' |
| 1149 | |
| 1150 | ref_spec = '%s:refs/%s/%s' % (R_HEADS + branch.name, upload_type, |
| 1151 | dest_branch) |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 1152 | if auto_topic: |
| 1153 | ref_spec = ref_spec + '/' + branch.name |
Shawn Pearce | 45d2168 | 2013-02-28 00:35:51 -0800 | [diff] [blame] | 1154 | if not url.startswith('ssh://'): |
| 1155 | rp = ['r=%s' % p for p in people[0]] + \ |
| 1156 | ['cc=%s' % p for p in people[1]] |
| 1157 | if rp: |
| 1158 | ref_spec = ref_spec + '%' + ','.join(rp) |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 1159 | cmd.append(ref_spec) |
| 1160 | |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1161 | if GitCommand(self, cmd, bare=True).Wait() != 0: |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 1162 | raise UploadError('Upload failed') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1163 | |
| 1164 | msg = "posted to %s for %s" % (branch.remote.review, dest_branch) |
| 1165 | self.bare_git.UpdateRef(R_PUB + branch.name, |
| 1166 | R_HEADS + branch.name, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1167 | message=msg) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1168 | |
| 1169 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1170 | # Sync ## |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1171 | |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 1172 | def _ExtractArchive(self, tarpath, path=None): |
| 1173 | """Extract the given tar on its current location |
| 1174 | |
| 1175 | Args: |
| 1176 | - tarpath: The path to the actual tar file |
| 1177 | |
| 1178 | """ |
| 1179 | try: |
| 1180 | with tarfile.open(tarpath, 'r') as tar: |
| 1181 | tar.extractall(path=path) |
| 1182 | return True |
| 1183 | except (IOError, tarfile.TarError) as e: |
David Pursehouse | f33929d | 2015-08-24 14:39:14 +0900 | [diff] [blame] | 1184 | _error("Cannot extract archive %s: %s", tarpath, str(e)) |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 1185 | return False |
| 1186 | |
Ningning Xia | c2fbc78 | 2016-08-22 14:24:39 -0700 | [diff] [blame] | 1187 | def CachePopulate(self, cache_dir, url): |
| 1188 | """Populate cache in the cache_dir. |
| 1189 | |
| 1190 | Args: |
| 1191 | cache_dir: Directory to cache git files from Google Storage. |
| 1192 | url: Git url of current repository. |
| 1193 | |
| 1194 | Raises: |
| 1195 | CacheApplyError if it fails to populate the git cache. |
| 1196 | """ |
| 1197 | cmd = ['cache', 'populate', '--ignore_locks', '-v', |
| 1198 | '--cache-dir', cache_dir, url] |
| 1199 | |
| 1200 | if GitCommand(self, cmd, cwd=cache_dir).Wait() != 0: |
| 1201 | raise CacheApplyError('Failed to populate cache. cache_dir: %s ' |
| 1202 | 'url: %s' % (cache_dir, url)) |
| 1203 | |
| 1204 | def CacheExists(self, cache_dir, url): |
| 1205 | """Check the existence of the cache files. |
| 1206 | |
| 1207 | Args: |
| 1208 | cache_dir: Directory to cache git files. |
| 1209 | url: Git url of current repository. |
| 1210 | |
| 1211 | Raises: |
| 1212 | CacheApplyError if the cache files do not exist. |
| 1213 | """ |
| 1214 | cmd = ['cache', 'exists', '--quiet', '--cache-dir', cache_dir, url] |
| 1215 | |
| 1216 | exist = GitCommand(self, cmd, cwd=self.gitdir, capture_stdout=True) |
| 1217 | if exist.Wait() != 0: |
| 1218 | raise CacheApplyError('Failed to execute git cache exists cmd. ' |
| 1219 | 'cache_dir: %s url: %s' % (cache_dir, url)) |
| 1220 | |
| 1221 | if not exist.stdout or not exist.stdout.strip(): |
| 1222 | raise CacheApplyError('Failed to find cache. cache_dir: %s ' |
| 1223 | 'url: %s' % (cache_dir, url)) |
| 1224 | return exist.stdout.strip() |
| 1225 | |
| 1226 | def CacheApply(self, cache_dir): |
| 1227 | """Apply git cache files populated from Google Storage buckets. |
| 1228 | |
| 1229 | Args: |
| 1230 | cache_dir: Directory to cache git files. |
| 1231 | |
| 1232 | Raises: |
| 1233 | CacheApplyError if it fails to apply git caches. |
| 1234 | """ |
| 1235 | remote = self.GetRemote(self.remote.name) |
| 1236 | |
| 1237 | self.CachePopulate(cache_dir, remote.url) |
| 1238 | |
| 1239 | mirror_dir = self.CacheExists(cache_dir, remote.url) |
| 1240 | |
| 1241 | refspec = RefSpec(True, 'refs/heads/*', |
| 1242 | 'refs/remotes/%s/*' % remote.name) |
| 1243 | |
| 1244 | fetch_cache_cmd = ['fetch', mirror_dir, str(refspec)] |
| 1245 | if GitCommand(self, fetch_cache_cmd, self.gitdir).Wait() != 0: |
| 1246 | raise CacheApplyError('Failed to fetch refs %s from %s' % |
| 1247 | (mirror_dir, str(refspec))) |
| 1248 | |
Shawn O. Pearce | e02ac0a | 2012-03-14 15:36:59 -0700 | [diff] [blame] | 1249 | def Sync_NetworkHalf(self, |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1250 | quiet=False, |
| 1251 | is_new=None, |
| 1252 | current_branch_only=False, |
| 1253 | force_sync=False, |
| 1254 | clone_bundle=True, |
| 1255 | no_tags=False, |
| 1256 | archive=False, |
| 1257 | optimized_fetch=False, |
Ningning Xia | c2fbc78 | 2016-08-22 14:24:39 -0700 | [diff] [blame] | 1258 | prune=False, |
| 1259 | cache_dir=None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1260 | """Perform only the network IO portion of the sync process. |
| 1261 | Local working directory/branch state is not affected. |
| 1262 | """ |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 1263 | if archive and not isinstance(self, MetaProject): |
| 1264 | if self.remote.url.startswith(('http://', 'https://')): |
David Pursehouse | f33929d | 2015-08-24 14:39:14 +0900 | [diff] [blame] | 1265 | _error("%s: Cannot fetch archives from http/https remotes.", self.name) |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 1266 | return False |
| 1267 | |
| 1268 | name = self.relpath.replace('\\', '/') |
| 1269 | name = name.replace('/', '_') |
| 1270 | tarpath = '%s.tar' % name |
| 1271 | topdir = self.manifest.topdir |
| 1272 | |
| 1273 | try: |
| 1274 | self._FetchArchive(tarpath, cwd=topdir) |
| 1275 | except GitError as e: |
David Pursehouse | f33929d | 2015-08-24 14:39:14 +0900 | [diff] [blame] | 1276 | _error('%s', e) |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 1277 | return False |
| 1278 | |
| 1279 | # From now on, we only need absolute tarpath |
| 1280 | tarpath = os.path.join(topdir, tarpath) |
| 1281 | |
| 1282 | if not self._ExtractArchive(tarpath, path=topdir): |
| 1283 | return False |
| 1284 | try: |
| 1285 | os.remove(tarpath) |
| 1286 | except OSError as e: |
David Pursehouse | f33929d | 2015-08-24 14:39:14 +0900 | [diff] [blame] | 1287 | _warn("Cannot remove archive %s: %s", tarpath, str(e)) |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1288 | self._CopyAndLinkFiles() |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 1289 | return True |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 1290 | if is_new is None: |
| 1291 | is_new = not self.Exists |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 1292 | if is_new: |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 1293 | self._InitGitDir(force_sync=force_sync) |
Jimmie Wester | a044458 | 2012-10-24 13:44:42 +0200 | [diff] [blame] | 1294 | else: |
| 1295 | self._UpdateHooks() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1296 | self._InitRemote() |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1297 | |
| 1298 | if is_new: |
| 1299 | alt = os.path.join(self.gitdir, 'objects/info/alternates') |
| 1300 | try: |
| 1301 | fd = open(alt, 'rb') |
| 1302 | try: |
| 1303 | alt_dir = fd.readline().rstrip() |
| 1304 | finally: |
| 1305 | fd.close() |
| 1306 | except IOError: |
| 1307 | alt_dir = None |
| 1308 | else: |
| 1309 | alt_dir = None |
| 1310 | |
Ningning Xia | c2fbc78 | 2016-08-22 14:24:39 -0700 | [diff] [blame] | 1311 | applied_cache = False |
| 1312 | # If cache_dir is provided, and it's a new repository without |
| 1313 | # alternative_dir, bootstrap this project repo with the git |
| 1314 | # cache files. |
| 1315 | if cache_dir is not None and is_new and alt_dir is None: |
| 1316 | try: |
| 1317 | self.CacheApply(cache_dir) |
| 1318 | applied_cache = True |
| 1319 | is_new = False |
| 1320 | except CacheApplyError as e: |
| 1321 | _error('Could not apply git cache: %s', e) |
| 1322 | _error('Please check if you have the right GS credentials.') |
| 1323 | _error('Please check if the cache files exist in GS.') |
| 1324 | |
Shawn O. Pearce | e02ac0a | 2012-03-14 15:36:59 -0700 | [diff] [blame] | 1325 | if clone_bundle \ |
Ningning Xia | c2fbc78 | 2016-08-22 14:24:39 -0700 | [diff] [blame] | 1326 | and not applied_cache \ |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1327 | and alt_dir is None \ |
| 1328 | and self._ApplyCloneBundle(initial=is_new, quiet=quiet): |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1329 | is_new = False |
| 1330 | |
Shawn O. Pearce | 6ba6ba0 | 2012-05-24 09:46:50 -0700 | [diff] [blame] | 1331 | if not current_branch_only: |
| 1332 | if self.sync_c: |
| 1333 | current_branch_only = True |
| 1334 | elif not self.manifest._loaded: |
| 1335 | # Manifest cannot check defaults until it syncs. |
| 1336 | current_branch_only = False |
| 1337 | elif self.manifest.default.sync_c: |
| 1338 | current_branch_only = True |
| 1339 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1340 | need_to_fetch = not (optimized_fetch and |
| 1341 | (ID_RE.match(self.revisionExpr) and |
| 1342 | self._CheckForSha1())) |
| 1343 | if (need_to_fetch and |
| 1344 | not self._RemoteFetch(initial=is_new, quiet=quiet, alt_dir=alt_dir, |
| 1345 | current_branch_only=current_branch_only, |
| 1346 | no_tags=no_tags, prune=prune)): |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1347 | return False |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 1348 | |
| 1349 | if self.worktree: |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 1350 | self._InitMRef() |
| 1351 | else: |
| 1352 | self._InitMirrorHead() |
| 1353 | try: |
| 1354 | os.remove(os.path.join(self.gitdir, 'FETCH_HEAD')) |
| 1355 | except OSError: |
| 1356 | pass |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1357 | return True |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 1358 | |
| 1359 | def PostRepoUpgrade(self): |
| 1360 | self._InitHooks() |
| 1361 | |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1362 | def _CopyAndLinkFiles(self): |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1363 | if self.manifest.isGitcClient: |
| 1364 | return |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1365 | for copyfile in self.copyfiles: |
| 1366 | copyfile._Copy() |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1367 | for linkfile in self.linkfiles: |
| 1368 | linkfile._Link() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1369 | |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 1370 | def GetCommitRevisionId(self): |
| 1371 | """Get revisionId of a commit. |
| 1372 | |
| 1373 | Use this method instead of GetRevisionId to get the id of the commit rather |
| 1374 | than the id of the current git object (for example, a tag) |
| 1375 | |
| 1376 | """ |
| 1377 | if not self.revisionExpr.startswith(R_TAGS): |
| 1378 | return self.GetRevisionId(self._allrefs) |
| 1379 | |
| 1380 | try: |
| 1381 | return self.bare_git.rev_list(self.revisionExpr, '-1')[0] |
| 1382 | except GitError: |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1383 | raise ManifestInvalidRevisionError('revision %s in %s not found' % |
| 1384 | (self.revisionExpr, self.name)) |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 1385 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1386 | def GetRevisionId(self, all_refs=None): |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1387 | if self.revisionId: |
| 1388 | return self.revisionId |
| 1389 | |
| 1390 | rem = self.GetRemote(self.remote.name) |
| 1391 | rev = rem.ToLocal(self.revisionExpr) |
| 1392 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1393 | if all_refs is not None and rev in all_refs: |
| 1394 | return all_refs[rev] |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1395 | |
| 1396 | try: |
| 1397 | return self.bare_git.rev_parse('--verify', '%s^0' % rev) |
| 1398 | except GitError: |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1399 | raise ManifestInvalidRevisionError('revision %s in %s not found' % |
| 1400 | (self.revisionExpr, self.name)) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1401 | |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 1402 | def Sync_LocalHalf(self, syncbuf, force_sync=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1403 | """Perform only the local IO portion of the sync process. |
| 1404 | Network access is not required. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1405 | """ |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 1406 | self._InitWorkTree(force_sync=force_sync) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1407 | all_refs = self.bare_ref.all |
| 1408 | self.CleanPublishedCache(all_refs) |
| 1409 | revid = self.GetRevisionId(all_refs) |
Skyler Kaufman | 835cd68 | 2011-03-08 12:14:41 -0800 | [diff] [blame] | 1410 | |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 1411 | def _doff(): |
| 1412 | self._FastForward(revid) |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1413 | self._CopyAndLinkFiles() |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 1414 | |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1415 | head = self.work_git.GetHead() |
| 1416 | if head.startswith(R_HEADS): |
| 1417 | branch = head[len(R_HEADS):] |
| 1418 | try: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1419 | head = all_refs[head] |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1420 | except KeyError: |
| 1421 | head = None |
| 1422 | else: |
| 1423 | branch = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1424 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1425 | if branch is None or syncbuf.detach_head: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1426 | # Currently on a detached HEAD. The user is assumed to |
| 1427 | # not have any local modifications worth worrying about. |
| 1428 | # |
Shawn O. Pearce | 3d2cdd0 | 2009-04-18 15:26:10 -0700 | [diff] [blame] | 1429 | if self.IsRebaseInProgress(): |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1430 | syncbuf.fail(self, _PriorSyncFailedError()) |
| 1431 | return |
| 1432 | |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1433 | if head == revid: |
| 1434 | # No changes; don't do anything further. |
Florian Vallee | 7cf1b36 | 2012-06-07 17:11:42 +0200 | [diff] [blame] | 1435 | # Except if the head needs to be detached |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1436 | # |
Florian Vallee | 7cf1b36 | 2012-06-07 17:11:42 +0200 | [diff] [blame] | 1437 | if not syncbuf.detach_head: |
Dan Willemsen | 029eaf3 | 2015-09-03 12:52:28 -0700 | [diff] [blame] | 1438 | # The copy/linkfile config may have changed. |
| 1439 | self._CopyAndLinkFiles() |
Florian Vallee | 7cf1b36 | 2012-06-07 17:11:42 +0200 | [diff] [blame] | 1440 | return |
| 1441 | else: |
| 1442 | lost = self._revlist(not_rev(revid), HEAD) |
| 1443 | if lost: |
| 1444 | syncbuf.info(self, "discarding %d commits", len(lost)) |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1445 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1446 | try: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1447 | self._Checkout(revid, quiet=True) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 1448 | except GitError as e: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1449 | syncbuf.fail(self, e) |
| 1450 | return |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1451 | self._CopyAndLinkFiles() |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1452 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1453 | |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1454 | if head == revid: |
| 1455 | # No changes; don't do anything further. |
| 1456 | # |
Dan Willemsen | 029eaf3 | 2015-09-03 12:52:28 -0700 | [diff] [blame] | 1457 | # The copy/linkfile config may have changed. |
| 1458 | self._CopyAndLinkFiles() |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1459 | return |
| 1460 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1461 | branch = self.GetBranch(branch) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1462 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1463 | if not branch.LocalMerge: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1464 | # The current branch has no tracking configuration. |
Anatol Pomazau | 2a32f6a | 2011-08-30 10:52:33 -0700 | [diff] [blame] | 1465 | # Jump off it to a detached HEAD. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1466 | # |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1467 | syncbuf.info(self, |
| 1468 | "leaving %s; does not track upstream", |
| 1469 | branch.name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1470 | try: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1471 | self._Checkout(revid, quiet=True) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 1472 | except GitError as e: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1473 | syncbuf.fail(self, e) |
| 1474 | return |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1475 | self._CopyAndLinkFiles() |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1476 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1477 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1478 | upstream_gain = self._revlist(not_rev(HEAD), revid) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1479 | pub = self.WasPublished(branch.name, all_refs) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1480 | if pub: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1481 | not_merged = self._revlist(not_rev(revid), pub) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1482 | if not_merged: |
| 1483 | if upstream_gain: |
| 1484 | # The user has published this branch and some of those |
| 1485 | # commits are not yet merged upstream. We do not want |
| 1486 | # to rewrite the published commits so we punt. |
| 1487 | # |
Daniel Sandler | 4c50dee | 2010-03-02 15:38:03 -0500 | [diff] [blame] | 1488 | syncbuf.fail(self, |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1489 | "branch %s is published (but not merged) and is now " |
| 1490 | "%d commits behind" % (branch.name, len(upstream_gain))) |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1491 | return |
Shawn O. Pearce | 05f66b6 | 2009-04-21 08:26:32 -0700 | [diff] [blame] | 1492 | elif pub == head: |
| 1493 | # All published commits are merged, and thus we are a |
| 1494 | # strict subset. We can fast-forward safely. |
Shawn O. Pearce | a54c527 | 2008-10-30 11:03:00 -0700 | [diff] [blame] | 1495 | # |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1496 | syncbuf.later1(self, _doff) |
| 1497 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1498 | |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1499 | # Examine the local commits not in the remote. Find the |
| 1500 | # last one attributed to this user, if any. |
| 1501 | # |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1502 | local_changes = self._revlist(not_rev(revid), HEAD, format='%H %ce') |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1503 | last_mine = None |
| 1504 | cnt_mine = 0 |
| 1505 | for commit in local_changes: |
Chirayu Desai | 0eb35cb | 2013-11-19 18:46:29 +0530 | [diff] [blame] | 1506 | commit_id, committer_email = commit.decode('utf-8').split(' ', 1) |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1507 | if committer_email == self.UserEmail: |
| 1508 | last_mine = commit_id |
| 1509 | cnt_mine += 1 |
| 1510 | |
Shawn O. Pearce | da88ff4 | 2009-06-03 11:09:12 -0700 | [diff] [blame] | 1511 | if not upstream_gain and cnt_mine == len(local_changes): |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1512 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1513 | |
| 1514 | if self.IsDirty(consider_untracked=False): |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1515 | syncbuf.fail(self, _DirtyError()) |
| 1516 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1517 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1518 | # If the upstream switched on us, warn the user. |
| 1519 | # |
| 1520 | if branch.merge != self.revisionExpr: |
| 1521 | if branch.merge and self.revisionExpr: |
| 1522 | syncbuf.info(self, |
| 1523 | 'manifest switched %s...%s', |
| 1524 | branch.merge, |
| 1525 | self.revisionExpr) |
| 1526 | elif branch.merge: |
| 1527 | syncbuf.info(self, |
| 1528 | 'manifest no longer tracks %s', |
| 1529 | branch.merge) |
| 1530 | |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1531 | if cnt_mine < len(local_changes): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1532 | # Upstream rebased. Not everything in HEAD |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1533 | # was created by this user. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1534 | # |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1535 | syncbuf.info(self, |
| 1536 | "discarding %d commits removed from upstream", |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1537 | len(local_changes) - cnt_mine) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1538 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1539 | branch.remote = self.GetRemote(self.remote.name) |
Anatol Pomazau | cd7c5de | 2012-03-20 13:45:00 -0700 | [diff] [blame] | 1540 | if not ID_RE.match(self.revisionExpr): |
| 1541 | # in case of manifest sync the revisionExpr might be a SHA1 |
| 1542 | branch.merge = self.revisionExpr |
Conley Owens | 04f2f0e | 2014-10-01 17:22:46 -0700 | [diff] [blame] | 1543 | if not branch.merge.startswith('refs/'): |
| 1544 | branch.merge = R_HEADS + branch.merge |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1545 | branch.Save() |
| 1546 | |
Mike Pontillo | d315382 | 2012-02-28 11:53:24 -0800 | [diff] [blame] | 1547 | if cnt_mine > 0 and self.rebase: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1548 | def _dorebase(): |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1549 | self._Rebase(upstream='%s^1' % last_mine, onto=revid) |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1550 | self._CopyAndLinkFiles() |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1551 | syncbuf.later2(self, _dorebase) |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1552 | elif local_changes: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1553 | try: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1554 | self._ResetHard(revid) |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1555 | self._CopyAndLinkFiles() |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 1556 | except GitError as e: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1557 | syncbuf.fail(self, e) |
| 1558 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1559 | else: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1560 | syncbuf.later1(self, _doff) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1561 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 1562 | def AddCopyFile(self, src, dest, absdest): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1563 | # dest should already be an absolute path, but src is project relative |
| 1564 | # make src an absolute path |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 1565 | abssrc = os.path.join(self.worktree, src) |
| 1566 | self.copyfiles.append(_CopyFile(src, dest, abssrc, absdest)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1567 | |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1568 | def AddLinkFile(self, src, dest, absdest): |
| 1569 | # dest should already be an absolute path, but src is project relative |
Colin Cross | 0184dcc | 2015-05-05 00:24:54 -0700 | [diff] [blame] | 1570 | # make src relative path to dest |
| 1571 | absdestdir = os.path.dirname(absdest) |
| 1572 | relsrc = os.path.relpath(os.path.join(self.worktree, src), absdestdir) |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 1573 | self.linkfiles.append(_LinkFile(self.worktree, src, dest, relsrc, absdest)) |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1574 | |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 1575 | def AddAnnotation(self, name, value, keep): |
| 1576 | self.annotations.append(_Annotation(name, value, keep)) |
| 1577 | |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 1578 | def DownloadPatchSet(self, change_id, patch_id): |
| 1579 | """Download a single patch set of a single change to FETCH_HEAD. |
| 1580 | """ |
| 1581 | remote = self.GetRemote(self.remote.name) |
| 1582 | |
| 1583 | cmd = ['fetch', remote.name] |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1584 | cmd.append('refs/changes/%2.2d/%d/%d' |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 1585 | % (change_id % 100, change_id, patch_id)) |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 1586 | if GitCommand(self, cmd, bare=True).Wait() != 0: |
| 1587 | return None |
| 1588 | return DownloadedChange(self, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1589 | self.GetRevisionId(), |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 1590 | change_id, |
| 1591 | patch_id, |
| 1592 | self.bare_git.rev_parse('FETCH_HEAD')) |
| 1593 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1594 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1595 | # Branch Management ## |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1596 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1597 | def StartBranch(self, name, branch_merge=''): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1598 | """Create a new branch off the manifest's revision. |
| 1599 | """ |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1600 | if not branch_merge: |
| 1601 | branch_merge = self.revisionExpr |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1602 | head = self.work_git.GetHead() |
| 1603 | if head == (R_HEADS + name): |
| 1604 | return True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1605 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1606 | all_refs = self.bare_ref.all |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1607 | if R_HEADS + name in all_refs: |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1608 | return GitCommand(self, |
Shawn O. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 1609 | ['checkout', name, '--'], |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1610 | capture_stdout=True, |
| 1611 | capture_stderr=True).Wait() == 0 |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 1612 | |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1613 | branch = self.GetBranch(name) |
| 1614 | branch.remote = self.GetRemote(self.remote.name) |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1615 | branch.merge = branch_merge |
| 1616 | if not branch.merge.startswith('refs/') and not ID_RE.match(branch_merge): |
| 1617 | branch.merge = R_HEADS + branch_merge |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1618 | revid = self.GetRevisionId(all_refs) |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 1619 | |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1620 | if head.startswith(R_HEADS): |
| 1621 | try: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1622 | head = all_refs[head] |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1623 | except KeyError: |
| 1624 | head = None |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1625 | if revid and head and revid == head: |
| 1626 | ref = os.path.join(self.gitdir, R_HEADS + name) |
| 1627 | try: |
| 1628 | os.makedirs(os.path.dirname(ref)) |
| 1629 | except OSError: |
| 1630 | pass |
| 1631 | _lwrite(ref, '%s\n' % revid) |
| 1632 | _lwrite(os.path.join(self.worktree, '.git', HEAD), |
| 1633 | 'ref: %s%s\n' % (R_HEADS, name)) |
| 1634 | branch.Save() |
| 1635 | return True |
| 1636 | |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1637 | if GitCommand(self, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1638 | ['checkout', '-b', branch.name, revid], |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1639 | capture_stdout=True, |
| 1640 | capture_stderr=True).Wait() == 0: |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1641 | branch.Save() |
| 1642 | return True |
| 1643 | return False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1644 | |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 1645 | def CheckoutBranch(self, name): |
| 1646 | """Checkout a local topic branch. |
Doug Anderson | 3ba5f95 | 2011-04-07 12:51:04 -0700 | [diff] [blame] | 1647 | |
| 1648 | Args: |
| 1649 | name: The name of the branch to checkout. |
| 1650 | |
| 1651 | Returns: |
| 1652 | True if the checkout succeeded; False if it didn't; None if the branch |
| 1653 | didn't exist. |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 1654 | """ |
Shawn O. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 1655 | rev = R_HEADS + name |
| 1656 | head = self.work_git.GetHead() |
| 1657 | if head == rev: |
| 1658 | # Already on the branch |
| 1659 | # |
| 1660 | return True |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 1661 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1662 | all_refs = self.bare_ref.all |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 1663 | try: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1664 | revid = all_refs[rev] |
Shawn O. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 1665 | except KeyError: |
| 1666 | # Branch does not exist in this project |
| 1667 | # |
Doug Anderson | 3ba5f95 | 2011-04-07 12:51:04 -0700 | [diff] [blame] | 1668 | return None |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 1669 | |
Shawn O. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 1670 | if head.startswith(R_HEADS): |
| 1671 | try: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1672 | head = all_refs[head] |
Shawn O. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 1673 | except KeyError: |
| 1674 | head = None |
| 1675 | |
| 1676 | if head == revid: |
| 1677 | # Same revision; just update HEAD to point to the new |
| 1678 | # target branch, but otherwise take no other action. |
| 1679 | # |
| 1680 | _lwrite(os.path.join(self.worktree, '.git', HEAD), |
| 1681 | 'ref: %s%s\n' % (R_HEADS, name)) |
| 1682 | return True |
| 1683 | |
| 1684 | return GitCommand(self, |
| 1685 | ['checkout', name, '--'], |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1686 | capture_stdout=True, |
| 1687 | capture_stderr=True).Wait() == 0 |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 1688 | |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 1689 | def AbandonBranch(self, name): |
| 1690 | """Destroy a local topic branch. |
Doug Anderson | dafb1d6 | 2011-04-07 11:46:59 -0700 | [diff] [blame] | 1691 | |
| 1692 | Args: |
| 1693 | name: The name of the branch to abandon. |
| 1694 | |
| 1695 | Returns: |
| 1696 | True if the abandon succeeded; False if it didn't; None if the branch |
| 1697 | didn't exist. |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 1698 | """ |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 1699 | rev = R_HEADS + name |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1700 | all_refs = self.bare_ref.all |
| 1701 | if rev not in all_refs: |
Doug Anderson | dafb1d6 | 2011-04-07 11:46:59 -0700 | [diff] [blame] | 1702 | # Doesn't exist |
| 1703 | return None |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 1704 | |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 1705 | head = self.work_git.GetHead() |
| 1706 | if head == rev: |
| 1707 | # We can't destroy the branch while we are sitting |
| 1708 | # on it. Switch to a detached HEAD. |
| 1709 | # |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1710 | head = all_refs[head] |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 1711 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1712 | revid = self.GetRevisionId(all_refs) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1713 | if head == revid: |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 1714 | _lwrite(os.path.join(self.worktree, '.git', HEAD), |
| 1715 | '%s\n' % revid) |
| 1716 | else: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1717 | self._Checkout(revid, quiet=True) |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 1718 | |
| 1719 | return GitCommand(self, |
| 1720 | ['branch', '-D', name], |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1721 | capture_stdout=True, |
| 1722 | capture_stderr=True).Wait() == 0 |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 1723 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1724 | def PruneHeads(self): |
| 1725 | """Prune any topic branches already merged into upstream. |
| 1726 | """ |
| 1727 | cb = self.CurrentBranch |
| 1728 | kill = [] |
Shawn O. Pearce | 3778f9d | 2009-03-02 12:30:50 -0800 | [diff] [blame] | 1729 | left = self._allrefs |
| 1730 | for name in left.keys(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1731 | if name.startswith(R_HEADS): |
| 1732 | name = name[len(R_HEADS):] |
| 1733 | if cb is None or name != cb: |
| 1734 | kill.append(name) |
| 1735 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1736 | rev = self.GetRevisionId(left) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1737 | if cb is not None \ |
| 1738 | and not self._revlist(HEAD + '...' + rev) \ |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1739 | and not self.IsDirty(consider_untracked=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1740 | self.work_git.DetachHead(HEAD) |
| 1741 | kill.append(cb) |
| 1742 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1743 | if kill: |
Shawn O. Pearce | 5b23f24 | 2009-04-17 18:43:33 -0700 | [diff] [blame] | 1744 | old = self.bare_git.GetHead() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1745 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1746 | try: |
| 1747 | self.bare_git.DetachHead(rev) |
| 1748 | |
| 1749 | b = ['branch', '-d'] |
| 1750 | b.extend(kill) |
| 1751 | b = GitCommand(self, b, bare=True, |
| 1752 | capture_stdout=True, |
| 1753 | capture_stderr=True) |
| 1754 | b.Wait() |
| 1755 | finally: |
Dan Willemsen | 1a799d1 | 2015-12-15 13:40:05 -0800 | [diff] [blame] | 1756 | if ID_RE.match(old): |
| 1757 | self.bare_git.DetachHead(old) |
| 1758 | else: |
| 1759 | self.bare_git.SetHead(old) |
Shawn O. Pearce | 3778f9d | 2009-03-02 12:30:50 -0800 | [diff] [blame] | 1760 | left = self._allrefs |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1761 | |
Shawn O. Pearce | 3778f9d | 2009-03-02 12:30:50 -0800 | [diff] [blame] | 1762 | for branch in kill: |
| 1763 | if (R_HEADS + branch) not in left: |
| 1764 | self.CleanPublishedCache() |
| 1765 | break |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1766 | |
| 1767 | if cb and cb not in kill: |
| 1768 | kill.append(cb) |
Shawn O. Pearce | 7c6c64d | 2009-03-02 12:38:13 -0800 | [diff] [blame] | 1769 | kill.sort() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1770 | |
| 1771 | kept = [] |
| 1772 | for branch in kill: |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1773 | if R_HEADS + branch in left: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1774 | branch = self.GetBranch(branch) |
| 1775 | base = branch.LocalMerge |
| 1776 | if not base: |
| 1777 | base = rev |
| 1778 | kept.append(ReviewableBranch(self, branch, base)) |
| 1779 | return kept |
| 1780 | |
| 1781 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1782 | # Submodule Management ## |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1783 | |
| 1784 | def GetRegisteredSubprojects(self): |
| 1785 | result = [] |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1786 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1787 | def rec(subprojects): |
| 1788 | if not subprojects: |
| 1789 | return |
| 1790 | result.extend(subprojects) |
| 1791 | for p in subprojects: |
| 1792 | rec(p.subprojects) |
| 1793 | rec(self.subprojects) |
| 1794 | return result |
| 1795 | |
| 1796 | def _GetSubmodules(self): |
| 1797 | # Unfortunately we cannot call `git submodule status --recursive` here |
| 1798 | # because the working tree might not exist yet, and it cannot be used |
| 1799 | # without a working tree in its current implementation. |
| 1800 | |
| 1801 | def get_submodules(gitdir, rev): |
| 1802 | # Parse .gitmodules for submodule sub_paths and sub_urls |
| 1803 | sub_paths, sub_urls = parse_gitmodules(gitdir, rev) |
| 1804 | if not sub_paths: |
| 1805 | return [] |
| 1806 | # Run `git ls-tree` to read SHAs of submodule object, which happen to be |
| 1807 | # revision of submodule repository |
| 1808 | sub_revs = git_ls_tree(gitdir, rev, sub_paths) |
| 1809 | submodules = [] |
| 1810 | for sub_path, sub_url in zip(sub_paths, sub_urls): |
| 1811 | try: |
| 1812 | sub_rev = sub_revs[sub_path] |
| 1813 | except KeyError: |
| 1814 | # Ignore non-exist submodules |
| 1815 | continue |
| 1816 | submodules.append((sub_rev, sub_path, sub_url)) |
| 1817 | return submodules |
| 1818 | |
| 1819 | re_path = re.compile(r'^submodule\.([^.]+)\.path=(.*)$') |
| 1820 | re_url = re.compile(r'^submodule\.([^.]+)\.url=(.*)$') |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1821 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1822 | def parse_gitmodules(gitdir, rev): |
| 1823 | cmd = ['cat-file', 'blob', '%s:.gitmodules' % rev] |
| 1824 | try: |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1825 | p = GitCommand(None, cmd, capture_stdout=True, capture_stderr=True, |
| 1826 | bare=True, gitdir=gitdir) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1827 | except GitError: |
| 1828 | return [], [] |
| 1829 | if p.Wait() != 0: |
| 1830 | return [], [] |
| 1831 | |
| 1832 | gitmodules_lines = [] |
| 1833 | fd, temp_gitmodules_path = tempfile.mkstemp() |
| 1834 | try: |
| 1835 | os.write(fd, p.stdout) |
| 1836 | os.close(fd) |
| 1837 | cmd = ['config', '--file', temp_gitmodules_path, '--list'] |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1838 | p = GitCommand(None, cmd, capture_stdout=True, capture_stderr=True, |
| 1839 | bare=True, gitdir=gitdir) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1840 | if p.Wait() != 0: |
| 1841 | return [], [] |
| 1842 | gitmodules_lines = p.stdout.split('\n') |
| 1843 | except GitError: |
| 1844 | return [], [] |
| 1845 | finally: |
| 1846 | os.remove(temp_gitmodules_path) |
| 1847 | |
| 1848 | names = set() |
| 1849 | paths = {} |
| 1850 | urls = {} |
| 1851 | for line in gitmodules_lines: |
| 1852 | if not line: |
| 1853 | continue |
| 1854 | m = re_path.match(line) |
| 1855 | if m: |
| 1856 | names.add(m.group(1)) |
| 1857 | paths[m.group(1)] = m.group(2) |
| 1858 | continue |
| 1859 | m = re_url.match(line) |
| 1860 | if m: |
| 1861 | names.add(m.group(1)) |
| 1862 | urls[m.group(1)] = m.group(2) |
| 1863 | continue |
| 1864 | names = sorted(names) |
| 1865 | return ([paths.get(name, '') for name in names], |
| 1866 | [urls.get(name, '') for name in names]) |
| 1867 | |
| 1868 | def git_ls_tree(gitdir, rev, paths): |
| 1869 | cmd = ['ls-tree', rev, '--'] |
| 1870 | cmd.extend(paths) |
| 1871 | try: |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1872 | p = GitCommand(None, cmd, capture_stdout=True, capture_stderr=True, |
| 1873 | bare=True, gitdir=gitdir) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1874 | except GitError: |
| 1875 | return [] |
| 1876 | if p.Wait() != 0: |
| 1877 | return [] |
| 1878 | objects = {} |
| 1879 | for line in p.stdout.split('\n'): |
| 1880 | if not line.strip(): |
| 1881 | continue |
| 1882 | object_rev, object_path = line.split()[2:4] |
| 1883 | objects[object_path] = object_rev |
| 1884 | return objects |
| 1885 | |
| 1886 | try: |
| 1887 | rev = self.GetRevisionId() |
| 1888 | except GitError: |
| 1889 | return [] |
| 1890 | return get_submodules(self.gitdir, rev) |
| 1891 | |
| 1892 | def GetDerivedSubprojects(self): |
| 1893 | result = [] |
| 1894 | if not self.Exists: |
| 1895 | # If git repo does not exist yet, querying its submodules will |
| 1896 | # mess up its states; so return here. |
| 1897 | return result |
| 1898 | for rev, path, url in self._GetSubmodules(): |
| 1899 | name = self.manifest.GetSubprojectName(self, path) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1900 | relpath, worktree, gitdir, objdir = \ |
| 1901 | self.manifest.GetSubprojectPaths(self, name, path) |
| 1902 | project = self.manifest.paths.get(relpath) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1903 | if project: |
| 1904 | result.extend(project.GetDerivedSubprojects()) |
| 1905 | continue |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1906 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1907 | remote = RemoteSpec(self.remote.name, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1908 | url=url, |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 1909 | pushUrl=self.remote.pushUrl, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1910 | review=self.remote.review, |
| 1911 | revision=self.remote.revision) |
| 1912 | subproject = Project(manifest=self.manifest, |
| 1913 | name=name, |
| 1914 | remote=remote, |
| 1915 | gitdir=gitdir, |
| 1916 | objdir=objdir, |
| 1917 | worktree=worktree, |
| 1918 | relpath=relpath, |
Aymen Bouaziz | 2598ed0 | 2016-06-24 14:34:08 +0200 | [diff] [blame] | 1919 | revisionExpr=rev, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1920 | revisionId=rev, |
| 1921 | rebase=self.rebase, |
| 1922 | groups=self.groups, |
| 1923 | sync_c=self.sync_c, |
| 1924 | sync_s=self.sync_s, |
| 1925 | parent=self, |
| 1926 | is_derived=True) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1927 | result.append(subproject) |
| 1928 | result.extend(subproject.GetDerivedSubprojects()) |
| 1929 | return result |
| 1930 | |
| 1931 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1932 | # Direct Git Commands ## |
Chris AtLee | 2fb6466 | 2014-01-16 21:32:33 -0500 | [diff] [blame] | 1933 | def _CheckForSha1(self): |
| 1934 | try: |
| 1935 | # if revision (sha or tag) is not present then following function |
| 1936 | # throws an error. |
| 1937 | self.bare_git.rev_parse('--verify', '%s^0' % self.revisionExpr) |
| 1938 | return True |
| 1939 | except GitError: |
| 1940 | # There is no such persistent revision. We have to fetch it. |
| 1941 | return False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1942 | |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 1943 | def _FetchArchive(self, tarpath, cwd=None): |
| 1944 | cmd = ['archive', '-v', '-o', tarpath] |
| 1945 | cmd.append('--remote=%s' % self.remote.url) |
| 1946 | cmd.append('--prefix=%s/' % self.relpath) |
| 1947 | cmd.append(self.revisionExpr) |
| 1948 | |
| 1949 | command = GitCommand(self, cmd, cwd=cwd, |
| 1950 | capture_stdout=True, |
| 1951 | capture_stderr=True) |
| 1952 | |
| 1953 | if command.Wait() != 0: |
| 1954 | raise GitError('git archive %s: %s' % (self.name, command.stderr)) |
| 1955 | |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1956 | def _RemoteFetch(self, name=None, |
| 1957 | current_branch_only=False, |
Shawn O. Pearce | 16614f8 | 2010-10-29 12:05:43 -0700 | [diff] [blame] | 1958 | initial=False, |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1959 | quiet=False, |
Mitchel Humpherys | 597868b | 2012-10-29 10:18:34 -0700 | [diff] [blame] | 1960 | alt_dir=None, |
David Pursehouse | 74cfd27 | 2015-10-14 10:50:15 +0900 | [diff] [blame] | 1961 | no_tags=False, |
| 1962 | prune=False): |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1963 | |
| 1964 | is_sha1 = False |
| 1965 | tag_name = None |
David Pursehouse | 9bc422f | 2014-04-15 10:28:56 +0900 | [diff] [blame] | 1966 | depth = None |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1967 | |
David Pursehouse | 9bc422f | 2014-04-15 10:28:56 +0900 | [diff] [blame] | 1968 | # The depth should not be used when fetching to a mirror because |
| 1969 | # it will result in a shallow repository that cannot be cloned or |
| 1970 | # fetched from. |
| 1971 | if not self.manifest.IsMirror: |
| 1972 | if self.clone_depth: |
| 1973 | depth = self.clone_depth |
| 1974 | else: |
| 1975 | depth = self.manifest.manifestProject.config.GetString('repo.depth') |
Conley Owens | e4978cf | 2015-02-03 18:06:16 -0800 | [diff] [blame] | 1976 | # The repo project should never be synced with partial depth |
| 1977 | if self.relpath == '.repo/repo': |
| 1978 | depth = None |
David Pursehouse | 9bc422f | 2014-04-15 10:28:56 +0900 | [diff] [blame] | 1979 | |
Shawn Pearce | 69e04d8 | 2014-01-29 12:48:54 -0800 | [diff] [blame] | 1980 | if depth: |
| 1981 | current_branch_only = True |
| 1982 | |
Nasser Grainawi | 909d58b | 2014-09-19 12:13:04 -0600 | [diff] [blame] | 1983 | if ID_RE.match(self.revisionExpr) is not None: |
| 1984 | is_sha1 = True |
| 1985 | |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1986 | if current_branch_only: |
Nasser Grainawi | 909d58b | 2014-09-19 12:13:04 -0600 | [diff] [blame] | 1987 | if self.revisionExpr.startswith(R_TAGS): |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1988 | # this is a tag and its sha1 value should never change |
| 1989 | tag_name = self.revisionExpr[len(R_TAGS):] |
| 1990 | |
| 1991 | if is_sha1 or tag_name is not None: |
Chris AtLee | 2fb6466 | 2014-01-16 21:32:33 -0500 | [diff] [blame] | 1992 | if self._CheckForSha1(): |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1993 | return True |
Bertrand SIMONNET | 3000cda | 2014-11-25 16:19:29 -0800 | [diff] [blame] | 1994 | if is_sha1 and not depth: |
| 1995 | # When syncing a specific commit and --depth is not set: |
| 1996 | # * if upstream is explicitly specified and is not a sha1, fetch only |
| 1997 | # upstream as users expect only upstream to be fetch. |
| 1998 | # Note: The commit might not be in upstream in which case the sync |
| 1999 | # will fail. |
| 2000 | # * otherwise, fetch all branches to make sure we end up with the |
| 2001 | # specific commit. |
Aymen Bouaziz | 037040f | 2016-06-28 12:27:23 +0200 | [diff] [blame] | 2002 | if self.upstream: |
| 2003 | current_branch_only = not ID_RE.match(self.upstream) |
| 2004 | else: |
| 2005 | current_branch_only = False |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 2006 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2007 | if not name: |
| 2008 | name = self.remote.name |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 2009 | |
| 2010 | ssh_proxy = False |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 2011 | remote = self.GetRemote(name) |
| 2012 | if remote.PreConnectFetch(): |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 2013 | ssh_proxy = True |
| 2014 | |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2015 | if initial: |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2016 | if alt_dir and 'objects' == os.path.basename(alt_dir): |
| 2017 | ref_dir = os.path.dirname(alt_dir) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2018 | packed_refs = os.path.join(self.gitdir, 'packed-refs') |
| 2019 | remote = self.GetRemote(name) |
| 2020 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 2021 | all_refs = self.bare_ref.all |
| 2022 | ids = set(all_refs.values()) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2023 | tmp = set() |
| 2024 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 2025 | for r, ref_id in GitRefs(ref_dir).all.items(): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 2026 | if r not in all_refs: |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2027 | if r.startswith(R_TAGS) or remote.WritesTo(r): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 2028 | all_refs[r] = ref_id |
| 2029 | ids.add(ref_id) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2030 | continue |
| 2031 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 2032 | if ref_id in ids: |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2033 | continue |
| 2034 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 2035 | r = 'refs/_alt/%s' % ref_id |
| 2036 | all_refs[r] = ref_id |
| 2037 | ids.add(ref_id) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2038 | tmp.add(r) |
| 2039 | |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2040 | tmp_packed = '' |
| 2041 | old_packed = '' |
| 2042 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 2043 | for r in sorted(all_refs): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 2044 | line = '%s %s\n' % (all_refs[r], r) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2045 | tmp_packed += line |
| 2046 | if r not in tmp: |
| 2047 | old_packed += line |
| 2048 | |
| 2049 | _lwrite(packed_refs, tmp_packed) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2050 | else: |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2051 | alt_dir = None |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2052 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 2053 | cmd = ['fetch'] |
Doug Anderson | 30d4529 | 2011-05-04 15:01:04 -0700 | [diff] [blame] | 2054 | |
Conley Owens | f97e838 | 2015-01-21 11:12:46 -0800 | [diff] [blame] | 2055 | if depth: |
Doug Anderson | 30d4529 | 2011-05-04 15:01:04 -0700 | [diff] [blame] | 2056 | cmd.append('--depth=%s' % depth) |
Dan Willemsen | eeab686 | 2015-08-03 13:11:53 -0700 | [diff] [blame] | 2057 | else: |
| 2058 | # If this repo has shallow objects, then we don't know which refs have |
| 2059 | # shallow objects or not. Tell git to unshallow all fetched refs. Don't |
| 2060 | # do this with projects that don't have shallow objects, since it is less |
| 2061 | # efficient. |
| 2062 | if os.path.exists(os.path.join(self.gitdir, 'shallow')): |
| 2063 | cmd.append('--depth=2147483647') |
Doug Anderson | 30d4529 | 2011-05-04 15:01:04 -0700 | [diff] [blame] | 2064 | |
Shawn O. Pearce | 16614f8 | 2010-10-29 12:05:43 -0700 | [diff] [blame] | 2065 | if quiet: |
| 2066 | cmd.append('--quiet') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 2067 | if not self.worktree: |
| 2068 | cmd.append('--update-head-ok') |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2069 | cmd.append(name) |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 2070 | |
Mitchel Humpherys | 26c45a7 | 2014-03-10 14:21:59 -0700 | [diff] [blame] | 2071 | # If using depth then we should not get all the tags since they may |
| 2072 | # be outside of the depth. |
| 2073 | if no_tags or depth: |
| 2074 | cmd.append('--no-tags') |
| 2075 | else: |
| 2076 | cmd.append('--tags') |
| 2077 | |
David Pursehouse | 74cfd27 | 2015-10-14 10:50:15 +0900 | [diff] [blame] | 2078 | if prune: |
| 2079 | cmd.append('--prune') |
| 2080 | |
Conley Owens | 80b87fe | 2014-05-09 17:13:44 -0700 | [diff] [blame] | 2081 | spec = [] |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 2082 | if not current_branch_only: |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 2083 | # Fetch whole repo |
Conley Owens | 80b87fe | 2014-05-09 17:13:44 -0700 | [diff] [blame] | 2084 | spec.append(str((u'+refs/heads/*:') + remote.ToLocal('refs/heads/*'))) |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 2085 | elif tag_name is not None: |
Conley Owens | 80b87fe | 2014-05-09 17:13:44 -0700 | [diff] [blame] | 2086 | spec.append('tag') |
| 2087 | spec.append(tag_name) |
Nasser Grainawi | 04e52d6 | 2014-09-30 13:34:52 -0600 | [diff] [blame] | 2088 | |
David Pursehouse | 403b64e | 2015-04-27 10:41:33 +0900 | [diff] [blame] | 2089 | if not self.manifest.IsMirror: |
| 2090 | branch = self.revisionExpr |
Kevin Degi | 679bac4 | 2015-06-22 15:31:26 -0600 | [diff] [blame] | 2091 | if is_sha1 and depth and git_require((1, 8, 3)): |
David Pursehouse | 403b64e | 2015-04-27 10:41:33 +0900 | [diff] [blame] | 2092 | # Shallow checkout of a specific commit, fetch from that commit and not |
| 2093 | # the heads only as the commit might be deeper in the history. |
| 2094 | spec.append(branch) |
| 2095 | else: |
| 2096 | if is_sha1: |
| 2097 | branch = self.upstream |
| 2098 | if branch is not None and branch.strip(): |
| 2099 | if not branch.startswith('refs/'): |
| 2100 | branch = R_HEADS + branch |
| 2101 | spec.append(str((u'+%s:' % branch) + remote.ToLocal(branch))) |
Conley Owens | 80b87fe | 2014-05-09 17:13:44 -0700 | [diff] [blame] | 2102 | cmd.extend(spec) |
| 2103 | |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2104 | ok = False |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 2105 | for _i in range(2): |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 2106 | gitcmd = GitCommand(self, cmd, bare=True, ssh_proxy=ssh_proxy) |
John L. Villalovos | 126e298 | 2015-01-29 21:58:12 -0800 | [diff] [blame] | 2107 | ret = gitcmd.Wait() |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 2108 | if ret == 0: |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2109 | ok = True |
| 2110 | break |
John L. Villalovos | 126e298 | 2015-01-29 21:58:12 -0800 | [diff] [blame] | 2111 | # If needed, run the 'git remote prune' the first time through the loop |
| 2112 | elif (not _i and |
| 2113 | "error:" in gitcmd.stderr and |
| 2114 | "git remote prune" in gitcmd.stderr): |
| 2115 | prunecmd = GitCommand(self, ['remote', 'prune', name], bare=True, |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 2116 | ssh_proxy=ssh_proxy) |
John L. Villalovos | e30f46b | 2015-02-25 14:27:02 -0800 | [diff] [blame] | 2117 | ret = prunecmd.Wait() |
John L. Villalovos | e30f46b | 2015-02-25 14:27:02 -0800 | [diff] [blame] | 2118 | if ret: |
John L. Villalovos | 126e298 | 2015-01-29 21:58:12 -0800 | [diff] [blame] | 2119 | break |
| 2120 | continue |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 2121 | elif current_branch_only and is_sha1 and ret == 128: |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2122 | # Exit code 128 means "couldn't find the ref you asked for"; if we're |
| 2123 | # in sha1 mode, we just tried sync'ing from the upstream field; it |
| 2124 | # doesn't exist, thus abort the optimization attempt and do a full sync. |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 2125 | break |
Colin Cross | c4b301f | 2015-05-13 00:10:02 -0700 | [diff] [blame] | 2126 | elif ret < 0: |
| 2127 | # Git died with a signal, exit immediately |
| 2128 | break |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2129 | time.sleep(random.randint(30, 45)) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2130 | |
| 2131 | if initial: |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2132 | if alt_dir: |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2133 | if old_packed != '': |
| 2134 | _lwrite(packed_refs, old_packed) |
| 2135 | else: |
| 2136 | os.remove(packed_refs) |
| 2137 | self.bare_git.pack_refs('--all', '--prune') |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 2138 | |
| 2139 | if is_sha1 and current_branch_only and self.upstream: |
| 2140 | # We just synced the upstream given branch; verify we |
| 2141 | # got what we wanted, else trigger a second run of all |
| 2142 | # refs. |
Chris AtLee | 2fb6466 | 2014-01-16 21:32:33 -0500 | [diff] [blame] | 2143 | if not self._CheckForSha1(): |
Kevin Degi | 679bac4 | 2015-06-22 15:31:26 -0600 | [diff] [blame] | 2144 | if not depth: |
| 2145 | # Avoid infinite recursion when depth is True (since depth implies |
| 2146 | # current_branch_only) |
| 2147 | return self._RemoteFetch(name=name, current_branch_only=False, |
| 2148 | initial=False, quiet=quiet, alt_dir=alt_dir) |
| 2149 | if self.clone_depth: |
| 2150 | self.clone_depth = None |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2151 | return self._RemoteFetch(name=name, |
| 2152 | current_branch_only=current_branch_only, |
Kevin Degi | 679bac4 | 2015-06-22 15:31:26 -0600 | [diff] [blame] | 2153 | initial=False, quiet=quiet, alt_dir=alt_dir) |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 2154 | |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2155 | return ok |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2156 | |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2157 | def _ApplyCloneBundle(self, initial=False, quiet=False): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2158 | if initial and \ |
| 2159 | (self.manifest.manifestProject.config.GetString('repo.depth') or |
| 2160 | self.clone_depth): |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2161 | return False |
| 2162 | |
| 2163 | remote = self.GetRemote(self.remote.name) |
| 2164 | bundle_url = remote.url + '/clone.bundle' |
| 2165 | bundle_url = GitConfig.ForUser().UrlInsteadOf(bundle_url) |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2166 | if GetSchemeFromUrl(bundle_url) not in ('http', 'https', |
| 2167 | 'persistent-http', |
| 2168 | 'persistent-https'): |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2169 | return False |
| 2170 | |
| 2171 | bundle_dst = os.path.join(self.gitdir, 'clone.bundle') |
| 2172 | bundle_tmp = os.path.join(self.gitdir, 'clone.bundle.tmp') |
| 2173 | |
| 2174 | exist_dst = os.path.exists(bundle_dst) |
| 2175 | exist_tmp = os.path.exists(bundle_tmp) |
| 2176 | |
| 2177 | if not initial and not exist_dst and not exist_tmp: |
| 2178 | return False |
| 2179 | |
| 2180 | if not exist_dst: |
| 2181 | exist_dst = self._FetchBundle(bundle_url, bundle_tmp, bundle_dst, quiet) |
| 2182 | if not exist_dst: |
| 2183 | return False |
| 2184 | |
| 2185 | cmd = ['fetch'] |
| 2186 | if quiet: |
| 2187 | cmd.append('--quiet') |
| 2188 | if not self.worktree: |
| 2189 | cmd.append('--update-head-ok') |
| 2190 | cmd.append(bundle_dst) |
| 2191 | for f in remote.fetch: |
| 2192 | cmd.append(str(f)) |
| 2193 | cmd.append('refs/tags/*:refs/tags/*') |
| 2194 | |
| 2195 | ok = GitCommand(self, cmd, bare=True).Wait() == 0 |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 2196 | if os.path.exists(bundle_dst): |
| 2197 | os.remove(bundle_dst) |
| 2198 | if os.path.exists(bundle_tmp): |
| 2199 | os.remove(bundle_tmp) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2200 | return ok |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2201 | |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2202 | def _FetchBundle(self, srcUrl, tmpPath, dstPath, quiet): |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 2203 | if os.path.exists(dstPath): |
| 2204 | os.remove(dstPath) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 2205 | |
Matt Gumbel | 2dc810c | 2012-08-30 09:39:36 -0700 | [diff] [blame] | 2206 | cmd = ['curl', '--fail', '--output', tmpPath, '--netrc', '--location'] |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 2207 | if quiet: |
| 2208 | cmd += ['--silent'] |
| 2209 | if os.path.exists(tmpPath): |
| 2210 | size = os.stat(tmpPath).st_size |
| 2211 | if size >= 1024: |
| 2212 | cmd += ['--continue-at', '%d' % (size,)] |
| 2213 | else: |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 2214 | os.remove(tmpPath) |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 2215 | if 'http_proxy' in os.environ and 'darwin' == sys.platform: |
| 2216 | cmd += ['--proxy', os.environ['http_proxy']] |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2217 | with GetUrlCookieFile(srcUrl, quiet) as (cookiefile, _proxy): |
Dave Borowitz | 137d013 | 2015-01-02 11:12:54 -0800 | [diff] [blame] | 2218 | if cookiefile: |
Dave Borowitz | 4abf8e6 | 2015-01-02 11:39:04 -0800 | [diff] [blame] | 2219 | cmd += ['--cookie', cookiefile, '--cookie-jar', cookiefile] |
Dave Borowitz | 137d013 | 2015-01-02 11:12:54 -0800 | [diff] [blame] | 2220 | if srcUrl.startswith('persistent-'): |
| 2221 | srcUrl = srcUrl[len('persistent-'):] |
| 2222 | cmd += [srcUrl] |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 2223 | |
Dave Borowitz | 137d013 | 2015-01-02 11:12:54 -0800 | [diff] [blame] | 2224 | if IsTrace(): |
| 2225 | Trace('%s', ' '.join(cmd)) |
| 2226 | try: |
| 2227 | proc = subprocess.Popen(cmd) |
| 2228 | except OSError: |
| 2229 | return False |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 2230 | |
Dave Borowitz | 137d013 | 2015-01-02 11:12:54 -0800 | [diff] [blame] | 2231 | curlret = proc.wait() |
Matt Gumbel | 2dc810c | 2012-08-30 09:39:36 -0700 | [diff] [blame] | 2232 | |
Dave Borowitz | 137d013 | 2015-01-02 11:12:54 -0800 | [diff] [blame] | 2233 | if curlret == 22: |
| 2234 | # From curl man page: |
| 2235 | # 22: HTTP page not retrieved. The requested url was not found or |
| 2236 | # returned another error with the HTTP error code being 400 or above. |
| 2237 | # This return code only appears if -f, --fail is used. |
| 2238 | if not quiet: |
| 2239 | print("Server does not provide clone.bundle; ignoring.", |
| 2240 | file=sys.stderr) |
| 2241 | return False |
Matt Gumbel | 2dc810c | 2012-08-30 09:39:36 -0700 | [diff] [blame] | 2242 | |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 2243 | if os.path.exists(tmpPath): |
Kris Giesing | c8d882a | 2014-12-23 13:02:32 -0800 | [diff] [blame] | 2244 | if curlret == 0 and self._IsValidBundle(tmpPath, quiet): |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 2245 | os.rename(tmpPath, dstPath) |
| 2246 | return True |
| 2247 | else: |
| 2248 | os.remove(tmpPath) |
| 2249 | return False |
| 2250 | else: |
| 2251 | return False |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 2252 | |
Kris Giesing | c8d882a | 2014-12-23 13:02:32 -0800 | [diff] [blame] | 2253 | def _IsValidBundle(self, path, quiet): |
Dave Borowitz | 91f3ba5 | 2013-06-03 12:15:23 -0700 | [diff] [blame] | 2254 | try: |
| 2255 | with open(path) as f: |
| 2256 | if f.read(16) == '# v2 git bundle\n': |
| 2257 | return True |
| 2258 | else: |
Kris Giesing | c8d882a | 2014-12-23 13:02:32 -0800 | [diff] [blame] | 2259 | if not quiet: |
| 2260 | print("Invalid clone.bundle file; ignoring.", file=sys.stderr) |
Dave Borowitz | 91f3ba5 | 2013-06-03 12:15:23 -0700 | [diff] [blame] | 2261 | return False |
| 2262 | except OSError: |
| 2263 | return False |
| 2264 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2265 | def _Checkout(self, rev, quiet=False): |
| 2266 | cmd = ['checkout'] |
| 2267 | if quiet: |
| 2268 | cmd.append('-q') |
| 2269 | cmd.append(rev) |
| 2270 | cmd.append('--') |
| 2271 | if GitCommand(self, cmd).Wait() != 0: |
| 2272 | if self._allrefs: |
| 2273 | raise GitError('%s checkout %s ' % (self.name, rev)) |
| 2274 | |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2275 | def _CherryPick(self, rev): |
Pierre Tardy | e5a2122 | 2011-03-24 16:28:18 +0100 | [diff] [blame] | 2276 | cmd = ['cherry-pick'] |
| 2277 | cmd.append(rev) |
| 2278 | cmd.append('--') |
| 2279 | if GitCommand(self, cmd).Wait() != 0: |
| 2280 | if self._allrefs: |
| 2281 | raise GitError('%s cherry-pick %s ' % (self.name, rev)) |
| 2282 | |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2283 | def _Revert(self, rev): |
Erwan Mahe | a94f162 | 2011-08-19 13:56:09 +0200 | [diff] [blame] | 2284 | cmd = ['revert'] |
| 2285 | cmd.append('--no-edit') |
| 2286 | cmd.append(rev) |
| 2287 | cmd.append('--') |
| 2288 | if GitCommand(self, cmd).Wait() != 0: |
| 2289 | if self._allrefs: |
| 2290 | raise GitError('%s revert %s ' % (self.name, rev)) |
| 2291 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2292 | def _ResetHard(self, rev, quiet=True): |
| 2293 | cmd = ['reset', '--hard'] |
| 2294 | if quiet: |
| 2295 | cmd.append('-q') |
| 2296 | cmd.append(rev) |
| 2297 | if GitCommand(self, cmd).Wait() != 0: |
| 2298 | raise GitError('%s reset --hard %s ' % (self.name, rev)) |
| 2299 | |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2300 | def _Rebase(self, upstream, onto=None): |
Shawn O. Pearce | 19a83d8 | 2009-04-16 08:14:26 -0700 | [diff] [blame] | 2301 | cmd = ['rebase'] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2302 | if onto is not None: |
| 2303 | cmd.extend(['--onto', onto]) |
| 2304 | cmd.append(upstream) |
Shawn O. Pearce | 19a83d8 | 2009-04-16 08:14:26 -0700 | [diff] [blame] | 2305 | if GitCommand(self, cmd).Wait() != 0: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2306 | raise GitError('%s rebase %s ' % (self.name, upstream)) |
| 2307 | |
Pierre Tardy | 3d12594 | 2012-05-04 12:18:12 +0200 | [diff] [blame] | 2308 | def _FastForward(self, head, ffonly=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2309 | cmd = ['merge', head] |
Pierre Tardy | 3d12594 | 2012-05-04 12:18:12 +0200 | [diff] [blame] | 2310 | if ffonly: |
| 2311 | cmd.append("--ff-only") |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2312 | if GitCommand(self, cmd).Wait() != 0: |
| 2313 | raise GitError('%s merge %s ' % (self.name, head)) |
| 2314 | |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 2315 | def _InitGitDir(self, mirror_git=None, force_sync=False): |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2316 | init_git_dir = not os.path.exists(self.gitdir) |
| 2317 | init_obj_dir = not os.path.exists(self.objdir) |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2318 | try: |
| 2319 | # Initialize the bare repository, which contains all of the objects. |
| 2320 | if init_obj_dir: |
| 2321 | os.makedirs(self.objdir) |
| 2322 | self.bare_objdir.init() |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2323 | |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2324 | # If we have a separate directory to hold refs, initialize it as well. |
| 2325 | if self.objdir != self.gitdir: |
| 2326 | if init_git_dir: |
| 2327 | os.makedirs(self.gitdir) |
| 2328 | |
| 2329 | if init_obj_dir or init_git_dir: |
| 2330 | self._ReferenceGitDir(self.objdir, self.gitdir, share_refs=False, |
| 2331 | copy_all=True) |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 2332 | try: |
| 2333 | self._CheckDirReference(self.objdir, self.gitdir, share_refs=False) |
| 2334 | except GitError as e: |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 2335 | if force_sync: |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2336 | print("Retrying clone after deleting %s" % |
| 2337 | self.gitdir, file=sys.stderr) |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 2338 | try: |
| 2339 | shutil.rmtree(os.path.realpath(self.gitdir)) |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2340 | if self.worktree and os.path.exists(os.path.realpath |
| 2341 | (self.worktree)): |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 2342 | shutil.rmtree(os.path.realpath(self.worktree)) |
| 2343 | return self._InitGitDir(mirror_git=mirror_git, force_sync=False) |
| 2344 | except: |
| 2345 | raise e |
| 2346 | raise e |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2347 | |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2348 | if init_git_dir: |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2349 | mp = self.manifest.manifestProject |
| 2350 | ref_dir = mp.config.GetString('repo.reference') or '' |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2351 | |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2352 | if ref_dir or mirror_git: |
| 2353 | if not mirror_git: |
| 2354 | mirror_git = os.path.join(ref_dir, self.name + '.git') |
| 2355 | repo_git = os.path.join(ref_dir, '.repo', 'projects', |
| 2356 | self.relpath + '.git') |
Shawn O. Pearce | 2816d4f | 2009-03-03 17:53:18 -0800 | [diff] [blame] | 2357 | |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2358 | if os.path.exists(mirror_git): |
| 2359 | ref_dir = mirror_git |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2360 | |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2361 | elif os.path.exists(repo_git): |
| 2362 | ref_dir = repo_git |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2363 | |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2364 | else: |
| 2365 | ref_dir = None |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2366 | |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2367 | if ref_dir: |
| 2368 | _lwrite(os.path.join(self.gitdir, 'objects/info/alternates'), |
| 2369 | os.path.join(ref_dir, 'objects') + '\n') |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2370 | |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2371 | self._UpdateHooks() |
| 2372 | |
| 2373 | m = self.manifest.manifestProject.config |
| 2374 | for key in ['user.name', 'user.email']: |
| 2375 | if m.Has(key, include_defaults=False): |
| 2376 | self.config.SetString(key, m.GetString(key)) |
David Pursehouse | 76a4a9d | 2016-08-16 12:11:12 +0900 | [diff] [blame] | 2377 | self.config.SetString('filter.lfs.smudge', 'git-lfs smudge --skip -- %f') |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2378 | if self.manifest.IsMirror: |
| 2379 | self.config.SetString('core.bare', 'true') |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2380 | else: |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2381 | self.config.SetString('core.bare', None) |
| 2382 | except Exception: |
| 2383 | if init_obj_dir and os.path.exists(self.objdir): |
| 2384 | shutil.rmtree(self.objdir) |
| 2385 | if init_git_dir and os.path.exists(self.gitdir): |
| 2386 | shutil.rmtree(self.gitdir) |
| 2387 | raise |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2388 | |
Jimmie Wester | a044458 | 2012-10-24 13:44:42 +0200 | [diff] [blame] | 2389 | def _UpdateHooks(self): |
| 2390 | if os.path.exists(self.gitdir): |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 2391 | self._InitHooks() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2392 | |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 2393 | def _InitHooks(self): |
Jesse Hall | 672cc49 | 2013-11-27 11:17:13 -0800 | [diff] [blame] | 2394 | hooks = os.path.realpath(self._gitdir_path('hooks')) |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 2395 | if not os.path.exists(hooks): |
| 2396 | os.makedirs(hooks) |
Jonathan Nieder | 9371979 | 2015-03-17 11:29:58 -0700 | [diff] [blame] | 2397 | for stock_hook in _ProjectHooks(): |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 2398 | name = os.path.basename(stock_hook) |
| 2399 | |
Victor Boivie | 65e0f35 | 2011-04-18 11:23:29 +0200 | [diff] [blame] | 2400 | if name in ('commit-msg',) and not self.remote.review \ |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2401 | and self is not self.manifest.manifestProject: |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 2402 | # Don't install a Gerrit Code Review hook if this |
| 2403 | # project does not appear to use it for reviews. |
| 2404 | # |
Victor Boivie | 65e0f35 | 2011-04-18 11:23:29 +0200 | [diff] [blame] | 2405 | # Since the manifest project is one of those, but also |
| 2406 | # managed through gerrit, it's excluded |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 2407 | continue |
| 2408 | |
| 2409 | dst = os.path.join(hooks, name) |
| 2410 | if os.path.islink(dst): |
| 2411 | continue |
| 2412 | if os.path.exists(dst): |
| 2413 | if filecmp.cmp(stock_hook, dst, shallow=False): |
| 2414 | os.remove(dst) |
| 2415 | else: |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2416 | _warn("%s: Not replacing locally modified %s hook", |
| 2417 | self.relpath, name) |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 2418 | continue |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 2419 | try: |
Mickaël Salaün | b9477bc | 2012-08-05 13:39:26 +0200 | [diff] [blame] | 2420 | os.symlink(os.path.relpath(stock_hook, os.path.dirname(dst)), dst) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 2421 | except OSError as e: |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 2422 | if e.errno == errno.EPERM: |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 2423 | raise GitError('filesystem must support symlinks') |
| 2424 | else: |
| 2425 | raise |
| 2426 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2427 | def _InitRemote(self): |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 2428 | if self.remote.url: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2429 | remote = self.GetRemote(self.remote.name) |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 2430 | remote.url = self.remote.url |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 2431 | remote.pushUrl = self.remote.pushUrl |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 2432 | remote.review = self.remote.review |
| 2433 | remote.projectname = self.name |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2434 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 2435 | if self.worktree: |
| 2436 | remote.ResetFetch(mirror=False) |
| 2437 | else: |
| 2438 | remote.ResetFetch(mirror=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2439 | remote.Save() |
| 2440 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2441 | def _InitMRef(self): |
| 2442 | if self.manifest.branch: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 2443 | self._InitAnyMRef(R_M + self.manifest.branch) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2444 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 2445 | def _InitMirrorHead(self): |
Shawn O. Pearce | fe200ee | 2009-06-01 15:28:21 -0700 | [diff] [blame] | 2446 | self._InitAnyMRef(HEAD) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 2447 | |
| 2448 | def _InitAnyMRef(self, ref): |
| 2449 | cur = self.bare_ref.symref(ref) |
| 2450 | |
| 2451 | if self.revisionId: |
| 2452 | if cur != '' or self.bare_ref.get(ref) != self.revisionId: |
| 2453 | msg = 'manifest set to %s' % self.revisionId |
| 2454 | dst = self.revisionId + '^0' |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2455 | self.bare_git.UpdateRef(ref, dst, message=msg, detach=True) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 2456 | else: |
| 2457 | remote = self.GetRemote(self.remote.name) |
| 2458 | dst = remote.ToLocal(self.revisionExpr) |
| 2459 | if cur != dst: |
| 2460 | msg = 'manifest set to %s' % self.revisionExpr |
| 2461 | self.bare_git.symbolic_ref('-m', msg, ref, dst) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 2462 | |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2463 | def _CheckDirReference(self, srcdir, destdir, share_refs): |
Dan Willemsen | bdb866e | 2016-04-05 17:22:02 -0700 | [diff] [blame] | 2464 | symlink_files = self.shareable_files[:] |
| 2465 | symlink_dirs = self.shareable_dirs[:] |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2466 | if share_refs: |
| 2467 | symlink_files += self.working_tree_files |
| 2468 | symlink_dirs += self.working_tree_dirs |
| 2469 | to_symlink = symlink_files + symlink_dirs |
| 2470 | for name in set(to_symlink): |
| 2471 | dst = os.path.realpath(os.path.join(destdir, name)) |
| 2472 | if os.path.lexists(dst): |
| 2473 | src = os.path.realpath(os.path.join(srcdir, name)) |
| 2474 | # Fail if the links are pointing to the wrong place |
| 2475 | if src != dst: |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 2476 | raise GitError('--force-sync not enabled; cannot overwrite a local ' |
Simon Ruggier | f9b7683 | 2015-07-31 17:18:34 -0400 | [diff] [blame] | 2477 | 'work tree. If you\'re comfortable with the ' |
| 2478 | 'possibility of losing the work tree\'s git metadata,' |
| 2479 | ' use `repo sync --force-sync {0}` to ' |
| 2480 | 'proceed.'.format(self.relpath)) |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2481 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2482 | def _ReferenceGitDir(self, gitdir, dotgit, share_refs, copy_all): |
| 2483 | """Update |dotgit| to reference |gitdir|, using symlinks where possible. |
| 2484 | |
| 2485 | Args: |
| 2486 | gitdir: The bare git repository. Must already be initialized. |
| 2487 | dotgit: The repository you would like to initialize. |
| 2488 | share_refs: If true, |dotgit| will store its refs under |gitdir|. |
| 2489 | Only one work tree can store refs under a given |gitdir|. |
| 2490 | copy_all: If true, copy all remaining files from |gitdir| -> |dotgit|. |
| 2491 | This saves you the effort of initializing |dotgit| yourself. |
| 2492 | """ |
Dan Willemsen | bdb866e | 2016-04-05 17:22:02 -0700 | [diff] [blame] | 2493 | symlink_files = self.shareable_files[:] |
| 2494 | symlink_dirs = self.shareable_dirs[:] |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2495 | if share_refs: |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2496 | symlink_files += self.working_tree_files |
| 2497 | symlink_dirs += self.working_tree_dirs |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2498 | to_symlink = symlink_files + symlink_dirs |
| 2499 | |
| 2500 | to_copy = [] |
| 2501 | if copy_all: |
| 2502 | to_copy = os.listdir(gitdir) |
| 2503 | |
Dan Willemsen | 2a3e152 | 2015-07-30 20:43:33 -0700 | [diff] [blame] | 2504 | dotgit = os.path.realpath(dotgit) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2505 | for name in set(to_copy).union(to_symlink): |
| 2506 | try: |
| 2507 | src = os.path.realpath(os.path.join(gitdir, name)) |
Dan Willemsen | 2a3e152 | 2015-07-30 20:43:33 -0700 | [diff] [blame] | 2508 | dst = os.path.join(dotgit, name) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2509 | |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2510 | if os.path.lexists(dst): |
| 2511 | continue |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2512 | |
| 2513 | # If the source dir doesn't exist, create an empty dir. |
| 2514 | if name in symlink_dirs and not os.path.lexists(src): |
| 2515 | os.makedirs(src) |
| 2516 | |
Conley Owens | 80b87fe | 2014-05-09 17:13:44 -0700 | [diff] [blame] | 2517 | # If the source file doesn't exist, ensure the destination |
| 2518 | # file doesn't either. |
| 2519 | if name in symlink_files and not os.path.lexists(src): |
| 2520 | try: |
| 2521 | os.remove(dst) |
| 2522 | except OSError: |
| 2523 | pass |
| 2524 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2525 | if name in to_symlink: |
| 2526 | os.symlink(os.path.relpath(src, os.path.dirname(dst)), dst) |
| 2527 | elif copy_all and not os.path.islink(dst): |
| 2528 | if os.path.isdir(src): |
| 2529 | shutil.copytree(src, dst) |
| 2530 | elif os.path.isfile(src): |
| 2531 | shutil.copy(src, dst) |
| 2532 | except OSError as e: |
| 2533 | if e.errno == errno.EPERM: |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 2534 | raise DownloadError('filesystem must support symlinks') |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2535 | else: |
| 2536 | raise |
| 2537 | |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 2538 | def _InitWorkTree(self, force_sync=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2539 | dotgit = os.path.join(self.worktree, '.git') |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2540 | init_dotgit = not os.path.exists(dotgit) |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2541 | try: |
| 2542 | if init_dotgit: |
| 2543 | os.makedirs(dotgit) |
| 2544 | self._ReferenceGitDir(self.gitdir, dotgit, share_refs=True, |
| 2545 | copy_all=False) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2546 | |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 2547 | try: |
| 2548 | self._CheckDirReference(self.gitdir, dotgit, share_refs=True) |
| 2549 | except GitError as e: |
| 2550 | if force_sync: |
| 2551 | try: |
| 2552 | shutil.rmtree(dotgit) |
| 2553 | return self._InitWorkTree(force_sync=False) |
| 2554 | except: |
| 2555 | raise e |
| 2556 | raise e |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2557 | |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2558 | if init_dotgit: |
| 2559 | _lwrite(os.path.join(dotgit, HEAD), '%s\n' % self.GetRevisionId()) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2560 | |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2561 | cmd = ['read-tree', '--reset', '-u'] |
| 2562 | cmd.append('-v') |
| 2563 | cmd.append(HEAD) |
| 2564 | if GitCommand(self, cmd).Wait() != 0: |
| 2565 | raise GitError("cannot initialize work tree") |
Victor Boivie | 0960b5b | 2010-11-26 13:42:13 +0100 | [diff] [blame] | 2566 | |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2567 | self._CopyAndLinkFiles() |
| 2568 | except Exception: |
| 2569 | if init_dotgit: |
| 2570 | shutil.rmtree(dotgit) |
| 2571 | raise |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2572 | |
| 2573 | def _gitdir_path(self, path): |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2574 | return os.path.realpath(os.path.join(self.gitdir, path)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2575 | |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 2576 | def _revlist(self, *args, **kw): |
| 2577 | a = [] |
| 2578 | a.extend(args) |
| 2579 | a.append('--') |
| 2580 | return self.work_git.rev_list(*a, **kw) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2581 | |
| 2582 | @property |
| 2583 | def _allrefs(self): |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 2584 | return self.bare_ref.all |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2585 | |
Sebastian Schuberth | 7ecccf6 | 2016-03-29 14:11:20 +0200 | [diff] [blame] | 2586 | def _getLogs(self, rev1, rev2, oneline=False, color=True, pretty_format=None): |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 2587 | """Get logs between two revisions of this project.""" |
| 2588 | comp = '..' |
| 2589 | if rev1: |
| 2590 | revs = [rev1] |
| 2591 | if rev2: |
| 2592 | revs.extend([comp, rev2]) |
| 2593 | cmd = ['log', ''.join(revs)] |
| 2594 | out = DiffColoring(self.config) |
| 2595 | if out.is_on and color: |
| 2596 | cmd.append('--color') |
Sebastian Schuberth | 7ecccf6 | 2016-03-29 14:11:20 +0200 | [diff] [blame] | 2597 | if pretty_format is not None: |
| 2598 | cmd.append('--pretty=format:%s' % pretty_format) |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 2599 | if oneline: |
| 2600 | cmd.append('--oneline') |
| 2601 | |
| 2602 | try: |
| 2603 | log = GitCommand(self, cmd, capture_stdout=True, capture_stderr=True) |
| 2604 | if log.Wait() == 0: |
| 2605 | return log.stdout |
| 2606 | except GitError: |
| 2607 | # worktree may not exist if groups changed for example. In that case, |
| 2608 | # try in gitdir instead. |
| 2609 | if not os.path.exists(self.worktree): |
| 2610 | return self.bare_git.log(*cmd[1:]) |
| 2611 | else: |
| 2612 | raise |
| 2613 | return None |
| 2614 | |
Sebastian Schuberth | 7ecccf6 | 2016-03-29 14:11:20 +0200 | [diff] [blame] | 2615 | def getAddedAndRemovedLogs(self, toProject, oneline=False, color=True, |
| 2616 | pretty_format=None): |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 2617 | """Get the list of logs from this revision to given revisionId""" |
| 2618 | logs = {} |
| 2619 | selfId = self.GetRevisionId(self._allrefs) |
| 2620 | toId = toProject.GetRevisionId(toProject._allrefs) |
| 2621 | |
Sebastian Schuberth | 7ecccf6 | 2016-03-29 14:11:20 +0200 | [diff] [blame] | 2622 | logs['added'] = self._getLogs(selfId, toId, oneline=oneline, color=color, |
| 2623 | pretty_format=pretty_format) |
| 2624 | logs['removed'] = self._getLogs(toId, selfId, oneline=oneline, color=color, |
| 2625 | pretty_format=pretty_format) |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 2626 | return logs |
| 2627 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2628 | class _GitGetByExec(object): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2629 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2630 | def __init__(self, project, bare, gitdir): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2631 | self._project = project |
| 2632 | self._bare = bare |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2633 | self._gitdir = gitdir |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2634 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2635 | def LsOthers(self): |
| 2636 | p = GitCommand(self._project, |
| 2637 | ['ls-files', |
| 2638 | '-z', |
| 2639 | '--others', |
| 2640 | '--exclude-standard'], |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2641 | bare=False, |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2642 | gitdir=self._gitdir, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2643 | capture_stdout=True, |
| 2644 | capture_stderr=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2645 | if p.Wait() == 0: |
| 2646 | out = p.stdout |
| 2647 | if out: |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2648 | # Backslash is not anomalous |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 2649 | return out[:-1].split('\0') # pylint: disable=W1401 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2650 | return [] |
| 2651 | |
| 2652 | def DiffZ(self, name, *args): |
| 2653 | cmd = [name] |
| 2654 | cmd.append('-z') |
| 2655 | cmd.extend(args) |
| 2656 | p = GitCommand(self._project, |
| 2657 | cmd, |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2658 | gitdir=self._gitdir, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2659 | bare=False, |
| 2660 | capture_stdout=True, |
| 2661 | capture_stderr=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2662 | try: |
| 2663 | out = p.process.stdout.read() |
| 2664 | r = {} |
| 2665 | if out: |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 2666 | out = iter(out[:-1].split('\0')) # pylint: disable=W1401 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2667 | while out: |
Shawn O. Pearce | 02dbb6d | 2008-10-21 13:59:08 -0700 | [diff] [blame] | 2668 | try: |
Anthony King | 2cd1f04 | 2014-05-05 21:24:05 +0100 | [diff] [blame] | 2669 | info = next(out) |
| 2670 | path = next(out) |
Shawn O. Pearce | 02dbb6d | 2008-10-21 13:59:08 -0700 | [diff] [blame] | 2671 | except StopIteration: |
| 2672 | break |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2673 | |
| 2674 | class _Info(object): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2675 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2676 | def __init__(self, path, omode, nmode, oid, nid, state): |
| 2677 | self.path = path |
| 2678 | self.src_path = None |
| 2679 | self.old_mode = omode |
| 2680 | self.new_mode = nmode |
| 2681 | self.old_id = oid |
| 2682 | self.new_id = nid |
| 2683 | |
| 2684 | if len(state) == 1: |
| 2685 | self.status = state |
| 2686 | self.level = None |
| 2687 | else: |
| 2688 | self.status = state[:1] |
| 2689 | self.level = state[1:] |
| 2690 | while self.level.startswith('0'): |
| 2691 | self.level = self.level[1:] |
| 2692 | |
| 2693 | info = info[1:].split(' ') |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 2694 | info = _Info(path, *info) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2695 | if info.status in ('R', 'C'): |
| 2696 | info.src_path = info.path |
Anthony King | 2cd1f04 | 2014-05-05 21:24:05 +0100 | [diff] [blame] | 2697 | info.path = next(out) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2698 | r[info.path] = info |
| 2699 | return r |
| 2700 | finally: |
| 2701 | p.Wait() |
| 2702 | |
| 2703 | def GetHead(self): |
Shawn O. Pearce | 5b23f24 | 2009-04-17 18:43:33 -0700 | [diff] [blame] | 2704 | if self._bare: |
| 2705 | path = os.path.join(self._project.gitdir, HEAD) |
| 2706 | else: |
| 2707 | path = os.path.join(self._project.worktree, '.git', HEAD) |
Conley Owens | 75ee057 | 2012-11-15 17:33:11 -0800 | [diff] [blame] | 2708 | try: |
| 2709 | fd = open(path, 'rb') |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 2710 | except IOError as e: |
| 2711 | raise NoManifestException(path, str(e)) |
Shawn O. Pearce | 76ca9f8 | 2009-04-18 14:48:03 -0700 | [diff] [blame] | 2712 | try: |
| 2713 | line = fd.read() |
| 2714 | finally: |
| 2715 | fd.close() |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 2716 | try: |
| 2717 | line = line.decode() |
| 2718 | except AttributeError: |
| 2719 | pass |
Shawn O. Pearce | 5b23f24 | 2009-04-17 18:43:33 -0700 | [diff] [blame] | 2720 | if line.startswith('ref: '): |
| 2721 | return line[5:-1] |
| 2722 | return line[:-1] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2723 | |
| 2724 | def SetHead(self, ref, message=None): |
| 2725 | cmdv = [] |
| 2726 | if message is not None: |
| 2727 | cmdv.extend(['-m', message]) |
| 2728 | cmdv.append(HEAD) |
| 2729 | cmdv.append(ref) |
| 2730 | self.symbolic_ref(*cmdv) |
| 2731 | |
| 2732 | def DetachHead(self, new, message=None): |
| 2733 | cmdv = ['--no-deref'] |
| 2734 | if message is not None: |
| 2735 | cmdv.extend(['-m', message]) |
| 2736 | cmdv.append(HEAD) |
| 2737 | cmdv.append(new) |
| 2738 | self.update_ref(*cmdv) |
| 2739 | |
| 2740 | def UpdateRef(self, name, new, old=None, |
| 2741 | message=None, |
| 2742 | detach=False): |
| 2743 | cmdv = [] |
| 2744 | if message is not None: |
| 2745 | cmdv.extend(['-m', message]) |
| 2746 | if detach: |
| 2747 | cmdv.append('--no-deref') |
| 2748 | cmdv.append(name) |
| 2749 | cmdv.append(new) |
| 2750 | if old is not None: |
| 2751 | cmdv.append(old) |
| 2752 | self.update_ref(*cmdv) |
| 2753 | |
| 2754 | def DeleteRef(self, name, old=None): |
| 2755 | if not old: |
| 2756 | old = self.rev_parse(name) |
| 2757 | self.update_ref('-d', name, old) |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 2758 | self._project.bare_ref.deleted(name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2759 | |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 2760 | def rev_list(self, *args, **kw): |
| 2761 | if 'format' in kw: |
| 2762 | cmdv = ['log', '--pretty=format:%s' % kw['format']] |
| 2763 | else: |
| 2764 | cmdv = ['rev-list'] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2765 | cmdv.extend(args) |
| 2766 | p = GitCommand(self._project, |
| 2767 | cmdv, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2768 | bare=self._bare, |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2769 | gitdir=self._gitdir, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2770 | capture_stdout=True, |
| 2771 | capture_stderr=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2772 | r = [] |
| 2773 | for line in p.process.stdout: |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 2774 | if line[-1] == '\n': |
| 2775 | line = line[:-1] |
| 2776 | r.append(line) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2777 | if p.Wait() != 0: |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2778 | raise GitError('%s rev-list %s: %s' % |
| 2779 | (self._project.name, str(args), p.stderr)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2780 | return r |
| 2781 | |
| 2782 | def __getattr__(self, name): |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 2783 | """Allow arbitrary git commands using pythonic syntax. |
| 2784 | |
| 2785 | This allows you to do things like: |
| 2786 | git_obj.rev_parse('HEAD') |
| 2787 | |
| 2788 | Since we don't have a 'rev_parse' method defined, the __getattr__ will |
| 2789 | run. We'll replace the '_' with a '-' and try to run a git command. |
Dave Borowitz | 091f893 | 2012-10-23 17:01:04 -0700 | [diff] [blame] | 2790 | Any other positional arguments will be passed to the git command, and the |
| 2791 | following keyword arguments are supported: |
| 2792 | config: An optional dict of git config options to be passed with '-c'. |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 2793 | |
| 2794 | Args: |
| 2795 | name: The name of the git command to call. Any '_' characters will |
| 2796 | be replaced with '-'. |
| 2797 | |
| 2798 | Returns: |
| 2799 | A callable object that will try to call git with the named command. |
| 2800 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2801 | name = name.replace('_', '-') |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2802 | |
Dave Borowitz | 091f893 | 2012-10-23 17:01:04 -0700 | [diff] [blame] | 2803 | def runner(*args, **kwargs): |
| 2804 | cmdv = [] |
| 2805 | config = kwargs.pop('config', None) |
| 2806 | for k in kwargs: |
| 2807 | raise TypeError('%s() got an unexpected keyword argument %r' |
| 2808 | % (name, k)) |
| 2809 | if config is not None: |
Dave Borowitz | b42b474 | 2012-10-31 12:27:27 -0700 | [diff] [blame] | 2810 | if not git_require((1, 7, 2)): |
| 2811 | raise ValueError('cannot set config on command line for %s()' |
| 2812 | % name) |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 2813 | for k, v in config.items(): |
Dave Borowitz | 091f893 | 2012-10-23 17:01:04 -0700 | [diff] [blame] | 2814 | cmdv.append('-c') |
| 2815 | cmdv.append('%s=%s' % (k, v)) |
| 2816 | cmdv.append(name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2817 | cmdv.extend(args) |
| 2818 | p = GitCommand(self._project, |
| 2819 | cmdv, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2820 | bare=self._bare, |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2821 | gitdir=self._gitdir, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2822 | capture_stdout=True, |
| 2823 | capture_stderr=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2824 | if p.Wait() != 0: |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2825 | raise GitError('%s %s: %s' % |
| 2826 | (self._project.name, name, p.stderr)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2827 | r = p.stdout |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 2828 | try: |
Conley Owens | edd0151 | 2013-09-26 12:59:58 -0700 | [diff] [blame] | 2829 | r = r.decode('utf-8') |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 2830 | except AttributeError: |
| 2831 | pass |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2832 | if r.endswith('\n') and r.index('\n') == len(r) - 1: |
| 2833 | return r[:-1] |
| 2834 | return r |
| 2835 | return runner |
| 2836 | |
| 2837 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 2838 | class _PriorSyncFailedError(Exception): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2839 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 2840 | def __str__(self): |
| 2841 | return 'prior sync failed; rebase still in progress' |
| 2842 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2843 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 2844 | class _DirtyError(Exception): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2845 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 2846 | def __str__(self): |
| 2847 | return 'contains uncommitted changes' |
| 2848 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2849 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 2850 | class _InfoMessage(object): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2851 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 2852 | def __init__(self, project, text): |
| 2853 | self.project = project |
| 2854 | self.text = text |
| 2855 | |
| 2856 | def Print(self, syncbuf): |
| 2857 | syncbuf.out.info('%s/: %s', self.project.relpath, self.text) |
| 2858 | syncbuf.out.nl() |
| 2859 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2860 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 2861 | class _Failure(object): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2862 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 2863 | def __init__(self, project, why): |
| 2864 | self.project = project |
| 2865 | self.why = why |
| 2866 | |
| 2867 | def Print(self, syncbuf): |
| 2868 | syncbuf.out.fail('error: %s/: %s', |
| 2869 | self.project.relpath, |
| 2870 | str(self.why)) |
| 2871 | syncbuf.out.nl() |
| 2872 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2873 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 2874 | class _Later(object): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2875 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 2876 | def __init__(self, project, action): |
| 2877 | self.project = project |
| 2878 | self.action = action |
| 2879 | |
| 2880 | def Run(self, syncbuf): |
| 2881 | out = syncbuf.out |
| 2882 | out.project('project %s/', self.project.relpath) |
| 2883 | out.nl() |
| 2884 | try: |
| 2885 | self.action() |
| 2886 | out.nl() |
| 2887 | return True |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 2888 | except GitError: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 2889 | out.nl() |
| 2890 | return False |
| 2891 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2892 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 2893 | class _SyncColoring(Coloring): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2894 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 2895 | def __init__(self, config): |
| 2896 | Coloring.__init__(self, config, 'reposync') |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2897 | self.project = self.printer('header', attr='bold') |
| 2898 | self.info = self.printer('info') |
| 2899 | self.fail = self.printer('fail', fg='red') |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 2900 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2901 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 2902 | class SyncBuffer(object): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2903 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 2904 | def __init__(self, config, detach_head=False): |
| 2905 | self._messages = [] |
| 2906 | self._failures = [] |
| 2907 | self._later_queue1 = [] |
| 2908 | self._later_queue2 = [] |
| 2909 | |
| 2910 | self.out = _SyncColoring(config) |
| 2911 | self.out.redirect(sys.stderr) |
| 2912 | |
| 2913 | self.detach_head = detach_head |
| 2914 | self.clean = True |
| 2915 | |
| 2916 | def info(self, project, fmt, *args): |
| 2917 | self._messages.append(_InfoMessage(project, fmt % args)) |
| 2918 | |
| 2919 | def fail(self, project, err=None): |
| 2920 | self._failures.append(_Failure(project, err)) |
| 2921 | self.clean = False |
| 2922 | |
| 2923 | def later1(self, project, what): |
| 2924 | self._later_queue1.append(_Later(project, what)) |
| 2925 | |
| 2926 | def later2(self, project, what): |
| 2927 | self._later_queue2.append(_Later(project, what)) |
| 2928 | |
| 2929 | def Finish(self): |
| 2930 | self._PrintMessages() |
| 2931 | self._RunLater() |
| 2932 | self._PrintMessages() |
| 2933 | return self.clean |
| 2934 | |
| 2935 | def _RunLater(self): |
| 2936 | for q in ['_later_queue1', '_later_queue2']: |
| 2937 | if not self._RunQueue(q): |
| 2938 | return |
| 2939 | |
| 2940 | def _RunQueue(self, queue): |
| 2941 | for m in getattr(self, queue): |
| 2942 | if not m.Run(self): |
| 2943 | self.clean = False |
| 2944 | return False |
| 2945 | setattr(self, queue, []) |
| 2946 | return True |
| 2947 | |
| 2948 | def _PrintMessages(self): |
| 2949 | for m in self._messages: |
| 2950 | m.Print(self) |
| 2951 | for m in self._failures: |
| 2952 | m.Print(self) |
| 2953 | |
| 2954 | self._messages = [] |
| 2955 | self._failures = [] |
| 2956 | |
| 2957 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2958 | class MetaProject(Project): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2959 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2960 | """A special project housed under .repo. |
| 2961 | """ |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2962 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2963 | def __init__(self, manifest, name, gitdir, worktree): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2964 | Project.__init__(self, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2965 | manifest=manifest, |
| 2966 | name=name, |
| 2967 | gitdir=gitdir, |
| 2968 | objdir=gitdir, |
| 2969 | worktree=worktree, |
| 2970 | remote=RemoteSpec('origin'), |
| 2971 | relpath='.repo/%s' % name, |
| 2972 | revisionExpr='refs/heads/master', |
| 2973 | revisionId=None, |
| 2974 | groups=None) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2975 | |
| 2976 | def PreSync(self): |
| 2977 | if self.Exists: |
| 2978 | cb = self.CurrentBranch |
| 2979 | if cb: |
| 2980 | base = self.GetBranch(cb).merge |
| 2981 | if base: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 2982 | self.revisionExpr = base |
| 2983 | self.revisionId = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2984 | |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2985 | def MetaBranchSwitch(self): |
Florian Vallee | 5d01650 | 2012-06-07 17:19:26 +0200 | [diff] [blame] | 2986 | """ Prepare MetaProject for manifest branch switch |
| 2987 | """ |
| 2988 | |
| 2989 | # detach and delete manifest branch, allowing a new |
| 2990 | # branch to take over |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2991 | syncbuf = SyncBuffer(self.config, detach_head=True) |
Florian Vallee | 5d01650 | 2012-06-07 17:19:26 +0200 | [diff] [blame] | 2992 | self.Sync_LocalHalf(syncbuf) |
| 2993 | syncbuf.Finish() |
| 2994 | |
| 2995 | return GitCommand(self, |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2996 | ['update-ref', '-d', 'refs/heads/default'], |
| 2997 | capture_stdout=True, |
| 2998 | capture_stderr=True).Wait() == 0 |
Florian Vallee | 5d01650 | 2012-06-07 17:19:26 +0200 | [diff] [blame] | 2999 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3000 | @property |
Shawn O. Pearce | f690687 | 2009-04-18 10:49:00 -0700 | [diff] [blame] | 3001 | def LastFetch(self): |
| 3002 | try: |
| 3003 | fh = os.path.join(self.gitdir, 'FETCH_HEAD') |
| 3004 | return os.path.getmtime(fh) |
| 3005 | except OSError: |
| 3006 | return 0 |
| 3007 | |
| 3008 | @property |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3009 | def HasChanges(self): |
| 3010 | """Has the remote received new commits not yet checked out? |
| 3011 | """ |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 3012 | if not self.remote or not self.revisionExpr: |
Shawn O. Pearce | 336f7bd | 2009-04-18 10:39:28 -0700 | [diff] [blame] | 3013 | return False |
| 3014 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 3015 | all_refs = self.bare_ref.all |
| 3016 | revid = self.GetRevisionId(all_refs) |
Shawn O. Pearce | 336f7bd | 2009-04-18 10:39:28 -0700 | [diff] [blame] | 3017 | head = self.work_git.GetHead() |
| 3018 | if head.startswith(R_HEADS): |
| 3019 | try: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 3020 | head = all_refs[head] |
Shawn O. Pearce | 336f7bd | 2009-04-18 10:39:28 -0700 | [diff] [blame] | 3021 | except KeyError: |
| 3022 | head = None |
| 3023 | |
| 3024 | if revid == head: |
| 3025 | return False |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 3026 | elif self._revlist(not_rev(HEAD), revid): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3027 | return True |
| 3028 | return False |