Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand/Collapse Files and Blob Excerpt while Reviewing/Comparing code #8924

Merged
merged 27 commits into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
7ff74b7
update #8659 fold/unfold code diffs
blueworrybear Oct 31, 2019
e860843
add fold button style
blueworrybear Nov 3, 2019
ff095a4
Merge branch 'master' into fold-code
blueworrybear Nov 3, 2019
55a39cb
Merge branch 'master' into fold-code
blueworrybear Nov 5, 2019
d33652e
update #8659 implement expand up/down codes (blob excerpt)
blueworrybear Nov 10, 2019
a16db00
fix golint errors
blueworrybear Nov 10, 2019
26bd7f9
fix expand direction
blueworrybear Nov 11, 2019
bda6f10
remove debug message
blueworrybear Nov 11, 2019
8578956
update css style for blob exceprt
blueworrybear Nov 11, 2019
9eeb5a8
Merge branch 'master' into fold-code
blueworrybear Nov 11, 2019
39aefe9
fix typo in comment
blueworrybear Nov 11, 2019
e2284c3
Merge branch 'master' into fold-code
blueworrybear Nov 11, 2019
c228524
update style sheet with less
blueworrybear Nov 11, 2019
560525f
Merge branch 'fold-code' of https://github.com/blueworrybear/gitea in…
blueworrybear Nov 11, 2019
559d951
update expect diff (add SectionInfo)
blueworrybear Nov 12, 2019
7151bc7
Merge branch 'master' into fold-code
blueworrybear Nov 12, 2019
62f59eb
update #8942 accept suggested change (fix typo)
blueworrybear Nov 12, 2019
203c5c4
close reader and check file type before get tail section
blueworrybear Nov 14, 2019
8dc6285
adjust button position and check file type before insert fold button
blueworrybear Nov 14, 2019
e34296e
move index js to web_src
blueworrybear Nov 14, 2019
cec6fcf
merge with master
blueworrybear Nov 14, 2019
f218833
merge index.js with master
blueworrybear Nov 14, 2019
d1c438e
generate index.js
blueworrybear Nov 14, 2019
ce2348b
Merge branch 'master' into fold-code
lunny Nov 14, 2019
bf22b7a
merge with master
blueworrybear Nov 15, 2019
a60dee6
update js coding style
blueworrybear Nov 15, 2019
36231d6
Merge branch 'fold-code' of https://github.com/blueworrybear/gitea in…
blueworrybear Nov 15, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions modules/git/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package git

import (
"bytes"
"encoding/base64"
"io"
"io/ioutil"
Expand Down Expand Up @@ -50,6 +51,28 @@ func (b *Blob) GetBlobContent() (string, error) {
return string(buf), nil
}

// GetBlobLineCount gets line count of lob as raw text
func (b *Blob) GetBlobLineCount() (int, error) {
reader, err := b.DataAsync()
blueworrybear marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return 0, err
}
defer reader.Close()
buf := make([]byte, 32*1024)
count := 0
lineSep := []byte{'\n'}
lafriks marked this conversation as resolved.
Show resolved Hide resolved
for {
c, err := reader.Read(buf)
count += bytes.Count(buf[:c], lineSep)
switch {
case err == io.EOF:
return count, nil
case err != nil:
return count, err
}
}
}

// GetBlobContentBase64 Reads the content of the blob with a base64 encode and returns the encoded string
func (b *Blob) GetBlobContentBase64() (string, error) {
dataRc, err := b.DataAsync()
Expand Down
9 changes: 9 additions & 0 deletions modules/repofiles/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ func TestGetDiffPreview(t *testing.T) {
Type: 4,
Content: "@@ -1,3 +1,4 @@",
Comments: nil,
SectionInfo: &gitdiff.DiffLineSectionInfo{
Path: "README.md",
LastLeftIdx: 0,
LastRightIdx: 0,
LeftIdx: 1,
RightIdx: 1,
LeftHunkSize: 3,
RightHunkSize: 4,
},
},
{
LeftIdx: 1,
Expand Down
7 changes: 6 additions & 1 deletion public/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,7 @@ tbody.commit-list{vertical-align:baseline}
.repo-buttons .disabled-repo-button a.button:hover{background:0 0!important;color:rgba(0,0,0,.6)!important;box-shadow:0 0 0 1px rgba(34,36,38,.15) inset!important}
.repo-buttons .ui.labeled.button>.label{border-left:0!important;margin:0!important}
.tag-code,.tag-code td{background-color:#f0f0f0!important;border-color:#d3cfcf!important;padding-top:8px;padding-bottom:8px}
td.blob-excerpt{background-color:#fafafa}
.issue-keyword{border-bottom:1px dotted #959da5;display:inline-block}
.file-header{display:flex;justify-content:space-between;align-items:center;padding:8px 12px!important}
.file-info{display:flex;align-items:center}
Expand Down Expand Up @@ -1068,4 +1069,8 @@ tbody.commit-list{vertical-align:baseline}
.comment-code-cloud .footer:after{clear:both;content:"";display:block}
.comment-code-cloud button.comment-form-reply{margin:.5em .5em .5em 4.5em}
.comment-code-cloud form.comment-form-reply{margin:0 0 0 4em}
.file-comment{font:12px 'SF Mono',Consolas,Menlo,'Liberation Mono',Monaco,'Lucida Console',monospace;color:rgba(0,0,0,.87)}
.file-comment{font:12px 'SF Mono',Consolas,Menlo,'Liberation Mono',Monaco,'Lucida Console',monospace;color:rgba(0,0,0,.87)}
.ui.fold-code{margin-right:1em;padding-left:5px;cursor:pointer;width:22px;font-size:12px}
.ui.fold-code:hover{color:#428bca}
.ui.blob-excerpt{display:block;line-height:20px;font-size:16px;cursor:pointer}
.ui.blob-excerpt:hover{color:#428bca}
2 changes: 1 addition & 1 deletion public/js/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/index.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions routers/repo/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
113 changes: 112 additions & 1 deletion routers/repo/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
blueworrybear marked this conversation as resolved.
Show resolved Hide resolved
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
}
1 change: 1 addition & 0 deletions routers/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading