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
|
|
|
|
|
|
|
|
from .base import BaseProject
|
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2014-01-16 00:41:35 +00:00
|
|
|
# str is unicode in Python3
|
|
|
|
try:
|
|
|
|
unicode
|
|
|
|
except NameError:
|
|
|
|
unicode = str
|
|
|
|
|
|
|
|
|
2013-07-06 07:51:09 +00:00
|
|
|
class Git(BaseProject):
|
|
|
|
|
2013-07-10 03:11:49 +00:00
|
|
|
def process(self):
|
2013-12-13 13:46:39 +00:00
|
|
|
self.configFile = self._find_git_config_file(self.path)
|
|
|
|
return self.configFile is not None
|
2013-07-10 03:11:49 +00:00
|
|
|
|
|
|
|
def name(self):
|
|
|
|
base = self._project_base()
|
|
|
|
if base:
|
2013-12-13 13:46:39 +00:00
|
|
|
return unicode(os.path.basename(base))
|
2013-07-06 07:51:09 +00:00
|
|
|
return None
|
|
|
|
|
2013-08-15 07:09:27 +00:00
|
|
|
def branch(self):
|
2013-10-13 23:31:37 +00:00
|
|
|
base = self._project_base()
|
|
|
|
if base:
|
|
|
|
head = os.path.join(self._project_base(), '.git', 'HEAD')
|
|
|
|
try:
|
2013-12-13 13:46:39 +00:00
|
|
|
with open(head) as fh:
|
|
|
|
return unicode(fh.readline().strip().rsplit('/', 1)[-1])
|
2013-10-13 23:31:37 +00:00
|
|
|
except IOError:
|
|
|
|
pass
|
2013-12-13 13:46:39 +00:00
|
|
|
return None
|
2013-08-15 07:09:27 +00:00
|
|
|
|
|
|
|
def _project_base(self):
|
2013-12-13 13:46:39 +00:00
|
|
|
if self.configFile:
|
|
|
|
return os.path.dirname(os.path.dirname(self.configFile))
|
2013-08-15 07:09:27 +00:00
|
|
|
return None
|
|
|
|
|
2013-12-13 13:46:39 +00:00
|
|
|
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
|
2013-12-13 13:46:39 +00:00
|
|
|
return self._find_git_config_file(split_path[0])
|