@@ -6,57 +6,61 @@ package git
66
77import  (
88	"context" 
9+ 	"path" 
910	"strings" 
1011
1112	giturl "code.gitea.io/gitea/modules/git/url" 
13+ 	"code.gitea.io/gitea/modules/util" 
1214)
1315
1416// CommitSubmoduleFile represents a file with submodule type. 
1517type  CommitSubmoduleFile  struct  {
16- 	refURL  string 
17- 	refID   string 
18+ 	repoLink  string 
19+ 	fullPath  string 
20+ 	refURL    string 
21+ 	refID     string 
1822
19- 	parsed          bool 
20- 	targetRepoLink  string 
23+ 	parsed             bool 
24+ 	parsedTargetLink  string 
2125}
2226
2327// NewCommitSubmoduleFile create a new submodule file 
24- func  NewCommitSubmoduleFile (refURL , refID  string ) * CommitSubmoduleFile  {
25- 	return  & CommitSubmoduleFile {refURL : refURL , refID : refID }
28+ func  NewCommitSubmoduleFile (repoLink ,  fullPath ,  refURL , refID  string ) * CommitSubmoduleFile  {
29+ 	return  & CommitSubmoduleFile {repoLink :  repoLink ,  fullPath :  fullPath ,  refURL : refURL , refID : refID }
2630}
2731
2832func  (sf  * CommitSubmoduleFile ) RefID () string  {
29- 	return  sf .refID   // this function is only used in templates 
33+ 	return  sf .refID 
3034}
3135
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  {
36+ func  (sf  * CommitSubmoduleFile ) getWebLinkInTargetRepo (ctx  context.Context , moreLinkPath  string ) * SubmoduleWebLink  {
3437	if  sf  ==  nil  {
3538		return  nil 
3639	}
40+ 	if  strings .HasPrefix (sf .refURL , "../" ) {
41+ 		targetLink  :=  path .Join (sf .repoLink , path .Dir (sf .fullPath ), sf .refURL )
42+ 		return  & SubmoduleWebLink {RepoWebLink : targetLink , CommitWebLink : targetLink  +  moreLinkPath }
43+ 	}
3744	if  ! sf .parsed  {
3845		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 )
46+ 		parsedURL , err  :=  giturl .ParseRepositoryURL (ctx , sf .refURL )
47+ 		if  err  !=  nil  {
48+ 			return  nil 
5149		}
50+ 		sf .parsedTargetLink  =  giturl .MakeRepositoryWebLink (parsedURL )
5251	}
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 
52+ 	return   & SubmoduleWebLink { RepoWebLink :  sf . parsedTargetLink ,  CommitWebLink :  sf . parsedTargetLink   +   moreLinkPath } 
53+ } 
54+ 
55+ // SubmoduleWebLinkTree tries to make the submodule's tree link in its own repo, it also works on "nil" receiver 
56+ func  ( sf   * CommitSubmoduleFile )  SubmoduleWebLinkTree ( ctx  context. Context ,  optCommitID   ... string )  * SubmoduleWebLink  { 
57+ 	if   sf   ==   nil  {
58+ 		return   nil 
6059	}
61- 	return  & SubmoduleWebLink {RepoWebLink : sf .targetRepoLink , CommitWebLink : commitLink }
60+ 	return  sf .getWebLinkInTargetRepo (ctx , "/tree/" + util .OptionalArg (optCommitID , sf .refID ))
61+ }
62+ 
63+ // SubmoduleWebLinkCompare tries to make the submodule's compare link in its own repo, it also works on "nil" receiver 
64+ func  (sf  * CommitSubmoduleFile ) SubmoduleWebLinkCompare (ctx  context.Context , commitID1 , commitID2  string ) * SubmoduleWebLink  {
65+ 	return  sf .getWebLinkInTargetRepo (ctx , "/compare/" + commitID1 + "..." + commitID2 )
6266}
0 commit comments