Skip to content

Commit

Permalink
Merge pull request #712 from divmain/randy3k/git_root
Browse files Browse the repository at this point in the history
Enhancement: find git root
  • Loading branch information
divmain committed Jul 20, 2017
2 parents 5bdb166 + 7fc41af commit c2d42fb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
36 changes: 11 additions & 25 deletions core/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,14 @@ def run(self):
sublime.set_timeout_async(self.run_async, 0)

def run_async(self):
open_folders = self.window.folders()
if open_folders:
suggested_git_root = open_folders[0]
else:
file_path = self.window.active_view().file_name()
if file_path:
suggested_git_root = os.path.dirname(file_path)
else:
suggested_git_root = ""
git_root = self.find_working_dir()

if suggested_git_root and os.path.exists(os.path.join(suggested_git_root, ".git")):
if git_root and os.path.exists(os.path.join(git_root, ".git")):
if sublime.ok_cancel_dialog(CONFIRM_REINITIALIZE):
self.on_done(suggested_git_root, re_init=True)
self.on_done(git_root, re_init=True)
return

self.window.show_input_panel(REPO_PATH_PROMPT, suggested_git_root, self.on_done, None, None)
self.window.show_input_panel(REPO_PATH_PROMPT, git_root, self.on_done, None, None)

def on_done(self, path, re_init=False):
self.git("init", working_dir=path)
Expand Down Expand Up @@ -106,21 +98,15 @@ def on_enter_url(self, url):
self.window.show_input_panel(REPO_PATH_PROMPT, self.suggested_git_root, self.on_enter_directory, None, None)

def find_suggested_git_root(self):
open_folders = self.window.folders()
folder = self.find_working_dir()
project = self.project_name_from_url(self.git_url)
if open_folders:
if not os.path.exists(os.path.join(open_folders[0], ".git")):
return "{}/{}".format(open_folders[0], project)
else:
folder = os.path.realpath(os.path.join(open_folders[0], ".."))
return "{}/{}".format(folder, project)

else:
file_path = self.window.active_view().file_name()
if file_path:
return "{}/{}".format(os.path.dirname(file_path), project)
if folder:
if not os.path.exists(os.path.join(folder, project, ".git")):
return os.path.join(folder, project)
else:
return ""
parent = os.path.realpath(os.path.join(folder, ".."))
return os.path.join(parent, project)
return ""

def on_enter_directory(self, path):
self.suggested_git_root = path
Expand Down
34 changes: 24 additions & 10 deletions core/git_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,23 @@ def git_binary_path(self):

return git_path

def find_working_dir(self):
view = self.window.active_view() if hasattr(self, "window") else self.view
window = view.window()
if not window:
return
open_folders = view.window().folders()
if open_folders:
working_dir = open_folders[0]
else:
file_path = view.file_name()
if file_path:
working_dir = os.path.dirname(file_path)
else:
working_dir = None

return working_dir

@property
def repo_path(self):
return self._repo_path()
Expand Down Expand Up @@ -240,15 +257,9 @@ def invalid_repo():
repo_path = view.settings().get("git_savvy.repo_path")

if not repo_path or not os.path.exists(repo_path):
file_path = self.file_path
file_dir = os.path.dirname(file_path) if file_path else None
working_dir = file_path and os.path.isdir(file_dir) and file_dir

if not working_dir:
window_folders = sublime.active_window().folders()
if not window_folders or not os.path.isdir(window_folders[0]):
return invalid_repo()
working_dir = window_folders[0]
working_dir = self.find_working_dir()
if not working_dir or not os.path.isdir(working_dir):
return invalid_repo()

stdout = self.git(
"rev-parse",
Expand All @@ -262,7 +273,10 @@ def invalid_repo():
if not repo_path:
return invalid_repo()

view.settings().set("git_savvy.repo_path", os.path.realpath(repo_path))
if view.file_name() and working_dir in view.file_name():
# only set repo_path for file in the working directory
# it allows a file living outside the repo to be edited in the current window
view.settings().set("git_savvy.repo_path", os.path.realpath(repo_path))

return os.path.realpath(repo_path) if repo_path else repo_path

Expand Down

0 comments on commit c2d42fb

Please sign in to comment.