From fba4c334c9248e2a5b24c6d9b625668f2c4748ba Mon Sep 17 00:00:00 2001 From: DryByte <93609026+DryByte@users.noreply.github.com> Date: Sun, 5 Nov 2023 12:10:40 -0300 Subject: [PATCH 1/9] Git repository button --- DiscordRichPresence.sublime-settings | 6 ++++++ drp.py | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/DiscordRichPresence.sublime-settings b/DiscordRichPresence.sublime-settings index 55da090..9c71dd9 100644 --- a/DiscordRichPresence.sublime-settings +++ b/DiscordRichPresence.sublime-settings @@ -41,6 +41,12 @@ // Use the language icon as the big icon, overrides 'small_icon' "big_icon": true, + // Show button for opening git repository on browser + "git_repository_button": false, + + // Default message for git repository button (supports format) + "git_repository_message": "Git Repository", + // Defines the order of name sources that should be used for the project name in format stings, if available. // // "project_file_name" - The name of the .sublime-project file. diff --git a/drp.py b/drp.py index ce3f425..4879019 100644 --- a/drp.py +++ b/drp.py @@ -2,6 +2,7 @@ import logging import os import time +import re from time import mktime import sublime @@ -230,6 +231,13 @@ def handle_activity(view, is_write=False, idle=False): if settings.get('show_elapsed_time'): act['timestamps'] = {'start': stamp} + if settings.get('git_repository_button'): + git_url = get_git_url(window) + git_btn_format = settings.get('git_repository_message') + + if git_btn_format and git_url is not None: + act['buttons'] = [{'label': git_btn_format.format(**format_dict), 'url': git_url}] + logger.info(window.folders()) try: ipc.set_activity(act) @@ -256,6 +264,20 @@ def handle_error(exc, retry=True): sublime.set_timeout_async(connect_background, 0) +def get_git_url(window): + for folder in window.folders(): + f = open(folder+"/.git/config", "r") + if (f): + filteredConfig = ''.join(f.read().split()) + ma = re.search("\[remote\"origin\"\]url=(.*)\.git", filteredConfig) + if ma is None: + continue + + return ma.group(1) + + return None + + def get_project_name(window, current_file): sources = settings.get("project_name", []) for source in sources: From cf21ac0757a7b2389877231c12c0b992f71dde44 Mon Sep 17 00:00:00 2001 From: DryByte <93609026+DryByte@users.noreply.github.com> Date: Sun, 5 Nov 2023 12:19:45 -0300 Subject: [PATCH 2/9] check if git path exists --- drp.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/drp.py b/drp.py index 4879019..efd99a3 100644 --- a/drp.py +++ b/drp.py @@ -266,8 +266,10 @@ def handle_error(exc, retry=True): def get_git_url(window): for folder in window.folders(): - f = open(folder+"/.git/config", "r") - if (f): + gitcfg_path = folder+"/.git/config" + + if (os.path.exists(gitcfg_path)): + f = open(gitcfg_path, "r") filteredConfig = ''.join(f.read().split()) ma = re.search("\[remote\"origin\"\]url=(.*)\.git", filteredConfig) if ma is None: From b7ad73960f392fcae185f2e80b6a5f3a032e3295 Mon Sep 17 00:00:00 2001 From: Snazzah <7025343+Snazzah@users.noreply.github.com> Date: Sun, 5 Nov 2023 15:56:26 -0600 Subject: [PATCH 3/9] Update DiscordRichPresence.sublime-settings --- DiscordRichPresence.sublime-settings | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DiscordRichPresence.sublime-settings b/DiscordRichPresence.sublime-settings index 9c71dd9..c275fd3 100644 --- a/DiscordRichPresence.sublime-settings +++ b/DiscordRichPresence.sublime-settings @@ -45,7 +45,7 @@ "git_repository_button": false, // Default message for git repository button (supports format) - "git_repository_message": "Git Repository", + "git_repository_message": "Open Repository", // Defines the order of name sources that should be used for the project name in format stings, if available. // From 35a953a9e4e828be95d3394959c736a1627c3d41 Mon Sep 17 00:00:00 2001 From: DryByte <93609026+DryByte@users.noreply.github.com> Date: Mon, 6 Nov 2023 22:25:29 -0300 Subject: [PATCH 4/9] use git cli for getting url --- drp.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/drp.py b/drp.py index efd99a3..f989d5c 100644 --- a/drp.py +++ b/drp.py @@ -3,6 +3,7 @@ import os import time import re +import subprocess from time import mktime import sublime @@ -265,19 +266,18 @@ def handle_error(exc, retry=True): def get_git_url(window): - for folder in window.folders(): - gitcfg_path = folder+"/.git/config" + subl_variables = window.extract_variables() + url = None - if (os.path.exists(gitcfg_path)): - f = open(gitcfg_path, "r") - filteredConfig = ''.join(f.read().split()) - ma = re.search("\[remote\"origin\"\]url=(.*)\.git", filteredConfig) - if ma is None: - continue - - return ma.group(1) + try: + url = subprocess.check_output(["git", "-C", subl_variables["folder"], "remote", "get-url", "origin"], universal_newlines=True) + except: + pass + #get_git_url_from_config(window) - return None + if url is not None: + url = url.replace(".git\n", "") + return url def get_project_name(window, current_file): From f9dad1c506b8121b649dd4cf69095021531b55d6 Mon Sep 17 00:00:00 2001 From: DryByte <93609026+DryByte@users.noreply.github.com> Date: Tue, 7 Nov 2023 07:18:59 -0300 Subject: [PATCH 5/9] add git config parser as fallback to cli --- drp.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/drp.py b/drp.py index f989d5c..732246a 100644 --- a/drp.py +++ b/drp.py @@ -265,19 +265,65 @@ def handle_error(exc, retry=True): sublime.set_timeout_async(connect_background, 0) +def git_config_parser(path): + obj = dict() + with open(path) as cfg: + lines = cfg.read().split("\n") + current_section = None + + for line in lines: + # remove comments and spaces + line = re.sub(" |;(.*)|#(.*)", "", line) + if not line: + continue + + if line.startswith("["): + res = re.search('"(.*)"', line) + if res is not None: + sec_name = re.sub('\[|"(.*)"|\]', "", line) + subsec_name = res.group(1) + if sec_name not in obj: + obj[sec_name] = {} + + obj[sec_name][subsec_name] = {} + current_section = [sec_name, subsec_name] + else: + sec_name = re.sub("\[|\]", "", line) + obj[sec_name] = {} + current_section = [sec_name] + + else: + parts = re.sub("\t|\0", "",line).split("=") + if len(current_section) < 2: + obj[current_section[0]][parts[0]] = parts[1] + else: + obj[current_section[0]][current_section[1]][parts[0]] = parts[1] + + return obj + + +def get_git_url_from_config(folder): + gitcfg_path = folder+"/.git/config" + if os.path.exists(gitcfg_path): + cfg = git_config_parser(gitcfg_path) + return cfg["remote"]["origin"]["url"] + + return None + + def get_git_url(window): - subl_variables = window.extract_variables() - url = None + for folder in window.folders(): + url = None + try: + url = subprocess.check_output(["git", "-C", folder, "remote", "get-url", "origin"], universal_newlines=True) + except: + url = get_git_url_from_config(folder) - try: - url = subprocess.check_output(["git", "-C", subl_variables["folder"], "remote", "get-url", "origin"], universal_newlines=True) - except: - pass - #get_git_url_from_config(window) + if url is not None: + url = re.sub("\.git\n?$", "", url) + return url - if url is not None: - url = url.replace(".git\n", "") - return url + return None def get_project_name(window, current_file): From fe588c5fa3d18b021643d55a05d067084e25f123 Mon Sep 17 00:00:00 2001 From: DryByte <93609026+DryByte@users.noreply.github.com> Date: Wed, 8 Nov 2023 07:08:32 -0300 Subject: [PATCH 6/9] parse https and ssh urls --- drp.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/drp.py b/drp.py index 732246a..608c052 100644 --- a/drp.py +++ b/drp.py @@ -311,6 +311,19 @@ def get_git_url_from_config(folder): return None +def parse_git_url(url): + url = re.sub("\.git\n?$", "", url) + if url.startswith("https"): + return url + + elif url.startswith("git@") or url.startswith("ssh"): + url = url.replace(":", "/") + return re.sub("git@|ssh///", "https://", url) + + else: + return None + + def get_git_url(window): for folder in window.folders(): url = None @@ -320,7 +333,7 @@ def get_git_url(window): url = get_git_url_from_config(folder) if url is not None: - url = re.sub("\.git\n?$", "", url) + url = parse_git_url(url) return url return None From 0ef608e240975c1ec2393e8931c53b05ed0f6121 Mon Sep 17 00:00:00 2001 From: DryByte <93609026+DryByte@users.noreply.github.com> Date: Sat, 11 Nov 2023 17:08:44 -0300 Subject: [PATCH 7/9] check if remote origin exists in git config --- drp.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drp.py b/drp.py index 608c052..1e9f5ce 100644 --- a/drp.py +++ b/drp.py @@ -306,7 +306,8 @@ def get_git_url_from_config(folder): gitcfg_path = folder+"/.git/config" if os.path.exists(gitcfg_path): cfg = git_config_parser(gitcfg_path) - return cfg["remote"]["origin"]["url"] + if "remote" in cfg and "origin" in cfg["remote"]: + return cfg["remote"]["origin"]["url"] return None From 0e33b8a0430e33d4c66cccb15e99d9bdd7bacf88 Mon Sep 17 00:00:00 2001 From: Snazzah Date: Tue, 14 Nov 2023 10:44:14 -0600 Subject: [PATCH 8/9] Don't create process windows --- drp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drp.py b/drp.py index 1e9f5ce..45a84d2 100644 --- a/drp.py +++ b/drp.py @@ -329,7 +329,7 @@ def get_git_url(window): for folder in window.folders(): url = None try: - url = subprocess.check_output(["git", "-C", folder, "remote", "get-url", "origin"], universal_newlines=True) + url = subprocess.check_output(["git", "-C", folder, "remote", "get-url", "origin"], universal_newlines=True, creationflags=subprocess.CREATE_NO_WINDOW) except: url = get_git_url_from_config(folder) From b46d5661d47a9b49ee7a1f166edbc70a5e9921c4 Mon Sep 17 00:00:00 2001 From: Snazzah Date: Tue, 14 Nov 2023 10:52:21 -0600 Subject: [PATCH 9/9] Only use git button from current file --- drp.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/drp.py b/drp.py index 45a84d2..712f7b2 100644 --- a/drp.py +++ b/drp.py @@ -233,7 +233,7 @@ def handle_activity(view, is_write=False, idle=False): act['timestamps'] = {'start': stamp} if settings.get('git_repository_button'): - git_url = get_git_url(window) + git_url = get_git_url(entity) git_btn_format = settings.get('git_repository_message') if git_btn_format and git_url is not None: @@ -325,17 +325,17 @@ def parse_git_url(url): return None -def get_git_url(window): - for folder in window.folders(): - url = None - try: - url = subprocess.check_output(["git", "-C", folder, "remote", "get-url", "origin"], universal_newlines=True, creationflags=subprocess.CREATE_NO_WINDOW) - except: - url = get_git_url_from_config(folder) +def get_git_url(entity): + folder = os.path.dirname(entity) + url = None + try: + url = subprocess.check_output(["git", "-C", folder, "remote", "get-url", "origin"], universal_newlines=True, creationflags=subprocess.CREATE_NO_WINDOW) + except: + url = get_git_url_from_config(folder) - if url is not None: - url = parse_git_url(url) - return url + if url is not None: + url = parse_git_url(url) + return url return None