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