blob: ad28a6117288837b386bd9a5f27580b2f1cf4d09 [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
20from command import InteractiveCommand
21from error import ManifestParseError
22from remote import Remote
23from git_command import git, MIN_GIT_VERSION
24
25class Init(InteractiveCommand):
26 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
92
93 if not m.Exists:
94 if not opt.manifest_url:
95 print >>sys.stderr, 'fatal: manifest url (-u) is required.'
96 sys.exit(1)
97
98 if not opt.quiet:
99 print >>sys.stderr, 'Getting manifest ...'
100 print >>sys.stderr, ' from %s' % opt.manifest_url
101 m._InitGitDir()
102
103 if opt.manifest_branch:
104 m.revision = opt.manifest_branch
105 else:
106 m.revision = 'refs/heads/master'
107 else:
108 if opt.manifest_branch:
109 m.revision = opt.manifest_branch
110 else:
111 m.PreSync()
112
113 if opt.manifest_url:
114 r = m.GetRemote(m.remote.name)
115 r.url = opt.manifest_url
116 r.ResetFetch()
117 r.Save()
118
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800119 if opt.mirror:
120 m.config.SetString('repo.mirror', 'true')
121
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700122 m.Sync_NetworkHalf()
123 m.Sync_LocalHalf()
124 m.StartBranch('default')
125
126 def _LinkManifest(self, name):
127 if not name:
128 print >>sys.stderr, 'fatal: manifest name (-m) is required.'
129 sys.exit(1)
130
131 try:
132 self.manifest.Link(name)
133 except ManifestParseError, e:
134 print >>sys.stderr, "fatal: manifest '%s' not available" % name
135 print >>sys.stderr, 'fatal: %s' % str(e)
136 sys.exit(1)
137
138 def _PromptKey(self, prompt, key, value):
139 mp = self.manifest.manifestProject
140
141 sys.stdout.write('%-10s [%s]: ' % (prompt, value))
142 a = sys.stdin.readline().strip()
143 if a != '' and a != value:
144 mp.config.SetString(key, a)
145
146 def _ConfigureUser(self):
147 mp = self.manifest.manifestProject
148
149 print ''
150 self._PromptKey('Your Name', 'user.name', mp.UserName)
151 self._PromptKey('Your Email', 'user.email', mp.UserEmail)
152
153 def _HasColorSet(self, gc):
154 for n in ['ui', 'diff', 'status']:
155 if gc.Has('color.%s' % n):
156 return True
157 return False
158
159 def _ConfigureColor(self):
160 gc = self.manifest.globalConfig
161 if self._HasColorSet(gc):
162 return
163
164 class _Test(Coloring):
165 def __init__(self):
166 Coloring.__init__(self, gc, 'test color display')
167 self._on = True
168 out = _Test()
169
170 print ''
171 print "Testing colorized output (for 'repo diff', 'repo status'):"
172
173 for c in ['black','red','green','yellow','blue','magenta','cyan']:
174 out.write(' ')
175 out.printer(fg=c)(' %-6s ', c)
176 out.write(' ')
177 out.printer(fg='white', bg='black')(' %s ' % 'white')
178 out.nl()
179
180 for c in ['bold','dim','ul','reverse']:
181 out.write(' ')
182 out.printer(fg='black', attr=c)(' %-6s ', c)
183 out.nl()
184
185 sys.stdout.write('Enable color display in this user account (y/n)? ')
186 a = sys.stdin.readline().strip().lower()
187 if a in ('y', 'yes', 't', 'true', 'on'):
188 gc.SetString('color.ui', 'auto')
189
190 def Execute(self, opt, args):
191 self._CheckGitVersion()
192 self._SyncManifest(opt)
193 self._LinkManifest(opt.manifest_name)
194
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800195 if os.isatty(0) and os.isatty(1) and not opt.mirror:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700196 self._ConfigureUser()
197 self._ConfigureColor()
198
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800199 if opt.mirror:
200 type = 'mirror '
201 else:
202 type = ''
203
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700204 print ''
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800205 print 'repo %sinitialized in %s' % (type, self.manifest.topdir)