Skip to content
Draft
55 changes: 55 additions & 0 deletions templates/repo/commit_author.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{{/* Template Attributes:
* User: The user associated with the commit, if any
* Commit: The commit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you should pass so many different structs.

Commit is not really used. What you need is a "git user" or "gitea user".

Copy link
Member Author

@silverwind silverwind Dec 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit is used for .Commit.Author vs .Author. Maybe it can be unified, not sure. The three pages all pass different context variables.

* Author: The author, currently only the commit page passes this
* AvatarSize: Avatar size in pixels
* AuthorBold: Whether to render the author as bold text
*/}}
<div class="flex-text-inline{{if .AuthorBold}} tw-font-semibold{{end}}">
{{if .User}}
{{ctx.AvatarUtils.Avatar .User .AvatarSize}}
{{$username := ""}}
{{if and .User.FullName DefaultShowFullName}}
{{$username = .User.FullName}}
{{else if .User.Name}}
{{$username = .User.Name}}
{{end}}
Comment on lines +12 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if *User will also be compatible with .Author.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if *User will also be compatible with .Author.

No. More complicated.

<a class="muted author-wrapper" href="{{.User.HomeLink}}" data-tooltip-content="{{$username}}">
{{$username}}
</a>
{{else if .Author}}
{{$username := ""}}
{{if and .Author.FullName DefaultShowFullName}}
{{$username = .Author.FullName}}
{{else if .Author.Name}}
{{$username = .Author.Name}}
{{else if .Commit.Author.Name}}
{{$username = .Commit.Author.Name}}
{{end}}
{{ctx.AvatarUtils.AvatarByEmail .Author.Email $username .AvatarSize}}
<span{{if ctx.RootData.IsSigned}} title="{{.Author.Email}}"{{end}}>
{{if .Author.HomeLink}}
<a class="muted author-wrapper" href="{{.Author.HomeLink}}">
{{$username}}
</a>
{{else}}
{{$username}}
{{end}}
</span>
{{else}}
{{$username := ""}}
{{if .Commit.Author.Name}}
{{$username = .Commit.Author.Name}}
{{else}}
{{$username = .Author.Name}}
{{end}}
{{ctx.AvatarUtils.AvatarByEmail .Commit.Author.Email $username .AvatarSize}}
<span class="author-wrapper" {{if ctx.RootData.IsSigned}} title="{{.Commit.Author.Email}}"{{end}}>
{{if .Commit.Author.Name}}
{{.Commit.Author.Name}}
{{else}}
{{.Author.Name}}
{{end}}
</span>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are full of "if-else" blocks.

I don't think such "shared template" can be right. It is very fragile and only causes maintainability problems.

image

Copy link
Member Author

@silverwind silverwind Dec 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a full picture what the 3 variants of data being passed into here are. Deeper analysis will be needed to determine if any of this can be reduced. All your remaining comments are about this same topic. If you have a better picture of the data passed in, feel free to change it yourself.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't have a full picture at the moment either. The details I know were written in #24087 (comment) (commit is not really related).

I think I don't have time or motivation to do it at the moment.

If you'd like to use the "shared templates", I think you need to make it clear and maintainable, but not leave the problem to the future maintainers ....

{{end}}
</div>
19 changes: 6 additions & 13 deletions templates/repo/commit_page.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,12 @@
</div>

<div class="ui bottom attached segment flex-text-block tw-flex-wrap">
<div class="flex-text-inline">
{{if .Author}}
{{ctx.AvatarUtils.Avatar .Author 20}}
{{if .Author.FullName}}
<a href="{{.Author.HomeLink}}"><strong>{{.Author.FullName}}</strong></a>
{{else}}
<a href="{{.Author.HomeLink}}"><strong>{{.Commit.Author.Name}}</strong></a>
{{end}}
{{else}}
{{ctx.AvatarUtils.AvatarByEmail .Commit.Author.Email .Commit.Author.Email 20}}
<strong>{{.Commit.Author.Name}}</strong>
{{end}}
</div>
{{template "repo/commit_author" (dict
"Commit" .Commit
"Author" .Author
"AvatarSize" 20
"AuthorBold" true
)}}

<span class="text grey">{{DateUtils.TimeSince .Commit.Author.When}}</span>

Expand Down
15 changes: 5 additions & 10 deletions templates/repo/commits_list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@
<tr>
<td class="author">
<div class="tw-flex">
{{$userName := .Author.Name}}
{{if .User}}
{{if and .User.FullName DefaultShowFullName}}
{{$userName = .User.FullName}}
{{end}}
{{ctx.AvatarUtils.Avatar .User 28 "tw-mr-2"}}<a class="muted author-wrapper" href="{{.User.HomeLink}}">{{$userName}}</a>
{{else}}
{{ctx.AvatarUtils.AvatarByEmail .Author.Email .Author.Name 28 "tw-mr-2"}}
<span class="author-wrapper">{{$userName}}</span>
{{end}}
{{template "repo/commit_author" (dict
"Commit" .
"User" .User
"AvatarSize" 28
)}}
</div>
</td>
<td class="sha">
Expand Down
20 changes: 6 additions & 14 deletions templates/repo/latest_commit.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@
{{if not .LatestCommit}}
{{else}}
{{if .LatestCommitUser}}
{{ctx.AvatarUtils.Avatar .LatestCommitUser 24}}
{{if and .LatestCommitUser.FullName DefaultShowFullName}}
<a class="muted author-wrapper" title="{{.LatestCommitUser.FullName}}" href="{{.LatestCommitUser.HomeLink}}"><strong>{{.LatestCommitUser.FullName}}</strong></a>
{{else}}
<a class="muted author-wrapper" title="{{if .LatestCommit.Author}}{{.LatestCommit.Author.Name}}{{else}}{{.LatestCommitUser.Name}}{{end}}" href="{{.LatestCommitUser.HomeLink}}"><strong>{{if .LatestCommit.Author}}{{.LatestCommit.Author.Name}}{{else}}{{.LatestCommitUser.Name}}{{end}}</strong></a>
{{end}}
{{else}}
{{if .LatestCommit.Author}}
{{ctx.AvatarUtils.AvatarByEmail .LatestCommit.Author.Email .LatestCommit.Author.Name 24}}
<span class="author-wrapper" title="{{.LatestCommit.Author.Name}}"><strong>{{.LatestCommit.Author.Name}}</strong></span>
{{end}}
{{end}}

{{template "repo/commit_author" (dict
"User" .LatestCommitUser
"Commit" .LatestCommit
"AvatarSize" 24
"AuthorBold" true
)}}
{{template "repo/commit_sign_badge" dict "Commit" .LatestCommit "CommitBaseLink" (print .RepoLink "/commit") "CommitSignVerification" .LatestCommitVerification}}

{{template "repo/commit_statuses" dict "Status" .LatestCommitStatus "Statuses" .LatestCommitStatuses}}
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/repo_commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/http/httptest"
"path"
"strings"
"sync"
"testing"

Expand Down Expand Up @@ -63,7 +64,7 @@ func TestRepoCommits(t *testing.T) {
commitHref := doc.doc.Find("#commits-table tr:first-child .commit-id-short").AttrOr("href", "")
assert.Equal(t, "/user2/repo1/commit/985f0301dba5e7b34be866819cd15ad3d8f508ee", commitHref)
authorElem := doc.doc.Find("#commits-table tr:first-child .author-wrapper")
assert.Equal(t, "6543", authorElem.Text())
assert.Equal(t, "6543", strings.TrimSpace(authorElem.Text()))
assert.Equal(t, "span", authorElem.Nodes[0].Data)
})

Expand All @@ -74,7 +75,7 @@ func TestRepoCommits(t *testing.T) {
commitHref := doc.doc.Find(".latest-commit .commit-id-short").AttrOr("href", "")
assert.Equal(t, "/user2/repo1/commit/985f0301dba5e7b34be866819cd15ad3d8f508ee", commitHref)
authorElem := doc.doc.Find(".latest-commit .author-wrapper")
assert.Equal(t, "6543", authorElem.Text())
assert.Equal(t, "6543", strings.TrimSpace(authorElem.Text()))
assert.Equal(t, "span", authorElem.Nodes[0].Data)
})
}
Expand Down