2013-07-08 01:38:01 +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
|
2015-08-25 07:42:37 +00:00
|
|
|
import sys
|
2013-07-08 01:38:01 +00:00
|
|
|
|
|
|
|
from .base import BaseProject
|
2014-09-30 16:27:35 +00:00
|
|
|
from ..compat import u, open
|
2013-07-08 01:38:01 +00:00
|
|
|
|
|
|
|
|
2014-07-25 08:01:39 +00:00
|
|
|
log = logging.getLogger('WakaTime')
|
2013-07-08 01:38:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Git(BaseProject):
|
|
|
|
|
2013-07-10 07:14:44 +00:00
|
|
|
def process(self):
|
2013-12-13 14:35:49 +00:00
|
|
|
self.configFile = self._find_git_config_file(self.path)
|
|
|
|
return self.configFile is not None
|
2013-07-10 07:14:44 +00:00
|
|
|
|
|
|
|
def name(self):
|
|
|
|
base = self._project_base()
|
|
|
|
if base:
|
2014-09-30 16:27:35 +00:00
|
|
|
return u(os.path.basename(base))
|
2015-09-29 10:11:25 +00:00
|
|
|
return None # pragma: nocover
|
2013-07-08 01:38:01 +00:00
|
|
|
|
2013-09-07 05:59:03 +00:00
|
|
|
def branch(self):
|
2013-10-13 23:33:53 +00:00
|
|
|
base = self._project_base()
|
|
|
|
if base:
|
|
|
|
head = os.path.join(self._project_base(), '.git', 'HEAD')
|
|
|
|
try:
|
2014-09-30 16:27:35 +00:00
|
|
|
with open(head, 'r', encoding='utf-8') as fh:
|
|
|
|
return u(fh.readline().strip().rsplit('/', 1)[-1])
|
2015-09-29 10:11:25 +00:00
|
|
|
except UnicodeDecodeError: # pragma: nocover
|
2015-08-25 07:42:37 +00:00
|
|
|
try:
|
|
|
|
with open(head, 'r', encoding=sys.getfilesystemencoding()) as fh:
|
|
|
|
return u(fh.readline().strip().rsplit('/', 1)[-1])
|
|
|
|
except:
|
|
|
|
log.exception("Exception:")
|
2015-09-29 10:11:25 +00:00
|
|
|
except IOError: # pragma: nocover
|
2015-08-25 07:42:37 +00:00
|
|
|
log.exception("Exception:")
|
2013-12-13 14:35:49 +00:00
|
|
|
return None
|
2013-09-07 05:59:03 +00:00
|
|
|
|
|
|
|
def _project_base(self):
|
2013-12-13 14:35:49 +00:00
|
|
|
if self.configFile:
|
|
|
|
return os.path.dirname(os.path.dirname(self.configFile))
|
2013-09-07 05:59:03 +00:00
|
|
|
return None
|
|
|
|
|
2013-12-13 14:35:49 +00:00
|
|
|
def _find_git_config_file(self, path):
|
2013-07-08 01:38:01 +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
|
2013-12-13 14:35:49 +00:00
|
|
|
return self._find_git_config_file(split_path[0])
|