From cb33ee026f2bd403485898fda07ee42f8b99a5b5 Mon Sep 17 00:00:00 2001 From: Jason 'vanRijn' Kasper Date: Wed, 21 Jun 2017 23:12:47 -0400 Subject: [PATCH] Make get_open_folder_from_window reusable, then use it to try to find git root directory first time GitGotoDiff is called. --- git/__init__.py | 13 +++++++++---- git/diff.py | 6 +++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/git/__init__.py b/git/__init__.py index 00aa7905..44db9583 100644 --- a/git/__init__.py +++ b/git/__init__.py @@ -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) @@ -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 diff --git a/git/diff.py b/git/diff.py index 73627e75..af815c76 100644 --- a/git/diff.py +++ b/git/diff.py @@ -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): @@ -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