support for git submodules

This commit is contained in:
Alan Hamlett 2017-11-04 11:48:49 -07:00
parent 06b8fa2b7c
commit 06a9d651db
14 changed files with 226 additions and 16 deletions

View file

@ -11,6 +11,7 @@
import logging
import os
import re
import sys
from .base import BaseProject
@ -21,21 +22,19 @@ log = logging.getLogger('WakaTime')
class Git(BaseProject):
_submodule = None
_project_name = None
_head_file = None
def process(self):
self.configFile = self._find_git_config_file(self.path)
return self.configFile is not None
return self._find_git_config_file(self.path)
def name(self):
base = self._project_base()
if base:
return u(os.path.basename(base))
return None # pragma: nocover
return u(self._project_name) if self._project_name else None
def branch(self):
base = self._project_base()
if base:
head = os.path.join(self._project_base(), '.git', 'HEAD')
head = self._head_file
if head:
try:
with open(head, 'r', encoding='utf-8') as fh:
return self._get_branch_from_head_file(fh.readline())
@ -49,23 +48,81 @@ class Git(BaseProject):
log.traceback(logging.WARNING)
return u('master')
def _project_base(self):
if self.configFile:
return os.path.dirname(os.path.dirname(self.configFile))
return None # pragma: nocover
def _find_git_config_file(self, path):
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')
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
split_path = os.path.split(path)
if split_path[1] == '':
return None
return False
return self._find_git_config_file(split_path[0])
def _get_branch_from_head_file(self, line):
if u(line.strip()).startswith('ref: '):
return u(line.strip().rsplit('/', 1)[-1])
return None
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