blob: 55ffca3e93314d7edcb85131122d8e938eff20c7 [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
Shawn O. Pearcef6906872009-04-18 10:49:00 -070021import time
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070022
23from git_command import GIT
Shawn O. Pearcee756c412009-04-13 11:51:15 -070024from project import HEAD
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080025from command import Command, MirrorSafeCommand
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070026from error import RepoChangedException, GitError
27from project import R_HEADS
Shawn O. Pearce350cde42009-04-16 11:21:18 -070028from project import SyncBuffer
Shawn O. Pearce68194f42009-04-10 16:48:52 -070029from progress import Progress
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070030
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080031class Sync(Command, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070032 common = True
33 helpSummary = "Update working tree to the latest revision"
34 helpUsage = """
35%prog [<project>...]
36"""
37 helpDescription = """
38The '%prog' command synchronizes local project directories
39with the remote repositories specified in the manifest. If a local
40project does not yet exist, it will clone a new local directory from
41the remote repository and set up tracking branches as specified in
42the manifest. If the local project already exists, '%prog'
43will update the remote branches and rebase any new local changes
44on top of the new remote changes.
45
46'%prog' will synchronize all projects listed at the command
47line. Projects can be specified either by name, or by a relative
48or absolute path to the project's local directory. If no projects
49are specified, '%prog' will synchronize all projects listed in
50the manifest.
Shawn O. Pearce3e768c92009-04-10 16:59:36 -070051
52The -d/--detach option can be used to switch specified projects
53back to the manifest revision. This option is especially helpful
54if the project is currently on a topic branch, but the manifest
55revision is temporarily needed.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070056"""
57
58 def _Options(self, p):
Shawn O. Pearceb1562fa2009-04-10 17:04:08 -070059 p.add_option('-l','--local-only',
60 dest='local_only', action='store_true',
61 help="only update working tree, don't fetch")
Shawn O. Pearce96fdcef2009-04-10 16:29:20 -070062 p.add_option('-n','--network-only',
63 dest='network_only', action='store_true',
64 help="fetch only, don't update working tree")
Shawn O. Pearce3e768c92009-04-10 16:59:36 -070065 p.add_option('-d','--detach',
66 dest='detach_head', action='store_true',
67 help='detach projects back to manifest revision')
68
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070069 p.add_option('--no-repo-verify',
70 dest='no_repo_verify', action='store_true',
71 help='do not verify repo source code')
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080072 p.add_option('--repo-upgraded',
73 dest='repo_upgraded', action='store_true',
Shawn O. Pearce2a1ccb22009-04-10 16:51:53 -070074 help=SUPPRESS_HELP)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070075
Shawn O. Pearcef6906872009-04-18 10:49:00 -070076 def _Fetch(self, projects):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070077 fetched = set()
Shawn O. Pearce68194f42009-04-10 16:48:52 -070078 pm = Progress('Fetching projects', len(projects))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070079 for project in projects:
Shawn O. Pearce68194f42009-04-10 16:48:52 -070080 pm.update()
81
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070082 if project.Sync_NetworkHalf():
83 fetched.add(project.gitdir)
84 else:
85 print >>sys.stderr, 'error: Cannot fetch %s' % project.name
86 sys.exit(1)
Shawn O. Pearce68194f42009-04-10 16:48:52 -070087 pm.end()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070088 return fetched
89
90 def Execute(self, opt, args):
Shawn O. Pearce3e768c92009-04-10 16:59:36 -070091 if opt.network_only and opt.detach_head:
92 print >>sys.stderr, 'error: cannot combine -n and -d'
93 sys.exit(1)
Shawn O. Pearceb1562fa2009-04-10 17:04:08 -070094 if opt.network_only and opt.local_only:
95 print >>sys.stderr, 'error: cannot combine -n and -l'
96 sys.exit(1)
Shawn O. Pearce3e768c92009-04-10 16:59:36 -070097
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070098 rp = self.manifest.repoProject
99 rp.PreSync()
100
101 mp = self.manifest.manifestProject
102 mp.PreSync()
103
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800104 if opt.repo_upgraded:
Shawn O. Pearcee756c412009-04-13 11:51:15 -0700105 _PostRepoUpgrade(self.manifest)
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800106
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700107 all = self.GetProjects(args, missing_ok=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700108
Shawn O. Pearceb1562fa2009-04-10 17:04:08 -0700109 if not opt.local_only:
Shawn O. Pearcef6906872009-04-18 10:49:00 -0700110 to_fetch = []
111 now = time.time()
112 if (24 * 60 * 60) <= (now - rp.LastFetch):
113 to_fetch.append(rp)
114 to_fetch.append(mp)
115 to_fetch.extend(all)
116
117 fetched = self._Fetch(to_fetch)
Shawn O. Pearcee756c412009-04-13 11:51:15 -0700118 _PostRepoFetch(rp, opt.no_repo_verify)
Shawn O. Pearceb1562fa2009-04-10 17:04:08 -0700119 if opt.network_only:
120 # bail out now; the rest touches the working tree
121 return
122
123 if mp.HasChanges:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700124 syncbuf = SyncBuffer(mp.config)
125 mp.Sync_LocalHalf(syncbuf)
126 if not syncbuf.Finish():
Shawn O. Pearceb1562fa2009-04-10 17:04:08 -0700127 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700128
Shawn O. Pearceb1562fa2009-04-10 17:04:08 -0700129 self.manifest._Unload()
130 all = self.GetProjects(args, missing_ok=True)
131 missing = []
132 for project in all:
133 if project.gitdir not in fetched:
134 missing.append(project)
Shawn O. Pearcef6906872009-04-18 10:49:00 -0700135 self._Fetch(missing)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700136
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700137 syncbuf = SyncBuffer(mp.config,
138 detach_head = opt.detach_head)
Shawn O. Pearce68194f42009-04-10 16:48:52 -0700139 pm = Progress('Syncing work tree', len(all))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700140 for project in all:
Shawn O. Pearce68194f42009-04-10 16:48:52 -0700141 pm.update()
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800142 if project.worktree:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700143 project.Sync_LocalHalf(syncbuf)
Shawn O. Pearce68194f42009-04-10 16:48:52 -0700144 pm.end()
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700145 print >>sys.stderr
146 if not syncbuf.Finish():
147 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700148
Shawn O. Pearcee756c412009-04-13 11:51:15 -0700149
150def _PostRepoUpgrade(manifest):
151 for project in manifest.projects.values():
152 if project.Exists:
153 project.PostRepoUpgrade()
154
155def _PostRepoFetch(rp, no_repo_verify=False, verbose=False):
156 if rp.HasChanges:
157 print >>sys.stderr, 'info: A new version of repo is available'
158 print >>sys.stderr, ''
159 if no_repo_verify or _VerifyTag(rp):
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700160 syncbuf = SyncBuffer(rp.config)
161 rp.Sync_LocalHalf(syncbuf)
162 if not syncbuf.Finish():
Shawn O. Pearcee756c412009-04-13 11:51:15 -0700163 sys.exit(1)
164 print >>sys.stderr, 'info: Restarting repo with latest version'
165 raise RepoChangedException(['--repo-upgraded'])
166 else:
167 print >>sys.stderr, 'warning: Skipped upgrade to unverified version'
168 else:
169 if verbose:
170 print >>sys.stderr, 'repo version %s is current' % rp.work_git.describe(HEAD)
171
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700172def _VerifyTag(project):
173 gpg_dir = os.path.expanduser('~/.repoconfig/gnupg')
174 if not os.path.exists(gpg_dir):
175 print >>sys.stderr,\
176"""warning: GnuPG was not available during last "repo init"
177warning: Cannot automatically authenticate repo."""
178 return True
179
180 remote = project.GetRemote(project.remote.name)
181 ref = remote.ToLocal(project.revision)
182
183 try:
184 cur = project.bare_git.describe(ref)
185 except GitError:
186 cur = None
187
188 if not cur \
189 or re.compile(r'^.*-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur):
190 rev = project.revision
191 if rev.startswith(R_HEADS):
192 rev = rev[len(R_HEADS):]
193
194 print >>sys.stderr
195 print >>sys.stderr,\
196 "warning: project '%s' branch '%s' is not signed" \
197 % (project.name, rev)
198 return False
199
200 env = dict(os.environ)
201 env['GIT_DIR'] = project.gitdir
202 env['GNUPGHOME'] = gpg_dir
203
204 cmd = [GIT, 'tag', '-v', cur]
205 proc = subprocess.Popen(cmd,
206 stdout = subprocess.PIPE,
207 stderr = subprocess.PIPE,
208 env = env)
209 out = proc.stdout.read()
210 proc.stdout.close()
211
212 err = proc.stderr.read()
213 proc.stderr.close()
214
215 if proc.wait() != 0:
216 print >>sys.stderr
217 print >>sys.stderr, out
218 print >>sys.stderr, err
219 print >>sys.stderr
220 return False
221 return True