2013-07-08 05:38:01 +04: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
|
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 Mercurial(BaseProject):
|
|
|
|
|
2013-07-10 11:14:44 +04:00
|
|
|
def process(self):
|
2014-02-05 13:01:47 +04:00
|
|
|
self.configDir = self._find_hg_config_dir(self.path)
|
|
|
|
return self.configDir is not None
|
2013-07-10 11:14:44 +04:00
|
|
|
|
|
|
|
def name(self):
|
2014-02-05 13:01:47 +04:00
|
|
|
if self.configDir:
|
2014-09-30 20:27:35 +04:00
|
|
|
return u(os.path.basename(os.path.dirname(self.configDir)))
|
2015-09-29 13:11:25 +03:00
|
|
|
return None # pragma: nocover
|
2013-07-08 05:38:01 +04:00
|
|
|
|
2013-09-07 09:59:03 +04:00
|
|
|
def branch(self):
|
2014-02-05 13:01:47 +04:00
|
|
|
if self.configDir:
|
|
|
|
branch_file = os.path.join(self.configDir, 'branch')
|
|
|
|
try:
|
2014-09-30 20:27:35 +04:00
|
|
|
with open(branch_file, 'r', encoding='utf-8') as fh:
|
|
|
|
return u(fh.readline().strip().rsplit('/', 1)[-1])
|
2015-09-29 13:11:25 +03:00
|
|
|
except UnicodeDecodeError: # pragma: nocover
|
2015-08-25 10:42:37 +03:00
|
|
|
try:
|
|
|
|
with open(branch_file, 'r', encoding=sys.getfilesystemencoding()) as fh:
|
|
|
|
return u(fh.readline().strip().rsplit('/', 1)[-1])
|
|
|
|
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)
|
2014-09-30 20:27:35 +04:00
|
|
|
return u('default')
|
2014-02-05 13:01:47 +04:00
|
|
|
|
2018-09-21 08:29:34 +03:00
|
|
|
def folder(self):
|
|
|
|
if self.configDir:
|
|
|
|
return os.path.dirname(self.configDir)
|
|
|
|
return None
|
|
|
|
|
2014-02-05 13:01:47 +04:00
|
|
|
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])
|