@@ -6,57 +6,56 @@ package git
66
77import (
88 "context"
9+ "path"
910 "strings"
1011
1112 giturl "code.gitea.io/gitea/modules/git/url"
1213)
1314
1415// CommitSubmoduleFile represents a file with submodule type.
1516type CommitSubmoduleFile struct {
16- refURL string
17- refID string
17+ fullPath string
18+ refURL string
19+ refID string
1820
19- parsed bool
20- targetRepoLink string
21+ parsed bool
22+ parsedTargetLink string
2123}
2224
2325// NewCommitSubmoduleFile create a new submodule file
24- func NewCommitSubmoduleFile (refURL , refID string ) * CommitSubmoduleFile {
25- return & CommitSubmoduleFile {refURL : refURL , refID : refID }
26+ func NewCommitSubmoduleFile (fullPath , refURL , refID string ) * CommitSubmoduleFile {
27+ return & CommitSubmoduleFile {fullPath : fullPath , refURL : refURL , refID : refID }
2628}
2729
2830func (sf * CommitSubmoduleFile ) RefID () string {
29- return sf .refID // this function is only used in templates
31+ return sf .refID
3032}
3133
32- // SubmoduleWebLink tries to make some web links for a submodule, it also works on "nil" receiver
33- func (sf * CommitSubmoduleFile ) SubmoduleWebLink (ctx context.Context , optCommitID ... string ) * SubmoduleWebLink {
34+ func (sf * CommitSubmoduleFile ) getWebLinkInTargetRepo (ctx context.Context , currentRepoHomeLink , moreLinkPath string ) * SubmoduleWebLink {
3435 if sf == nil {
3536 return nil
3637 }
38+ if strings .HasPrefix (sf .refURL , "../" ) {
39+ targetLink := path .Join (currentRepoHomeLink , path .Dir (sf .fullPath ), sf .refURL )
40+ return & SubmoduleWebLink {RepoWebLink : targetLink , CommitWebLink : targetLink + moreLinkPath }
41+ }
3742 if ! sf .parsed {
3843 sf .parsed = true
39- if strings .HasPrefix (sf .refURL , "../" ) {
40- // FIXME: when handling relative path, this logic is not right. It needs to:
41- // 1. Remember the submodule's full path and its commit's repo home link
42- // 2. Resolve the relative path: targetRepoLink = path.Join(repoHomeLink, path.Dir(submoduleFullPath), refURL)
43- // Not an easy task and need to refactor related code a lot.
44- sf .targetRepoLink = sf .refURL
45- } else {
46- parsedURL , err := giturl .ParseRepositoryURL (ctx , sf .refURL )
47- if err != nil {
48- return nil
49- }
50- sf .targetRepoLink = giturl .MakeRepositoryWebLink (parsedURL )
44+ parsedURL , err := giturl .ParseRepositoryURL (ctx , sf .refURL )
45+ if err != nil {
46+ return nil
5147 }
48+ sf .parsedTargetLink = giturl .MakeRepositoryWebLink (parsedURL )
5249 }
53- var commitLink string
54- if len (optCommitID ) == 2 {
55- commitLink = sf .targetRepoLink + "/compare/" + optCommitID [0 ] + "..." + optCommitID [1 ]
56- } else if len (optCommitID ) == 1 {
57- commitLink = sf .targetRepoLink + "/tree/" + optCommitID [0 ]
58- } else {
59- commitLink = sf .targetRepoLink + "/tree/" + sf .refID
60- }
61- return & SubmoduleWebLink {RepoWebLink : sf .targetRepoLink , CommitWebLink : commitLink }
50+ return & SubmoduleWebLink {RepoWebLink : sf .parsedTargetLink , CommitWebLink : sf .parsedTargetLink + moreLinkPath }
51+ }
52+
53+ // SubmoduleWebLinkTree tries to make the submodule's tree link in its own repo, it also works on "nil" receiver
54+ func (sf * CommitSubmoduleFile ) SubmoduleWebLinkTree (ctx context.Context , currentRepoHomeLink , refCommitID string ) * SubmoduleWebLink {
55+ return sf .getWebLinkInTargetRepo (ctx , currentRepoHomeLink , "/tree/" + refCommitID )
56+ }
57+
58+ // SubmoduleWebLinkCompare tries to make the submodule's compare link in its own repo, it also works on "nil" receiver
59+ func (sf * CommitSubmoduleFile ) SubmoduleWebLinkCompare (ctx context.Context , currentRepoHomeLink , commitID1 , commitID2 string ) * SubmoduleWebLink {
60+ return sf .getWebLinkInTargetRepo (ctx , currentRepoHomeLink , "/compare/" + commitID1 + "..." + commitID2 )
6261}
0 commit comments