Skip to content

Commit

Permalink
update go-gitea#8659 implement expand up/down codes (blob excerpt)
Browse files Browse the repository at this point in the history
  • Loading branch information
blueworrybear committed Nov 10, 2019
1 parent 55a39cb commit d33652e
Show file tree
Hide file tree
Showing 10 changed files with 385 additions and 27 deletions.
22 changes: 22 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,27 @@ func (b *Blob) GetBlobContent() (string, error) {
return string(buf), nil
}

// GetGlobLineCount gets line count of lob as raw text
func (b *Blob) GetGlobLineCount() (int, error) {
reader, err := b.DataAsync()
if err != nil {
return 0, err
}
buf := make([]byte, 32*1024)
count := 0
lineSep := []byte{'\n'}
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
10 changes: 10 additions & 0 deletions public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2004,6 +2004,16 @@ function initCodeView() {
});
}
})

function insertBlobExcerpt(e){
const $blob = $(e.target);
const $row = $blob.parent().parent();
$.get($blob.data("url") + "?" + $blob.data("query") + "&anchor=" + $blob.data("anchor"), function(blob){
$row.replaceWith(blob);
$('[data-anchor="'+ $blob.data("anchor") +'"]').on('click', function(e){insertBlobExcerpt(e)});
})
}
$('.ui.blob-excerpt').on('click', function(e){insertBlobExcerpt(e)});
}

function initU2FAuth() {
Expand Down
1 change: 1 addition & 0 deletions routers/repo/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ func Diff(ctx *context.Context) {
}

ctx.Data["CommitID"] = commitID
ctx.Data["AfterCommitID"] = commitID
ctx.Data["Username"] = userName
ctx.Data["Reponame"] = repoName

Expand Down
109 changes: 108 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 @@ -423,3 +428,105 @@ func CompareDiff(ctx *context.Context) {

ctx.HTML(200, tplCompare)
}

// ExcerptBlob render blob excerpt conten
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 := 20
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)
}
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()
defer reader.Close()
if err != nil {
return nil, err
}
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 @@ -540,6 +540,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

0 comments on commit d33652e

Please sign in to comment.