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 |
| 17 | import sys |
| 18 | |
| 19 | from color import Coloring |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 20 | from command import InteractiveCommand, MirrorSafeCommand |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | from error import ManifestParseError |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 22 | from project import SyncBuffer |
Shawn O. Pearce | 2ec00b9 | 2009-06-12 09:32:50 -0700 | [diff] [blame] | 23 | from git_command import git_require, MIN_GIT_VERSION |
Shawn O. Pearce | 0125ae2 | 2009-07-03 18:05:23 -0700 | [diff] [blame] | 24 | from manifest_submodule import SubmoduleManifest |
Shawn O. Pearce | 446c4e5 | 2009-05-19 18:14:04 -0700 | [diff] [blame] | 25 | from manifest_xml import XmlManifest |
Shawn O. Pearce | 87bda12 | 2009-07-03 16:37:30 -0700 | [diff] [blame] | 26 | from subcmds.sync import _ReloadManifest |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 27 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 28 | class Init(InteractiveCommand, MirrorSafeCommand): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | common = True |
| 30 | helpSummary = "Initialize repo in the current directory" |
| 31 | helpUsage = """ |
| 32 | %prog [options] |
| 33 | """ |
| 34 | helpDescription = """ |
| 35 | The '%prog' command is run once to install and initialize repo. |
| 36 | The latest repo source code and manifest collection is downloaded |
| 37 | from the server and is installed in the .repo/ directory in the |
| 38 | current working directory. |
| 39 | |
Shawn O. Pearce | 77bb4af | 2009-04-18 11:33:32 -0700 | [diff] [blame] | 40 | The optional -b argument can be used to select the manifest branch |
| 41 | to checkout and use. If no branch is specified, master is assumed. |
| 42 | |
| 43 | The optional -m argument can be used to specify an alternate manifest |
| 44 | to be used. If no manifest is specified, the manifest default.xml |
| 45 | will be used. |
| 46 | |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 47 | The --reference option can be used to point to a directory that |
| 48 | has the content of a --mirror sync. This will make the working |
| 49 | directory use as much data as possible from the local reference |
| 50 | directory when fetching from the server. This will make the sync |
| 51 | go a lot faster by reducing data traffic on the network. |
| 52 | |
| 53 | |
Shawn O. Pearce | 77bb4af | 2009-04-18 11:33:32 -0700 | [diff] [blame] | 54 | Switching Manifest Branches |
| 55 | --------------------------- |
| 56 | |
| 57 | To switch to another manifest branch, `repo init -b otherbranch` |
| 58 | may be used in an existing client. However, as this only updates the |
| 59 | manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary |
| 60 | to update the working directory files. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 61 | """ |
| 62 | |
| 63 | def _Options(self, p): |
| 64 | # Logging |
| 65 | g = p.add_option_group('Logging options') |
| 66 | g.add_option('-q', '--quiet', |
| 67 | dest="quiet", action="store_true", default=False, |
| 68 | help="be quiet") |
| 69 | |
| 70 | # Manifest |
| 71 | g = p.add_option_group('Manifest options') |
| 72 | g.add_option('-u', '--manifest-url', |
| 73 | dest='manifest_url', |
| 74 | help='manifest repository location', metavar='URL') |
| 75 | g.add_option('-b', '--manifest-branch', |
| 76 | dest='manifest_branch', |
| 77 | help='manifest branch or revision', metavar='REVISION') |
Shawn O. Pearce | 5f947bb | 2009-07-03 17:24:17 -0700 | [diff] [blame] | 78 | g.add_option('-o', '--origin', |
| 79 | dest='manifest_origin', |
| 80 | help="use REMOTE instead of 'origin' to track upstream", |
| 81 | metavar='REMOTE') |
Shawn O. Pearce | 446c4e5 | 2009-05-19 18:14:04 -0700 | [diff] [blame] | 82 | if isinstance(self.manifest, XmlManifest) \ |
| 83 | or not self.manifest.manifestProject.Exists: |
| 84 | g.add_option('-m', '--manifest-name', |
| 85 | dest='manifest_name', default='default.xml', |
| 86 | help='initial manifest file', metavar='NAME.xml') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 87 | g.add_option('--mirror', |
| 88 | dest='mirror', action='store_true', |
| 89 | help='mirror the forrest') |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 90 | g.add_option('--reference', |
| 91 | dest='reference', |
| 92 | help='location of mirror directory', metavar='DIR') |
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 | |
Shawn O. Pearce | 5f947bb | 2009-07-03 17:24:17 -0700 | [diff] [blame] | 106 | def _ApplyOptions(self, opt, is_new): |
| 107 | m = self.manifest.manifestProject |
| 108 | |
| 109 | if is_new: |
| 110 | if opt.manifest_origin: |
| 111 | m.remote.name = opt.manifest_origin |
| 112 | |
| 113 | if opt.manifest_branch: |
| 114 | m.revisionExpr = opt.manifest_branch |
| 115 | else: |
| 116 | m.revisionExpr = 'refs/heads/master' |
| 117 | else: |
| 118 | if opt.manifest_origin: |
| 119 | print >>sys.stderr, 'fatal: cannot change origin name' |
| 120 | sys.exit(1) |
| 121 | |
| 122 | if opt.manifest_branch: |
| 123 | m.revisionExpr = opt.manifest_branch |
| 124 | else: |
| 125 | m.PreSync() |
| 126 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 127 | def _SyncManifest(self, opt): |
| 128 | m = self.manifest.manifestProject |
Shawn O. Pearce | 5470df6 | 2009-03-09 18:51:58 -0700 | [diff] [blame] | 129 | is_new = not m.Exists |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 130 | |
Shawn O. Pearce | 5470df6 | 2009-03-09 18:51:58 -0700 | [diff] [blame] | 131 | if is_new: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 132 | if not opt.manifest_url: |
| 133 | print >>sys.stderr, 'fatal: manifest url (-u) is required.' |
| 134 | sys.exit(1) |
| 135 | |
| 136 | if not opt.quiet: |
| 137 | print >>sys.stderr, 'Getting manifest ...' |
| 138 | print >>sys.stderr, ' from %s' % opt.manifest_url |
| 139 | m._InitGitDir() |
| 140 | |
Shawn O. Pearce | 5f947bb | 2009-07-03 17:24:17 -0700 | [diff] [blame] | 141 | self._ApplyOptions(opt, is_new) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 142 | if opt.manifest_url: |
| 143 | r = m.GetRemote(m.remote.name) |
| 144 | r.url = opt.manifest_url |
| 145 | r.ResetFetch() |
| 146 | r.Save() |
| 147 | |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 148 | if opt.reference: |
| 149 | m.config.SetString('repo.reference', opt.reference) |
| 150 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 151 | if opt.mirror: |
Shawn O. Pearce | 5470df6 | 2009-03-09 18:51:58 -0700 | [diff] [blame] | 152 | if is_new: |
| 153 | m.config.SetString('repo.mirror', 'true') |
Shawn O. Pearce | 7354d88 | 2009-07-03 20:06:13 -0700 | [diff] [blame] | 154 | m.config.ClearCache() |
Shawn O. Pearce | 5470df6 | 2009-03-09 18:51:58 -0700 | [diff] [blame] | 155 | else: |
| 156 | print >>sys.stderr, 'fatal: --mirror not supported on existing client' |
| 157 | sys.exit(1) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 158 | |
Shawn O. Pearce | 1fc99f4 | 2009-03-17 08:06:18 -0700 | [diff] [blame] | 159 | if not m.Sync_NetworkHalf(): |
| 160 | r = m.GetRemote(m.remote.name) |
| 161 | print >>sys.stderr, 'fatal: cannot obtain manifest %s' % r.url |
| 162 | sys.exit(1) |
| 163 | |
Shawn O. Pearce | 0125ae2 | 2009-07-03 18:05:23 -0700 | [diff] [blame] | 164 | if is_new and SubmoduleManifest.IsBare(m): |
| 165 | new = self.GetManifest(reparse=True, type=SubmoduleManifest) |
| 166 | if m.gitdir != new.manifestProject.gitdir: |
| 167 | os.rename(m.gitdir, new.manifestProject.gitdir) |
| 168 | new = self.GetManifest(reparse=True, type=SubmoduleManifest) |
| 169 | m = new.manifestProject |
| 170 | self._ApplyOptions(opt, is_new) |
| 171 | |
Shawn O. Pearce | 87bda12 | 2009-07-03 16:37:30 -0700 | [diff] [blame] | 172 | if not is_new: |
| 173 | # Force the manifest to load if it exists, the old graph |
| 174 | # may be needed inside of _ReloadManifest(). |
| 175 | # |
| 176 | self.manifest.projects |
| 177 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 178 | syncbuf = SyncBuffer(m.config) |
| 179 | m.Sync_LocalHalf(syncbuf) |
| 180 | syncbuf.Finish() |
| 181 | |
Shawn O. Pearce | 13f3da5 | 2010-12-07 10:31:19 -0800 | [diff] [blame^] | 182 | if isinstance(self.manifest, XmlManifest): |
| 183 | self._LinkManifest(opt.manifest_name) |
Shawn O. Pearce | 87bda12 | 2009-07-03 16:37:30 -0700 | [diff] [blame] | 184 | _ReloadManifest(self) |
Shawn O. Pearce | 13f3da5 | 2010-12-07 10:31:19 -0800 | [diff] [blame^] | 185 | |
Shawn O. Pearce | 87bda12 | 2009-07-03 16:37:30 -0700 | [diff] [blame] | 186 | self._ApplyOptions(opt, is_new) |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 187 | |
Shawn O. Pearce | 75b87c8 | 2009-07-03 16:24:57 -0700 | [diff] [blame] | 188 | if not self.manifest.InitBranch(): |
| 189 | print >>sys.stderr, 'fatal: cannot create branch in manifest' |
| 190 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 191 | |
| 192 | def _LinkManifest(self, name): |
| 193 | if not name: |
| 194 | print >>sys.stderr, 'fatal: manifest name (-m) is required.' |
| 195 | sys.exit(1) |
| 196 | |
| 197 | try: |
| 198 | self.manifest.Link(name) |
| 199 | except ManifestParseError, e: |
| 200 | print >>sys.stderr, "fatal: manifest '%s' not available" % name |
| 201 | print >>sys.stderr, 'fatal: %s' % str(e) |
| 202 | sys.exit(1) |
| 203 | |
Shawn O. Pearce | 37dbf2b | 2009-07-02 10:53:04 -0700 | [diff] [blame] | 204 | def _Prompt(self, prompt, value): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 205 | mp = self.manifest.manifestProject |
| 206 | |
| 207 | sys.stdout.write('%-10s [%s]: ' % (prompt, value)) |
| 208 | a = sys.stdin.readline().strip() |
Shawn O. Pearce | 37dbf2b | 2009-07-02 10:53:04 -0700 | [diff] [blame] | 209 | if a == '': |
| 210 | return value |
| 211 | return a |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 212 | |
| 213 | def _ConfigureUser(self): |
| 214 | mp = self.manifest.manifestProject |
| 215 | |
Shawn O. Pearce | 37dbf2b | 2009-07-02 10:53:04 -0700 | [diff] [blame] | 216 | while True: |
| 217 | print '' |
| 218 | name = self._Prompt('Your Name', mp.UserName) |
| 219 | email = self._Prompt('Your Email', mp.UserEmail) |
| 220 | |
| 221 | print '' |
| 222 | print 'Your identity is: %s <%s>' % (name, email) |
Nico Sallembien | 6d7508b | 2010-04-01 11:03:53 -0700 | [diff] [blame] | 223 | sys.stdout.write('is this correct [y/n]? ') |
| 224 | a = sys.stdin.readline().strip() |
| 225 | if a in ('yes', 'y', 't', 'true'): |
Shawn O. Pearce | 37dbf2b | 2009-07-02 10:53:04 -0700 | [diff] [blame] | 226 | break |
| 227 | |
| 228 | if name != mp.UserName: |
| 229 | mp.config.SetString('user.name', name) |
| 230 | if email != mp.UserEmail: |
| 231 | mp.config.SetString('user.email', email) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 232 | |
| 233 | def _HasColorSet(self, gc): |
| 234 | for n in ['ui', 'diff', 'status']: |
| 235 | if gc.Has('color.%s' % n): |
| 236 | return True |
| 237 | return False |
| 238 | |
| 239 | def _ConfigureColor(self): |
| 240 | gc = self.manifest.globalConfig |
| 241 | if self._HasColorSet(gc): |
| 242 | return |
| 243 | |
| 244 | class _Test(Coloring): |
| 245 | def __init__(self): |
| 246 | Coloring.__init__(self, gc, 'test color display') |
| 247 | self._on = True |
| 248 | out = _Test() |
| 249 | |
| 250 | print '' |
| 251 | print "Testing colorized output (for 'repo diff', 'repo status'):" |
| 252 | |
| 253 | for c in ['black','red','green','yellow','blue','magenta','cyan']: |
| 254 | out.write(' ') |
| 255 | out.printer(fg=c)(' %-6s ', c) |
| 256 | out.write(' ') |
| 257 | out.printer(fg='white', bg='black')(' %s ' % 'white') |
| 258 | out.nl() |
| 259 | |
| 260 | for c in ['bold','dim','ul','reverse']: |
| 261 | out.write(' ') |
| 262 | out.printer(fg='black', attr=c)(' %-6s ', c) |
| 263 | out.nl() |
| 264 | |
| 265 | sys.stdout.write('Enable color display in this user account (y/n)? ') |
| 266 | a = sys.stdin.readline().strip().lower() |
| 267 | if a in ('y', 'yes', 't', 'true', 'on'): |
| 268 | gc.SetString('color.ui', 'auto') |
| 269 | |
| 270 | def Execute(self, opt, args): |
Shawn O. Pearce | 2ec00b9 | 2009-06-12 09:32:50 -0700 | [diff] [blame] | 271 | git_require(MIN_GIT_VERSION, fail=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 272 | self._SyncManifest(opt) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 273 | |
Shawn O. Pearce | 8630f39 | 2009-03-19 10:17:12 -0700 | [diff] [blame] | 274 | if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 275 | self._ConfigureUser() |
| 276 | self._ConfigureColor() |
| 277 | |
Shawn O. Pearce | 8630f39 | 2009-03-19 10:17:12 -0700 | [diff] [blame] | 278 | if self.manifest.IsMirror: |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 279 | type = 'mirror ' |
| 280 | else: |
| 281 | type = '' |
| 282 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 283 | print '' |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 284 | print 'repo %sinitialized in %s' % (type, self.manifest.topdir) |