Skip to content

Commit f3bb0c3

Browse files
authored
Merge pull request #1 from zillding/support-climb-dirs
Change the way of finding git root to walking up the filesystem
2 parents f4d32a1 + 4a9a268 commit f3bb0c3

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

SublimeGitUp.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
import sublime_plugin
66

77

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)
1013

1114

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
1720

1821

1922
class GitupOpenCommand(sublime_plugin.WindowCommand):
@@ -24,13 +27,11 @@ def is_enabled(self):
2427
def get_path(self):
2528
filepath = self.window.active_view().file_name()
2629
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])
3134

32-
elif self.window.folders() and has_git(self.window.folders()[0]):
33-
return self.window.folders()[0]
3435
else:
3536
sublime.status_message('No place to open GitUp to')
3637
return False
@@ -50,17 +51,15 @@ def run(self, *args):
5051

5152
class SideBarGitupCommand(sublime_plugin.WindowCommand):
5253

53-
def is_visible(self, paths):
54-
for path in paths:
55-
return os.path.isdir(path)
56-
5754
def is_enabled(self, paths):
5855
for path in paths:
59-
return has_git(path)
56+
if find_git_root(path):
57+
return True
58+
return False
6059

6160
def get_path(self, paths):
6261
try:
63-
return paths[0]
62+
return find_git_root(paths[0])
6463
except IndexError:
6564
return self.window.active_view().file_name()
6665

0 commit comments

Comments
 (0)