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