read .git/HEAD file directly to find branch instead of running git command line
This commit is contained in:
parent
5ad2d3d0e6
commit
da3f519dc0
1 changed files with 10 additions and 18 deletions
|
@ -11,7 +11,6 @@
|
|||
|
||||
import logging
|
||||
import os
|
||||
from subprocess import Popen, PIPE
|
||||
|
||||
from .base import BaseProject
|
||||
try:
|
||||
|
@ -38,23 +37,16 @@ class Git(BaseProject):
|
|||
return None
|
||||
|
||||
def branch(self):
|
||||
stdout = None
|
||||
try:
|
||||
stdout, stderr = Popen([
|
||||
'git', 'branch', '--no-color'
|
||||
], stdout=PIPE, stderr=PIPE, cwd=self._project_base()
|
||||
).communicate()
|
||||
except OSError:
|
||||
pass
|
||||
if stdout:
|
||||
for line in stdout.splitlines():
|
||||
if isinstance(line, bytes):
|
||||
line = bytes.decode(line)
|
||||
line = line.split(' ', 1)
|
||||
if line[0] == '*':
|
||||
return line[1]
|
||||
return None
|
||||
|
||||
branch = None
|
||||
base = self._project_base()
|
||||
if base:
|
||||
head = os.path.join(self._project_base(), '.git', 'HEAD')
|
||||
try:
|
||||
with open(head) as f:
|
||||
branch = f.readline().strip().rsplit('/', 1)[-1]
|
||||
except IOError:
|
||||
pass
|
||||
return branch
|
||||
|
||||
def _project_base(self):
|
||||
if self.config:
|
||||
|
|
Loading…
Reference in a new issue