forked from jisaacks/GitGutter
-
Notifications
You must be signed in to change notification settings - Fork 20
/
vcs_helpers.py
85 lines (69 loc) · 2.43 KB
/
vcs_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
import sublime
class VcsHelper(object):
@classmethod
def vcs_dir(cls, directory):
"""Return the path to the metadata directory, assuming it is
directly under the passed directory."""
if not directory:
return False
return os.path.join(directory, cls.meta_data_directory())
@classmethod
def vcs_file_path(cls, view, vcs_path):
"""Returns the relative path to the file in the Sublime view, in
the repository rooted at vcs_path."""
if not vcs_path:
return False
full_file_path = os.path.realpath(view.file_name())
vcs_path_to_file = \
full_file_path.replace(vcs_path, '').replace('\\', '/')
if vcs_path_to_file[0] == '/':
vcs_path_to_file = vcs_path_to_file[1:]
return vcs_path_to_file
@classmethod
def vcs_root(cls, directory):
"""Returns the top-level directory of the repository."""
if os.path.exists(os.path.join(directory,
cls.meta_data_directory())):
return directory
else:
parent = os.path.realpath(os.path.join(directory, os.path.pardir))
if parent == directory:
# we have reached root dir
return False
else:
return cls.vcs_root(parent)
@classmethod
def vcs_tree(cls, view):
"""Returns the directory at the top of the tree that contains
the file in the passed Sublime view."""
full_file_path = view.file_name()
file_parent_dir = os.path.realpath(os.path.dirname(full_file_path))
return cls.vcs_root(file_parent_dir)
@classmethod
def is_repository(cls, view):
if view is None or view.file_name() is None or not cls.vcs_dir(cls.vcs_tree(view)):
return False
else:
return True
class GitHelper(VcsHelper):
@classmethod
def meta_data_directory(cls):
return '.git'
@classmethod
def is_git_repository(cls, view):
return cls.is_repository(view)
class HgHelper(VcsHelper):
@classmethod
def meta_data_directory(cls):
return '.hg'
@classmethod
def is_hg_repository(cls, view):
return cls.is_repository(view)
class SvnHelper(VcsHelper):
@classmethod
def meta_data_directory(cls):
return '.svn'
@classmethod
def is_svn_repository(cls, view):
return cls.is_repository(view)