blob: b5207fbf0a8a3ca5df6369f08a337d371c4e561f [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. Pearce446c4e52009-05-19 18:14:04 -070024from manifest_xml import XmlManifest
Shawn O. Pearce87bda122009-07-03 16:37:30 -070025from subcmds.sync import _ReloadManifest
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
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070042Switching Manifest Branches
43---------------------------
44
45To switch to another manifest branch, `repo init -b otherbranch`
46may be used in an existing client. However, as this only updates the
47manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary
48to update the working directory files.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070049"""
50
51 def _Options(self, p):
52 # Logging
53 g = p.add_option_group('Logging options')
54 g.add_option('-q', '--quiet',
55 dest="quiet", action="store_true", default=False,
56 help="be quiet")
57
58 # Manifest
59 g = p.add_option_group('Manifest options')
60 g.add_option('-u', '--manifest-url',
61 dest='manifest_url',
62 help='manifest repository location', metavar='URL')
63 g.add_option('-b', '--manifest-branch',
64 dest='manifest_branch',
65 help='manifest branch or revision', metavar='REVISION')
Shawn O. Pearce5f947bb2009-07-03 17:24:17 -070066 g.add_option('-o', '--origin',
67 dest='manifest_origin',
68 help="use REMOTE instead of 'origin' to track upstream",
69 metavar='REMOTE')
Shawn O. Pearce446c4e52009-05-19 18:14:04 -070070 if isinstance(self.manifest, XmlManifest) \
71 or not self.manifest.manifestProject.Exists:
72 g.add_option('-m', '--manifest-name',
73 dest='manifest_name', default='default.xml',
74 help='initial manifest file', metavar='NAME.xml')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -080075 g.add_option('--mirror',
76 dest='mirror', action='store_true',
77 help='mirror the forrest')
78
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070079
80 # Tool
Shawn O. Pearcefd89b672009-04-18 11:28:57 -070081 g = p.add_option_group('repo Version options')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070082 g.add_option('--repo-url',
83 dest='repo_url',
84 help='repo repository location', metavar='URL')
85 g.add_option('--repo-branch',
86 dest='repo_branch',
87 help='repo branch or revision', metavar='REVISION')
88 g.add_option('--no-repo-verify',
89 dest='no_repo_verify', action='store_true',
90 help='do not verify repo source code')
91
Shawn O. Pearce5f947bb2009-07-03 17:24:17 -070092 def _ApplyOptions(self, opt, is_new):
93 m = self.manifest.manifestProject
94
95 if is_new:
96 if opt.manifest_origin:
97 m.remote.name = opt.manifest_origin
98
99 if opt.manifest_branch:
100 m.revisionExpr = opt.manifest_branch
101 else:
102 m.revisionExpr = 'refs/heads/master'
103 else:
104 if opt.manifest_origin:
105 print >>sys.stderr, 'fatal: cannot change origin name'
106 sys.exit(1)
107
108 if opt.manifest_branch:
109 m.revisionExpr = opt.manifest_branch
110 else:
111 m.PreSync()
112
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700113 def _SyncManifest(self, opt):
114 m = self.manifest.manifestProject
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700115 is_new = not m.Exists
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700116
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700117 if is_new:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700118 if not opt.manifest_url:
119 print >>sys.stderr, 'fatal: manifest url (-u) is required.'
120 sys.exit(1)
121
122 if not opt.quiet:
123 print >>sys.stderr, 'Getting manifest ...'
124 print >>sys.stderr, ' from %s' % opt.manifest_url
125 m._InitGitDir()
126
Shawn O. Pearce5f947bb2009-07-03 17:24:17 -0700127 self._ApplyOptions(opt, is_new)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700128 if opt.manifest_url:
129 r = m.GetRemote(m.remote.name)
130 r.url = opt.manifest_url
131 r.ResetFetch()
132 r.Save()
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')
Shawn O. Pearce7354d882009-07-03 20:06:13 -0700137 m.config.ClearCache()
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700138 else:
139 print >>sys.stderr, 'fatal: --mirror not supported on existing client'
140 sys.exit(1)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800141
Shawn O. Pearce1fc99f42009-03-17 08:06:18 -0700142 if not m.Sync_NetworkHalf():
143 r = m.GetRemote(m.remote.name)
144 print >>sys.stderr, 'fatal: cannot obtain manifest %s' % r.url
145 sys.exit(1)
146
Shawn O. Pearce87bda122009-07-03 16:37:30 -0700147 if not is_new:
148 # Force the manifest to load if it exists, the old graph
149 # may be needed inside of _ReloadManifest().
150 #
151 self.manifest.projects
152
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700153 syncbuf = SyncBuffer(m.config)
154 m.Sync_LocalHalf(syncbuf)
155 syncbuf.Finish()
Shawn O. Pearce87bda122009-07-03 16:37:30 -0700156 _ReloadManifest(self)
157 self._ApplyOptions(opt, is_new)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700158
Shawn O. Pearce75b87c82009-07-03 16:24:57 -0700159 if not self.manifest.InitBranch():
160 print >>sys.stderr, 'fatal: cannot create branch in manifest'
161 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700162
163 def _LinkManifest(self, name):
164 if not name:
165 print >>sys.stderr, 'fatal: manifest name (-m) is required.'
166 sys.exit(1)
167
168 try:
169 self.manifest.Link(name)
170 except ManifestParseError, e:
171 print >>sys.stderr, "fatal: manifest '%s' not available" % name
172 print >>sys.stderr, 'fatal: %s' % str(e)
173 sys.exit(1)
174
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700175 def _Prompt(self, prompt, value):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700176 mp = self.manifest.manifestProject
177
178 sys.stdout.write('%-10s [%s]: ' % (prompt, value))
179 a = sys.stdin.readline().strip()
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700180 if a == '':
181 return value
182 return a
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700183
184 def _ConfigureUser(self):
185 mp = self.manifest.manifestProject
186
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700187 while True:
188 print ''
189 name = self._Prompt('Your Name', mp.UserName)
190 email = self._Prompt('Your Email', mp.UserEmail)
191
192 print ''
193 print 'Your identity is: %s <%s>' % (name, email)
194 sys.stdout.write('is this correct [yes/no]? ')
195 if 'yes' == sys.stdin.readline().strip():
196 break
197
198 if name != mp.UserName:
199 mp.config.SetString('user.name', name)
200 if email != mp.UserEmail:
201 mp.config.SetString('user.email', email)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700202
203 def _HasColorSet(self, gc):
204 for n in ['ui', 'diff', 'status']:
205 if gc.Has('color.%s' % n):
206 return True
207 return False
208
209 def _ConfigureColor(self):
210 gc = self.manifest.globalConfig
211 if self._HasColorSet(gc):
212 return
213
214 class _Test(Coloring):
215 def __init__(self):
216 Coloring.__init__(self, gc, 'test color display')
217 self._on = True
218 out = _Test()
219
220 print ''
221 print "Testing colorized output (for 'repo diff', 'repo status'):"
222
223 for c in ['black','red','green','yellow','blue','magenta','cyan']:
224 out.write(' ')
225 out.printer(fg=c)(' %-6s ', c)
226 out.write(' ')
227 out.printer(fg='white', bg='black')(' %s ' % 'white')
228 out.nl()
229
230 for c in ['bold','dim','ul','reverse']:
231 out.write(' ')
232 out.printer(fg='black', attr=c)(' %-6s ', c)
233 out.nl()
234
235 sys.stdout.write('Enable color display in this user account (y/n)? ')
236 a = sys.stdin.readline().strip().lower()
237 if a in ('y', 'yes', 't', 'true', 'on'):
238 gc.SetString('color.ui', 'auto')
239
240 def Execute(self, opt, args):
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700241 git_require(MIN_GIT_VERSION, fail=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700242 self._SyncManifest(opt)
Shawn O. Pearce446c4e52009-05-19 18:14:04 -0700243 if isinstance(self.manifest, XmlManifest):
244 self._LinkManifest(opt.manifest_name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700245
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700246 if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700247 self._ConfigureUser()
248 self._ConfigureColor()
249
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700250 if self.manifest.IsMirror:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800251 type = 'mirror '
252 else:
253 type = ''
254
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700255 print ''
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800256 print 'repo %sinitialized in %s' % (type, self.manifest.topdir)