|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2014 - 2017 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Git version control plugin. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import os |
|
13 |
|
14 from PyQt5.QtCore import QObject, QCoreApplication, QTranslator, QByteArray |
|
15 |
|
16 from E5Gui.E5Application import e5App |
|
17 |
|
18 import Preferences |
|
19 from Preferences.Shortcuts import readShortcuts |
|
20 |
|
21 from VcsPlugins.vcsGit.GitUtilities import getConfigPath |
|
22 |
|
23 import Utilities |
|
24 import UI.Info |
|
25 |
|
26 # Start-Of-Header |
|
27 name = "Git Plugin" |
|
28 author = "Detlev Offenbach <detlev@die-offenbachs.de>" |
|
29 autoactivate = False |
|
30 deactivateable = True |
|
31 version = UI.Info.VersionOnly |
|
32 pluginType = "version_control" |
|
33 pluginTypename = "Git" |
|
34 className = "VcsGitPlugin" |
|
35 packageName = "__core__" |
|
36 shortDescription = "Implements the Git version control interface." |
|
37 longDescription = \ |
|
38 """This plugin provides the Git version control interface.""" |
|
39 pyqtApi = 2 |
|
40 python2Compatible = True |
|
41 # End-Of-Header |
|
42 |
|
43 error = "" |
|
44 |
|
45 |
|
46 def exeDisplayData(): |
|
47 """ |
|
48 Public method to support the display of some executable info. |
|
49 |
|
50 @return dictionary containing the data to query the presence of |
|
51 the executable |
|
52 """ |
|
53 exe = 'git' |
|
54 if Utilities.isWindowsPlatform(): |
|
55 exe += '.exe' |
|
56 |
|
57 data = { |
|
58 "programEntry": True, |
|
59 "header": QCoreApplication.translate( |
|
60 "VcsGitPlugin", "Version Control - Git"), |
|
61 "exe": exe, |
|
62 "versionCommand": 'version', |
|
63 "versionStartsWith": 'git', |
|
64 "versionPosition": 2, |
|
65 "version": "", |
|
66 "versionCleanup": None, |
|
67 } |
|
68 |
|
69 return data |
|
70 |
|
71 |
|
72 def getVcsSystemIndicator(): |
|
73 """ |
|
74 Public function to get the indicators for this version control system. |
|
75 |
|
76 @return dictionary with indicator as key and a tuple with the vcs name |
|
77 (string) and vcs display string (string) |
|
78 """ |
|
79 global pluginTypename |
|
80 data = {} |
|
81 exe = 'git' |
|
82 if Utilities.isWindowsPlatform(): |
|
83 exe += '.exe' |
|
84 if Utilities.isinpath(exe): |
|
85 data[".git"] = (pluginTypename, displayString()) |
|
86 data["_git"] = (pluginTypename, displayString()) |
|
87 return data |
|
88 |
|
89 |
|
90 def displayString(): |
|
91 """ |
|
92 Public function to get the display string. |
|
93 |
|
94 @return display string (string) |
|
95 """ |
|
96 exe = 'git' |
|
97 if Utilities.isWindowsPlatform(): |
|
98 exe += '.exe' |
|
99 if Utilities.isinpath(exe): |
|
100 return QCoreApplication.translate('VcsGitPlugin', 'Git') |
|
101 else: |
|
102 return "" |
|
103 |
|
104 |
|
105 gitCfgPluginObject = None |
|
106 |
|
107 |
|
108 def createConfigurationPage(configDlg): |
|
109 """ |
|
110 Module function to create the configuration page. |
|
111 |
|
112 @param configDlg reference to the configuration dialog (QDialog) |
|
113 @return reference to the configuration page |
|
114 """ |
|
115 global gitCfgPluginObject |
|
116 from VcsPlugins.vcsGit.ConfigurationPage.GitPage import \ |
|
117 GitPage |
|
118 if gitCfgPluginObject is None: |
|
119 gitCfgPluginObject = VcsGitPlugin(None) |
|
120 page = GitPage(gitCfgPluginObject) |
|
121 return page |
|
122 |
|
123 |
|
124 def getConfigData(): |
|
125 """ |
|
126 Module function returning data as required by the configuration dialog. |
|
127 |
|
128 @return dictionary with key "zzz_gitPage" containing the relevant |
|
129 data |
|
130 """ |
|
131 return { |
|
132 "zzz_gitPage": |
|
133 [QCoreApplication.translate("VcsGitPlugin", "Git"), |
|
134 os.path.join("VcsPlugins", "vcsGit", "icons", |
|
135 "preferences-git.png"), |
|
136 createConfigurationPage, "vcsPage", None], |
|
137 } |
|
138 |
|
139 |
|
140 def prepareUninstall(): |
|
141 """ |
|
142 Module function to prepare for an uninstallation. |
|
143 """ |
|
144 if not e5App().getObject("PluginManager").isPluginLoaded( |
|
145 "PluginVcsGit"): |
|
146 Preferences.Prefs.settings.remove("Git") |
|
147 |
|
148 |
|
149 def clearPrivateData(): |
|
150 """ |
|
151 Module function to clear the private data of the plug-in. |
|
152 """ |
|
153 for key in ["RepositoryUrlHistory"]: |
|
154 VcsGitPlugin.setPreferences(key, []) |
|
155 |
|
156 |
|
157 class VcsGitPlugin(QObject): |
|
158 """ |
|
159 Class implementing the Git version control plugin. |
|
160 """ |
|
161 GitDefaults = { |
|
162 "StopLogOnCopy": True, # used in log browser |
|
163 "ShowAuthorColumns": True, # used in log browser |
|
164 "ShowCommitterColumns": True, # used in log browser |
|
165 "ShowCommitIdColumn": True, # used in log browser |
|
166 "ShowBranchesColumn": True, # used in log browser |
|
167 "ShowTagsColumn": True, # used in log browser |
|
168 "FindCopiesHarder": False, # used in log browser |
|
169 "LogLimit": 20, |
|
170 "LogSubjectColumnWidth": 30, |
|
171 "LogBrowserGeometry": QByteArray(), |
|
172 "LogBrowserSplitterStates": [QByteArray(), QByteArray(), |
|
173 QByteArray()], |
|
174 # mainSplitter, detailsSplitter, diffSplitter |
|
175 "StatusDialogGeometry": QByteArray(), |
|
176 "StatusDialogSplitterStates": [QByteArray(), QByteArray()], |
|
177 # vertical splitter, horizontal splitter |
|
178 "CommitMessages": 20, |
|
179 "Commits": [], |
|
180 "CommitIdLength": 10, |
|
181 "CleanupPatterns": "*.orig *.rej *~", |
|
182 "AggressiveGC": True, |
|
183 "RepositoryUrlHistory": [], |
|
184 } |
|
185 |
|
186 def __init__(self, ui): |
|
187 """ |
|
188 Constructor |
|
189 |
|
190 @param ui reference to the user interface object (UI.UserInterface) |
|
191 """ |
|
192 super(VcsGitPlugin, self).__init__(ui) |
|
193 self.__ui = ui |
|
194 |
|
195 self.__translator = None |
|
196 self.__loadTranslator() |
|
197 |
|
198 from VcsPlugins.vcsGit.ProjectHelper import GitProjectHelper |
|
199 self.__projectHelperObject = GitProjectHelper(None, None) |
|
200 try: |
|
201 e5App().registerPluginObject( |
|
202 pluginTypename, self.__projectHelperObject, pluginType) |
|
203 except KeyError: |
|
204 pass # ignore duplicate registration |
|
205 |
|
206 readShortcuts(pluginName=pluginTypename) |
|
207 |
|
208 def getProjectHelper(self): |
|
209 """ |
|
210 Public method to get a reference to the project helper object. |
|
211 |
|
212 @return reference to the project helper object |
|
213 """ |
|
214 return self.__projectHelperObject |
|
215 |
|
216 def initToolbar(self, ui, toolbarManager): |
|
217 """ |
|
218 Public slot to initialize the VCS toolbar. |
|
219 |
|
220 @param ui reference to the main window (UserInterface) |
|
221 @param toolbarManager reference to a toolbar manager object |
|
222 (E5ToolBarManager) |
|
223 """ |
|
224 if self.__projectHelperObject: |
|
225 self.__projectHelperObject.initToolbar(ui, toolbarManager) |
|
226 |
|
227 def activate(self): |
|
228 """ |
|
229 Public method to activate this plugin. |
|
230 |
|
231 @return tuple of reference to instantiated viewmanager and |
|
232 activation status (boolean) |
|
233 """ |
|
234 from VcsPlugins.vcsGit.git import Git |
|
235 self.__object = Git(self, self.__ui) |
|
236 |
|
237 tbData = self.__ui.getToolbar("vcs") |
|
238 if tbData: |
|
239 tb = tbData[1] |
|
240 tb.setVisible(False) |
|
241 tb.setEnabled(False) |
|
242 |
|
243 tbData = self.__ui.getToolbar("git") |
|
244 if tbData: |
|
245 tb = tbData[1] |
|
246 tb.setVisible(True) |
|
247 tb.setEnabled(True) |
|
248 |
|
249 return self.__object, True |
|
250 |
|
251 def deactivate(self): |
|
252 """ |
|
253 Public method to deactivate this plugin. |
|
254 """ |
|
255 self.__object = None |
|
256 |
|
257 tbData = self.__ui.getToolbar("git") |
|
258 if tbData: |
|
259 tb = tbData[1] |
|
260 tb.setVisible(False) |
|
261 tb.setEnabled(False) |
|
262 |
|
263 tbData = self.__ui.getToolbar("vcs") |
|
264 if tbData: |
|
265 tb = tbData[1] |
|
266 tb.setVisible(True) |
|
267 tb.setEnabled(True) |
|
268 |
|
269 @classmethod |
|
270 def getPreferences(cls, key): |
|
271 """ |
|
272 Class method to retrieve the various settings. |
|
273 |
|
274 @param key the key of the value to get |
|
275 @return the requested setting |
|
276 """ |
|
277 if key in ["StopLogOnCopy", "ShowReflogInfo", "ShowAuthorColumns", |
|
278 "ShowCommitterColumns", "ShowCommitIdColumn", |
|
279 "ShowBranchesColumn", "ShowTagsColumn", "FindCopiesHarder", |
|
280 "AggressiveGC"]: |
|
281 return Preferences.toBool(Preferences.Prefs.settings.value( |
|
282 "Git/" + key, cls.GitDefaults[key])) |
|
283 elif key in ["LogLimit", "CommitMessages", "CommitIdLength", |
|
284 "LogSubjectColumnWidth"]: |
|
285 return int(Preferences.Prefs.settings.value( |
|
286 "Git/" + key, cls.GitDefaults[key])) |
|
287 elif key in ["Commits", "RepositoryUrlHistory"]: |
|
288 return Preferences.toList(Preferences.Prefs.settings.value( |
|
289 "Git/" + key)) |
|
290 elif key in ["LogBrowserGeometry", "StatusDialogGeometry"]: |
|
291 v = Preferences.Prefs.settings.value("Git/" + key) |
|
292 if v is not None: |
|
293 return v |
|
294 else: |
|
295 return cls.GitDefaults[key] |
|
296 elif key in ["LogBrowserSplitterStates", "StatusDialogSplitterStates"]: |
|
297 states = Preferences.Prefs.settings.value("Git/" + key) |
|
298 if states is not None: |
|
299 return states |
|
300 else: |
|
301 return cls.GitDefaults[key] |
|
302 else: |
|
303 return Preferences.Prefs.settings.value( |
|
304 "Git/" + key, cls.GitDefaults[key]) |
|
305 |
|
306 @classmethod |
|
307 def setPreferences(cls, key, value): |
|
308 """ |
|
309 Class method to store the various settings. |
|
310 |
|
311 @param key the key of the setting to be set |
|
312 @param value the value to be set |
|
313 """ |
|
314 Preferences.Prefs.settings.setValue("Git/" + key, value) |
|
315 |
|
316 def getConfigPath(self): |
|
317 """ |
|
318 Public method to get the filename of the config file. |
|
319 |
|
320 @return filename of the config file (string) |
|
321 """ |
|
322 return getConfigPath() |
|
323 |
|
324 def prepareUninstall(self): |
|
325 """ |
|
326 Public method to prepare for an uninstallation. |
|
327 """ |
|
328 e5App().unregisterPluginObject(pluginTypename) |
|
329 |
|
330 def prepareUnload(self): |
|
331 """ |
|
332 Public method to prepare for an unload. |
|
333 """ |
|
334 if self.__projectHelperObject: |
|
335 self.__projectHelperObject.removeToolbar( |
|
336 self.__ui, e5App().getObject("ToolbarManager")) |
|
337 e5App().unregisterPluginObject(pluginTypename) |
|
338 |
|
339 def __loadTranslator(self): |
|
340 """ |
|
341 Private method to load the translation file. |
|
342 """ |
|
343 if self.__ui is not None: |
|
344 loc = self.__ui.getLocale() |
|
345 if loc and loc != "C": |
|
346 locale_dir = os.path.join( |
|
347 os.path.dirname(__file__), "vcsGit", "i18n") |
|
348 translation = "git_{0}".format(loc) |
|
349 translator = QTranslator(None) |
|
350 loaded = translator.load(translation, locale_dir) |
|
351 if loaded: |
|
352 self.__translator = translator |
|
353 e5App().installTranslator(self.__translator) |
|
354 else: |
|
355 print("Warning: translation file '{0}' could not be" |
|
356 " loaded.".format(translation)) |
|
357 print("Using default.") |
|
358 |
|
359 # |
|
360 # eflag: noqa = M801 |