blob: c35cc82cb6a7cd878f54b1fd1aefec0b20c64c7a [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. Pearce2ec00b92009-06-12 09:32:50 -070024from git_command import git_require, MIN_GIT_VERSION
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070025
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080026class Init(InteractiveCommand, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070027 common = True
28 helpSummary = "Initialize repo in the current directory"
29 helpUsage = """
30%prog [options]
31"""
32 helpDescription = """
33The '%prog' command is run once to install and initialize repo.
34The latest repo source code and manifest collection is downloaded
35from the server and is installed in the .repo/ directory in the
36current working directory.
37
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070038The optional -b argument can be used to select the manifest branch
39to checkout and use. If no branch is specified, master is assumed.
40
41The optional -m argument can be used to specify an alternate manifest
42to be used. If no manifest is specified, the manifest default.xml
43will be used.
44
Shawn O. Pearce88443382010-10-08 10:02:09 +020045The --reference option can be used to point to a directory that
46has the content of a --mirror sync. This will make the working
47directory use as much data as possible from the local reference
48directory when fetching from the server. This will make the sync
49go a lot faster by reducing data traffic on the network.
50
51
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070052Switching Manifest Branches
53---------------------------
54
55To switch to another manifest branch, `repo init -b otherbranch`
56may be used in an existing client. However, as this only updates the
57manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary
58to update the working directory files.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070059"""
60
61 def _Options(self, p):
62 # Logging
63 g = p.add_option_group('Logging options')
64 g.add_option('-q', '--quiet',
65 dest="quiet", action="store_true", default=False,
66 help="be quiet")
67
68 # Manifest
69 g = p.add_option_group('Manifest options')
70 g.add_option('-u', '--manifest-url',
71 dest='manifest_url',
72 help='manifest repository location', metavar='URL')
73 g.add_option('-b', '--manifest-branch',
74 dest='manifest_branch',
75 help='manifest branch or revision', metavar='REVISION')
76 g.add_option('-m', '--manifest-name',
77 dest='manifest_name', default='default.xml',
78 help='initial manifest file', metavar='NAME.xml')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -080079 g.add_option('--mirror',
80 dest='mirror', action='store_true',
81 help='mirror the forrest')
Shawn O. Pearce88443382010-10-08 10:02:09 +020082 g.add_option('--reference',
83 dest='reference',
84 help='location of mirror directory', metavar='DIR')
Doug Anderson30d45292011-05-04 15:01:04 -070085 g.add_option('--depth', type='int', default=None,
86 dest='depth',
87 help='create a shallow clone with given depth; see git clone')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070088
89 # Tool
Shawn O. Pearcefd89b672009-04-18 11:28:57 -070090 g = p.add_option_group('repo Version options')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070091 g.add_option('--repo-url',
92 dest='repo_url',
93 help='repo repository location', metavar='URL')
94 g.add_option('--repo-branch',
95 dest='repo_branch',
96 help='repo branch or revision', metavar='REVISION')
97 g.add_option('--no-repo-verify',
98 dest='no_repo_verify', action='store_true',
99 help='do not verify repo source code')
100
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700101 def _SyncManifest(self, opt):
102 m = self.manifest.manifestProject
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700103 is_new = not m.Exists
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700104
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700105 if is_new:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700106 if not opt.manifest_url:
107 print >>sys.stderr, 'fatal: manifest url (-u) is required.'
108 sys.exit(1)
109
110 if not opt.quiet:
111 print >>sys.stderr, 'Getting manifest ...'
112 print >>sys.stderr, ' from %s' % opt.manifest_url
113 m._InitGitDir()
114
115 if opt.manifest_branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700116 m.revisionExpr = opt.manifest_branch
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700117 else:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700118 m.revisionExpr = 'refs/heads/master'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700119 else:
120 if opt.manifest_branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700121 m.revisionExpr = opt.manifest_branch
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700122 else:
123 m.PreSync()
124
125 if opt.manifest_url:
126 r = m.GetRemote(m.remote.name)
127 r.url = opt.manifest_url
128 r.ResetFetch()
129 r.Save()
130
Shawn O. Pearce88443382010-10-08 10:02:09 +0200131 if opt.reference:
132 m.config.SetString('repo.reference', opt.reference)
133
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800134 if opt.mirror:
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700135 if is_new:
136 m.config.SetString('repo.mirror', 'true')
137 else:
138 print >>sys.stderr, 'fatal: --mirror not supported on existing client'
139 sys.exit(1)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800140
Shawn O. Pearce1fc99f42009-03-17 08:06:18 -0700141 if not m.Sync_NetworkHalf():
142 r = m.GetRemote(m.remote.name)
143 print >>sys.stderr, 'fatal: cannot obtain manifest %s' % r.url
Doug Anderson2630dd92011-04-07 13:36:30 -0700144
145 # Better delete the manifest git dir if we created it; otherwise next
146 # time (when user fixes problems) we won't go through the "is_new" logic.
147 if is_new:
148 shutil.rmtree(m.gitdir)
Shawn O. Pearce1fc99f42009-03-17 08:06:18 -0700149 sys.exit(1)
150
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700151 syncbuf = SyncBuffer(m.config)
152 m.Sync_LocalHalf(syncbuf)
153 syncbuf.Finish()
154
Shawn O. Pearcedf018832009-03-17 08:15:27 -0700155 if is_new or m.CurrentBranch is None:
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700156 if not m.StartBranch('default'):
157 print >>sys.stderr, 'fatal: cannot create default in manifest'
158 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700159
160 def _LinkManifest(self, name):
161 if not name:
162 print >>sys.stderr, 'fatal: manifest name (-m) is required.'
163 sys.exit(1)
164
165 try:
166 self.manifest.Link(name)
167 except ManifestParseError, e:
168 print >>sys.stderr, "fatal: manifest '%s' not available" % name
169 print >>sys.stderr, 'fatal: %s' % str(e)
170 sys.exit(1)
171
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700172 def _Prompt(self, prompt, value):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700173 mp = self.manifest.manifestProject
174
175 sys.stdout.write('%-10s [%s]: ' % (prompt, value))
176 a = sys.stdin.readline().strip()
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700177 if a == '':
178 return value
179 return a
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700180
181 def _ConfigureUser(self):
182 mp = self.manifest.manifestProject
183
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700184 while True:
185 print ''
186 name = self._Prompt('Your Name', mp.UserName)
187 email = self._Prompt('Your Email', mp.UserEmail)
188
189 print ''
190 print 'Your identity is: %s <%s>' % (name, email)
Nico Sallembien6d7508b2010-04-01 11:03:53 -0700191 sys.stdout.write('is this correct [y/n]? ')
192 a = sys.stdin.readline().strip()
193 if a in ('yes', 'y', 't', 'true'):
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700194 break
195
196 if name != mp.UserName:
197 mp.config.SetString('user.name', name)
198 if email != mp.UserEmail:
199 mp.config.SetString('user.email', email)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700200
201 def _HasColorSet(self, gc):
202 for n in ['ui', 'diff', 'status']:
203 if gc.Has('color.%s' % n):
204 return True
205 return False
206
207 def _ConfigureColor(self):
208 gc = self.manifest.globalConfig
209 if self._HasColorSet(gc):
210 return
211
212 class _Test(Coloring):
213 def __init__(self):
214 Coloring.__init__(self, gc, 'test color display')
215 self._on = True
216 out = _Test()
217
218 print ''
219 print "Testing colorized output (for 'repo diff', 'repo status'):"
220
221 for c in ['black','red','green','yellow','blue','magenta','cyan']:
222 out.write(' ')
223 out.printer(fg=c)(' %-6s ', c)
224 out.write(' ')
225 out.printer(fg='white', bg='black')(' %s ' % 'white')
226 out.nl()
227
228 for c in ['bold','dim','ul','reverse']:
229 out.write(' ')
230 out.printer(fg='black', attr=c)(' %-6s ', c)
231 out.nl()
232
233 sys.stdout.write('Enable color display in this user account (y/n)? ')
234 a = sys.stdin.readline().strip().lower()
235 if a in ('y', 'yes', 't', 'true', 'on'):
236 gc.SetString('color.ui', 'auto')
237
Doug Anderson30d45292011-05-04 15:01:04 -0700238 def _ConfigureDepth(self, opt):
239 """Configure the depth we'll sync down.
240
241 Args:
242 opt: Options from optparse. We care about opt.depth.
243 """
244 # Opt.depth will be non-None if user actually passed --depth to repo init.
245 if opt.depth is not None:
246 if opt.depth > 0:
247 # Positive values will set the depth.
248 depth = str(opt.depth)
249 else:
250 # Negative numbers will clear the depth; passing None to SetString
251 # will do that.
252 depth = None
253
254 # We store the depth in the main manifest project.
255 self.manifest.manifestProject.config.SetString('repo.depth', depth)
256
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700257 def Execute(self, opt, args):
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700258 git_require(MIN_GIT_VERSION, fail=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700259 self._SyncManifest(opt)
260 self._LinkManifest(opt.manifest_name)
261
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700262 if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700263 self._ConfigureUser()
264 self._ConfigureColor()
265
Doug Anderson30d45292011-05-04 15:01:04 -0700266 self._ConfigureDepth(opt)
267
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700268 if self.manifest.IsMirror:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800269 type = 'mirror '
270 else:
271 type = ''
272
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700273 print ''
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800274 print 'repo %sinitialized in %s' % (type, self.manifest.topdir)