diff --git a/GitHub.sublime-settings b/GitHub.sublime-settings index 49c35a9..bb4a3ee 100644 --- a/GitHub.sublime-settings +++ b/GitHub.sublime-settings @@ -45,5 +45,6 @@ // The default branch to use when issuing Copy Remote URL to Clipboard, Open Remote URL in Browser, View, Blame, // History, or Edit commands + // Set to falsey values to fetch the current repo's default branch "default_branch": "main", } diff --git a/sublime_github.py b/sublime_github.py index b228fa7..5ac5ddc 100644 --- a/sublime_github.py +++ b/sublime_github.py @@ -9,6 +9,7 @@ import plistlib import sublime import sublime_plugin +import subprocess sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from github import GitHubApi @@ -430,7 +431,32 @@ class RemoteUrlCommand(git.GitTextCommand): def run(self, edit): if self.branch == "default": self.settings = sublime.load_settings("GitHub.sublime-settings") - branch = self.settings.get("default_branch") + default_branch_setting = self.settings.get("default_branch") + + if default_branch_setting: + branch = default_branch_setting + else: + # Get the path of the currently opened file + file_path = self.view.window().active_view().file_name() + + # Get the list of folders in the project + folders = self.view.window().folders() + + top_level_folder = None + for folder in folders: + if file_path.startswith(folder): + top_level_folder = folder + break + + branch = ( + subprocess.check_output( + "git -C %s symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'"%(top_level_folder), + stderr=subprocess.STDOUT, + shell=True, + ) + .decode("utf-8") + .strip() + ) else: # Get the current remote branch--useful whether we want to link directly to that # branch or to the branch's HEAD.