77using System . Net . Http ;
88using System . Net . Http . Json ;
99using System . Text ;
10+ using System . Text . RegularExpressions ;
1011
1112namespace 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