OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """This script ensures that a given directory is an initialized git repo.""" |
| 7 |
| 8 import argparse |
| 9 import logging |
| 10 import os |
| 11 import subprocess |
| 12 import sys |
| 13 |
| 14 |
| 15 def run_git(git_cmd, *args, **kwargs): |
| 16 """Runs git with given arguments. |
| 17 |
| 18 kwargs are passed through to subprocess. |
| 19 |
| 20 If the kwarg 'throw' is provided, this behaves as check_call, otherwise will |
| 21 return git's return value. |
| 22 """ |
| 23 logging.info('Running: %s %s %s', git_cmd, args, kwargs) |
| 24 func = subprocess.check_call if kwargs.pop('throw', True) else subprocess.call |
| 25 return func((git_cmd,)+args, **kwargs) |
| 26 |
| 27 |
| 28 def main(): |
| 29 parser = argparse.ArgumentParser() |
| 30 parser.add_argument('--path', help='Path to prospective git repo.', |
| 31 required=True) |
| 32 parser.add_argument('--url', help='URL of remote to make origin.', |
| 33 required=True) |
| 34 parser.add_argument('--git_cmd_path', |
| 35 help='Path to the git command to run.', |
| 36 default='git') |
| 37 parser.add_argument('--remote', help='Name of the git remote.', |
| 38 default='origin') |
| 39 parser.add_argument('-v', '--verbose', action='store_true') |
| 40 opts = parser.parse_args() |
| 41 |
| 42 path = opts.path |
| 43 remote = opts.remote |
| 44 url = opts.url |
| 45 |
| 46 logging.getLogger().setLevel(logging.DEBUG if opts.verbose else logging.WARN) |
| 47 |
| 48 if not os.path.exists(path): |
| 49 os.makedirs(path) |
| 50 |
| 51 if os.path.exists(os.path.join(path, '.git')): |
| 52 run_git(opts.git_cmd_path, 'config', '--remove-section', |
| 53 'remote.%s' % remote, cwd=path) |
| 54 else: |
| 55 run_git(opts.git_cmd_path, 'init', cwd=path) |
| 56 run_git(opts.git_cmd_path, 'remote', 'add', remote, url, cwd=path) |
| 57 return 0 |
| 58 |
| 59 |
| 60 if __name__ == '__main__': |
| 61 sys.exit(main()) |
OLD | NEW |