-
Notifications
You must be signed in to change notification settings - Fork 7
/
Git GUI Clients.py
66 lines (49 loc) · 2.18 KB
/
Git GUI Clients.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
import sublime
import sublime_plugin
import os
import shutil
import subprocess
class GgcOpenCommand(sublime_plugin.WindowCommand):
def get_git_repository(self):
# Get the folder of the current view
dirs = [os.path.dirname(self.window.active_view().file_name())] if self.window.active_view() and self.window.active_view().file_name() else []
# Get get windows folders
dirs += [f for f in self.window.folders()]
# Detect folders of open views
dirs += [os.path.dirname(view.file_name()) for view in self.window.views() if view and view.file_name()]
# preserve order of folders, from
def customSet(seq):
seen = set()
seen_add = seen.add
return [ x for x in seq if not (x in seen or seen_add(x))]
# Check for git folder
for dir_path in list(customSet(dirs)):
# search for git folder
while dir_path:
if os.path.exists(os.path.join(dir_path, '.git')):
return dir_path
parent = os.path.realpath(os.path.join(dir_path, os.path.pardir))
if parent == dir_path:
break
dir_path = parent
def get_excecutable(self, cmd):
s = sublime.load_settings("Git GUI Clients.sublime-settings")
for excecutable in s.get(cmd):
if os.path.exists(excecutable):
return excecutable
# Fallback search on path
return shutil.which(os.path.basename(s.get(cmd)[0])) if s.get(cmd) else None
def is_enabled(self, cmd):
return True if self.get_excecutable(cmd) else False
def run(self, cmd):
# Get repository location and git gui client
excecutable = self.get_excecutable(cmd)
repository = self.get_git_repository()
if not excecutable:
print("Git GUI Clients: No GUI executable exists on the computer. Check path configs.")
return
if not repository:
print("Git GUI Clients: File/project is not in a Git repo.")
return
print("Git GUI Clients:", excecutable, repository)
subprocess.Popen(excecutable, cwd=repository, shell=True)