5
5
import sublime_plugin
6
6
7
7
8
- def has_git (path ):
9
- return os .path .exists (os .path .join (path , '.git' ))
8
+ def climb_dirs (start_dir ):
9
+ right = True
10
+ while right :
11
+ yield start_dir
12
+ start_dir , right = os .path .split (start_dir )
10
13
11
14
12
- def is_parent ( parent , child ):
13
- if os . path . commonprefix ([ parent , child ]) == parent :
14
- return True
15
- else :
16
- return False
15
+ # look for the git root by traversing up the dir
16
+ def find_git_root ( path ) :
17
+ for folder in climb_dirs ( path ):
18
+ if os . path . exists ( os . path . join ( folder , '.git' )) :
19
+ return folder
17
20
18
21
19
22
class GitupOpenCommand (sublime_plugin .WindowCommand ):
@@ -24,13 +27,11 @@ def is_enabled(self):
24
27
def get_path (self ):
25
28
filepath = self .window .active_view ().file_name ()
26
29
if filepath :
27
- # look for the git root in the sidebar root folders
28
- for folder in self . window . folders ():
29
- if is_parent ( folder , filepath ) and has_git ( folder ):
30
- return folder
30
+ return find_git_root ( os . path . dirname ( filepath ))
31
+
32
+ elif self . window . folders ( ):
33
+ return find_git_root ( self . window . folders ()[ 0 ])
31
34
32
- elif self .window .folders () and has_git (self .window .folders ()[0 ]):
33
- return self .window .folders ()[0 ]
34
35
else :
35
36
sublime .status_message ('No place to open GitUp to' )
36
37
return False
@@ -50,17 +51,15 @@ def run(self, *args):
50
51
51
52
class SideBarGitupCommand (sublime_plugin .WindowCommand ):
52
53
53
- def is_visible (self , paths ):
54
- for path in paths :
55
- return os .path .isdir (path )
56
-
57
54
def is_enabled (self , paths ):
58
55
for path in paths :
59
- return has_git (path )
56
+ if find_git_root (path ):
57
+ return True
58
+ return False
60
59
61
60
def get_path (self , paths ):
62
61
try :
63
- return paths [0 ]
62
+ return find_git_root ( paths [0 ])
64
63
except IndexError :
65
64
return self .window .active_view ().file_name ()
66
65
0 commit comments