1
# -*- coding: utf-8 -*-
3
# Copyright © 2011 Pierre Raybaut
4
# Licensed under the terms of the MIT License
5
# (see spyderlib/__init__.py for details)
7
"""Utilities for version control systems"""
13
from spyderlib.baseconfig import _
14
from spyderlib.utils import programs
15
from spyderlib.utils.misc import abspardir
19
'.hg': dict(name="Mercurial",
20
commit=( ('thg', ['commit']),
21
('hgtk', ['commit']) ),
22
browse=( ('thg', ['log']),
25
'.git': dict(name="git",
26
commit=( ('git', ['gui']), ),
27
browse=( ('gitk', []), )
32
def get_vcs_infos(path):
33
"""Return VCS infos if path is a supported VCS repository"""
34
for dirname, infos in VCS_INFOS.iteritems():
35
vcs_path = osp.join(path, dirname)
36
if osp.isdir(vcs_path):
40
def get_vcs_root(path):
41
"""Return VCS root directory path
42
Return None if path is not within a supported VCS repository"""
44
while get_vcs_infos(path) is None:
45
path = abspardir(path)
46
if path == previous_path:
50
return osp.abspath(path)
53
def is_vcs_repository(path):
54
"""Return True if path is a supported VCS repository"""
55
return get_vcs_root(path) is not None
58
def run_vcs_tool(path, tool):
59
"""If path is a valid VCS repository, run the corresponding VCS tool
60
Supported VCS tools: 'commit', 'browse'
61
Return False if the VCS tool is not installed"""
62
infos = get_vcs_infos(get_vcs_root(path))
63
for name, args in infos[tool]:
64
if programs.find_program(name):
65
programs.run_program(name, args, cwd=path)
68
raise RuntimeError(_("For %s support, please install one of the<br/> "
69
"following tools:<br/><br/> %s")
71
', '.join([name for name,cmd in infos['commit']])
75
def is_hg_installed():
76
"""Return True if Mercurial is installed"""
77
return programs.find_program('hg') is not None
80
def get_hg_revision(repopath):
81
"""Return Mercurial revision for the repository located at repopath
82
Result is a tuple (global, local, branch), with None values on error
84
>>> get_hg_revision(".")
85
('eba7273c69df+', '2015+', 'default')
88
hg = programs.find_program('hg')
90
output = subprocess.check_output([hg, 'id', '-nib', repopath])
91
# output is now: ('eba7273c69df+ 2015+ default\n', None)
92
return tuple(output.strip().split())
93
except (subprocess.CalledProcessError, AssertionError, AttributeError):
94
# print("Error: Failed to get revision number from Mercurial - %s" % exc)
95
return (None, None, None)
98
if __name__ == '__main__':
99
print get_vcs_root(osp.dirname(__file__))
100
print get_vcs_root(r'D:\Python\ipython\IPython\frontend')
101
#run_vcs_tool(r'D:\Python\userconfig\userconfig', 'commit')
102
print get_hg_revision(osp.dirname(__file__)+"/../..")
103
print get_hg_revision('/')