Skip to content

Commit 926e918

Browse files
committed
gogs#2071 Diff is not showing full content when has super long one line
1 parent 91ae2ad commit 926e918

File tree

5 files changed

+77
-59
lines changed

5 files changed

+77
-59
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra
55

66
![](public/img/gogs-large-resize.png)
77

8-
##### Current version: 0.7.25 Beta
8+
##### Current version: 0.7.26 Beta
99

1010
<table>
1111
<tr>

gogs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/gogits/gogs/modules/setting"
1818
)
1919

20-
const APP_VER = "0.7.25.1202 Beta"
20+
const APP_VER = "0.7.26.1202 Beta"
2121

2222
func init() {
2323
runtime.GOMAXPROCS(runtime.NumCPU())

models/git_diff.go

Lines changed: 72 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@ import (
1212
"os"
1313
"os/exec"
1414
"strings"
15-
"time"
1615

16+
"github.com/Unknwon/com"
1717
"golang.org/x/net/html/charset"
1818
"golang.org/x/text/transform"
1919

20-
"github.com/Unknwon/com"
20+
"github.com/gogits/git-shell"
2121

2222
"github.com/gogits/gogs/modules/base"
23-
"github.com/gogits/gogs/modules/git"
2423
"github.com/gogits/gogs/modules/log"
2524
"github.com/gogits/gogs/modules/process"
2625
)
@@ -80,36 +79,50 @@ func (diff *Diff) NumFiles() int {
8079

8180
const DIFF_HEAD = "diff --git "
8281

83-
func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff, error) {
84-
scanner := bufio.NewScanner(reader)
82+
func ParsePatch(maxlines int, reader io.Reader) (*Diff, error) {
8583
var (
84+
diff = &Diff{Files: make([]*DiffFile, 0)}
85+
8686
curFile *DiffFile
8787
curSection = &DiffSection{
8888
Lines: make([]*DiffLine, 0, 10),
8989
}
9090

9191
leftLine, rightLine int
92-
// FIXME: Should use cache in the future.
93-
buf bytes.Buffer
92+
lineCount int
9493
)
9594

96-
diff := &Diff{Files: make([]*DiffFile, 0)}
97-
var i int
98-
for scanner.Scan() {
99-
line := scanner.Text()
95+
input := bufio.NewReader(reader)
96+
isEOF := false
97+
for {
98+
if isEOF {
99+
break
100+
}
100101

101-
if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") {
102-
continue
102+
line, err := input.ReadString('\n')
103+
if err != nil {
104+
if err == io.EOF {
105+
isEOF = true
106+
} else {
107+
return nil, fmt.Errorf("ReadString: %v", err)
108+
}
109+
}
110+
111+
if len(line) > 0 && line[len(line)-1] == '\n' {
112+
// Remove line break.
113+
line = line[:len(line)-1]
103114
}
104115

105-
if line == "" {
116+
if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") {
117+
continue
118+
} else if len(line) == 0 {
106119
continue
107120
}
108121

109-
i = i + 1
122+
lineCount++
110123

111124
// Diff data too large, we only show the first about maxlines lines
112-
if i >= maxlines {
125+
if lineCount >= maxlines {
113126
log.Warn("Diff data too large")
114127
diff.Files = nil
115128
return diff, nil
@@ -189,17 +202,26 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff
189202
diff.Files = append(diff.Files, curFile)
190203

191204
// Check file diff type.
192-
for scanner.Scan() {
205+
for {
206+
line, err := input.ReadString('\n')
207+
if err != nil {
208+
if err == io.EOF {
209+
isEOF = true
210+
} else {
211+
return nil, fmt.Errorf("ReadString: %v", err)
212+
}
213+
}
214+
193215
switch {
194-
case strings.HasPrefix(scanner.Text(), "new file"):
216+
case strings.HasPrefix(line, "new file"):
195217
curFile.Type = DIFF_FILE_ADD
196218
curFile.IsCreated = true
197-
case strings.HasPrefix(scanner.Text(), "deleted"):
219+
case strings.HasPrefix(line, "deleted"):
198220
curFile.Type = DIFF_FILE_DEL
199221
curFile.IsDeleted = true
200-
case strings.HasPrefix(scanner.Text(), "index"):
222+
case strings.HasPrefix(line, "index"):
201223
curFile.Type = DIFF_FILE_CHANGE
202-
case strings.HasPrefix(scanner.Text(), "similarity index 100%"):
224+
case strings.HasPrefix(line, "similarity index 100%"):
203225
curFile.Type = DIFF_FILE_RENAME
204226
curFile.IsRenamed = true
205227
curFile.OldName = curFile.Name
@@ -212,6 +234,8 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff
212234
}
213235
}
214236

237+
// FIXME: detect encoding while parsing.
238+
var buf bytes.Buffer
215239
for _, f := range diff.Files {
216240
buf.Reset()
217241
for _, sec := range f.Sections {
@@ -238,61 +262,55 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff
238262
return diff, nil
239263
}
240264

241-
func GetDiffRange(repoPath, beforeCommitId string, afterCommitId string, maxlines int) (*Diff, error) {
265+
func GetDiffRange(repoPath, beforeCommitID string, afterCommitID string, maxlines int) (*Diff, error) {
242266
repo, err := git.OpenRepository(repoPath)
243267
if err != nil {
244268
return nil, err
245269
}
246270

247-
commit, err := repo.GetCommit(afterCommitId)
271+
commit, err := repo.GetCommit(afterCommitID)
248272
if err != nil {
249273
return nil, err
250274
}
251275

252-
rd, wr := io.Pipe()
253276
var cmd *exec.Cmd
254277
// if "after" commit given
255-
if beforeCommitId == "" {
278+
if len(beforeCommitID) == 0 {
256279
// First commit of repository.
257280
if commit.ParentCount() == 0 {
258-
cmd = exec.Command("git", "show", afterCommitId)
281+
cmd = exec.Command("git", "show", afterCommitID)
259282
} else {
260283
c, _ := commit.Parent(0)
261-
cmd = exec.Command("git", "diff", "-M", c.ID.String(), afterCommitId)
284+
cmd = exec.Command("git", "diff", "-M", c.ID.String(), afterCommitID)
262285
}
263286
} else {
264-
cmd = exec.Command("git", "diff", "-M", beforeCommitId, afterCommitId)
287+
cmd = exec.Command("git", "diff", "-M", beforeCommitID, afterCommitID)
265288
}
266289
cmd.Dir = repoPath
267-
cmd.Stdout = wr
268-
cmd.Stdin = os.Stdin
269290
cmd.Stderr = os.Stderr
270291

271-
done := make(chan error)
272-
go func() {
273-
cmd.Start()
274-
done <- cmd.Wait()
275-
wr.Close()
276-
}()
277-
defer rd.Close()
278-
279-
desc := fmt.Sprintf("GetDiffRange(%s)", repoPath)
280-
pid := process.Add(desc, cmd)
281-
go func() {
282-
// In case process became zombie.
283-
select {
284-
case <-time.After(5 * time.Minute):
285-
if errKill := process.Kill(pid); errKill != nil {
286-
log.Error(4, "git_diff.ParsePatch(Kill): %v", err)
287-
}
288-
<-done
289-
// return "", ErrExecTimeout.Error(), ErrExecTimeout
290-
case err = <-done:
291-
process.Remove(pid)
292-
}
293-
}()
292+
stdout, err := cmd.StdoutPipe()
293+
if err != nil {
294+
return nil, fmt.Errorf("StdoutPipe: %v", err)
295+
}
296+
297+
if err = cmd.Start(); err != nil {
298+
return nil, fmt.Errorf("Start: %v", err)
299+
}
294300

295-
return ParsePatch(pid, maxlines, cmd, rd)
301+
pid := process.Add(fmt.Sprintf("GetDiffRange (%s)", repoPath), cmd)
302+
defer process.Remove(pid)
303+
304+
diff, err := ParsePatch(maxlines, stdout)
305+
if err != nil {
306+
return nil, fmt.Errorf("ParsePatch: %v", err)
307+
}
308+
309+
if err = cmd.Wait(); err != nil {
310+
return nil, fmt.Errorf("Wait: %v", err)
311+
}
312+
313+
return diff, nil
296314
}
297315

298316
func GetDiffCommit(repoPath, commitId string, maxlines int) (*Diff, error) {

modules/bindata/bindata.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/.VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.7.25.1202 Beta
1+
0.7.26.1202 Beta

0 commit comments

Comments
 (0)