blob: 9af123226c8275098a6df5ced833ef619fb93626 [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
16import os
17import re
18import subprocess
19import sys
20
21from git_command import GIT
22from command import Command
23from error import RepoChangedException, GitError
24from project import R_HEADS
25
26class Sync(Command):
27 common = True
28 helpSummary = "Update working tree to the latest revision"
29 helpUsage = """
30%prog [<project>...]
31"""
32 helpDescription = """
33The '%prog' command synchronizes local project directories
34with the remote repositories specified in the manifest. If a local
35project does not yet exist, it will clone a new local directory from
36the remote repository and set up tracking branches as specified in
37the manifest. If the local project already exists, '%prog'
38will update the remote branches and rebase any new local changes
39on top of the new remote changes.
40
41'%prog' will synchronize all projects listed at the command
42line. Projects can be specified either by name, or by a relative
43or absolute path to the project's local directory. If no projects
44are specified, '%prog' will synchronize all projects listed in
45the manifest.
46"""
47
48 def _Options(self, p):
49 p.add_option('--no-repo-verify',
50 dest='no_repo_verify', action='store_true',
51 help='do not verify repo source code')
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080052 p.add_option('--repo-upgraded',
53 dest='repo_upgraded', action='store_true',
54 help='perform additional actions after a repo upgrade')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070055
56 def _Fetch(self, *projects):
57 fetched = set()
58 for project in projects:
59 if project.Sync_NetworkHalf():
60 fetched.add(project.gitdir)
61 else:
62 print >>sys.stderr, 'error: Cannot fetch %s' % project.name
63 sys.exit(1)
64 return fetched
65
66 def Execute(self, opt, args):
67 rp = self.manifest.repoProject
68 rp.PreSync()
69
70 mp = self.manifest.manifestProject
71 mp.PreSync()
72
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080073 if opt.repo_upgraded:
74 for project in self.manifest.projects.values():
75 if project.Exists:
76 project.PostRepoUpgrade()
77
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070078 all = self.GetProjects(args, missing_ok=True)
79 fetched = self._Fetch(rp, mp, *all)
80
81 if rp.HasChanges:
82 print >>sys.stderr, 'info: A new version of repo is available'
83 print >>sys.stderr, ''
84 if opt.no_repo_verify or _VerifyTag(rp):
85 if not rp.Sync_LocalHalf():
86 sys.exit(1)
87 print >>sys.stderr, 'info: Restarting repo with latest version'
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080088 raise RepoChangedException(['--repo-upgraded'])
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070089 else:
90 print >>sys.stderr, 'warning: Skipped upgrade to unverified version'
91
92 if mp.HasChanges:
93 if not mp.Sync_LocalHalf():
94 sys.exit(1)
95
96 self.manifest._Unload()
97 all = self.GetProjects(args, missing_ok=True)
98 missing = []
99 for project in all:
100 if project.gitdir not in fetched:
101 missing.append(project)
102 self._Fetch(*missing)
103
104 for project in all:
105 if not project.Sync_LocalHalf():
106 sys.exit(1)
107
108
109def _VerifyTag(project):
110 gpg_dir = os.path.expanduser('~/.repoconfig/gnupg')
111 if not os.path.exists(gpg_dir):
112 print >>sys.stderr,\
113"""warning: GnuPG was not available during last "repo init"
114warning: Cannot automatically authenticate repo."""
115 return True
116
117 remote = project.GetRemote(project.remote.name)
118 ref = remote.ToLocal(project.revision)
119
120 try:
121 cur = project.bare_git.describe(ref)
122 except GitError:
123 cur = None
124
125 if not cur \
126 or re.compile(r'^.*-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur):
127 rev = project.revision
128 if rev.startswith(R_HEADS):
129 rev = rev[len(R_HEADS):]
130
131 print >>sys.stderr
132 print >>sys.stderr,\
133 "warning: project '%s' branch '%s' is not signed" \
134 % (project.name, rev)
135 return False
136
137 env = dict(os.environ)
138 env['GIT_DIR'] = project.gitdir
139 env['GNUPGHOME'] = gpg_dir
140
141 cmd = [GIT, 'tag', '-v', cur]
142 proc = subprocess.Popen(cmd,
143 stdout = subprocess.PIPE,
144 stderr = subprocess.PIPE,
145 env = env)
146 out = proc.stdout.read()
147 proc.stdout.close()
148
149 err = proc.stderr.read()
150 proc.stderr.close()
151
152 if proc.wait() != 0:
153 print >>sys.stderr
154 print >>sys.stderr, out
155 print >>sys.stderr, err
156 print >>sys.stderr
157 return False
158 return True