Skip to content

Commit fd2fc61

Browse files
committed
clean up code-expander-button
1 parent aafa231 commit fd2fc61

File tree

11 files changed

+140
-201
lines changed

11 files changed

+140
-201
lines changed

routers/web/repo/commit.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -276,20 +276,24 @@ func Diff(ctx *context.Context) {
276276
userName := ctx.Repo.Owner.Name
277277
repoName := ctx.Repo.Repository.Name
278278
commitID := ctx.PathParam("sha")
279-
var (
280-
gitRepo *git.Repository
281-
err error
282-
)
279+
280+
diffBlobExcerptData := &gitdiff.DiffBlobExcerptData{
281+
BaseLink: ctx.Repo.RepoLink + "/blob_excerpt",
282+
DiffStyle: ctx.FormString("style"),
283+
AfterCommitID: commitID,
284+
}
285+
gitRepo := ctx.Repo.GitRepo
286+
var gitRepoStore gitrepo.Repository = ctx.Repo.Repository
283287

284288
if ctx.Data["PageIsWiki"] != nil {
285-
gitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository.WikiStorageRepo())
289+
var err error
290+
gitRepoStore = ctx.Repo.Repository.WikiStorageRepo()
291+
gitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, gitRepoStore)
286292
if err != nil {
287293
ctx.ServerError("Repo.GitRepo.GetCommit", err)
288294
return
289295
}
290-
defer gitRepo.Close()
291-
} else {
292-
gitRepo = ctx.Repo.GitRepo
296+
diffBlobExcerptData.BaseLink = ctx.Repo.RepoLink + "/wiki/blob_excerpt"
293297
}
294298

295299
commit, err := gitRepo.GetCommit(commitID)
@@ -324,7 +328,7 @@ func Diff(ctx *context.Context) {
324328
ctx.NotFound(err)
325329
return
326330
}
327-
diffShortStat, err := gitdiff.GetDiffShortStat(ctx, ctx.Repo.Repository, gitRepo, "", commitID)
331+
diffShortStat, err := gitdiff.GetDiffShortStat(ctx, gitRepoStore, gitRepo, "", commitID)
328332
if err != nil {
329333
ctx.ServerError("GetDiffShortStat", err)
330334
return
@@ -360,6 +364,7 @@ func Diff(ctx *context.Context) {
360364
ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID)
361365
ctx.Data["Commit"] = commit
362366
ctx.Data["Diff"] = diff
367+
ctx.Data["DiffBlobExcerptData"] = diffBlobExcerptData
363368

364369
if !fileOnly {
365370
diffTree, err := gitdiff.GetDiffTree(ctx, gitRepo, false, parentCommitID, commitID)

routers/web/repo/compare.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -885,9 +885,7 @@ func attachCommentsToLines(section *gitdiff.DiffSection, lineComments map[int64]
885885
// attachHiddenCommentIDs calculates and attaches hidden comment IDs to expand buttons
886886
func attachHiddenCommentIDs(section *gitdiff.DiffSection, lineComments map[int64][]*issues_model.Comment) {
887887
for _, line := range section.Lines {
888-
if hiddenIDs := gitdiff.CalculateHiddenCommentIDsForLine(line, lineComments); len(hiddenIDs) > 0 {
889-
line.HiddenCommentIDs = hiddenIDs
890-
}
888+
gitdiff.FillHiddenCommentIDsForDiffLine(line, lineComments)
891889
}
892890
}
893891

@@ -904,15 +902,23 @@ func ExcerptBlob(ctx *context.Context) {
904902
direction := ctx.FormString("direction")
905903
filePath := ctx.FormString("path")
906904
gitRepo := ctx.Repo.GitRepo
905+
906+
diffBlobExcerptData := &gitdiff.DiffBlobExcerptData{
907+
BaseLink: ctx.Repo.RepoLink + "/blob_excerpt",
908+
DiffStyle: ctx.FormString("style"),
909+
AfterCommitID: commitID,
910+
}
911+
907912
if ctx.Data["PageIsWiki"] == true {
908913
var err error
909-
gitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository.WikiStorageRepo())
914+
gitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository.WikiStorageRepo())
910915
if err != nil {
911916
ctx.ServerError("OpenRepository", err)
912917
return
913918
}
914-
defer gitRepo.Close()
919+
diffBlobExcerptData.BaseLink = ctx.Repo.RepoLink + "/wiki/blob_excerpt"
915920
}
921+
916922
chunkSize := gitdiff.BlobExcerptChunkSize
917923
commit, err := gitRepo.GetCommit(commitID)
918924
if err != nil {
@@ -985,6 +991,7 @@ func ExcerptBlob(ctx *context.Context) {
985991
return
986992
}
987993
issueIndex := ctx.FormInt64("issue_index")
994+
diffBlobExcerptData.PullIndex = issueIndex
988995
ctx.Data["IssueIndex"] = issueIndex
989996
if issueIndex > 0 {
990997
if issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, issueIndex); err == nil && issue.IsPull {
@@ -1007,6 +1014,8 @@ func ExcerptBlob(ctx *context.Context) {
10071014
ctx.Data["FileNameHash"] = git.HashFilePathForWebUI(filePath)
10081015
ctx.Data["AfterCommitID"] = commitID
10091016
ctx.Data["Anchor"] = anchor
1017+
ctx.Data["DiffBlobExcerptData"] = diffBlobExcerptData
1018+
10101019
ctx.HTML(http.StatusOK, tplBlobExcerpt)
10111020
}
10121021

routers/web/repo/pull.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,12 @@ func viewPullFiles(ctx *context.Context, beforeCommitID, afterCommitID string) {
827827
}
828828

829829
ctx.Data["Diff"] = diff
830+
ctx.Data["DiffBlobExcerptData"] = &gitdiff.DiffBlobExcerptData{
831+
BaseLink: ctx.Repo.RepoLink + "/blob_excerpt",
832+
PullIndex: pull.Index,
833+
DiffStyle: ctx.FormString("style"),
834+
AfterCommitID: afterCommitID,
835+
}
830836
ctx.Data["DiffNotAvailable"] = diffShortStat.NumFiles == 0
831837

832838
if ctx.IsSigned && ctx.Doer != nil {

services/gitdiff/gitdiff.go

Lines changed: 75 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,21 @@ import (
2222
git_model "code.gitea.io/gitea/models/git"
2323
issues_model "code.gitea.io/gitea/models/issues"
2424
pull_model "code.gitea.io/gitea/models/pull"
25-
repo_model "code.gitea.io/gitea/models/repo"
2625
user_model "code.gitea.io/gitea/models/user"
2726
"code.gitea.io/gitea/modules/analyze"
27+
"code.gitea.io/gitea/modules/base"
2828
"code.gitea.io/gitea/modules/charset"
2929
"code.gitea.io/gitea/modules/git"
3030
"code.gitea.io/gitea/modules/git/attribute"
3131
"code.gitea.io/gitea/modules/git/gitcmd"
3232
"code.gitea.io/gitea/modules/gitrepo"
3333
"code.gitea.io/gitea/modules/highlight"
34+
"code.gitea.io/gitea/modules/htmlutil"
3435
"code.gitea.io/gitea/modules/lfs"
3536
"code.gitea.io/gitea/modules/log"
3637
"code.gitea.io/gitea/modules/optional"
3738
"code.gitea.io/gitea/modules/setting"
39+
"code.gitea.io/gitea/modules/svg"
3840
"code.gitea.io/gitea/modules/translation"
3941
"code.gitea.io/gitea/modules/util"
4042

@@ -67,28 +69,15 @@ const (
6769
DiffFileCopy
6870
)
6971

70-
// DiffLineExpandDirection represents the DiffLineSection expand direction
71-
type DiffLineExpandDirection uint8
72-
73-
// DiffLineExpandDirection possible values.
74-
const (
75-
DiffLineExpandNone DiffLineExpandDirection = iota + 1
76-
DiffLineExpandSingle
77-
DiffLineExpandUpDown
78-
DiffLineExpandUp
79-
DiffLineExpandDown
80-
)
81-
8272
// DiffLine represents a line difference in a DiffSection.
8373
type DiffLine struct {
84-
LeftIdx int // line number, 1-based
85-
RightIdx int // line number, 1-based
86-
Match int // the diff matched index. -1: no match. 0: plain and no need to match. >0: for add/del, "Lines" slice index of the other side
87-
Type DiffLineType
88-
Content string
89-
Comments issues_model.CommentList // related PR code comments
90-
SectionInfo *DiffLineSectionInfo
91-
HiddenCommentIDs []int64 // IDs of hidden comments in this section
74+
LeftIdx int // line number, 1-based
75+
RightIdx int // line number, 1-based
76+
Match int // the diff matched index. -1: no match. 0: plain and no need to match. >0: for add/del, "Lines" slice index of the other side
77+
Type DiffLineType
78+
Content string
79+
Comments issues_model.CommentList // related PR code comments
80+
SectionInfo *DiffLineSectionInfo
9281
}
9382

9483
// DiffLineSectionInfo represents diff line section meta data
@@ -100,6 +89,9 @@ type DiffLineSectionInfo struct {
10089
RightIdx int
10190
LeftHunkSize int
10291
RightHunkSize int
92+
93+
HiddenCommentIDs []int64 // IDs of hidden comments in this section
94+
10395
}
10496

10597
// DiffHTMLOperation is the HTML version of diffmatchpatch.Diff
@@ -154,8 +146,7 @@ func (d *DiffLine) GetLineTypeMarker() string {
154146
return ""
155147
}
156148

157-
// GetBlobExcerptQuery builds query string to get blob excerpt
158-
func (d *DiffLine) GetBlobExcerptQuery() string {
149+
func (d *DiffLine) getBlobExcerptQuery() string {
159150
query := fmt.Sprintf(
160151
"last_left=%d&last_right=%d&"+
161152
"left=%d&right=%d&"+
@@ -168,25 +159,70 @@ func (d *DiffLine) GetBlobExcerptQuery() string {
168159
return query
169160
}
170161

171-
// GetExpandDirection gets DiffLineExpandDirection
172-
func (d *DiffLine) GetExpandDirection() DiffLineExpandDirection {
162+
func (d *DiffLine) getExpandDirection() string {
173163
if d.Type != DiffLineSection || d.SectionInfo == nil || d.SectionInfo.LeftIdx-d.SectionInfo.LastLeftIdx <= 1 || d.SectionInfo.RightIdx-d.SectionInfo.LastRightIdx <= 1 {
174-
return DiffLineExpandNone
164+
return ""
175165
}
176166
if d.SectionInfo.LastLeftIdx <= 0 && d.SectionInfo.LastRightIdx <= 0 {
177-
return DiffLineExpandUp
167+
return "up"
178168
} else if d.SectionInfo.RightIdx-d.SectionInfo.LastRightIdx > BlobExcerptChunkSize && d.SectionInfo.RightHunkSize > 0 {
179-
return DiffLineExpandUpDown
169+
return "updown"
180170
} else if d.SectionInfo.LeftHunkSize <= 0 && d.SectionInfo.RightHunkSize <= 0 {
181-
return DiffLineExpandDown
171+
return "down"
182172
}
183-
return DiffLineExpandSingle
173+
return "single"
184174
}
185175

186-
// CalculateHiddenCommentIDsForLine finds comment IDs that are in the hidden range of an expand button
187-
func CalculateHiddenCommentIDsForLine(line *DiffLine, lineComments map[int64][]*issues_model.Comment) []int64 {
188-
if line.Type != DiffLineSection || line.SectionInfo == nil {
189-
return nil
176+
type DiffBlobExcerptData struct {
177+
BaseLink string
178+
IsWikiRepo bool
179+
PullIndex int64
180+
DiffStyle string
181+
AfterCommitID string
182+
}
183+
184+
func (d *DiffLine) RenderBlobExcerptButtons(fileNameHash string, data *DiffBlobExcerptData) template.HTML {
185+
dataHiddenCommentIDs := strings.Join(base.Int64sToStrings(d.SectionInfo.HiddenCommentIDs), ",")
186+
anchor := fmt.Sprintf("diff-%sK%d", fileNameHash, d.SectionInfo.RightIdx)
187+
188+
makeButton := func(direction, svgName string) template.HTML {
189+
style := util.IfZero(data.DiffStyle, "unified")
190+
link := data.BaseLink + "/" + data.AfterCommitID + fmt.Sprintf("?style=%s&direction=%s&anchor=%s", url.QueryEscape(style), direction, url.QueryEscape(anchor))
191+
if data.PullIndex > 0 {
192+
link += fmt.Sprintf("&is_pull=1&issue_index=%d", data.PullIndex)
193+
}
194+
link += "&" + d.getBlobExcerptQuery()
195+
196+
svgContent := svg.RenderHTML(svgName)
197+
return htmlutil.HTMLFormat(
198+
`<button class="code-expander-button" hx-target="closest tr" hx-get="%s" data-hidden-comment-ids="%s">%s</button>`,
199+
link, dataHiddenCommentIDs, svgContent,
200+
)
201+
}
202+
var content template.HTML
203+
204+
if len(d.SectionInfo.HiddenCommentIDs) > 0 {
205+
tooltip := fmt.Sprintf("%d hidden comment(s)", len(d.SectionInfo.HiddenCommentIDs))
206+
content += htmlutil.HTMLFormat(`<span class="code-comment-more" data-tooltip-content="%s">%d</span>`, tooltip, len(d.SectionInfo.HiddenCommentIDs))
207+
}
208+
209+
expandDirection := d.getExpandDirection()
210+
if expandDirection == "up" || expandDirection == "updown" {
211+
content += makeButton("up", "octicon-fold-up")
212+
}
213+
if expandDirection == "updown" || expandDirection == "down" {
214+
content += makeButton("down", "octicon-fold-down")
215+
}
216+
if expandDirection == "single" {
217+
content += makeButton("single", "octicon-fold")
218+
}
219+
return htmlutil.HTMLFormat(`<div class="code-expander-buttons" data-expand-direction="%s">%s</div>`, expandDirection, content)
220+
}
221+
222+
// FillHiddenCommentIDsForDiffLine finds comment IDs that are in the hidden range of an expand button
223+
func FillHiddenCommentIDsForDiffLine(line *DiffLine, lineComments map[int64][]*issues_model.Comment) {
224+
if line.Type != DiffLineSection {
225+
return
190226
}
191227

192228
var hiddenCommentIDs []int64
@@ -196,13 +232,13 @@ func CalculateHiddenCommentIDsForLine(line *DiffLine, lineComments map[int64][]*
196232
absLineNum = int(-commentLineNum)
197233
}
198234
// Check if comments are in the hidden range
199-
if absLineNum > line.SectionInfo.LastRightIdx && absLineNum < line.SectionInfo.RightIdx {
235+
if absLineNum > line.SectionInfo.LastRightIdx && absLineNum <= line.SectionInfo.RightIdx {
200236
for _, comment := range comments {
201237
hiddenCommentIDs = append(hiddenCommentIDs, comment.ID)
202238
}
203239
}
204240
}
205-
return hiddenCommentIDs
241+
line.SectionInfo.HiddenCommentIDs = hiddenCommentIDs
206242
}
207243

208244
func getDiffLineSectionInfo(treePath, line string, lastLeftIdx, lastRightIdx int) *DiffLineSectionInfo {
@@ -508,11 +544,8 @@ func (diff *Diff) LoadComments(ctx context.Context, issue *issues_model.Issue, c
508544
sort.SliceStable(line.Comments, func(i, j int) bool {
509545
return line.Comments[i].CreatedUnix < line.Comments[j].CreatedUnix
510546
})
511-
512547
// Mark expand buttons that have comments in hidden lines
513-
if hiddenIDs := CalculateHiddenCommentIDsForLine(line, lineCommits); len(hiddenIDs) > 0 {
514-
line.HiddenCommentIDs = hiddenIDs
515-
}
548+
FillHiddenCommentIDsForDiffLine(line, lineCommits)
516549
}
517550
}
518551
}
@@ -1309,7 +1342,7 @@ type DiffShortStat struct {
13091342
NumFiles, TotalAddition, TotalDeletion int
13101343
}
13111344

1312-
func GetDiffShortStat(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, beforeCommitID, afterCommitID string) (*DiffShortStat, error) {
1345+
func GetDiffShortStat(ctx context.Context, repoStorage gitrepo.Repository, gitRepo *git.Repository, beforeCommitID, afterCommitID string) (*DiffShortStat, error) {
13131346
afterCommit, err := gitRepo.GetCommit(afterCommitID)
13141347
if err != nil {
13151348
return nil, err
@@ -1321,7 +1354,7 @@ func GetDiffShortStat(ctx context.Context, repo *repo_model.Repository, gitRepo
13211354
}
13221355

13231356
diff := &DiffShortStat{}
1324-
diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = gitrepo.GetDiffShortStatByCmdArgs(ctx, repo, nil, actualBeforeCommitID.String(), afterCommitID)
1357+
diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = gitrepo.GetDiffShortStatByCmdArgs(ctx, repoStorage, nil, actualBeforeCommitID.String(), afterCommitID)
13251358
if err != nil {
13261359
return nil, err
13271360
}

services/gitdiff/gitdiff_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ func TestGeneratePatchForUnchangedLineFromReader(t *testing.T) {
729729
--- a/main.go
730730
+++ b/main.go
731731
@@ -2,3 +2,3 @@
732-
732+
733733
func main() {
734734
fmt.Println("Hello")
735735
`,
@@ -851,12 +851,9 @@ func TestCalculateHiddenCommentIDsForLine(t *testing.T) {
851851

852852
for _, tt := range tests {
853853
t.Run(tt.name, func(t *testing.T) {
854-
result := CalculateHiddenCommentIDsForLine(tt.line, tt.lineComments)
855-
if tt.expected == nil {
856-
assert.Nil(t, result)
857-
} else {
858-
assert.ElementsMatch(t, tt.expected, result)
859-
}
854+
tt.line.SectionInfo = &DiffLineSectionInfo{}
855+
FillHiddenCommentIDsForDiffLine(tt.line, tt.lineComments)
856+
assert.ElementsMatch(t, tt.expected, tt.line.SectionInfo.HiddenCommentIDs)
860857
})
861858
}
862859
}

0 commit comments

Comments
 (0)