blob: cdbbfdf7e73a06a5e16d03c41868c0ab9696cdc7 [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. Pearce0125ae22009-07-03 18:05:23 -070024from manifest_submodule import SubmoduleManifest
Shawn O. Pearce446c4e52009-05-19 18:14:04 -070025from manifest_xml import XmlManifest
Shawn O. Pearce87bda122009-07-03 16:37:30 -070026from subcmds.sync import _ReloadManifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070027
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080028class Init(InteractiveCommand, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070029 common = True
30 helpSummary = "Initialize repo in the current directory"
31 helpUsage = """
32%prog [options]
33"""
34 helpDescription = """
35The '%prog' command is run once to install and initialize repo.
36The latest repo source code and manifest collection is downloaded
37from the server and is installed in the .repo/ directory in the
38current working directory.
39
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070040The optional -b argument can be used to select the manifest branch
41to checkout and use. If no branch is specified, master is assumed.
42
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070043Switching Manifest Branches
44---------------------------
45
46To switch to another manifest branch, `repo init -b otherbranch`
47may be used in an existing client. However, as this only updates the
48manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary
49to update the working directory files.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070050"""
51
52 def _Options(self, p):
53 # Logging
54 g = p.add_option_group('Logging options')
55 g.add_option('-q', '--quiet',
56 dest="quiet", action="store_true", default=False,
57 help="be quiet")
58
59 # Manifest
60 g = p.add_option_group('Manifest options')
61 g.add_option('-u', '--manifest-url',
62 dest='manifest_url',
63 help='manifest repository location', metavar='URL')
64 g.add_option('-b', '--manifest-branch',
65 dest='manifest_branch',
66 help='manifest branch or revision', metavar='REVISION')
Shawn O. Pearce5f947bb2009-07-03 17:24:17 -070067 g.add_option('-o', '--origin',
68 dest='manifest_origin',
69 help="use REMOTE instead of 'origin' to track upstream",
70 metavar='REMOTE')
Shawn O. Pearce446c4e52009-05-19 18:14:04 -070071 if isinstance(self.manifest, XmlManifest) \
72 or not self.manifest.manifestProject.Exists:
73 g.add_option('-m', '--manifest-name',
74 dest='manifest_name', default='default.xml',
75 help='initial manifest file', metavar='NAME.xml')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -080076 g.add_option('--mirror',
77 dest='mirror', action='store_true',
78 help='mirror the forrest')
79
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070080
81 # Tool
Shawn O. Pearcefd89b672009-04-18 11:28:57 -070082 g = p.add_option_group('repo Version options')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070083 g.add_option('--repo-url',
84 dest='repo_url',
85 help='repo repository location', metavar='URL')
86 g.add_option('--repo-branch',
87 dest='repo_branch',
88 help='repo branch or revision', metavar='REVISION')
89 g.add_option('--no-repo-verify',
90 dest='no_repo_verify', action='store_true',
91 help='do not verify repo source code')
92
Shawn O. Pearce5f947bb2009-07-03 17:24:17 -070093 def _ApplyOptions(self, opt, is_new):
94 m = self.manifest.manifestProject
95
96 if is_new:
97 if opt.manifest_origin:
98 m.remote.name = opt.manifest_origin
99
100 if opt.manifest_branch:
101 m.revisionExpr = opt.manifest_branch
102 else:
103 m.revisionExpr = 'refs/heads/master'
104 else:
105 if opt.manifest_origin:
106 print >>sys.stderr, 'fatal: cannot change origin name'
107 sys.exit(1)
108
109 if opt.manifest_branch:
110 m.revisionExpr = opt.manifest_branch
111 else:
112 m.PreSync()
113
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700114 def _SyncManifest(self, opt):
115 m = self.manifest.manifestProject
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700116 is_new = not m.Exists
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700117
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700118 if is_new:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700119 if not opt.manifest_url:
120 print >>sys.stderr, 'fatal: manifest url (-u) is required.'
121 sys.exit(1)
122
123 if not opt.quiet:
124 print >>sys.stderr, 'Getting manifest ...'
125 print >>sys.stderr, ' from %s' % opt.manifest_url
126 m._InitGitDir()
127
Shawn O. Pearce5f947bb2009-07-03 17:24:17 -0700128 self._ApplyOptions(opt, is_new)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700129 if opt.manifest_url:
130 r = m.GetRemote(m.remote.name)
131 r.url = opt.manifest_url
132 r.ResetFetch()
133 r.Save()
134
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800135 if opt.mirror:
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700136 if is_new:
137 m.config.SetString('repo.mirror', 'true')
Shawn O. Pearce7354d882009-07-03 20:06:13 -0700138 m.config.ClearCache()
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700139 else:
140 print >>sys.stderr, 'fatal: --mirror not supported on existing client'
141 sys.exit(1)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800142
Shawn O. Pearce1fc99f42009-03-17 08:06:18 -0700143 if not m.Sync_NetworkHalf():
144 r = m.GetRemote(m.remote.name)
145 print >>sys.stderr, 'fatal: cannot obtain manifest %s' % r.url
146 sys.exit(1)
147
Shawn O. Pearce0125ae22009-07-03 18:05:23 -0700148 if is_new and SubmoduleManifest.IsBare(m):
149 new = self.GetManifest(reparse=True, type=SubmoduleManifest)
150 if m.gitdir != new.manifestProject.gitdir:
151 os.rename(m.gitdir, new.manifestProject.gitdir)
152 new = self.GetManifest(reparse=True, type=SubmoduleManifest)
153 m = new.manifestProject
154 self._ApplyOptions(opt, is_new)
155
Shawn O. Pearce87bda122009-07-03 16:37:30 -0700156 if not is_new:
157 # Force the manifest to load if it exists, the old graph
158 # may be needed inside of _ReloadManifest().
159 #
160 self.manifest.projects
161
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700162 syncbuf = SyncBuffer(m.config)
163 m.Sync_LocalHalf(syncbuf)
164 syncbuf.Finish()
Shawn O. Pearce87bda122009-07-03 16:37:30 -0700165 _ReloadManifest(self)
166 self._ApplyOptions(opt, is_new)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700167
Shawn O. Pearce75b87c82009-07-03 16:24:57 -0700168 if not self.manifest.InitBranch():
169 print >>sys.stderr, 'fatal: cannot create branch in manifest'
170 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700171
172 def _LinkManifest(self, name):
173 if not name:
174 print >>sys.stderr, 'fatal: manifest name (-m) is required.'
175 sys.exit(1)
176
177 try:
178 self.manifest.Link(name)
179 except ManifestParseError, e:
180 print >>sys.stderr, "fatal: manifest '%s' not available" % name
181 print >>sys.stderr, 'fatal: %s' % str(e)
182 sys.exit(1)
183
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700184 def _Prompt(self, prompt, value):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700185 mp = self.manifest.manifestProject
186
187 sys.stdout.write('%-10s [%s]: ' % (prompt, value))
188 a = sys.stdin.readline().strip()
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700189 if a == '':
190 return value
191 return a
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700192
193 def _ConfigureUser(self):
194 mp = self.manifest.manifestProject
195
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700196 while True:
197 print ''
198 name = self._Prompt('Your Name', mp.UserName)
199 email = self._Prompt('Your Email', mp.UserEmail)
200
201 print ''
202 print 'Your identity is: %s <%s>' % (name, email)
203 sys.stdout.write('is this correct [yes/no]? ')
204 if 'yes' == sys.stdin.readline().strip():
205 break
206
207 if name != mp.UserName:
208 mp.config.SetString('user.name', name)
209 if email != mp.UserEmail:
210 mp.config.SetString('user.email', email)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700211
212 def _HasColorSet(self, gc):
213 for n in ['ui', 'diff', 'status']:
214 if gc.Has('color.%s' % n):
215 return True
216 return False
217
218 def _ConfigureColor(self):
219 gc = self.manifest.globalConfig
220 if self._HasColorSet(gc):
221 return
222
223 class _Test(Coloring):
224 def __init__(self):
225 Coloring.__init__(self, gc, 'test color display')
226 self._on = True
227 out = _Test()
228
229 print ''
230 print "Testing colorized output (for 'repo diff', 'repo status'):"
231
232 for c in ['black','red','green','yellow','blue','magenta','cyan']:
233 out.write(' ')
234 out.printer(fg=c)(' %-6s ', c)
235 out.write(' ')
236 out.printer(fg='white', bg='black')(' %s ' % 'white')
237 out.nl()
238
239 for c in ['bold','dim','ul','reverse']:
240 out.write(' ')
241 out.printer(fg='black', attr=c)(' %-6s ', c)
242 out.nl()
243
244 sys.stdout.write('Enable color display in this user account (y/n)? ')
245 a = sys.stdin.readline().strip().lower()
246 if a in ('y', 'yes', 't', 'true', 'on'):
247 gc.SetString('color.ui', 'auto')
248
249 def Execute(self, opt, args):
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700250 git_require(MIN_GIT_VERSION, fail=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700251 self._SyncManifest(opt)
Shawn O. Pearce446c4e52009-05-19 18:14:04 -0700252 if isinstance(self.manifest, XmlManifest):
253 self._LinkManifest(opt.manifest_name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700254
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700255 if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700256 self._ConfigureUser()
257 self._ConfigureColor()
258
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700259 if self.manifest.IsMirror:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800260 type = 'mirror '
261 else:
262 type = ''
263
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700264 print ''
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800265 print 'repo %sinitialized in %s' % (type, self.manifest.topdir)