blob: 0075b0b48cfb8db507c87d43cda9a14d8f64bd36 [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. Pearce75b87c82009-07-03 16:24:57 -0700133 if not self.manifest.InitBranch():
134 print >>sys.stderr, 'fatal: cannot create branch in manifest'
135 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700136
137 def _LinkManifest(self, name):
138 if not name:
139 print >>sys.stderr, 'fatal: manifest name (-m) is required.'
140 sys.exit(1)
141
142 try:
143 self.manifest.Link(name)
144 except ManifestParseError, e:
145 print >>sys.stderr, "fatal: manifest '%s' not available" % name
146 print >>sys.stderr, 'fatal: %s' % str(e)
147 sys.exit(1)
148
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700149 def _Prompt(self, prompt, value):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700150 mp = self.manifest.manifestProject
151
152 sys.stdout.write('%-10s [%s]: ' % (prompt, value))
153 a = sys.stdin.readline().strip()
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700154 if a == '':
155 return value
156 return a
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700157
158 def _ConfigureUser(self):
159 mp = self.manifest.manifestProject
160
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700161 while True:
162 print ''
163 name = self._Prompt('Your Name', mp.UserName)
164 email = self._Prompt('Your Email', mp.UserEmail)
165
166 print ''
167 print 'Your identity is: %s <%s>' % (name, email)
168 sys.stdout.write('is this correct [yes/no]? ')
169 if 'yes' == sys.stdin.readline().strip():
170 break
171
172 if name != mp.UserName:
173 mp.config.SetString('user.name', name)
174 if email != mp.UserEmail:
175 mp.config.SetString('user.email', email)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700176
177 def _HasColorSet(self, gc):
178 for n in ['ui', 'diff', 'status']:
179 if gc.Has('color.%s' % n):
180 return True
181 return False
182
183 def _ConfigureColor(self):
184 gc = self.manifest.globalConfig
185 if self._HasColorSet(gc):
186 return
187
188 class _Test(Coloring):
189 def __init__(self):
190 Coloring.__init__(self, gc, 'test color display')
191 self._on = True
192 out = _Test()
193
194 print ''
195 print "Testing colorized output (for 'repo diff', 'repo status'):"
196
197 for c in ['black','red','green','yellow','blue','magenta','cyan']:
198 out.write(' ')
199 out.printer(fg=c)(' %-6s ', c)
200 out.write(' ')
201 out.printer(fg='white', bg='black')(' %s ' % 'white')
202 out.nl()
203
204 for c in ['bold','dim','ul','reverse']:
205 out.write(' ')
206 out.printer(fg='black', attr=c)(' %-6s ', c)
207 out.nl()
208
209 sys.stdout.write('Enable color display in this user account (y/n)? ')
210 a = sys.stdin.readline().strip().lower()
211 if a in ('y', 'yes', 't', 'true', 'on'):
212 gc.SetString('color.ui', 'auto')
213
214 def Execute(self, opt, args):
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700215 git_require(MIN_GIT_VERSION, fail=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700216 self._SyncManifest(opt)
Shawn O. Pearce446c4e52009-05-19 18:14:04 -0700217 if isinstance(self.manifest, XmlManifest):
218 self._LinkManifest(opt.manifest_name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700219
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700220 if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700221 self._ConfigureUser()
222 self._ConfigureColor()
223
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700224 if self.manifest.IsMirror:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800225 type = 'mirror '
226 else:
227 type = ''
228
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700229 print ''
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800230 print 'repo %sinitialized in %s' % (type, self.manifest.topdir)