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. |
| 47 | """ |
| 48 | |
| 49 | def _Options(self, p): |
Shawn O. Pearce | 96fdcef | 2009-04-10 16:29:20 -0700 | [diff] [blame^] | 50 | p.add_option('-n','--network-only', |
| 51 | dest='network_only', action='store_true', |
| 52 | help="fetch only, don't update working tree") |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 53 | p.add_option('--no-repo-verify', |
| 54 | dest='no_repo_verify', action='store_true', |
| 55 | help='do not verify repo source code') |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 56 | p.add_option('--repo-upgraded', |
| 57 | dest='repo_upgraded', action='store_true', |
Shawn O. Pearce | 2a1ccb2 | 2009-04-10 16:51:53 -0700 | [diff] [blame] | 58 | help=SUPPRESS_HELP) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 59 | |
| 60 | def _Fetch(self, *projects): |
| 61 | fetched = set() |
| 62 | for project in projects: |
| 63 | if project.Sync_NetworkHalf(): |
| 64 | fetched.add(project.gitdir) |
| 65 | else: |
| 66 | print >>sys.stderr, 'error: Cannot fetch %s' % project.name |
| 67 | sys.exit(1) |
| 68 | return fetched |
| 69 | |
| 70 | def Execute(self, opt, args): |
| 71 | rp = self.manifest.repoProject |
| 72 | rp.PreSync() |
| 73 | |
| 74 | mp = self.manifest.manifestProject |
| 75 | mp.PreSync() |
| 76 | |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 77 | if opt.repo_upgraded: |
| 78 | for project in self.manifest.projects.values(): |
| 79 | if project.Exists: |
| 80 | project.PostRepoUpgrade() |
| 81 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 82 | all = self.GetProjects(args, missing_ok=True) |
| 83 | fetched = self._Fetch(rp, mp, *all) |
| 84 | |
| 85 | if rp.HasChanges: |
| 86 | print >>sys.stderr, 'info: A new version of repo is available' |
| 87 | print >>sys.stderr, '' |
| 88 | if opt.no_repo_verify or _VerifyTag(rp): |
| 89 | if not rp.Sync_LocalHalf(): |
| 90 | sys.exit(1) |
| 91 | print >>sys.stderr, 'info: Restarting repo with latest version' |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 92 | raise RepoChangedException(['--repo-upgraded']) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 93 | else: |
| 94 | print >>sys.stderr, 'warning: Skipped upgrade to unverified version' |
| 95 | |
Shawn O. Pearce | 96fdcef | 2009-04-10 16:29:20 -0700 | [diff] [blame^] | 96 | if opt.network_only: |
| 97 | # bail out now; the rest touches the working tree |
| 98 | return |
| 99 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 100 | if mp.HasChanges: |
| 101 | if not mp.Sync_LocalHalf(): |
| 102 | sys.exit(1) |
| 103 | |
| 104 | self.manifest._Unload() |
| 105 | all = self.GetProjects(args, missing_ok=True) |
| 106 | missing = [] |
| 107 | for project in all: |
| 108 | if project.gitdir not in fetched: |
| 109 | missing.append(project) |
| 110 | self._Fetch(*missing) |
| 111 | |
| 112 | for project in all: |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 113 | if project.worktree: |
| 114 | if not project.Sync_LocalHalf(): |
| 115 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 116 | |
| 117 | |
| 118 | def _VerifyTag(project): |
| 119 | gpg_dir = os.path.expanduser('~/.repoconfig/gnupg') |
| 120 | if not os.path.exists(gpg_dir): |
| 121 | print >>sys.stderr,\ |
| 122 | """warning: GnuPG was not available during last "repo init" |
| 123 | warning: Cannot automatically authenticate repo.""" |
| 124 | return True |
| 125 | |
| 126 | remote = project.GetRemote(project.remote.name) |
| 127 | ref = remote.ToLocal(project.revision) |
| 128 | |
| 129 | try: |
| 130 | cur = project.bare_git.describe(ref) |
| 131 | except GitError: |
| 132 | cur = None |
| 133 | |
| 134 | if not cur \ |
| 135 | or re.compile(r'^.*-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur): |
| 136 | rev = project.revision |
| 137 | if rev.startswith(R_HEADS): |
| 138 | rev = rev[len(R_HEADS):] |
| 139 | |
| 140 | print >>sys.stderr |
| 141 | print >>sys.stderr,\ |
| 142 | "warning: project '%s' branch '%s' is not signed" \ |
| 143 | % (project.name, rev) |
| 144 | return False |
| 145 | |
| 146 | env = dict(os.environ) |
| 147 | env['GIT_DIR'] = project.gitdir |
| 148 | env['GNUPGHOME'] = gpg_dir |
| 149 | |
| 150 | cmd = [GIT, 'tag', '-v', cur] |
| 151 | proc = subprocess.Popen(cmd, |
| 152 | stdout = subprocess.PIPE, |
| 153 | stderr = subprocess.PIPE, |
| 154 | env = env) |
| 155 | out = proc.stdout.read() |
| 156 | proc.stdout.close() |
| 157 | |
| 158 | err = proc.stderr.read() |
| 159 | proc.stderr.close() |
| 160 | |
| 161 | if proc.wait() != 0: |
| 162 | print >>sys.stderr |
| 163 | print >>sys.stderr, out |
| 164 | print >>sys.stderr, err |
| 165 | print >>sys.stderr |
| 166 | return False |
| 167 | return True |