blob: 53c3a01030be959ca9afbe115e8bcd9416460960 [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
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
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070041Switching Manifest Branches
42---------------------------
43
44To switch to another manifest branch, `repo init -b otherbranch`
45may be used in an existing client. However, as this only updates the
46manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary
47to update the working directory files.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070048"""
49
50 def _Options(self, p):
51 # Logging
52 g = p.add_option_group('Logging options')
53 g.add_option('-q', '--quiet',
54 dest="quiet", action="store_true", default=False,
55 help="be quiet")
56
57 # Manifest
58 g = p.add_option_group('Manifest options')
59 g.add_option('-u', '--manifest-url',
60 dest='manifest_url',
61 help='manifest repository location', metavar='URL')
62 g.add_option('-b', '--manifest-branch',
63 dest='manifest_branch',
64 help='manifest branch or revision', metavar='REVISION')
Shawn O. Pearce5f947bb2009-07-03 17:24:17 -070065 g.add_option('-o', '--origin',
66 dest='manifest_origin',
67 help="use REMOTE instead of 'origin' to track upstream",
68 metavar='REMOTE')
Shawn O. Pearce446c4e52009-05-19 18:14:04 -070069 if isinstance(self.manifest, XmlManifest) \
70 or not self.manifest.manifestProject.Exists:
71 g.add_option('-m', '--manifest-name',
72 dest='manifest_name', default='default.xml',
73 help='initial manifest file', metavar='NAME.xml')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -080074 g.add_option('--mirror',
75 dest='mirror', action='store_true',
76 help='mirror the forrest')
77
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070078
79 # Tool
Shawn O. Pearcefd89b672009-04-18 11:28:57 -070080 g = p.add_option_group('repo Version options')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070081 g.add_option('--repo-url',
82 dest='repo_url',
83 help='repo repository location', metavar='URL')
84 g.add_option('--repo-branch',
85 dest='repo_branch',
86 help='repo branch or revision', metavar='REVISION')
87 g.add_option('--no-repo-verify',
88 dest='no_repo_verify', action='store_true',
89 help='do not verify repo source code')
90
Shawn O. Pearce5f947bb2009-07-03 17:24:17 -070091 def _ApplyOptions(self, opt, is_new):
92 m = self.manifest.manifestProject
93
94 if is_new:
95 if opt.manifest_origin:
96 m.remote.name = opt.manifest_origin
97
98 if opt.manifest_branch:
99 m.revisionExpr = opt.manifest_branch
100 else:
101 m.revisionExpr = 'refs/heads/master'
102 else:
103 if opt.manifest_origin:
104 print >>sys.stderr, 'fatal: cannot change origin name'
105 sys.exit(1)
106
107 if opt.manifest_branch:
108 m.revisionExpr = opt.manifest_branch
109 else:
110 m.PreSync()
111
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700112 def _SyncManifest(self, opt):
113 m = self.manifest.manifestProject
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700114 is_new = not m.Exists
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700115
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700116 if is_new:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700117 if not opt.manifest_url:
118 print >>sys.stderr, 'fatal: manifest url (-u) is required.'
119 sys.exit(1)
120
121 if not opt.quiet:
122 print >>sys.stderr, 'Getting manifest ...'
123 print >>sys.stderr, ' from %s' % opt.manifest_url
124 m._InitGitDir()
125
Shawn O. Pearce5f947bb2009-07-03 17:24:17 -0700126 self._ApplyOptions(opt, is_new)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700127 if opt.manifest_url:
128 r = m.GetRemote(m.remote.name)
129 r.url = opt.manifest_url
130 r.ResetFetch()
131 r.Save()
132
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800133 if opt.mirror:
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700134 if is_new:
135 m.config.SetString('repo.mirror', 'true')
Shawn O. Pearce7354d882009-07-03 20:06:13 -0700136 m.config.ClearCache()
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700137 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
144 sys.exit(1)
145
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700146 syncbuf = SyncBuffer(m.config)
147 m.Sync_LocalHalf(syncbuf)
148 syncbuf.Finish()
149
Shawn O. Pearce75b87c82009-07-03 16:24:57 -0700150 if not self.manifest.InitBranch():
151 print >>sys.stderr, 'fatal: cannot create branch in manifest'
152 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700153
154 def _LinkManifest(self, name):
155 if not name:
156 print >>sys.stderr, 'fatal: manifest name (-m) is required.'
157 sys.exit(1)
158
159 try:
160 self.manifest.Link(name)
161 except ManifestParseError, e:
162 print >>sys.stderr, "fatal: manifest '%s' not available" % name
163 print >>sys.stderr, 'fatal: %s' % str(e)
164 sys.exit(1)
165
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700166 def _Prompt(self, prompt, value):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700167 mp = self.manifest.manifestProject
168
169 sys.stdout.write('%-10s [%s]: ' % (prompt, value))
170 a = sys.stdin.readline().strip()
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700171 if a == '':
172 return value
173 return a
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700174
175 def _ConfigureUser(self):
176 mp = self.manifest.manifestProject
177
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700178 while True:
179 print ''
180 name = self._Prompt('Your Name', mp.UserName)
181 email = self._Prompt('Your Email', mp.UserEmail)
182
183 print ''
184 print 'Your identity is: %s <%s>' % (name, email)
185 sys.stdout.write('is this correct [yes/no]? ')
186 if 'yes' == sys.stdin.readline().strip():
187 break
188
189 if name != mp.UserName:
190 mp.config.SetString('user.name', name)
191 if email != mp.UserEmail:
192 mp.config.SetString('user.email', email)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700193
194 def _HasColorSet(self, gc):
195 for n in ['ui', 'diff', 'status']:
196 if gc.Has('color.%s' % n):
197 return True
198 return False
199
200 def _ConfigureColor(self):
201 gc = self.manifest.globalConfig
202 if self._HasColorSet(gc):
203 return
204
205 class _Test(Coloring):
206 def __init__(self):
207 Coloring.__init__(self, gc, 'test color display')
208 self._on = True
209 out = _Test()
210
211 print ''
212 print "Testing colorized output (for 'repo diff', 'repo status'):"
213
214 for c in ['black','red','green','yellow','blue','magenta','cyan']:
215 out.write(' ')
216 out.printer(fg=c)(' %-6s ', c)
217 out.write(' ')
218 out.printer(fg='white', bg='black')(' %s ' % 'white')
219 out.nl()
220
221 for c in ['bold','dim','ul','reverse']:
222 out.write(' ')
223 out.printer(fg='black', attr=c)(' %-6s ', c)
224 out.nl()
225
226 sys.stdout.write('Enable color display in this user account (y/n)? ')
227 a = sys.stdin.readline().strip().lower()
228 if a in ('y', 'yes', 't', 'true', 'on'):
229 gc.SetString('color.ui', 'auto')
230
231 def Execute(self, opt, args):
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700232 git_require(MIN_GIT_VERSION, fail=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700233 self._SyncManifest(opt)
Shawn O. Pearce446c4e52009-05-19 18:14:04 -0700234 if isinstance(self.manifest, XmlManifest):
235 self._LinkManifest(opt.manifest_name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700236
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700237 if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700238 self._ConfigureUser()
239 self._ConfigureColor()
240
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700241 if self.manifest.IsMirror:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800242 type = 'mirror '
243 else:
244 type = ''
245
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700246 print ''
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800247 print 'repo %sinitialized in %s' % (type, self.manifest.topdir)