blob: ec87d0361630d47722f97e23e693444bcfab5631 [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. Pearce446c4e52009-05-19 18:14:04 -070065 if isinstance(self.manifest, XmlManifest) \
66 or not self.manifest.manifestProject.Exists:
67 g.add_option('-m', '--manifest-name',
68 dest='manifest_name', default='default.xml',
69 help='initial manifest file', metavar='NAME.xml')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -080070 g.add_option('--mirror',
71 dest='mirror', action='store_true',
72 help='mirror the forrest')
73
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070074
75 # Tool
Shawn O. Pearcefd89b672009-04-18 11:28:57 -070076 g = p.add_option_group('repo Version options')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070077 g.add_option('--repo-url',
78 dest='repo_url',
79 help='repo repository location', metavar='URL')
80 g.add_option('--repo-branch',
81 dest='repo_branch',
82 help='repo branch or revision', metavar='REVISION')
83 g.add_option('--no-repo-verify',
84 dest='no_repo_verify', action='store_true',
85 help='do not verify repo source code')
86
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070087 def _SyncManifest(self, opt):
88 m = self.manifest.manifestProject
Shawn O. Pearce5470df62009-03-09 18:51:58 -070089 is_new = not m.Exists
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070090
Shawn O. Pearce5470df62009-03-09 18:51:58 -070091 if is_new:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070092 if not opt.manifest_url:
93 print >>sys.stderr, 'fatal: manifest url (-u) is required.'
94 sys.exit(1)
95
96 if not opt.quiet:
97 print >>sys.stderr, 'Getting manifest ...'
98 print >>sys.stderr, ' from %s' % opt.manifest_url
99 m._InitGitDir()
100
101 if opt.manifest_branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700102 m.revisionExpr = opt.manifest_branch
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700103 else:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700104 m.revisionExpr = 'refs/heads/master'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700105 else:
106 if opt.manifest_branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700107 m.revisionExpr = opt.manifest_branch
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700108 else:
109 m.PreSync()
110
111 if opt.manifest_url:
112 r = m.GetRemote(m.remote.name)
113 r.url = opt.manifest_url
114 r.ResetFetch()
115 r.Save()
116
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800117 if opt.mirror:
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700118 if is_new:
119 m.config.SetString('repo.mirror', 'true')
120 else:
121 print >>sys.stderr, 'fatal: --mirror not supported on existing client'
122 sys.exit(1)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800123
Shawn O. Pearce1fc99f42009-03-17 08:06:18 -0700124 if not m.Sync_NetworkHalf():
125 r = m.GetRemote(m.remote.name)
126 print >>sys.stderr, 'fatal: cannot obtain manifest %s' % r.url
127 sys.exit(1)
128
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700129 syncbuf = SyncBuffer(m.config)
130 m.Sync_LocalHalf(syncbuf)
131 syncbuf.Finish()
132
Shawn O. Pearcedf018832009-03-17 08:15:27 -0700133 if is_new or m.CurrentBranch is None:
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700134 if not m.StartBranch('default'):
135 print >>sys.stderr, 'fatal: cannot create default in manifest'
136 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700137
138 def _LinkManifest(self, name):
139 if not name:
140 print >>sys.stderr, 'fatal: manifest name (-m) is required.'
141 sys.exit(1)
142
143 try:
144 self.manifest.Link(name)
145 except ManifestParseError, e:
146 print >>sys.stderr, "fatal: manifest '%s' not available" % name
147 print >>sys.stderr, 'fatal: %s' % str(e)
148 sys.exit(1)
149
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700150 def _Prompt(self, prompt, value):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700151 mp = self.manifest.manifestProject
152
153 sys.stdout.write('%-10s [%s]: ' % (prompt, value))
154 a = sys.stdin.readline().strip()
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700155 if a == '':
156 return value
157 return a
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700158
159 def _ConfigureUser(self):
160 mp = self.manifest.manifestProject
161
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700162 while True:
163 print ''
164 name = self._Prompt('Your Name', mp.UserName)
165 email = self._Prompt('Your Email', mp.UserEmail)
166
167 print ''
168 print 'Your identity is: %s <%s>' % (name, email)
169 sys.stdout.write('is this correct [yes/no]? ')
170 if 'yes' == sys.stdin.readline().strip():
171 break
172
173 if name != mp.UserName:
174 mp.config.SetString('user.name', name)
175 if email != mp.UserEmail:
176 mp.config.SetString('user.email', email)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700177
178 def _HasColorSet(self, gc):
179 for n in ['ui', 'diff', 'status']:
180 if gc.Has('color.%s' % n):
181 return True
182 return False
183
184 def _ConfigureColor(self):
185 gc = self.manifest.globalConfig
186 if self._HasColorSet(gc):
187 return
188
189 class _Test(Coloring):
190 def __init__(self):
191 Coloring.__init__(self, gc, 'test color display')
192 self._on = True
193 out = _Test()
194
195 print ''
196 print "Testing colorized output (for 'repo diff', 'repo status'):"
197
198 for c in ['black','red','green','yellow','blue','magenta','cyan']:
199 out.write(' ')
200 out.printer(fg=c)(' %-6s ', c)
201 out.write(' ')
202 out.printer(fg='white', bg='black')(' %s ' % 'white')
203 out.nl()
204
205 for c in ['bold','dim','ul','reverse']:
206 out.write(' ')
207 out.printer(fg='black', attr=c)(' %-6s ', c)
208 out.nl()
209
210 sys.stdout.write('Enable color display in this user account (y/n)? ')
211 a = sys.stdin.readline().strip().lower()
212 if a in ('y', 'yes', 't', 'true', 'on'):
213 gc.SetString('color.ui', 'auto')
214
215 def Execute(self, opt, args):
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700216 git_require(MIN_GIT_VERSION, fail=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700217 self._SyncManifest(opt)
Shawn O. Pearce446c4e52009-05-19 18:14:04 -0700218 if isinstance(self.manifest, XmlManifest):
219 self._LinkManifest(opt.manifest_name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700220
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700221 if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700222 self._ConfigureUser()
223 self._ConfigureColor()
224
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700225 if self.manifest.IsMirror:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800226 type = 'mirror '
227 else:
228 type = ''
229
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700230 print ''
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800231 print 'repo %sinitialized in %s' % (type, self.manifest.topdir)