blob: 542b4c2028a0e193c635b44b59359a8232e89658 [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
26
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080027class Sync(Command, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070028 common = True
29 helpSummary = "Update working tree to the latest revision"
30 helpUsage = """
31%prog [<project>...]
32"""
33 helpDescription = """
34The '%prog' command synchronizes local project directories
35with the remote repositories specified in the manifest. If a local
36project does not yet exist, it will clone a new local directory from
37the remote repository and set up tracking branches as specified in
38the manifest. If the local project already exists, '%prog'
39will update the remote branches and rebase any new local changes
40on top of the new remote changes.
41
42'%prog' will synchronize all projects listed at the command
43line. Projects can be specified either by name, or by a relative
44or absolute path to the project's local directory. If no projects
45are specified, '%prog' will synchronize all projects listed in
46the manifest.
Shawn O. Pearce3e768c92009-04-10 16:59:36 -070047
48The -d/--detach option can be used to switch specified projects
49back to the manifest revision. This option is especially helpful
50if the project is currently on a topic branch, but the manifest
51revision is temporarily needed.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070052"""
53
54 def _Options(self, p):
Shawn O. Pearce96fdcef2009-04-10 16:29:20 -070055 p.add_option('-n','--network-only',
56 dest='network_only', action='store_true',
57 help="fetch only, don't update working tree")
Shawn O. Pearce3e768c92009-04-10 16:59:36 -070058 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 Projectcf31fe92008-10-21 07:00:00 -070062 p.add_option('--no-repo-verify',
63 dest='no_repo_verify', action='store_true',
64 help='do not verify repo source code')
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080065 p.add_option('--repo-upgraded',
66 dest='repo_upgraded', action='store_true',
Shawn O. Pearce2a1ccb22009-04-10 16:51:53 -070067 help=SUPPRESS_HELP)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070068
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. Pearce3e768c92009-04-10 16:59:36 -070080 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 Projectcf31fe92008-10-21 07:00:00 -070084 rp = self.manifest.repoProject
85 rp.PreSync()
86
87 mp = self.manifest.manifestProject
88 mp.PreSync()
89
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080090 if opt.repo_upgraded:
91 for project in self.manifest.projects.values():
92 if project.Exists:
93 project.PostRepoUpgrade()
94
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070095 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. Pearcec9ef7442008-11-03 10:32:09 -0800105 raise RepoChangedException(['--repo-upgraded'])
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700106 else:
107 print >>sys.stderr, 'warning: Skipped upgrade to unverified version'
108
Shawn O. Pearce96fdcef2009-04-10 16:29:20 -0700109 if opt.network_only:
110 # bail out now; the rest touches the working tree
111 return
112
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700113 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. Pearcee284ad12008-11-04 07:37:10 -0800126 if project.worktree:
Shawn O. Pearce3e768c92009-04-10 16:59:36 -0700127 if not project.Sync_LocalHalf(
128 detach_head=opt.detach_head):
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800129 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700130
131
132def _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"
137warning: 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