From 7697b54e8ea12f1ef80afe353371e968fa6aebc6 Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Wed, 5 Feb 2014 00:55:52 -0800 Subject: [PATCH] detect project name and branch from mercurial revision control --- wakatime/projects/git.py | 4 ---- wakatime/projects/mercurial.py | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/wakatime/projects/git.py b/wakatime/projects/git.py index ae433d5..c8539a6 100644 --- a/wakatime/projects/git.py +++ b/wakatime/projects/git.py @@ -13,10 +13,6 @@ import logging import os from .base import BaseProject -try: - from collections import OrderedDict -except ImportError: - from ..packages.ordereddict import OrderedDict log = logging.getLogger(__name__) diff --git a/wakatime/projects/mercurial.py b/wakatime/projects/mercurial.py index 4e9ce96..f91ab71 100644 --- a/wakatime/projects/mercurial.py +++ b/wakatime/projects/mercurial.py @@ -18,13 +18,41 @@ from .base import BaseProject log = logging.getLogger(__name__) +# str is unicode in Python3 +try: + unicode +except NameError: + unicode = str + + class Mercurial(BaseProject): def process(self): - return False + self.configDir = self._find_hg_config_dir(self.path) + return self.configDir is not None def name(self): + if self.configDir: + return unicode(os.path.basename(os.path.dirname(self.configDir))) return None def branch(self): - return None + 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])