Skip to content

Commit a0ee9b5

Browse files
authored
Merge pull request #524 from vanrijn/try-to-find-git-root-harder-the-first-time
Try harder to get the git root directory the first time GitGotoDiff is called
2 parents 7f6dee4 + cb33ee0 commit a0ee9b5

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

git/__init__.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ def git_root_exist(directory):
8181
return git_root(directory)
8282

8383

84+
# try to get an open folder from the window
85+
def get_open_folder_from_window(window):
86+
try: # handle case with no open folder
87+
return window.folders()[0]
88+
except IndexError:
89+
return ''
90+
91+
8492
def view_contents(view):
8593
region = sublime.Region(0, view.size())
8694
return view.substr(region)
@@ -392,10 +400,7 @@ def get_working_dir(self):
392400
file_name = self.active_file_path()
393401
if file_name:
394402
return os.path.realpath(os.path.dirname(file_name))
395-
try: # handle case with no open folder
396-
return self.window.folders()[0]
397-
except IndexError:
398-
return ''
403+
return get_open_folder_from_window(self.window)
399404

400405
def get_window(self):
401406
return self.window

git/diff.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sublime_plugin
55
import os
66
import re
7-
from . import GitTextCommand, GitWindowCommand, do_when, goto_xy
7+
from . import GitTextCommand, GitWindowCommand, do_when, goto_xy, git_root, get_open_folder_from_window
88

99

1010
class GitDiff (object):
@@ -99,6 +99,10 @@ def run(self, edit):
9999
self.goto_line = int(hunk_start_line) + line_offset - 1
100100

101101
git_root_dir = v.settings().get("git_root_dir")
102+
# See if we can get the git root directory if we haven't saved it yet
103+
if not git_root_dir:
104+
working_dir = get_open_folder_from_window(v.window())
105+
git_root_dir = git_root(working_dir) if working_dir else None
102106

103107
# Sanity check and see if the file we're going to try to open even
104108
# exists. If it does not, prompt the user for the correct base directory

0 commit comments

Comments
 (0)