The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | ## repo default configuration |
| 4 | ## |
Shawn O. Pearce | 24d8dfb | 2008-12-18 07:21:32 -0800 | [diff] [blame] | 5 | REPO_URL='git://android.git.kernel.org/tools/repo.git' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 6 | REPO_REV='stable' |
| 7 | |
| 8 | # Copyright (C) 2008 Google Inc. |
| 9 | # |
| 10 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 | # you may not use this file except in compliance with the License. |
| 12 | # You may obtain a copy of the License at |
| 13 | # |
| 14 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | # |
| 16 | # Unless required by applicable law or agreed to in writing, software |
| 17 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | # See the License for the specific language governing permissions and |
| 20 | # limitations under the License. |
| 21 | |
| 22 | magic='--calling-python-from-/bin/sh--' |
Shawn O. Pearce | 7542d66 | 2008-10-21 07:11:36 -0700 | [diff] [blame] | 23 | """exec" python -E "$0" "$@" """#$magic" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 24 | if __name__ == '__main__': |
| 25 | import sys |
| 26 | if sys.argv[-1] == '#%s' % magic: |
| 27 | del sys.argv[-1] |
| 28 | del magic |
| 29 | |
| 30 | # increment this whenever we make important changes to this script |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 31 | VERSION = (1, 9) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 32 | |
| 33 | # increment this if the MAINTAINER_KEYS block is modified |
| 34 | KEYRING_VERSION = (1,0) |
| 35 | MAINTAINER_KEYS = """ |
| 36 | |
| 37 | Repo Maintainer <repo@android.kernel.org> |
| 38 |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3v5ILwfC7hVx4jHSnOgZ65L9s8EQdVr1ckN9243yta7rNgwfcqb60ILMFF1BRk/ |
| 71 | 0V7wCL+68UwwiQDvyMOQuqkysKLSDCLb7BFcyA7j6KG+5hpsREstFX2wK1yKeraz |
| 72 | 5xGrFy8tfAaeBMIQ17gvFSp/suc9DYO0ICK2BISzq+F+ZiAKsjMYOBNdH/h0zobQ |
| 73 | HTHs37+/QLMomGEGKZMWi0dShU2J5mNRQu3Hhxl3hHDVbt5CeJBb26aQcQrFz69W |
| 74 | zE3GNvmJosh6leayjtI9P2A6iEkEGBECAAkFAkj3uiACGwwACgkQFlMNXpIPXGWp |
| 75 | TACbBS+Up3RpfYVfd63c1cDdlru13pQAn3NQy/SN858MkxN+zym86UBgOad2 |
| 76 | =CMiZ |
| 77 | -----END PGP PUBLIC KEY BLOCK----- |
| 78 | """ |
| 79 | |
| 80 | GIT = 'git' # our git command |
| 81 | MIN_GIT_VERSION = (1, 5, 4) # minimum supported git version |
| 82 | repodir = '.repo' # name of repo's private directory |
| 83 | S_repo = 'repo' # special repo reposiory |
| 84 | S_manifests = 'manifests' # special manifest repository |
| 85 | REPO_MAIN = S_repo + '/main.py' # main script |
| 86 | |
| 87 | |
| 88 | import optparse |
| 89 | import os |
| 90 | import re |
| 91 | import readline |
| 92 | import subprocess |
| 93 | import sys |
| 94 | |
| 95 | home_dot_repo = os.path.expanduser('~/.repoconfig') |
| 96 | gpg_dir = os.path.join(home_dot_repo, 'gnupg') |
| 97 | |
| 98 | extra_args = [] |
| 99 | init_optparse = optparse.OptionParser(usage="repo init -u url [options]") |
| 100 | |
| 101 | # Logging |
| 102 | group = init_optparse.add_option_group('Logging options') |
| 103 | group.add_option('-q', '--quiet', |
| 104 | dest="quiet", action="store_true", default=False, |
| 105 | help="be quiet") |
| 106 | |
| 107 | # Manifest |
| 108 | group = init_optparse.add_option_group('Manifest options') |
| 109 | group.add_option('-u', '--manifest-url', |
| 110 | dest='manifest_url', |
| 111 | help='manifest repository location', metavar='URL') |
| 112 | group.add_option('-b', '--manifest-branch', |
| 113 | dest='manifest_branch', |
| 114 | help='manifest branch or revision', metavar='REVISION') |
| 115 | group.add_option('-m', '--manifest-name', |
| 116 | dest='manifest_name', |
| 117 | help='initial manifest file', metavar='NAME.xml') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 118 | group.add_option('--mirror', |
| 119 | dest='mirror', action='store_true', |
| 120 | help='mirror the forrest') |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 121 | group.add_option('--reference', |
| 122 | dest='reference', |
| 123 | help='location of mirror directory', metavar='DIR') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 124 | |
| 125 | # Tool |
Shawn O. Pearce | fd89b67 | 2009-04-18 11:28:57 -0700 | [diff] [blame] | 126 | group = init_optparse.add_option_group('repo Version options') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 127 | group.add_option('--repo-url', |
| 128 | dest='repo_url', |
| 129 | help='repo repository location', metavar='URL') |
| 130 | group.add_option('--repo-branch', |
| 131 | dest='repo_branch', |
| 132 | help='repo branch or revision', metavar='REVISION') |
| 133 | group.add_option('--no-repo-verify', |
| 134 | dest='no_repo_verify', action='store_true', |
| 135 | help='do not verify repo source code') |
| 136 | |
| 137 | |
| 138 | class CloneFailure(Exception): |
| 139 | """Indicate the remote clone of repo itself failed. |
| 140 | """ |
| 141 | |
| 142 | |
| 143 | def _Init(args): |
| 144 | """Installs repo by cloning it over the network. |
| 145 | """ |
| 146 | opt, args = init_optparse.parse_args(args) |
| 147 | if args or not opt.manifest_url: |
| 148 | init_optparse.print_usage() |
| 149 | sys.exit(1) |
| 150 | |
| 151 | url = opt.repo_url |
| 152 | if not url: |
| 153 | url = REPO_URL |
| 154 | extra_args.append('--repo-url=%s' % url) |
| 155 | |
| 156 | branch = opt.repo_branch |
| 157 | if not branch: |
| 158 | branch = REPO_REV |
| 159 | extra_args.append('--repo-branch=%s' % branch) |
| 160 | |
| 161 | if branch.startswith('refs/heads/'): |
| 162 | branch = branch[len('refs/heads/'):] |
| 163 | if branch.startswith('refs/'): |
| 164 | print >>sys.stderr, "fatal: invalid branch name '%s'" % branch |
| 165 | raise CloneFailure() |
| 166 | |
| 167 | if not os.path.isdir(repodir): |
| 168 | try: |
| 169 | os.mkdir(repodir) |
| 170 | except OSError, e: |
| 171 | print >>sys.stderr, \ |
| 172 | 'fatal: cannot make %s directory: %s' % ( |
| 173 | repodir, e.strerror) |
| 174 | # Don't faise CloneFailure; that would delete the |
| 175 | # name. Instead exit immediately. |
| 176 | # |
| 177 | sys.exit(1) |
| 178 | |
| 179 | _CheckGitVersion() |
| 180 | try: |
| 181 | if _NeedSetupGnuPG(): |
| 182 | can_verify = _SetupGnuPG(opt.quiet) |
| 183 | else: |
| 184 | can_verify = True |
| 185 | |
| 186 | if not opt.quiet: |
| 187 | print >>sys.stderr, 'Getting repo ...' |
| 188 | print >>sys.stderr, ' from %s' % url |
| 189 | |
| 190 | dst = os.path.abspath(os.path.join(repodir, S_repo)) |
| 191 | _Clone(url, dst, opt.quiet) |
| 192 | |
| 193 | if can_verify and not opt.no_repo_verify: |
| 194 | rev = _Verify(dst, branch, opt.quiet) |
| 195 | else: |
| 196 | rev = 'refs/remotes/origin/%s^0' % branch |
| 197 | |
| 198 | _Checkout(dst, branch, rev, opt.quiet) |
| 199 | except CloneFailure: |
| 200 | if opt.quiet: |
| 201 | print >>sys.stderr, \ |
| 202 | 'fatal: repo init failed; run without --quiet to see why' |
| 203 | raise |
| 204 | |
| 205 | |
| 206 | def _CheckGitVersion(): |
| 207 | cmd = [GIT, '--version'] |
| 208 | proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
| 209 | ver_str = proc.stdout.read().strip() |
| 210 | proc.stdout.close() |
Shawn O. Pearce | 1619134 | 2008-10-28 08:33:34 -0700 | [diff] [blame] | 211 | proc.wait() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 212 | |
| 213 | if not ver_str.startswith('git version '): |
| 214 | print >>sys.stderr, 'error: "%s" unsupported' % ver_str |
| 215 | raise CloneFailure() |
| 216 | |
| 217 | ver_str = ver_str[len('git version '):].strip() |
| 218 | ver_act = tuple(map(lambda x: int(x), ver_str.split('.')[0:3])) |
| 219 | if ver_act < MIN_GIT_VERSION: |
| 220 | need = '.'.join(map(lambda x: str(x), MIN_GIT_VERSION)) |
| 221 | print >>sys.stderr, 'fatal: git %s or later required' % need |
| 222 | raise CloneFailure() |
| 223 | |
| 224 | |
| 225 | def _NeedSetupGnuPG(): |
| 226 | if not os.path.isdir(home_dot_repo): |
| 227 | return True |
| 228 | |
| 229 | kv = os.path.join(home_dot_repo, 'keyring-version') |
| 230 | if not os.path.exists(kv): |
| 231 | return True |
| 232 | |
| 233 | kv = open(kv).read() |
| 234 | if not kv: |
| 235 | return True |
| 236 | |
| 237 | kv = tuple(map(lambda x: int(x), kv.split('.'))) |
| 238 | if kv < KEYRING_VERSION: |
| 239 | return True |
| 240 | return False |
| 241 | |
| 242 | |
| 243 | def _SetupGnuPG(quiet): |
| 244 | if not os.path.isdir(home_dot_repo): |
| 245 | try: |
| 246 | os.mkdir(home_dot_repo) |
| 247 | except OSError, e: |
| 248 | print >>sys.stderr, \ |
| 249 | 'fatal: cannot make %s directory: %s' % ( |
| 250 | home_dot_repo, e.strerror) |
| 251 | sys.exit(1) |
| 252 | |
| 253 | if not os.path.isdir(gpg_dir): |
| 254 | try: |
| 255 | os.mkdir(gpg_dir, 0700) |
| 256 | except OSError, e: |
| 257 | print >>sys.stderr, \ |
| 258 | 'fatal: cannot make %s directory: %s' % ( |
| 259 | gpg_dir, e.strerror) |
| 260 | sys.exit(1) |
| 261 | |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 262 | env = os.environ.copy() |
| 263 | env['GNUPGHOME'] = gpg_dir.encode() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 264 | |
| 265 | cmd = ['gpg', '--import'] |
| 266 | try: |
| 267 | proc = subprocess.Popen(cmd, |
| 268 | env = env, |
| 269 | stdin = subprocess.PIPE) |
| 270 | except OSError, e: |
| 271 | if not quiet: |
| 272 | print >>sys.stderr, 'warning: gpg (GnuPG) is not available.' |
| 273 | print >>sys.stderr, 'warning: Installing it is strongly encouraged.' |
| 274 | print >>sys.stderr |
| 275 | return False |
| 276 | |
| 277 | proc.stdin.write(MAINTAINER_KEYS) |
| 278 | proc.stdin.close() |
| 279 | |
| 280 | if proc.wait() != 0: |
| 281 | print >>sys.stderr, 'fatal: registering repo maintainer keys failed' |
| 282 | sys.exit(1) |
| 283 | print |
| 284 | |
| 285 | fd = open(os.path.join(home_dot_repo, 'keyring-version'), 'w') |
| 286 | fd.write('.'.join(map(lambda x: str(x), KEYRING_VERSION)) + '\n') |
| 287 | fd.close() |
| 288 | return True |
| 289 | |
| 290 | |
| 291 | def _SetConfig(local, name, value): |
| 292 | """Set a git configuration option to the specified value. |
| 293 | """ |
| 294 | cmd = [GIT, 'config', name, value] |
| 295 | if subprocess.Popen(cmd, cwd = local).wait() != 0: |
| 296 | raise CloneFailure() |
| 297 | |
| 298 | |
| 299 | def _Fetch(local, quiet, *args): |
| 300 | cmd = [GIT, 'fetch'] |
| 301 | if quiet: |
| 302 | cmd.append('--quiet') |
| 303 | err = subprocess.PIPE |
| 304 | else: |
| 305 | err = None |
| 306 | cmd.extend(args) |
| 307 | cmd.append('origin') |
| 308 | |
| 309 | proc = subprocess.Popen(cmd, cwd = local, stderr = err) |
| 310 | if err: |
| 311 | proc.stderr.read() |
| 312 | proc.stderr.close() |
| 313 | if proc.wait() != 0: |
| 314 | raise CloneFailure() |
| 315 | |
| 316 | |
| 317 | def _Clone(url, local, quiet): |
| 318 | """Clones a git repository to a new subdirectory of repodir |
| 319 | """ |
| 320 | try: |
| 321 | os.mkdir(local) |
| 322 | except OSError, e: |
| 323 | print >>sys.stderr, \ |
| 324 | 'fatal: cannot make %s directory: %s' \ |
| 325 | % (local, e.strerror) |
| 326 | raise CloneFailure() |
| 327 | |
| 328 | cmd = [GIT, 'init', '--quiet'] |
| 329 | try: |
| 330 | proc = subprocess.Popen(cmd, cwd = local) |
| 331 | except OSError, e: |
| 332 | print >>sys.stderr |
| 333 | print >>sys.stderr, "fatal: '%s' is not available" % GIT |
| 334 | print >>sys.stderr, 'fatal: %s' % e |
| 335 | print >>sys.stderr |
| 336 | print >>sys.stderr, 'Please make sure %s is installed'\ |
| 337 | ' and in your path.' % GIT |
| 338 | raise CloneFailure() |
| 339 | if proc.wait() != 0: |
| 340 | print >>sys.stderr, 'fatal: could not create %s' % local |
| 341 | raise CloneFailure() |
| 342 | |
| 343 | _SetConfig(local, 'remote.origin.url', url) |
| 344 | _SetConfig(local, 'remote.origin.fetch', |
| 345 | '+refs/heads/*:refs/remotes/origin/*') |
| 346 | _Fetch(local, quiet) |
| 347 | _Fetch(local, quiet, '--tags') |
| 348 | |
| 349 | |
| 350 | def _Verify(cwd, branch, quiet): |
| 351 | """Verify the branch has been signed by a tag. |
| 352 | """ |
| 353 | cmd = [GIT, 'describe', 'origin/%s' % branch] |
| 354 | proc = subprocess.Popen(cmd, |
| 355 | stdout=subprocess.PIPE, |
| 356 | stderr=subprocess.PIPE, |
| 357 | cwd = cwd) |
| 358 | cur = proc.stdout.read().strip() |
| 359 | proc.stdout.close() |
| 360 | |
| 361 | proc.stderr.read() |
| 362 | proc.stderr.close() |
| 363 | |
| 364 | if proc.wait() != 0 or not cur: |
| 365 | print >>sys.stderr |
| 366 | print >>sys.stderr,\ |
| 367 | "fatal: branch '%s' has not been signed" \ |
| 368 | % branch |
| 369 | raise CloneFailure() |
| 370 | |
| 371 | m = re.compile(r'^(.*)-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur) |
| 372 | if m: |
| 373 | cur = m.group(1) |
| 374 | if not quiet: |
| 375 | print >>sys.stderr |
| 376 | print >>sys.stderr, \ |
| 377 | "info: Ignoring branch '%s'; using tagged release '%s'" \ |
| 378 | % (branch, cur) |
| 379 | print >>sys.stderr |
| 380 | |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 381 | env = os.environ.copy() |
| 382 | env['GNUPGHOME'] = gpg_dir.encode() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 383 | |
| 384 | cmd = [GIT, 'tag', '-v', cur] |
| 385 | proc = subprocess.Popen(cmd, |
| 386 | stdout = subprocess.PIPE, |
| 387 | stderr = subprocess.PIPE, |
| 388 | cwd = cwd, |
| 389 | env = env) |
| 390 | out = proc.stdout.read() |
| 391 | proc.stdout.close() |
| 392 | |
| 393 | err = proc.stderr.read() |
| 394 | proc.stderr.close() |
| 395 | |
| 396 | if proc.wait() != 0: |
| 397 | print >>sys.stderr |
| 398 | print >>sys.stderr, out |
| 399 | print >>sys.stderr, err |
| 400 | print >>sys.stderr |
| 401 | raise CloneFailure() |
| 402 | return '%s^0' % cur |
| 403 | |
| 404 | |
| 405 | def _Checkout(cwd, branch, rev, quiet): |
| 406 | """Checkout an upstream branch into the repository and track it. |
| 407 | """ |
| 408 | cmd = [GIT, 'update-ref', 'refs/heads/default', rev] |
| 409 | if subprocess.Popen(cmd, cwd = cwd).wait() != 0: |
| 410 | raise CloneFailure() |
| 411 | |
| 412 | _SetConfig(cwd, 'branch.default.remote', 'origin') |
| 413 | _SetConfig(cwd, 'branch.default.merge', 'refs/heads/%s' % branch) |
| 414 | |
| 415 | cmd = [GIT, 'symbolic-ref', 'HEAD', 'refs/heads/default'] |
| 416 | if subprocess.Popen(cmd, cwd = cwd).wait() != 0: |
| 417 | raise CloneFailure() |
| 418 | |
| 419 | cmd = [GIT, 'read-tree', '--reset', '-u'] |
| 420 | if not quiet: |
| 421 | cmd.append('-v') |
| 422 | cmd.append('HEAD') |
| 423 | if subprocess.Popen(cmd, cwd = cwd).wait() != 0: |
| 424 | raise CloneFailure() |
| 425 | |
| 426 | |
| 427 | def _FindRepo(): |
| 428 | """Look for a repo installation, starting at the current directory. |
| 429 | """ |
| 430 | dir = os.getcwd() |
| 431 | repo = None |
| 432 | |
Anthony Newnam | df14a70 | 2011-01-09 17:31:57 -0800 | [diff] [blame] | 433 | olddir = None |
| 434 | while dir != '/' \ |
| 435 | and dir != olddir \ |
| 436 | and not repo: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 437 | repo = os.path.join(dir, repodir, REPO_MAIN) |
| 438 | if not os.path.isfile(repo): |
| 439 | repo = None |
Anthony Newnam | df14a70 | 2011-01-09 17:31:57 -0800 | [diff] [blame] | 440 | olddir = dir |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 441 | dir = os.path.dirname(dir) |
| 442 | return (repo, os.path.join(dir, repodir)) |
| 443 | |
| 444 | |
| 445 | class _Options: |
| 446 | help = False |
| 447 | |
| 448 | |
| 449 | def _ParseArguments(args): |
| 450 | cmd = None |
| 451 | opt = _Options() |
| 452 | arg = [] |
| 453 | |
| 454 | for i in xrange(0, len(args)): |
| 455 | a = args[i] |
| 456 | if a == '-h' or a == '--help': |
| 457 | opt.help = True |
| 458 | |
| 459 | elif not a.startswith('-'): |
| 460 | cmd = a |
| 461 | arg = args[i + 1:] |
| 462 | break |
| 463 | return cmd, opt, arg |
| 464 | |
| 465 | |
| 466 | def _Usage(): |
| 467 | print >>sys.stderr,\ |
| 468 | """usage: repo COMMAND [ARGS] |
| 469 | |
| 470 | repo is not yet installed. Use "repo init" to install it here. |
| 471 | |
| 472 | The most commonly used repo commands are: |
| 473 | |
| 474 | init Install repo in the current working directory |
| 475 | help Display detailed help on a command |
| 476 | |
| 477 | For access to the full online help, install repo ("repo init"). |
| 478 | """ |
| 479 | sys.exit(1) |
| 480 | |
| 481 | |
| 482 | def _Help(args): |
| 483 | if args: |
| 484 | if args[0] == 'init': |
| 485 | init_optparse.print_help() |
Trond Norbye | d3fd537 | 2011-01-03 11:35:15 +0100 | [diff] [blame] | 486 | sys.exit(0) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 487 | else: |
| 488 | print >>sys.stderr,\ |
| 489 | "error: '%s' is not a bootstrap command.\n"\ |
| 490 | ' For access to online help, install repo ("repo init").'\ |
| 491 | % args[0] |
| 492 | else: |
| 493 | _Usage() |
| 494 | sys.exit(1) |
| 495 | |
| 496 | |
| 497 | def _NotInstalled(): |
| 498 | print >>sys.stderr,\ |
| 499 | 'error: repo is not installed. Use "repo init" to install it here.' |
| 500 | sys.exit(1) |
| 501 | |
| 502 | |
| 503 | def _NoCommands(cmd): |
| 504 | print >>sys.stderr,\ |
| 505 | """error: command '%s' requires repo to be installed first. |
| 506 | Use "repo init" to install it here.""" % cmd |
| 507 | sys.exit(1) |
| 508 | |
| 509 | |
| 510 | def _RunSelf(wrapper_path): |
| 511 | my_dir = os.path.dirname(wrapper_path) |
| 512 | my_main = os.path.join(my_dir, 'main.py') |
| 513 | my_git = os.path.join(my_dir, '.git') |
| 514 | |
| 515 | if os.path.isfile(my_main) and os.path.isdir(my_git): |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame] | 516 | for name in ['git_config.py', |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 517 | 'project.py', |
| 518 | 'subcmds']: |
| 519 | if not os.path.exists(os.path.join(my_dir, name)): |
| 520 | return None, None |
| 521 | return my_main, my_git |
| 522 | return None, None |
| 523 | |
| 524 | |
| 525 | def _SetDefaultsTo(gitdir): |
| 526 | global REPO_URL |
| 527 | global REPO_REV |
| 528 | |
| 529 | REPO_URL = gitdir |
| 530 | proc = subprocess.Popen([GIT, |
| 531 | '--git-dir=%s' % gitdir, |
| 532 | 'symbolic-ref', |
| 533 | 'HEAD'], |
| 534 | stdout = subprocess.PIPE, |
| 535 | stderr = subprocess.PIPE) |
| 536 | REPO_REV = proc.stdout.read().strip() |
| 537 | proc.stdout.close() |
| 538 | |
| 539 | proc.stderr.read() |
| 540 | proc.stderr.close() |
| 541 | |
| 542 | if proc.wait() != 0: |
| 543 | print >>sys.stderr, 'fatal: %s has no current branch' % gitdir |
| 544 | sys.exit(1) |
| 545 | |
| 546 | |
| 547 | def main(orig_args): |
| 548 | main, dir = _FindRepo() |
| 549 | cmd, opt, args = _ParseArguments(orig_args) |
| 550 | |
| 551 | wrapper_path = os.path.abspath(__file__) |
| 552 | my_main, my_git = _RunSelf(wrapper_path) |
| 553 | |
| 554 | if not main: |
| 555 | if opt.help: |
| 556 | _Usage() |
| 557 | if cmd == 'help': |
| 558 | _Help(args) |
| 559 | if not cmd: |
| 560 | _NotInstalled() |
| 561 | if cmd == 'init': |
| 562 | if my_git: |
| 563 | _SetDefaultsTo(my_git) |
| 564 | try: |
| 565 | _Init(args) |
| 566 | except CloneFailure: |
| 567 | for root, dirs, files in os.walk(repodir, topdown=False): |
| 568 | for name in files: |
| 569 | os.remove(os.path.join(root, name)) |
| 570 | for name in dirs: |
| 571 | os.rmdir(os.path.join(root, name)) |
| 572 | os.rmdir(repodir) |
| 573 | sys.exit(1) |
| 574 | main, dir = _FindRepo() |
| 575 | else: |
| 576 | _NoCommands(cmd) |
| 577 | |
| 578 | if my_main: |
| 579 | main = my_main |
| 580 | |
| 581 | ver_str = '.'.join(map(lambda x: str(x), VERSION)) |
| 582 | me = [main, |
| 583 | '--repo-dir=%s' % dir, |
| 584 | '--wrapper-version=%s' % ver_str, |
| 585 | '--wrapper-path=%s' % wrapper_path, |
| 586 | '--'] |
| 587 | me.extend(orig_args) |
| 588 | me.extend(extra_args) |
| 589 | try: |
| 590 | os.execv(main, me) |
| 591 | except OSError, e: |
| 592 | print >>sys.stderr, "fatal: unable to start %s" % main |
| 593 | print >>sys.stderr, "fatal: %s" % e |
| 594 | sys.exit(148) |
| 595 | |
| 596 | |
| 597 | if __name__ == '__main__': |
| 598 | main(sys.argv[1:]) |
| 599 | |