Skip to content

Commit c974396

Browse files
JakobDevdenyskon
authored and
Henry Goodman
committed
Show latest commit for file (go-gitea#28067)
If you view a file, you can now see the latest commit that changed that file. ![grafik](https://github.com/go-gitea/gitea/assets/15185051/272c3120-6db7-4f88-86e1-60080c9aabe5) --------- Co-authored-by: Denys Konovalov <[email protected]>
1 parent 5883665 commit c974396

File tree

5 files changed

+107
-54
lines changed

5 files changed

+107
-54
lines changed

routers/web/repo/view.go

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,35 @@ func renderReadmeFile(ctx *context.Context, subfolder string, readmeFile *git.Tr
341341
}
342342
}
343343

344+
func loadLatestCommitData(ctx *context.Context, latestCommit *git.Commit) bool {
345+
// Show latest commit info of repository in table header,
346+
// or of directory if not in root directory.
347+
ctx.Data["LatestCommit"] = latestCommit
348+
if latestCommit != nil {
349+
350+
verification := asymkey_model.ParseCommitWithSignature(ctx, latestCommit)
351+
352+
if err := asymkey_model.CalculateTrustStatus(verification, ctx.Repo.Repository.GetTrustModel(), func(user *user_model.User) (bool, error) {
353+
return repo_model.IsOwnerMemberCollaborator(ctx, ctx.Repo.Repository, user.ID)
354+
}, nil); err != nil {
355+
ctx.ServerError("CalculateTrustStatus", err)
356+
return false
357+
}
358+
ctx.Data["LatestCommitVerification"] = verification
359+
ctx.Data["LatestCommitUser"] = user_model.ValidateCommitWithEmail(ctx, latestCommit)
360+
361+
statuses, _, err := git_model.GetLatestCommitStatus(ctx, ctx.Repo.Repository.ID, latestCommit.ID.String(), db.ListOptions{ListAll: true})
362+
if err != nil {
363+
log.Error("GetLatestCommitStatus: %v", err)
364+
}
365+
366+
ctx.Data["LatestCommitStatus"] = git_model.CalcCommitStatus(statuses)
367+
ctx.Data["LatestCommitStatuses"] = statuses
368+
}
369+
370+
return true
371+
}
372+
344373
func renderFile(ctx *context.Context, entry *git.TreeEntry) {
345374
ctx.Data["IsViewFile"] = true
346375
ctx.Data["HideRepoInfo"] = true
@@ -357,6 +386,16 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry) {
357386
ctx.Data["FileName"] = blob.Name()
358387
ctx.Data["RawFileLink"] = ctx.Repo.RepoLink + "/raw/" + ctx.Repo.BranchNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
359388

389+
commit, err := ctx.Repo.Commit.GetCommitByPath(ctx.Repo.TreePath)
390+
if err != nil {
391+
ctx.ServerError("GetCommitByPath", err)
392+
return
393+
}
394+
395+
if !loadLatestCommitData(ctx, commit) {
396+
return
397+
}
398+
360399
if ctx.Repo.TreePath == ".editorconfig" {
361400
_, editorconfigWarning, editorconfigErr := ctx.Repo.GetEditorconfig(ctx.Repo.Commit)
362401
if editorconfigWarning != nil {
@@ -846,29 +885,8 @@ func renderDirectoryFiles(ctx *context.Context, timeout time.Duration) git.Entri
846885
return nil
847886
}
848887

849-
// Show latest commit info of repository in table header,
850-
// or of directory if not in root directory.
851-
ctx.Data["LatestCommit"] = latestCommit
852-
if latestCommit != nil {
853-
854-
verification := asymkey_model.ParseCommitWithSignature(ctx, latestCommit)
855-
856-
if err := asymkey_model.CalculateTrustStatus(verification, ctx.Repo.Repository.GetTrustModel(), func(user *user_model.User) (bool, error) {
857-
return repo_model.IsOwnerMemberCollaborator(ctx, ctx.Repo.Repository, user.ID)
858-
}, nil); err != nil {
859-
ctx.ServerError("CalculateTrustStatus", err)
860-
return nil
861-
}
862-
ctx.Data["LatestCommitVerification"] = verification
863-
ctx.Data["LatestCommitUser"] = user_model.ValidateCommitWithEmail(ctx, latestCommit)
864-
865-
statuses, _, err := git_model.GetLatestCommitStatus(ctx, ctx.Repo.Repository.ID, latestCommit.ID.String(), db.ListOptions{ListAll: true})
866-
if err != nil {
867-
log.Error("GetLatestCommitStatus: %v", err)
868-
}
869-
870-
ctx.Data["LatestCommitStatus"] = git_model.CalcCommitStatus(statuses)
871-
ctx.Data["LatestCommitStatuses"] = statuses
888+
if !loadLatestCommitData(ctx, latestCommit) {
889+
return nil
872890
}
873891

874892
branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()

templates/repo/latest_commit.tmpl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{{if not .LatestCommit}}
2+
<div class="ui active tiny slow centered inline">…</div>
3+
{{else}}
4+
{{if .LatestCommitUser}}
5+
{{ctx.AvatarUtils.Avatar .LatestCommitUser 24 "gt-mr-2"}}
6+
{{if .LatestCommitUser.FullName}}
7+
<a class="muted author-wrapper" title="{{.LatestCommitUser.FullName}}" href="{{.LatestCommitUser.HomeLink}}"><strong>{{.LatestCommitUser.FullName}}</strong></a>
8+
{{else}}
9+
<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>
10+
{{end}}
11+
{{else}}
12+
{{if .LatestCommit.Author}}
13+
{{ctx.AvatarUtils.AvatarByEmail .LatestCommit.Author.Email .LatestCommit.Author.Name 24 "gt-mr-2"}}
14+
<span class="author-wrapper" title="{{.LatestCommit.Author.Name}}"><strong>{{.LatestCommit.Author.Name}}</strong></span>
15+
{{end}}
16+
{{end}}
17+
<a rel="nofollow" class="ui sha label {{if .LatestCommit.Signature}} isSigned {{if .LatestCommitVerification.Verified}} isVerified{{if eq .LatestCommitVerification.TrustStatus "trusted"}}{{else if eq .LatestCommitVerification.TrustStatus "untrusted"}}Untrusted{{else}}Unmatched{{end}}{{else if .LatestCommitVerification.Warning}} isWarning{{end}}{{end}}" href="{{.RepoLink}}/commit/{{PathEscape .LatestCommit.ID.String}}">
18+
<span class="shortsha">{{ShortSha .LatestCommit.ID.String}}</span>
19+
{{if .LatestCommit.Signature}}
20+
{{template "repo/shabox_badge" dict "root" $ "verification" .LatestCommitVerification}}
21+
{{end}}
22+
</a>
23+
{{template "repo/commit_statuses" dict "Status" .LatestCommitStatus "Statuses" .LatestCommitStatuses}}
24+
{{$commitLink:= printf "%s/commit/%s" .RepoLink (PathEscape .LatestCommit.ID.String)}}
25+
<span class="grey commit-summary" title="{{.LatestCommit.Summary}}"><span class="message-wrapper">{{RenderCommitMessageLinkSubject $.Context .LatestCommit.Message $commitLink ($.Repository.ComposeMetas ctx)}}</span>
26+
{{if IsMultilineCommitMessage .LatestCommit.Message}}
27+
<button class="ui button js-toggle-commit-body ellipsis-button" aria-expanded="false">...</button>
28+
<pre class="commit-body gt-hidden">{{RenderCommitBody $.Context .LatestCommit.Message ($.Repository.ComposeMetas ctx)}}</pre>
29+
{{end}}
30+
</span>
31+
{{end}}

templates/repo/view_file.tmpl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@
99
<div class="text left gt-whitespace-pre">{{.FileWarning}}</div>
1010
</div>
1111
{{end}}
12+
13+
{{if not .ReadmeInList}}
14+
<div id="repo-file-commit-box" class="ui top attached header list-header gt-mb-4">
15+
<div>
16+
{{template "repo/latest_commit" .}}
17+
</div>
18+
{{if .LatestCommit}}
19+
{{if .LatestCommit.Committer}}
20+
<div class="ui text grey right age">
21+
{{TimeSince .LatestCommit.Committer.When ctx.Locale}}
22+
</div>
23+
{{end}}
24+
{{end}}
25+
</div>
26+
{{end}}
27+
1228
<h4 class="file-header ui top attached header gt-df gt-ac gt-sb gt-fw">
1329
<div class="file-header-left gt-df gt-ac gt-py-3 gt-pr-4">
1430
{{if .ReadmeInList}}

templates/repo/view_list.tmpl

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,7 @@
22
<thead>
33
<tr class="commit-list">
44
<th colspan="2" {{if not .LatestCommit}}class="notready"{{end}}>
5-
{{if not .LatestCommit}}
6-
<div class="ui active tiny slow centered inline">…</div>
7-
{{else}}
8-
{{if .LatestCommitUser}}
9-
{{ctx.AvatarUtils.Avatar .LatestCommitUser 24 "gt-mr-2"}}
10-
{{if .LatestCommitUser.FullName}}
11-
<a class="muted author-wrapper" title="{{.LatestCommitUser.FullName}}" href="{{.LatestCommitUser.HomeLink}}"><strong>{{.LatestCommitUser.FullName}}</strong></a>
12-
{{else}}
13-
<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>
14-
{{end}}
15-
{{else}}
16-
{{if .LatestCommit.Author}}
17-
{{ctx.AvatarUtils.AvatarByEmail .LatestCommit.Author.Email .LatestCommit.Author.Name 24 "gt-mr-2"}}
18-
<span class="author-wrapper" title="{{.LatestCommit.Author.Name}}"><strong>{{.LatestCommit.Author.Name}}</strong></span>
19-
{{end}}
20-
{{end}}
21-
<a rel="nofollow" class="ui sha label {{if .LatestCommit.Signature}} isSigned {{if .LatestCommitVerification.Verified}} isVerified{{if eq .LatestCommitVerification.TrustStatus "trusted"}}{{else if eq .LatestCommitVerification.TrustStatus "untrusted"}}Untrusted{{else}}Unmatched{{end}}{{else if .LatestCommitVerification.Warning}} isWarning{{end}}{{end}}" href="{{.RepoLink}}/commit/{{PathEscape .LatestCommit.ID.String}}">
22-
<span class="shortsha">{{ShortSha .LatestCommit.ID.String}}</span>
23-
{{if .LatestCommit.Signature}}
24-
{{template "repo/shabox_badge" dict "root" $ "verification" .LatestCommitVerification}}
25-
{{end}}
26-
</a>
27-
{{template "repo/commit_statuses" dict "Status" .LatestCommitStatus "Statuses" .LatestCommitStatuses}}
28-
{{$commitLink:= printf "%s/commit/%s" .RepoLink (PathEscape .LatestCommit.ID.String)}}
29-
<span class="grey commit-summary" title="{{.LatestCommit.Summary}}"><span class="message-wrapper">{{RenderCommitMessageLinkSubject $.Context .LatestCommit.Message $commitLink ($.Repository.ComposeMetas ctx)}}</span>
30-
{{if IsMultilineCommitMessage .LatestCommit.Message}}
31-
<button class="ui button js-toggle-commit-body ellipsis-button" aria-expanded="false">...</button>
32-
<pre class="commit-body gt-hidden">{{RenderCommitBody $.Context .LatestCommit.Message ($.Repository.ComposeMetas ctx)}}</pre>
33-
{{end}}
34-
</span>
35-
{{end}}
5+
{{template "repo/latest_commit" .}}
366
</th>
377
<th class="text grey right age">{{if .LatestCommit}}{{if .LatestCommit.Committer}}{{TimeSince .LatestCommit.Committer.When ctx.Locale}}{{end}}{{end}}</th>
388
</tr>

web_src/css/repo.css

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,13 +1260,15 @@
12601260

12611261
.repository #commits-table td.sha .sha.label,
12621262
.repository #repo-files-table .sha.label,
1263+
.repository #repo-file-commit-box .sha.label,
12631264
.repository #rev-list .sha.label,
12641265
.repository .timeline-item.commits-list .singular-commit .sha.label {
12651266
border: 1px solid var(--color-light-border);
12661267
}
12671268

12681269
.repository #commits-table td.sha .sha.label .ui.signature.avatar,
12691270
.repository #repo-files-table .sha.label .ui.signature.avatar,
1271+
.repository #repo-file-commit-box .sha.label .ui.signature.avatar,
12701272
.repository #rev-list .sha.label .ui.signature.avatar,
12711273
.repository .timeline-item.commits-list .singular-commit .sha.label .ui.signature.avatar {
12721274
height: 16px;
@@ -1276,6 +1278,7 @@
12761278

12771279
.repository #commits-table td.sha .sha.label .detail.icon,
12781280
.repository #repo-files-table .sha.label .detail.icon,
1281+
.repository #repo-file-commit-box .sha.label .detail.icon,
12791282
.repository #rev-list .sha.label .detail.icon,
12801283
.repository .timeline-item.commits-list .singular-commit .sha.label .detail.icon {
12811284
background: var(--color-light);
@@ -1291,20 +1294,23 @@
12911294

12921295
.repository #commits-table td.sha .sha.label .detail.icon img,
12931296
.repository #repo-files-table .sha.label .detail.icon img,
1297+
.repository #repo-file-commit-box .sha.label .detail.icon img,
12941298
.repository #rev-list .sha.label .detail.icon img,
12951299
.repository .timeline-item.commits-list .singular-commit .sha.label .detail.icon img {
12961300
margin-right: 0;
12971301
}
12981302

12991303
.repository #commits-table td.sha .sha.label .detail.icon .svg,
13001304
.repository #repo-files-table .sha.label .detail.icon .svg,
1305+
.repository #repo-file-commit-box .sha.label .detail.icon .svg,
13011306
.repository #rev-list .sha.label .detail.icon .svg,
13021307
.repository .timeline-item.commits-list .singular-commit .sha.label .detail.icon .svg {
13031308
margin: 0 0.25em 0 0;
13041309
}
13051310

13061311
.repository #commits-table td.sha .sha.label .detail.icon > div,
13071312
.repository #repo-files-table .sha.label .detail.icon > div,
1313+
.repository #repo-file-commit-box .sha.label .detail.icon > div,
13081314
.repository #rev-list .sha.label .detail.icon > div,
13091315
.repository .timeline-item.commits-list .singular-commit .sha.label .detail.icon > div {
13101316
display: flex;
@@ -1313,6 +1319,7 @@
13131319

13141320
.repository #commits-table td.sha .sha.label.isSigned.isWarning,
13151321
.repository #repo-files-table .sha.label.isSigned.isWarning,
1322+
.repository #repo-file-commit-box .sha.label.isSigned.isWarning,
13161323
.repository #rev-list .sha.label.isSigned.isWarning,
13171324
.repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isWarning {
13181325
border: 1px solid var(--color-red-badge);
@@ -1321,6 +1328,7 @@
13211328

13221329
.repository #commits-table td.sha .sha.label.isSigned.isWarning .detail.icon,
13231330
.repository #repo-files-table .sha.label.isSigned.isWarning .detail.icon,
1331+
.repository #repo-file-commit-box .sha.label.isSigned.isWarning .detail.icon,
13241332
.repository #rev-list .sha.label.isSigned.isWarning .detail.icon,
13251333
.repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isWarning .detail.icon {
13261334
border-left: 1px solid var(--color-red-badge);
@@ -1329,13 +1337,15 @@
13291337

13301338
.repository #commits-table td.sha .sha.label.isSigned.isWarning:hover,
13311339
.repository #repo-files-table .sha.label.isSigned.isWarning:hover,
1340+
.repository #repo-file-commit-box .sha.label.isSigned.isWarning:hover,
13321341
.repository #rev-list .sha.label.isSigned.isWarning:hover,
13331342
.repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isWarning:hover {
13341343
background: var(--color-red-badge-hover-bg) !important;
13351344
}
13361345

13371346
.repository #commits-table td.sha .sha.label.isSigned.isVerified,
13381347
.repository #repo-files-table .sha.label.isSigned.isVerified,
1348+
.repository #repo-file-commit-box .sha.label.isSigned.isVerified,
13391349
.repository #rev-list .sha.label.isSigned.isVerified,
13401350
.repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isVerified {
13411351
border: 1px solid var(--color-green-badge);
@@ -1344,6 +1354,7 @@
13441354

13451355
.repository #commits-table td.sha .sha.label.isSigned.isVerified .detail.icon,
13461356
.repository #repo-files-table .sha.label.isSigned.isVerified .detail.icon,
1357+
.repository #repo-file-commit-box .sha.label.isSigned.isVerified .detail.icon,
13471358
.repository #rev-list .sha.label.isSigned.isVerified .detail.icon,
13481359
.repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isVerified .detail.icon {
13491360
border-left: 1px solid var(--color-green-badge);
@@ -1352,13 +1363,15 @@
13521363

13531364
.repository #commits-table td.sha .sha.label.isSigned.isVerified:hover,
13541365
.repository #repo-files-table .sha.label.isSigned.isVerified:hover,
1366+
.repository #repo-file-commit-box .sha.label.isSigned.isVerified:hover,
13551367
.repository #rev-list .sha.label.isSigned.isVerified:hover,
13561368
.repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isVerified:hover {
13571369
background: var(--color-green-badge-hover-bg) !important;
13581370
}
13591371

13601372
.repository #commits-table td.sha .sha.label.isSigned.isVerifiedUntrusted,
13611373
.repository #repo-files-table .sha.label.isSigned.isVerifiedUntrusted,
1374+
.repository #repo-file-commit-box .sha.label.isSigned.isVerifiedUntrusted,
13621375
.repository #rev-list .sha.label.isSigned.isVerifiedUntrusted,
13631376
.repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isVerifiedUntrusted {
13641377
border: 1px solid var(--color-yellow-badge);
@@ -1367,6 +1380,7 @@
13671380

13681381
.repository #commits-table td.sha .sha.label.isSigned.isVerifiedUntrusted .detail.icon,
13691382
.repository #repo-files-table .sha.label.isSigned.isVerifiedUntrusted .detail.icon,
1383+
.repository #repo-file-commit-box .sha.label.isSigned.isVerifiedUntrusted .detail.icon,
13701384
.repository #rev-list .sha.label.isSigned.isVerifiedUntrusted .detail.icon,
13711385
.repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isVerifiedUntrusted .detail.icon {
13721386
border-left: 1px solid var(--color-yellow-badge);
@@ -1375,13 +1389,15 @@
13751389

13761390
.repository #commits-table td.sha .sha.label.isSigned.isVerifiedUntrusted:hover,
13771391
.repository #repo-files-table .sha.label.isSigned.isVerifiedUntrusted:hover,
1392+
.repository #repo-file-commit-box .sha.label.isSigned.isVerifiedUntrusted:hover,
13781393
.repository #rev-list .sha.label.isSigned.isVerifiedUntrusted:hover,
13791394
.repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isVerifiedUntrusted:hover {
13801395
background: var(--color-yellow-badge-hover-bg) !important;
13811396
}
13821397

13831398
.repository #commits-table td.sha .sha.label.isSigned.isVerifiedUnmatched,
13841399
.repository #repo-files-table .sha.label.isSigned.isVerifiedUnmatched,
1400+
.repository #repo-file-commit-box .sha.label.isSigned.isVerifiedUnmatched,
13851401
.repository #rev-list .sha.label.isSigned.isVerifiedUnmatched,
13861402
.repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isVerifiedUnmatched {
13871403
border: 1px solid var(--color-orange-badge);
@@ -1390,6 +1406,7 @@
13901406

13911407
.repository #commits-table td.sha .sha.label.isSigned.isVerifiedUnmatched .detail.icon,
13921408
.repository #repo-files-table .sha.label.isSigned.isVerifiedUnmatched .detail.icon,
1409+
.repository #repo-file-commit-box .sha.label.isSigned.isVerifiedUnmatched .detail.icon,
13931410
.repository #rev-list .sha.label.isSigned.isVerifiedUnmatched .detail.icon,
13941411
.repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isVerifiedUnmatched .detail.icon {
13951412
border-left: 1px solid var(--color-orange-badge);
@@ -1398,6 +1415,7 @@
13981415

13991416
.repository #commits-table td.sha .sha.label.isSigned.isVerifiedUnmatched:hover,
14001417
.repository #repo-files-table .sha.label.isSigned.isVerifiedUnmatched:hover,
1418+
.repository #repo-file-commit-box .sha.label.isSigned.isVerifiedUnmatched:hover,
14011419
.repository #rev-list .sha.label.isSigned.isVerifiedUnmatched:hover,
14021420
.repository .timeline-item.commits-list .singular-commit .sha.label.isSigned.isVerifiedUnmatched:hover {
14031421
background: var(--color-orange-badge-hover-bg) !important;

0 commit comments

Comments
 (0)