blob: d3f6adc10347e4c2b2933531c22ff05da20e97ad [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):
50 p.add_option('--no-repo-verify',
51 dest='no_repo_verify', action='store_true',
52 help='do not verify repo source code')
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080053 p.add_option('--repo-upgraded',
54 dest='repo_upgraded', action='store_true',
Shawn O. Pearce2a1ccb22009-04-10 16:51:53 -070055 help=SUPPRESS_HELP)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070056
57 def _Fetch(self, *projects):
58 fetched = set()
59 for project in projects:
60 if project.Sync_NetworkHalf():
61 fetched.add(project.gitdir)
62 else:
63 print >>sys.stderr, 'error: Cannot fetch %s' % project.name
64 sys.exit(1)
65 return fetched
66
67 def Execute(self, opt, args):
68 rp = self.manifest.repoProject
69 rp.PreSync()
70
71 mp = self.manifest.manifestProject
72 mp.PreSync()
73
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080074 if opt.repo_upgraded:
75 for project in self.manifest.projects.values():
76 if project.Exists:
77 project.PostRepoUpgrade()
78
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070079 all = self.GetProjects(args, missing_ok=True)
80 fetched = self._Fetch(rp, mp, *all)
81
82 if rp.HasChanges:
83 print >>sys.stderr, 'info: A new version of repo is available'
84 print >>sys.stderr, ''
85 if opt.no_repo_verify or _VerifyTag(rp):
86 if not rp.Sync_LocalHalf():
87 sys.exit(1)
88 print >>sys.stderr, 'info: Restarting repo with latest version'
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080089 raise RepoChangedException(['--repo-upgraded'])
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070090 else:
91 print >>sys.stderr, 'warning: Skipped upgrade to unverified version'
92
93 if mp.HasChanges:
94 if not mp.Sync_LocalHalf():
95 sys.exit(1)
96
97 self.manifest._Unload()
98 all = self.GetProjects(args, missing_ok=True)
99 missing = []
100 for project in all:
101 if project.gitdir not in fetched:
102 missing.append(project)
103 self._Fetch(*missing)
104
105 for project in all:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800106 if project.worktree:
107 if not project.Sync_LocalHalf():
108 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700109
110
111def _VerifyTag(project):
112 gpg_dir = os.path.expanduser('~/.repoconfig/gnupg')
113 if not os.path.exists(gpg_dir):
114 print >>sys.stderr,\
115"""warning: GnuPG was not available during last "repo init"
116warning: Cannot automatically authenticate repo."""
117 return True
118
119 remote = project.GetRemote(project.remote.name)
120 ref = remote.ToLocal(project.revision)
121
122 try:
123 cur = project.bare_git.describe(ref)
124 except GitError:
125 cur = None
126
127 if not cur \
128 or re.compile(r'^.*-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur):
129 rev = project.revision
130 if rev.startswith(R_HEADS):
131 rev = rev[len(R_HEADS):]
132
133 print >>sys.stderr
134 print >>sys.stderr,\
135 "warning: project '%s' branch '%s' is not signed" \
136 % (project.name, rev)
137 return False
138
139 env = dict(os.environ)
140 env['GIT_DIR'] = project.gitdir
141 env['GNUPGHOME'] = gpg_dir
142
143 cmd = [GIT, 'tag', '-v', cur]
144 proc = subprocess.Popen(cmd,
145 stdout = subprocess.PIPE,
146 stderr = subprocess.PIPE,
147 env = env)
148 out = proc.stdout.read()
149 proc.stdout.close()
150
151 err = proc.stderr.read()
152 proc.stderr.close()
153
154 if proc.wait() != 0:
155 print >>sys.stderr
156 print >>sys.stderr, out
157 print >>sys.stderr, err
158 print >>sys.stderr
159 return False
160 return True