blob: 01e2eba65057531eb081279dc20fdb1c3d0cb82c [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.
47"""
48
49 def _Options(self, p):
Shawn O. Pearce96fdcef2009-04-10 16:29:20 -070050 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 Projectcf31fe92008-10-21 07:00:00 -070053 p.add_option('--no-repo-verify',
54 dest='no_repo_verify', action='store_true',
55 help='do not verify repo source code')
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080056 p.add_option('--repo-upgraded',
57 dest='repo_upgraded', action='store_true',
Shawn O. Pearce2a1ccb22009-04-10 16:51:53 -070058 help=SUPPRESS_HELP)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070059
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. Pearcec9ef7442008-11-03 10:32:09 -080077 if opt.repo_upgraded:
78 for project in self.manifest.projects.values():
79 if project.Exists:
80 project.PostRepoUpgrade()
81
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070082 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. Pearcec9ef7442008-11-03 10:32:09 -080092 raise RepoChangedException(['--repo-upgraded'])
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070093 else:
94 print >>sys.stderr, 'warning: Skipped upgrade to unverified version'
95
Shawn O. Pearce96fdcef2009-04-10 16:29:20 -070096 if opt.network_only:
97 # bail out now; the rest touches the working tree
98 return
99
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700100 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. Pearcee284ad12008-11-04 07:37:10 -0800113 if project.worktree:
114 if not project.Sync_LocalHalf():
115 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700116
117
118def _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"
123warning: 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