33
44package gitdiff
55
6- import "code.gitea.io/gitea/modules/git"
6+ import (
7+ "html/template"
8+
9+ "code.gitea.io/gitea/modules/base"
10+ "code.gitea.io/gitea/modules/git"
11+ "code.gitea.io/gitea/modules/htmlutil"
12+ "code.gitea.io/gitea/modules/log"
13+ )
714
815type SubmoduleInfo struct {
916 SubmoduleURL string
@@ -12,44 +19,49 @@ type SubmoduleInfo struct {
1219}
1320
1421func (si * SubmoduleInfo ) PopulateURL (diffFile * DiffFile , leftCommit , rightCommit * git.Commit ) error {
15- // If the submodule is removed, we need to check it at the left commit
16- if diffFile .IsDeleted {
17- if leftCommit == nil {
18- return nil
19- }
20-
21- submodule , err := leftCommit .GetSubModule (diffFile .GetDiffFileName ())
22+ var submoduleCommit * git.Commit
23+ switch {
24+ case diffFile .IsDeleted :
25+ submoduleCommit = leftCommit // If the submodule is removed, we need to check it at the left commit
26+ default :
27+ submoduleCommit = rightCommit // If the submodule path is added or updated, we check this at the right commit
28+ }
29+ if submoduleCommit != nil {
30+ submodule , err := submoduleCommit .GetSubModule (diffFile .GetDiffFileName ())
2231 if err != nil {
23- return err
32+ log .Error ("Unable to PopulateURL for submodule %q: GetSubModule: %v" , diffFile .GetDiffFileName (), err )
33+ return nil // ignore the error, do not cause 500 errors for end users
2434 }
25-
2635 if submodule != nil {
2736 si .SubmoduleURL = submodule .URL
2837 }
29-
30- return nil
3138 }
39+ return nil
40+ }
3241
33- // Even if the submodule path is updated, we check this at the right commit
34- submodule , err := rightCommit . GetSubModule ( diffFile . Name )
35- if err != nil {
36- return err
42+ func ( si * SubmoduleInfo ) NewRefIDLinkHTML () template. HTML {
43+ refURL := si . refURL ( )
44+ if si . PreviousRefID == "" {
45+ return htmlutil . HTMLFormat ( `<a href="%s/commit/%s"">%s</a>` , refURL , si . NewRefID , base . ShortSha ( si . NewRefID ))
3746 }
47+ return htmlutil .HTMLFormat (`<a href="%s/compare/%s...%s">%s</a>` , refURL , si .PreviousRefID , si .NewRefID , base .ShortSha (si .NewRefID ))
48+ }
3849
39- if submodule != nil {
40- si .SubmoduleURL = submodule .URL
41- }
42- return nil
50+ func (si * SubmoduleInfo ) PreviousRefIDLinkHTML () template.HTML {
51+ refURL := si .refURL ()
52+ return htmlutil .HTMLFormat (`<a href="%s/commit/%s">%s</a>` , refURL , si .PreviousRefID , base .ShortSha (si .PreviousRefID ))
4353}
4454
45- func (si * SubmoduleInfo ) RefID () string {
46- if si .NewRefID != "" {
47- return si .NewRefID
48- }
49- return si .PreviousRefID
55+ func (si * SubmoduleInfo ) SubmoduleRepoLinkHTML (name string ) template.HTML {
56+ refURL := si .refURL ()
57+ return htmlutil .HTMLFormat (`<a href="%s">%s</a>` , refURL , name )
5058}
5159
5260// RefURL guesses and returns reference URL.
53- func (si * SubmoduleInfo ) RefURL (repoFullName string ) string {
54- return git .NewCommitSubModuleFile (si .SubmoduleURL , si .RefID ()).RefURL (repoFullName )
61+ func (si * SubmoduleInfo ) refURL () string {
62+ // FIXME: use unified way to handle domain and subpath
63+ // FIXME: RefURL(repoFullName) is only used to provide relative path support for submodules
64+ // it is not our use case because it won't work for git clone via http/ssh
65+ // FIXME: the current RefURL is not right, it doesn't consider the subpath
66+ return git .NewCommitSubModuleFile (si .SubmoduleURL , "" ).RefURL ("" )
5567}
0 commit comments