@@ -6,57 +6,61 @@ package git
6
6
7
7
import (
8
8
"context"
9
+ "path"
9
10
"strings"
10
11
11
12
giturl "code.gitea.io/gitea/modules/git/url"
13
+ "code.gitea.io/gitea/modules/util"
12
14
)
13
15
14
16
// CommitSubmoduleFile represents a file with submodule type.
15
17
type CommitSubmoduleFile struct {
16
- refURL string
17
- refID string
18
+ repoLink string
19
+ fullPath string
20
+ refURL string
21
+ refID string
18
22
19
- parsed bool
20
- targetRepoLink string
23
+ parsed bool
24
+ parsedTargetLink string
21
25
}
22
26
23
27
// 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 }
26
30
}
27
31
28
32
func (sf * CommitSubmoduleFile ) RefID () string {
29
- return sf .refID // this function is only used in templates
33
+ return sf .refID
30
34
}
31
35
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 {
34
37
if sf == nil {
35
38
return nil
36
39
}
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
+ }
37
44
if ! sf .parsed {
38
45
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
51
49
}
50
+ sf .parsedTargetLink = giturl .MakeRepositoryWebLink (parsedURL )
52
51
}
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
60
59
}
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 )
62
66
}
0 commit comments