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