Skip to content

Commit

Permalink
keep fixing some comments :)
Browse files Browse the repository at this point in the history
  • Loading branch information
Villaquiranm committed Oct 9, 2024
1 parent 44441cc commit 3550f0b
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 81 deletions.
102 changes: 76 additions & 26 deletions misc/stdlib_diff/filediff.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,35 @@ import (
"github.com/hexops/gotextdiff/span"
)

// FileDiff is a struct for comparing differences between two files.
type FileDiff struct {
// DiffChecker is a struct for comparing differences between two files.
type DiffChecker struct {
Src string // Name of the source file.
Dst string // Name of the destination file.
srcContent string // Content of the source file.
dstContent string // Content of the destination file.
srcLines []string // Lines of the source file.
dstLines []string // Lines of the destination file.

}

// LineDifferrence represents a difference in a line during file comparison.
type LineDifferrence struct {
Line string // The line content.
Operation operation // The operation performed on the line (e.g., "add", "delete", "equal").
Number int
SrcLine string // The line on Src.
DestLine string // The line on Src.
SrcOperation operation // The operation performed on the line (e.g., "add", "delete", "equal").
DestOperation operation
SrcNumber int
DestNumber int
}
type Diff struct {
Diffs []LineDifferrence
MissingSrc bool
MissingDst bool
}

// NewFileDiff creates a new FileDiff instance for comparing differences between
// NewDiffChecker creates a new DiffChecker instance for comparing differences between
// the specified source and destination files. It initializes the source and
// destination file lines .
func NewFileDiff(srcPath, dstPath string) (*FileDiff, error) {
func NewDiffChecker(srcPath, dstPath string) (*DiffChecker, error) {
src, err := getFileContent(srcPath)
if err != nil {
return nil, fmt.Errorf("can't read src file: %w", err)
Expand All @@ -42,7 +49,7 @@ func NewFileDiff(srcPath, dstPath string) (*FileDiff, error) {
return nil, fmt.Errorf("can't read dst file: %w", err)
}

return &FileDiff{
return &DiffChecker{
srcContent: src,
dstContent: dst,
srcLines: strings.Split(src, "\n"),
Expand All @@ -54,11 +61,11 @@ func NewFileDiff(srcPath, dstPath string) (*FileDiff, error) {

// Differences returns the differences in lines between the source and
// destination files using the configured diff algorithm.
func (f *FileDiff) Differences() (src, dst []LineDifferrence) {
func (f *DiffChecker) Differences() *Diff {
var (
srcIndex, dstIndex int
insertCount, deleteCount int
dstDiff, srcDiff []LineDifferrence
diff []LineDifferrence
)

if len(f.dstContent) == 0 {
Expand All @@ -77,8 +84,15 @@ func (f *FileDiff) Differences() (src, dst []LineDifferrence) {
*/
printUntil := func(until int) {
for i := srcIndex; i < until; i++ {
dstDiff = append(dstDiff, LineDifferrence{Line: f.srcLines[srcIndex], Operation: equal, Number: dstIndex + 1})
srcDiff = append(srcDiff, LineDifferrence{Line: f.srcLines[srcIndex], Operation: equal, Number: srcIndex + 1})
diff = append(diff, LineDifferrence{
SrcLine: f.srcLines[srcIndex],
DestLine: f.srcLines[srcIndex],
DestOperation: equal,
SrcOperation: equal,
SrcNumber: srcIndex + 1,
DestNumber: dstIndex + 1,
})

srcIndex++
dstIndex++
}
Expand All @@ -89,48 +103,84 @@ func (f *FileDiff) Differences() (src, dst []LineDifferrence) {
for _, hunk := range unified.Hunks {
printUntil(hunk.FromLine - 1)

currentLine := LineDifferrence{}
for _, line := range hunk.Lines {
switch line.Kind {
case gotextdiff.Insert:
if currentLine.DestLine != "" {
diff = append(diff, currentLine)
currentLine = LineDifferrence{}
}

insertCount++
dstIndex++
dstDiff = append(dstDiff, LineDifferrence{Line: line.Content, Operation: insert, Number: dstIndex})

currentLine.DestLine = line.Content
currentLine.DestOperation = insert
currentLine.DestNumber = dstIndex

case gotextdiff.Equal:
if currentLine.DestLine != "" || currentLine.SrcLine != "" {
diff = append(diff, currentLine)
currentLine = LineDifferrence{}
}

srcIndex++
dstIndex++
dstDiff = append(dstDiff, LineDifferrence{Line: line.Content, Operation: equal, Number: dstIndex})
srcDiff = append(srcDiff, LineDifferrence{Line: line.Content, Operation: equal, Number: srcIndex})

currentLine = LineDifferrence{
SrcLine: line.Content,
DestLine: line.Content,
DestOperation: equal,
SrcOperation: equal,
SrcNumber: srcIndex,
DestNumber: dstIndex,
}

case gotextdiff.Delete:
if currentLine.SrcLine != "" {
diff = append(diff, currentLine)
currentLine = LineDifferrence{}
}
srcIndex++
deleteCount++
srcDiff = append(srcDiff, LineDifferrence{Line: line.Content, Operation: delete, Number: srcIndex})
currentLine.SrcLine = line.Content
currentLine.SrcOperation = delete
currentLine.SrcNumber = srcIndex
}
}
diff = append(diff, currentLine)
}

printUntil(len(f.srcLines))

return srcDiff, dstDiff
return &Diff{
Diffs: diff,
}
}

func (f *FileDiff) destEmpty() ([]LineDifferrence, []LineDifferrence) {
srcDiff := []LineDifferrence{}
func (f *DiffChecker) destEmpty() *Diff {
diffs := []LineDifferrence{}
for index, line := range f.srcLines {
srcDiff = append(srcDiff, LineDifferrence{Line: line, Operation: delete, Number: index + 1})
diffs = append(diffs, LineDifferrence{SrcLine: line, SrcOperation: delete, SrcNumber: index + 1})
}

return srcDiff, make([]LineDifferrence, 0)
return &Diff{
Diffs: diffs,
MissingDst: true,
}
}

func (f *FileDiff) srcEmpty() ([]LineDifferrence, []LineDifferrence) {
destDiff := []LineDifferrence{}
func (f *DiffChecker) srcEmpty() *Diff {
diffs := []LineDifferrence{}
for index, line := range f.dstLines {
destDiff = append(destDiff, LineDifferrence{Line: line, Operation: insert, Number: index + 1})
diffs = append(diffs, LineDifferrence{DestLine: line, DestOperation: insert, DestNumber: index + 1})
}

return make([]LineDifferrence, 0), destDiff
return &Diff{
Diffs: diffs,
MissingSrc: true,
}
}

// getFileContent reads and returns the lines of a file given its path.
Expand Down
42 changes: 18 additions & 24 deletions misc/stdlib_diff/packagediff.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"os"
"slices"
"strings"
)

Expand All @@ -24,11 +23,10 @@ type Differences struct {

// FileDifference represents the differences between source and destination files.
type FileDifference struct {
Status string // Diff status of the processed files.
SourceName string // Name of the source file.
DestinationName string // Name of the destination file.
SrcLineDiff []LineDifferrence // Differences in source file lines.
DstLineDiff []LineDifferrence // Differences in destination file lines.
Status string // Diff status of the processed files.
SourceName string // Name of the source file.
DestinationName string // Name of the destination file.
LineDiffferrences []LineDifferrence // Differences in source file lines.
}

// NewPackageDiffChecker creates a new PackageDiffChecker instance with the specified
Expand Down Expand Up @@ -71,19 +69,18 @@ func (p *PackageDiffChecker) Differences() (*Differences, error) {
dstFileName := trimmedFileName + dstFileExt
dstFilePath := p.DstPath + "/" + dstFileName

fileDiff, err := NewFileDiff(srcFilePath, dstFilePath)
diffChecker, err := NewDiffChecker(srcFilePath, dstFilePath)
if err != nil {
return nil, err
}

srcDiff, dstDiff := fileDiff.Differences()
diff := diffChecker.Differences()

d.FilesDifferences = append(d.FilesDifferences, FileDifference{
Status: p.getStatus(srcDiff, dstDiff).String(),
SourceName: srcFileName,
DestinationName: dstFileName,
SrcLineDiff: srcDiff,
DstLineDiff: dstDiff,
Status: p.getStatus(diff).String(),
SourceName: srcFileName,
DestinationName: dstFileName,
LineDiffferrences: diff.Diffs,
})
}

Expand Down Expand Up @@ -126,25 +123,22 @@ func (p *PackageDiffChecker) inferFileExtensions() (string, string) {

// getStatus determines the diff status based on the differences in source and destination.
// It returns a diffStatus indicating whether there is no difference, missing in source, missing in destination, or differences exist.
func (p *PackageDiffChecker) getStatus(srcDiff, dstDiff []LineDifferrence) diffStatus {
slicesAreEquals := slices.Equal(srcDiff, dstDiff)
if slicesAreEquals {
return noDiff
}

if len(srcDiff) == 0 {
func (p *PackageDiffChecker) getStatus(diff *Diff) diffStatus {
if diff.MissingSrc {
return missingInSrc
}

if len(dstDiff) == 0 {
if diff.MissingDst {
return missingInDst
}

if !slicesAreEquals {
return hasDiff
for _, diff := range diff.Diffs {
if diff.SrcOperation == delete || diff.DestOperation == insert {
return hasDiff
}
}

return 0
return noDiff
}

// hasSameNumberOfFiles checks if the source and destination have the same number of files.
Expand Down
39 changes: 36 additions & 3 deletions misc/stdlib_diff/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,25 @@ func NewReportBuilder(srcPath, dstPath, outDir string, srcIsGno bool) (*ReportBu
return nil, err
}

realSrcPath, err := getRealPath(srcPath)
if err != nil {
return nil, err
}

realDstPath, err := getRealPath(dstPath)
if err != nil {
return nil, err
}

realOutPath, err := getRealPath(outDir)
if err != nil {
return nil, err
}
return &ReportBuilder{
// Trim suffix / in order to standardize paths accept path with or without `/`
SrcPath: strings.TrimSuffix(srcPath, `/`),
DstPath: strings.TrimSuffix(dstPath, `/`),
OutDir: strings.TrimSuffix(outDir, `/`),
SrcPath: strings.TrimSuffix(realSrcPath, `/`),
DstPath: strings.TrimSuffix(realDstPath, `/`),
OutDir: strings.TrimSuffix(realOutPath, `/`),
SrcIsGno: srcIsGno,
packageTemplate: packageTemplate,
indexTemplate: indexTemplate,
Expand Down Expand Up @@ -262,3 +276,22 @@ func (builder *ReportBuilder) writePackageTemplate(templateData any, packageName

return nil
}

// getRealPath will check if the directory is a symbolic link and resolve if path before returning it
func getRealPath(path string) (string, error) {
info, err := os.Lstat(path)
if err != nil {
return "", err
}

if info.Mode()&fs.ModeSymlink != 0 {
// File is symbolic link, no need to resolve
link, err := os.Readlink(path)
if err != nil {
return "", fmt.Errorf("can't resolve symbolic link: %w", err)
}
return link, nil
}

return path, nil
}
Loading

0 comments on commit 3550f0b

Please sign in to comment.