|
|
@ -70,7 +70,7 @@ func (diff *Diff) NumFiles() int { |
|
|
|
|
|
|
|
|
|
|
|
const DIFF_HEAD = "diff --git " |
|
|
|
const DIFF_HEAD = "diff --git " |
|
|
|
|
|
|
|
|
|
|
|
func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { |
|
|
|
func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { |
|
|
|
scanner := bufio.NewScanner(reader) |
|
|
|
scanner := bufio.NewScanner(reader) |
|
|
|
var ( |
|
|
|
var ( |
|
|
|
curFile *DiffFile |
|
|
|
curFile *DiffFile |
|
|
@ -79,6 +79,7 @@ func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
leftLine, rightLine int |
|
|
|
leftLine, rightLine int |
|
|
|
|
|
|
|
isTooLong bool |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
diff := &Diff{Files: make([]*DiffFile, 0)} |
|
|
|
diff := &Diff{Files: make([]*DiffFile, 0)} |
|
|
@ -90,16 +91,17 @@ func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { |
|
|
|
continue |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if line == "" { |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
i = i + 1 |
|
|
|
i = i + 1 |
|
|
|
|
|
|
|
|
|
|
|
// Diff data too large.
|
|
|
|
// Diff data too large, we only show the first about maxlines lines
|
|
|
|
if i == 5000 { |
|
|
|
if i == maxlines { |
|
|
|
|
|
|
|
isTooLong = true |
|
|
|
log.Warn("Diff data too large") |
|
|
|
log.Warn("Diff data too large") |
|
|
|
return &Diff{}, nil |
|
|
|
//return &Diff{}, nil
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if line == "" { |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
switch { |
|
|
|
switch { |
|
|
@ -110,6 +112,10 @@ func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { |
|
|
|
curSection.Lines = append(curSection.Lines, diffLine) |
|
|
|
curSection.Lines = append(curSection.Lines, diffLine) |
|
|
|
continue |
|
|
|
continue |
|
|
|
case line[0] == '@': |
|
|
|
case line[0] == '@': |
|
|
|
|
|
|
|
if isTooLong { |
|
|
|
|
|
|
|
return diff, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
curSection = &DiffSection{} |
|
|
|
curSection = &DiffSection{} |
|
|
|
curFile.Sections = append(curFile.Sections, curSection) |
|
|
|
curFile.Sections = append(curFile.Sections, curSection) |
|
|
|
ss := strings.Split(line, "@@") |
|
|
|
ss := strings.Split(line, "@@") |
|
|
@ -143,6 +149,10 @@ func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { |
|
|
|
|
|
|
|
|
|
|
|
// Get new file.
|
|
|
|
// Get new file.
|
|
|
|
if strings.HasPrefix(line, DIFF_HEAD) { |
|
|
|
if strings.HasPrefix(line, DIFF_HEAD) { |
|
|
|
|
|
|
|
if isTooLong { |
|
|
|
|
|
|
|
return diff, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fs := strings.Split(line[len(DIFF_HEAD):], " ") |
|
|
|
fs := strings.Split(line[len(DIFF_HEAD):], " ") |
|
|
|
a := fs[0] |
|
|
|
a := fs[0] |
|
|
|
|
|
|
|
|
|
|
@ -174,7 +184,7 @@ func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { |
|
|
|
return diff, nil |
|
|
|
return diff, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func GetDiffRange(repoPath, beforeCommitId string, afterCommitId string) (*Diff, error) { |
|
|
|
func GetDiffRange(repoPath, beforeCommitId string, afterCommitId string, maxlines int) (*Diff, error) { |
|
|
|
repo, err := git.OpenRepository(repoPath) |
|
|
|
repo, err := git.OpenRepository(repoPath) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
@ -228,9 +238,9 @@ func GetDiffRange(repoPath, beforeCommitId string, afterCommitId string) (*Diff, |
|
|
|
} |
|
|
|
} |
|
|
|
}() |
|
|
|
}() |
|
|
|
|
|
|
|
|
|
|
|
return ParsePatch(pid, cmd, rd) |
|
|
|
return ParsePatch(pid, maxlines, cmd, rd) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func GetDiffCommit(repoPath, commitId string) (*Diff, error) { |
|
|
|
func GetDiffCommit(repoPath, commitId string, maxlines int) (*Diff, error) { |
|
|
|
return GetDiffRange(repoPath, "", commitId) |
|
|
|
return GetDiffRange(repoPath, "", commitId, maxlines) |
|
|
|
} |
|
|
|
} |
|
|
|