Skip to content

Commit 6cbbeb8

Browse files
Fix: Fixed issue where repository names lost casing and retained .git suffix (#17421)
1 parent 333577c commit 6cbbeb8

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/Files.App/Utils/Git/GitHelpers.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
using System.Net.Http;
88
using System.Net.Http.Json;
99
using System.Text;
10+
using System.Text.RegularExpressions;
1011

1112
namespace Files.App.Utils.Git
1213
{
13-
internal static class GitHelpers
14+
internal static partial class GitHelpers
1415
{
1516
private static readonly StatusCenterViewModel StatusCenterViewModel = Ioc.Default.GetRequiredService<StatusCenterViewModel>();
1617

@@ -864,20 +865,16 @@ private static bool IsAuthorizationException(Exception ex)
864865
/// <returns></returns>
865866
public static (string RepoUrl, string RepoName) GetRepoInfo(string url)
866867
{
867-
// Remove protocol and normalize slashes
868-
var normalizedUrl = url.ToLower().Replace("https://", "").Replace("http://", "").Replace("//", "");
868+
var match = GitHubRepositoryRegex().Match(url);
869869

870-
string[] parts = normalizedUrl.Split('/');
870+
if (!match.Success)
871+
return (string.Empty, string.Empty);
871872

872-
// Check if the URL includes an organization or user name + repo (github.com/username/repo)
873-
if (parts.Length >= 3 && parts[0] == "github.com")
874-
{
875-
// Construct the repo URL from the first three parts
876-
string repoUrl = $"https://{parts[0]}/{parts[1]}/{parts[2]}";
877-
return (repoUrl, parts[2]);
878-
}
873+
string userOrOrg = match.Groups["user"].Value;
874+
string repoName = match.Groups["repo"].Value;
879875

880-
return (string.Empty, string.Empty);
876+
string repoUrl = $"https://github.com/{userOrOrg}/{repoName}";
877+
return (repoUrl, repoName);
881878
}
882879

883880
/// <summary>
@@ -887,7 +884,7 @@ public static (string RepoUrl, string RepoName) GetRepoInfo(string url)
887884
/// <returns>True if the URL is a valid GitHub URL; otherwise, false.</returns>
888885
public static bool IsValidRepoUrl(string url)
889886
{
890-
return !string.IsNullOrWhiteSpace(url) && url.Contains("github.com");
887+
return GitHubRepositoryRegex().IsMatch(url);
891888
}
892889

893890
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
945942
banner.CancellationToken.IsCancellationRequested ? ReturnResult.Cancelled :
946943
ReturnResult.Failed);
947944
}
945+
946+
[GeneratedRegex(@"^(?:https?:\/\/)?(?:www\.)?github\.com\/(?<user>[^\/]+)\/(?<repo>[^\/]+?)(?=\.git|\/|$)(?:\.git)?(?:\/)?", RegexOptions.IgnoreCase)]
947+
private static partial Regex GitHubRepositoryRegex();
948948
}
949949
}

0 commit comments

Comments
 (0)