Skip to content

Instantly share code, notes, and snippets.

@kampersanda
Created January 21, 2020 11:34
Show Gist options
  • Save kampersanda/6e8fc4457e2590295a84de260464ac11 to your computer and use it in GitHub Desktop.
Save kampersanda/6e8fc4457e2590295a84de260464ac11 to your computer and use it in GitHub Desktop.
Git clone script with making directories
#!/usr/bin/env python3
import os
import sys
import subprocess
from argparse import ArgumentParser
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument('path')
parser.add_argument('-r', '--recursive', action='store_true')
args = parser.parse_args()
path = args.path
if path.startswith('https://'): # e.g., https://github.com/simongog/sdsl-lite.git
name = path.split('/')[-1]
dirs = path[len('https://'):-(len(name) + 1)]
elif path.startswith('git@'): # e.g., git@github.com:simongog/sdsl-lite.git
name = path.split('/')[-1]
dirs = path[len('git@'):-(len(name) + 1)].replace(':', '/')
else:
print('unexpected URL', file=sys.stderr)
sys.exit()
recur = ''
if args.recursive:
recur = '--recursive'
os.makedirs(dirs, exist_ok=True)
os.chdir(dirs)
subprocess.run(f'git clone {recur} {path}', shell=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment