diff --git a/src/Files.App/Utils/Git/GitHelpers.cs b/src/Files.App/Utils/Git/GitHelpers.cs index 340e9395f89c..d41357f90495 100644 --- a/src/Files.App/Utils/Git/GitHelpers.cs +++ b/src/Files.App/Utils/Git/GitHelpers.cs @@ -7,10 +7,11 @@ using System.Net.Http; using System.Net.Http.Json; using System.Text; +using System.Text.RegularExpressions; namespace Files.App.Utils.Git { - internal static class GitHelpers + internal static partial class GitHelpers { private static readonly StatusCenterViewModel StatusCenterViewModel = Ioc.Default.GetRequiredService(); @@ -864,20 +865,16 @@ private static bool IsAuthorizationException(Exception ex) /// public static (string RepoUrl, string RepoName) GetRepoInfo(string url) { - // Remove protocol and normalize slashes - var normalizedUrl = url.ToLower().Replace("https://", "").Replace("http://", "").Replace("//", ""); + var match = GitHubRepositoryRegex().Match(url); - string[] parts = normalizedUrl.Split('/'); + if (!match.Success) + return (string.Empty, string.Empty); - // Check if the URL includes an organization or user name + repo (github.com/username/repo) - if (parts.Length >= 3 && parts[0] == "github.com") - { - // Construct the repo URL from the first three parts - string repoUrl = $"https://{parts[0]}/{parts[1]}/{parts[2]}"; - return (repoUrl, parts[2]); - } + string userOrOrg = match.Groups["user"].Value; + string repoName = match.Groups["repo"].Value; - return (string.Empty, string.Empty); + string repoUrl = $"https://github.com/{userOrOrg}/{repoName}"; + return (repoUrl, repoName); } /// @@ -887,7 +884,7 @@ public static (string RepoUrl, string RepoName) GetRepoInfo(string url) /// True if the URL is a valid GitHub URL; otherwise, false. public static bool IsValidRepoUrl(string url) { - return !string.IsNullOrWhiteSpace(url) && url.Contains("github.com"); + return GitHubRepositoryRegex().IsMatch(url); } public static async Task CloneRepoAsync(string repoUrl, string repoName, string targetDirectory) @@ -945,5 +942,8 @@ public static async Task CloneRepoAsync(string repoUrl, string repoName, string banner.CancellationToken.IsCancellationRequested ? ReturnResult.Cancelled : ReturnResult.Failed); } + + [GeneratedRegex(@"^(?:https?:\/\/)?(?:www\.)?github\.com\/(?[^\/]+)\/(?[^\/]+?)(?=\.git|\/|$)(?:\.git)?(?:\/)?", RegexOptions.IgnoreCase)] + private static partial Regex GitHubRepositoryRegex(); } }