blob: 17edfa05e02d0564128ec714ee6657956fa86e6a [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
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070024
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080025class Init(InteractiveCommand, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070026 common = True
27 helpSummary = "Initialize repo in the current directory"
28 helpUsage = """
29%prog [options]
30"""
31 helpDescription = """
32The '%prog' command is run once to install and initialize repo.
33The latest repo source code and manifest collection is downloaded
34from the server and is installed in the .repo/ directory in the
35current working directory.
36
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070037The optional -b argument can be used to select the manifest branch
38to checkout and use. If no branch is specified, master is assumed.
39
40The optional -m argument can be used to specify an alternate manifest
41to be used. If no manifest is specified, the manifest default.xml
42will be used.
43
Shawn O. Pearce88443382010-10-08 10:02:09 +020044The --reference option can be used to point to a directory that
45has the content of a --mirror sync. This will make the working
46directory use as much data as possible from the local reference
47directory when fetching from the server. This will make the sync
48go a lot faster by reducing data traffic on the network.
49
50
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070051Switching Manifest Branches
52---------------------------
53
54To switch to another manifest branch, `repo init -b otherbranch`
55may be used in an existing client. However, as this only updates the
56manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary
57to update the working directory files.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070058"""
59
60 def _Options(self, p):
61 # Logging
62 g = p.add_option_group('Logging options')
63 g.add_option('-q', '--quiet',
64 dest="quiet", action="store_true", default=False,
65 help="be quiet")
66
67 # Manifest
68 g = p.add_option_group('Manifest options')
69 g.add_option('-u', '--manifest-url',
70 dest='manifest_url',
71 help='manifest repository location', metavar='URL')
72 g.add_option('-b', '--manifest-branch',
73 dest='manifest_branch',
74 help='manifest branch or revision', metavar='REVISION')
75 g.add_option('-m', '--manifest-name',
76 dest='manifest_name', default='default.xml',
77 help='initial manifest file', metavar='NAME.xml')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -080078 g.add_option('--mirror',
79 dest='mirror', action='store_true',
80 help='mirror the forrest')
Shawn O. Pearce88443382010-10-08 10:02:09 +020081 g.add_option('--reference',
82 dest='reference',
83 help='location of mirror directory', metavar='DIR')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070084
85 # Tool
Shawn O. Pearcefd89b672009-04-18 11:28:57 -070086 g = p.add_option_group('repo Version options')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070087 g.add_option('--repo-url',
88 dest='repo_url',
89 help='repo repository location', metavar='URL')
90 g.add_option('--repo-branch',
91 dest='repo_branch',
92 help='repo branch or revision', metavar='REVISION')
93 g.add_option('--no-repo-verify',
94 dest='no_repo_verify', action='store_true',
95 help='do not verify repo source code')
96
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070097 def _SyncManifest(self, opt):
98 m = self.manifest.manifestProject
Shawn O. Pearce5470df62009-03-09 18:51:58 -070099 is_new = not m.Exists
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700100
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700101 if is_new:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700102 if not opt.manifest_url:
103 print >>sys.stderr, 'fatal: manifest url (-u) is required.'
104 sys.exit(1)
105
106 if not opt.quiet:
107 print >>sys.stderr, 'Getting manifest ...'
108 print >>sys.stderr, ' from %s' % opt.manifest_url
109 m._InitGitDir()
110
111 if opt.manifest_branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700112 m.revisionExpr = opt.manifest_branch
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700113 else:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700114 m.revisionExpr = 'refs/heads/master'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700115 else:
116 if opt.manifest_branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700117 m.revisionExpr = opt.manifest_branch
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700118 else:
119 m.PreSync()
120
121 if opt.manifest_url:
122 r = m.GetRemote(m.remote.name)
123 r.url = opt.manifest_url
124 r.ResetFetch()
125 r.Save()
126
Shawn O. Pearce88443382010-10-08 10:02:09 +0200127 if opt.reference:
128 m.config.SetString('repo.reference', opt.reference)
129
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800130 if opt.mirror:
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700131 if is_new:
132 m.config.SetString('repo.mirror', 'true')
133 else:
134 print >>sys.stderr, 'fatal: --mirror not supported on existing client'
135 sys.exit(1)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800136
Shawn O. Pearce1fc99f42009-03-17 08:06:18 -0700137 if not m.Sync_NetworkHalf():
138 r = m.GetRemote(m.remote.name)
139 print >>sys.stderr, 'fatal: cannot obtain manifest %s' % r.url
140 sys.exit(1)
141
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700142 syncbuf = SyncBuffer(m.config)
143 m.Sync_LocalHalf(syncbuf)
144 syncbuf.Finish()
145
Shawn O. Pearcedf018832009-03-17 08:15:27 -0700146 if is_new or m.CurrentBranch is None:
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700147 if not m.StartBranch('default'):
148 print >>sys.stderr, 'fatal: cannot create default in manifest'
149 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700150
151 def _LinkManifest(self, name):
152 if not name:
153 print >>sys.stderr, 'fatal: manifest name (-m) is required.'
154 sys.exit(1)
155
156 try:
157 self.manifest.Link(name)
158 except ManifestParseError, e:
159 print >>sys.stderr, "fatal: manifest '%s' not available" % name
160 print >>sys.stderr, 'fatal: %s' % str(e)
161 sys.exit(1)
162
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700163 def _Prompt(self, prompt, value):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700164 mp = self.manifest.manifestProject
165
166 sys.stdout.write('%-10s [%s]: ' % (prompt, value))
167 a = sys.stdin.readline().strip()
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700168 if a == '':
169 return value
170 return a
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700171
172 def _ConfigureUser(self):
173 mp = self.manifest.manifestProject
174
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700175 while True:
176 print ''
177 name = self._Prompt('Your Name', mp.UserName)
178 email = self._Prompt('Your Email', mp.UserEmail)
179
180 print ''
181 print 'Your identity is: %s <%s>' % (name, email)
Nico Sallembien6d7508b2010-04-01 11:03:53 -0700182 sys.stdout.write('is this correct [y/n]? ')
183 a = sys.stdin.readline().strip()
184 if a in ('yes', 'y', 't', 'true'):
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700185 break
186
187 if name != mp.UserName:
188 mp.config.SetString('user.name', name)
189 if email != mp.UserEmail:
190 mp.config.SetString('user.email', email)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700191
192 def _HasColorSet(self, gc):
193 for n in ['ui', 'diff', 'status']:
194 if gc.Has('color.%s' % n):
195 return True
196 return False
197
198 def _ConfigureColor(self):
199 gc = self.manifest.globalConfig
200 if self._HasColorSet(gc):
201 return
202
203 class _Test(Coloring):
204 def __init__(self):
205 Coloring.__init__(self, gc, 'test color display')
206 self._on = True
207 out = _Test()
208
209 print ''
210 print "Testing colorized output (for 'repo diff', 'repo status'):"
211
212 for c in ['black','red','green','yellow','blue','magenta','cyan']:
213 out.write(' ')
214 out.printer(fg=c)(' %-6s ', c)
215 out.write(' ')
216 out.printer(fg='white', bg='black')(' %s ' % 'white')
217 out.nl()
218
219 for c in ['bold','dim','ul','reverse']:
220 out.write(' ')
221 out.printer(fg='black', attr=c)(' %-6s ', c)
222 out.nl()
223
224 sys.stdout.write('Enable color display in this user account (y/n)? ')
225 a = sys.stdin.readline().strip().lower()
226 if a in ('y', 'yes', 't', 'true', 'on'):
227 gc.SetString('color.ui', 'auto')
228
229 def Execute(self, opt, args):
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700230 git_require(MIN_GIT_VERSION, fail=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700231 self._SyncManifest(opt)
232 self._LinkManifest(opt.manifest_name)
233
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700234 if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700235 self._ConfigureUser()
236 self._ConfigureColor()
237
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700238 if self.manifest.IsMirror:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800239 type = 'mirror '
240 else:
241 type = ''
242
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700243 print ''
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800244 print 'repo %sinitialized in %s' % (type, self.manifest.topdir)