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 8import argparse 9import os 10import subprocess 11import sys 12 13# Import "git_common" from "depot_tools" root. 14DEPOT_TOOLS_ROOT = os.path.abspath(os.path.join( 15 os.path.dirname(__file__), os.pardir, os.pardir, os.pardir, os.pardir)) 16sys.path.insert(0, DEPOT_TOOLS_ROOT) 17import git_common 18 19 20def run_git(*cmd, **kwargs): 21 kwargs['stdout'] = sys.stdout 22 kwargs['stderr'] = sys.stderr 23 git_common.run(*cmd, **kwargs) 24 25 26def main(): 27 parser = argparse.ArgumentParser() 28 parser.add_argument('--path', help='Path to prospective git repo.', 29 required=True) 30 parser.add_argument('--url', help='URL of remote to make origin.', 31 required=True) 32 parser.add_argument('--remote', help='Name of the git remote.', 33 default='origin') 34 opts = parser.parse_args() 35 36 path = opts.path 37 remote = opts.remote 38 url = opts.url 39 40 if not os.path.exists(path): 41 os.makedirs(path) 42 43 if os.path.exists(os.path.join(path, '.git')): 44 run_git('config', '--remove-section', 'remote.%s' % remote, cwd=path) 45 else: 46 run_git('init', cwd=path) 47 run_git('remote', 'add', remote, url, cwd=path) 48 return 0 49 50 51if __name__ == '__main__': 52 sys.exit(main()) 53