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