rana-cli/wakatime/projects/git.py

67 lines
1.9 KiB
Python
Raw Normal View History

2013-07-06 07:51:09 +00:00
# -*- coding: utf-8 -*-
"""
wakatime.projects.git
~~~~~~~~~~~~~~~~~~~~~
Information about the git project for a given file.
:copyright: (c) 2013 Alan Hamlett.
:license: BSD, see LICENSE for more details.
"""
import logging
import os
import sys
2013-07-06 07:51:09 +00:00
from .base import BaseProject
from ..compat import u, open
2013-07-06 07:51:09 +00:00
log = logging.getLogger('WakaTime')
2013-07-06 07:51:09 +00:00
class Git(BaseProject):
def process(self):
self.configFile = self._find_git_config_file(self.path)
return self.configFile is not None
def name(self):
base = self._project_base()
if base:
return u(os.path.basename(base))
2015-09-08 03:57:21 +00:00
return None # pragma: nocover
2013-07-06 07:51:09 +00:00
2013-08-15 07:09:27 +00:00
def branch(self):
base = self._project_base()
if base:
head = os.path.join(self._project_base(), '.git', 'HEAD')
try:
with open(head, 'r', encoding='utf-8') as fh:
return u(fh.readline().strip().rsplit('/', 1)[-1])
2015-09-08 03:57:21 +00:00
except UnicodeDecodeError: # pragma: nocover
try:
with open(head, 'r', encoding=sys.getfilesystemencoding()) as fh:
return u(fh.readline().strip().rsplit('/', 1)[-1])
except:
log.traceback('warn')
2015-09-08 03:57:21 +00:00
except IOError: # pragma: nocover
log.traceback('warn')
return None
2013-08-15 07:09:27 +00:00
def _project_base(self):
if self.configFile:
return os.path.dirname(os.path.dirname(self.configFile))
2013-08-15 07:09:27 +00:00
return None
def _find_git_config_file(self, path):
2013-07-06 07:51:09 +00:00
path = os.path.realpath(path)
if os.path.isfile(path):
path = os.path.split(path)[0]
if os.path.isfile(os.path.join(path, '.git', 'config')):
return os.path.join(path, '.git', 'config')
split_path = os.path.split(path)
if split_path[1] == '':
return None
return self._find_git_config_file(split_path[0])