blob: 1cba366549803f67de52ef0af2c32637a553700b [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
Doug Anderson2630dd92011-04-07 13:36:30 -070017import shutil
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070018import sys
19
20from color import Coloring
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080021from command import InteractiveCommand, MirrorSafeCommand
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070022from error import ManifestParseError
Shawn O. Pearce350cde42009-04-16 11:21:18 -070023from project import SyncBuffer
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -070024from git_config import GitConfig
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -070025from git_command import git_require, MIN_GIT_VERSION
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070026
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080027class Init(InteractiveCommand, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070028 common = True
29 helpSummary = "Initialize repo in the current directory"
30 helpUsage = """
31%prog [options]
32"""
33 helpDescription = """
34The '%prog' command is run once to install and initialize repo.
35The latest repo source code and manifest collection is downloaded
36from the server and is installed in the .repo/ directory in the
37current working directory.
38
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070039The optional -b argument can be used to select the manifest branch
40to checkout and use. If no branch is specified, master is assumed.
41
42The optional -m argument can be used to specify an alternate manifest
43to be used. If no manifest is specified, the manifest default.xml
44will be used.
45
Shawn O. Pearce88443382010-10-08 10:02:09 +020046The --reference option can be used to point to a directory that
47has the content of a --mirror sync. This will make the working
48directory use as much data as possible from the local reference
49directory when fetching from the server. This will make the sync
50go a lot faster by reducing data traffic on the network.
51
52
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070053Switching Manifest Branches
54---------------------------
55
56To switch to another manifest branch, `repo init -b otherbranch`
57may be used in an existing client. However, as this only updates the
58manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary
59to update the working directory files.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070060"""
61
62 def _Options(self, p):
63 # Logging
64 g = p.add_option_group('Logging options')
65 g.add_option('-q', '--quiet',
66 dest="quiet", action="store_true", default=False,
67 help="be quiet")
68
69 # Manifest
70 g = p.add_option_group('Manifest options')
71 g.add_option('-u', '--manifest-url',
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -080072 dest='manifest_url',
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070073 help='manifest repository location', metavar='URL')
74 g.add_option('-b', '--manifest-branch',
75 dest='manifest_branch',
76 help='manifest branch or revision', metavar='REVISION')
77 g.add_option('-m', '--manifest-name',
78 dest='manifest_name', default='default.xml',
79 help='initial manifest file', metavar='NAME.xml')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -080080 g.add_option('--mirror',
81 dest='mirror', action='store_true',
82 help='mirror the forrest')
Shawn O. Pearce88443382010-10-08 10:02:09 +020083 g.add_option('--reference',
84 dest='reference',
85 help='location of mirror directory', metavar='DIR')
Doug Anderson30d45292011-05-04 15:01:04 -070086 g.add_option('--depth', type='int', default=None,
87 dest='depth',
88 help='create a shallow clone with given depth; see git clone')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070089
90 # Tool
Shawn O. Pearcefd89b672009-04-18 11:28:57 -070091 g = p.add_option_group('repo Version options')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070092 g.add_option('--repo-url',
93 dest='repo_url',
94 help='repo repository location', metavar='URL')
95 g.add_option('--repo-branch',
96 dest='repo_branch',
97 help='repo branch or revision', metavar='REVISION')
98 g.add_option('--no-repo-verify',
99 dest='no_repo_verify', action='store_true',
100 help='do not verify repo source code')
101
Victor Boivie841be342011-04-05 11:31:10 +0200102 # Other
103 g = p.add_option_group('Other options')
104 g.add_option('--config-name',
105 dest='config_name', action="store_true", default=False,
106 help='Always prompt for name/e-mail')
107
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700108 def _SyncManifest(self, opt):
109 m = self.manifest.manifestProject
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700110 is_new = not m.Exists
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700111
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700112 if is_new:
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800113 if not opt.manifest_url:
114 print >>sys.stderr, 'fatal: manifest url (-u) is required.'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700115 sys.exit(1)
116
117 if not opt.quiet:
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700118 print >>sys.stderr, 'Get %s' \
119 % GitConfig.ForUser().UrlInsteadOf(opt.manifest_url)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700120 m._InitGitDir()
121
122 if opt.manifest_branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700123 m.revisionExpr = opt.manifest_branch
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700124 else:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700125 m.revisionExpr = 'refs/heads/master'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700126 else:
127 if opt.manifest_branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700128 m.revisionExpr = opt.manifest_branch
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700129 else:
130 m.PreSync()
131
132 if opt.manifest_url:
133 r = m.GetRemote(m.remote.name)
134 r.url = opt.manifest_url
135 r.ResetFetch()
136 r.Save()
137
Shawn O. Pearce88443382010-10-08 10:02:09 +0200138 if opt.reference:
139 m.config.SetString('repo.reference', opt.reference)
140
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800141 if opt.mirror:
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700142 if is_new:
143 m.config.SetString('repo.mirror', 'true')
144 else:
145 print >>sys.stderr, 'fatal: --mirror not supported on existing client'
146 sys.exit(1)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800147
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700148 if not m.Sync_NetworkHalf(is_new=is_new):
Shawn O. Pearce1fc99f42009-03-17 08:06:18 -0700149 r = m.GetRemote(m.remote.name)
150 print >>sys.stderr, 'fatal: cannot obtain manifest %s' % r.url
Doug Anderson2630dd92011-04-07 13:36:30 -0700151
152 # Better delete the manifest git dir if we created it; otherwise next
153 # time (when user fixes problems) we won't go through the "is_new" logic.
154 if is_new:
155 shutil.rmtree(m.gitdir)
Shawn O. Pearce1fc99f42009-03-17 08:06:18 -0700156 sys.exit(1)
157
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700158 syncbuf = SyncBuffer(m.config)
159 m.Sync_LocalHalf(syncbuf)
160 syncbuf.Finish()
161
Shawn O. Pearcedf018832009-03-17 08:15:27 -0700162 if is_new or m.CurrentBranch is None:
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700163 if not m.StartBranch('default'):
164 print >>sys.stderr, 'fatal: cannot create default in manifest'
165 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700166
167 def _LinkManifest(self, name):
168 if not name:
169 print >>sys.stderr, 'fatal: manifest name (-m) is required.'
170 sys.exit(1)
171
172 try:
173 self.manifest.Link(name)
174 except ManifestParseError, e:
175 print >>sys.stderr, "fatal: manifest '%s' not available" % name
176 print >>sys.stderr, 'fatal: %s' % str(e)
177 sys.exit(1)
178
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700179 def _Prompt(self, prompt, value):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700180 mp = self.manifest.manifestProject
181
182 sys.stdout.write('%-10s [%s]: ' % (prompt, value))
183 a = sys.stdin.readline().strip()
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700184 if a == '':
185 return value
186 return a
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700187
Victor Boivie841be342011-04-05 11:31:10 +0200188 def _ShouldConfigureUser(self):
189 gc = self.manifest.globalConfig
190 mp = self.manifest.manifestProject
191
192 # If we don't have local settings, get from global.
193 if not mp.config.Has('user.name') or not mp.config.Has('user.email'):
194 if not gc.Has('user.name') or not gc.Has('user.email'):
195 return True
196
197 mp.config.SetString('user.name', gc.GetString('user.name'))
198 mp.config.SetString('user.email', gc.GetString('user.email'))
199
200 print ''
201 print 'Your identity is: %s <%s>' % (mp.config.GetString('user.name'),
202 mp.config.GetString('user.email'))
203 print 'If you want to change this, please re-run \'repo init\' with --config-name'
204 return False
205
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700206 def _ConfigureUser(self):
207 mp = self.manifest.manifestProject
208
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700209 while True:
210 print ''
211 name = self._Prompt('Your Name', mp.UserName)
212 email = self._Prompt('Your Email', mp.UserEmail)
213
214 print ''
215 print 'Your identity is: %s <%s>' % (name, email)
Mike Frysingere9311272011-08-11 15:46:43 -0400216 sys.stdout.write('is this correct [y/N]? ')
Nico Sallembien6d7508b2010-04-01 11:03:53 -0700217 a = sys.stdin.readline().strip()
218 if a in ('yes', 'y', 't', 'true'):
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700219 break
220
221 if name != mp.UserName:
222 mp.config.SetString('user.name', name)
223 if email != mp.UserEmail:
224 mp.config.SetString('user.email', email)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700225
226 def _HasColorSet(self, gc):
227 for n in ['ui', 'diff', 'status']:
228 if gc.Has('color.%s' % n):
229 return True
230 return False
231
232 def _ConfigureColor(self):
233 gc = self.manifest.globalConfig
234 if self._HasColorSet(gc):
235 return
236
237 class _Test(Coloring):
238 def __init__(self):
239 Coloring.__init__(self, gc, 'test color display')
240 self._on = True
241 out = _Test()
242
243 print ''
244 print "Testing colorized output (for 'repo diff', 'repo status'):"
245
246 for c in ['black','red','green','yellow','blue','magenta','cyan']:
247 out.write(' ')
248 out.printer(fg=c)(' %-6s ', c)
249 out.write(' ')
250 out.printer(fg='white', bg='black')(' %s ' % 'white')
251 out.nl()
252
253 for c in ['bold','dim','ul','reverse']:
254 out.write(' ')
255 out.printer(fg='black', attr=c)(' %-6s ', c)
256 out.nl()
257
Mike Frysingere9311272011-08-11 15:46:43 -0400258 sys.stdout.write('Enable color display in this user account (y/N)? ')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700259 a = sys.stdin.readline().strip().lower()
260 if a in ('y', 'yes', 't', 'true', 'on'):
261 gc.SetString('color.ui', 'auto')
262
Doug Anderson30d45292011-05-04 15:01:04 -0700263 def _ConfigureDepth(self, opt):
264 """Configure the depth we'll sync down.
265
266 Args:
267 opt: Options from optparse. We care about opt.depth.
268 """
269 # Opt.depth will be non-None if user actually passed --depth to repo init.
270 if opt.depth is not None:
271 if opt.depth > 0:
272 # Positive values will set the depth.
273 depth = str(opt.depth)
274 else:
275 # Negative numbers will clear the depth; passing None to SetString
276 # will do that.
277 depth = None
278
279 # We store the depth in the main manifest project.
280 self.manifest.manifestProject.config.SetString('repo.depth', depth)
281
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700282 def Execute(self, opt, args):
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700283 git_require(MIN_GIT_VERSION, fail=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700284 self._SyncManifest(opt)
285 self._LinkManifest(opt.manifest_name)
286
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700287 if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror:
Victor Boivie841be342011-04-05 11:31:10 +0200288 if opt.config_name or self._ShouldConfigureUser():
289 self._ConfigureUser()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700290 self._ConfigureColor()
291
Doug Anderson30d45292011-05-04 15:01:04 -0700292 self._ConfigureDepth(opt)
293
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700294 if self.manifest.IsMirror:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800295 type = 'mirror '
296 else:
297 type = ''
298
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700299 print ''
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800300 print 'repo %sinitialized in %s' % (type, self.manifest.topdir)