2013-07-06 07:51:09 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
wakatime.projects.mercurial
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Information about the mercurial 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
|
|
|
|
|
|
|
|
|
2014-07-25 07:59:25 +00:00
|
|
|
log = logging.getLogger('WakaTime')
|
2013-07-06 07:51:09 +00:00
|
|
|
|
|
|
|
|
2014-02-05 08:55:52 +00:00
|
|
|
# str is unicode in Python3
|
|
|
|
try:
|
|
|
|
unicode
|
|
|
|
except NameError:
|
|
|
|
unicode = str
|
|
|
|
|
|
|
|
|
2013-07-06 07:51:09 +00:00
|
|
|
class Mercurial(BaseProject):
|
|
|
|
|
2013-07-10 03:11:49 +00:00
|
|
|
def process(self):
|
2014-02-05 08:55:52 +00:00
|
|
|
self.configDir = self._find_hg_config_dir(self.path)
|
|
|
|
return self.configDir is not None
|
2013-07-10 03:11:49 +00:00
|
|
|
|
|
|
|
def name(self):
|
2014-02-05 08:55:52 +00:00
|
|
|
if self.configDir:
|
|
|
|
return unicode(os.path.basename(os.path.dirname(self.configDir)))
|
2013-07-10 03:11:49 +00:00
|
|
|
return None
|
2013-07-06 07:51:09 +00:00
|
|
|
|
2013-08-15 07:09:27 +00:00
|
|
|
def branch(self):
|
2014-02-05 08:55:52 +00:00
|
|
|
if self.configDir:
|
|
|
|
branch_file = os.path.join(self.configDir, 'branch')
|
|
|
|
try:
|
|
|
|
with open(branch_file) as fh:
|
|
|
|
return unicode(fh.readline().strip().rsplit('/', 1)[-1])
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
return unicode('default')
|
|
|
|
|
|
|
|
def _find_hg_config_dir(self, path):
|
|
|
|
path = os.path.realpath(path)
|
|
|
|
if os.path.isfile(path):
|
|
|
|
path = os.path.split(path)[0]
|
|
|
|
if os.path.isdir(os.path.join(path, '.hg')):
|
|
|
|
return os.path.join(path, '.hg')
|
|
|
|
split_path = os.path.split(path)
|
|
|
|
if split_path[1] == '':
|
|
|
|
return None
|
|
|
|
return self._find_hg_config_dir(split_path[0])
|