blob: 379108430949538bfc2de04d35df51d7ae6601db [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
Doug Anderson2630dd92011-04-07 13:36:30 -070017import shutil
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070018import sys
19
20from color import Coloring
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080021from command import InteractiveCommand, MirrorSafeCommand
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070022from error import ManifestParseError
Shawn O. Pearce350cde42009-04-16 11:21:18 -070023from project import SyncBuffer
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -070024from git_command import git_require, MIN_GIT_VERSION
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
41The optional -m argument can be used to specify an alternate manifest
42to be used. If no manifest is specified, the manifest default.xml
43will be used.
44
Shawn O. Pearce88443382010-10-08 10:02:09 +020045The --reference option can be used to point to a directory that
46has the content of a --mirror sync. This will make the working
47directory use as much data as possible from the local reference
48directory when fetching from the server. This will make the sync
49go a lot faster by reducing data traffic on the network.
50
51
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070052Switching Manifest Branches
53---------------------------
54
55To switch to another manifest branch, `repo init -b otherbranch`
56may be used in an existing client. However, as this only updates the
57manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary
58to update the working directory files.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070059"""
60
61 def _Options(self, p):
62 # Logging
63 g = p.add_option_group('Logging options')
64 g.add_option('-q', '--quiet',
65 dest="quiet", action="store_true", default=False,
66 help="be quiet")
67
68 # Manifest
69 g = p.add_option_group('Manifest options')
70 g.add_option('-u', '--manifest-url',
71 dest='manifest_url',
72 help='manifest repository location', metavar='URL')
73 g.add_option('-b', '--manifest-branch',
74 dest='manifest_branch',
75 help='manifest branch or revision', metavar='REVISION')
76 g.add_option('-m', '--manifest-name',
77 dest='manifest_name', default='default.xml',
78 help='initial manifest file', metavar='NAME.xml')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -080079 g.add_option('--mirror',
80 dest='mirror', action='store_true',
81 help='mirror the forrest')
Shawn O. Pearce88443382010-10-08 10:02:09 +020082 g.add_option('--reference',
83 dest='reference',
84 help='location of mirror directory', metavar='DIR')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070085
86 # Tool
Shawn O. Pearcefd89b672009-04-18 11:28:57 -070087 g = p.add_option_group('repo Version options')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070088 g.add_option('--repo-url',
89 dest='repo_url',
90 help='repo repository location', metavar='URL')
91 g.add_option('--repo-branch',
92 dest='repo_branch',
93 help='repo branch or revision', metavar='REVISION')
94 g.add_option('--no-repo-verify',
95 dest='no_repo_verify', action='store_true',
96 help='do not verify repo source code')
97
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070098 def _SyncManifest(self, opt):
99 m = self.manifest.manifestProject
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700100 is_new = not m.Exists
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700101
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700102 if is_new:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700103 if not opt.manifest_url:
104 print >>sys.stderr, 'fatal: manifest url (-u) is required.'
105 sys.exit(1)
106
107 if not opt.quiet:
108 print >>sys.stderr, 'Getting manifest ...'
109 print >>sys.stderr, ' from %s' % opt.manifest_url
110 m._InitGitDir()
111
112 if opt.manifest_branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700113 m.revisionExpr = opt.manifest_branch
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700114 else:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700115 m.revisionExpr = 'refs/heads/master'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700116 else:
117 if opt.manifest_branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700118 m.revisionExpr = opt.manifest_branch
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700119 else:
120 m.PreSync()
121
122 if opt.manifest_url:
123 r = m.GetRemote(m.remote.name)
124 r.url = opt.manifest_url
125 r.ResetFetch()
126 r.Save()
127
Shawn O. Pearce88443382010-10-08 10:02:09 +0200128 if opt.reference:
129 m.config.SetString('repo.reference', opt.reference)
130
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800131 if opt.mirror:
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700132 if is_new:
133 m.config.SetString('repo.mirror', 'true')
134 else:
135 print >>sys.stderr, 'fatal: --mirror not supported on existing client'
136 sys.exit(1)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800137
Shawn O. Pearce1fc99f42009-03-17 08:06:18 -0700138 if not m.Sync_NetworkHalf():
139 r = m.GetRemote(m.remote.name)
140 print >>sys.stderr, 'fatal: cannot obtain manifest %s' % r.url
Doug Anderson2630dd92011-04-07 13:36:30 -0700141
142 # Better delete the manifest git dir if we created it; otherwise next
143 # time (when user fixes problems) we won't go through the "is_new" logic.
144 if is_new:
145 shutil.rmtree(m.gitdir)
Shawn O. Pearce1fc99f42009-03-17 08:06:18 -0700146 sys.exit(1)
147
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700148 syncbuf = SyncBuffer(m.config)
149 m.Sync_LocalHalf(syncbuf)
150 syncbuf.Finish()
151
Shawn O. Pearcedf018832009-03-17 08:15:27 -0700152 if is_new or m.CurrentBranch is None:
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700153 if not m.StartBranch('default'):
154 print >>sys.stderr, 'fatal: cannot create default in manifest'
155 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700156
157 def _LinkManifest(self, name):
158 if not name:
159 print >>sys.stderr, 'fatal: manifest name (-m) is required.'
160 sys.exit(1)
161
162 try:
163 self.manifest.Link(name)
164 except ManifestParseError, e:
165 print >>sys.stderr, "fatal: manifest '%s' not available" % name
166 print >>sys.stderr, 'fatal: %s' % str(e)
167 sys.exit(1)
168
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700169 def _Prompt(self, prompt, value):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700170 mp = self.manifest.manifestProject
171
172 sys.stdout.write('%-10s [%s]: ' % (prompt, value))
173 a = sys.stdin.readline().strip()
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700174 if a == '':
175 return value
176 return a
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700177
178 def _ConfigureUser(self):
179 mp = self.manifest.manifestProject
180
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700181 while True:
182 print ''
183 name = self._Prompt('Your Name', mp.UserName)
184 email = self._Prompt('Your Email', mp.UserEmail)
185
186 print ''
187 print 'Your identity is: %s <%s>' % (name, email)
Nico Sallembien6d7508b2010-04-01 11:03:53 -0700188 sys.stdout.write('is this correct [y/n]? ')
189 a = sys.stdin.readline().strip()
190 if a in ('yes', 'y', 't', 'true'):
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700191 break
192
193 if name != mp.UserName:
194 mp.config.SetString('user.name', name)
195 if email != mp.UserEmail:
196 mp.config.SetString('user.email', email)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700197
198 def _HasColorSet(self, gc):
199 for n in ['ui', 'diff', 'status']:
200 if gc.Has('color.%s' % n):
201 return True
202 return False
203
204 def _ConfigureColor(self):
205 gc = self.manifest.globalConfig
206 if self._HasColorSet(gc):
207 return
208
209 class _Test(Coloring):
210 def __init__(self):
211 Coloring.__init__(self, gc, 'test color display')
212 self._on = True
213 out = _Test()
214
215 print ''
216 print "Testing colorized output (for 'repo diff', 'repo status'):"
217
218 for c in ['black','red','green','yellow','blue','magenta','cyan']:
219 out.write(' ')
220 out.printer(fg=c)(' %-6s ', c)
221 out.write(' ')
222 out.printer(fg='white', bg='black')(' %s ' % 'white')
223 out.nl()
224
225 for c in ['bold','dim','ul','reverse']:
226 out.write(' ')
227 out.printer(fg='black', attr=c)(' %-6s ', c)
228 out.nl()
229
230 sys.stdout.write('Enable color display in this user account (y/n)? ')
231 a = sys.stdin.readline().strip().lower()
232 if a in ('y', 'yes', 't', 'true', 'on'):
233 gc.SetString('color.ui', 'auto')
234
235 def Execute(self, opt, args):
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700236 git_require(MIN_GIT_VERSION, fail=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700237 self._SyncManifest(opt)
238 self._LinkManifest(opt.manifest_name)
239
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700240 if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700241 self._ConfigureUser()
242 self._ConfigureColor()
243
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700244 if self.manifest.IsMirror:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800245 type = 'mirror '
246 else:
247 type = ''
248
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700249 print ''
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800250 print 'repo %sinitialized in %s' % (type, self.manifest.topdir)