From 263cd0cef851150db80e0e547ec38c1adad034bc Mon Sep 17 00:00:00 2001 From: Dat Le-Tien Date: Fri, 10 Nov 2023 17:14:59 +0700 Subject: [PATCH 1/2] Add option to fetch upstream default branch --- GitHub.sublime-settings | 1 + sublime_github.py | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) 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..ab9a9ec 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,20 @@ 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: + branch = ( + subprocess.check_output( + "git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'", + 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. From 02775672968d23c9bcb00fb612b6480a2d78a9d8 Mon Sep 17 00:00:00 2001 From: Dat Le-Tien Date: Fri, 10 Nov 2023 18:32:04 +0700 Subject: [PATCH 2/2] Fix issue Switch project has not reloaded working_dir yet --- sublime_github.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/sublime_github.py b/sublime_github.py index ab9a9ec..5ac5ddc 100644 --- a/sublime_github.py +++ b/sublime_github.py @@ -436,9 +436,21 @@ def run(self, edit): 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 symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'", + "git -C %s symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'"%(top_level_folder), stderr=subprocess.STDOUT, shell=True, )