Skip to content

Commit 0581b27

Browse files
committed
Refactor the structure of the commit list page to grouping by date.
1 parent 073c593 commit 0581b27

11 files changed

+285
-26
lines changed

routers/web/repo/commit.go

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net/http"
1212
"path"
1313
"strings"
14+
"time"
1415

1516
asymkey_model "code.gitea.io/gitea/models/asymkey"
1617
"code.gitea.io/gitea/models/db"
@@ -82,11 +83,12 @@ func Commits(ctx *context.Context) {
8283
ctx.ServerError("CommitsByRange", err)
8384
return
8485
}
85-
ctx.Data["Commits"], err = processGitCommits(ctx, commits)
86+
processedCommits, err := processGitCommits(ctx, commits)
8687
if err != nil {
8788
ctx.ServerError("processGitCommits", err)
8889
return
8990
}
91+
ctx.Data["GroupCommits"] = GroupCommitsByDate(processedCommits)
9092
commitIDs := make([]string, 0, len(commits))
9193
for _, c := range commits {
9294
commitIDs = append(commitIDs, c.ID.String())
@@ -198,11 +200,12 @@ func SearchCommits(ctx *context.Context) {
198200
return
199201
}
200202
ctx.Data["CommitCount"] = len(commits)
201-
ctx.Data["Commits"], err = processGitCommits(ctx, commits)
203+
processedCommits, err := processGitCommits(ctx, commits)
202204
if err != nil {
203205
ctx.ServerError("processGitCommits", err)
204206
return
205207
}
208+
ctx.Data["GroupCommits"] = GroupCommitsByDate(processedCommits)
206209

207210
ctx.Data["Keyword"] = query
208211
if all {
@@ -244,11 +247,12 @@ func FileHistory(ctx *context.Context) {
244247
ctx.ServerError("CommitsByFileAndRange", err)
245248
return
246249
}
247-
ctx.Data["Commits"], err = processGitCommits(ctx, commits)
250+
processedCommits, err := processGitCommits(ctx, commits)
248251
if err != nil {
249252
ctx.ServerError("processGitCommits", err)
250253
return
251254
}
255+
ctx.Data["GroupCommits"] = GroupCommitsByDate(processedCommits)
252256

253257
ctx.Data["Username"] = ctx.Repo.Owner.Name
254258
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
@@ -458,3 +462,38 @@ func processGitCommits(ctx *context.Context, gitCommits []*git.Commit) ([]*git_m
458462
}
459463
return commits, nil
460464
}
465+
466+
// GroupedCommits defines the structure for grouped commits.
467+
type GroupedCommits struct {
468+
Date time.Time
469+
Commits []*git_model.SignCommitWithStatuses
470+
}
471+
472+
// GroupCommitsByDate groups the commits by date (in days).
473+
func GroupCommitsByDate(commits []*git_model.SignCommitWithStatuses) []GroupedCommits {
474+
grouped := make(map[string][]*git_model.SignCommitWithStatuses)
475+
476+
for _, commit := range commits {
477+
var sigTime time.Time
478+
if commit.Committer != nil {
479+
sigTime = commit.Committer.When
480+
} else if commit.Author != nil {
481+
sigTime = commit.Author.When
482+
}
483+
484+
// Extract the date part
485+
date := sigTime.Format("2006-01-02")
486+
grouped[date] = append(grouped[date], commit)
487+
}
488+
489+
result := make([]GroupedCommits, 0, len(grouped))
490+
for dateStr, commitsGroup := range grouped {
491+
date, _ := time.Parse("2006-01-02", dateStr)
492+
result = append(result, GroupedCommits{
493+
Date: date,
494+
Commits: commitsGroup,
495+
})
496+
}
497+
498+
return result
499+
}

routers/web/repo/compare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ func PrepareCompareDiff(
661661
ctx.ServerError("processGitCommits", err)
662662
return false
663663
}
664-
ctx.Data["Commits"] = commits
664+
ctx.Data["GroupCommits"] = GroupCommitsByDate(commits)
665665
ctx.Data["CommitCount"] = len(commits)
666666

667667
title := ci.HeadBranch

routers/web/repo/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ func ViewPullCommits(ctx *context.Context) {
627627
ctx.ServerError("processGitCommits", err)
628628
return
629629
}
630-
ctx.Data["Commits"] = commits
630+
ctx.Data["GroupCommits"] = GroupCommitsByDate(commits)
631631
ctx.Data["CommitCount"] = len(commits)
632632

633633
ctx.Data["HasIssuesOrPullsWritePermission"] = ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{{$index := 0}}
2+
{{range .GroupCommits}}
3+
{{$index = Eval $index "+" 1}}
4+
<div class="ui timeline commits_list_group_by_date" data-index="{{$index}}">
5+
6+
<div class="timeline-badge-wrapper">
7+
<div class="timeline-badge">
8+
{{svg "octicon-git-commit"}}
9+
</div>
10+
</div>
11+
12+
<div class="timeline-body">
13+
<h3 class="flex-text-block tw-py-2 timeline-heading">
14+
Commits on {{DateUtils.AbsoluteShort .Date}}
15+
</h3>
16+
<div class="tw-flex tw-mt-2 timeline-list-container">
17+
<ul class="commits-list">
18+
{{range .Commits}}
19+
{{$commitRepoLink := $.RepoLink}}{{if $.CommitRepoLink}}{{$commitRepoLink = $.CommitRepoLink}}{{end}}
20+
<li class="commits-list-item">
21+
<div class="tw-pt-4 tw-pl-4 title">
22+
<h4>
23+
<span class="message-wrapper">
24+
{{if $.PageIsWiki}}
25+
<span class="commit-summary {{if gt .ParentCount 1}} grey text{{end}}" title="{{.Summary}}">{{.Summary | ctx.RenderUtils.RenderEmoji}}</span>
26+
{{else}}
27+
{{$commitLink:= printf "%s/commit/%s" $commitRepoLink (PathEscape .ID.String)}}
28+
<span class="commit-summary {{if gt .ParentCount 1}} grey text{{end}}" title="{{.Summary}}">{{ctx.RenderUtils.RenderCommitMessageLinkSubject .Message $commitLink ($.Repository.ComposeMetas ctx)}}</span>
29+
{{end}}
30+
</span>
31+
{{if IsMultilineCommitMessage .Message}}
32+
<button class="ui button ellipsis-button" aria-expanded="false" data-global-click="onRepoEllipsisButtonClick">...</button>
33+
{{end}}
34+
{{if IsMultilineCommitMessage .Message}}
35+
<pre class="commit-body tw-hidden">{{ctx.RenderUtils.RenderCommitBody .Message ($.Repository.ComposeMetas ctx)}}</pre>
36+
{{end}}
37+
{{if $.CommitsTagsMap}}
38+
{{range (index $.CommitsTagsMap .ID.String)}}
39+
{{- template "repo/tag/name" dict "RepoLink" $.Repository.Link "TagName" .TagName "IsRelease" (not .IsTag) -}}
40+
{{end}}
41+
{{end}}
42+
</h4>
43+
</div>
44+
<div class="tw-flex tw-items-center tw-gap-1 tw-pb-4 tw-pl-4 description">
45+
<div class="author">
46+
{{$userName := .Author.Name}}
47+
{{if .User}}
48+
{{if and .User.FullName DefaultShowFullName}}
49+
{{$userName = .User.FullName}}
50+
{{end}}
51+
{{ctx.AvatarUtils.Avatar .User 28 "tw-mr-2"}}<a class="muted author-wrapper" href="{{.User.HomeLink}}">{{$userName}}</a>
52+
{{else}}
53+
{{ctx.AvatarUtils.AvatarByEmail .Author.Email .Author.Name 28 "tw-mr-2"}}
54+
<span class="author-wrapper">{{$userName}}</span>
55+
{{end}}
56+
</div>
57+
<span>
58+
{{if .Committer}}
59+
committed
60+
{{else}}
61+
authored
62+
{{end}}
63+
</span>
64+
{{if .Committer}}
65+
{{DateUtils.TimeSince .Committer.When}}
66+
{{else}}
67+
{{DateUtils.TimeSince .Author.When}}
68+
{{end}}
69+
{{if .Statuses}}
70+
<span>·</span>
71+
{{end}}
72+
{{template "repo/commit_statuses" dict "Status" .Status "Statuses" .Statuses}}
73+
</div>
74+
<div class="tw-flex tw-flex-wrap tw-items-center tw-gap-2 tw-pr-4 metadata">
75+
{{$commitBaseLink := ""}}
76+
{{if $.PageIsWiki}}
77+
{{$commitBaseLink = printf "%s/wiki/commit" $commitRepoLink}}
78+
{{else if $.PageIsPullCommits}}
79+
{{$commitBaseLink = printf "%s/pulls/%d/commits" $commitRepoLink $.Issue.Index}}
80+
{{else if $.Reponame}}
81+
{{$commitBaseLink = printf "%s/commit" $commitRepoLink}}
82+
{{end}}
83+
<div class="commit_sign_badge">
84+
{{template "repo/commit_sign_badge" dict "Commit" . "CommitBaseLink" $commitBaseLink "CommitSignVerification" .Verification}}
85+
</div>
86+
<div class="tw-flex tw-flex-wrap tw-items-center tw-gap-2">
87+
<div>
88+
<button class="btn interact-bg tw-p-2 copy-commit-id" data-tooltip-content="{{ctx.Locale.Tr "copy_hash"}}" data-clipboard-text="{{.ID}}">{{svg "octicon-copy"}}</button>
89+
</div>
90+
{{/* at the moment, wiki doesn't support these "view" links like "view at history point" */}}
91+
{{if not $.PageIsWiki}}
92+
{{/* view single file diff */}}
93+
{{if $.FileName}}
94+
<a class="btn interact-bg tw-p-2 view-single-diff" data-tooltip-content="{{ctx.Locale.Tr "repo.commits.view_file_diff"}}"
95+
href="{{$commitRepoLink}}/commit/{{.ID.String}}?files={{$.FileName}}"
96+
>{{svg "octicon-file-diff"}}</a>
97+
{{end}}
98+
99+
{{/* view at history point */}}
100+
{{$viewCommitLink := printf "%s/src/commit/%s" $commitRepoLink (PathEscape .ID.String)}}
101+
{{if $.FileName}}{{$viewCommitLink = printf "%s/%s" $viewCommitLink (PathEscapeSegments $.FileName)}}{{end}}
102+
<a class="btn interact-bg tw-p-2 view-commit-path" data-tooltip-content="{{ctx.Locale.Tr "repo.commits.view_path"}}" href="{{$viewCommitLink}}">{{svg "octicon-file-code"}}</a>
103+
{{end}}
104+
</div>
105+
</div>
106+
</li>
107+
{{end}}
108+
</ul>
109+
</div>
110+
</div>
111+
112+
</div>
113+
{{end}}

templates/repo/commits_table.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
</div>
3030
{{end}}
3131

32-
{{if and .Commits (gt .CommitCount 0)}}
33-
{{template "repo/commits_list" .}}
32+
{{if and .GroupCommits (gt .CommitCount 0)}}
33+
{{template "repo/commits_list_group_by_date" .}}
3434
{{end}}
3535

3636
{{template "base/paginate" .}}

tests/integration/git_general_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ func doAutoPRMerge(baseCtx *APITestContext, dstPath string) func(t *testing.T) {
669669
doc := NewHTMLParser(t, resp.Body)
670670

671671
// Get first commit URL
672-
commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Last().Attr("href")
672+
commitURL, exists := doc.doc.Find(".timeline.commits_list_group_by_date .commit_sign_badge a").Last().Attr("href")
673673
assert.True(t, exists)
674674
assert.NotEmpty(t, commitURL)
675675

tests/integration/pull_status_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestPullCreate_CommitStatus(t *testing.T) {
4545
doc := NewHTMLParser(t, resp.Body)
4646

4747
// Get first commit URL
48-
commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Last().Attr("href")
48+
commitURL, exists := doc.doc.Find(".timeline.commits_list_group_by_date .commit_sign_badge a").Last().Attr("href")
4949
assert.True(t, exists)
5050
assert.NotEmpty(t, commitURL)
5151

@@ -83,12 +83,12 @@ func TestPullCreate_CommitStatus(t *testing.T) {
8383
resp = session.MakeRequest(t, req, http.StatusOK)
8484
doc = NewHTMLParser(t, resp.Body)
8585

86-
commitURL, exists = doc.doc.Find("#commits-table tbody tr td.sha a").Last().Attr("href")
86+
commitURL, exists = doc.doc.Find(".timeline.commits_list_group_by_date .commit_sign_badge a").Last().Attr("href")
8787
assert.True(t, exists)
8888
assert.NotEmpty(t, commitURL)
8989
assert.Equal(t, commitID, path.Base(commitURL))
9090

91-
cls, ok := doc.doc.Find("#commits-table tbody tr td.message .commit-status").Last().Attr("class")
91+
cls, ok := doc.doc.Find(".timeline.commits_list_group_by_date .description .commit-status").Last().Attr("class")
9292
assert.True(t, ok)
9393
assert.Contains(t, cls, statesIcons[status])
9494
}

tests/integration/repo_commits_search_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func testRepoCommitsSearch(t *testing.T, query, commit string) {
2222
resp := session.MakeRequest(t, req, http.StatusOK)
2323

2424
doc := NewHTMLParser(t, resp.Body)
25-
sel := doc.doc.Find("#commits-table tbody tr td.sha a")
25+
sel := doc.doc.Find(".timeline.commits_list_group_by_date .commit_sign_badge a")
2626
assert.Equal(t, commit, strings.TrimSpace(sel.Text()))
2727
}
2828

tests/integration/repo_commits_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestRepoCommits(t *testing.T) {
3333
resp := session.MakeRequest(t, req, http.StatusOK)
3434

3535
doc := NewHTMLParser(t, resp.Body)
36-
commitURL, exists := doc.doc.Find("#commits-table .commit-id-short").Attr("href")
36+
commitURL, exists := doc.doc.Find(".timeline.commits_list_group_by_date .commit_sign_badge .commit-id-short").Attr("href")
3737
assert.True(t, exists)
3838
assert.NotEmpty(t, commitURL)
3939
}
@@ -50,7 +50,7 @@ func Test_ReposGitCommitListNotMaster(t *testing.T) {
5050

5151
doc := NewHTMLParser(t, resp.Body)
5252
commits := []string{}
53-
doc.doc.Find("#commits-table .commit-id-short").Each(func(i int, s *goquery.Selection) {
53+
doc.doc.Find(".timeline.commits_list_group_by_date .commit_sign_badge .commit-id-short").Each(func(i int, s *goquery.Selection) {
5454
commitURL, exists := s.Attr("href")
5555
assert.True(t, exists)
5656
assert.NotEmpty(t, commitURL)
@@ -63,7 +63,7 @@ func Test_ReposGitCommitListNotMaster(t *testing.T) {
6363
assert.Equal(t, "5099b81332712fe655e34e8dd63574f503f61811", commits[2])
6464

6565
userNames := []string{}
66-
doc.doc.Find("#commits-table .author-wrapper").Each(func(i int, s *goquery.Selection) {
66+
doc.doc.Find(".timeline.commits_list_group_by_date .description .author-wrapper").Each(func(i int, s *goquery.Selection) {
6767
userPath, exists := s.Attr("href")
6868
assert.True(t, exists)
6969
assert.NotEmpty(t, userPath)
@@ -87,7 +87,7 @@ func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) {
8787

8888
doc := NewHTMLParser(t, resp.Body)
8989
// Get first commit URL
90-
commitURL, exists := doc.doc.Find("#commits-table .commit-id-short").Attr("href")
90+
commitURL, exists := doc.doc.Find(".timeline.commits_list_group_by_date .commit_sign_badge .commit-id-short").Attr("href")
9191
assert.True(t, exists)
9292
assert.NotEmpty(t, commitURL)
9393

@@ -105,7 +105,7 @@ func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) {
105105

106106
doc = NewHTMLParser(t, resp.Body)
107107
// Check if commit status is displayed in message column (.tippy-target to ignore the tippy trigger)
108-
sel := doc.doc.Find("#commits-table .message .tippy-target .commit-status")
108+
sel := doc.doc.Find(".timeline.commits_list_group_by_date .description .tippy-target .commit-status")
109109
assert.Equal(t, 1, sel.Length())
110110
for _, class := range classes {
111111
assert.True(t, sel.HasClass(class))
@@ -181,7 +181,7 @@ func TestRepoCommitsStatusParallel(t *testing.T) {
181181

182182
doc := NewHTMLParser(t, resp.Body)
183183
// Get first commit URL
184-
commitURL, exists := doc.doc.Find("#commits-table .commit-id-short").Attr("href")
184+
commitURL, exists := doc.doc.Find(".timeline.commits_list_group_by_date .commit_sign_badge .commit-id-short").Attr("href")
185185
assert.True(t, exists)
186186
assert.NotEmpty(t, commitURL)
187187

@@ -216,7 +216,7 @@ func TestRepoCommitsStatusMultiple(t *testing.T) {
216216

217217
doc := NewHTMLParser(t, resp.Body)
218218
// Get first commit URL
219-
commitURL, exists := doc.doc.Find("#commits-table .commit-id-short").Attr("href")
219+
commitURL, exists := doc.doc.Find(".timeline.commits_list_group_by_date .commit_sign_badge .commit-id-short").Attr("href")
220220
assert.True(t, exists)
221221
assert.NotEmpty(t, commitURL)
222222

@@ -241,6 +241,6 @@ func TestRepoCommitsStatusMultiple(t *testing.T) {
241241

242242
doc = NewHTMLParser(t, resp.Body)
243243
// Check that the data-global-init="initCommitStatuses" (for trigger) and commit-status (svg) are present
244-
sel := doc.doc.Find(`#commits-table .message [data-global-init="initCommitStatuses"] .commit-status`)
244+
sel := doc.doc.Find(`.timeline.commits_list_group_by_date .description [data-global-init="initCommitStatuses"] .commit-status`)
245245
assert.Equal(t, 1, sel.Length())
246246
}

tests/integration/view_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ func TestCommitListActions(t *testing.T) {
4848
resp := session.MakeRequest(t, req, http.StatusOK)
4949
htmlDoc := NewHTMLParser(t, resp.Body)
5050

51-
AssertHTMLElement(t, htmlDoc, `.commit-list .copy-commit-id`, true)
52-
AssertHTMLElement(t, htmlDoc, `.commit-list .view-single-diff`, false)
53-
AssertHTMLElement(t, htmlDoc, `.commit-list .view-commit-path`, true)
51+
AssertHTMLElement(t, htmlDoc, `.timeline.commits_list_group_by_date .copy-commit-id`, true)
52+
AssertHTMLElement(t, htmlDoc, `.timeline.commits_list_group_by_date .view-single-diff`, false)
53+
AssertHTMLElement(t, htmlDoc, `.timeline.commits_list_group_by_date .view-commit-path`, true)
5454
})
5555

5656
t.Run("RepoFileHistory", func(t *testing.T) {
@@ -60,8 +60,8 @@ func TestCommitListActions(t *testing.T) {
6060
resp := session.MakeRequest(t, req, http.StatusOK)
6161
htmlDoc := NewHTMLParser(t, resp.Body)
6262

63-
AssertHTMLElement(t, htmlDoc, `.commit-list .copy-commit-id`, true)
64-
AssertHTMLElement(t, htmlDoc, `.commit-list .view-single-diff`, true)
65-
AssertHTMLElement(t, htmlDoc, `.commit-list .view-commit-path`, true)
63+
AssertHTMLElement(t, htmlDoc, `.timeline.commits_list_group_by_date .copy-commit-id`, true)
64+
AssertHTMLElement(t, htmlDoc, `.timeline.commits_list_group_by_date .view-single-diff`, true)
65+
AssertHTMLElement(t, htmlDoc, `.timeline.commits_list_group_by_date .view-commit-path`, true)
6666
})
6767
}

0 commit comments

Comments
 (0)