The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # |
| 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 | |
| 16 | import os |
Doug Anderson | 2630dd9 | 2011-04-07 13:36:30 -0700 | [diff] [blame] | 17 | import shutil |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 18 | import sys |
| 19 | |
| 20 | from color import Coloring |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 21 | from command import InteractiveCommand, MirrorSafeCommand |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 22 | from error import ManifestParseError |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 23 | from project import SyncBuffer |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 24 | from git_config import GitConfig |
Shawn O. Pearce | 2ec00b9 | 2009-06-12 09:32:50 -0700 | [diff] [blame] | 25 | from git_command import git_require, MIN_GIT_VERSION |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 27 | class Init(InteractiveCommand, MirrorSafeCommand): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 28 | common = True |
| 29 | helpSummary = "Initialize repo in the current directory" |
| 30 | helpUsage = """ |
| 31 | %prog [options] |
| 32 | """ |
| 33 | helpDescription = """ |
| 34 | The '%prog' command is run once to install and initialize repo. |
| 35 | The latest repo source code and manifest collection is downloaded |
| 36 | from the server and is installed in the .repo/ directory in the |
| 37 | current working directory. |
| 38 | |
Shawn O. Pearce | 77bb4af | 2009-04-18 11:33:32 -0700 | [diff] [blame] | 39 | The optional -b argument can be used to select the manifest branch |
| 40 | to checkout and use. If no branch is specified, master is assumed. |
| 41 | |
| 42 | The optional -m argument can be used to specify an alternate manifest |
| 43 | to be used. If no manifest is specified, the manifest default.xml |
| 44 | will be used. |
| 45 | |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 46 | The --reference option can be used to point to a directory that |
| 47 | has the content of a --mirror sync. This will make the working |
| 48 | directory use as much data as possible from the local reference |
| 49 | directory when fetching from the server. This will make the sync |
| 50 | go a lot faster by reducing data traffic on the network. |
| 51 | |
| 52 | |
Shawn O. Pearce | 77bb4af | 2009-04-18 11:33:32 -0700 | [diff] [blame] | 53 | Switching Manifest Branches |
| 54 | --------------------------- |
| 55 | |
| 56 | To switch to another manifest branch, `repo init -b otherbranch` |
| 57 | may be used in an existing client. However, as this only updates the |
| 58 | manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary |
| 59 | to update the working directory files. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 60 | """ |
| 61 | |
| 62 | def _Options(self, p): |
| 63 | # Logging |
| 64 | g = p.add_option_group('Logging options') |
| 65 | g.add_option('-q', '--quiet', |
| 66 | dest="quiet", action="store_true", default=False, |
| 67 | help="be quiet") |
| 68 | |
| 69 | # Manifest |
| 70 | g = p.add_option_group('Manifest options') |
| 71 | g.add_option('-u', '--manifest-url', |
Shawn O. Pearce | 34fb20f | 2011-11-30 13:41:02 -0800 | [diff] [blame] | 72 | dest='manifest_url', |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 73 | help='manifest repository location', metavar='URL') |
| 74 | g.add_option('-b', '--manifest-branch', |
| 75 | dest='manifest_branch', |
| 76 | help='manifest branch or revision', metavar='REVISION') |
| 77 | g.add_option('-m', '--manifest-name', |
| 78 | dest='manifest_name', default='default.xml', |
| 79 | help='initial manifest file', metavar='NAME.xml') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 80 | g.add_option('--mirror', |
| 81 | dest='mirror', action='store_true', |
| 82 | help='mirror the forrest') |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 83 | g.add_option('--reference', |
| 84 | dest='reference', |
| 85 | help='location of mirror directory', metavar='DIR') |
Doug Anderson | 30d4529 | 2011-05-04 15:01:04 -0700 | [diff] [blame] | 86 | g.add_option('--depth', type='int', default=None, |
| 87 | dest='depth', |
| 88 | help='create a shallow clone with given depth; see git clone') |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 89 | g.add_option('-g', '--groups', |
| 90 | dest='groups', default="", |
| 91 | help='restrict manifest projects to ones with a specified group', |
| 92 | metavar='GROUP') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 93 | |
| 94 | # Tool |
Shawn O. Pearce | fd89b67 | 2009-04-18 11:28:57 -0700 | [diff] [blame] | 95 | g = p.add_option_group('repo Version options') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 96 | g.add_option('--repo-url', |
| 97 | dest='repo_url', |
| 98 | help='repo repository location', metavar='URL') |
| 99 | g.add_option('--repo-branch', |
| 100 | dest='repo_branch', |
| 101 | help='repo branch or revision', metavar='REVISION') |
| 102 | g.add_option('--no-repo-verify', |
| 103 | dest='no_repo_verify', action='store_true', |
| 104 | help='do not verify repo source code') |
| 105 | |
Victor Boivie | 841be34 | 2011-04-05 11:31:10 +0200 | [diff] [blame] | 106 | # Other |
| 107 | g = p.add_option_group('Other options') |
| 108 | g.add_option('--config-name', |
| 109 | dest='config_name', action="store_true", default=False, |
| 110 | help='Always prompt for name/e-mail') |
| 111 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 112 | def _SyncManifest(self, opt): |
| 113 | m = self.manifest.manifestProject |
Shawn O. Pearce | 5470df6 | 2009-03-09 18:51:58 -0700 | [diff] [blame] | 114 | is_new = not m.Exists |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 115 | |
Shawn O. Pearce | 5470df6 | 2009-03-09 18:51:58 -0700 | [diff] [blame] | 116 | if is_new: |
Shawn O. Pearce | 34fb20f | 2011-11-30 13:41:02 -0800 | [diff] [blame] | 117 | if not opt.manifest_url: |
| 118 | print >>sys.stderr, 'fatal: manifest url (-u) is required.' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 119 | sys.exit(1) |
| 120 | |
| 121 | if not opt.quiet: |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 122 | print >>sys.stderr, 'Get %s' \ |
| 123 | % GitConfig.ForUser().UrlInsteadOf(opt.manifest_url) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 124 | m._InitGitDir() |
| 125 | |
| 126 | if opt.manifest_branch: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 127 | m.revisionExpr = opt.manifest_branch |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 128 | else: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 129 | m.revisionExpr = 'refs/heads/master' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 130 | else: |
| 131 | if opt.manifest_branch: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 132 | m.revisionExpr = opt.manifest_branch |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | else: |
| 134 | m.PreSync() |
| 135 | |
| 136 | if opt.manifest_url: |
| 137 | r = m.GetRemote(m.remote.name) |
| 138 | r.url = opt.manifest_url |
| 139 | r.ResetFetch() |
| 140 | r.Save() |
| 141 | |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 142 | m.config.SetString('manifest.groups', opt.groups) |
| 143 | |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 144 | if opt.reference: |
| 145 | m.config.SetString('repo.reference', opt.reference) |
| 146 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 147 | if opt.mirror: |
Shawn O. Pearce | 5470df6 | 2009-03-09 18:51:58 -0700 | [diff] [blame] | 148 | if is_new: |
| 149 | m.config.SetString('repo.mirror', 'true') |
| 150 | else: |
| 151 | print >>sys.stderr, 'fatal: --mirror not supported on existing client' |
| 152 | sys.exit(1) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 153 | |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 154 | if not m.Sync_NetworkHalf(is_new=is_new): |
Shawn O. Pearce | 1fc99f4 | 2009-03-17 08:06:18 -0700 | [diff] [blame] | 155 | r = m.GetRemote(m.remote.name) |
| 156 | print >>sys.stderr, 'fatal: cannot obtain manifest %s' % r.url |
Doug Anderson | 2630dd9 | 2011-04-07 13:36:30 -0700 | [diff] [blame] | 157 | |
| 158 | # Better delete the manifest git dir if we created it; otherwise next |
| 159 | # time (when user fixes problems) we won't go through the "is_new" logic. |
| 160 | if is_new: |
| 161 | shutil.rmtree(m.gitdir) |
Shawn O. Pearce | 1fc99f4 | 2009-03-17 08:06:18 -0700 | [diff] [blame] | 162 | sys.exit(1) |
| 163 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 164 | syncbuf = SyncBuffer(m.config) |
| 165 | m.Sync_LocalHalf(syncbuf) |
| 166 | syncbuf.Finish() |
| 167 | |
Shawn O. Pearce | df01883 | 2009-03-17 08:15:27 -0700 | [diff] [blame] | 168 | if is_new or m.CurrentBranch is None: |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 169 | if not m.StartBranch('default'): |
| 170 | print >>sys.stderr, 'fatal: cannot create default in manifest' |
| 171 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 172 | |
| 173 | def _LinkManifest(self, name): |
| 174 | if not name: |
| 175 | print >>sys.stderr, 'fatal: manifest name (-m) is required.' |
| 176 | sys.exit(1) |
| 177 | |
| 178 | try: |
| 179 | self.manifest.Link(name) |
| 180 | except ManifestParseError, e: |
| 181 | print >>sys.stderr, "fatal: manifest '%s' not available" % name |
| 182 | print >>sys.stderr, 'fatal: %s' % str(e) |
| 183 | sys.exit(1) |
| 184 | |
Shawn O. Pearce | 37dbf2b | 2009-07-02 10:53:04 -0700 | [diff] [blame] | 185 | def _Prompt(self, prompt, value): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 186 | mp = self.manifest.manifestProject |
| 187 | |
| 188 | sys.stdout.write('%-10s [%s]: ' % (prompt, value)) |
| 189 | a = sys.stdin.readline().strip() |
Shawn O. Pearce | 37dbf2b | 2009-07-02 10:53:04 -0700 | [diff] [blame] | 190 | if a == '': |
| 191 | return value |
| 192 | return a |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 193 | |
Victor Boivie | 841be34 | 2011-04-05 11:31:10 +0200 | [diff] [blame] | 194 | def _ShouldConfigureUser(self): |
| 195 | gc = self.manifest.globalConfig |
| 196 | mp = self.manifest.manifestProject |
| 197 | |
| 198 | # If we don't have local settings, get from global. |
| 199 | if not mp.config.Has('user.name') or not mp.config.Has('user.email'): |
| 200 | if not gc.Has('user.name') or not gc.Has('user.email'): |
| 201 | return True |
| 202 | |
| 203 | mp.config.SetString('user.name', gc.GetString('user.name')) |
| 204 | mp.config.SetString('user.email', gc.GetString('user.email')) |
| 205 | |
| 206 | print '' |
| 207 | print 'Your identity is: %s <%s>' % (mp.config.GetString('user.name'), |
| 208 | mp.config.GetString('user.email')) |
| 209 | print 'If you want to change this, please re-run \'repo init\' with --config-name' |
| 210 | return False |
| 211 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 212 | def _ConfigureUser(self): |
| 213 | mp = self.manifest.manifestProject |
| 214 | |
Shawn O. Pearce | 37dbf2b | 2009-07-02 10:53:04 -0700 | [diff] [blame] | 215 | while True: |
| 216 | print '' |
| 217 | name = self._Prompt('Your Name', mp.UserName) |
| 218 | email = self._Prompt('Your Email', mp.UserEmail) |
| 219 | |
| 220 | print '' |
| 221 | print 'Your identity is: %s <%s>' % (name, email) |
Mike Frysinger | e931127 | 2011-08-11 15:46:43 -0400 | [diff] [blame] | 222 | sys.stdout.write('is this correct [y/N]? ') |
Nico Sallembien | 6d7508b | 2010-04-01 11:03:53 -0700 | [diff] [blame] | 223 | a = sys.stdin.readline().strip() |
| 224 | if a in ('yes', 'y', 't', 'true'): |
Shawn O. Pearce | 37dbf2b | 2009-07-02 10:53:04 -0700 | [diff] [blame] | 225 | break |
| 226 | |
| 227 | if name != mp.UserName: |
| 228 | mp.config.SetString('user.name', name) |
| 229 | if email != mp.UserEmail: |
| 230 | mp.config.SetString('user.email', email) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 231 | |
| 232 | def _HasColorSet(self, gc): |
| 233 | for n in ['ui', 'diff', 'status']: |
| 234 | if gc.Has('color.%s' % n): |
| 235 | return True |
| 236 | return False |
| 237 | |
| 238 | def _ConfigureColor(self): |
| 239 | gc = self.manifest.globalConfig |
| 240 | if self._HasColorSet(gc): |
| 241 | return |
| 242 | |
| 243 | class _Test(Coloring): |
| 244 | def __init__(self): |
| 245 | Coloring.__init__(self, gc, 'test color display') |
| 246 | self._on = True |
| 247 | out = _Test() |
| 248 | |
| 249 | print '' |
| 250 | print "Testing colorized output (for 'repo diff', 'repo status'):" |
| 251 | |
| 252 | for c in ['black','red','green','yellow','blue','magenta','cyan']: |
| 253 | out.write(' ') |
| 254 | out.printer(fg=c)(' %-6s ', c) |
| 255 | out.write(' ') |
| 256 | out.printer(fg='white', bg='black')(' %s ' % 'white') |
| 257 | out.nl() |
| 258 | |
| 259 | for c in ['bold','dim','ul','reverse']: |
| 260 | out.write(' ') |
| 261 | out.printer(fg='black', attr=c)(' %-6s ', c) |
| 262 | out.nl() |
| 263 | |
Mike Frysinger | e931127 | 2011-08-11 15:46:43 -0400 | [diff] [blame] | 264 | sys.stdout.write('Enable color display in this user account (y/N)? ') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 265 | a = sys.stdin.readline().strip().lower() |
| 266 | if a in ('y', 'yes', 't', 'true', 'on'): |
| 267 | gc.SetString('color.ui', 'auto') |
| 268 | |
Doug Anderson | 30d4529 | 2011-05-04 15:01:04 -0700 | [diff] [blame] | 269 | def _ConfigureDepth(self, opt): |
| 270 | """Configure the depth we'll sync down. |
| 271 | |
| 272 | Args: |
| 273 | opt: Options from optparse. We care about opt.depth. |
| 274 | """ |
| 275 | # Opt.depth will be non-None if user actually passed --depth to repo init. |
| 276 | if opt.depth is not None: |
| 277 | if opt.depth > 0: |
| 278 | # Positive values will set the depth. |
| 279 | depth = str(opt.depth) |
| 280 | else: |
| 281 | # Negative numbers will clear the depth; passing None to SetString |
| 282 | # will do that. |
| 283 | depth = None |
| 284 | |
| 285 | # We store the depth in the main manifest project. |
| 286 | self.manifest.manifestProject.config.SetString('repo.depth', depth) |
| 287 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 288 | def Execute(self, opt, args): |
Shawn O. Pearce | 2ec00b9 | 2009-06-12 09:32:50 -0700 | [diff] [blame] | 289 | git_require(MIN_GIT_VERSION, fail=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 290 | self._SyncManifest(opt) |
| 291 | self._LinkManifest(opt.manifest_name) |
| 292 | |
Shawn O. Pearce | 8630f39 | 2009-03-19 10:17:12 -0700 | [diff] [blame] | 293 | if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror: |
Victor Boivie | 841be34 | 2011-04-05 11:31:10 +0200 | [diff] [blame] | 294 | if opt.config_name or self._ShouldConfigureUser(): |
| 295 | self._ConfigureUser() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 296 | self._ConfigureColor() |
| 297 | |
Doug Anderson | 30d4529 | 2011-05-04 15:01:04 -0700 | [diff] [blame] | 298 | self._ConfigureDepth(opt) |
| 299 | |
Shawn O. Pearce | 8630f39 | 2009-03-19 10:17:12 -0700 | [diff] [blame] | 300 | if self.manifest.IsMirror: |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 301 | type = 'mirror ' |
| 302 | else: |
| 303 | type = '' |
| 304 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 305 | print '' |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 306 | print 'repo %sinitialized in %s' % (type, self.manifest.topdir) |