-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSublimeGitUp.py
76 lines (57 loc) · 1.97 KB
/
SublimeGitUp.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
import os
import subprocess
import sublime
import sublime_plugin
def climb_dirs(start_dir):
right = True
while right:
yield start_dir
start_dir, right = os.path.split(start_dir)
# look for the git root by traversing up the dir
def find_git_root(path):
for folder in climb_dirs(path):
if os.path.exists(os.path.join(folder, '.git')):
return folder
class GitupOpenCommand(sublime_plugin.WindowCommand):
def is_enabled(self):
return True
def get_path(self):
filepath = self.window.active_view().file_name()
if filepath:
return find_git_root(os.path.dirname(filepath))
elif self.window.folders():
return find_git_root(self.window.folders()[0])
else:
sublime.status_message('No place to open GitUp to')
return False
def run(self, *args):
sublime.status_message('GitUp: running')
path = self.get_path()
if not path:
sublime.status_message('GitUp: No path')
return False
if os.path.isfile(path):
path = os.path.dirname(path)
app_path = '/Applications/GitUp.app'
subprocess.Popen(['open', '-a', app_path, path])
class SideBarGitupCommand(sublime_plugin.WindowCommand):
def is_enabled(self, paths):
for path in paths:
if find_git_root(path):
return True
return False
def get_path(self, paths):
try:
return find_git_root(paths[0])
except IndexError:
return self.window.active_view().file_name()
def run(self, paths):
sublime.status_message('GitUp: running')
path = self.get_path(paths)
if not path:
sublime.status_message('GitUp: No path')
return False
if os.path.isfile(path):
path = os.path.dirname(path)
app_path = '/Applications/GitUp.app'
subprocess.Popen(['open', '-a', app_path, path])