mirror of
https://github.com/go-gitea/gitea.git
synced 2025-11-14 12:36:56 +01:00
Merge branch 'main' into optimize-access-refresh-clean
This commit is contained in:
commit
06fd3ec507
@ -276,20 +276,24 @@ func Diff(ctx *context.Context) {
|
|||||||
userName := ctx.Repo.Owner.Name
|
userName := ctx.Repo.Owner.Name
|
||||||
repoName := ctx.Repo.Repository.Name
|
repoName := ctx.Repo.Repository.Name
|
||||||
commitID := ctx.PathParam("sha")
|
commitID := ctx.PathParam("sha")
|
||||||
var (
|
|
||||||
gitRepo *git.Repository
|
diffBlobExcerptData := &gitdiff.DiffBlobExcerptData{
|
||||||
err error
|
BaseLink: ctx.Repo.RepoLink + "/blob_excerpt",
|
||||||
)
|
DiffStyle: ctx.FormString("style"),
|
||||||
|
AfterCommitID: commitID,
|
||||||
|
}
|
||||||
|
gitRepo := ctx.Repo.GitRepo
|
||||||
|
var gitRepoStore gitrepo.Repository = ctx.Repo.Repository
|
||||||
|
|
||||||
if ctx.Data["PageIsWiki"] != nil {
|
if ctx.Data["PageIsWiki"] != nil {
|
||||||
gitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository.WikiStorageRepo())
|
var err error
|
||||||
|
gitRepoStore = ctx.Repo.Repository.WikiStorageRepo()
|
||||||
|
gitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, gitRepoStore)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.ServerError("Repo.GitRepo.GetCommit", err)
|
ctx.ServerError("Repo.GitRepo.GetCommit", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer gitRepo.Close()
|
diffBlobExcerptData.BaseLink = ctx.Repo.RepoLink + "/wiki/blob_excerpt"
|
||||||
} else {
|
|
||||||
gitRepo = ctx.Repo.GitRepo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
commit, err := gitRepo.GetCommit(commitID)
|
commit, err := gitRepo.GetCommit(commitID)
|
||||||
@ -324,7 +328,7 @@ func Diff(ctx *context.Context) {
|
|||||||
ctx.NotFound(err)
|
ctx.NotFound(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
diffShortStat, err := gitdiff.GetDiffShortStat(ctx, ctx.Repo.Repository, gitRepo, "", commitID)
|
diffShortStat, err := gitdiff.GetDiffShortStat(ctx, gitRepoStore, gitRepo, "", commitID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.ServerError("GetDiffShortStat", err)
|
ctx.ServerError("GetDiffShortStat", err)
|
||||||
return
|
return
|
||||||
@ -360,6 +364,7 @@ func Diff(ctx *context.Context) {
|
|||||||
ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID)
|
ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID)
|
||||||
ctx.Data["Commit"] = commit
|
ctx.Data["Commit"] = commit
|
||||||
ctx.Data["Diff"] = diff
|
ctx.Data["Diff"] = diff
|
||||||
|
ctx.Data["DiffBlobExcerptData"] = diffBlobExcerptData
|
||||||
|
|
||||||
if !fileOnly {
|
if !fileOnly {
|
||||||
diffTree, err := gitdiff.GetDiffTree(ctx, gitRepo, false, parentCommitID, commitID)
|
diffTree, err := gitdiff.GetDiffTree(ctx, gitRepo, false, parentCommitID, commitID)
|
||||||
|
|||||||
@ -14,6 +14,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/db"
|
"code.gitea.io/gitea/models/db"
|
||||||
@ -43,6 +44,7 @@ import (
|
|||||||
"code.gitea.io/gitea/services/context/upload"
|
"code.gitea.io/gitea/services/context/upload"
|
||||||
"code.gitea.io/gitea/services/gitdiff"
|
"code.gitea.io/gitea/services/gitdiff"
|
||||||
pull_service "code.gitea.io/gitea/services/pull"
|
pull_service "code.gitea.io/gitea/services/pull"
|
||||||
|
user_service "code.gitea.io/gitea/services/user"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -638,6 +640,11 @@ func PrepareCompareDiff(
|
|||||||
}
|
}
|
||||||
ctx.Data["DiffShortStat"] = diffShortStat
|
ctx.Data["DiffShortStat"] = diffShortStat
|
||||||
ctx.Data["Diff"] = diff
|
ctx.Data["Diff"] = diff
|
||||||
|
ctx.Data["DiffBlobExcerptData"] = &gitdiff.DiffBlobExcerptData{
|
||||||
|
BaseLink: ci.HeadRepo.Link() + "/blob_excerpt",
|
||||||
|
DiffStyle: ctx.FormString("style"),
|
||||||
|
AfterCommitID: headCommitID,
|
||||||
|
}
|
||||||
ctx.Data["DiffNotAvailable"] = diffShortStat.NumFiles == 0
|
ctx.Data["DiffNotAvailable"] = diffShortStat.NumFiles == 0
|
||||||
|
|
||||||
if !fileOnly {
|
if !fileOnly {
|
||||||
@ -865,6 +872,28 @@ func CompareDiff(ctx *context.Context) {
|
|||||||
ctx.HTML(http.StatusOK, tplCompare)
|
ctx.HTML(http.StatusOK, tplCompare)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// attachCommentsToLines attaches comments to their corresponding diff lines
|
||||||
|
func attachCommentsToLines(section *gitdiff.DiffSection, lineComments map[int64][]*issues_model.Comment) {
|
||||||
|
for _, line := range section.Lines {
|
||||||
|
if comments, ok := lineComments[int64(line.LeftIdx*-1)]; ok {
|
||||||
|
line.Comments = append(line.Comments, comments...)
|
||||||
|
}
|
||||||
|
if comments, ok := lineComments[int64(line.RightIdx)]; ok {
|
||||||
|
line.Comments = append(line.Comments, comments...)
|
||||||
|
}
|
||||||
|
sort.SliceStable(line.Comments, func(i, j int) bool {
|
||||||
|
return line.Comments[i].CreatedUnix < line.Comments[j].CreatedUnix
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// attachHiddenCommentIDs calculates and attaches hidden comment IDs to expand buttons
|
||||||
|
func attachHiddenCommentIDs(section *gitdiff.DiffSection, lineComments map[int64][]*issues_model.Comment) {
|
||||||
|
for _, line := range section.Lines {
|
||||||
|
gitdiff.FillHiddenCommentIDsForDiffLine(line, lineComments)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ExcerptBlob render blob excerpt contents
|
// ExcerptBlob render blob excerpt contents
|
||||||
func ExcerptBlob(ctx *context.Context) {
|
func ExcerptBlob(ctx *context.Context) {
|
||||||
commitID := ctx.PathParam("sha")
|
commitID := ctx.PathParam("sha")
|
||||||
@ -874,19 +903,26 @@ func ExcerptBlob(ctx *context.Context) {
|
|||||||
idxRight := ctx.FormInt("right")
|
idxRight := ctx.FormInt("right")
|
||||||
leftHunkSize := ctx.FormInt("left_hunk_size")
|
leftHunkSize := ctx.FormInt("left_hunk_size")
|
||||||
rightHunkSize := ctx.FormInt("right_hunk_size")
|
rightHunkSize := ctx.FormInt("right_hunk_size")
|
||||||
anchor := ctx.FormString("anchor")
|
|
||||||
direction := ctx.FormString("direction")
|
direction := ctx.FormString("direction")
|
||||||
filePath := ctx.FormString("path")
|
filePath := ctx.FormString("path")
|
||||||
gitRepo := ctx.Repo.GitRepo
|
gitRepo := ctx.Repo.GitRepo
|
||||||
|
|
||||||
|
diffBlobExcerptData := &gitdiff.DiffBlobExcerptData{
|
||||||
|
BaseLink: ctx.Repo.RepoLink + "/blob_excerpt",
|
||||||
|
DiffStyle: ctx.FormString("style"),
|
||||||
|
AfterCommitID: commitID,
|
||||||
|
}
|
||||||
|
|
||||||
if ctx.Data["PageIsWiki"] == true {
|
if ctx.Data["PageIsWiki"] == true {
|
||||||
var err error
|
var err error
|
||||||
gitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository.WikiStorageRepo())
|
gitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository.WikiStorageRepo())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.ServerError("OpenRepository", err)
|
ctx.ServerError("OpenRepository", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer gitRepo.Close()
|
diffBlobExcerptData.BaseLink = ctx.Repo.RepoLink + "/wiki/blob_excerpt"
|
||||||
}
|
}
|
||||||
|
|
||||||
chunkSize := gitdiff.BlobExcerptChunkSize
|
chunkSize := gitdiff.BlobExcerptChunkSize
|
||||||
commit, err := gitRepo.GetCommit(commitID)
|
commit, err := gitRepo.GetCommit(commitID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -947,10 +983,43 @@ func ExcerptBlob(ctx *context.Context) {
|
|||||||
section.Lines = append(section.Lines, lineSection)
|
section.Lines = append(section.Lines, lineSection)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
diffBlobExcerptData.PullIssueIndex = ctx.FormInt64("pull_issue_index")
|
||||||
|
if diffBlobExcerptData.PullIssueIndex > 0 {
|
||||||
|
if !ctx.Repo.CanRead(unit.TypePullRequests) {
|
||||||
|
ctx.NotFound(nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, diffBlobExcerptData.PullIssueIndex)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("GetIssueByIndex error: %v", err)
|
||||||
|
} else if issue.IsPull {
|
||||||
|
// FIXME: DIFF-CONVERSATION-DATA: the following data assignment is fragile
|
||||||
|
ctx.Data["Issue"] = issue
|
||||||
|
ctx.Data["CanBlockUser"] = func(blocker, blockee *user_model.User) bool {
|
||||||
|
return user_service.CanBlockUser(ctx, ctx.Doer, blocker, blockee)
|
||||||
|
}
|
||||||
|
// and "diff/comment_form.tmpl" (reply comment) needs them
|
||||||
|
ctx.Data["PageIsPullFiles"] = true
|
||||||
|
ctx.Data["AfterCommitID"] = diffBlobExcerptData.AfterCommitID
|
||||||
|
|
||||||
|
allComments, err := issues_model.FetchCodeComments(ctx, issue, ctx.Doer, ctx.FormBool("show_outdated"))
|
||||||
|
if err != nil {
|
||||||
|
log.Error("FetchCodeComments error: %v", err)
|
||||||
|
} else {
|
||||||
|
if lineComments, ok := allComments[filePath]; ok {
|
||||||
|
attachCommentsToLines(section, lineComments)
|
||||||
|
attachHiddenCommentIDs(section, lineComments)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ctx.Data["section"] = section
|
ctx.Data["section"] = section
|
||||||
ctx.Data["FileNameHash"] = git.HashFilePathForWebUI(filePath)
|
ctx.Data["FileNameHash"] = git.HashFilePathForWebUI(filePath)
|
||||||
ctx.Data["AfterCommitID"] = commitID
|
ctx.Data["DiffBlobExcerptData"] = diffBlobExcerptData
|
||||||
ctx.Data["Anchor"] = anchor
|
|
||||||
ctx.HTML(http.StatusOK, tplBlobExcerpt)
|
ctx.HTML(http.StatusOK, tplBlobExcerpt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
40
routers/web/repo/compare_test.go
Normal file
40
routers/web/repo/compare_test.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
issues_model "code.gitea.io/gitea/models/issues"
|
||||||
|
"code.gitea.io/gitea/services/gitdiff"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAttachCommentsToLines(t *testing.T) {
|
||||||
|
section := &gitdiff.DiffSection{
|
||||||
|
Lines: []*gitdiff.DiffLine{
|
||||||
|
{LeftIdx: 5, RightIdx: 10},
|
||||||
|
{LeftIdx: 6, RightIdx: 11},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
lineComments := map[int64][]*issues_model.Comment{
|
||||||
|
-5: {{ID: 100, CreatedUnix: 1000}}, // left side comment
|
||||||
|
10: {{ID: 200, CreatedUnix: 2000}}, // right side comment
|
||||||
|
11: {{ID: 300, CreatedUnix: 1500}, {ID: 301, CreatedUnix: 2500}}, // multiple comments
|
||||||
|
}
|
||||||
|
|
||||||
|
attachCommentsToLines(section, lineComments)
|
||||||
|
|
||||||
|
// First line should have left and right comments
|
||||||
|
assert.Len(t, section.Lines[0].Comments, 2)
|
||||||
|
assert.Equal(t, int64(100), section.Lines[0].Comments[0].ID)
|
||||||
|
assert.Equal(t, int64(200), section.Lines[0].Comments[1].ID)
|
||||||
|
|
||||||
|
// Second line should have two comments, sorted by creation time
|
||||||
|
assert.Len(t, section.Lines[1].Comments, 2)
|
||||||
|
assert.Equal(t, int64(300), section.Lines[1].Comments[0].ID)
|
||||||
|
assert.Equal(t, int64(301), section.Lines[1].Comments[1].ID)
|
||||||
|
}
|
||||||
@ -827,6 +827,12 @@ func viewPullFiles(ctx *context.Context, beforeCommitID, afterCommitID string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx.Data["Diff"] = diff
|
ctx.Data["Diff"] = diff
|
||||||
|
ctx.Data["DiffBlobExcerptData"] = &gitdiff.DiffBlobExcerptData{
|
||||||
|
BaseLink: ctx.Repo.RepoLink + "/blob_excerpt",
|
||||||
|
PullIssueIndex: pull.Index,
|
||||||
|
DiffStyle: ctx.FormString("style"),
|
||||||
|
AfterCommitID: afterCommitID,
|
||||||
|
}
|
||||||
ctx.Data["DiffNotAvailable"] = diffShortStat.NumFiles == 0
|
ctx.Data["DiffNotAvailable"] = diffShortStat.NumFiles == 0
|
||||||
|
|
||||||
if ctx.IsSigned && ctx.Doer != nil {
|
if ctx.IsSigned && ctx.Doer != nil {
|
||||||
|
|||||||
@ -22,19 +22,21 @@ import (
|
|||||||
git_model "code.gitea.io/gitea/models/git"
|
git_model "code.gitea.io/gitea/models/git"
|
||||||
issues_model "code.gitea.io/gitea/models/issues"
|
issues_model "code.gitea.io/gitea/models/issues"
|
||||||
pull_model "code.gitea.io/gitea/models/pull"
|
pull_model "code.gitea.io/gitea/models/pull"
|
||||||
repo_model "code.gitea.io/gitea/models/repo"
|
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
"code.gitea.io/gitea/modules/analyze"
|
"code.gitea.io/gitea/modules/analyze"
|
||||||
|
"code.gitea.io/gitea/modules/base"
|
||||||
"code.gitea.io/gitea/modules/charset"
|
"code.gitea.io/gitea/modules/charset"
|
||||||
"code.gitea.io/gitea/modules/git"
|
"code.gitea.io/gitea/modules/git"
|
||||||
"code.gitea.io/gitea/modules/git/attribute"
|
"code.gitea.io/gitea/modules/git/attribute"
|
||||||
"code.gitea.io/gitea/modules/git/gitcmd"
|
"code.gitea.io/gitea/modules/git/gitcmd"
|
||||||
"code.gitea.io/gitea/modules/gitrepo"
|
"code.gitea.io/gitea/modules/gitrepo"
|
||||||
"code.gitea.io/gitea/modules/highlight"
|
"code.gitea.io/gitea/modules/highlight"
|
||||||
|
"code.gitea.io/gitea/modules/htmlutil"
|
||||||
"code.gitea.io/gitea/modules/lfs"
|
"code.gitea.io/gitea/modules/lfs"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/optional"
|
"code.gitea.io/gitea/modules/optional"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
|
"code.gitea.io/gitea/modules/svg"
|
||||||
"code.gitea.io/gitea/modules/translation"
|
"code.gitea.io/gitea/modules/translation"
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
|
|
||||||
@ -67,18 +69,6 @@ const (
|
|||||||
DiffFileCopy
|
DiffFileCopy
|
||||||
)
|
)
|
||||||
|
|
||||||
// DiffLineExpandDirection represents the DiffLineSection expand direction
|
|
||||||
type DiffLineExpandDirection uint8
|
|
||||||
|
|
||||||
// DiffLineExpandDirection possible values.
|
|
||||||
const (
|
|
||||||
DiffLineExpandNone DiffLineExpandDirection = iota + 1
|
|
||||||
DiffLineExpandSingle
|
|
||||||
DiffLineExpandUpDown
|
|
||||||
DiffLineExpandUp
|
|
||||||
DiffLineExpandDown
|
|
||||||
)
|
|
||||||
|
|
||||||
// DiffLine represents a line difference in a DiffSection.
|
// DiffLine represents a line difference in a DiffSection.
|
||||||
type DiffLine struct {
|
type DiffLine struct {
|
||||||
LeftIdx int // line number, 1-based
|
LeftIdx int // line number, 1-based
|
||||||
@ -99,6 +89,8 @@ type DiffLineSectionInfo struct {
|
|||||||
RightIdx int
|
RightIdx int
|
||||||
LeftHunkSize int
|
LeftHunkSize int
|
||||||
RightHunkSize int
|
RightHunkSize int
|
||||||
|
|
||||||
|
HiddenCommentIDs []int64 // IDs of hidden comments in this section
|
||||||
}
|
}
|
||||||
|
|
||||||
// DiffHTMLOperation is the HTML version of diffmatchpatch.Diff
|
// DiffHTMLOperation is the HTML version of diffmatchpatch.Diff
|
||||||
@ -153,8 +145,7 @@ func (d *DiffLine) GetLineTypeMarker() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBlobExcerptQuery builds query string to get blob excerpt
|
func (d *DiffLine) getBlobExcerptQuery() string {
|
||||||
func (d *DiffLine) GetBlobExcerptQuery() string {
|
|
||||||
query := fmt.Sprintf(
|
query := fmt.Sprintf(
|
||||||
"last_left=%d&last_right=%d&"+
|
"last_left=%d&last_right=%d&"+
|
||||||
"left=%d&right=%d&"+
|
"left=%d&right=%d&"+
|
||||||
@ -167,19 +158,88 @@ func (d *DiffLine) GetBlobExcerptQuery() string {
|
|||||||
return query
|
return query
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetExpandDirection gets DiffLineExpandDirection
|
func (d *DiffLine) getExpandDirection() string {
|
||||||
func (d *DiffLine) GetExpandDirection() DiffLineExpandDirection {
|
|
||||||
if d.Type != DiffLineSection || d.SectionInfo == nil || d.SectionInfo.LeftIdx-d.SectionInfo.LastLeftIdx <= 1 || d.SectionInfo.RightIdx-d.SectionInfo.LastRightIdx <= 1 {
|
if d.Type != DiffLineSection || d.SectionInfo == nil || d.SectionInfo.LeftIdx-d.SectionInfo.LastLeftIdx <= 1 || d.SectionInfo.RightIdx-d.SectionInfo.LastRightIdx <= 1 {
|
||||||
return DiffLineExpandNone
|
return ""
|
||||||
}
|
}
|
||||||
if d.SectionInfo.LastLeftIdx <= 0 && d.SectionInfo.LastRightIdx <= 0 {
|
if d.SectionInfo.LastLeftIdx <= 0 && d.SectionInfo.LastRightIdx <= 0 {
|
||||||
return DiffLineExpandUp
|
return "up"
|
||||||
} else if d.SectionInfo.RightIdx-d.SectionInfo.LastRightIdx > BlobExcerptChunkSize && d.SectionInfo.RightHunkSize > 0 {
|
} else if d.SectionInfo.RightIdx-d.SectionInfo.LastRightIdx > BlobExcerptChunkSize && d.SectionInfo.RightHunkSize > 0 {
|
||||||
return DiffLineExpandUpDown
|
return "updown"
|
||||||
} else if d.SectionInfo.LeftHunkSize <= 0 && d.SectionInfo.RightHunkSize <= 0 {
|
} else if d.SectionInfo.LeftHunkSize <= 0 && d.SectionInfo.RightHunkSize <= 0 {
|
||||||
return DiffLineExpandDown
|
return "down"
|
||||||
}
|
}
|
||||||
return DiffLineExpandSingle
|
return "single"
|
||||||
|
}
|
||||||
|
|
||||||
|
type DiffBlobExcerptData struct {
|
||||||
|
BaseLink string
|
||||||
|
IsWikiRepo bool
|
||||||
|
PullIssueIndex int64
|
||||||
|
DiffStyle string
|
||||||
|
AfterCommitID string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DiffLine) RenderBlobExcerptButtons(fileNameHash string, data *DiffBlobExcerptData) template.HTML {
|
||||||
|
dataHiddenCommentIDs := strings.Join(base.Int64sToStrings(d.SectionInfo.HiddenCommentIDs), ",")
|
||||||
|
anchor := fmt.Sprintf("diff-%sK%d", fileNameHash, d.SectionInfo.RightIdx)
|
||||||
|
|
||||||
|
makeButton := func(direction, svgName string) template.HTML {
|
||||||
|
style := util.IfZero(data.DiffStyle, "unified")
|
||||||
|
link := data.BaseLink + "/" + data.AfterCommitID + fmt.Sprintf("?style=%s&direction=%s&anchor=%s", url.QueryEscape(style), direction, url.QueryEscape(anchor)) + "&" + d.getBlobExcerptQuery()
|
||||||
|
if data.PullIssueIndex > 0 {
|
||||||
|
link += fmt.Sprintf("&pull_issue_index=%d", data.PullIssueIndex)
|
||||||
|
}
|
||||||
|
return htmlutil.HTMLFormat(
|
||||||
|
`<button class="code-expander-button" hx-target="closest tr" hx-get="%s" data-hidden-comment-ids=",%s,">%s</button>`,
|
||||||
|
link, dataHiddenCommentIDs, svg.RenderHTML(svgName),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
var content template.HTML
|
||||||
|
|
||||||
|
if len(d.SectionInfo.HiddenCommentIDs) > 0 {
|
||||||
|
tooltip := fmt.Sprintf("%d hidden comment(s)", len(d.SectionInfo.HiddenCommentIDs))
|
||||||
|
content += htmlutil.HTMLFormat(`<span class="code-comment-more" data-tooltip-content="%s">%d</span>`, tooltip, len(d.SectionInfo.HiddenCommentIDs))
|
||||||
|
}
|
||||||
|
|
||||||
|
expandDirection := d.getExpandDirection()
|
||||||
|
if expandDirection == "up" || expandDirection == "updown" {
|
||||||
|
content += makeButton("up", "octicon-fold-up")
|
||||||
|
}
|
||||||
|
if expandDirection == "updown" || expandDirection == "down" {
|
||||||
|
content += makeButton("down", "octicon-fold-down")
|
||||||
|
}
|
||||||
|
if expandDirection == "single" {
|
||||||
|
content += makeButton("single", "octicon-fold")
|
||||||
|
}
|
||||||
|
return htmlutil.HTMLFormat(`<div class="code-expander-buttons" data-expand-direction="%s">%s</div>`, expandDirection, content)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FillHiddenCommentIDsForDiffLine finds comment IDs that are in the hidden range of an expand button
|
||||||
|
func FillHiddenCommentIDsForDiffLine(line *DiffLine, lineComments map[int64][]*issues_model.Comment) {
|
||||||
|
if line.Type != DiffLineSection {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var hiddenCommentIDs []int64
|
||||||
|
for commentLineNum, comments := range lineComments {
|
||||||
|
if commentLineNum < 0 {
|
||||||
|
// ATTENTION: BLOB-EXCERPT-COMMENT-RIGHT: skip left-side, unchanged lines always use "right (proposed)" side for comments
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
lineNum := int(commentLineNum)
|
||||||
|
isEndOfFileExpansion := line.SectionInfo.RightHunkSize == 0
|
||||||
|
inRange := lineNum > line.SectionInfo.LastRightIdx &&
|
||||||
|
(isEndOfFileExpansion && lineNum <= line.SectionInfo.RightIdx ||
|
||||||
|
!isEndOfFileExpansion && lineNum < line.SectionInfo.RightIdx)
|
||||||
|
|
||||||
|
if inRange {
|
||||||
|
for _, comment := range comments {
|
||||||
|
hiddenCommentIDs = append(hiddenCommentIDs, comment.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
line.SectionInfo.HiddenCommentIDs = hiddenCommentIDs
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDiffLineSectionInfo(treePath, line string, lastLeftIdx, lastRightIdx int) *DiffLineSectionInfo {
|
func getDiffLineSectionInfo(treePath, line string, lastLeftIdx, lastRightIdx int) *DiffLineSectionInfo {
|
||||||
@ -485,6 +545,8 @@ func (diff *Diff) LoadComments(ctx context.Context, issue *issues_model.Issue, c
|
|||||||
sort.SliceStable(line.Comments, func(i, j int) bool {
|
sort.SliceStable(line.Comments, func(i, j int) bool {
|
||||||
return line.Comments[i].CreatedUnix < line.Comments[j].CreatedUnix
|
return line.Comments[i].CreatedUnix < line.Comments[j].CreatedUnix
|
||||||
})
|
})
|
||||||
|
// Mark expand buttons that have comments in hidden lines
|
||||||
|
FillHiddenCommentIDsForDiffLine(line, lineCommits)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1281,7 +1343,7 @@ type DiffShortStat struct {
|
|||||||
NumFiles, TotalAddition, TotalDeletion int
|
NumFiles, TotalAddition, TotalDeletion int
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDiffShortStat(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, beforeCommitID, afterCommitID string) (*DiffShortStat, error) {
|
func GetDiffShortStat(ctx context.Context, repoStorage gitrepo.Repository, gitRepo *git.Repository, beforeCommitID, afterCommitID string) (*DiffShortStat, error) {
|
||||||
afterCommit, err := gitRepo.GetCommit(afterCommitID)
|
afterCommit, err := gitRepo.GetCommit(afterCommitID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -1293,7 +1355,7 @@ func GetDiffShortStat(ctx context.Context, repo *repo_model.Repository, gitRepo
|
|||||||
}
|
}
|
||||||
|
|
||||||
diff := &DiffShortStat{}
|
diff := &DiffShortStat{}
|
||||||
diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = gitrepo.GetDiffShortStatByCmdArgs(ctx, repo, nil, actualBeforeCommitID.String(), afterCommitID)
|
diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = gitrepo.GetDiffShortStatByCmdArgs(ctx, repoStorage, nil, actualBeforeCommitID.String(), afterCommitID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -1386,6 +1448,75 @@ func CommentAsDiff(ctx context.Context, c *issues_model.Comment) (*Diff, error)
|
|||||||
return diff, nil
|
return diff, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GeneratePatchForUnchangedLine creates a patch showing code context for an unchanged line
|
||||||
|
func GeneratePatchForUnchangedLine(gitRepo *git.Repository, commitID, treePath string, line int64, contextLines int) (string, error) {
|
||||||
|
commit, err := gitRepo.GetCommit(commitID)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("GetCommit: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
entry, err := commit.GetTreeEntryByPath(treePath)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("GetTreeEntryByPath: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
blob := entry.Blob()
|
||||||
|
dataRc, err := blob.DataAsync()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("DataAsync: %w", err)
|
||||||
|
}
|
||||||
|
defer dataRc.Close()
|
||||||
|
|
||||||
|
return generatePatchForUnchangedLineFromReader(dataRc, treePath, line, contextLines)
|
||||||
|
}
|
||||||
|
|
||||||
|
// generatePatchForUnchangedLineFromReader is the testable core logic that generates a patch from a reader
|
||||||
|
func generatePatchForUnchangedLineFromReader(reader io.Reader, treePath string, line int64, contextLines int) (string, error) {
|
||||||
|
// Calculate line range (commented line + lines above it)
|
||||||
|
commentLine := int(line)
|
||||||
|
if line < 0 {
|
||||||
|
commentLine = int(-line)
|
||||||
|
}
|
||||||
|
startLine := max(commentLine-contextLines, 1)
|
||||||
|
endLine := commentLine
|
||||||
|
|
||||||
|
// Read only the needed lines efficiently
|
||||||
|
scanner := bufio.NewScanner(reader)
|
||||||
|
currentLine := 0
|
||||||
|
var lines []string
|
||||||
|
for scanner.Scan() {
|
||||||
|
currentLine++
|
||||||
|
if currentLine >= startLine && currentLine <= endLine {
|
||||||
|
lines = append(lines, scanner.Text())
|
||||||
|
}
|
||||||
|
if currentLine > endLine {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return "", fmt.Errorf("scanner error: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(lines) == 0 {
|
||||||
|
return "", fmt.Errorf("no lines found in range %d-%d", startLine, endLine)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate synthetic patch
|
||||||
|
var patchBuilder strings.Builder
|
||||||
|
patchBuilder.WriteString(fmt.Sprintf("diff --git a/%s b/%s\n", treePath, treePath))
|
||||||
|
patchBuilder.WriteString(fmt.Sprintf("--- a/%s\n", treePath))
|
||||||
|
patchBuilder.WriteString(fmt.Sprintf("+++ b/%s\n", treePath))
|
||||||
|
patchBuilder.WriteString(fmt.Sprintf("@@ -%d,%d +%d,%d @@\n", startLine, len(lines), startLine, len(lines)))
|
||||||
|
|
||||||
|
for _, lineContent := range lines {
|
||||||
|
patchBuilder.WriteString(" ")
|
||||||
|
patchBuilder.WriteString(lineContent)
|
||||||
|
patchBuilder.WriteString("\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
return patchBuilder.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
// CommentMustAsDiff executes AsDiff and logs the error instead of returning
|
// CommentMustAsDiff executes AsDiff and logs the error instead of returning
|
||||||
func CommentMustAsDiff(ctx context.Context, c *issues_model.Comment) *Diff {
|
func CommentMustAsDiff(ctx context.Context, c *issues_model.Comment) *Diff {
|
||||||
if c == nil {
|
if c == nil {
|
||||||
|
|||||||
@ -640,3 +640,346 @@ func TestNoCrashes(t *testing.T) {
|
|||||||
ParsePatch(t.Context(), setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(testcase.gitdiff), "")
|
ParsePatch(t.Context(), setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(testcase.gitdiff), "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGeneratePatchForUnchangedLineFromReader(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
content string
|
||||||
|
treePath string
|
||||||
|
line int64
|
||||||
|
contextLines int
|
||||||
|
want string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "single line with context",
|
||||||
|
content: "line1\nline2\nline3\nline4\nline5\n",
|
||||||
|
treePath: "test.txt",
|
||||||
|
line: 3,
|
||||||
|
contextLines: 1,
|
||||||
|
want: `diff --git a/test.txt b/test.txt
|
||||||
|
--- a/test.txt
|
||||||
|
+++ b/test.txt
|
||||||
|
@@ -2,2 +2,2 @@
|
||||||
|
line2
|
||||||
|
line3
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "negative line number (left side)",
|
||||||
|
content: "line1\nline2\nline3\nline4\nline5\n",
|
||||||
|
treePath: "test.txt",
|
||||||
|
line: -3,
|
||||||
|
contextLines: 1,
|
||||||
|
want: `diff --git a/test.txt b/test.txt
|
||||||
|
--- a/test.txt
|
||||||
|
+++ b/test.txt
|
||||||
|
@@ -2,2 +2,2 @@
|
||||||
|
line2
|
||||||
|
line3
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "line near start of file",
|
||||||
|
content: "line1\nline2\nline3\n",
|
||||||
|
treePath: "test.txt",
|
||||||
|
line: 2,
|
||||||
|
contextLines: 5,
|
||||||
|
want: `diff --git a/test.txt b/test.txt
|
||||||
|
--- a/test.txt
|
||||||
|
+++ b/test.txt
|
||||||
|
@@ -1,2 +1,2 @@
|
||||||
|
line1
|
||||||
|
line2
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "first line with context",
|
||||||
|
content: "line1\nline2\nline3\n",
|
||||||
|
treePath: "test.txt",
|
||||||
|
line: 1,
|
||||||
|
contextLines: 3,
|
||||||
|
want: `diff --git a/test.txt b/test.txt
|
||||||
|
--- a/test.txt
|
||||||
|
+++ b/test.txt
|
||||||
|
@@ -1,1 +1,1 @@
|
||||||
|
line1
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "zero context lines",
|
||||||
|
content: "line1\nline2\nline3\n",
|
||||||
|
treePath: "test.txt",
|
||||||
|
line: 2,
|
||||||
|
contextLines: 0,
|
||||||
|
want: `diff --git a/test.txt b/test.txt
|
||||||
|
--- a/test.txt
|
||||||
|
+++ b/test.txt
|
||||||
|
@@ -2,1 +2,1 @@
|
||||||
|
line2
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multi-line context",
|
||||||
|
content: "package main\n\nfunc main() {\n fmt.Println(\"Hello\")\n}\n",
|
||||||
|
treePath: "main.go",
|
||||||
|
line: 4,
|
||||||
|
contextLines: 2,
|
||||||
|
want: `diff --git a/main.go b/main.go
|
||||||
|
--- a/main.go
|
||||||
|
+++ b/main.go
|
||||||
|
@@ -2,3 +2,3 @@
|
||||||
|
<SP>
|
||||||
|
func main() {
|
||||||
|
fmt.Println("Hello")
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty file",
|
||||||
|
content: "",
|
||||||
|
treePath: "empty.txt",
|
||||||
|
line: 1,
|
||||||
|
contextLines: 1,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
reader := strings.NewReader(tt.content)
|
||||||
|
got, err := generatePatchForUnchangedLineFromReader(reader, tt.treePath, tt.line, tt.contextLines)
|
||||||
|
if tt.wantErr {
|
||||||
|
assert.Error(t, err)
|
||||||
|
} else {
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, strings.ReplaceAll(tt.want, "<SP>", " "), got)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCalculateHiddenCommentIDsForLine(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
line *DiffLine
|
||||||
|
lineComments map[int64][]*issues_model.Comment
|
||||||
|
expected []int64
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "comments in hidden range",
|
||||||
|
line: &DiffLine{
|
||||||
|
Type: DiffLineSection,
|
||||||
|
SectionInfo: &DiffLineSectionInfo{
|
||||||
|
LastRightIdx: 10,
|
||||||
|
RightIdx: 20,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
lineComments: map[int64][]*issues_model.Comment{
|
||||||
|
15: {{ID: 100}, {ID: 101}},
|
||||||
|
12: {{ID: 102}},
|
||||||
|
},
|
||||||
|
expected: []int64{100, 101, 102},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "comments outside hidden range",
|
||||||
|
line: &DiffLine{
|
||||||
|
Type: DiffLineSection,
|
||||||
|
SectionInfo: &DiffLineSectionInfo{
|
||||||
|
LastRightIdx: 10,
|
||||||
|
RightIdx: 20,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
lineComments: map[int64][]*issues_model.Comment{
|
||||||
|
5: {{ID: 100}},
|
||||||
|
25: {{ID: 101}},
|
||||||
|
},
|
||||||
|
expected: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "negative line numbers (left side)",
|
||||||
|
line: &DiffLine{
|
||||||
|
Type: DiffLineSection,
|
||||||
|
SectionInfo: &DiffLineSectionInfo{
|
||||||
|
LastRightIdx: 10,
|
||||||
|
RightIdx: 20,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
lineComments: map[int64][]*issues_model.Comment{
|
||||||
|
-15: {{ID: 100}}, // Left-side comment, should NOT be counted
|
||||||
|
15: {{ID: 101}}, // Right-side comment, should be counted
|
||||||
|
},
|
||||||
|
expected: []int64{101}, // Only right-side comment
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "boundary conditions - normal expansion (both boundaries exclusive)",
|
||||||
|
line: &DiffLine{
|
||||||
|
Type: DiffLineSection,
|
||||||
|
SectionInfo: &DiffLineSectionInfo{
|
||||||
|
LastRightIdx: 10,
|
||||||
|
RightIdx: 20,
|
||||||
|
RightHunkSize: 5, // Normal case: next section has content
|
||||||
|
},
|
||||||
|
},
|
||||||
|
lineComments: map[int64][]*issues_model.Comment{
|
||||||
|
10: {{ID: 100}}, // at LastRightIdx (visible line), should NOT be included
|
||||||
|
20: {{ID: 101}}, // at RightIdx (visible line), should NOT be included
|
||||||
|
11: {{ID: 102}}, // just inside range, should be included
|
||||||
|
19: {{ID: 103}}, // just inside range, should be included
|
||||||
|
},
|
||||||
|
expected: []int64{102, 103},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "boundary conditions - end of file expansion (RightIdx inclusive)",
|
||||||
|
line: &DiffLine{
|
||||||
|
Type: DiffLineSection,
|
||||||
|
SectionInfo: &DiffLineSectionInfo{
|
||||||
|
LastRightIdx: 54,
|
||||||
|
RightIdx: 70,
|
||||||
|
RightHunkSize: 0, // End of file: no more content after
|
||||||
|
},
|
||||||
|
},
|
||||||
|
lineComments: map[int64][]*issues_model.Comment{
|
||||||
|
54: {{ID: 54}}, // at LastRightIdx (visible line), should NOT be included
|
||||||
|
70: {{ID: 70}}, // at RightIdx (last hidden line), SHOULD be included
|
||||||
|
60: {{ID: 60}}, // inside range, should be included
|
||||||
|
},
|
||||||
|
expected: []int64{60, 70}, // Lines 60 and 70 are hidden
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "real-world scenario - start of file with hunk",
|
||||||
|
line: &DiffLine{
|
||||||
|
Type: DiffLineSection,
|
||||||
|
SectionInfo: &DiffLineSectionInfo{
|
||||||
|
LastRightIdx: 0, // No previous visible section
|
||||||
|
RightIdx: 26, // Line 26 is first visible line of hunk
|
||||||
|
RightHunkSize: 9, // Normal hunk with content
|
||||||
|
},
|
||||||
|
},
|
||||||
|
lineComments: map[int64][]*issues_model.Comment{
|
||||||
|
1: {{ID: 1}}, // Line 1 is hidden
|
||||||
|
26: {{ID: 26}}, // Line 26 is visible (hunk start) - should NOT be hidden
|
||||||
|
10: {{ID: 10}}, // Line 10 is hidden
|
||||||
|
15: {{ID: 15}}, // Line 15 is hidden
|
||||||
|
},
|
||||||
|
expected: []int64{1, 10, 15}, // Lines 1, 10, 15 are hidden; line 26 is visible
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
FillHiddenCommentIDsForDiffLine(tt.line, tt.lineComments)
|
||||||
|
assert.ElementsMatch(t, tt.expected, tt.line.SectionInfo.HiddenCommentIDs)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDiffLine_RenderBlobExcerptButtons(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
line *DiffLine
|
||||||
|
fileNameHash string
|
||||||
|
data *DiffBlobExcerptData
|
||||||
|
expectContains []string
|
||||||
|
expectNotContain []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "expand up button with hidden comments",
|
||||||
|
line: &DiffLine{
|
||||||
|
Type: DiffLineSection,
|
||||||
|
SectionInfo: &DiffLineSectionInfo{
|
||||||
|
LastRightIdx: 0,
|
||||||
|
RightIdx: 26,
|
||||||
|
LeftIdx: 26,
|
||||||
|
LastLeftIdx: 0,
|
||||||
|
LeftHunkSize: 0,
|
||||||
|
RightHunkSize: 0,
|
||||||
|
HiddenCommentIDs: []int64{100},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
fileNameHash: "abc123",
|
||||||
|
data: &DiffBlobExcerptData{
|
||||||
|
BaseLink: "/repo/blob_excerpt",
|
||||||
|
AfterCommitID: "commit123",
|
||||||
|
DiffStyle: "unified",
|
||||||
|
},
|
||||||
|
expectContains: []string{
|
||||||
|
"octicon-fold-up",
|
||||||
|
"direction=up",
|
||||||
|
"code-comment-more",
|
||||||
|
"1 hidden comment(s)",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "expand up and down buttons with pull request",
|
||||||
|
line: &DiffLine{
|
||||||
|
Type: DiffLineSection,
|
||||||
|
SectionInfo: &DiffLineSectionInfo{
|
||||||
|
LastRightIdx: 10,
|
||||||
|
RightIdx: 50,
|
||||||
|
LeftIdx: 10,
|
||||||
|
LastLeftIdx: 5,
|
||||||
|
LeftHunkSize: 5,
|
||||||
|
RightHunkSize: 5,
|
||||||
|
HiddenCommentIDs: []int64{200, 201},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
fileNameHash: "def456",
|
||||||
|
data: &DiffBlobExcerptData{
|
||||||
|
BaseLink: "/repo/blob_excerpt",
|
||||||
|
AfterCommitID: "commit456",
|
||||||
|
DiffStyle: "split",
|
||||||
|
PullIssueIndex: 42,
|
||||||
|
},
|
||||||
|
expectContains: []string{
|
||||||
|
"octicon-fold-down",
|
||||||
|
"octicon-fold-up",
|
||||||
|
"direction=down",
|
||||||
|
"direction=up",
|
||||||
|
`data-hidden-comment-ids=",200,201,"`, // use leading and trailing commas to ensure exact match by CSS selector `attr*=",id,"`
|
||||||
|
"pull_issue_index=42",
|
||||||
|
"2 hidden comment(s)",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "no hidden comments",
|
||||||
|
line: &DiffLine{
|
||||||
|
Type: DiffLineSection,
|
||||||
|
SectionInfo: &DiffLineSectionInfo{
|
||||||
|
LastRightIdx: 10,
|
||||||
|
RightIdx: 20,
|
||||||
|
LeftIdx: 10,
|
||||||
|
LastLeftIdx: 5,
|
||||||
|
LeftHunkSize: 5,
|
||||||
|
RightHunkSize: 5,
|
||||||
|
HiddenCommentIDs: nil,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
fileNameHash: "ghi789",
|
||||||
|
data: &DiffBlobExcerptData{
|
||||||
|
BaseLink: "/repo/blob_excerpt",
|
||||||
|
AfterCommitID: "commit789",
|
||||||
|
},
|
||||||
|
expectContains: []string{
|
||||||
|
"code-expander-button",
|
||||||
|
},
|
||||||
|
expectNotContain: []string{
|
||||||
|
"code-comment-more",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result := tt.line.RenderBlobExcerptButtons(tt.fileNameHash, tt.data)
|
||||||
|
resultStr := string(result)
|
||||||
|
|
||||||
|
for _, expected := range tt.expectContains {
|
||||||
|
assert.Contains(t, resultStr, expected, "Expected to contain: %s", expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, notExpected := range tt.expectNotContain {
|
||||||
|
assert.NotContains(t, resultStr, notExpected, "Expected NOT to contain: %s", notExpected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -22,6 +22,7 @@ import (
|
|||||||
"code.gitea.io/gitea/modules/optional"
|
"code.gitea.io/gitea/modules/optional"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
|
"code.gitea.io/gitea/services/gitdiff"
|
||||||
notify_service "code.gitea.io/gitea/services/notify"
|
notify_service "code.gitea.io/gitea/services/notify"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -283,6 +284,15 @@ func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_mo
|
|||||||
log.Error("Error whilst generating patch: %v", err)
|
log.Error("Error whilst generating patch: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If patch is still empty (unchanged line), generate code context
|
||||||
|
if patch == "" && commitID != "" {
|
||||||
|
patch, err = gitdiff.GeneratePatchForUnchangedLine(gitRepo, commitID, treePath, line, setting.UI.CodeCommentLines)
|
||||||
|
if err != nil {
|
||||||
|
// Log the error but don't fail comment creation
|
||||||
|
log.Debug("Unable to generate patch for unchanged line (file=%s, line=%d, commit=%s): %v", treePath, line, commitID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return issues_model.CreateComment(ctx, &issues_model.CreateCommentOptions{
|
return issues_model.CreateComment(ctx, &issues_model.CreateCommentOptions{
|
||||||
Type: issues_model.CommentTypeCode,
|
Type: issues_model.CommentTypeCode,
|
||||||
|
|||||||
@ -1,28 +1,10 @@
|
|||||||
{{$blobExcerptLink := print $.RepoLink (Iif $.PageIsWiki "/wiki" "") "/blob_excerpt/" (PathEscape $.AfterCommitID) (QueryBuild "?" "anchor" $.Anchor)}}
|
{{$diffBlobExcerptData := .DiffBlobExcerptData}}
|
||||||
|
{{$canCreateComment := and ctx.RootData.SignedUserID $diffBlobExcerptData.PullIssueIndex}}
|
||||||
{{if $.IsSplitStyle}}
|
{{if $.IsSplitStyle}}
|
||||||
{{range $k, $line := $.section.Lines}}
|
{{range $k, $line := $.section.Lines}}
|
||||||
<tr class="{{.GetHTMLDiffLineType}}-code nl-{{$k}} ol-{{$k}} line-expanded">
|
<tr class="{{.GetHTMLDiffLineType}}-code nl-{{$k}} ol-{{$k}} line-expanded" data-line-type="{{.GetHTMLDiffLineType}}">
|
||||||
{{if eq .GetType 4}}
|
{{if eq .GetType 4}}
|
||||||
{{$expandDirection := $line.GetExpandDirection}}
|
<td class="lines-num lines-num-old">{{$line.RenderBlobExcerptButtons $.FileNameHash $diffBlobExcerptData}}</td>
|
||||||
<td class="lines-num lines-num-old" data-line-num="{{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}}">
|
|
||||||
<div class="code-expander-buttons" data-expand-direction="{{$expandDirection}}">
|
|
||||||
{{if or (eq $expandDirection 3) (eq $expandDirection 5)}}
|
|
||||||
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=split&direction=down">
|
|
||||||
{{svg "octicon-fold-down"}}
|
|
||||||
</button>
|
|
||||||
{{end}}
|
|
||||||
{{if or (eq $expandDirection 3) (eq $expandDirection 4)}}
|
|
||||||
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=split&direction=up">
|
|
||||||
{{svg "octicon-fold-up"}}
|
|
||||||
</button>
|
|
||||||
{{end}}
|
|
||||||
{{if eq $expandDirection 2}}
|
|
||||||
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=split">
|
|
||||||
{{svg "octicon-fold"}}
|
|
||||||
</button>
|
|
||||||
{{end}}
|
|
||||||
</div>
|
|
||||||
</td>
|
|
||||||
<td colspan="7" class="lines-code lines-code-old">
|
<td colspan="7" class="lines-code lines-code-old">
|
||||||
{{- $inlineDiff := $.section.GetComputedInlineDiffFor $line ctx.Locale -}}
|
{{- $inlineDiff := $.section.GetComputedInlineDiffFor $line ctx.Locale -}}
|
||||||
{{- template "repo/diff/section_code" dict "diff" $inlineDiff -}}
|
{{- template "repo/diff/section_code" dict "diff" $inlineDiff -}}
|
||||||
@ -33,6 +15,12 @@
|
|||||||
<td class="lines-escape lines-escape-old">{{if and $line.LeftIdx $inlineDiff.EscapeStatus.Escaped}}<button class="toggle-escape-button btn interact-bg" title="{{template "repo/diff/escape_title" dict "diff" $inlineDiff}}"></button>{{end}}</td>
|
<td class="lines-escape lines-escape-old">{{if and $line.LeftIdx $inlineDiff.EscapeStatus.Escaped}}<button class="toggle-escape-button btn interact-bg" title="{{template "repo/diff/escape_title" dict "diff" $inlineDiff}}"></button>{{end}}</td>
|
||||||
<td class="lines-type-marker lines-type-marker-old">{{if $line.LeftIdx}}<span class="tw-font-mono" data-type-marker=""></span>{{end}}</td>
|
<td class="lines-type-marker lines-type-marker-old">{{if $line.LeftIdx}}<span class="tw-font-mono" data-type-marker=""></span>{{end}}</td>
|
||||||
<td class="lines-code lines-code-old">
|
<td class="lines-code lines-code-old">
|
||||||
|
{{/* ATTENTION: BLOB-EXCERPT-COMMENT-RIGHT: here it intentially use "right" side to comment, because the backend code depends on the assumption that the comment only happens on right side*/}}
|
||||||
|
{{- if and $canCreateComment $line.RightIdx -}}
|
||||||
|
<button type="button" aria-label="{{ctx.Locale.Tr "repo.diff.comment.add_line_comment"}}" class="ui primary button add-code-comment add-code-comment-right{{if (not $line.CanComment)}} tw-invisible{{end}}" data-side="right" data-idx="{{$line.RightIdx}}">
|
||||||
|
{{- svg "octicon-plus" -}}
|
||||||
|
</button>
|
||||||
|
{{- end -}}
|
||||||
{{- if $line.LeftIdx -}}
|
{{- if $line.LeftIdx -}}
|
||||||
{{- template "repo/diff/section_code" dict "diff" $inlineDiff -}}
|
{{- template "repo/diff/section_code" dict "diff" $inlineDiff -}}
|
||||||
{{- else -}}
|
{{- else -}}
|
||||||
@ -43,6 +31,11 @@
|
|||||||
<td class="lines-escape lines-escape-new">{{if and $line.RightIdx $inlineDiff.EscapeStatus.Escaped}}<button class="toggle-escape-button btn interact-bg" title="{{template "repo/diff/escape_title" dict "diff" $inlineDiff}}"></button>{{end}}</td>
|
<td class="lines-escape lines-escape-new">{{if and $line.RightIdx $inlineDiff.EscapeStatus.Escaped}}<button class="toggle-escape-button btn interact-bg" title="{{template "repo/diff/escape_title" dict "diff" $inlineDiff}}"></button>{{end}}</td>
|
||||||
<td class="lines-type-marker lines-type-marker-new">{{if $line.RightIdx}}<span class="tw-font-mono" data-type-marker=""></span>{{end}}</td>
|
<td class="lines-type-marker lines-type-marker-new">{{if $line.RightIdx}}<span class="tw-font-mono" data-type-marker=""></span>{{end}}</td>
|
||||||
<td class="lines-code lines-code-new">
|
<td class="lines-code lines-code-new">
|
||||||
|
{{- if and $canCreateComment $line.RightIdx -}}
|
||||||
|
<button type="button" aria-label="{{ctx.Locale.Tr "repo.diff.comment.add_line_comment"}}" class="ui primary button add-code-comment add-code-comment-right{{if (not $line.CanComment)}} tw-invisible{{end}}" data-side="right" data-idx="{{$line.RightIdx}}">
|
||||||
|
{{- svg "octicon-plus" -}}
|
||||||
|
</button>
|
||||||
|
{{- end -}}
|
||||||
{{- if $line.RightIdx -}}
|
{{- if $line.RightIdx -}}
|
||||||
{{- template "repo/diff/section_code" dict "diff" $inlineDiff -}}
|
{{- template "repo/diff/section_code" dict "diff" $inlineDiff -}}
|
||||||
{{- else -}}
|
{{- else -}}
|
||||||
@ -51,31 +44,26 @@
|
|||||||
</td>
|
</td>
|
||||||
{{end}}
|
{{end}}
|
||||||
</tr>
|
</tr>
|
||||||
|
{{if $line.Comments}}
|
||||||
|
<tr class="add-comment" data-line-type="{{.GetHTMLDiffLineType}}">
|
||||||
|
<td class="add-comment-left" colspan="4">
|
||||||
|
{{if eq $line.GetCommentSide "previous"}}
|
||||||
|
{{template "repo/diff/conversation" dict "." $ "comments" $line.Comments}}
|
||||||
|
{{end}}
|
||||||
|
</td>
|
||||||
|
<td class="add-comment-right" colspan="4">
|
||||||
|
{{if eq $line.GetCommentSide "proposed"}}
|
||||||
|
{{template "repo/diff/conversation" dict "." $ "comments" $line.Comments}}
|
||||||
|
{{end}}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
{{else}}
|
{{else}}
|
||||||
{{range $k, $line := $.section.Lines}}
|
{{range $k, $line := $.section.Lines}}
|
||||||
<tr class="{{.GetHTMLDiffLineType}}-code nl-{{$k}} ol-{{$k}} line-expanded">
|
<tr class="{{.GetHTMLDiffLineType}}-code nl-{{$k}} ol-{{$k}} line-expanded" data-line-type="{{.GetHTMLDiffLineType}}">
|
||||||
{{if eq .GetType 4}}
|
{{if eq .GetType 4}}
|
||||||
{{$expandDirection := $line.GetExpandDirection}}
|
<td colspan="2" class="lines-num">{{$line.RenderBlobExcerptButtons $.FileNameHash $diffBlobExcerptData}}</td>
|
||||||
<td colspan="2" class="lines-num">
|
|
||||||
<div class="code-expander-buttons" data-expand-direction="{{$expandDirection}}">
|
|
||||||
{{if or (eq $expandDirection 3) (eq $expandDirection 5)}}
|
|
||||||
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified&direction=down">
|
|
||||||
{{svg "octicon-fold-down"}}
|
|
||||||
</button>
|
|
||||||
{{end}}
|
|
||||||
{{if or (eq $expandDirection 3) (eq $expandDirection 4)}}
|
|
||||||
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified&direction=up">
|
|
||||||
{{svg "octicon-fold-up"}}
|
|
||||||
</button>
|
|
||||||
{{end}}
|
|
||||||
{{if eq $expandDirection 2}}
|
|
||||||
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified">
|
|
||||||
{{svg "octicon-fold"}}
|
|
||||||
</button>
|
|
||||||
{{end}}
|
|
||||||
</div>
|
|
||||||
</td>
|
|
||||||
{{else}}
|
{{else}}
|
||||||
<td class="lines-num lines-num-old" data-line-num="{{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}}"><span rel="{{if $line.LeftIdx}}diff-{{$.FileNameHash}}L{{$line.LeftIdx}}{{end}}"></span></td>
|
<td class="lines-num lines-num-old" data-line-num="{{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}}"><span rel="{{if $line.LeftIdx}}diff-{{$.FileNameHash}}L{{$line.LeftIdx}}{{end}}"></span></td>
|
||||||
<td class="lines-num lines-num-new" data-line-num="{{if $line.RightIdx}}{{$line.RightIdx}}{{end}}"><span rel="{{if $line.RightIdx}}diff-{{$.FileNameHash}}R{{$line.RightIdx}}{{end}}"></span></td>
|
<td class="lines-num lines-num-new" data-line-num="{{if $line.RightIdx}}{{$line.RightIdx}}{{end}}"><span rel="{{if $line.RightIdx}}diff-{{$.FileNameHash}}R{{$line.RightIdx}}{{end}}"></span></td>
|
||||||
@ -83,7 +71,21 @@
|
|||||||
{{$inlineDiff := $.section.GetComputedInlineDiffFor $line ctx.Locale}}
|
{{$inlineDiff := $.section.GetComputedInlineDiffFor $line ctx.Locale}}
|
||||||
<td class="lines-escape">{{if $inlineDiff.EscapeStatus.Escaped}}<button class="toggle-escape-button btn interact-bg" title="{{template "repo/diff/escape_title" dict "diff" $inlineDiff}}"></button>{{end}}</td>
|
<td class="lines-escape">{{if $inlineDiff.EscapeStatus.Escaped}}<button class="toggle-escape-button btn interact-bg" title="{{template "repo/diff/escape_title" dict "diff" $inlineDiff}}"></button>{{end}}</td>
|
||||||
<td class="lines-type-marker"><span class="tw-font-mono" data-type-marker="{{$line.GetLineTypeMarker}}"></span></td>
|
<td class="lines-type-marker"><span class="tw-font-mono" data-type-marker="{{$line.GetLineTypeMarker}}"></span></td>
|
||||||
<td class="lines-code{{if (not $line.RightIdx)}} lines-code-old{{end}}"><code {{if $inlineDiff.EscapeStatus.Escaped}}class="code-inner has-escaped" title="{{template "repo/diff/escape_title" dict "diff" $inlineDiff}}"{{else}}class="code-inner"{{end}}>{{$inlineDiff.Content}}</code></td>
|
<td class="lines-code{{if (not $line.RightIdx)}} lines-code-old{{end}}">
|
||||||
|
{{- if and $canCreateComment -}}
|
||||||
|
<button type="button" aria-label="{{ctx.Locale.Tr "repo.diff.comment.add_line_comment"}}" class="ui primary button add-code-comment add-code-comment-{{if $line.RightIdx}}right{{else}}left{{end}}{{if (not $line.CanComment)}} tw-invisible{{end}}" data-side="{{if $line.RightIdx}}right{{else}}left{{end}}" data-idx="{{if $line.RightIdx}}{{$line.RightIdx}}{{else}}{{$line.LeftIdx}}{{end}}">
|
||||||
|
{{- svg "octicon-plus" -}}
|
||||||
|
</button>
|
||||||
|
{{- end -}}
|
||||||
|
<code {{if $inlineDiff.EscapeStatus.Escaped}}class="code-inner has-escaped" title="{{template "repo/diff/escape_title" dict "diff" $inlineDiff}}"{{else}}class="code-inner"{{end}}>{{$inlineDiff.Content}}</code>
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
{{if $line.Comments}}
|
||||||
|
<tr class="add-comment" data-line-type="{{.GetHTMLDiffLineType}}">
|
||||||
|
<td class="add-comment-left add-comment-right" colspan="5">
|
||||||
|
{{template "repo/diff/conversation" dict "." $ "comments" $line.Comments}}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
{{$file := .file}}
|
{{$file := .file}}
|
||||||
{{$blobExcerptLink := print (or ctx.RootData.CommitRepoLink ctx.RootData.RepoLink) (Iif $.root.PageIsWiki "/wiki" "") "/blob_excerpt/" (PathEscape $.root.AfterCommitID) "?"}}
|
{{$diffBlobExcerptData := $.root.DiffBlobExcerptData}}
|
||||||
<colgroup>
|
<colgroup>
|
||||||
<col width="50">
|
<col width="50">
|
||||||
<col width="10">
|
<col width="10">
|
||||||
@ -16,26 +16,8 @@
|
|||||||
{{if or (ne .GetType 2) (not $hasmatch)}}
|
{{if or (ne .GetType 2) (not $hasmatch)}}
|
||||||
<tr class="{{.GetHTMLDiffLineType}}-code nl-{{$k}} ol-{{$k}}" data-line-type="{{.GetHTMLDiffLineType}}">
|
<tr class="{{.GetHTMLDiffLineType}}-code nl-{{$k}} ol-{{$k}}" data-line-type="{{.GetHTMLDiffLineType}}">
|
||||||
{{if eq .GetType 4}}
|
{{if eq .GetType 4}}
|
||||||
{{$expandDirection := $line.GetExpandDirection}}
|
{{$inlineDiff := $section.GetComputedInlineDiffFor $line ctx.Locale}}
|
||||||
<td class="lines-num lines-num-old">
|
<td class="lines-num lines-num-old">{{$line.RenderBlobExcerptButtons $file.NameHash $diffBlobExcerptData}}</td>
|
||||||
<div class="code-expander-buttons" data-expand-direction="{{$expandDirection}}">
|
|
||||||
{{if or (eq $expandDirection 3) (eq $expandDirection 5)}}
|
|
||||||
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=split&direction=down&&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}">
|
|
||||||
{{svg "octicon-fold-down"}}
|
|
||||||
</button>
|
|
||||||
{{end}}
|
|
||||||
{{if or (eq $expandDirection 3) (eq $expandDirection 4)}}
|
|
||||||
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=split&direction=up&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}">
|
|
||||||
{{svg "octicon-fold-up"}}
|
|
||||||
</button>
|
|
||||||
{{end}}
|
|
||||||
{{if eq $expandDirection 2}}
|
|
||||||
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=split&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}">
|
|
||||||
{{svg "octicon-fold"}}
|
|
||||||
</button>
|
|
||||||
{{end}}
|
|
||||||
</div>
|
|
||||||
</td>{{$inlineDiff := $section.GetComputedInlineDiffFor $line ctx.Locale}}
|
|
||||||
<td class="lines-escape lines-escape-old">{{if $inlineDiff.EscapeStatus.Escaped}}<button class="toggle-escape-button btn interact-bg" title="{{template "repo/diff/escape_title" dict "diff" $inlineDiff}}"></button>{{end}}</td>
|
<td class="lines-escape lines-escape-old">{{if $inlineDiff.EscapeStatus.Escaped}}<button class="toggle-escape-button btn interact-bg" title="{{template "repo/diff/escape_title" dict "diff" $inlineDiff}}"></button>{{end}}</td>
|
||||||
<td colspan="6" class="lines-code lines-code-old">{{template "repo/diff/section_code" dict "diff" $inlineDiff}}</td>
|
<td colspan="6" class="lines-code lines-code-old">{{template "repo/diff/section_code" dict "diff" $inlineDiff}}</td>
|
||||||
{{else if and (eq .GetType 3) $hasmatch}}{{/* DEL */}}
|
{{else if and (eq .GetType 3) $hasmatch}}{{/* DEL */}}
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
{{$file := .file}}
|
{{$file := .file}}
|
||||||
{{$repoLink := or ctx.RootData.CommitRepoLink ctx.RootData.RepoLink}}
|
{{/* this tmpl is also used by the PR Conversation page, so the "AfterCommitID" and "DiffBlobExcerptData" may not exist */}}
|
||||||
{{$afterCommitID := or $.root.AfterCommitID "no-after-commit-id"}}{{/* this tmpl is also used by the PR Conversation page, so the "AfterCommitID" may not exist */}}
|
{{$diffBlobExcerptData := $.root.DiffBlobExcerptData}}
|
||||||
{{$blobExcerptLink := print $repoLink (Iif $.root.PageIsWiki "/wiki" "") "/blob_excerpt/" (PathEscape $afterCommitID) "?"}}
|
|
||||||
<colgroup>
|
<colgroup>
|
||||||
<col width="50">
|
<col width="50">
|
||||||
<col width="50">
|
<col width="50">
|
||||||
@ -14,26 +13,7 @@
|
|||||||
<tr class="{{.GetHTMLDiffLineType}}-code nl-{{$k}} ol-{{$k}}" data-line-type="{{.GetHTMLDiffLineType}}">
|
<tr class="{{.GetHTMLDiffLineType}}-code nl-{{$k}} ol-{{$k}}" data-line-type="{{.GetHTMLDiffLineType}}">
|
||||||
{{if eq .GetType 4}}
|
{{if eq .GetType 4}}
|
||||||
{{if $.root.AfterCommitID}}
|
{{if $.root.AfterCommitID}}
|
||||||
{{$expandDirection := $line.GetExpandDirection}}
|
<td colspan="2" class="lines-num">{{$line.RenderBlobExcerptButtons $file.NameHash $diffBlobExcerptData}}</td>
|
||||||
<td colspan="2" class="lines-num">
|
|
||||||
<div class="code-expander-buttons" data-expand-direction="{{$expandDirection}}">
|
|
||||||
{{if or (eq $expandDirection 3) (eq $expandDirection 5)}}
|
|
||||||
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified&direction=down&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}">
|
|
||||||
{{svg "octicon-fold-down"}}
|
|
||||||
</button>
|
|
||||||
{{end}}
|
|
||||||
{{if or (eq $expandDirection 3) (eq $expandDirection 4)}}
|
|
||||||
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified&direction=up&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}">
|
|
||||||
{{svg "octicon-fold-up"}}
|
|
||||||
</button>
|
|
||||||
{{end}}
|
|
||||||
{{if eq $expandDirection 2}}
|
|
||||||
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}">
|
|
||||||
{{svg "octicon-fold"}}
|
|
||||||
</button>
|
|
||||||
{{end}}
|
|
||||||
</div>
|
|
||||||
</td>
|
|
||||||
{{else}}
|
{{else}}
|
||||||
{{/* for code file preview page or comment diffs on pull comment pages, do not show the expansion arrows */}}
|
{{/* for code file preview page or comment diffs on pull comment pages, do not show the expansion arrows */}}
|
||||||
<td colspan="2" class="lines-num"></td>
|
<td colspan="2" class="lines-num"></td>
|
||||||
|
|||||||
@ -1,3 +1,8 @@
|
|||||||
|
{{/* FIXME: DIFF-CONVERSATION-DATA: in the future this template should be refactor to avoid called by {{... "." $}}
|
||||||
|
At the moment, two kinds of request handler call this template:
|
||||||
|
* ExcerptBlob -> blob_excerpt.tmpl -> this
|
||||||
|
* Other compare and diff pages -> ... -> {section_unified.tmpl|section_split.tmpl} -> this)
|
||||||
|
The variables in "ctx.Data" are different in each case, making this template fragile, hard to read and maintain. */}}
|
||||||
{{if len .comments}}
|
{{if len .comments}}
|
||||||
{{$comment := index .comments 0}}
|
{{$comment := index .comments 0}}
|
||||||
{{$invalid := $comment.Invalidated}}
|
{{$invalid := $comment.Invalidated}}
|
||||||
|
|||||||
@ -115,6 +115,23 @@
|
|||||||
color: var(--color-text);
|
color: var(--color-text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.code-expander-buttons {
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
|
||||||
|
.code-expander-buttons .code-comment-more {
|
||||||
|
position: absolute;
|
||||||
|
line-height: 12px;
|
||||||
|
padding: 2px 4px;
|
||||||
|
font-size: 10px;
|
||||||
|
background-color: var(--color-primary);
|
||||||
|
color: var(--color-primary-contrast);
|
||||||
|
border-radius: 8px !important;
|
||||||
|
left: -12px;
|
||||||
|
top: 6px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
.code-expander-button {
|
.code-expander-button {
|
||||||
border: none;
|
border: none;
|
||||||
color: var(--color-text-light);
|
color: var(--color-text-light);
|
||||||
@ -127,8 +144,8 @@
|
|||||||
flex: 1;
|
flex: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* expand direction 3 is both ways with two buttons */
|
/* expand direction "updown" is both ways with two buttons */
|
||||||
.code-expander-buttons[data-expand-direction="3"] .code-expander-button {
|
.code-expander-buttons[data-expand-direction="updown"] .code-expander-button {
|
||||||
height: 18px;
|
height: 18px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import {submitEventSubmitter, queryElemSiblings, hideElem, showElem, animateOnce
|
|||||||
import {POST, GET} from '../modules/fetch.ts';
|
import {POST, GET} from '../modules/fetch.ts';
|
||||||
import {createTippy} from '../modules/tippy.ts';
|
import {createTippy} from '../modules/tippy.ts';
|
||||||
import {invertFileFolding} from './file-fold.ts';
|
import {invertFileFolding} from './file-fold.ts';
|
||||||
import {parseDom} from '../utils.ts';
|
import {parseDom, sleep} from '../utils.ts';
|
||||||
import {registerGlobalSelectorFunc} from '../modules/observer.ts';
|
import {registerGlobalSelectorFunc} from '../modules/observer.ts';
|
||||||
|
|
||||||
const {i18n} = window.config;
|
const {i18n} = window.config;
|
||||||
@ -218,21 +218,46 @@ function initRepoDiffShowMore() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadUntilFound() {
|
async function onLocationHashChange() {
|
||||||
const hashTargetSelector = window.location.hash;
|
// try to scroll to the target element by the current hash
|
||||||
if (!hashTargetSelector.startsWith('#diff-') && !hashTargetSelector.startsWith('#issuecomment-')) {
|
const currentHash = window.location.hash;
|
||||||
return;
|
if (!currentHash.startsWith('#diff-') && !currentHash.startsWith('#issuecomment-')) return;
|
||||||
}
|
|
||||||
|
|
||||||
while (true) {
|
// avoid reentrance when we are changing the hash to scroll and trigger ":target" selection
|
||||||
|
const attrAutoScrollRunning = 'data-auto-scroll-running';
|
||||||
|
if (document.body.hasAttribute(attrAutoScrollRunning)) return;
|
||||||
|
|
||||||
|
const targetElementId = currentHash.substring(1);
|
||||||
|
while (currentHash === window.location.hash) {
|
||||||
// use getElementById to avoid querySelector throws an error when the hash is invalid
|
// use getElementById to avoid querySelector throws an error when the hash is invalid
|
||||||
// eslint-disable-next-line unicorn/prefer-query-selector
|
// eslint-disable-next-line unicorn/prefer-query-selector
|
||||||
const targetElement = document.getElementById(hashTargetSelector.substring(1));
|
const targetElement = document.getElementById(targetElementId);
|
||||||
if (targetElement) {
|
if (targetElement) {
|
||||||
|
// need to change hash to re-trigger ":target" CSS selector, let's manually scroll to it
|
||||||
targetElement.scrollIntoView();
|
targetElement.scrollIntoView();
|
||||||
|
document.body.setAttribute(attrAutoScrollRunning, 'true');
|
||||||
|
window.location.hash = '';
|
||||||
|
window.location.hash = currentHash;
|
||||||
|
setTimeout(() => document.body.removeAttribute(attrAutoScrollRunning), 0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If looking for a hidden comment, try to expand the section that contains it
|
||||||
|
const issueCommentPrefix = '#issuecomment-';
|
||||||
|
if (currentHash.startsWith(issueCommentPrefix)) {
|
||||||
|
const commentId = currentHash.substring(issueCommentPrefix.length);
|
||||||
|
const expandButton = document.querySelector<HTMLElement>(`.code-expander-button[data-hidden-comment-ids*=",${commentId},"]`);
|
||||||
|
if (expandButton) {
|
||||||
|
// avoid infinite loop, do not re-click the button if already clicked
|
||||||
|
const attrAutoLoadClicked = 'data-auto-load-clicked';
|
||||||
|
if (expandButton.hasAttribute(attrAutoLoadClicked)) return;
|
||||||
|
expandButton.setAttribute(attrAutoLoadClicked, 'true');
|
||||||
|
expandButton.click();
|
||||||
|
await sleep(500); // Wait for HTMX to load the content. FIXME: need to drop htmx in the future
|
||||||
|
continue; // Try again to find the element
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// the button will be refreshed after each "load more", so query it every time
|
// the button will be refreshed after each "load more", so query it every time
|
||||||
const showMoreButton = document.querySelector('#diff-show-more-files');
|
const showMoreButton = document.querySelector('#diff-show-more-files');
|
||||||
if (!showMoreButton) {
|
if (!showMoreButton) {
|
||||||
@ -246,8 +271,8 @@ async function loadUntilFound() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function initRepoDiffHashChangeListener() {
|
function initRepoDiffHashChangeListener() {
|
||||||
window.addEventListener('hashchange', loadUntilFound);
|
window.addEventListener('hashchange', onLocationHashChange);
|
||||||
loadUntilFound();
|
onLocationHashChange();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function initRepoDiffView() {
|
export function initRepoDiffView() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user