7
7
using System . Net . Http ;
8
8
using System . Net . Http . Json ;
9
9
using System . Text ;
10
+ using System . Text . RegularExpressions ;
10
11
11
12
namespace Files . App . Utils . Git
12
13
{
13
- internal static class GitHelpers
14
+ internal static partial class GitHelpers
14
15
{
15
16
private static readonly StatusCenterViewModel StatusCenterViewModel = Ioc . Default . GetRequiredService < StatusCenterViewModel > ( ) ;
16
17
@@ -864,20 +865,16 @@ private static bool IsAuthorizationException(Exception ex)
864
865
/// <returns></returns>
865
866
public static ( string RepoUrl , string RepoName ) GetRepoInfo ( string url )
866
867
{
867
- // Remove protocol and normalize slashes
868
- var normalizedUrl = url . ToLower ( ) . Replace ( "https://" , "" ) . Replace ( "http://" , "" ) . Replace ( "//" , "" ) ;
868
+ var match = GitHubRepositoryRegex ( ) . Match ( url ) ;
869
869
870
- string [ ] parts = normalizedUrl . Split ( '/' ) ;
870
+ if ( ! match . Success )
871
+ return ( string . Empty , string . Empty ) ;
871
872
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 ;
879
875
880
- return ( string . Empty , string . Empty ) ;
876
+ string repoUrl = $ "https://github.com/{ userOrOrg } /{ repoName } ";
877
+ return ( repoUrl , repoName ) ;
881
878
}
882
879
883
880
/// <summary>
@@ -887,7 +884,7 @@ public static (string RepoUrl, string RepoName) GetRepoInfo(string url)
887
884
/// <returns>True if the URL is a valid GitHub URL; otherwise, false.</returns>
888
885
public static bool IsValidRepoUrl ( string url )
889
886
{
890
- return ! string . IsNullOrWhiteSpace ( url ) && url . Contains ( "github.com" ) ;
887
+ return GitHubRepositoryRegex ( ) . IsMatch ( url ) ;
891
888
}
892
889
893
890
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
945
942
banner . CancellationToken . IsCancellationRequested ? ReturnResult . Cancelled :
946
943
ReturnResult . Failed ) ;
947
944
}
945
+
946
+ [ GeneratedRegex ( @"^(?:https?:\/\/)?(?:www\.)?github\.com\/(?<user>[^\/]+)\/(?<repo>[^\/]+?)(?=\.git|\/|$)(?:\.git)?(?:\/)?" , RegexOptions . IgnoreCase ) ]
947
+ private static partial Regex GitHubRepositoryRegex ( ) ;
948
948
}
949
949
}
0 commit comments