Skip to content

Instantly share code, notes, and snippets.

@mythmon
Created November 18, 2013 22:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mythmon/7536335 to your computer and use it in GitHub Desktop.
Save mythmon/7536335 to your computer and use it in GitHub Desktop.
A helper to make calling git with subprocess nicer in python.
def to_cli_args(*args, **kwargs):
cmd = []
for k, v in kwargs.items():
short = len(k) == 1
if short:
cmd.append('-' + k)
if v is not True:
cmd.append(v)
else:
if v is True:
cmd.append('--' + k)
else:
cmd.append('--{0}={1}'.format(k, v))
cmd.extend(args)
return cmd
class Git(object):
def __init__(self, cmd=None):
self.current = list(cmd) if cmd else ['git']
def __call__(self, *args, **kwargs):
cmd = self.current + to_cli_args(*args, **kwargs)
return subprocess.check_output(cmd).decode('utf8')
def __getattr__(self, name):
name = name.replace('_', '-')
return Git(self.current + [name])
def __str__(self):
return str(self.current)
def __repr__(self):
return '<git.Git object {0}>'.format(str(self))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment