blob: a32eaae011e63bb85631438174d55350172c17fa [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
22from remote import Remote
23from git_command import git, MIN_GIT_VERSION
24
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
37The optional <manifest> argument can be used to specify an alternate
38manifest to be used. If no manifest is specified, the manifest
39default.xml will be used.
40"""
41
42 def _Options(self, p):
43 # Logging
44 g = p.add_option_group('Logging options')
45 g.add_option('-q', '--quiet',
46 dest="quiet", action="store_true", default=False,
47 help="be quiet")
48
49 # Manifest
50 g = p.add_option_group('Manifest options')
51 g.add_option('-u', '--manifest-url',
52 dest='manifest_url',
53 help='manifest repository location', metavar='URL')
54 g.add_option('-b', '--manifest-branch',
55 dest='manifest_branch',
56 help='manifest branch or revision', metavar='REVISION')
57 g.add_option('-m', '--manifest-name',
58 dest='manifest_name', default='default.xml',
59 help='initial manifest file', metavar='NAME.xml')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -080060 g.add_option('--mirror',
61 dest='mirror', action='store_true',
62 help='mirror the forrest')
63
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070064
65 # Tool
66 g = p.add_option_group('Version options')
67 g.add_option('--repo-url',
68 dest='repo_url',
69 help='repo repository location', metavar='URL')
70 g.add_option('--repo-branch',
71 dest='repo_branch',
72 help='repo branch or revision', metavar='REVISION')
73 g.add_option('--no-repo-verify',
74 dest='no_repo_verify', action='store_true',
75 help='do not verify repo source code')
76
77 def _CheckGitVersion(self):
78 ver_str = git.version()
79 if not ver_str.startswith('git version '):
80 print >>sys.stderr, 'error: "%s" unsupported' % ver_str
81 sys.exit(1)
82
83 ver_str = ver_str[len('git version '):].strip()
84 ver_act = tuple(map(lambda x: int(x), ver_str.split('.')[0:3]))
85 if ver_act < MIN_GIT_VERSION:
86 need = '.'.join(map(lambda x: str(x), MIN_GIT_VERSION))
87 print >>sys.stderr, 'fatal: git %s or later required' % need
88 sys.exit(1)
89
90 def _SyncManifest(self, opt):
91 m = self.manifest.manifestProject
Shawn O. Pearce5470df62009-03-09 18:51:58 -070092 is_new = not m.Exists
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070093
Shawn O. Pearce5470df62009-03-09 18:51:58 -070094 if is_new:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070095 if not opt.manifest_url:
96 print >>sys.stderr, 'fatal: manifest url (-u) is required.'
97 sys.exit(1)
98
99 if not opt.quiet:
100 print >>sys.stderr, 'Getting manifest ...'
101 print >>sys.stderr, ' from %s' % opt.manifest_url
102 m._InitGitDir()
103
104 if opt.manifest_branch:
105 m.revision = opt.manifest_branch
106 else:
107 m.revision = 'refs/heads/master'
108 else:
109 if opt.manifest_branch:
110 m.revision = opt.manifest_branch
111 else:
112 m.PreSync()
113
114 if opt.manifest_url:
115 r = m.GetRemote(m.remote.name)
116 r.url = opt.manifest_url
117 r.ResetFetch()
118 r.Save()
119
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800120 if opt.mirror:
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700121 if is_new:
122 m.config.SetString('repo.mirror', 'true')
123 else:
124 print >>sys.stderr, 'fatal: --mirror not supported on existing client'
125 sys.exit(1)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800126
Shawn O. Pearce1fc99f42009-03-17 08:06:18 -0700127 if not m.Sync_NetworkHalf():
128 r = m.GetRemote(m.remote.name)
129 print >>sys.stderr, 'fatal: cannot obtain manifest %s' % r.url
130 sys.exit(1)
131
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700132 m.Sync_LocalHalf()
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
150 def _PromptKey(self, prompt, key, value):
151 mp = self.manifest.manifestProject
152
153 sys.stdout.write('%-10s [%s]: ' % (prompt, value))
154 a = sys.stdin.readline().strip()
155 if a != '' and a != value:
156 mp.config.SetString(key, a)
157
158 def _ConfigureUser(self):
159 mp = self.manifest.manifestProject
160
161 print ''
162 self._PromptKey('Your Name', 'user.name', mp.UserName)
163 self._PromptKey('Your Email', 'user.email', mp.UserEmail)
164
165 def _HasColorSet(self, gc):
166 for n in ['ui', 'diff', 'status']:
167 if gc.Has('color.%s' % n):
168 return True
169 return False
170
171 def _ConfigureColor(self):
172 gc = self.manifest.globalConfig
173 if self._HasColorSet(gc):
174 return
175
176 class _Test(Coloring):
177 def __init__(self):
178 Coloring.__init__(self, gc, 'test color display')
179 self._on = True
180 out = _Test()
181
182 print ''
183 print "Testing colorized output (for 'repo diff', 'repo status'):"
184
185 for c in ['black','red','green','yellow','blue','magenta','cyan']:
186 out.write(' ')
187 out.printer(fg=c)(' %-6s ', c)
188 out.write(' ')
189 out.printer(fg='white', bg='black')(' %s ' % 'white')
190 out.nl()
191
192 for c in ['bold','dim','ul','reverse']:
193 out.write(' ')
194 out.printer(fg='black', attr=c)(' %-6s ', c)
195 out.nl()
196
197 sys.stdout.write('Enable color display in this user account (y/n)? ')
198 a = sys.stdin.readline().strip().lower()
199 if a in ('y', 'yes', 't', 'true', 'on'):
200 gc.SetString('color.ui', 'auto')
201
202 def Execute(self, opt, args):
203 self._CheckGitVersion()
204 self._SyncManifest(opt)
205 self._LinkManifest(opt.manifest_name)
206
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700207 if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700208 self._ConfigureUser()
209 self._ConfigureColor()
210
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700211 if self.manifest.IsMirror:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800212 type = 'mirror '
213 else:
214 type = ''
215
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700216 print ''
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800217 print 'repo %sinitialized in %s' % (type, self.manifest.topdir)