From a283332b1300cc43070c9b86cecf41f233f20353 Mon Sep 17 00:00:00 2001 From: Yaros Date: Tue, 2 Jul 2024 18:12:48 +0200 Subject: [PATCH] Detect GitHub branch name automatically from URL --- src/tabs/presets/SourcesDialog/PresetSource.js | 14 ++++++++++++++ src/tabs/presets/SourcesDialog/SourcePanel.js | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/src/tabs/presets/SourcesDialog/PresetSource.js b/src/tabs/presets/SourcesDialog/PresetSource.js index ead8760db8..a7b8a5edd9 100644 --- a/src/tabs/presets/SourcesDialog/PresetSource.js +++ b/src/tabs/presets/SourcesDialog/PresetSource.js @@ -9,4 +9,18 @@ export default class PresetSource { static isUrlGithubRepo(url) { return url.trim().toLowerCase().startsWith("https://github.com/"); } + + static containsBranchName(url) { + return url.includes("/tree/"); + } + + static getBranchName(url) { + const pattern = /https:\/\/github\.com\/[^\/]+\/[^\/]+\/tree\/([^\/]+)/; + const match = url.match(pattern); + if (match) { + return match[1]; + } else { + return null; + } + } } diff --git a/src/tabs/presets/SourcesDialog/SourcePanel.js b/src/tabs/presets/SourcesDialog/SourcePanel.js index 7bf118694d..b3162e5c8f 100644 --- a/src/tabs/presets/SourcesDialog/SourcePanel.js +++ b/src/tabs/presets/SourcesDialog/SourcePanel.js @@ -147,6 +147,10 @@ export default class SourcePanel { _onInputChange() { this._checkIfGithub(); + if (PresetSource.containsBranchName(this._domEditUrl.val())) { + this._domEditGitHubBranch.val(PresetSource.getBranchName(this._domEditUrl.val())); + this._domEditUrl.val(this._domEditUrl.val().split("/tree/")[0]); + } this._setIsSaved(false); }