55 "fmt"
66 "io"
77 "regexp"
8+ "strings"
89
910 "github.com/go-git/go-git/v5"
1011 "github.com/go-git/go-git/v5/plumbing/object"
@@ -40,12 +41,14 @@ type repository struct {
4041
4142// Metadata contains repository metadata.
4243type Metadata struct {
43- Path string
44- Protocol string
45- Provider string
46- Name string
47- Branch string
48- CommitHash string
44+ DirPath string
45+ Protocol string
46+ Provider string
47+ UserAccount string
48+ RemoteURL string
49+ RepoName string
50+ Branch string
51+ CommitHash string
4952}
5053
5154// New instantiates new repository.
@@ -59,7 +62,7 @@ func New(projectPath string) (Repository, error) {
5962 return nil , err
6063 }
6164
62- return & repository {client : client , Metadata : & Metadata {Path : projectPath }}, nil
65+ return & repository {client : client , Metadata : & Metadata {DirPath : projectPath }}, nil
6366}
6467
6568// GetMetadataFromRemoteURL implements repository.Repository interface.
@@ -75,19 +78,8 @@ func (r *repository) GetMetadataFromRemoteURL() (*Metadata, error) {
7578 return nil , ErrGitRemoteURLNotFound
7679 }
7780
78- remoteURL := remoteURLs [0 ]
79-
80- re := regexp .MustCompile (`(?P<Protocol>git@|http(s)?:\/\/)(.+@)*(?P<Provider>[\w\d\.-]+)(:[\d]+){0,1}(\/scm)?\/*(?P<Name>.*)` )
81- matches := re .FindStringSubmatch (remoteURL )
82-
83- protocolRe := regexp .MustCompile (`[^\w]` )
84- protocol := protocolRe .ReplaceAllString (matches [re .SubexpIndex ("Protocol" )], "" )
85-
86- providerRe := regexp .MustCompile (`^([\w\d-]+)([\.\w\d]*)` )
87- provider := providerRe .FindStringSubmatch (matches [re .SubexpIndex ("Provider" )])
88-
89- nameRe := regexp .MustCompile (`\/(.*)\.git$` )
90- name := nameRe .FindStringSubmatch (matches [re .SubexpIndex ("Name" )])
81+ // retrieves all possible information from git remote url.
82+ getMetadataFromRemoteURL (r .Metadata , remoteURLs [0 ])
9183
9284 ref , err := r .client .Head ()
9385 if err != nil {
@@ -97,15 +89,41 @@ func (r *repository) GetMetadataFromRemoteURL() (*Metadata, error) {
9789 branchRe := regexp .MustCompile (`([^\/]+$)` )
9890 branch := branchRe .FindString (ref .Name ().String ())
9991
100- r .Metadata .Protocol = protocol
101- r .Metadata .Provider = provider [1 ]
102- r .Metadata .Name = name [1 ]
92+ r .Metadata .RemoteURL = remoteURLs [0 ]
10393 r .Metadata .Branch = branch
10494 r .Metadata .CommitHash = ref .Hash ().String ()
10595
10696 return r .Metadata , nil
10797}
10898
99+ func getMetadataFromRemoteURL (metadata * Metadata , gitRemoteURL string ) {
100+ re := regexp .MustCompile (`(?P<Protocol>git@|http(s)?:\/\/)(.+@)*(?P<Provider>[\w\d\.-]+)(:[\d]+){0,1}(\/scm)?\/*(?P<Name>.*)` )
101+ matches := re .FindStringSubmatch (gitRemoteURL )
102+
103+ protocolRe := regexp .MustCompile (`[^\w]` )
104+ protocol := protocolRe .ReplaceAllString (matches [re .SubexpIndex ("Protocol" )], "" )
105+
106+ providerRe := regexp .MustCompile (`^([\w\d-]+)([\.\w\d]*)` )
107+ provider := providerRe .FindStringSubmatch (matches [re .SubexpIndex ("Provider" )])
108+
109+ // use url parser to extract username and repository name by extracting the last 2 parts of the url
110+ urlParts := strings .Split (gitRemoteURL , "/" )
111+
112+ userAccount := urlParts [len (urlParts )- 2 ]
113+ // remove the "[email protected] :" from user account part if exists 114+ userAccountParts := strings .Split (userAccount , ":" )
115+ if len (userAccountParts ) > 1 {
116+ userAccount = userAccountParts [1 ]
117+ }
118+
119+ repoName := strings .TrimSuffix (urlParts [len (urlParts )- 1 ], ".git" )
120+
121+ metadata .Protocol = protocol
122+ metadata .Provider = provider [1 ]
123+ metadata .UserAccount = userAccount
124+ metadata .RepoName = repoName
125+ }
126+
109127// ListFiles implements repository.Repository interface.
110128func (r * repository ) ListFiles () ([]string , error ) {
111129 filepaths := make ([]string , 0 )
0 commit comments