blob: ec5ada21446ead3e3b238604c0933370ab7332d2 [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001#
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. Pearce2a1ccb22009-04-10 16:51:53 -070016from optparse import SUPPRESS_HELP
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070017import os
18import re
19import subprocess
20import sys
21
22from git_command import GIT
Shawn O. Pearcee756c412009-04-13 11:51:15 -070023from project import HEAD
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080024from command import Command, MirrorSafeCommand
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070025from error import RepoChangedException, GitError
26from project import R_HEADS
Shawn O. Pearce350cde42009-04-16 11:21:18 -070027from project import SyncBuffer
Shawn O. Pearce68194f42009-04-10 16:48:52 -070028from progress import Progress
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070029
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080030class Sync(Command, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070031 common = True
32 helpSummary = "Update working tree to the latest revision"
33 helpUsage = """
34%prog [<project>...]
35"""
36 helpDescription = """
37The '%prog' command synchronizes local project directories
38with the remote repositories specified in the manifest. If a local
39project does not yet exist, it will clone a new local directory from
40the remote repository and set up tracking branches as specified in
41the manifest. If the local project already exists, '%prog'
42will update the remote branches and rebase any new local changes
43on top of the new remote changes.
44
45'%prog' will synchronize all projects listed at the command
46line. Projects can be specified either by name, or by a relative
47or absolute path to the project's local directory. If no projects
48are specified, '%prog' will synchronize all projects listed in
49the manifest.
Shawn O. Pearce3e768c92009-04-10 16:59:36 -070050
51The -d/--detach option can be used to switch specified projects
52back to the manifest revision. This option is especially helpful
53if the project is currently on a topic branch, but the manifest
54revision is temporarily needed.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070055"""
56
57 def _Options(self, p):
Shawn O. Pearceb1562fa2009-04-10 17:04:08 -070058 p.add_option('-l','--local-only',
59 dest='local_only', action='store_true',
60 help="only update working tree, don't fetch")
Shawn O. Pearce96fdcef2009-04-10 16:29:20 -070061 p.add_option('-n','--network-only',
62 dest='network_only', action='store_true',
63 help="fetch only, don't update working tree")
Shawn O. Pearce3e768c92009-04-10 16:59:36 -070064 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 Projectcf31fe92008-10-21 07:00:00 -070068 p.add_option('--no-repo-verify',
69 dest='no_repo_verify', action='store_true',
70 help='do not verify repo source code')
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080071 p.add_option('--repo-upgraded',
72 dest='repo_upgraded', action='store_true',
Shawn O. Pearce2a1ccb22009-04-10 16:51:53 -070073 help=SUPPRESS_HELP)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070074
75 def _Fetch(self, *projects):
76 fetched = set()
Shawn O. Pearce68194f42009-04-10 16:48:52 -070077 pm = Progress('Fetching projects', len(projects))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070078 for project in projects:
Shawn O. Pearce68194f42009-04-10 16:48:52 -070079 pm.update()
80
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070081 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. Pearce68194f42009-04-10 16:48:52 -070086 pm.end()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070087 return fetched
88
89 def Execute(self, opt, args):
Shawn O. Pearce3e768c92009-04-10 16:59:36 -070090 if opt.network_only and opt.detach_head:
91 print >>sys.stderr, 'error: cannot combine -n and -d'
92 sys.exit(1)
Shawn O. Pearceb1562fa2009-04-10 17:04:08 -070093 if opt.network_only and opt.local_only:
94 print >>sys.stderr, 'error: cannot combine -n and -l'
95 sys.exit(1)
Shawn O. Pearce3e768c92009-04-10 16:59:36 -070096
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070097 rp = self.manifest.repoProject
98 rp.PreSync()
99
100 mp = self.manifest.manifestProject
101 mp.PreSync()
102
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800103 if opt.repo_upgraded:
Shawn O. Pearcee756c412009-04-13 11:51:15 -0700104 _PostRepoUpgrade(self.manifest)
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800105
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700106 all = self.GetProjects(args, missing_ok=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700107
Shawn O. Pearceb1562fa2009-04-10 17:04:08 -0700108 if not opt.local_only:
109 fetched = self._Fetch(rp, mp, *all)
Shawn O. Pearcee756c412009-04-13 11:51:15 -0700110 _PostRepoFetch(rp, opt.no_repo_verify)
Shawn O. Pearceb1562fa2009-04-10 17:04:08 -0700111 if opt.network_only:
112 # bail out now; the rest touches the working tree
113 return
114
115 if mp.HasChanges:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700116 syncbuf = SyncBuffer(mp.config)
117 mp.Sync_LocalHalf(syncbuf)
118 if not syncbuf.Finish():
Shawn O. Pearceb1562fa2009-04-10 17:04:08 -0700119 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700120
Shawn O. Pearceb1562fa2009-04-10 17:04:08 -0700121 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 Projectcf31fe92008-10-21 07:00:00 -0700128
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700129 syncbuf = SyncBuffer(mp.config,
130 detach_head = opt.detach_head)
Shawn O. Pearce68194f42009-04-10 16:48:52 -0700131 pm = Progress('Syncing work tree', len(all))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700132 for project in all:
Shawn O. Pearce68194f42009-04-10 16:48:52 -0700133 pm.update()
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800134 if project.worktree:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700135 project.Sync_LocalHalf(syncbuf)
Shawn O. Pearce68194f42009-04-10 16:48:52 -0700136 pm.end()
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700137 print >>sys.stderr
138 if not syncbuf.Finish():
139 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700140
Shawn O. Pearcee756c412009-04-13 11:51:15 -0700141
142def _PostRepoUpgrade(manifest):
143 for project in manifest.projects.values():
144 if project.Exists:
145 project.PostRepoUpgrade()
146
147def _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. Pearce350cde42009-04-16 11:21:18 -0700152 syncbuf = SyncBuffer(rp.config)
153 rp.Sync_LocalHalf(syncbuf)
154 if not syncbuf.Finish():
Shawn O. Pearcee756c412009-04-13 11:51:15 -0700155 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 Projectcf31fe92008-10-21 07:00:00 -0700164def _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"
169warning: 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