read .git/HEAD file directly to find branch instead of running git command line

This commit is contained in:
Alan Hamlett 2013-10-13 16:31:37 -07:00
parent 5ad2d3d0e6
commit da3f519dc0

View file

@ -11,7 +11,6 @@
import logging import logging
import os import os
from subprocess import Popen, PIPE
from .base import BaseProject from .base import BaseProject
try: try:
@ -38,23 +37,16 @@ class Git(BaseProject):
return None return None
def branch(self): def branch(self):
stdout = None branch = None
try: base = self._project_base()
stdout, stderr = Popen([ if base:
'git', 'branch', '--no-color' head = os.path.join(self._project_base(), '.git', 'HEAD')
], stdout=PIPE, stderr=PIPE, cwd=self._project_base() try:
).communicate() with open(head) as f:
except OSError: branch = f.readline().strip().rsplit('/', 1)[-1]
pass except IOError:
if stdout: pass
for line in stdout.splitlines(): return branch
if isinstance(line, bytes):
line = bytes.decode(line)
line = line.split(' ', 1)
if line[0] == '*':
return line[1]
return None
def _project_base(self): def _project_base(self):
if self.config: if self.config: