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') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 85 | |
| 86 | # Tool |
Shawn O. Pearce | fd89b67 | 2009-04-18 11:28:57 -0700 | [diff] [blame] | 87 | g = p.add_option_group('repo Version options') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 88 | g.add_option('--repo-url', |
| 89 | dest='repo_url', |
| 90 | help='repo repository location', metavar='URL') |
| 91 | g.add_option('--repo-branch', |
| 92 | dest='repo_branch', |
| 93 | help='repo branch or revision', metavar='REVISION') |
| 94 | g.add_option('--no-repo-verify', |
| 95 | dest='no_repo_verify', action='store_true', |
| 96 | help='do not verify repo source code') |
| 97 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 98 | def _SyncManifest(self, opt): |
| 99 | m = self.manifest.manifestProject |
Shawn O. Pearce | 5470df6 | 2009-03-09 18:51:58 -0700 | [diff] [blame] | 100 | is_new = not m.Exists |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 101 | |
Shawn O. Pearce | 5470df6 | 2009-03-09 18:51:58 -0700 | [diff] [blame] | 102 | if is_new: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 103 | if not opt.manifest_url: |
| 104 | print >>sys.stderr, 'fatal: manifest url (-u) is required.' |
| 105 | sys.exit(1) |
| 106 | |
| 107 | if not opt.quiet: |
| 108 | print >>sys.stderr, 'Getting manifest ...' |
| 109 | print >>sys.stderr, ' from %s' % opt.manifest_url |
| 110 | m._InitGitDir() |
| 111 | |
| 112 | if opt.manifest_branch: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 113 | m.revisionExpr = opt.manifest_branch |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 114 | else: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 115 | m.revisionExpr = 'refs/heads/master' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 116 | else: |
| 117 | if opt.manifest_branch: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 118 | m.revisionExpr = opt.manifest_branch |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 119 | else: |
| 120 | m.PreSync() |
| 121 | |
| 122 | if opt.manifest_url: |
| 123 | r = m.GetRemote(m.remote.name) |
| 124 | r.url = opt.manifest_url |
| 125 | r.ResetFetch() |
| 126 | r.Save() |
| 127 | |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 128 | if opt.reference: |
| 129 | m.config.SetString('repo.reference', opt.reference) |
| 130 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 131 | if opt.mirror: |
Shawn O. Pearce | 5470df6 | 2009-03-09 18:51:58 -0700 | [diff] [blame] | 132 | if is_new: |
| 133 | m.config.SetString('repo.mirror', 'true') |
| 134 | else: |
| 135 | print >>sys.stderr, 'fatal: --mirror not supported on existing client' |
| 136 | sys.exit(1) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 137 | |
Shawn O. Pearce | 1fc99f4 | 2009-03-17 08:06:18 -0700 | [diff] [blame] | 138 | if not m.Sync_NetworkHalf(): |
| 139 | r = m.GetRemote(m.remote.name) |
| 140 | print >>sys.stderr, 'fatal: cannot obtain manifest %s' % r.url |
Doug Anderson | 2630dd9 | 2011-04-07 13:36:30 -0700 | [diff] [blame] | 141 | |
| 142 | # Better delete the manifest git dir if we created it; otherwise next |
| 143 | # time (when user fixes problems) we won't go through the "is_new" logic. |
| 144 | if is_new: |
| 145 | shutil.rmtree(m.gitdir) |
Shawn O. Pearce | 1fc99f4 | 2009-03-17 08:06:18 -0700 | [diff] [blame] | 146 | sys.exit(1) |
| 147 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 148 | syncbuf = SyncBuffer(m.config) |
| 149 | m.Sync_LocalHalf(syncbuf) |
| 150 | syncbuf.Finish() |
| 151 | |
Shawn O. Pearce | df01883 | 2009-03-17 08:15:27 -0700 | [diff] [blame] | 152 | if is_new or m.CurrentBranch is None: |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 153 | if not m.StartBranch('default'): |
| 154 | print >>sys.stderr, 'fatal: cannot create default in manifest' |
| 155 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 156 | |
| 157 | def _LinkManifest(self, name): |
| 158 | if not name: |
| 159 | print >>sys.stderr, 'fatal: manifest name (-m) is required.' |
| 160 | sys.exit(1) |
| 161 | |
| 162 | try: |
| 163 | self.manifest.Link(name) |
| 164 | except ManifestParseError, e: |
| 165 | print >>sys.stderr, "fatal: manifest '%s' not available" % name |
| 166 | print >>sys.stderr, 'fatal: %s' % str(e) |
| 167 | sys.exit(1) |
| 168 | |
Shawn O. Pearce | 37dbf2b | 2009-07-02 10:53:04 -0700 | [diff] [blame] | 169 | def _Prompt(self, prompt, value): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 170 | mp = self.manifest.manifestProject |
| 171 | |
| 172 | sys.stdout.write('%-10s [%s]: ' % (prompt, value)) |
| 173 | a = sys.stdin.readline().strip() |
Shawn O. Pearce | 37dbf2b | 2009-07-02 10:53:04 -0700 | [diff] [blame] | 174 | if a == '': |
| 175 | return value |
| 176 | return a |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 177 | |
| 178 | def _ConfigureUser(self): |
| 179 | mp = self.manifest.manifestProject |
| 180 | |
Shawn O. Pearce | 37dbf2b | 2009-07-02 10:53:04 -0700 | [diff] [blame] | 181 | while True: |
| 182 | print '' |
| 183 | name = self._Prompt('Your Name', mp.UserName) |
| 184 | email = self._Prompt('Your Email', mp.UserEmail) |
| 185 | |
| 186 | print '' |
| 187 | print 'Your identity is: %s <%s>' % (name, email) |
Nico Sallembien | 6d7508b | 2010-04-01 11:03:53 -0700 | [diff] [blame] | 188 | sys.stdout.write('is this correct [y/n]? ') |
| 189 | a = sys.stdin.readline().strip() |
| 190 | if a in ('yes', 'y', 't', 'true'): |
Shawn O. Pearce | 37dbf2b | 2009-07-02 10:53:04 -0700 | [diff] [blame] | 191 | break |
| 192 | |
| 193 | if name != mp.UserName: |
| 194 | mp.config.SetString('user.name', name) |
| 195 | if email != mp.UserEmail: |
| 196 | mp.config.SetString('user.email', email) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 197 | |
| 198 | def _HasColorSet(self, gc): |
| 199 | for n in ['ui', 'diff', 'status']: |
| 200 | if gc.Has('color.%s' % n): |
| 201 | return True |
| 202 | return False |
| 203 | |
| 204 | def _ConfigureColor(self): |
| 205 | gc = self.manifest.globalConfig |
| 206 | if self._HasColorSet(gc): |
| 207 | return |
| 208 | |
| 209 | class _Test(Coloring): |
| 210 | def __init__(self): |
| 211 | Coloring.__init__(self, gc, 'test color display') |
| 212 | self._on = True |
| 213 | out = _Test() |
| 214 | |
| 215 | print '' |
| 216 | print "Testing colorized output (for 'repo diff', 'repo status'):" |
| 217 | |
| 218 | for c in ['black','red','green','yellow','blue','magenta','cyan']: |
| 219 | out.write(' ') |
| 220 | out.printer(fg=c)(' %-6s ', c) |
| 221 | out.write(' ') |
| 222 | out.printer(fg='white', bg='black')(' %s ' % 'white') |
| 223 | out.nl() |
| 224 | |
| 225 | for c in ['bold','dim','ul','reverse']: |
| 226 | out.write(' ') |
| 227 | out.printer(fg='black', attr=c)(' %-6s ', c) |
| 228 | out.nl() |
| 229 | |
| 230 | sys.stdout.write('Enable color display in this user account (y/n)? ') |
| 231 | a = sys.stdin.readline().strip().lower() |
| 232 | if a in ('y', 'yes', 't', 'true', 'on'): |
| 233 | gc.SetString('color.ui', 'auto') |
| 234 | |
| 235 | def Execute(self, opt, args): |
Shawn O. Pearce | 2ec00b9 | 2009-06-12 09:32:50 -0700 | [diff] [blame] | 236 | git_require(MIN_GIT_VERSION, fail=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 237 | self._SyncManifest(opt) |
| 238 | self._LinkManifest(opt.manifest_name) |
| 239 | |
Shawn O. Pearce | 8630f39 | 2009-03-19 10:17:12 -0700 | [diff] [blame] | 240 | 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] | 241 | self._ConfigureUser() |
| 242 | self._ConfigureColor() |
| 243 | |
Shawn O. Pearce | 8630f39 | 2009-03-19 10:17:12 -0700 | [diff] [blame] | 244 | if self.manifest.IsMirror: |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 245 | type = 'mirror ' |
| 246 | else: |
| 247 | type = '' |
| 248 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 249 | print '' |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 250 | print 'repo %sinitialized in %s' % (type, self.manifest.topdir) |