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
|
2013-07-10 03:11:49 +00:00
|
|
|
from subprocess import Popen, PIPE
|
2013-07-06 07:51:09 +00:00
|
|
|
|
|
|
|
from .base import BaseProject
|
2013-07-10 03:11:49 +00:00
|
|
|
from ..packages.ordereddict import OrderedDict
|
2013-07-06 07:51:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Git(BaseProject):
|
|
|
|
|
2013-07-10 03:11:49 +00:00
|
|
|
def process(self):
|
|
|
|
self.config = self._find_config(self.path)
|
2013-07-06 07:51:09 +00:00
|
|
|
if self.config:
|
2013-07-10 03:11:49 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def name(self):
|
|
|
|
base = self._project_base()
|
|
|
|
if base:
|
|
|
|
return os.path.basename(base)
|
2013-07-06 07:51:09 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
def tags(self):
|
|
|
|
tags = []
|
|
|
|
if self.config:
|
2013-07-20 21:42:19 +00:00
|
|
|
proj_name = self.name()
|
|
|
|
if proj_name:
|
|
|
|
tags.append(proj_name)
|
2013-07-10 03:11:49 +00:00
|
|
|
sections = self._parse_config()
|
2013-07-06 07:51:09 +00:00
|
|
|
for section in sections:
|
|
|
|
if section.split(' ', 1)[0] == 'remote' and 'url' in sections[section]:
|
2013-07-20 21:42:19 +00:00
|
|
|
remote = sections[section]['url'].rsplit(':', 1)[1].rsplit('/', 1)[1].split('.git', 1)[0]
|
|
|
|
tags.append(remote)
|
2013-07-10 03:11:49 +00:00
|
|
|
branch = self._current_branch()
|
|
|
|
if branch is not None:
|
|
|
|
tags.append(branch)
|
2013-07-06 07:51:09 +00:00
|
|
|
return tags
|
|
|
|
|
2013-07-10 03:11:49 +00:00
|
|
|
def _project_base(self):
|
|
|
|
if self.config:
|
|
|
|
return os.path.dirname(os.path.dirname(self.config))
|
|
|
|
return None
|
|
|
|
|
|
|
|
def _current_branch(self):
|
|
|
|
stdout = None
|
|
|
|
try:
|
|
|
|
stdout, stderr = Popen([
|
2013-07-10 08:33:36 +00:00
|
|
|
'git', 'branch', '--no-color', '--list'
|
|
|
|
], stdout=PIPE, stderr=PIPE, cwd=self._project_base()
|
|
|
|
).communicate()
|
2013-07-10 03:11:49 +00:00
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
if stdout:
|
|
|
|
for line in stdout.splitlines():
|
|
|
|
line = line.split(' ', 1)
|
|
|
|
if line[0] == '*':
|
|
|
|
return line[1]
|
|
|
|
return None
|
|
|
|
|
|
|
|
def _find_config(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-07-10 03:11:49 +00:00
|
|
|
return self._find_config(split_path[0])
|
2013-07-06 07:51:09 +00:00
|
|
|
|
2013-07-10 03:11:49 +00:00
|
|
|
def _parse_config(self):
|
2013-07-06 07:51:09 +00:00
|
|
|
sections = {}
|
|
|
|
try:
|
2013-07-07 23:28:18 +00:00
|
|
|
f = open(self.config, 'r')
|
2013-07-06 07:51:09 +00:00
|
|
|
except IOError as e:
|
|
|
|
log.exception("Exception:")
|
|
|
|
else:
|
|
|
|
with f:
|
|
|
|
section = None
|
|
|
|
for line in f.readlines():
|
|
|
|
line = line.lstrip()
|
|
|
|
if len(line) > 0 and line[0] == '[':
|
|
|
|
section = line[1:].split(']', 1)[0]
|
|
|
|
temp = section.split(' ', 1)
|
|
|
|
section = temp[0].lower()
|
|
|
|
if len(temp) > 1:
|
|
|
|
section = ' '.join([section, temp[1]])
|
|
|
|
sections[section] = {}
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
(setting, value) = line.split('=', 1)
|
|
|
|
except ValueError:
|
|
|
|
setting = line.split('#', 1)[0].split(';', 1)[0]
|
|
|
|
value = 'true'
|
|
|
|
setting = setting.strip().lower()
|
|
|
|
value = value.split('#', 1)[0].split(';', 1)[0].strip()
|
|
|
|
sections[section][setting] = value
|
|
|
|
f.close()
|
|
|
|
return sections
|