Skip to content

Instantly share code, notes, and snippets.

@dketov
Created March 20, 2018 11:16
Show Gist options
  • Save dketov/3145a80e9b8dd66e9af49994d95208e8 to your computer and use it in GitHub Desktop.
Save dketov/3145a80e9b8dd66e9af49994d95208e8 to your computer and use it in GitHub Desktop.
# -*- encoding: utf-8 -*-
import sys, subprocess as sp
from time import time
def cmd(cmd, result = False, pipe = False):
class piper:
def __init__(self, process):
self.process = process
self.__iter__ = self.process.stdout.__iter__
self.write = self.process.stdin.write
def __len__(self):
return sum(1 for i in self.__iter__())
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
if type:
self.process.kill()
self.process.stdin.close()
self.process.stdout.close()
self.process.wait()
def do(*args, **kwargs):
cmdline = ["git"]
cmdline.extend(cmd.split())
cmdline.extend(args)
print >> sys.stderr, "🤖\t%s" % " ".join(cmdline)
if result:
process = sp.Popen(cmdline, stdout = sp.PIPE, stdin = sp.PIPE)
stdout, stderr = process.communicate(kwargs.get('input'))
assert process.wait() == 0, "RC != 0"
return stdout.strip()
if pipe:
return piper(sp.Popen(cmdline, stdout = sp.PIPE, stdin = sp.PIPE))
return sp.Popen(cmdline, stdin = sp.PIPE).wait()
return do
fetch = cmd("fetch --tags")
get_remote = lambda branch: cmd("config --get branch.%s.remote" % branch, result = True)()
rm_branch = cmd("branch -D")
add_branch = cmd("branch")
where = cmd("symbolic-ref --short HEAD", result = True)
config_email = cmd("config --get user.email", result = True)
rev_list = cmd("rev-list", pipe = True)
find_mpt = cmd("rev-list -1 --min-age", result = True)
merge_base = cmd("merge-base", result = True)
show = cmd("rev-list -1 --format=fuller")
commit_date = cmd("log -1 --format=%cd --date=format:%d-%m-%Y", result = True)
commit_uxts = cmd("log -1 --format=%ct", result = True)
commit_subject = cmd("log -1 --format=%s", result = True)
checkout = cmd("checkout")
merge = cmd("merge --no-ff")
status = cmd("status -s", pipe = True)
add = cmd("add")
commit = cmd("commit -n")
write_tree = cmd("write-tree", result = True)
rev_parse = cmd("rev-parse", result = True)
author = cmd("var GIT_AUTHOR_IDENT", result = True)
committer = cmd("var GIT_COMMITTER_IDENT", result = True)
hash_commit = cmd("hash-object -t commit --stdin", result = True)
push = cmd("push --tags")
upstream_get = lambda name, branch: cmd("describe --tags --abbrev=0 --match upstream@%s.* --candidates=1" % name, result = True)(branch)
upstream_set = lambda branch, commit: cmd("tag")("upstream@%s.%s" % (branch, commit_uxts(commit)), commit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment