blob: 79ffcf513eae6e952bcfe517e9b72139b24cf177 [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. Pearcec95583b2009-03-03 17:47:06 -080023from command import Command, MirrorSafeCommand
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070024from error import RepoChangedException, GitError
25from project import R_HEADS
Shawn O. Pearce68194f42009-04-10 16:48:52 -070026from progress import Progress
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070027
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080028class Sync(Command, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070029 common = True
30 helpSummary = "Update working tree to the latest revision"
31 helpUsage = """
32%prog [<project>...]
33"""
34 helpDescription = """
35The '%prog' command synchronizes local project directories
36with the remote repositories specified in the manifest. If a local
37project does not yet exist, it will clone a new local directory from
38the remote repository and set up tracking branches as specified in
39the manifest. If the local project already exists, '%prog'
40will update the remote branches and rebase any new local changes
41on top of the new remote changes.
42
43'%prog' will synchronize all projects listed at the command
44line. Projects can be specified either by name, or by a relative
45or absolute path to the project's local directory. If no projects
46are specified, '%prog' will synchronize all projects listed in
47the manifest.
Shawn O. Pearce3e768c92009-04-10 16:59:36 -070048
49The -d/--detach option can be used to switch specified projects
50back to the manifest revision. This option is especially helpful
51if the project is currently on a topic branch, but the manifest
52revision is temporarily needed.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070053"""
54
55 def _Options(self, p):
Shawn O. Pearceb1562fa2009-04-10 17:04:08 -070056 p.add_option('-l','--local-only',
57 dest='local_only', action='store_true',
58 help="only update working tree, don't fetch")
Shawn O. Pearce96fdcef2009-04-10 16:29:20 -070059 p.add_option('-n','--network-only',
60 dest='network_only', action='store_true',
61 help="fetch only, don't update working tree")
Shawn O. Pearce3e768c92009-04-10 16:59:36 -070062 p.add_option('-d','--detach',
63 dest='detach_head', action='store_true',
64 help='detach projects back to manifest revision')
65
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070066 p.add_option('--no-repo-verify',
67 dest='no_repo_verify', action='store_true',
68 help='do not verify repo source code')
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080069 p.add_option('--repo-upgraded',
70 dest='repo_upgraded', action='store_true',
Shawn O. Pearce2a1ccb22009-04-10 16:51:53 -070071 help=SUPPRESS_HELP)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070072
73 def _Fetch(self, *projects):
74 fetched = set()
Shawn O. Pearce68194f42009-04-10 16:48:52 -070075 pm = Progress('Fetching projects', len(projects))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070076 for project in projects:
Shawn O. Pearce68194f42009-04-10 16:48:52 -070077 pm.update()
78
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070079 if project.Sync_NetworkHalf():
80 fetched.add(project.gitdir)
81 else:
82 print >>sys.stderr, 'error: Cannot fetch %s' % project.name
83 sys.exit(1)
Shawn O. Pearce68194f42009-04-10 16:48:52 -070084 pm.end()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070085 return fetched
86
87 def Execute(self, opt, args):
Shawn O. Pearce3e768c92009-04-10 16:59:36 -070088 if opt.network_only and opt.detach_head:
89 print >>sys.stderr, 'error: cannot combine -n and -d'
90 sys.exit(1)
Shawn O. Pearceb1562fa2009-04-10 17:04:08 -070091 if opt.network_only and opt.local_only:
92 print >>sys.stderr, 'error: cannot combine -n and -l'
93 sys.exit(1)
Shawn O. Pearce3e768c92009-04-10 16:59:36 -070094
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070095 rp = self.manifest.repoProject
96 rp.PreSync()
97
98 mp = self.manifest.manifestProject
99 mp.PreSync()
100
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800101 if opt.repo_upgraded:
102 for project in self.manifest.projects.values():
103 if project.Exists:
104 project.PostRepoUpgrade()
105
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)
110
111 if rp.HasChanges:
112 print >>sys.stderr, 'info: A new version of repo is available'
113 print >>sys.stderr, ''
114 if opt.no_repo_verify or _VerifyTag(rp):
115 if not rp.Sync_LocalHalf():
116 sys.exit(1)
117 print >>sys.stderr, 'info: Restarting repo with latest version'
118 raise RepoChangedException(['--repo-upgraded'])
119 else:
120 print >>sys.stderr, 'warning: Skipped upgrade to unverified version'
121
122 if opt.network_only:
123 # bail out now; the rest touches the working tree
124 return
125
126 if mp.HasChanges:
127 if not mp.Sync_LocalHalf():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700128 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700129
Shawn O. Pearceb1562fa2009-04-10 17:04:08 -0700130 self.manifest._Unload()
131 all = self.GetProjects(args, missing_ok=True)
132 missing = []
133 for project in all:
134 if project.gitdir not in fetched:
135 missing.append(project)
136 self._Fetch(*missing)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700137
Shawn O. Pearce68194f42009-04-10 16:48:52 -0700138 pm = Progress('Syncing work tree', len(all))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700139 for project in all:
Shawn O. Pearce68194f42009-04-10 16:48:52 -0700140 pm.update()
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800141 if project.worktree:
Shawn O. Pearce3e768c92009-04-10 16:59:36 -0700142 if not project.Sync_LocalHalf(
143 detach_head=opt.detach_head):
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800144 sys.exit(1)
Shawn O. Pearce68194f42009-04-10 16:48:52 -0700145 pm.end()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700146
147def _VerifyTag(project):
148 gpg_dir = os.path.expanduser('~/.repoconfig/gnupg')
149 if not os.path.exists(gpg_dir):
150 print >>sys.stderr,\
151"""warning: GnuPG was not available during last "repo init"
152warning: Cannot automatically authenticate repo."""
153 return True
154
155 remote = project.GetRemote(project.remote.name)
156 ref = remote.ToLocal(project.revision)
157
158 try:
159 cur = project.bare_git.describe(ref)
160 except GitError:
161 cur = None
162
163 if not cur \
164 or re.compile(r'^.*-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur):
165 rev = project.revision
166 if rev.startswith(R_HEADS):
167 rev = rev[len(R_HEADS):]
168
169 print >>sys.stderr
170 print >>sys.stderr,\
171 "warning: project '%s' branch '%s' is not signed" \
172 % (project.name, rev)
173 return False
174
175 env = dict(os.environ)
176 env['GIT_DIR'] = project.gitdir
177 env['GNUPGHOME'] = gpg_dir
178
179 cmd = [GIT, 'tag', '-v', cur]
180 proc = subprocess.Popen(cmd,
181 stdout = subprocess.PIPE,
182 stderr = subprocess.PIPE,
183 env = env)
184 out = proc.stdout.read()
185 proc.stdout.close()
186
187 err = proc.stderr.read()
188 proc.stderr.close()
189
190 if proc.wait() != 0:
191 print >>sys.stderr
192 print >>sys.stderr, out
193 print >>sys.stderr, err
194 print >>sys.stderr
195 return False
196 return True