2013-07-08 05:38:01 +04: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
|
2017-11-06 06:51:43 +03:00
|
|
|
import re
|
2015-08-25 10:42:37 +03:00
|
|
|
import sys
|
2013-07-08 05:38:01 +04:00
|
|
|
|
|
|
|
from .base import BaseProject
|
2014-09-30 20:27:35 +04:00
|
|
|
from ..compat import u, open
|
2013-07-08 05:38:01 +04:00
|
|
|
|
|
|
|
|
2014-07-25 12:01:39 +04:00
|
|
|
log = logging.getLogger('WakaTime')
|
2013-07-08 05:38:01 +04:00
|
|
|
|
|
|
|
|
|
|
|
class Git(BaseProject):
|
2017-11-06 06:51:43 +03:00
|
|
|
_submodule = None
|
|
|
|
_project_name = None
|
|
|
|
_head_file = None
|
2013-07-08 05:38:01 +04:00
|
|
|
|
2013-07-10 11:14:44 +04:00
|
|
|
def process(self):
|
2017-11-06 06:51:43 +03:00
|
|
|
return self._find_git_config_file(self.path)
|
2013-07-10 11:14:44 +04:00
|
|
|
|
|
|
|
def name(self):
|
2017-11-06 06:51:43 +03:00
|
|
|
return u(self._project_name) if self._project_name else None
|
2013-07-08 05:38:01 +04:00
|
|
|
|
2013-09-07 09:59:03 +04:00
|
|
|
def branch(self):
|
2017-11-06 06:51:43 +03:00
|
|
|
head = self._head_file
|
|
|
|
if head:
|
2013-10-14 03:33:53 +04:00
|
|
|
try:
|
2014-09-30 20:27:35 +04:00
|
|
|
with open(head, 'r', encoding='utf-8') as fh:
|
2016-06-08 21:43:24 +03:00
|
|
|
return self._get_branch_from_head_file(fh.readline())
|
2015-09-29 13:11:25 +03:00
|
|
|
except UnicodeDecodeError: # pragma: nocover
|
2015-08-25 10:42:37 +03:00
|
|
|
try:
|
|
|
|
with open(head, 'r', encoding=sys.getfilesystemencoding()) as fh:
|
2016-06-08 21:43:24 +03:00
|
|
|
return self._get_branch_from_head_file(fh.readline())
|
2015-08-25 10:42:37 +03:00
|
|
|
except:
|
2016-09-02 11:50:54 +03:00
|
|
|
log.traceback(logging.WARNING)
|
2015-09-29 13:11:25 +03:00
|
|
|
except IOError: # pragma: nocover
|
2016-09-02 11:50:54 +03:00
|
|
|
log.traceback(logging.WARNING)
|
2016-05-21 15:28:50 +03:00
|
|
|
return u('master')
|
2013-09-07 09:59:03 +04:00
|
|
|
|
2013-12-13 18:35:49 +04:00
|
|
|
def _find_git_config_file(self, path):
|
2013-07-08 05:38:01 +04: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')):
|
2017-11-06 06:51:43 +03:00
|
|
|
self._project_name = os.path.basename(path)
|
|
|
|
self._head_file = os.path.join(path, '.git', 'HEAD')
|
|
|
|
return True
|
|
|
|
if self._submodules_supported_for_path(path):
|
|
|
|
submodule_path = self._find_path_from_submodule(path)
|
|
|
|
if submodule_path:
|
|
|
|
self._project_name = os.path.basename(path)
|
|
|
|
self._head_file = os.path.join(submodule_path, 'HEAD')
|
|
|
|
return True
|
2013-07-08 05:38:01 +04:00
|
|
|
split_path = os.path.split(path)
|
|
|
|
if split_path[1] == '':
|
2017-11-06 06:51:43 +03:00
|
|
|
return False
|
2013-12-13 18:35:49 +04:00
|
|
|
return self._find_git_config_file(split_path[0])
|
2016-06-08 21:43:24 +03:00
|
|
|
|
|
|
|
def _get_branch_from_head_file(self, line):
|
|
|
|
if u(line.strip()).startswith('ref: '):
|
|
|
|
return u(line.strip().rsplit('/', 1)[-1])
|
|
|
|
return None
|
2017-11-06 06:51:43 +03:00
|
|
|
|
|
|
|
def _submodules_supported_for_path(self, path):
|
|
|
|
if not self._configs:
|
|
|
|
return True
|
|
|
|
|
|
|
|
disabled = self._configs.get('submodules_disabled')
|
|
|
|
if not disabled:
|
|
|
|
return True
|
|
|
|
|
|
|
|
if disabled.strip().lower() == 'true':
|
|
|
|
return False
|
|
|
|
if disabled.strip().lower() == 'false':
|
|
|
|
return True
|
|
|
|
|
|
|
|
for pattern in disabled.split("\n"):
|
|
|
|
if pattern.strip():
|
|
|
|
try:
|
|
|
|
compiled = re.compile(pattern, re.IGNORECASE)
|
|
|
|
if compiled.search(path):
|
|
|
|
return False
|
|
|
|
except re.error as ex:
|
|
|
|
log.warning(u('Regex error ({msg}) for disable git submodules pattern: {pattern}').format(
|
|
|
|
msg=u(ex),
|
|
|
|
pattern=u(pattern),
|
|
|
|
))
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _find_path_from_submodule(self, path):
|
|
|
|
link = os.path.join(path, '.git')
|
|
|
|
if not os.path.isfile(link):
|
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(link, 'r', encoding='utf-8') as fh:
|
|
|
|
return self._get_path_from_submodule_link(path, fh.readline())
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
try:
|
|
|
|
with open(link, 'r', encoding=sys.getfilesystemencoding()) as fh:
|
|
|
|
return self._get_path_from_submodule_link(path, fh.readline())
|
|
|
|
except:
|
|
|
|
log.traceback(logging.WARNING)
|
|
|
|
except IOError:
|
|
|
|
log.traceback(logging.WARNING)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def _get_path_from_submodule_link(self, path, line):
|
|
|
|
if line.startswith('gitdir: '):
|
|
|
|
subpath = line[len('gitdir: '):].strip()
|
|
|
|
if os.path.isfile(os.path.join(path, subpath, 'config')) and \
|
|
|
|
os.path.isfile(os.path.join(path, subpath, 'HEAD')):
|
|
|
|
return os.path.join(path, subpath)
|
|
|
|
|
|
|
|
return None
|