diff options
author | Benno <blueworrybear@gmail.com> | 2019-11-15 10:52:59 +0800 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2019-11-15 10:52:59 +0800 |
commit | 149a9df9e8e7e91c813232fb82e644b0d3369b09 (patch) | |
tree | cfb677cde7578ad448a201224522c50b227a6097 /routers | |
parent | 42ada741e3360b14ede8772aa1a2dd3e83209033 (diff) | |
download | gitea-149a9df9e8e7e91c813232fb82e644b0d3369b09.tar.gz gitea-149a9df9e8e7e91c813232fb82e644b0d3369b09.zip |
Expand/Collapse Files and Blob Excerpt while Reviewing/Comparing code (#8924)
* update #8659 fold/unfold code diffs
* add fold button style
* update #8659 implement expand up/down codes (blob excerpt)
* fix golint errors
* fix expand direction
* remove debug message
* update css style for blob exceprt
* fix typo in comment
* update style sheet with less
* update expect diff (add SectionInfo)
* update #8942 accept suggested change (fix typo)
* close reader and check file type before get tail section
* adjust button position and check file type before insert fold button
* move index js to web_src
* merge index.js with master
* generate index.js
* update js coding style
Diffstat (limited to 'routers')
-rw-r--r-- | routers/repo/commit.go | 1 | ||||
-rw-r--r-- | routers/repo/compare.go | 113 | ||||
-rw-r--r-- | routers/repo/pull.go | 1 | ||||
-rw-r--r-- | routers/routes/routes.go | 4 |
4 files changed, 118 insertions, 1 deletions
diff --git a/routers/repo/commit.go b/routers/repo/commit.go index f067729ca9..2a123a2eb9 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -245,6 +245,7 @@ func Diff(ctx *context.Context) { } ctx.Data["CommitID"] = commitID + ctx.Data["AfterCommitID"] = commitID ctx.Data["Username"] = userName ctx.Data["Reponame"] = repoName diff --git a/routers/repo/compare.go b/routers/repo/compare.go index e45f046435..203b740082 100644 --- a/routers/repo/compare.go +++ b/routers/repo/compare.go @@ -5,21 +5,26 @@ package repo import ( + "bufio" "fmt" + "html" "path" + "path/filepath" "strings" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/highlight" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/services/gitdiff" ) const ( - tplCompare base.TplName = "repo/diff/compare" + tplCompare base.TplName = "repo/diff/compare" + tplBlobExcerpt base.TplName = "repo/diff/blob_excerpt" ) // setPathsCompareContext sets context data for source and raw paths @@ -434,3 +439,109 @@ func CompareDiff(ctx *context.Context) { ctx.HTML(200, tplCompare) } + +// ExcerptBlob render blob excerpt contents +func ExcerptBlob(ctx *context.Context) { + commitID := ctx.Params("sha") + lastLeft := ctx.QueryInt("last_left") + lastRight := ctx.QueryInt("last_right") + idxLeft := ctx.QueryInt("left") + idxRight := ctx.QueryInt("right") + leftHunkSize := ctx.QueryInt("left_hunk_size") + rightHunkSize := ctx.QueryInt("right_hunk_size") + anchor := ctx.Query("anchor") + direction := ctx.Query("direction") + filePath := ctx.Query("path") + gitRepo := ctx.Repo.GitRepo + chunkSize := gitdiff.BlobExceprtChunkSize + commit, err := gitRepo.GetCommit(commitID) + if err != nil { + ctx.Error(500, "GetCommit") + return + } + section := &gitdiff.DiffSection{ + Name: filePath, + } + if direction == "up" && (idxLeft-lastLeft) > chunkSize { + idxLeft -= chunkSize + idxRight -= chunkSize + leftHunkSize += chunkSize + rightHunkSize += chunkSize + section.Lines, err = getExcerptLines(commit, filePath, idxLeft-1, idxRight-1, chunkSize) + } else if direction == "down" && (idxLeft-lastLeft) > chunkSize { + section.Lines, err = getExcerptLines(commit, filePath, lastLeft, lastRight, chunkSize) + lastLeft += chunkSize + lastRight += chunkSize + } else { + section.Lines, err = getExcerptLines(commit, filePath, lastLeft, lastRight, idxRight-lastRight-1) + leftHunkSize = 0 + rightHunkSize = 0 + idxLeft = lastLeft + idxRight = lastRight + } + if err != nil { + ctx.Error(500, "getExcerptLines") + return + } + if idxRight > lastRight { + lineText := " " + if rightHunkSize > 0 || leftHunkSize > 0 { + lineText = fmt.Sprintf("@@ -%d,%d +%d,%d @@\n", idxLeft, leftHunkSize, idxRight, rightHunkSize) + } + lineText = html.EscapeString(lineText) + lineSection := &gitdiff.DiffLine{ + Type: gitdiff.DiffLineSection, + Content: lineText, + SectionInfo: &gitdiff.DiffLineSectionInfo{ + Path: filePath, + LastLeftIdx: lastLeft, + LastRightIdx: lastRight, + LeftIdx: idxLeft, + RightIdx: idxRight, + LeftHunkSize: leftHunkSize, + RightHunkSize: rightHunkSize, + }} + if direction == "up" { + section.Lines = append([]*gitdiff.DiffLine{lineSection}, section.Lines...) + } else if direction == "down" { + section.Lines = append(section.Lines, lineSection) + } + } + ctx.Data["section"] = section + ctx.Data["fileName"] = filePath + ctx.Data["highlightClass"] = highlight.FileNameToHighlightClass(filepath.Base(filePath)) + ctx.Data["AfterCommitID"] = commitID + ctx.Data["Anchor"] = anchor + ctx.HTML(200, tplBlobExcerpt) +} + +func getExcerptLines(commit *git.Commit, filePath string, idxLeft int, idxRight int, chunkSize int) ([]*gitdiff.DiffLine, error) { + blob, err := commit.Tree.GetBlobByPath(filePath) + if err != nil { + return nil, err + } + reader, err := blob.DataAsync() + if err != nil { + return nil, err + } + defer reader.Close() + scanner := bufio.NewScanner(reader) + var diffLines []*gitdiff.DiffLine + for line := 0; line < idxRight+chunkSize; line++ { + if ok := scanner.Scan(); !ok { + break + } + if line < idxRight { + continue + } + lineText := scanner.Text() + diffLine := &gitdiff.DiffLine{ + LeftIdx: idxLeft + (line - idxRight) + 1, + RightIdx: line + 1, + Type: gitdiff.DiffLinePlain, + Content: " " + lineText, + } + diffLines = append(diffLines, diffLine) + } + return diffLines, nil +} diff --git a/routers/repo/pull.go b/routers/repo/pull.go index 67849f33e1..a1a4e5e2bb 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -552,6 +552,7 @@ func ViewPullFiles(ctx *context.Context) { ctx.Data["Username"] = pull.MustHeadUserName() ctx.Data["Reponame"] = pull.HeadRepo.Name } + ctx.Data["AfterCommitID"] = endCommitID diff, err := gitdiff.GetDiffRangeWithWhitespaceBehavior(diffRepoPath, startCommitID, endCommitID, setting.Git.MaxGitDiffLines, diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 13a5bb2708..48bba16bf6 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -864,6 +864,10 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("", repo.Branches) }, repo.MustBeNotEmpty, context.RepoRef(), reqRepoCodeReader) + m.Group("/blob_excerpt", func() { + m.Get("/:sha", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ExcerptBlob) + }, repo.MustBeNotEmpty, context.RepoRef(), reqRepoCodeReader) + m.Group("/pulls/:index", func() { m.Get(".diff", repo.DownloadPullDiff) m.Get(".patch", repo.DownloadPullPatch) |