forked from csch0/SublimeText-Git-GUI-Clients
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGit GUI Clients.py
45 lines (34 loc) · 1.55 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
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()]
# Check for git folder
for dir_path in list(set(dirs)):
git_dir = os.path.join(dir_path, '.git')
if os.path.exists(git_dir):
return dir_path
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 self.get_excecutable(cmd) != None
def run(self, cmd):
# Get repository location and git gui client
excecutable = self.get_excecutable(cmd)
repository = self.get_git_repository()
if repository and excecutable:
print("Git GUI Clients:", excecutable, repository)
p = subprocess.Popen(excecutable, cwd=repository, shell=True)