Skip to content

Commit

Permalink
Merge pull request #524 from vanrijn/try-to-find-git-root-harder-the-…
Browse files Browse the repository at this point in the history
…first-time

Try harder to get the git root directory the first time GitGotoDiff is called
  • Loading branch information
kemayo authored Aug 25, 2017
2 parents 7f6dee4 + cb33ee0 commit a0ee9b5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
13 changes: 9 additions & 4 deletions git/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ def git_root_exist(directory):
return git_root(directory)


# try to get an open folder from the window
def get_open_folder_from_window(window):
try: # handle case with no open folder
return window.folders()[0]
except IndexError:
return ''


def view_contents(view):
region = sublime.Region(0, view.size())
return view.substr(region)
Expand Down Expand Up @@ -392,10 +400,7 @@ def get_working_dir(self):
file_name = self.active_file_path()
if file_name:
return os.path.realpath(os.path.dirname(file_name))
try: # handle case with no open folder
return self.window.folders()[0]
except IndexError:
return ''
return get_open_folder_from_window(self.window)

def get_window(self):
return self.window
Expand Down
6 changes: 5 additions & 1 deletion git/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sublime_plugin
import os
import re
from . import GitTextCommand, GitWindowCommand, do_when, goto_xy
from . import GitTextCommand, GitWindowCommand, do_when, goto_xy, git_root, get_open_folder_from_window


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

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

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

0 comments on commit a0ee9b5

Please sign in to comment.