Skip to content

Commit edc9623

Browse files
committed
refactor
1 parent a3d1f26 commit edc9623

File tree

3 files changed

+47
-47
lines changed

3 files changed

+47
-47
lines changed

services/gitdiff/submodule.go

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33

44
package 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

815
type SubmoduleInfo struct {
916
SubmoduleURL string
@@ -12,44 +19,49 @@ type SubmoduleInfo struct {
1219
}
1320

1421
func (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
}

services/gitdiff/submodule_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,7 @@ index 0000000..68972a9
215215
})
216216
}
217217
}
218+
219+
func TestSubmoduleInfo(t *testing.T) {
220+
// TODO: test NewRefIDLinkHTML PreviousRefIDLinkHTML SubmoduleRepoLinkHTML after we get the unifed "RefURL" function
221+
}

templates/repo/diff/box.tmpl

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -199,29 +199,13 @@
199199
</div>
200200
{{else if and $file.SubmoduleInfo}}
201201
<div class="tw-p-3">{{svg "octicon-file-submodule"}} {{$submodule := $file.SubmoduleInfo -}}
202-
{{- $refURL := $submodule.RefURL $.Repository.FullName -}}
203-
{{- $submoduleName := $file.Name -}}
204-
{{- $refShortID := ShortSha $submodule.RefID -}}
202+
{{- $submoduleName := ($submodule.SubmoduleRepoLinkHTML $file.Name) -}}
205203
{{- if $file.IsDeleted -}}
206-
{{- ctx.Locale.Tr "repo.diff.submodule_deleted" $submoduleName $refShortID -}}
204+
{{- ctx.Locale.Tr "repo.diff.submodule_deleted" $submoduleName $submodule.PreviousRefIDLinkHTML -}}
207205
{{- else if $file.IsCreated -}}
208-
{{- $refAddedSubModule := $submoduleName -}}
209-
{{- $refAddedAt := $refShortID -}}
210-
{{- if $refURL -}}
211-
{{- $refAddedSubModule = HTMLFormat `<a href="%s">%s</a>` $refURL $submoduleName -}}
212-
{{- $refAddedAt = HTMLFormat `<a href="%s/commit/%s">%s</a>` $refURL (PathEscape $submodule.RefID) $refShortID -}}
213-
{{- end -}}
214-
{{- ctx.Locale.Tr "repo.diff.submodule_added" $refAddedSubModule $refAddedAt -}}
206+
{{- ctx.Locale.Tr "repo.diff.submodule_added" $submoduleName $submodule.NewRefIDLinkHTML -}}
215207
{{- else -}}
216-
{{- $refUpdatedSubModule := $submoduleName -}}
217-
{{- $refUpdatedFrom := ShortSha $submodule.PreviousRefID -}}
218-
{{- $refUpdatedTo := $refShortID -}}
219-
{{- if $refURL -}}
220-
{{- $refUpdatedSubModule = HTMLFormat `<a href="%s">%s</a>` $refURL $submoduleName -}}
221-
{{- $refUpdatedFrom = HTMLFormat `<a href="%s/commit/%s">%s</a>` $refURL (PathEscape $submodule.PreviousRefID) (ShortSha $submodule.PreviousRefID) -}}
222-
{{- $refUpdatedTo = HTMLFormat `<a href="%s/commit/%s">%s</a>` $refURL (PathEscape $submodule.RefID) (ShortSha $submodule.RefID) -}}
223-
{{- end -}}
224-
{{- ctx.Locale.Tr "repo.diff.submodule_updated" $refUpdatedSubModule $refUpdatedFrom $refUpdatedTo -}}
208+
{{- ctx.Locale.Tr "repo.diff.submodule_updated" $submoduleName $submodule.PreviousRefIDLinkHTML $submodule.NewRefIDLinkHTML -}}
225209
{{end}}
226210
</div>
227211
{{else}}

0 commit comments

Comments
 (0)