Skip to content

Commit d33652e

Browse files
committed
update go-gitea#8659 implement expand up/down codes (blob excerpt)
1 parent 55a39cb commit d33652e

File tree

10 files changed

+385
-27
lines changed

10 files changed

+385
-27
lines changed

modules/git/blob.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package git
77

88
import (
9+
"bytes"
910
"encoding/base64"
1011
"io"
1112
"io/ioutil"
@@ -50,6 +51,27 @@ func (b *Blob) GetBlobContent() (string, error) {
5051
return string(buf), nil
5152
}
5253

54+
// GetGlobLineCount gets line count of lob as raw text
55+
func (b *Blob) GetGlobLineCount() (int, error) {
56+
reader, err := b.DataAsync()
57+
if err != nil {
58+
return 0, err
59+
}
60+
buf := make([]byte, 32*1024)
61+
count := 0
62+
lineSep := []byte{'\n'}
63+
for {
64+
c, err := reader.Read(buf)
65+
count += bytes.Count(buf[:c], lineSep)
66+
switch {
67+
case err == io.EOF:
68+
return count, nil
69+
case err != nil:
70+
return count, err
71+
}
72+
}
73+
}
74+
5375
// GetBlobContentBase64 Reads the content of the blob with a base64 encode and returns the encoded string
5476
func (b *Blob) GetBlobContentBase64() (string, error) {
5577
dataRc, err := b.DataAsync()

public/js/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,6 +2004,16 @@ function initCodeView() {
20042004
});
20052005
}
20062006
})
2007+
2008+
function insertBlobExcerpt(e){
2009+
const $blob = $(e.target);
2010+
const $row = $blob.parent().parent();
2011+
$.get($blob.data("url") + "?" + $blob.data("query") + "&anchor=" + $blob.data("anchor"), function(blob){
2012+
$row.replaceWith(blob);
2013+
$('[data-anchor="'+ $blob.data("anchor") +'"]').on('click', function(e){insertBlobExcerpt(e)});
2014+
})
2015+
}
2016+
$('.ui.blob-excerpt').on('click', function(e){insertBlobExcerpt(e)});
20072017
}
20082018

20092019
function initU2FAuth() {

routers/repo/commit.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ func Diff(ctx *context.Context) {
239239
}
240240

241241
ctx.Data["CommitID"] = commitID
242+
ctx.Data["AfterCommitID"] = commitID
242243
ctx.Data["Username"] = userName
243244
ctx.Data["Reponame"] = repoName
244245

routers/repo/compare.go

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,26 @@
55
package repo
66

77
import (
8+
"bufio"
89
"fmt"
10+
"html"
911
"path"
12+
"path/filepath"
1013
"strings"
1114

1215
"code.gitea.io/gitea/models"
1316
"code.gitea.io/gitea/modules/base"
1417
"code.gitea.io/gitea/modules/context"
1518
"code.gitea.io/gitea/modules/git"
19+
"code.gitea.io/gitea/modules/highlight"
1620
"code.gitea.io/gitea/modules/log"
1721
"code.gitea.io/gitea/modules/setting"
1822
"code.gitea.io/gitea/services/gitdiff"
1923
)
2024

2125
const (
22-
tplCompare base.TplName = "repo/diff/compare"
26+
tplCompare base.TplName = "repo/diff/compare"
27+
tplBlobExcerpt base.TplName = "repo/diff/blob_excerpt"
2328
)
2429

2530
// setPathsCompareContext sets context data for source and raw paths
@@ -423,3 +428,105 @@ func CompareDiff(ctx *context.Context) {
423428

424429
ctx.HTML(200, tplCompare)
425430
}
431+
432+
// ExcerptBlob render blob excerpt conten
433+
func ExcerptBlob(ctx *context.Context) {
434+
commitID := ctx.Params("sha")
435+
lastLeft := ctx.QueryInt("last_left")
436+
lastRight := ctx.QueryInt("last_right")
437+
idxLeft := ctx.QueryInt("left")
438+
idxRight := ctx.QueryInt("right")
439+
leftHunkSize := ctx.QueryInt("left_hunk_size")
440+
rightHunkSize := ctx.QueryInt("right_hunk_size")
441+
anchor := ctx.Query("anchor")
442+
direction := ctx.Query("direction")
443+
filePath := ctx.Query("path")
444+
gitRepo := ctx.Repo.GitRepo
445+
chunkSize := 20
446+
commit, err := gitRepo.GetCommit(commitID)
447+
if err != nil {
448+
ctx.Error(500, "GetCommit")
449+
return
450+
}
451+
section := &gitdiff.DiffSection{
452+
Name: filePath,
453+
}
454+
if direction == "up" && (idxLeft-lastLeft) > chunkSize {
455+
idxLeft -= chunkSize
456+
idxRight -= chunkSize
457+
leftHunkSize += chunkSize
458+
rightHunkSize += chunkSize
459+
section.Lines, err = getExcerptLines(commit, filePath, idxLeft-1, idxRight-1, chunkSize)
460+
} else if direction == "down" && (idxLeft-lastLeft) > chunkSize {
461+
section.Lines, err = getExcerptLines(commit, filePath, lastLeft, lastRight, chunkSize)
462+
lastLeft += chunkSize
463+
lastRight += chunkSize
464+
} else {
465+
section.Lines, err = getExcerptLines(commit, filePath, lastLeft, lastRight, idxRight-lastRight-1)
466+
}
467+
if err != nil {
468+
ctx.Error(500, "getExcerptLines")
469+
return
470+
}
471+
if idxRight > lastRight {
472+
lineText := " "
473+
if rightHunkSize > 0 || leftHunkSize > 0 {
474+
lineText = fmt.Sprintf("@@ -%d,%d +%d,%d @@\n", idxLeft, leftHunkSize, idxRight, rightHunkSize)
475+
}
476+
lineText = html.EscapeString(lineText)
477+
lineSection := &gitdiff.DiffLine{
478+
Type: gitdiff.DiffLineSection,
479+
Content: lineText,
480+
SectionInfo: &gitdiff.DiffLineSectionInfo{
481+
Path: filePath,
482+
LastLeftIdx: lastLeft,
483+
LastRightIdx: lastRight,
484+
LeftIdx: idxLeft,
485+
RightIdx: idxRight,
486+
LeftHunkSize: leftHunkSize,
487+
RightHunkSize: rightHunkSize,
488+
}}
489+
if direction == "up" {
490+
section.Lines = append([]*gitdiff.DiffLine{lineSection}, section.Lines...)
491+
} else if direction == "down" {
492+
section.Lines = append(section.Lines, lineSection)
493+
}
494+
}
495+
ctx.Data["section"] = section
496+
ctx.Data["fileName"] = filePath
497+
ctx.Data["highlightClass"] = highlight.FileNameToHighlightClass(filepath.Base(filePath))
498+
ctx.Data["AfterCommitID"] = commitID
499+
ctx.Data["Anchor"] = anchor
500+
ctx.HTML(200, tplBlobExcerpt)
501+
}
502+
503+
func getExcerptLines(commit *git.Commit, filePath string, idxLeft int, idxRight int, chunkSize int) ([]*gitdiff.DiffLine, error) {
504+
blob, err := commit.Tree.GetBlobByPath(filePath)
505+
if err != nil {
506+
return nil, err
507+
}
508+
reader, err := blob.DataAsync()
509+
defer reader.Close()
510+
if err != nil {
511+
return nil, err
512+
}
513+
scanner := bufio.NewScanner(reader)
514+
var diffLines []*gitdiff.DiffLine
515+
for line := 0; line < idxRight+chunkSize; line++ {
516+
if ok := scanner.Scan(); !ok {
517+
break
518+
}
519+
if line < idxRight {
520+
continue
521+
}
522+
lineText := scanner.Text()
523+
diffLine := &gitdiff.DiffLine{
524+
LeftIdx: idxLeft + (line - idxRight) + 1,
525+
RightIdx: line + 1,
526+
Type: gitdiff.DiffLinePlain,
527+
Content: " " + lineText,
528+
}
529+
diffLines = append(diffLines, diffLine)
530+
}
531+
return diffLines, nil
532+
}

routers/repo/pull.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ func ViewPullFiles(ctx *context.Context) {
540540
ctx.Data["Username"] = pull.MustHeadUserName()
541541
ctx.Data["Reponame"] = pull.HeadRepo.Name
542542
}
543+
ctx.Data["AfterCommitID"] = endCommitID
543544

544545
diff, err := gitdiff.GetDiffRangeWithWhitespaceBehavior(diffRepoPath,
545546
startCommitID, endCommitID, setting.Git.MaxGitDiffLines,

routers/routes/routes.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,10 @@ func RegisterRoutes(m *macaron.Macaron) {
864864
m.Get("", repo.Branches)
865865
}, repo.MustBeNotEmpty, context.RepoRef(), reqRepoCodeReader)
866866

867+
m.Group("/blob_excerpt", func() {
868+
m.Get("/:sha", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ExcerptBlob)
869+
}, repo.MustBeNotEmpty, context.RepoRef(), reqRepoCodeReader)
870+
867871
m.Group("/pulls/:index", func() {
868872
m.Get(".diff", repo.DownloadPullDiff)
869873
m.Get(".patch", repo.DownloadPullPatch)

0 commit comments

Comments
 (0)