[maint] diff with json response contain diff with headers (#1035)

This commit is contained in:
Enver Bisevac 2024-02-09 12:20:23 +00:00 committed by Harness
parent a937793edb
commit 8e0b35c0ce
2 changed files with 48 additions and 30 deletions

View File

@ -446,20 +446,11 @@ func (s *Service) Diff(
defer pr.Close() defer pr.Close()
parser := diff.Parser{ parser := diff.Parser{
Reader: bufio.NewReader(pr), Reader: bufio.NewReader(pr),
IncludePatch: params.IncludePatch,
} }
err := parser.Parse(func(f *diff.File) error { err := parser.Parse(func(f *diff.File) error {
patch := bytes.Buffer{}
if params.IncludePatch {
for _, sec := range f.Sections {
for _, line := range sec.Lines {
if line.Type != diff.DiffLinePlain {
patch.WriteString(line.Content)
}
}
}
}
ch <- &FileDiff{ ch <- &FileDiff{
SHA: f.SHA, SHA: f.SHA,
OldSHA: f.OldSHA, OldSHA: f.OldSHA,
@ -469,7 +460,7 @@ func (s *Service) Diff(
Additions: int64(f.NumAdditions()), Additions: int64(f.NumAdditions()),
Deletions: int64(f.NumDeletions()), Deletions: int64(f.NumDeletions()),
Changes: int64(f.NumChanges()), Changes: int64(f.NumChanges()),
Patch: patch.Bytes(), Patch: f.Patch.Bytes(),
IsBinary: f.IsBinary, IsBinary: f.IsBinary,
IsSubmodule: f.IsSubmodule, IsSubmodule: f.IsSubmodule,
} }

View File

@ -148,6 +148,7 @@ type File struct {
IsBinary bool IsBinary bool
IsSubmodule bool IsSubmodule bool
Patch bytes.Buffer
} }
func (f *File) Status() string { func (f *File) Status() string {
@ -206,18 +207,20 @@ type Parser struct {
// of process should go in. // of process should go in.
buffer []byte buffer []byte
isEOF bool isEOF bool
IncludePatch bool
Patch bytes.Buffer
} }
func (p *Parser) readLine() error { func (p *Parser) readLine() (newLine bool, err error) {
if p.buffer != nil { if p.buffer != nil {
return nil return false, nil
} }
var err error
p.buffer, err = p.ReadBytes('\n') p.buffer, err = p.ReadBytes('\n')
if err != nil { if err != nil {
if !errors.Is(err, io.EOF) { if !errors.Is(err, io.EOF) {
return fmt.Errorf("read string: %w", err) return false, fmt.Errorf("read string: %w", err)
} }
p.isEOF = true p.isEOF = true
@ -225,16 +228,22 @@ func (p *Parser) readLine() error {
// Remove line break // Remove line break
if len(p.buffer) > 0 && p.buffer[len(p.buffer)-1] == '\n' { if len(p.buffer) > 0 && p.buffer[len(p.buffer)-1] == '\n' {
newLine = true
p.buffer = p.buffer[:len(p.buffer)-1] p.buffer = p.buffer[:len(p.buffer)-1]
} }
return nil return newLine, nil
} }
var diffHead = []byte("diff --git ") var diffHead = []byte("diff --git ")
//nolint:gocognit //nolint:gocognit
func (p *Parser) parseFileHeader() (*File, error) { func (p *Parser) parseFileHeader() (*File, error) {
p.Patch.Reset()
submoduleMode := " 160000" submoduleMode := " 160000"
if p.IncludePatch && len(p.buffer) > 0 {
p.Patch.Write(p.buffer)
p.Patch.Write([]byte{'\n'})
}
line := string(p.buffer) line := string(p.buffer)
p.buffer = nil p.buffer = nil
@ -260,14 +269,20 @@ func (p *Parser) parseFileHeader() (*File, error) {
Type: FileChange, Type: FileChange,
} }
// Check file diff type and submodule
var err error
checkType: checkType:
for !p.isEOF { for !p.isEOF {
if err = p.readLine(); err != nil { newLine, err := p.readLine()
if err != nil {
return nil, err return nil, err
} }
if p.IncludePatch && len(p.buffer) > 0 {
p.Patch.Write(p.buffer)
if newLine {
p.Patch.Write([]byte{'\n'})
}
}
subLine := string(p.buffer) subLine := string(p.buffer)
p.buffer = nil p.buffer = nil
@ -365,9 +380,9 @@ func (p *Parser) parseSection() (*Section, error) {
rightLine = leftLine rightLine = leftLine
} }
var err error
for !p.isEOF { for !p.isEOF {
if err = p.readLine(); err != nil { newLine, err := p.readLine()
if err != nil {
return nil, err return nil, err
} }
@ -389,6 +404,13 @@ func (p *Parser) parseSection() (*Section, error) {
return section, nil return section, nil
} }
if p.IncludePatch && len(p.buffer) > 0 {
p.Patch.Write(p.buffer)
if newLine {
p.Patch.Write([]byte{'\n'})
}
}
subLine := string(p.buffer) subLine := string(p.buffer)
p.buffer = nil p.buffer = nil
@ -433,17 +455,13 @@ func (p *Parser) Parse(send func(f *File) error) error {
additions := 0 additions := 0
deletions := 0 deletions := 0
var (
err error
)
for !p.isEOF { for !p.isEOF {
if err = p.readLine(); err != nil { newLine, err := p.readLine()
if err != nil {
return err return err
} }
if len(p.buffer) == 0 || if len(p.buffer) == 0 {
bytes.HasPrefix(p.buffer, []byte("+++ ")) ||
bytes.HasPrefix(p.buffer, []byte("--- ")) {
p.buffer = nil p.buffer = nil
continue continue
} }
@ -452,6 +470,7 @@ func (p *Parser) Parse(send func(f *File) error) error {
if bytes.HasPrefix(p.buffer, diffHead) { if bytes.HasPrefix(p.buffer, diffHead) {
// stream previous file // stream previous file
if !file.IsEmpty() && send != nil { if !file.IsEmpty() && send != nil {
_, _ = p.Patch.WriteTo(&file.Patch)
err = send(file) err = send(file)
if err != nil { if err != nil {
return fmt.Errorf("failed to send out file: %w", err) return fmt.Errorf("failed to send out file: %w", err)
@ -471,6 +490,13 @@ func (p *Parser) Parse(send func(f *File) error) error {
continue continue
} }
if p.IncludePatch && len(p.buffer) > 0 {
p.Patch.Write(p.buffer)
if newLine {
p.Patch.Write([]byte{'\n'})
}
}
if bytes.HasPrefix(p.buffer, []byte("Binary")) { if bytes.HasPrefix(p.buffer, []byte("Binary")) {
p.buffer = nil p.buffer = nil
file.IsBinary = true file.IsBinary = true
@ -497,7 +523,8 @@ func (p *Parser) Parse(send func(f *File) error) error {
// stream last file // stream last file
if !file.IsEmpty() && send != nil { if !file.IsEmpty() && send != nil {
err = send(file) file.Patch.Write(p.Patch.Bytes())
err := send(file)
if err != nil { if err != nil {
return fmt.Errorf("failed to send last file: %w", err) return fmt.Errorf("failed to send last file: %w", err)
} }