-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_gutter_settings.py
124 lines (103 loc) · 4.51 KB
/
git_gutter_settings.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os
import shutil
import sublime
ST3 = int(sublime.version()) >= 3000
settings = None
def plugin_loaded():
settings.load_settings()
class GitGutterSettings:
compare_against = None
def __init__(self):
self._settings = None
self._user_settings = None
self._git_binary_path_fallback = None
self._git_binary_path_error_shown = False
# These settings have public getters as they go through more
# complex initialization than just getting the value from settings.
self.git_binary_path = None
self.ignore_whitespace = False
self.patience_switch = ''
self.show_in_minimap = False
self.show_status = 'none'
def get(self, key, default=None):
if self._settings is None or not self._settings.has(key):
self.load_settings()
return self._settings.get(key, default)
def set(self, key, value):
if self._settings is None:
self.load_settings()
return self._settings.set(key, value)
def load_settings(self):
self._settings = sublime.load_settings('GitGutter.sublime-settings')
self._user_settings = sublime.load_settings(
'Preferences.sublime-settings')
# Git Binary Setting
git_binary_setting = (
self._user_settings.get("git_binary") or
self._settings.get("git_binary"))
if isinstance(git_binary_setting, dict):
self.git_binary_path = git_binary_setting.get(sublime.platform())
if not self.git_binary_path:
self.git_binary_path = git_binary_setting.get('default')
else:
self.git_binary_path = git_binary_setting
if self.git_binary_path:
self.git_binary_path = os.path.expandvars(self.git_binary_path)
elif self._git_binary_path_fallback:
self.git_binary_path = self._git_binary_path_fallback
elif ST3:
self.git_binary_path = shutil.which("git")
self._git_binary_path_fallback = self.git_binary_path
else:
git_exe = "git.exe" if sublime.platform() == "windows" else "git"
for folder in os.environ["PATH"].split(os.pathsep):
path = os.path.join(folder.strip('"'), git_exe)
if os.path.isfile(path) and os.access(path, os.X_OK):
self.git_binary_path = path
self._git_binary_path_fallback = path
break
if not self.git_binary_path:
if not self._git_binary_path_error_shown:
self._git_binary_path_error_shown = True
msg = ("Your Git binary cannot be found. If it is installed, "
"add it to your PATH environment variable, or add "
"a `git_binary` setting in the "
"`User/GitGutter.sublime-settings` file.")
sublime.error_message(msg)
raise ValueError("Git binary not found.")
# Ignore White Space Setting
self.ignore_whitespace = self._settings.get('ignore_whitespace')
if self.ignore_whitespace == 'all':
self.ignore_whitespace = '-w'
elif self.ignore_whitespace == 'eol':
self.ignore_whitespace = '--ignore-space-at-eol'
else:
self.ignore_whitespace = ''
# Patience Setting
self.patience_switch = ''
if self._settings.get('patience'):
self.patience_switch = '--patience'
# Show in minimap
self.show_in_minimap = (
self._user_settings.get('show_in_minimap') or
self._settings.get('show_in_minimap'))
# Show information in status bar
self.show_status = (
self._user_settings.get('show_status') or
self._settings.get('show_status'))
if self.show_status != 'all' and self.show_status != 'none':
self.show_status = 'default'
def get_compare_against(self, view):
# GitGutterSettings.compare_against overrides both project settings and
# plugin settings if set.
compare = GitGutterSettings.compare_against
if compare:
return compare
compare = self.get('compare_against', compare)
# Project settings override plugin settings if set.
return view.settings().get('git_gutter_compare_against', compare)
def set_compare_against(self, new_compare_against):
GitGutterSettings.compare_against = new_compare_against
settings = GitGutterSettings()
if not ST3:
plugin_loaded()