The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2008 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
Shawn O. Pearce | 2a1ccb2 | 2009-04-10 16:51:53 -0700 | [diff] [blame] | 16 | from optparse import SUPPRESS_HELP |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 17 | import os |
| 18 | import re |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 19 | import shutil |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | import subprocess |
| 21 | import sys |
Shawn O. Pearce | f690687 | 2009-04-18 10:49:00 -0700 | [diff] [blame] | 22 | import time |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | |
| 24 | from git_command import GIT |
Shawn O. Pearce | e756c41 | 2009-04-13 11:51:15 -0700 | [diff] [blame] | 25 | from project import HEAD |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 26 | from project import Project |
| 27 | from project import RemoteSpec |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 28 | from command import Command, MirrorSafeCommand |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | from error import RepoChangedException, GitError |
| 30 | from project import R_HEADS |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 31 | from project import SyncBuffer |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 32 | from progress import Progress |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 34 | class Sync(Command, MirrorSafeCommand): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 35 | common = True |
| 36 | helpSummary = "Update working tree to the latest revision" |
| 37 | helpUsage = """ |
| 38 | %prog [<project>...] |
| 39 | """ |
| 40 | helpDescription = """ |
| 41 | The '%prog' command synchronizes local project directories |
| 42 | with the remote repositories specified in the manifest. If a local |
| 43 | project does not yet exist, it will clone a new local directory from |
| 44 | the remote repository and set up tracking branches as specified in |
| 45 | the manifest. If the local project already exists, '%prog' |
| 46 | will update the remote branches and rebase any new local changes |
| 47 | on top of the new remote changes. |
| 48 | |
| 49 | '%prog' will synchronize all projects listed at the command |
| 50 | line. Projects can be specified either by name, or by a relative |
| 51 | or absolute path to the project's local directory. If no projects |
| 52 | are specified, '%prog' will synchronize all projects listed in |
| 53 | the manifest. |
Shawn O. Pearce | 3e768c9 | 2009-04-10 16:59:36 -0700 | [diff] [blame] | 54 | |
| 55 | The -d/--detach option can be used to switch specified projects |
| 56 | back to the manifest revision. This option is especially helpful |
| 57 | if the project is currently on a topic branch, but the manifest |
| 58 | revision is temporarily needed. |
Shawn O. Pearce | eb7af87 | 2009-04-21 08:02:04 -0700 | [diff] [blame] | 59 | |
| 60 | SSH Connections |
| 61 | --------------- |
| 62 | |
| 63 | If at least one project remote URL uses an SSH connection (ssh://, |
| 64 | git+ssh://, or user@host:path syntax) repo will automatically |
| 65 | enable the SSH ControlMaster option when connecting to that host. |
| 66 | This feature permits other projects in the same '%prog' session to |
| 67 | reuse the same SSH tunnel, saving connection setup overheads. |
| 68 | |
| 69 | To disable this behavior on UNIX platforms, set the GIT_SSH |
| 70 | environment variable to 'ssh'. For example: |
| 71 | |
| 72 | export GIT_SSH=ssh |
| 73 | %prog |
| 74 | |
| 75 | Compatibility |
| 76 | ~~~~~~~~~~~~~ |
| 77 | |
| 78 | This feature is automatically disabled on Windows, due to the lack |
| 79 | of UNIX domain socket support. |
| 80 | |
| 81 | This feature is not compatible with url.insteadof rewrites in the |
| 82 | user's ~/.gitconfig. '%prog' is currently not able to perform the |
| 83 | rewrite early enough to establish the ControlMaster tunnel. |
| 84 | |
| 85 | If the remote SSH daemon is Gerrit Code Review, version 2.0.10 or |
| 86 | later is required to fix a server side protocol bug. |
| 87 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 88 | """ |
| 89 | |
| 90 | def _Options(self, p): |
Shawn O. Pearce | b1562fa | 2009-04-10 17:04:08 -0700 | [diff] [blame] | 91 | p.add_option('-l','--local-only', |
| 92 | dest='local_only', action='store_true', |
| 93 | help="only update working tree, don't fetch") |
Shawn O. Pearce | 96fdcef | 2009-04-10 16:29:20 -0700 | [diff] [blame] | 94 | p.add_option('-n','--network-only', |
| 95 | dest='network_only', action='store_true', |
| 96 | help="fetch only, don't update working tree") |
Shawn O. Pearce | 3e768c9 | 2009-04-10 16:59:36 -0700 | [diff] [blame] | 97 | p.add_option('-d','--detach', |
| 98 | dest='detach_head', action='store_true', |
| 99 | help='detach projects back to manifest revision') |
| 100 | |
Shawn O. Pearce | fd89b67 | 2009-04-18 11:28:57 -0700 | [diff] [blame] | 101 | g = p.add_option_group('repo Version options') |
| 102 | g.add_option('--no-repo-verify', |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 103 | dest='no_repo_verify', action='store_true', |
| 104 | help='do not verify repo source code') |
Shawn O. Pearce | fd89b67 | 2009-04-18 11:28:57 -0700 | [diff] [blame] | 105 | g.add_option('--repo-upgraded', |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 106 | dest='repo_upgraded', action='store_true', |
Shawn O. Pearce | 2a1ccb2 | 2009-04-10 16:51:53 -0700 | [diff] [blame] | 107 | help=SUPPRESS_HELP) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 108 | |
Shawn O. Pearce | f690687 | 2009-04-18 10:49:00 -0700 | [diff] [blame] | 109 | def _Fetch(self, projects): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 110 | fetched = set() |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 111 | pm = Progress('Fetching projects', len(projects)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 112 | for project in projects: |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 113 | pm.update() |
| 114 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 115 | if project.Sync_NetworkHalf(): |
| 116 | fetched.add(project.gitdir) |
| 117 | else: |
| 118 | print >>sys.stderr, 'error: Cannot fetch %s' % project.name |
| 119 | sys.exit(1) |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 120 | pm.end() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 121 | return fetched |
| 122 | |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 123 | def UpdateProjectList(self): |
| 124 | new_project_paths = [] |
| 125 | for project in self.manifest.projects.values(): |
| 126 | new_project_paths.append(project.relpath) |
| 127 | file_name = 'project.list' |
| 128 | file_path = os.path.join(self.manifest.repodir, file_name) |
| 129 | old_project_paths = [] |
| 130 | |
| 131 | if os.path.exists(file_path): |
| 132 | fd = open(file_path, 'r') |
| 133 | try: |
| 134 | old_project_paths = fd.read().split('\n') |
| 135 | finally: |
| 136 | fd.close() |
| 137 | for path in old_project_paths: |
| 138 | if path not in new_project_paths: |
| 139 | project = Project( |
| 140 | manifest = self.manifest, |
| 141 | name = path, |
| 142 | remote = RemoteSpec('origin'), |
| 143 | gitdir = os.path.join(self.manifest.topdir, |
| 144 | path, '.git'), |
| 145 | worktree = os.path.join(self.manifest.topdir, path), |
| 146 | relpath = path, |
| 147 | revisionExpr = 'HEAD', |
| 148 | revisionId = None) |
| 149 | if project.IsDirty(): |
| 150 | print >>sys.stderr, 'error: Cannot remove project "%s": \ |
| 151 | uncommitted changes are present' % project.relpath |
| 152 | print >>sys.stderr, ' commit changes, then run sync again' |
| 153 | return -1 |
| 154 | else: |
| 155 | print >>sys.stderr, 'Deleting obsolete path %s' % project.worktree |
| 156 | shutil.rmtree(project.worktree) |
Jaikumar Ganesh | 8135cdc | 2009-06-02 15:07:44 -0700 | [diff] [blame^] | 157 | # Try deleting parent subdirs if they are empty |
| 158 | dir = os.path.dirname(project.worktree) |
| 159 | while dir != self.manifest.topdir: |
| 160 | try: |
| 161 | os.rmdir(dir) |
| 162 | except OSError: |
| 163 | break |
| 164 | dir = os.path.dirname(dir) |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 165 | |
| 166 | fd = open(file_path, 'w') |
| 167 | try: |
| 168 | fd.write('\n'.join(new_project_paths)) |
| 169 | finally: |
| 170 | fd.close() |
| 171 | return 0 |
| 172 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 173 | def Execute(self, opt, args): |
Shawn O. Pearce | 3e768c9 | 2009-04-10 16:59:36 -0700 | [diff] [blame] | 174 | if opt.network_only and opt.detach_head: |
| 175 | print >>sys.stderr, 'error: cannot combine -n and -d' |
| 176 | sys.exit(1) |
Shawn O. Pearce | b1562fa | 2009-04-10 17:04:08 -0700 | [diff] [blame] | 177 | if opt.network_only and opt.local_only: |
| 178 | print >>sys.stderr, 'error: cannot combine -n and -l' |
| 179 | sys.exit(1) |
Shawn O. Pearce | 3e768c9 | 2009-04-10 16:59:36 -0700 | [diff] [blame] | 180 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 181 | rp = self.manifest.repoProject |
| 182 | rp.PreSync() |
| 183 | |
| 184 | mp = self.manifest.manifestProject |
| 185 | mp.PreSync() |
| 186 | |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 187 | if opt.repo_upgraded: |
Shawn O. Pearce | e756c41 | 2009-04-13 11:51:15 -0700 | [diff] [blame] | 188 | _PostRepoUpgrade(self.manifest) |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 189 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 190 | all = self.GetProjects(args, missing_ok=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 191 | |
Shawn O. Pearce | b1562fa | 2009-04-10 17:04:08 -0700 | [diff] [blame] | 192 | if not opt.local_only: |
Shawn O. Pearce | f690687 | 2009-04-18 10:49:00 -0700 | [diff] [blame] | 193 | to_fetch = [] |
| 194 | now = time.time() |
| 195 | if (24 * 60 * 60) <= (now - rp.LastFetch): |
| 196 | to_fetch.append(rp) |
| 197 | to_fetch.append(mp) |
| 198 | to_fetch.extend(all) |
| 199 | |
| 200 | fetched = self._Fetch(to_fetch) |
Shawn O. Pearce | e756c41 | 2009-04-13 11:51:15 -0700 | [diff] [blame] | 201 | _PostRepoFetch(rp, opt.no_repo_verify) |
Shawn O. Pearce | b1562fa | 2009-04-10 17:04:08 -0700 | [diff] [blame] | 202 | if opt.network_only: |
| 203 | # bail out now; the rest touches the working tree |
| 204 | return |
| 205 | |
| 206 | if mp.HasChanges: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 207 | syncbuf = SyncBuffer(mp.config) |
| 208 | mp.Sync_LocalHalf(syncbuf) |
| 209 | if not syncbuf.Finish(): |
Shawn O. Pearce | b1562fa | 2009-04-10 17:04:08 -0700 | [diff] [blame] | 210 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 211 | |
Shawn O. Pearce | b1562fa | 2009-04-10 17:04:08 -0700 | [diff] [blame] | 212 | self.manifest._Unload() |
| 213 | all = self.GetProjects(args, missing_ok=True) |
| 214 | missing = [] |
| 215 | for project in all: |
| 216 | if project.gitdir not in fetched: |
| 217 | missing.append(project) |
Shawn O. Pearce | f690687 | 2009-04-18 10:49:00 -0700 | [diff] [blame] | 218 | self._Fetch(missing) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 219 | |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 220 | if self.UpdateProjectList(): |
| 221 | sys.exit(1) |
| 222 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 223 | syncbuf = SyncBuffer(mp.config, |
| 224 | detach_head = opt.detach_head) |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 225 | pm = Progress('Syncing work tree', len(all)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 226 | for project in all: |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 227 | pm.update() |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 228 | if project.worktree: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 229 | project.Sync_LocalHalf(syncbuf) |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 230 | pm.end() |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 231 | print >>sys.stderr |
| 232 | if not syncbuf.Finish(): |
| 233 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 234 | |
Shawn O. Pearce | e756c41 | 2009-04-13 11:51:15 -0700 | [diff] [blame] | 235 | |
| 236 | def _PostRepoUpgrade(manifest): |
| 237 | for project in manifest.projects.values(): |
| 238 | if project.Exists: |
| 239 | project.PostRepoUpgrade() |
| 240 | |
| 241 | def _PostRepoFetch(rp, no_repo_verify=False, verbose=False): |
| 242 | if rp.HasChanges: |
| 243 | print >>sys.stderr, 'info: A new version of repo is available' |
| 244 | print >>sys.stderr, '' |
| 245 | if no_repo_verify or _VerifyTag(rp): |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 246 | syncbuf = SyncBuffer(rp.config) |
| 247 | rp.Sync_LocalHalf(syncbuf) |
| 248 | if not syncbuf.Finish(): |
Shawn O. Pearce | e756c41 | 2009-04-13 11:51:15 -0700 | [diff] [blame] | 249 | sys.exit(1) |
| 250 | print >>sys.stderr, 'info: Restarting repo with latest version' |
| 251 | raise RepoChangedException(['--repo-upgraded']) |
| 252 | else: |
| 253 | print >>sys.stderr, 'warning: Skipped upgrade to unverified version' |
| 254 | else: |
| 255 | if verbose: |
| 256 | print >>sys.stderr, 'repo version %s is current' % rp.work_git.describe(HEAD) |
| 257 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 258 | def _VerifyTag(project): |
| 259 | gpg_dir = os.path.expanduser('~/.repoconfig/gnupg') |
| 260 | if not os.path.exists(gpg_dir): |
| 261 | print >>sys.stderr,\ |
| 262 | """warning: GnuPG was not available during last "repo init" |
| 263 | warning: Cannot automatically authenticate repo.""" |
| 264 | return True |
| 265 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 266 | try: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 267 | cur = project.bare_git.describe(project.GetRevisionId()) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 268 | except GitError: |
| 269 | cur = None |
| 270 | |
| 271 | if not cur \ |
| 272 | or re.compile(r'^.*-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur): |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 273 | rev = project.revisionExpr |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 274 | if rev.startswith(R_HEADS): |
| 275 | rev = rev[len(R_HEADS):] |
| 276 | |
| 277 | print >>sys.stderr |
| 278 | print >>sys.stderr,\ |
| 279 | "warning: project '%s' branch '%s' is not signed" \ |
| 280 | % (project.name, rev) |
| 281 | return False |
| 282 | |
| 283 | env = dict(os.environ) |
| 284 | env['GIT_DIR'] = project.gitdir |
| 285 | env['GNUPGHOME'] = gpg_dir |
| 286 | |
| 287 | cmd = [GIT, 'tag', '-v', cur] |
| 288 | proc = subprocess.Popen(cmd, |
| 289 | stdout = subprocess.PIPE, |
| 290 | stderr = subprocess.PIPE, |
| 291 | env = env) |
| 292 | out = proc.stdout.read() |
| 293 | proc.stdout.close() |
| 294 | |
| 295 | err = proc.stderr.read() |
| 296 | proc.stderr.close() |
| 297 | |
| 298 | if proc.wait() != 0: |
| 299 | print >>sys.stderr |
| 300 | print >>sys.stderr, out |
| 301 | print >>sys.stderr, err |
| 302 | print >>sys.stderr |
| 303 | return False |
| 304 | return True |