blob: 937296bb356a3f9080b0de7741e5ab251c6223d2 [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
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700127 m.Sync_NetworkHalf()
128 m.Sync_LocalHalf()
Shawn O. Pearce521cd3c2009-03-09 18:53:20 -0700129 if is_new:
130 m.StartBranch('default')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700131
132 def _LinkManifest(self, name):
133 if not name:
134 print >>sys.stderr, 'fatal: manifest name (-m) is required.'
135 sys.exit(1)
136
137 try:
138 self.manifest.Link(name)
139 except ManifestParseError, e:
140 print >>sys.stderr, "fatal: manifest '%s' not available" % name
141 print >>sys.stderr, 'fatal: %s' % str(e)
142 sys.exit(1)
143
144 def _PromptKey(self, prompt, key, value):
145 mp = self.manifest.manifestProject
146
147 sys.stdout.write('%-10s [%s]: ' % (prompt, value))
148 a = sys.stdin.readline().strip()
149 if a != '' and a != value:
150 mp.config.SetString(key, a)
151
152 def _ConfigureUser(self):
153 mp = self.manifest.manifestProject
154
155 print ''
156 self._PromptKey('Your Name', 'user.name', mp.UserName)
157 self._PromptKey('Your Email', 'user.email', mp.UserEmail)
158
159 def _HasColorSet(self, gc):
160 for n in ['ui', 'diff', 'status']:
161 if gc.Has('color.%s' % n):
162 return True
163 return False
164
165 def _ConfigureColor(self):
166 gc = self.manifest.globalConfig
167 if self._HasColorSet(gc):
168 return
169
170 class _Test(Coloring):
171 def __init__(self):
172 Coloring.__init__(self, gc, 'test color display')
173 self._on = True
174 out = _Test()
175
176 print ''
177 print "Testing colorized output (for 'repo diff', 'repo status'):"
178
179 for c in ['black','red','green','yellow','blue','magenta','cyan']:
180 out.write(' ')
181 out.printer(fg=c)(' %-6s ', c)
182 out.write(' ')
183 out.printer(fg='white', bg='black')(' %s ' % 'white')
184 out.nl()
185
186 for c in ['bold','dim','ul','reverse']:
187 out.write(' ')
188 out.printer(fg='black', attr=c)(' %-6s ', c)
189 out.nl()
190
191 sys.stdout.write('Enable color display in this user account (y/n)? ')
192 a = sys.stdin.readline().strip().lower()
193 if a in ('y', 'yes', 't', 'true', 'on'):
194 gc.SetString('color.ui', 'auto')
195
196 def Execute(self, opt, args):
197 self._CheckGitVersion()
198 self._SyncManifest(opt)
199 self._LinkManifest(opt.manifest_name)
200
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800201 if os.isatty(0) and os.isatty(1) and not opt.mirror:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700202 self._ConfigureUser()
203 self._ConfigureColor()
204
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800205 if opt.mirror:
206 type = 'mirror '
207 else:
208 type = ''
209
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700210 print ''
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800211 print 'repo %sinitialized in %s' % (type, self.manifest.topdir)