wkentaro
8/17/2015 - 9:00 AM

Speed test to get refname and hash

Speed test to get refname and hash

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function
import time
import re
import subprocess

# git describe and rev-parse
print('w/ git desc & rev-parse')
t_start = time.time()
cmd = 'git rev-parse --short HEAD'
output = subprocess.check_output(cmd, shell=True).strip()
print('hash:', output)
cmd = 'git describe --all --exact-match HEAD'
output = subprocess.check_output(cmd, shell=True).strip()
print('refname:', output)
print('time:', time.time() - t_start)

# git log
print('--------------------------------------------------')
print('w/ git log')
t_start = time.time()
cmd = 'git log -1 --format="%h%d"'
output = subprocess.check_output(cmd, shell=True).strip()
m = re.search('\(.*\)$', output)
if m:
    print('hash:', output[:m.start()])
else:
    print('hash:', output)
m = re.search('tag: .*[,\)]', output)
if m:
    print('refname:', 'tags/'+output[m.start()+5: m.end()-1])
print('time:', time.time() - t_start)