blob: 2ca4e163241fa6f21f8b0e66afa694332c0e74fc [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 sys
18
19from color import Coloring
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080020from command import InteractiveCommand, MirrorSafeCommand
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070021from error import ManifestParseError
Shawn O. Pearce350cde42009-04-16 11:21:18 -070022from project import SyncBuffer
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -070023from git_command import git_require, MIN_GIT_VERSION
Shawn O. Pearce0125ae22009-07-03 18:05:23 -070024from manifest_submodule import SubmoduleManifest
Shawn O. Pearce446c4e52009-05-19 18:14:04 -070025from manifest_xml import XmlManifest
Shawn O. Pearce87bda122009-07-03 16:37:30 -070026from subcmds.sync import _ReloadManifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070027
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080028class Init(InteractiveCommand, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070029 common = True
30 helpSummary = "Initialize repo in the current directory"
31 helpUsage = """
32%prog [options]
33"""
34 helpDescription = """
35The '%prog' command is run once to install and initialize repo.
36The latest repo source code and manifest collection is downloaded
37from the server and is installed in the .repo/ directory in the
38current working directory.
39
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070040The optional -b argument can be used to select the manifest branch
41to checkout and use. If no branch is specified, master is assumed.
42
43The optional -m argument can be used to specify an alternate manifest
44to be used. If no manifest is specified, the manifest default.xml
45will be used.
46
Shawn O. Pearce88443382010-10-08 10:02:09 +020047The --reference option can be used to point to a directory that
48has the content of a --mirror sync. This will make the working
49directory use as much data as possible from the local reference
50directory when fetching from the server. This will make the sync
51go a lot faster by reducing data traffic on the network.
52
53
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070054Switching Manifest Branches
55---------------------------
56
57To switch to another manifest branch, `repo init -b otherbranch`
58may be used in an existing client. However, as this only updates the
59manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary
60to update the working directory files.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070061"""
62
63 def _Options(self, p):
64 # Logging
65 g = p.add_option_group('Logging options')
66 g.add_option('-q', '--quiet',
67 dest="quiet", action="store_true", default=False,
68 help="be quiet")
69
70 # Manifest
71 g = p.add_option_group('Manifest options')
72 g.add_option('-u', '--manifest-url',
73 dest='manifest_url',
74 help='manifest repository location', metavar='URL')
75 g.add_option('-b', '--manifest-branch',
76 dest='manifest_branch',
77 help='manifest branch or revision', metavar='REVISION')
Shawn O. Pearce5f947bb2009-07-03 17:24:17 -070078 g.add_option('-o', '--origin',
79 dest='manifest_origin',
80 help="use REMOTE instead of 'origin' to track upstream",
81 metavar='REMOTE')
Shawn O. Pearce446c4e52009-05-19 18:14:04 -070082 if isinstance(self.manifest, XmlManifest) \
83 or not self.manifest.manifestProject.Exists:
84 g.add_option('-m', '--manifest-name',
85 dest='manifest_name', default='default.xml',
86 help='initial manifest file', metavar='NAME.xml')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -080087 g.add_option('--mirror',
88 dest='mirror', action='store_true',
89 help='mirror the forrest')
Shawn O. Pearce88443382010-10-08 10:02:09 +020090 g.add_option('--reference',
91 dest='reference',
92 help='location of mirror directory', metavar='DIR')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070093
94 # Tool
Shawn O. Pearcefd89b672009-04-18 11:28:57 -070095 g = p.add_option_group('repo Version options')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070096 g.add_option('--repo-url',
97 dest='repo_url',
98 help='repo repository location', metavar='URL')
99 g.add_option('--repo-branch',
100 dest='repo_branch',
101 help='repo branch or revision', metavar='REVISION')
102 g.add_option('--no-repo-verify',
103 dest='no_repo_verify', action='store_true',
104 help='do not verify repo source code')
105
Shawn O. Pearce5f947bb2009-07-03 17:24:17 -0700106 def _ApplyOptions(self, opt, is_new):
107 m = self.manifest.manifestProject
108
109 if is_new:
110 if opt.manifest_origin:
111 m.remote.name = opt.manifest_origin
112
113 if opt.manifest_branch:
114 m.revisionExpr = opt.manifest_branch
115 else:
116 m.revisionExpr = 'refs/heads/master'
117 else:
118 if opt.manifest_origin:
119 print >>sys.stderr, 'fatal: cannot change origin name'
120 sys.exit(1)
121
122 if opt.manifest_branch:
123 m.revisionExpr = opt.manifest_branch
124 else:
125 m.PreSync()
126
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700127 def _SyncManifest(self, opt):
128 m = self.manifest.manifestProject
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700129 is_new = not m.Exists
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700130
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700131 if is_new:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700132 if not opt.manifest_url:
133 print >>sys.stderr, 'fatal: manifest url (-u) is required.'
134 sys.exit(1)
135
136 if not opt.quiet:
137 print >>sys.stderr, 'Getting manifest ...'
138 print >>sys.stderr, ' from %s' % opt.manifest_url
139 m._InitGitDir()
140
Shawn O. Pearce5f947bb2009-07-03 17:24:17 -0700141 self._ApplyOptions(opt, is_new)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700142 if opt.manifest_url:
143 r = m.GetRemote(m.remote.name)
144 r.url = opt.manifest_url
145 r.ResetFetch()
146 r.Save()
147
Shawn O. Pearce88443382010-10-08 10:02:09 +0200148 if opt.reference:
149 m.config.SetString('repo.reference', opt.reference)
150
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800151 if opt.mirror:
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700152 if is_new:
153 m.config.SetString('repo.mirror', 'true')
Shawn O. Pearce7354d882009-07-03 20:06:13 -0700154 m.config.ClearCache()
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700155 else:
156 print >>sys.stderr, 'fatal: --mirror not supported on existing client'
157 sys.exit(1)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800158
Shawn O. Pearce1fc99f42009-03-17 08:06:18 -0700159 if not m.Sync_NetworkHalf():
160 r = m.GetRemote(m.remote.name)
161 print >>sys.stderr, 'fatal: cannot obtain manifest %s' % r.url
162 sys.exit(1)
163
Shawn O. Pearce0125ae22009-07-03 18:05:23 -0700164 if is_new and SubmoduleManifest.IsBare(m):
165 new = self.GetManifest(reparse=True, type=SubmoduleManifest)
166 if m.gitdir != new.manifestProject.gitdir:
167 os.rename(m.gitdir, new.manifestProject.gitdir)
168 new = self.GetManifest(reparse=True, type=SubmoduleManifest)
169 m = new.manifestProject
170 self._ApplyOptions(opt, is_new)
171
Shawn O. Pearce87bda122009-07-03 16:37:30 -0700172 if not is_new:
173 # Force the manifest to load if it exists, the old graph
174 # may be needed inside of _ReloadManifest().
175 #
176 self.manifest.projects
177
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700178 syncbuf = SyncBuffer(m.config)
179 m.Sync_LocalHalf(syncbuf)
180 syncbuf.Finish()
181
Shawn O. Pearce13f3da52010-12-07 10:31:19 -0800182 if isinstance(self.manifest, XmlManifest):
183 self._LinkManifest(opt.manifest_name)
Shawn O. Pearce87bda122009-07-03 16:37:30 -0700184 _ReloadManifest(self)
Shawn O. Pearce13f3da52010-12-07 10:31:19 -0800185
Shawn O. Pearce87bda122009-07-03 16:37:30 -0700186 self._ApplyOptions(opt, is_new)
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700187
Shawn O. Pearce75b87c82009-07-03 16:24:57 -0700188 if not self.manifest.InitBranch():
189 print >>sys.stderr, 'fatal: cannot create branch in manifest'
190 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700191
192 def _LinkManifest(self, name):
193 if not name:
194 print >>sys.stderr, 'fatal: manifest name (-m) is required.'
195 sys.exit(1)
196
197 try:
198 self.manifest.Link(name)
199 except ManifestParseError, e:
200 print >>sys.stderr, "fatal: manifest '%s' not available" % name
201 print >>sys.stderr, 'fatal: %s' % str(e)
202 sys.exit(1)
203
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700204 def _Prompt(self, prompt, value):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700205 mp = self.manifest.manifestProject
206
207 sys.stdout.write('%-10s [%s]: ' % (prompt, value))
208 a = sys.stdin.readline().strip()
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700209 if a == '':
210 return value
211 return a
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700212
213 def _ConfigureUser(self):
214 mp = self.manifest.manifestProject
215
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700216 while True:
217 print ''
218 name = self._Prompt('Your Name', mp.UserName)
219 email = self._Prompt('Your Email', mp.UserEmail)
220
221 print ''
222 print 'Your identity is: %s <%s>' % (name, email)
Nico Sallembien6d7508b2010-04-01 11:03:53 -0700223 sys.stdout.write('is this correct [y/n]? ')
224 a = sys.stdin.readline().strip()
225 if a in ('yes', 'y', 't', 'true'):
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700226 break
227
228 if name != mp.UserName:
229 mp.config.SetString('user.name', name)
230 if email != mp.UserEmail:
231 mp.config.SetString('user.email', email)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700232
233 def _HasColorSet(self, gc):
234 for n in ['ui', 'diff', 'status']:
235 if gc.Has('color.%s' % n):
236 return True
237 return False
238
239 def _ConfigureColor(self):
240 gc = self.manifest.globalConfig
241 if self._HasColorSet(gc):
242 return
243
244 class _Test(Coloring):
245 def __init__(self):
246 Coloring.__init__(self, gc, 'test color display')
247 self._on = True
248 out = _Test()
249
250 print ''
251 print "Testing colorized output (for 'repo diff', 'repo status'):"
252
253 for c in ['black','red','green','yellow','blue','magenta','cyan']:
254 out.write(' ')
255 out.printer(fg=c)(' %-6s ', c)
256 out.write(' ')
257 out.printer(fg='white', bg='black')(' %s ' % 'white')
258 out.nl()
259
260 for c in ['bold','dim','ul','reverse']:
261 out.write(' ')
262 out.printer(fg='black', attr=c)(' %-6s ', c)
263 out.nl()
264
265 sys.stdout.write('Enable color display in this user account (y/n)? ')
266 a = sys.stdin.readline().strip().lower()
267 if a in ('y', 'yes', 't', 'true', 'on'):
268 gc.SetString('color.ui', 'auto')
269
270 def Execute(self, opt, args):
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700271 git_require(MIN_GIT_VERSION, fail=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700272 self._SyncManifest(opt)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700273
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700274 if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700275 self._ConfigureUser()
276 self._ConfigureColor()
277
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700278 if self.manifest.IsMirror:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800279 type = 'mirror '
280 else:
281 type = ''
282
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700283 print ''
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800284 print 'repo %sinitialized in %s' % (type, self.manifest.topdir)