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