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 |
| 19 | import subprocess |
| 20 | import sys |
Shawn O. Pearce | f690687 | 2009-04-18 10:49:00 -0700 | [diff] [blame] | 21 | import time |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 22 | |
| 23 | from git_command import GIT |
Shawn O. Pearce | e756c41 | 2009-04-13 11:51:15 -0700 | [diff] [blame] | 24 | from project import HEAD |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 25 | from command import Command, MirrorSafeCommand |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | from error import RepoChangedException, GitError |
| 27 | from project import R_HEADS |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 28 | from project import SyncBuffer |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 29 | from progress import Progress |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 30 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 31 | class Sync(Command, MirrorSafeCommand): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 32 | common = True |
| 33 | helpSummary = "Update working tree to the latest revision" |
| 34 | helpUsage = """ |
| 35 | %prog [<project>...] |
| 36 | """ |
| 37 | helpDescription = """ |
| 38 | The '%prog' command synchronizes local project directories |
| 39 | with the remote repositories specified in the manifest. If a local |
| 40 | project does not yet exist, it will clone a new local directory from |
| 41 | the remote repository and set up tracking branches as specified in |
| 42 | the manifest. If the local project already exists, '%prog' |
| 43 | will update the remote branches and rebase any new local changes |
| 44 | on top of the new remote changes. |
| 45 | |
| 46 | '%prog' will synchronize all projects listed at the command |
| 47 | line. Projects can be specified either by name, or by a relative |
| 48 | or absolute path to the project's local directory. If no projects |
| 49 | are specified, '%prog' will synchronize all projects listed in |
| 50 | the manifest. |
Shawn O. Pearce | 3e768c9 | 2009-04-10 16:59:36 -0700 | [diff] [blame] | 51 | |
| 52 | The -d/--detach option can be used to switch specified projects |
| 53 | back to the manifest revision. This option is especially helpful |
| 54 | if the project is currently on a topic branch, but the manifest |
| 55 | revision is temporarily needed. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 56 | """ |
| 57 | |
| 58 | def _Options(self, p): |
Shawn O. Pearce | b1562fa | 2009-04-10 17:04:08 -0700 | [diff] [blame] | 59 | p.add_option('-l','--local-only', |
| 60 | dest='local_only', action='store_true', |
| 61 | help="only update working tree, don't fetch") |
Shawn O. Pearce | 96fdcef | 2009-04-10 16:29:20 -0700 | [diff] [blame] | 62 | p.add_option('-n','--network-only', |
| 63 | dest='network_only', action='store_true', |
| 64 | help="fetch only, don't update working tree") |
Shawn O. Pearce | 3e768c9 | 2009-04-10 16:59:36 -0700 | [diff] [blame] | 65 | p.add_option('-d','--detach', |
| 66 | dest='detach_head', action='store_true', |
| 67 | help='detach projects back to manifest revision') |
| 68 | |
Shawn O. Pearce | fd89b67 | 2009-04-18 11:28:57 -0700 | [diff] [blame] | 69 | g = p.add_option_group('repo Version options') |
| 70 | g.add_option('--no-repo-verify', |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 71 | dest='no_repo_verify', action='store_true', |
| 72 | help='do not verify repo source code') |
Shawn O. Pearce | fd89b67 | 2009-04-18 11:28:57 -0700 | [diff] [blame] | 73 | g.add_option('--repo-upgraded', |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 74 | dest='repo_upgraded', action='store_true', |
Shawn O. Pearce | 2a1ccb2 | 2009-04-10 16:51:53 -0700 | [diff] [blame] | 75 | help=SUPPRESS_HELP) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 76 | |
Shawn O. Pearce | f690687 | 2009-04-18 10:49:00 -0700 | [diff] [blame] | 77 | def _Fetch(self, projects): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 78 | fetched = set() |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 79 | pm = Progress('Fetching projects', len(projects)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 80 | for project in projects: |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 81 | pm.update() |
| 82 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 83 | if project.Sync_NetworkHalf(): |
| 84 | fetched.add(project.gitdir) |
| 85 | else: |
| 86 | print >>sys.stderr, 'error: Cannot fetch %s' % project.name |
| 87 | sys.exit(1) |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 88 | pm.end() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 89 | return fetched |
| 90 | |
| 91 | def Execute(self, opt, args): |
Shawn O. Pearce | 3e768c9 | 2009-04-10 16:59:36 -0700 | [diff] [blame] | 92 | if opt.network_only and opt.detach_head: |
| 93 | print >>sys.stderr, 'error: cannot combine -n and -d' |
| 94 | sys.exit(1) |
Shawn O. Pearce | b1562fa | 2009-04-10 17:04:08 -0700 | [diff] [blame] | 95 | if opt.network_only and opt.local_only: |
| 96 | print >>sys.stderr, 'error: cannot combine -n and -l' |
| 97 | sys.exit(1) |
Shawn O. Pearce | 3e768c9 | 2009-04-10 16:59:36 -0700 | [diff] [blame] | 98 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 99 | rp = self.manifest.repoProject |
| 100 | rp.PreSync() |
| 101 | |
| 102 | mp = self.manifest.manifestProject |
| 103 | mp.PreSync() |
| 104 | |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 105 | if opt.repo_upgraded: |
Shawn O. Pearce | e756c41 | 2009-04-13 11:51:15 -0700 | [diff] [blame] | 106 | _PostRepoUpgrade(self.manifest) |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 107 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 108 | all = self.GetProjects(args, missing_ok=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 109 | |
Shawn O. Pearce | b1562fa | 2009-04-10 17:04:08 -0700 | [diff] [blame] | 110 | if not opt.local_only: |
Shawn O. Pearce | f690687 | 2009-04-18 10:49:00 -0700 | [diff] [blame] | 111 | to_fetch = [] |
| 112 | now = time.time() |
| 113 | if (24 * 60 * 60) <= (now - rp.LastFetch): |
| 114 | to_fetch.append(rp) |
| 115 | to_fetch.append(mp) |
| 116 | to_fetch.extend(all) |
| 117 | |
| 118 | fetched = self._Fetch(to_fetch) |
Shawn O. Pearce | e756c41 | 2009-04-13 11:51:15 -0700 | [diff] [blame] | 119 | _PostRepoFetch(rp, opt.no_repo_verify) |
Shawn O. Pearce | b1562fa | 2009-04-10 17:04:08 -0700 | [diff] [blame] | 120 | if opt.network_only: |
| 121 | # bail out now; the rest touches the working tree |
| 122 | return |
| 123 | |
| 124 | if mp.HasChanges: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 125 | syncbuf = SyncBuffer(mp.config) |
| 126 | mp.Sync_LocalHalf(syncbuf) |
| 127 | if not syncbuf.Finish(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 128 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 129 | |
Shawn O. Pearce | b1562fa | 2009-04-10 17:04:08 -0700 | [diff] [blame] | 130 | self.manifest._Unload() |
| 131 | all = self.GetProjects(args, missing_ok=True) |
| 132 | missing = [] |
| 133 | for project in all: |
| 134 | if project.gitdir not in fetched: |
| 135 | missing.append(project) |
Shawn O. Pearce | f690687 | 2009-04-18 10:49:00 -0700 | [diff] [blame] | 136 | self._Fetch(missing) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 137 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 138 | syncbuf = SyncBuffer(mp.config, |
| 139 | detach_head = opt.detach_head) |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 140 | pm = Progress('Syncing work tree', len(all)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 141 | for project in all: |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 142 | pm.update() |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 143 | if project.worktree: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 144 | project.Sync_LocalHalf(syncbuf) |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 145 | pm.end() |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 146 | print >>sys.stderr |
| 147 | if not syncbuf.Finish(): |
| 148 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 149 | |
Shawn O. Pearce | e756c41 | 2009-04-13 11:51:15 -0700 | [diff] [blame] | 150 | |
| 151 | def _PostRepoUpgrade(manifest): |
| 152 | for project in manifest.projects.values(): |
| 153 | if project.Exists: |
| 154 | project.PostRepoUpgrade() |
| 155 | |
| 156 | def _PostRepoFetch(rp, no_repo_verify=False, verbose=False): |
| 157 | if rp.HasChanges: |
| 158 | print >>sys.stderr, 'info: A new version of repo is available' |
| 159 | print >>sys.stderr, '' |
| 160 | if no_repo_verify or _VerifyTag(rp): |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 161 | syncbuf = SyncBuffer(rp.config) |
| 162 | rp.Sync_LocalHalf(syncbuf) |
| 163 | if not syncbuf.Finish(): |
Shawn O. Pearce | e756c41 | 2009-04-13 11:51:15 -0700 | [diff] [blame] | 164 | sys.exit(1) |
| 165 | print >>sys.stderr, 'info: Restarting repo with latest version' |
| 166 | raise RepoChangedException(['--repo-upgraded']) |
| 167 | else: |
| 168 | print >>sys.stderr, 'warning: Skipped upgrade to unverified version' |
| 169 | else: |
| 170 | if verbose: |
| 171 | print >>sys.stderr, 'repo version %s is current' % rp.work_git.describe(HEAD) |
| 172 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 173 | def _VerifyTag(project): |
| 174 | gpg_dir = os.path.expanduser('~/.repoconfig/gnupg') |
| 175 | if not os.path.exists(gpg_dir): |
| 176 | print >>sys.stderr,\ |
| 177 | """warning: GnuPG was not available during last "repo init" |
| 178 | warning: Cannot automatically authenticate repo.""" |
| 179 | return True |
| 180 | |
| 181 | remote = project.GetRemote(project.remote.name) |
| 182 | ref = remote.ToLocal(project.revision) |
| 183 | |
| 184 | try: |
| 185 | cur = project.bare_git.describe(ref) |
| 186 | except GitError: |
| 187 | cur = None |
| 188 | |
| 189 | if not cur \ |
| 190 | or re.compile(r'^.*-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur): |
| 191 | rev = project.revision |
| 192 | if rev.startswith(R_HEADS): |
| 193 | rev = rev[len(R_HEADS):] |
| 194 | |
| 195 | print >>sys.stderr |
| 196 | print >>sys.stderr,\ |
| 197 | "warning: project '%s' branch '%s' is not signed" \ |
| 198 | % (project.name, rev) |
| 199 | return False |
| 200 | |
| 201 | env = dict(os.environ) |
| 202 | env['GIT_DIR'] = project.gitdir |
| 203 | env['GNUPGHOME'] = gpg_dir |
| 204 | |
| 205 | cmd = [GIT, 'tag', '-v', cur] |
| 206 | proc = subprocess.Popen(cmd, |
| 207 | stdout = subprocess.PIPE, |
| 208 | stderr = subprocess.PIPE, |
| 209 | env = env) |
| 210 | out = proc.stdout.read() |
| 211 | proc.stdout.close() |
| 212 | |
| 213 | err = proc.stderr.read() |
| 214 | proc.stderr.close() |
| 215 | |
| 216 | if proc.wait() != 0: |
| 217 | print >>sys.stderr |
| 218 | print >>sys.stderr, out |
| 219 | print >>sys.stderr, err |
| 220 | print >>sys.stderr |
| 221 | return False |
| 222 | return True |