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 |
| 22 | from remote import Remote |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 23 | from project import SyncBuffer |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 24 | from git_command import git, MIN_GIT_VERSION |
| 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 | |
| 45 | Switching Manifest Branches |
| 46 | --------------------------- |
| 47 | |
| 48 | To switch to another manifest branch, `repo init -b otherbranch` |
| 49 | may be used in an existing client. However, as this only updates the |
| 50 | manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary |
| 51 | to update the working directory files. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 52 | """ |
| 53 | |
| 54 | def _Options(self, p): |
| 55 | # Logging |
| 56 | g = p.add_option_group('Logging options') |
| 57 | g.add_option('-q', '--quiet', |
| 58 | dest="quiet", action="store_true", default=False, |
| 59 | help="be quiet") |
| 60 | |
| 61 | # Manifest |
| 62 | g = p.add_option_group('Manifest options') |
| 63 | g.add_option('-u', '--manifest-url', |
| 64 | dest='manifest_url', |
| 65 | help='manifest repository location', metavar='URL') |
| 66 | g.add_option('-b', '--manifest-branch', |
| 67 | dest='manifest_branch', |
| 68 | help='manifest branch or revision', metavar='REVISION') |
| 69 | g.add_option('-m', '--manifest-name', |
| 70 | dest='manifest_name', default='default.xml', |
| 71 | help='initial manifest file', metavar='NAME.xml') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 72 | g.add_option('--mirror', |
| 73 | dest='mirror', action='store_true', |
| 74 | help='mirror the forrest') |
| 75 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 76 | |
| 77 | # Tool |
Shawn O. Pearce | fd89b67 | 2009-04-18 11:28:57 -0700 | [diff] [blame] | 78 | g = p.add_option_group('repo Version options') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 79 | g.add_option('--repo-url', |
| 80 | dest='repo_url', |
| 81 | help='repo repository location', metavar='URL') |
| 82 | g.add_option('--repo-branch', |
| 83 | dest='repo_branch', |
| 84 | help='repo branch or revision', metavar='REVISION') |
| 85 | g.add_option('--no-repo-verify', |
| 86 | dest='no_repo_verify', action='store_true', |
| 87 | help='do not verify repo source code') |
| 88 | |
| 89 | def _CheckGitVersion(self): |
| 90 | ver_str = git.version() |
| 91 | if not ver_str.startswith('git version '): |
| 92 | print >>sys.stderr, 'error: "%s" unsupported' % ver_str |
| 93 | sys.exit(1) |
| 94 | |
| 95 | ver_str = ver_str[len('git version '):].strip() |
| 96 | ver_act = tuple(map(lambda x: int(x), ver_str.split('.')[0:3])) |
| 97 | if ver_act < MIN_GIT_VERSION: |
| 98 | need = '.'.join(map(lambda x: str(x), MIN_GIT_VERSION)) |
| 99 | print >>sys.stderr, 'fatal: git %s or later required' % need |
| 100 | sys.exit(1) |
| 101 | |
| 102 | def _SyncManifest(self, opt): |
| 103 | m = self.manifest.manifestProject |
Shawn O. Pearce | 5470df6 | 2009-03-09 18:51:58 -0700 | [diff] [blame] | 104 | is_new = not m.Exists |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 105 | |
Shawn O. Pearce | 5470df6 | 2009-03-09 18:51:58 -0700 | [diff] [blame] | 106 | if is_new: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 107 | if not opt.manifest_url: |
| 108 | print >>sys.stderr, 'fatal: manifest url (-u) is required.' |
| 109 | sys.exit(1) |
| 110 | |
| 111 | if not opt.quiet: |
| 112 | print >>sys.stderr, 'Getting manifest ...' |
| 113 | print >>sys.stderr, ' from %s' % opt.manifest_url |
| 114 | m._InitGitDir() |
| 115 | |
| 116 | if opt.manifest_branch: |
| 117 | m.revision = opt.manifest_branch |
| 118 | else: |
| 119 | m.revision = 'refs/heads/master' |
| 120 | else: |
| 121 | if opt.manifest_branch: |
| 122 | m.revision = opt.manifest_branch |
| 123 | else: |
| 124 | m.PreSync() |
| 125 | |
| 126 | if opt.manifest_url: |
| 127 | r = m.GetRemote(m.remote.name) |
| 128 | r.url = opt.manifest_url |
| 129 | r.ResetFetch() |
| 130 | r.Save() |
| 131 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 132 | if opt.mirror: |
Shawn O. Pearce | 5470df6 | 2009-03-09 18:51:58 -0700 | [diff] [blame] | 133 | if is_new: |
| 134 | m.config.SetString('repo.mirror', 'true') |
| 135 | else: |
| 136 | print >>sys.stderr, 'fatal: --mirror not supported on existing client' |
| 137 | sys.exit(1) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 138 | |
Shawn O. Pearce | 1fc99f4 | 2009-03-17 08:06:18 -0700 | [diff] [blame] | 139 | if not m.Sync_NetworkHalf(): |
| 140 | r = m.GetRemote(m.remote.name) |
| 141 | print >>sys.stderr, 'fatal: cannot obtain manifest %s' % r.url |
| 142 | sys.exit(1) |
| 143 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 144 | syncbuf = SyncBuffer(m.config) |
| 145 | m.Sync_LocalHalf(syncbuf) |
| 146 | syncbuf.Finish() |
| 147 | |
Shawn O. Pearce | df01883 | 2009-03-17 08:15:27 -0700 | [diff] [blame] | 148 | if is_new or m.CurrentBranch is None: |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 149 | if not m.StartBranch('default'): |
| 150 | print >>sys.stderr, 'fatal: cannot create default in manifest' |
| 151 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 152 | |
| 153 | def _LinkManifest(self, name): |
| 154 | if not name: |
| 155 | print >>sys.stderr, 'fatal: manifest name (-m) is required.' |
| 156 | sys.exit(1) |
| 157 | |
| 158 | try: |
| 159 | self.manifest.Link(name) |
| 160 | except ManifestParseError, e: |
| 161 | print >>sys.stderr, "fatal: manifest '%s' not available" % name |
| 162 | print >>sys.stderr, 'fatal: %s' % str(e) |
| 163 | sys.exit(1) |
| 164 | |
| 165 | def _PromptKey(self, prompt, key, value): |
| 166 | mp = self.manifest.manifestProject |
| 167 | |
| 168 | sys.stdout.write('%-10s [%s]: ' % (prompt, value)) |
| 169 | a = sys.stdin.readline().strip() |
| 170 | if a != '' and a != value: |
| 171 | mp.config.SetString(key, a) |
| 172 | |
| 173 | def _ConfigureUser(self): |
| 174 | mp = self.manifest.manifestProject |
| 175 | |
| 176 | print '' |
| 177 | self._PromptKey('Your Name', 'user.name', mp.UserName) |
| 178 | self._PromptKey('Your Email', 'user.email', mp.UserEmail) |
| 179 | |
| 180 | def _HasColorSet(self, gc): |
| 181 | for n in ['ui', 'diff', 'status']: |
| 182 | if gc.Has('color.%s' % n): |
| 183 | return True |
| 184 | return False |
| 185 | |
| 186 | def _ConfigureColor(self): |
| 187 | gc = self.manifest.globalConfig |
| 188 | if self._HasColorSet(gc): |
| 189 | return |
| 190 | |
| 191 | class _Test(Coloring): |
| 192 | def __init__(self): |
| 193 | Coloring.__init__(self, gc, 'test color display') |
| 194 | self._on = True |
| 195 | out = _Test() |
| 196 | |
| 197 | print '' |
| 198 | print "Testing colorized output (for 'repo diff', 'repo status'):" |
| 199 | |
| 200 | for c in ['black','red','green','yellow','blue','magenta','cyan']: |
| 201 | out.write(' ') |
| 202 | out.printer(fg=c)(' %-6s ', c) |
| 203 | out.write(' ') |
| 204 | out.printer(fg='white', bg='black')(' %s ' % 'white') |
| 205 | out.nl() |
| 206 | |
| 207 | for c in ['bold','dim','ul','reverse']: |
| 208 | out.write(' ') |
| 209 | out.printer(fg='black', attr=c)(' %-6s ', c) |
| 210 | out.nl() |
| 211 | |
| 212 | sys.stdout.write('Enable color display in this user account (y/n)? ') |
| 213 | a = sys.stdin.readline().strip().lower() |
| 214 | if a in ('y', 'yes', 't', 'true', 'on'): |
| 215 | gc.SetString('color.ui', 'auto') |
| 216 | |
| 217 | def Execute(self, opt, args): |
| 218 | self._CheckGitVersion() |
| 219 | self._SyncManifest(opt) |
| 220 | self._LinkManifest(opt.manifest_name) |
| 221 | |
Shawn O. Pearce | 8630f39 | 2009-03-19 10:17:12 -0700 | [diff] [blame] | 222 | 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] | 223 | self._ConfigureUser() |
| 224 | self._ConfigureColor() |
| 225 | |
Shawn O. Pearce | 8630f39 | 2009-03-19 10:17:12 -0700 | [diff] [blame] | 226 | if self.manifest.IsMirror: |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 227 | type = 'mirror ' |
| 228 | else: |
| 229 | type = '' |
| 230 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 231 | print '' |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 232 | print 'repo %sinitialized in %s' % (type, self.manifest.topdir) |