blob: fbc406e22418b1046e423ff76f5bc334b1348cd5 [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
44Switching Manifest Branches
45---------------------------
46
47To switch to another manifest branch, `repo init -b otherbranch`
48may be used in an existing client. However, as this only updates the
49manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary
50to update the working directory files.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070051"""
52
53 def _Options(self, p):
54 # Logging
55 g = p.add_option_group('Logging options')
56 g.add_option('-q', '--quiet',
57 dest="quiet", action="store_true", default=False,
58 help="be quiet")
59
60 # Manifest
61 g = p.add_option_group('Manifest options')
62 g.add_option('-u', '--manifest-url',
63 dest='manifest_url',
64 help='manifest repository location', metavar='URL')
65 g.add_option('-b', '--manifest-branch',
66 dest='manifest_branch',
67 help='manifest branch or revision', metavar='REVISION')
68 g.add_option('-m', '--manifest-name',
69 dest='manifest_name', default='default.xml',
70 help='initial manifest file', metavar='NAME.xml')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -080071 g.add_option('--mirror',
72 dest='mirror', action='store_true',
73 help='mirror the forrest')
74
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070075
76 # Tool
Shawn O. Pearcefd89b672009-04-18 11:28:57 -070077 g = p.add_option_group('repo Version options')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070078 g.add_option('--repo-url',
79 dest='repo_url',
80 help='repo repository location', metavar='URL')
81 g.add_option('--repo-branch',
82 dest='repo_branch',
83 help='repo branch or revision', metavar='REVISION')
84 g.add_option('--no-repo-verify',
85 dest='no_repo_verify', action='store_true',
86 help='do not verify repo source code')
87
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070088 def _SyncManifest(self, opt):
89 m = self.manifest.manifestProject
Shawn O. Pearce5470df62009-03-09 18:51:58 -070090 is_new = not m.Exists
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070091
Shawn O. Pearce5470df62009-03-09 18:51:58 -070092 if is_new:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070093 if not opt.manifest_url:
94 print >>sys.stderr, 'fatal: manifest url (-u) is required.'
95 sys.exit(1)
96
97 if not opt.quiet:
98 print >>sys.stderr, 'Getting manifest ...'
99 print >>sys.stderr, ' from %s' % opt.manifest_url
100 m._InitGitDir()
101
102 if opt.manifest_branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700103 m.revisionExpr = opt.manifest_branch
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700104 else:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700105 m.revisionExpr = 'refs/heads/master'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700106 else:
107 if opt.manifest_branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700108 m.revisionExpr = opt.manifest_branch
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700109 else:
110 m.PreSync()
111
112 if opt.manifest_url:
113 r = m.GetRemote(m.remote.name)
114 r.url = opt.manifest_url
115 r.ResetFetch()
116 r.Save()
117
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800118 if opt.mirror:
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700119 if is_new:
120 m.config.SetString('repo.mirror', 'true')
121 else:
122 print >>sys.stderr, 'fatal: --mirror not supported on existing client'
123 sys.exit(1)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800124
Shawn O. Pearce1fc99f42009-03-17 08:06:18 -0700125 if not m.Sync_NetworkHalf():
126 r = m.GetRemote(m.remote.name)
127 print >>sys.stderr, 'fatal: cannot obtain manifest %s' % r.url
128 sys.exit(1)
129
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700130 syncbuf = SyncBuffer(m.config)
131 m.Sync_LocalHalf(syncbuf)
132 syncbuf.Finish()
133
Shawn O. Pearcedf018832009-03-17 08:15:27 -0700134 if is_new or m.CurrentBranch is None:
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700135 if not m.StartBranch('default'):
136 print >>sys.stderr, 'fatal: cannot create default in manifest'
137 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700138
139 def _LinkManifest(self, name):
140 if not name:
141 print >>sys.stderr, 'fatal: manifest name (-m) is required.'
142 sys.exit(1)
143
144 try:
145 self.manifest.Link(name)
146 except ManifestParseError, e:
147 print >>sys.stderr, "fatal: manifest '%s' not available" % name
148 print >>sys.stderr, 'fatal: %s' % str(e)
149 sys.exit(1)
150
151 def _PromptKey(self, prompt, key, value):
152 mp = self.manifest.manifestProject
153
154 sys.stdout.write('%-10s [%s]: ' % (prompt, value))
155 a = sys.stdin.readline().strip()
156 if a != '' and a != value:
157 mp.config.SetString(key, a)
158
159 def _ConfigureUser(self):
160 mp = self.manifest.manifestProject
161
162 print ''
163 self._PromptKey('Your Name', 'user.name', mp.UserName)
164 self._PromptKey('Your Email', 'user.email', mp.UserEmail)
165
166 def _HasColorSet(self, gc):
167 for n in ['ui', 'diff', 'status']:
168 if gc.Has('color.%s' % n):
169 return True
170 return False
171
172 def _ConfigureColor(self):
173 gc = self.manifest.globalConfig
174 if self._HasColorSet(gc):
175 return
176
177 class _Test(Coloring):
178 def __init__(self):
179 Coloring.__init__(self, gc, 'test color display')
180 self._on = True
181 out = _Test()
182
183 print ''
184 print "Testing colorized output (for 'repo diff', 'repo status'):"
185
186 for c in ['black','red','green','yellow','blue','magenta','cyan']:
187 out.write(' ')
188 out.printer(fg=c)(' %-6s ', c)
189 out.write(' ')
190 out.printer(fg='white', bg='black')(' %s ' % 'white')
191 out.nl()
192
193 for c in ['bold','dim','ul','reverse']:
194 out.write(' ')
195 out.printer(fg='black', attr=c)(' %-6s ', c)
196 out.nl()
197
198 sys.stdout.write('Enable color display in this user account (y/n)? ')
199 a = sys.stdin.readline().strip().lower()
200 if a in ('y', 'yes', 't', 'true', 'on'):
201 gc.SetString('color.ui', 'auto')
202
203 def Execute(self, opt, args):
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700204 git_require(MIN_GIT_VERSION, fail=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700205 self._SyncManifest(opt)
206 self._LinkManifest(opt.manifest_name)
207
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700208 if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700209 self._ConfigureUser()
210 self._ConfigureColor()
211
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700212 if self.manifest.IsMirror:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800213 type = 'mirror '
214 else:
215 type = ''
216
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700217 print ''
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800218 print 'repo %sinitialized in %s' % (type, self.manifest.topdir)