From 0519bf1c87ab0c7afc628e7a916f300b575e6a90 Mon Sep 17 00:00:00 2001 From: JakobDev Date: Wed, 15 Nov 2023 09:03:16 +0100 Subject: [PATCH 1/5] Show latest commit for file --- routers/web/repo/view.go | 64 ++++++++++++++++++++----------- templates/repo/latest_commit.tmpl | 31 +++++++++++++++ templates/repo/view_file.tmpl | 16 ++++++++ templates/repo/view_list.tmpl | 32 +--------------- web_src/css/repo.css | 18 +++++++++ 5 files changed, 107 insertions(+), 54 deletions(-) create mode 100644 templates/repo/latest_commit.tmpl diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 89bb1839e1747..7455bc0578014 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -333,6 +333,35 @@ func renderReadmeFile(ctx *context.Context, subfolder string, readmeFile *git.Tr } } +func loadLatestCommitData(ctx *context.Context, latestCommit *git.Commit) bool { + // Show latest commit info of repository in table header, + // or of directory if not in root directory. + ctx.Data["LatestCommit"] = latestCommit + if latestCommit != nil { + + verification := asymkey_model.ParseCommitWithSignature(ctx, latestCommit) + + if err := asymkey_model.CalculateTrustStatus(verification, ctx.Repo.Repository.GetTrustModel(), func(user *user_model.User) (bool, error) { + return repo_model.IsOwnerMemberCollaborator(ctx, ctx.Repo.Repository, user.ID) + }, nil); err != nil { + ctx.ServerError("CalculateTrustStatus", err) + return false + } + ctx.Data["LatestCommitVerification"] = verification + ctx.Data["LatestCommitUser"] = user_model.ValidateCommitWithEmail(ctx, latestCommit) + + statuses, _, err := git_model.GetLatestCommitStatus(ctx, ctx.Repo.Repository.ID, latestCommit.ID.String(), db.ListOptions{ListAll: true}) + if err != nil { + log.Error("GetLatestCommitStatus: %v", err) + } + + ctx.Data["LatestCommitStatus"] = git_model.CalcCommitStatus(statuses) + ctx.Data["LatestCommitStatuses"] = statuses + } + + return true +} + func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink string) { ctx.Data["IsViewFile"] = true ctx.Data["HideRepoInfo"] = true @@ -349,6 +378,16 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st ctx.Data["FileName"] = blob.Name() ctx.Data["RawFileLink"] = rawLink + "/" + util.PathEscapeSegments(ctx.Repo.TreePath) + commit, err := ctx.Repo.Commit.GetCommitByPath(util.PathEscapeSegments(ctx.Repo.TreePath)) + if err != nil { + ctx.ServerError("GetCommitByPath", err) + return + } + + if !loadLatestCommitData(ctx, commit) { + return + } + if ctx.Repo.TreePath == ".editorconfig" { _, editorconfigWarning, editorconfigErr := ctx.Repo.GetEditorconfig(ctx.Repo.Commit) if editorconfigWarning != nil { @@ -837,29 +876,8 @@ func renderDirectoryFiles(ctx *context.Context, timeout time.Duration) git.Entri return nil } - // Show latest commit info of repository in table header, - // or of directory if not in root directory. - ctx.Data["LatestCommit"] = latestCommit - if latestCommit != nil { - - verification := asymkey_model.ParseCommitWithSignature(ctx, latestCommit) - - if err := asymkey_model.CalculateTrustStatus(verification, ctx.Repo.Repository.GetTrustModel(), func(user *user_model.User) (bool, error) { - return repo_model.IsOwnerMemberCollaborator(ctx, ctx.Repo.Repository, user.ID) - }, nil); err != nil { - ctx.ServerError("CalculateTrustStatus", err) - return nil - } - ctx.Data["LatestCommitVerification"] = verification - ctx.Data["LatestCommitUser"] = user_model.ValidateCommitWithEmail(ctx, latestCommit) - - statuses, _, err := git_model.GetLatestCommitStatus(ctx, ctx.Repo.Repository.ID, latestCommit.ID.String(), db.ListOptions{ListAll: true}) - if err != nil { - log.Error("GetLatestCommitStatus: %v", err) - } - - ctx.Data["LatestCommitStatus"] = git_model.CalcCommitStatus(statuses) - ctx.Data["LatestCommitStatuses"] = statuses + if !loadLatestCommitData(ctx, latestCommit) { + return nil } branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL() diff --git a/templates/repo/latest_commit.tmpl b/templates/repo/latest_commit.tmpl new file mode 100644 index 0000000000000..15506cfc81b70 --- /dev/null +++ b/templates/repo/latest_commit.tmpl @@ -0,0 +1,31 @@ +{{if not .LatestCommit}} +
+{{else}} + {{if .LatestCommitUser}} + {{ctx.AvatarUtils.Avatar .LatestCommitUser 24 "gt-mr-2"}} + {{if .LatestCommitUser.FullName}} + {{.LatestCommitUser.FullName}} + {{else}} + {{if .LatestCommit.Author}}{{.LatestCommit.Author.Name}}{{else}}{{.LatestCommitUser.Name}}{{end}} + {{end}} + {{else}} + {{if .LatestCommit.Author}} + {{ctx.AvatarUtils.AvatarByEmail .LatestCommit.Author.Email .LatestCommit.Author.Name 24 "gt-mr-2"}} + {{.LatestCommit.Author.Name}} + {{end}} + {{end}} + + {{ShortSha .LatestCommit.ID.String}} + {{if .LatestCommit.Signature}} + {{template "repo/shabox_badge" dict "root" $ "verification" .LatestCommitVerification}} + {{end}} + + {{template "repo/commit_statuses" dict "Status" .LatestCommitStatus "Statuses" .LatestCommitStatuses}} + {{$commitLink:= printf "%s/commit/%s" .RepoLink (PathEscape .LatestCommit.ID.String)}} + {{RenderCommitMessageLinkSubject $.Context .LatestCommit.Message $.RepoLink $commitLink ($.Repository.ComposeMetas ctx)}} + {{if IsMultilineCommitMessage .LatestCommit.Message}} + +
{{RenderCommitBody $.Context .LatestCommit.Message $.RepoLink ($.Repository.ComposeMetas ctx)}}
+ {{end}} +
+{{end}} diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index 53e889fefc3a6..f2bff4613b73b 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -9,6 +9,22 @@
{{.FileWarning}}
{{end}} + + {{if not .ReadmeInList}} +
+
+ {{template "repo/latest_commit" .}} +
+
+ {{if .LatestCommit}} + {{if .LatestCommit.Committer}} + {{TimeSince .LatestCommit.Committer.When ctx.Locale}} + {{end}} + {{end}} +
+
+ {{end}} +

{{if .ReadmeInList}} diff --git a/templates/repo/view_list.tmpl b/templates/repo/view_list.tmpl index 836c633ced668..14bc443c55cac 100644 --- a/templates/repo/view_list.tmpl +++ b/templates/repo/view_list.tmpl @@ -2,37 +2,7 @@ - {{if not .LatestCommit}} -
- {{else}} - {{if .LatestCommitUser}} - {{ctx.AvatarUtils.Avatar .LatestCommitUser 24 "gt-mr-2"}} - {{if .LatestCommitUser.FullName}} - {{.LatestCommitUser.FullName}} - {{else}} - {{if .LatestCommit.Author}}{{.LatestCommit.Author.Name}}{{else}}{{.LatestCommitUser.Name}}{{end}} - {{end}} - {{else}} - {{if .LatestCommit.Author}} - {{ctx.AvatarUtils.AvatarByEmail .LatestCommit.Author.Email .LatestCommit.Author.Name 24 "gt-mr-2"}} - {{.LatestCommit.Author.Name}} - {{end}} - {{end}} - - {{ShortSha .LatestCommit.ID.String}} - {{if .LatestCommit.Signature}} - {{template "repo/shabox_badge" dict "root" $ "verification" .LatestCommitVerification}} - {{end}} - - {{template "repo/commit_statuses" dict "Status" .LatestCommitStatus "Statuses" .LatestCommitStatuses}} - {{$commitLink:= printf "%s/commit/%s" .RepoLink (PathEscape .LatestCommit.ID.String)}} - {{RenderCommitMessageLinkSubject $.Context .LatestCommit.Message $.RepoLink $commitLink ($.Repository.ComposeMetas ctx)}} - {{if IsMultilineCommitMessage .LatestCommit.Message}} - -
{{RenderCommitBody $.Context .LatestCommit.Message $.RepoLink ($.Repository.ComposeMetas ctx)}}
- {{end}} -
- {{end}} + {{template "repo/latest_commit" .}} {{if .LatestCommit}}{{if .LatestCommit.Committer}}{{TimeSince .LatestCommit.Committer.When ctx.Locale}}{{end}}{{end}} diff --git a/web_src/css/repo.css b/web_src/css/repo.css index d759d10c8a06f..489cf09b559ea 100644 --- a/web_src/css/repo.css +++ b/web_src/css/repo.css @@ -1283,6 +1283,7 @@ .repository #commits-table td.sha .sha.label, .repository #repo-files-table .sha.label, +.repository #repo-file-commit-box .sha.label, .repository #rev-list .sha.label, .repository .timeline-item.commits-list .singular-commit .sha.label { border: 1px solid var(--color-light-border); @@ -1290,6 +1291,7 @@ .repository #commits-table td.sha .sha.label .ui.signature.avatar, .repository #repo-files-table .sha.label .ui.signature.avatar, +.repository #repo-file-commit-box .sha.label .ui.signature.avatar, .repository #rev-list .sha.label .ui.signature.avatar, .repository .timeline-item.commits-list .singular-commit .sha.label .ui.signature.avatar { height: 16px; @@ -1299,6 +1301,7 @@ .repository #commits-table td.sha .sha.label .detail.icon, .repository #repo-files-table .sha.label .detail.icon, +.repository #repo-file-commit-box .sha.label .detail.icon, .repository #rev-list .sha.label .detail.icon, .repository .timeline-item.commits-list .singular-commit .sha.label .detail.icon { background: var(--color-light); @@ -1314,6 +1317,7 @@ .repository #commits-table td.sha .sha.label .detail.icon img, .repository #repo-files-table .sha.label .detail.icon img, +.repository #repo-file-commit-box .sha.label .detail.icon img, .repository #rev-list .sha.label .detail.icon img, .repository .timeline-item.commits-list .singular-commit .sha.label .detail.icon img { margin-right: 0; @@ -1321,6 +1325,7 @@ .repository #commits-table td.sha .sha.label .detail.icon .svg, .repository #repo-files-table .sha.label .detail.icon .svg, +.repository #repo-file-commit-box .sha.label .detail.icon .svg, .repository #rev-list .sha.label .detail.icon .svg, .repository .timeline-item.commits-list .singular-commit .sha.label .detail.icon .svg { margin: 0 0.25em 0 0; @@ -1328,6 +1333,7 @@ .repository #commits-table td.sha .sha.label .detail.icon > div, .repository #repo-files-table .sha.label .detail.icon > div, +.repository #repo-file-commit-box .sha.label .detail.icon > div, .repository #rev-list .sha.label .detail.icon > div, .repository .timeline-item.commits-list .singular-commit .sha.label .detail.icon > div { display: flex; @@ -1336,6 +1342,7 @@ .repository #commits-table td.sha .sha.label.isSigned.isWarning, .repository #repo-files-table .sha.label.isSigned.isWarning, +.repository #repo-file-commit-box .sha.label.isSigned.isWarning, .repository #rev-list .sha.label.isSigned.isWarning, .repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isWarning { border: 1px solid var(--color-red-badge); @@ -1344,6 +1351,7 @@ .repository #commits-table td.sha .sha.label.isSigned.isWarning .detail.icon, .repository #repo-files-table .sha.label.isSigned.isWarning .detail.icon, +.repository #repo-file-commit-box .sha.label.isSigned.isWarning .detail.icon, .repository #rev-list .sha.label.isSigned.isWarning .detail.icon, .repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isWarning .detail.icon { border-left: 1px solid var(--color-red-badge); @@ -1352,6 +1360,7 @@ .repository #commits-table td.sha .sha.label.isSigned.isWarning:hover, .repository #repo-files-table .sha.label.isSigned.isWarning:hover, +.repository #repo-file-commit-box .sha.label.isSigned.isWarning:hover, .repository #rev-list .sha.label.isSigned.isWarning:hover, .repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isWarning:hover { background: var(--color-red-badge-hover-bg) !important; @@ -1359,6 +1368,7 @@ .repository #commits-table td.sha .sha.label.isSigned.isVerified, .repository #repo-files-table .sha.label.isSigned.isVerified, +.repository #repo-file-commit-box .sha.label.isSigned.isVerified, .repository #rev-list .sha.label.isSigned.isVerified, .repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isVerified { border: 1px solid var(--color-green-badge); @@ -1367,6 +1377,7 @@ .repository #commits-table td.sha .sha.label.isSigned.isVerified .detail.icon, .repository #repo-files-table .sha.label.isSigned.isVerified .detail.icon, +.repository #repo-file-commit-box .sha.label.isSigned.isVerified .detail.icon, .repository #rev-list .sha.label.isSigned.isVerified .detail.icon, .repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isVerified .detail.icon { border-left: 1px solid var(--color-green-badge); @@ -1375,6 +1386,7 @@ .repository #commits-table td.sha .sha.label.isSigned.isVerified:hover, .repository #repo-files-table .sha.label.isSigned.isVerified:hover, +.repository #repo-file-commit-box .sha.label.isSigned.isVerified:hover, .repository #rev-list .sha.label.isSigned.isVerified:hover, .repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isVerified:hover { background: var(--color-green-badge-hover-bg) !important; @@ -1382,6 +1394,7 @@ .repository #commits-table td.sha .sha.label.isSigned.isVerifiedUntrusted, .repository #repo-files-table .sha.label.isSigned.isVerifiedUntrusted, +.repository #repo-file-commit-box .sha.label.isSigned.isVerifiedUntrusted, .repository #rev-list .sha.label.isSigned.isVerifiedUntrusted, .repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isVerifiedUntrusted { border: 1px solid var(--color-yellow-badge); @@ -1390,6 +1403,7 @@ .repository #commits-table td.sha .sha.label.isSigned.isVerifiedUntrusted .detail.icon, .repository #repo-files-table .sha.label.isSigned.isVerifiedUntrusted .detail.icon, +.repository #repo-file-commit-box .sha.label.isSigned.isVerifiedUntrusted .detail.icon, .repository #rev-list .sha.label.isSigned.isVerifiedUntrusted .detail.icon, .repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isVerifiedUntrusted .detail.icon { border-left: 1px solid var(--color-yellow-badge); @@ -1398,6 +1412,7 @@ .repository #commits-table td.sha .sha.label.isSigned.isVerifiedUntrusted:hover, .repository #repo-files-table .sha.label.isSigned.isVerifiedUntrusted:hover, +.repository #repo-file-commit-box .sha.label.isSigned.isVerifiedUntrusted:hover, .repository #rev-list .sha.label.isSigned.isVerifiedUntrusted:hover, .repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isVerifiedUntrusted:hover { background: var(--color-yellow-badge-hover-bg) !important; @@ -1405,6 +1420,7 @@ .repository #commits-table td.sha .sha.label.isSigned.isVerifiedUnmatched, .repository #repo-files-table .sha.label.isSigned.isVerifiedUnmatched, +.repository #repo-file-commit-box .sha.label.isSigned.isVerifiedUnmatched, .repository #rev-list .sha.label.isSigned.isVerifiedUnmatched, .repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isVerifiedUnmatched { border: 1px solid var(--color-orange-badge); @@ -1413,6 +1429,7 @@ .repository #commits-table td.sha .sha.label.isSigned.isVerifiedUnmatched .detail.icon, .repository #repo-files-table .sha.label.isSigned.isVerifiedUnmatched .detail.icon, +.repository #repo-file-commit-box .sha.label.isSigned.isVerifiedUnmatched .detail.icon, .repository #rev-list .sha.label.isSigned.isVerifiedUnmatched .detail.icon, .repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isVerifiedUnmatched .detail.icon { border-left: 1px solid var(--color-orange-badge); @@ -1421,6 +1438,7 @@ .repository #commits-table td.sha .sha.label.isSigned.isVerifiedUnmatched:hover, .repository #repo-files-table .sha.label.isSigned.isVerifiedUnmatched:hover, +.repository #repo-file-commit-box .sha.label.isSigned.isVerifiedUnmatched:hover, .repository #rev-list .sha.label.isSigned.isVerifiedUnmatched:hover, .repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isVerifiedUnmatched:hover { background: var(--color-orange-badge-hover-bg) !important; From 2db46e153467e06c0ed464ba79bdf48f48f89de0 Mon Sep 17 00:00:00 2001 From: JakobDev Date: Sat, 30 Dec 2023 18:23:19 +0100 Subject: [PATCH 2/5] Some fixes --- routers/web/repo/view.go | 2 +- templates/repo/latest_commit.tmpl | 2 +- templates/repo/view_file.tmpl | 10 +++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 934d6d8d219ce..eea760c6ccf14 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -382,7 +382,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st ctx.Data["FileName"] = blob.Name() ctx.Data["RawFileLink"] = rawLink + "/" + util.PathEscapeSegments(ctx.Repo.TreePath) - commit, err := ctx.Repo.Commit.GetCommitByPath(util.PathEscapeSegments(ctx.Repo.TreePath)) + commit, err := ctx.Repo.Commit.GetCommitByPath(ctx.Repo.TreePath) if err != nil { ctx.ServerError("GetCommitByPath", err) return diff --git a/templates/repo/latest_commit.tmpl b/templates/repo/latest_commit.tmpl index 15506cfc81b70..c7da187bb97ef 100644 --- a/templates/repo/latest_commit.tmpl +++ b/templates/repo/latest_commit.tmpl @@ -17,7 +17,7 @@ {{ShortSha .LatestCommit.ID.String}} {{if .LatestCommit.Signature}} - {{template "repo/shabox_badge" dict "root" $ "verification" .LatestCommitVerification}} + {{template "repo/shabox_badge" dict "root" $ "verification" .LatestCommitVerification}} {{end}} {{template "repo/commit_statuses" dict "Status" .LatestCommitStatus "Statuses" .LatestCommitStatuses}} diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index c19a589a64a6e..c33261635abe6 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -15,13 +15,13 @@
{{template "repo/latest_commit" .}}
-
- {{if .LatestCommit}} - {{if .LatestCommit.Committer}} + {{if .LatestCommit}} + {{if .LatestCommit.Committer}} +
{{TimeSince .LatestCommit.Committer.When ctx.Locale}} - {{end}} +
{{end}} -
+ {{end}}
{{end}} From 5298bbf38fb6d034db6892c070117c1e32e0a7b6 Mon Sep 17 00:00:00 2001 From: JakobDev Date: Mon, 15 Jan 2024 15:14:18 +0100 Subject: [PATCH 3/5] Update templates/repo/view_file.tmpl Co-authored-by: Denys Konovalov --- templates/repo/view_file.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index c33261635abe6..e6591df7e3485 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -11,7 +11,7 @@ {{end}} {{if not .ReadmeInList}} -
+
{{template "repo/latest_commit" .}}
From be6f5cbc5c11a7ebbe0b00a33a602cd2a42b570c Mon Sep 17 00:00:00 2001 From: JakobDev Date: Mon, 15 Jan 2024 15:33:48 +0100 Subject: [PATCH 4/5] Fix build --- routers/web/repo/view.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index c25748f3969a3..aa07d5939deb0 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -370,7 +370,7 @@ func loadLatestCommitData(ctx *context.Context, latestCommit *git.Commit) bool { return true } -func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink string) { +func renderFile(ctx *context.Context, entry *git.TreeEntry) { ctx.Data["IsViewFile"] = true ctx.Data["HideRepoInfo"] = true blob := entry.Blob() From a1c1f28c31622e72a88a155d4a5439de753739b6 Mon Sep 17 00:00:00 2001 From: JakobDev Date: Mon, 15 Jan 2024 16:02:12 +0100 Subject: [PATCH 5/5] Fix template --- templates/repo/latest_commit.tmpl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/repo/latest_commit.tmpl b/templates/repo/latest_commit.tmpl index c7da187bb97ef..b2f079891770e 100644 --- a/templates/repo/latest_commit.tmpl +++ b/templates/repo/latest_commit.tmpl @@ -22,10 +22,10 @@ {{template "repo/commit_statuses" dict "Status" .LatestCommitStatus "Statuses" .LatestCommitStatuses}} {{$commitLink:= printf "%s/commit/%s" .RepoLink (PathEscape .LatestCommit.ID.String)}} - {{RenderCommitMessageLinkSubject $.Context .LatestCommit.Message $.RepoLink $commitLink ($.Repository.ComposeMetas ctx)}} + {{RenderCommitMessageLinkSubject $.Context .LatestCommit.Message $commitLink ($.Repository.ComposeMetas ctx)}} {{if IsMultilineCommitMessage .LatestCommit.Message}} -
{{RenderCommitBody $.Context .LatestCommit.Message $.RepoLink ($.Repository.ComposeMetas ctx)}}
+
{{RenderCommitBody $.Context .LatestCommit.Message ($.Repository.ComposeMetas ctx)}}
{{end}}
{{end}}