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 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | from git_command import git, MIN_GIT_VERSION |
| 24 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 25 | class Init(InteractiveCommand, MirrorSafeCommand): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | common = True |
| 27 | helpSummary = "Initialize repo in the current directory" |
| 28 | helpUsage = """ |
| 29 | %prog [options] |
| 30 | """ |
| 31 | helpDescription = """ |
| 32 | The '%prog' command is run once to install and initialize repo. |
| 33 | The latest repo source code and manifest collection is downloaded |
| 34 | from the server and is installed in the .repo/ directory in the |
| 35 | current working directory. |
| 36 | |
Shawn O. Pearce | 77bb4af | 2009-04-18 11:33:32 -0700 | [diff] [blame] | 37 | The optional -b argument can be used to select the manifest branch |
| 38 | to checkout and use. If no branch is specified, master is assumed. |
| 39 | |
| 40 | The optional -m argument can be used to specify an alternate manifest |
| 41 | to be used. If no manifest is specified, the manifest default.xml |
| 42 | will be used. |
| 43 | |
| 44 | Switching Manifest Branches |
| 45 | --------------------------- |
| 46 | |
| 47 | To switch to another manifest branch, `repo init -b otherbranch` |
| 48 | may be used in an existing client. However, as this only updates the |
| 49 | manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary |
| 50 | to update the working directory files. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 51 | """ |
| 52 | |
| 53 | def _Options(self, p): |
| 54 | # Logging |
| 55 | g = p.add_option_group('Logging options') |
| 56 | g.add_option('-q', '--quiet', |
| 57 | dest="quiet", action="store_true", default=False, |
| 58 | help="be quiet") |
| 59 | |
| 60 | # Manifest |
| 61 | g = p.add_option_group('Manifest options') |
| 62 | g.add_option('-u', '--manifest-url', |
| 63 | dest='manifest_url', |
| 64 | help='manifest repository location', metavar='URL') |
| 65 | g.add_option('-b', '--manifest-branch', |
| 66 | dest='manifest_branch', |
| 67 | help='manifest branch or revision', metavar='REVISION') |
| 68 | g.add_option('-m', '--manifest-name', |
| 69 | dest='manifest_name', default='default.xml', |
| 70 | help='initial manifest file', metavar='NAME.xml') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 71 | g.add_option('--mirror', |
| 72 | dest='mirror', action='store_true', |
| 73 | help='mirror the forrest') |
| 74 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 75 | |
| 76 | # Tool |
Shawn O. Pearce | fd89b67 | 2009-04-18 11:28:57 -0700 | [diff] [blame] | 77 | g = p.add_option_group('repo Version options') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 78 | g.add_option('--repo-url', |
| 79 | dest='repo_url', |
| 80 | help='repo repository location', metavar='URL') |
| 81 | g.add_option('--repo-branch', |
| 82 | dest='repo_branch', |
| 83 | help='repo branch or revision', metavar='REVISION') |
| 84 | g.add_option('--no-repo-verify', |
| 85 | dest='no_repo_verify', action='store_true', |
| 86 | help='do not verify repo source code') |
| 87 | |
| 88 | def _CheckGitVersion(self): |
| 89 | ver_str = git.version() |
| 90 | if not ver_str.startswith('git version '): |
| 91 | print >>sys.stderr, 'error: "%s" unsupported' % ver_str |
| 92 | sys.exit(1) |
| 93 | |
| 94 | ver_str = ver_str[len('git version '):].strip() |
| 95 | ver_act = tuple(map(lambda x: int(x), ver_str.split('.')[0:3])) |
| 96 | if ver_act < MIN_GIT_VERSION: |
| 97 | need = '.'.join(map(lambda x: str(x), MIN_GIT_VERSION)) |
| 98 | print >>sys.stderr, 'fatal: git %s or later required' % need |
| 99 | sys.exit(1) |
| 100 | |
| 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 | 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 |
| 141 | sys.exit(1) |
| 142 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 143 | syncbuf = SyncBuffer(m.config) |
| 144 | m.Sync_LocalHalf(syncbuf) |
| 145 | syncbuf.Finish() |
| 146 | |
Shawn O. Pearce | df01883 | 2009-03-17 08:15:27 -0700 | [diff] [blame] | 147 | if is_new or m.CurrentBranch is None: |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 148 | if not m.StartBranch('default'): |
| 149 | print >>sys.stderr, 'fatal: cannot create default in manifest' |
| 150 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 151 | |
| 152 | def _LinkManifest(self, name): |
| 153 | if not name: |
| 154 | print >>sys.stderr, 'fatal: manifest name (-m) is required.' |
| 155 | sys.exit(1) |
| 156 | |
| 157 | try: |
| 158 | self.manifest.Link(name) |
| 159 | except ManifestParseError, e: |
| 160 | print >>sys.stderr, "fatal: manifest '%s' not available" % name |
| 161 | print >>sys.stderr, 'fatal: %s' % str(e) |
| 162 | sys.exit(1) |
| 163 | |
| 164 | def _PromptKey(self, prompt, key, value): |
| 165 | mp = self.manifest.manifestProject |
| 166 | |
| 167 | sys.stdout.write('%-10s [%s]: ' % (prompt, value)) |
| 168 | a = sys.stdin.readline().strip() |
| 169 | if a != '' and a != value: |
| 170 | mp.config.SetString(key, a) |
| 171 | |
| 172 | def _ConfigureUser(self): |
| 173 | mp = self.manifest.manifestProject |
| 174 | |
| 175 | print '' |
| 176 | self._PromptKey('Your Name', 'user.name', mp.UserName) |
| 177 | self._PromptKey('Your Email', 'user.email', mp.UserEmail) |
| 178 | |
| 179 | def _HasColorSet(self, gc): |
| 180 | for n in ['ui', 'diff', 'status']: |
| 181 | if gc.Has('color.%s' % n): |
| 182 | return True |
| 183 | return False |
| 184 | |
| 185 | def _ConfigureColor(self): |
| 186 | gc = self.manifest.globalConfig |
| 187 | if self._HasColorSet(gc): |
| 188 | return |
| 189 | |
| 190 | class _Test(Coloring): |
| 191 | def __init__(self): |
| 192 | Coloring.__init__(self, gc, 'test color display') |
| 193 | self._on = True |
| 194 | out = _Test() |
| 195 | |
| 196 | print '' |
| 197 | print "Testing colorized output (for 'repo diff', 'repo status'):" |
| 198 | |
| 199 | for c in ['black','red','green','yellow','blue','magenta','cyan']: |
| 200 | out.write(' ') |
| 201 | out.printer(fg=c)(' %-6s ', c) |
| 202 | out.write(' ') |
| 203 | out.printer(fg='white', bg='black')(' %s ' % 'white') |
| 204 | out.nl() |
| 205 | |
| 206 | for c in ['bold','dim','ul','reverse']: |
| 207 | out.write(' ') |
| 208 | out.printer(fg='black', attr=c)(' %-6s ', c) |
| 209 | out.nl() |
| 210 | |
| 211 | sys.stdout.write('Enable color display in this user account (y/n)? ') |
| 212 | a = sys.stdin.readline().strip().lower() |
| 213 | if a in ('y', 'yes', 't', 'true', 'on'): |
| 214 | gc.SetString('color.ui', 'auto') |
| 215 | |
| 216 | def Execute(self, opt, args): |
| 217 | self._CheckGitVersion() |
| 218 | self._SyncManifest(opt) |
| 219 | self._LinkManifest(opt.manifest_name) |
| 220 | |
Shawn O. Pearce | 8630f39 | 2009-03-19 10:17:12 -0700 | [diff] [blame] | 221 | 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] | 222 | self._ConfigureUser() |
| 223 | self._ConfigureColor() |
| 224 | |
Shawn O. Pearce | 8630f39 | 2009-03-19 10:17:12 -0700 | [diff] [blame] | 225 | if self.manifest.IsMirror: |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 226 | type = 'mirror ' |
| 227 | else: |
| 228 | type = '' |
| 229 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 230 | print '' |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 231 | print 'repo %sinitialized in %s' % (type, self.manifest.topdir) |